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

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • 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  QIcon icon;
55 
56  KMessageWidget::MessageType messageType;
57  bool wordWrap;
58  QList<QToolButton*> buttons;
59  QPixmap contentSnapShot;
60 
61  void createLayout();
62  void updateSnapShot();
63  void updateLayout();
64  void slotTimeLineChanged(qreal);
65  void slotTimeLineFinished();
66 
67  int bestContentHeight() const;
68 };
69 
70 void KMessageWidgetPrivate::init(KMessageWidget *q_ptr)
71 {
72  q = q_ptr;
73 
74  q->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
75 
76  timeLine = new QTimeLine(500, q);
77  QObject::connect(timeLine, SIGNAL(valueChanged(qreal)), q, SLOT(slotTimeLineChanged(qreal)));
78  QObject::connect(timeLine, SIGNAL(finished()), q, SLOT(slotTimeLineFinished()));
79 
80  content = new QFrame(q);
81  content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
82 
83  wordWrap = false;
84 
85  iconLabel = new QLabel(content);
86  iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
87  iconLabel->hide();
88 
89  textLabel = new QLabel(content);
90  textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
91  textLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
92  QObject::connect(textLabel, SIGNAL(linkActivated(QString)), q, SIGNAL(linkActivated(QString)));
93  QObject::connect(textLabel, SIGNAL(linkHovered(QString)), q, SIGNAL(linkHovered(QString)));
94 
95  KAction* closeAction = KStandardAction::close(q, SLOT(animatedHide()), q);
96 
97  closeButton = new QToolButton(content);
98  closeButton->setAutoRaise(true);
99  closeButton->setDefaultAction(closeAction);
100 
101  q->setMessageType(KMessageWidget::Information);
102 }
103 
104 void KMessageWidgetPrivate::createLayout()
105 {
106  delete content->layout();
107 
108  content->resize(q->size());
109 
110  qDeleteAll(buttons);
111  buttons.clear();
112 
113  Q_FOREACH(QAction* action, q->actions()) {
114  QToolButton* button = new QToolButton(content);
115  button->setDefaultAction(action);
116  button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
117  buttons.append(button);
118  }
119 
120  // AutoRaise reduces visual clutter, but we don't want to turn it on if
121  // there are other buttons, otherwise the close button will look different
122  // from the others.
123  closeButton->setAutoRaise(buttons.isEmpty());
124 
125  if (wordWrap) {
126  QGridLayout* layout = new QGridLayout(content);
127  // Set alignment to make sure icon does not move down if text wraps
128  layout->addWidget(iconLabel, 0, 0, 1, 1, Qt::AlignHCenter | Qt::AlignTop);
129  layout->addWidget(textLabel, 0, 1);
130 
131  QHBoxLayout* buttonLayout = new QHBoxLayout;
132  buttonLayout->addStretch();
133  Q_FOREACH(QToolButton* button, buttons) {
134  // For some reason, calling show() is necessary if wordwrap is true,
135  // otherwise the buttons do not show up. It is not needed if
136  // wordwrap is false.
137  button->show();
138  buttonLayout->addWidget(button);
139  }
140  buttonLayout->addWidget(closeButton);
141  layout->addItem(buttonLayout, 1, 0, 1, 2);
142  } else {
143  QHBoxLayout* layout = new QHBoxLayout(content);
144  layout->addWidget(iconLabel);
145  layout->addWidget(textLabel);
146 
147  Q_FOREACH(QToolButton* button, buttons) {
148  layout->addWidget(button);
149  }
150 
151  layout->addWidget(closeButton);
152  };
153 
154  if (q->isVisible()) {
155  q->setFixedHeight(content->sizeHint().height());
156  }
157  q->updateGeometry();
158 }
159 
160 void KMessageWidgetPrivate::updateLayout()
161 {
162  if (content->layout()) {
163  createLayout();
164  }
165 }
166 
167 void KMessageWidgetPrivate::updateSnapShot()
168 {
169  // Attention: updateSnapShot calls QWidget::render(), which causes the whole
170  // window layouts to be activated. Calling this method from resizeEvent()
171  // can lead to infinite recursion, see:
172  // https://bugs.kde.org/show_bug.cgi?id=311336
173  contentSnapShot = QPixmap(content->size());
174  contentSnapShot.fill(Qt::transparent);
175  content->render(&contentSnapShot, QPoint(), QRegion(), QWidget::DrawChildren);
176 }
177 
178 void KMessageWidgetPrivate::slotTimeLineChanged(qreal value)
179 {
180  q->setFixedHeight(qMin(value * 2, qreal(1.0)) * content->height());
181  q->update();
182 }
183 
184 void KMessageWidgetPrivate::slotTimeLineFinished()
185 {
186  if (timeLine->direction() == QTimeLine::Forward) {
187  // Show
188  // We set the whole geometry here, because it may be wrong if a
189  // KMessageWidget is shown right when the toplevel window is created.
190  content->setGeometry(0, 0, q->width(), bestContentHeight());
191  } else {
192  // Hide
193  q->hide();
194  }
195 }
196 
197 int KMessageWidgetPrivate::bestContentHeight() const
198 {
199  int height = content->heightForWidth(q->width());
200  if (height == -1) {
201  height = content->sizeHint().height();
202  }
203  return height;
204 }
205 
206 
207 //---------------------------------------------------------------------
208 // KMessageWidget
209 //---------------------------------------------------------------------
210 KMessageWidget::KMessageWidget(QWidget* parent)
211  : QFrame(parent)
212  , d(new KMessageWidgetPrivate)
213 {
214  d->init(this);
215 }
216 
217 KMessageWidget::KMessageWidget(const QString& text, QWidget* parent)
218  : QFrame(parent)
219  , d(new KMessageWidgetPrivate)
220 {
221  d->init(this);
222  setText(text);
223 }
224 
225 KMessageWidget::~KMessageWidget()
226 {
227  delete d;
228 }
229 
230 QString KMessageWidget::text() const
231 {
232  return d->textLabel->text();
233 }
234 
235 void KMessageWidget::setText(const QString& text)
236 {
237  d->textLabel->setText(text);
238  updateGeometry();
239 }
240 
241 KMessageWidget::MessageType KMessageWidget::messageType() const
242 {
243  return d->messageType;
244 }
245 
246 static void getColorsFromColorScheme(KColorScheme::BackgroundRole bgRole, QColor* bg, QColor* fg)
247 {
248  KColorScheme scheme(QPalette::Active, KColorScheme::Window);
249  *bg = scheme.background(bgRole).color();
250  *fg = scheme.foreground().color();
251 }
252 
253 void KMessageWidget::setMessageType(KMessageWidget::MessageType type)
254 {
255  d->messageType = type;
256  QColor bg0, bg1, bg2, border, fg;
257  switch (type) {
258  case Positive:
259  getColorsFromColorScheme(KColorScheme::PositiveBackground, &bg1, &fg);
260  break;
261  case 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  getColorsFromColorScheme(KColorScheme::NeutralBackground, &bg1, &fg);
269  break;
270  case Error:
271  getColorsFromColorScheme(KColorScheme::NegativeBackground, &bg1, &fg);
272  break;
273  }
274 
275  // Colors
276  bg0 = bg1.lighter(110);
277  bg2 = bg1.darker(110);
278  border = KColorScheme::shade(bg1, KColorScheme::DarkShade);
279 
280  d->content->setStyleSheet(
281  QString(".QFrame {"
282  "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
283  " stop: 0 %1,"
284  " stop: 0.1 %2,"
285  " stop: 1.0 %3);"
286  "border-radius: 5px;"
287  "border: 1px solid %4;"
288  "margin: %5px;"
289  "}"
290  ".QLabel { color: %6; }"
291  )
292  .arg(bg0.name())
293  .arg(bg1.name())
294  .arg(bg2.name())
295  .arg(border.name())
296  // 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
297  .arg(style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this) -1)
298  .arg(fg.name())
299  );
300 }
301 
302 QSize KMessageWidget::sizeHint() const
303 {
304  ensurePolished();
305  return d->content->sizeHint();
306 }
307 
308 QSize KMessageWidget::minimumSizeHint() const
309 {
310  ensurePolished();
311  return d->content->minimumSizeHint();
312 }
313 
314 bool KMessageWidget::event(QEvent* event)
315 {
316  if (event->type() == QEvent::Polish && !d->content->layout()) {
317  d->createLayout();
318  }
319  return QFrame::event(event);
320 }
321 
322 void KMessageWidget::resizeEvent(QResizeEvent* event)
323 {
324  QFrame::resizeEvent(event);
325 
326  if (d->timeLine->state() == QTimeLine::NotRunning) {
327  d->content->resize(width(), d->bestContentHeight());
328  }
329 }
330 
331 int KMessageWidget::heightForWidth(int width) const
332 {
333  ensurePolished();
334  return d->content->heightForWidth(width);
335 }
336 
337 void KMessageWidget::paintEvent(QPaintEvent* event)
338 {
339  QFrame::paintEvent(event);
340  if (d->timeLine->state() == QTimeLine::Running) {
341  QPainter painter(this);
342  painter.setOpacity(d->timeLine->currentValue() * d->timeLine->currentValue());
343  painter.drawPixmap(0, 0, d->contentSnapShot);
344  }
345 }
346 
347 void KMessageWidget::showEvent(QShowEvent* event)
348 {
349  // Keep this method here to avoid breaking binary compatibility:
350  // QFrame::showEvent() used to be reimplemented.
351  QFrame::showEvent(event);
352 }
353 
354 bool KMessageWidget::wordWrap() const
355 {
356  return d->wordWrap;
357 }
358 
359 void KMessageWidget::setWordWrap(bool wordWrap)
360 {
361  d->wordWrap = wordWrap;
362  d->textLabel->setWordWrap(wordWrap);
363  QSizePolicy policy = sizePolicy();
364  policy.setHeightForWidth(wordWrap);
365  setSizePolicy(policy);
366  d->updateLayout();
367  // Without this, when user does wordWrap -> !wordWrap -> wordWrap, a minimum
368  // height is set, causing the widget to be too high.
369  // Mostly visible in test programs.
370  if (wordWrap) {
371  setMinimumHeight(0);
372  }
373 }
374 
375 bool KMessageWidget::isCloseButtonVisible() const
376 {
377  return d->closeButton->isVisible();
378 }
379 
380 void KMessageWidget::setCloseButtonVisible(bool show)
381 {
382  d->closeButton->setVisible(show);
383  updateGeometry();
384 }
385 
386 void KMessageWidget::addAction(QAction* action)
387 {
388  QFrame::addAction(action);
389  d->updateLayout();
390 }
391 
392 void KMessageWidget::removeAction(QAction* action)
393 {
394  QFrame::removeAction(action);
395  d->updateLayout();
396 }
397 
398 void KMessageWidget::animatedShow()
399 {
400  if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
401  show();
402  return;
403  }
404 
405  if (isVisible()) {
406  return;
407  }
408 
409  QFrame::show();
410  setFixedHeight(0);
411  int wantedHeight = d->bestContentHeight();
412  d->content->setGeometry(0, -wantedHeight, width(), wantedHeight);
413 
414  d->updateSnapShot();
415 
416  d->timeLine->setDirection(QTimeLine::Forward);
417  if (d->timeLine->state() == QTimeLine::NotRunning) {
418  d->timeLine->start();
419  }
420 }
421 
422 void KMessageWidget::animatedHide()
423 {
424  if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
425  hide();
426  return;
427  }
428 
429  if (!isVisible()) {
430  return;
431  }
432 
433  d->content->move(0, -d->content->height());
434  d->updateSnapShot();
435 
436  d->timeLine->setDirection(QTimeLine::Backward);
437  if (d->timeLine->state() == QTimeLine::NotRunning) {
438  d->timeLine->start();
439  }
440 }
441 
442 QIcon KMessageWidget::icon() const
443 {
444  return d->icon;
445 }
446 
447 void KMessageWidget::setIcon(const QIcon& icon)
448 {
449  d->icon = icon;
450  if (d->icon.isNull()) {
451  d->iconLabel->hide();
452  } else {
453  const int size = KIconLoader::global()->currentSize(KIconLoader::MainToolbar);
454  d->iconLabel->setPixmap(d->icon.pixmap(size));
455  d->iconLabel->show();
456  }
457 }
458 
459 
460 #include "kmessagewidget.moc"
KMessageWidget::setWordWrap
void setWordWrap(bool wordWrap)
Definition: kmessagewidget.cpp:359
QColor
KMessageWidget::setCloseButtonVisible
void setCloseButtonVisible(bool visible)
Definition: kmessagewidget.cpp:380
KMessageWidget::Positive
Definition: kmessagewidget.h:104
KIconLoader::currentSize
int currentSize(KIconLoader::Group group) const
Returns the current size of the icon group.
Definition: kiconloader.cpp:1370
KColorScheme::background
QBrush background(BackgroundRole=NormalBackground) const
Retrieve the requested background brush.
Definition: kcolorscheme.cpp:454
kdebug.h
KIconLoader::global
static KIconLoader * global()
Returns the global icon loader initialized with the global KComponentData.
kglobalsettings.h
KMessageWidget::showEvent
void showEvent(QShowEvent *event)
Definition: kmessagewidget.cpp:347
KMessageWidget::text
QString text() const
KMessageWidget::KMessageWidget
KMessageWidget(QWidget *parent=0)
Constructs a KMessageWidget with the specified parent.
Definition: kmessagewidget.cpp:210
KMessageWidget::MessageType
MessageType
Definition: kmessagewidget.h:103
QWidget
KMessageWidget::addAction
void addAction(QAction *action)
Definition: kmessagewidget.cpp:386
KMessageWidget::messageType
MessageType messageType() const
kiconloader.h
KMessageWidget::paintEvent
void paintEvent(QPaintEvent *event)
Definition: kmessagewidget.cpp:337
QString
KMessageWidget::setIcon
void setIcon(const QIcon &icon)
Define an icon to be shown on the left of the text.
Definition: kmessagewidget.cpp:447
KColorScheme::Window
Non-editable window elements; for example, menus.
Definition: kcolorscheme.h:93
KMessageWidget::icon
QIcon icon() const
The icon shown on the left of the text.
KMessageWidget::animatedHide
void animatedHide()
Hide the widget using an animation, unless KGlobalSettings::graphicsEffectLevel() does not allow simp...
Definition: kmessagewidget.cpp:422
KColorScheme::DarkShade
The dark color is in between mid() and shadow().
Definition: kcolorscheme.h:292
KMessageWidget::Error
Definition: kmessagewidget.h:107
KMessageWidget::~KMessageWidget
~KMessageWidget()
Definition: kmessagewidget.cpp:225
KColorScheme::foreground
QBrush foreground(ForegroundRole=NormalText) const
Retrieve the requested foreground brush.
Definition: kcolorscheme.cpp:459
KMessageWidget::minimumSizeHint
QSize minimumSizeHint() const
Definition: kmessagewidget.cpp:308
KMessageWidget::event
bool event(QEvent *event)
Definition: kmessagewidget.cpp:314
KColorScheme::PositiveBackground
Eigth color; for example, success messages, trusted content.
Definition: kcolorscheme.h:180
kmessagewidget.h
KMessageWidget::isCloseButtonVisible
bool isCloseButtonVisible() const
Definition: kmessagewidget.cpp:375
KColorScheme::shade
QColor shade(ShadeRole) const
Retrieve the requested shade color, using KColorScheme::background(KColorScheme::NormalBackground) as...
Definition: kcolorscheme.cpp:469
KMessageWidget::removeAction
void removeAction(QAction *action)
Definition: kmessagewidget.cpp:392
kstandardaction.h
KMessageWidget::animatedShow
void animatedShow()
Show the widget using an animation, unless KGlobalSettings::graphicsEffectLevel() does not allow simp...
Definition: kmessagewidget.cpp:398
KMessageWidget::wordWrap
bool wordWrap() const
kaction.h
KIconLoader::MainToolbar
Main toolbar icons.
Definition: kiconloader.h:137
KColorScheme::NegativeBackground
Sixth color; for example, errors, untrusted content, etc.
Definition: kcolorscheme.h:172
KStandardAction::Forward
Definition: kstandardaction.h:141
QPoint
KColorScheme
A set of methods used to work with colors.
Definition: kcolorscheme.h:71
KMessageWidget::Warning
Definition: kmessagewidget.h:106
KAction
Class to encapsulate user-driven action or event.
Definition: kaction.h:216
QLabel
KMessageWidget::setMessageType
void setMessageType(KMessageWidget::MessageType type)
Definition: kmessagewidget.cpp:253
KColorScheme::BackgroundRole
BackgroundRole
This enumeration describes the background color being selected from the given set.
Definition: kcolorscheme.h:130
KMessageWidget::Information
Definition: kmessagewidget.h:105
KMessageWidget::sizeHint
QSize sizeHint() const
Definition: kmessagewidget.cpp:302
QSize
KGlobalSettings::graphicEffectsLevel
static GraphicEffects graphicEffectsLevel()
This function determines the desired level of effects on the GUI.
Definition: kglobalsettings.cpp:782
QToolButton
kicon.h
KMessageWidget
A widget to provide feedback or propose opportunistic interactions.
Definition: kmessagewidget.h:92
getColorsFromColorScheme
static void getColorsFromColorScheme(KColorScheme::BackgroundRole bgRole, QColor *bg, QColor *fg)
Definition: kmessagewidget.cpp:246
KMessageWidget::setText
void setText(const QString &text)
Definition: kmessagewidget.cpp:235
QFrame
KMessageWidget::heightForWidth
int heightForWidth(int width) const
Definition: kmessagewidget.cpp:331
KColorScheme::NeutralBackground
Seventh color; for example, warnings, secure/encrypted content.
Definition: kcolorscheme.h:176
kcolorscheme.h
KGlobalSettings::SimpleAnimationEffects
GUI with simple animations enabled.
Definition: kglobalsettings.h:467
QAction
KStandardAction::close
KAction * close(const QObject *recvr, const char *slot, QObject *parent)
Close the current document.
Definition: kstandardaction.cpp:269
KMessageWidget::resizeEvent
void resizeEvent(QResizeEvent *event)
Definition: kmessagewidget.cpp:322
QList< QToolButton * >
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:15 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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