• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • widgets
kmessagewidget.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  *
3  * Copyright (c) 2011 Aurélien Gâteau <agateau@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  */
20 #include "kmessagewidget.h"
21 
22 #include <kaction.h>
23 #include <kcolorscheme.h>
24 #include <kdebug.h>
25 #include <kglobalsettings.h>
26 #include <kicon.h>
27 #include <kiconloader.h>
28 #include <kstandardaction.h>
29 
30 #include <QEvent>
31 #include <QGridLayout>
32 #include <QHBoxLayout>
33 #include <QLabel>
34 #include <QPainter>
35 #include <QShowEvent>
36 #include <QTimeLine>
37 #include <QToolButton>
38 #include <QStyle>
39 
40 //---------------------------------------------------------------------
41 // KMessageWidgetPrivate
42 //---------------------------------------------------------------------
43 class KMessageWidgetPrivate
44 {
45 public:
46  void init(KMessageWidget*);
47 
48  KMessageWidget* q;
49  QFrame* content;
50  QLabel* iconLabel;
51  QLabel* textLabel;
52  QToolButton* closeButton;
53  QTimeLine* timeLine;
54 
55  KMessageWidget::MessageType messageType;
56  bool wordWrap;
57  QList<QToolButton*> buttons;
58  QPixmap contentSnapShot;
59 
60  void createLayout();
61  void updateSnapShot();
62  void updateLayout();
63  void slotTimeLineChanged(qreal);
64  void slotTimeLineFinished();
65 
66  int bestContentHeight() const;
67 };
68 
69 void KMessageWidgetPrivate::init(KMessageWidget *q_ptr)
70 {
71  q = q_ptr;
72 
73  q->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
74 
75  timeLine = new QTimeLine(500, q);
76  QObject::connect(timeLine, SIGNAL(valueChanged(qreal)), q, SLOT(slotTimeLineChanged(qreal)));
77  QObject::connect(timeLine, SIGNAL(finished()), q, SLOT(slotTimeLineFinished()));
78 
79  content = new QFrame(q);
80  content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
81 
82  wordWrap = false;
83 
84  iconLabel = new QLabel(content);
85  iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
86 
87  textLabel = new QLabel(content);
88  textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
89  textLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
90  QObject::connect(textLabel, SIGNAL(linkActivated(const QString&)), q, SIGNAL(linkActivated(const QString&)));
91 
92  KAction* closeAction = KStandardAction::close(q, SLOT(animatedHide()), q);
93 
94  closeButton = new QToolButton(content);
95  closeButton->setAutoRaise(true);
96  closeButton->setDefaultAction(closeAction);
97 
98  q->setMessageType(KMessageWidget::Information);
99 }
100 
101 void KMessageWidgetPrivate::createLayout()
102 {
103  delete content->layout();
104 
105  content->resize(q->size());
106 
107  qDeleteAll(buttons);
108  buttons.clear();
109 
110  Q_FOREACH(QAction* action, q->actions()) {
111  QToolButton* button = new QToolButton(content);
112  button->setDefaultAction(action);
113  button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
114  buttons.append(button);
115  }
116 
117  // AutoRaise reduces visual clutter, but we don't want to turn it on if
118  // there are other buttons, otherwise the close button will look different
119  // from the others.
120  closeButton->setAutoRaise(buttons.isEmpty());
121 
122  if (wordWrap) {
123  QGridLayout* layout = new QGridLayout(content);
124  // Set alignment to make sure icon does not move down if text wraps
125  layout->addWidget(iconLabel, 0, 0, 1, 1, Qt::AlignHCenter | Qt::AlignTop);
126  layout->addWidget(textLabel, 0, 1);
127 
128  QHBoxLayout* buttonLayout = new QHBoxLayout;
129  buttonLayout->addStretch();
130  Q_FOREACH(QToolButton* button, buttons) {
131  // For some reason, calling show() is necessary if wordwrap is true,
132  // otherwise the buttons do not show up. It is not needed if
133  // wordwrap is false.
134  button->show();
135  buttonLayout->addWidget(button);
136  }
137  buttonLayout->addWidget(closeButton);
138  layout->addItem(buttonLayout, 1, 0, 1, 2);
139  } else {
140  QHBoxLayout* layout = new QHBoxLayout(content);
141  layout->addWidget(iconLabel);
142  layout->addWidget(textLabel);
143 
144  Q_FOREACH(QToolButton* button, buttons) {
145  layout->addWidget(button);
146  }
147 
148  layout->addWidget(closeButton);
149  };
150 
151  if (q->isVisible()) {
152  q->setFixedHeight(content->sizeHint().height());
153  }
154  q->updateGeometry();
155 }
156 
157 void KMessageWidgetPrivate::updateLayout()
158 {
159  if (content->layout()) {
160  createLayout();
161  }
162 }
163 
164 void KMessageWidgetPrivate::updateSnapShot()
165 {
166  // Attention: updateSnapShot calls QWidget::render(), which causes the whole
167  // window layouts to be activated. Calling this method from resizeEvent()
168  // can lead to infinite recursion, see:
169  // https://bugs.kde.org/show_bug.cgi?id=311336
170  contentSnapShot = QPixmap(content->size());
171  contentSnapShot.fill(Qt::transparent);
172  content->render(&contentSnapShot, QPoint(), QRegion(), QWidget::DrawChildren);
173 }
174 
175 void KMessageWidgetPrivate::slotTimeLineChanged(qreal value)
176 {
177  q->setFixedHeight(qMin(value * 2, qreal(1.0)) * content->height());
178  q->update();
179 }
180 
181 void KMessageWidgetPrivate::slotTimeLineFinished()
182 {
183  if (timeLine->direction() == QTimeLine::Forward) {
184  // Show
185  // We set the whole geometry here, because it may be wrong if a
186  // KMessageWidget is shown right when the toplevel window is created.
187  content->setGeometry(0, 0, q->width(), bestContentHeight());
188  } else {
189  // Hide
190  q->hide();
191  }
192 }
193 
194 int KMessageWidgetPrivate::bestContentHeight() const
195 {
196  int height = content->heightForWidth(q->width());
197  if (height == -1) {
198  height = content->sizeHint().height();
199  }
200  return height;
201 }
202 
203 
204 //---------------------------------------------------------------------
205 // KMessageWidget
206 //---------------------------------------------------------------------
207 KMessageWidget::KMessageWidget(QWidget* parent)
208  : QFrame(parent)
209  , d(new KMessageWidgetPrivate)
210 {
211  d->init(this);
212 }
213 
214 KMessageWidget::KMessageWidget(const QString& text, QWidget* parent)
215  : QFrame(parent)
216  , d(new KMessageWidgetPrivate)
217 {
218  d->init(this);
219  setText(text);
220 }
221 
222 KMessageWidget::~KMessageWidget()
223 {
224  delete d;
225 }
226 
227 QString KMessageWidget::text() const
228 {
229  return d->textLabel->text();
230 }
231 
232 void KMessageWidget::setText(const QString& text)
233 {
234  d->textLabel->setText(text);
235  updateGeometry();
236 }
237 
238 KMessageWidget::MessageType KMessageWidget::messageType() const
239 {
240  return d->messageType;
241 }
242 
243 static void getColorsFromColorScheme(KColorScheme::BackgroundRole bgRole, QColor* bg, QColor* fg)
244 {
245  KColorScheme scheme(QPalette::Active, KColorScheme::Window);
246  *bg = scheme.background(bgRole).color();
247  *fg = scheme.foreground().color();
248 }
249 
250 void KMessageWidget::setMessageType(KMessageWidget::MessageType type)
251 {
252  d->messageType = type;
253  KIcon icon;
254  QColor bg0, bg1, bg2, border, fg;
255  switch (type) {
256  case Positive:
257  icon = KIcon("dialog-ok");
258  getColorsFromColorScheme(KColorScheme::PositiveBackground, &bg1, &fg);
259  break;
260  case Information:
261  icon = KIcon("dialog-information");
262  // There is no "information" background role in KColorScheme, use the
263  // colors of highlighted items instead
264  bg1 = palette().highlight().color();
265  fg = palette().highlightedText().color();
266  break;
267  case Warning:
268  icon = KIcon("dialog-warning");
269  getColorsFromColorScheme(KColorScheme::NeutralBackground, &bg1, &fg);
270  break;
271  case Error:
272  icon = KIcon("dialog-error");
273  getColorsFromColorScheme(KColorScheme::NegativeBackground, &bg1, &fg);
274  break;
275  }
276 
277  // Colors
278  bg0 = bg1.lighter(110);
279  bg2 = bg1.darker(110);
280  border = KColorScheme::shade(bg1, KColorScheme::DarkShade);
281 
282  d->content->setStyleSheet(
283  QString(".QFrame {"
284  "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
285  " stop: 0 %1,"
286  " stop: 0.1 %2,"
287  " stop: 1.0 %3);"
288  "border-radius: 5px;"
289  "border: 1px solid %4;"
290  "margin: %5px;"
291  "}"
292  ".QLabel { color: %6; }"
293  )
294  .arg(bg0.name())
295  .arg(bg1.name())
296  .arg(bg2.name())
297  .arg(border.name())
298  // DefaultFrameWidth returns the size of the external margin + border width. We know our border is 1px, so we subtract this from the frame normal QStyle FrameWidth to get our margin
299  .arg(style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this) -1)
300  .arg(fg.name())
301  );
302 
303  // Icon
304  const int size = KIconLoader::global()->currentSize(KIconLoader::MainToolbar);
305  d->iconLabel->setPixmap(icon.pixmap(size));
306 }
307 
308 QSize KMessageWidget::sizeHint() const
309 {
310  ensurePolished();
311  return d->content->sizeHint();
312 }
313 
314 QSize KMessageWidget::minimumSizeHint() const
315 {
316  ensurePolished();
317  return d->content->minimumSizeHint();
318 }
319 
320 bool KMessageWidget::event(QEvent* event)
321 {
322  if (event->type() == QEvent::Polish && !d->content->layout()) {
323  d->createLayout();
324  }
325  return QFrame::event(event);
326 }
327 
328 void KMessageWidget::resizeEvent(QResizeEvent* event)
329 {
330  QFrame::resizeEvent(event);
331 
332  if (d->timeLine->state() == QTimeLine::NotRunning) {
333  d->content->resize(width(), d->bestContentHeight());
334  }
335 }
336 
337 int KMessageWidget::heightForWidth(int width) const
338 {
339  ensurePolished();
340  return d->content->heightForWidth(width);
341 }
342 
343 void KMessageWidget::paintEvent(QPaintEvent* event)
344 {
345  QFrame::paintEvent(event);
346  if (d->timeLine->state() == QTimeLine::Running) {
347  QPainter painter(this);
348  painter.setOpacity(d->timeLine->currentValue() * d->timeLine->currentValue());
349  painter.drawPixmap(0, 0, d->contentSnapShot);
350  }
351 }
352 
353 void KMessageWidget::showEvent(QShowEvent* event)
354 {
355  // Keep this method here to avoid breaking binary compatibility:
356  // QFrame::showEvent() used to be reimplemented.
357  QFrame::showEvent(event);
358 }
359 
360 bool KMessageWidget::wordWrap() const
361 {
362  return d->wordWrap;
363 }
364 
365 void KMessageWidget::setWordWrap(bool wordWrap)
366 {
367  d->wordWrap = wordWrap;
368  d->textLabel->setWordWrap(wordWrap);
369  QSizePolicy policy = sizePolicy();
370  policy.setHeightForWidth(wordWrap);
371  setSizePolicy(policy);
372  d->updateLayout();
373  // Without this, when user does wordWrap -> !wordWrap -> wordWrap, a minimum
374  // height is set, causing the widget to be too high.
375  // Mostly visible in test programs.
376  if (wordWrap) {
377  setMinimumHeight(0);
378  }
379 }
380 
381 bool KMessageWidget::isCloseButtonVisible() const
382 {
383  return d->closeButton->isVisible();
384 }
385 
386 void KMessageWidget::setCloseButtonVisible(bool show)
387 {
388  d->closeButton->setVisible(show);
389  updateGeometry();
390 }
391 
392 void KMessageWidget::addAction(QAction* action)
393 {
394  QFrame::addAction(action);
395  d->updateLayout();
396 }
397 
398 void KMessageWidget::removeAction(QAction* action)
399 {
400  QFrame::removeAction(action);
401  d->updateLayout();
402 }
403 
404 void KMessageWidget::animatedShow()
405 {
406  if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
407  show();
408  return;
409  }
410 
411  if (isVisible()) {
412  return;
413  }
414 
415  QFrame::show();
416  setFixedHeight(0);
417  int wantedHeight = d->bestContentHeight();
418  d->content->setGeometry(0, -wantedHeight, width(), wantedHeight);
419 
420  d->updateSnapShot();
421 
422  d->timeLine->setDirection(QTimeLine::Forward);
423  if (d->timeLine->state() == QTimeLine::NotRunning) {
424  d->timeLine->start();
425  }
426 }
427 
428 void KMessageWidget::animatedHide()
429 {
430  if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
431  hide();
432  return;
433  }
434 
435  if (!isVisible()) {
436  return;
437  }
438 
439  d->content->move(0, -d->content->height());
440  d->updateSnapShot();
441 
442  d->timeLine->setDirection(QTimeLine::Backward);
443  if (d->timeLine->state() == QTimeLine::NotRunning) {
444  d->timeLine->start();
445  }
446 }
447 
448 #include "kmessagewidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Jul 16 2013 11:46:14 by doxygen 1.8.1.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.10.5 API Reference

Skip menu "kdelibs-4.10.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal