• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.9.4 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 
39 //---------------------------------------------------------------------
40 // KMessageWidgetPrivate
41 //---------------------------------------------------------------------
42 class KMessageWidgetPrivate
43 {
44 public:
45  void init(KMessageWidget*);
46 
47  KMessageWidget* q;
48  QFrame* content;
49  QLabel* iconLabel;
50  QLabel* textLabel;
51  QToolButton* closeButton;
52  QTimeLine* timeLine;
53 
54  KMessageWidget::MessageType messageType;
55  bool wordWrap;
56  QList<QToolButton*> buttons;
57  QPixmap contentSnapShot;
58 
59  void createLayout();
60  void updateSnapShot();
61  void updateLayout();
62  void slotTimeLineChanged(qreal);
63  void slotTimeLineFinished();
64 
65  int bestContentHeight() const;
66 };
67 
68 void KMessageWidgetPrivate::init(KMessageWidget *q_ptr)
69 {
70  q = q_ptr;
71 
72  q->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
73 
74  timeLine = new QTimeLine(500, q);
75  QObject::connect(timeLine, SIGNAL(valueChanged(qreal)), q, SLOT(slotTimeLineChanged(qreal)));
76  QObject::connect(timeLine, SIGNAL(finished()), q, SLOT(slotTimeLineFinished()));
77 
78  content = new QFrame(q);
79  content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
80 
81  wordWrap = false;
82 
83  iconLabel = new QLabel(content);
84  iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
85 
86  textLabel = new QLabel(content);
87  textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
88  textLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
89 
90  KAction* closeAction = KStandardAction::close(q, SLOT(animatedHide()), q);
91 
92  closeButton = new QToolButton(content);
93  closeButton->setAutoRaise(true);
94  closeButton->setDefaultAction(closeAction);
95 
96  q->setMessageType(KMessageWidget::Information);
97 }
98 
99 void KMessageWidgetPrivate::createLayout()
100 {
101  delete content->layout();
102 
103  content->resize(q->size());
104 
105  qDeleteAll(buttons);
106  buttons.clear();
107 
108  Q_FOREACH(QAction* action, q->actions()) {
109  QToolButton* button = new QToolButton(content);
110  button->setDefaultAction(action);
111  button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
112  buttons.append(button);
113  }
114 
115  // AutoRaise reduces visual clutter, but we don't want to turn it on if
116  // there are other buttons, otherwise the close button will look different
117  // from the others.
118  closeButton->setAutoRaise(buttons.isEmpty());
119 
120  if (wordWrap) {
121  QGridLayout* layout = new QGridLayout(content);
122  // Set alignment to make sure icon does not move down if text wraps
123  layout->addWidget(iconLabel, 0, 0, 1, 1, Qt::AlignHCenter | Qt::AlignTop);
124  layout->addWidget(textLabel, 0, 1);
125 
126  QHBoxLayout* buttonLayout = new QHBoxLayout;
127  buttonLayout->addStretch();
128  Q_FOREACH(QToolButton* button, buttons) {
129  // For some reason, calling show() is necessary if wordwrap is true,
130  // otherwise the buttons do not show up. It is not needed if
131  // wordwrap is false.
132  button->show();
133  buttonLayout->addWidget(button);
134  }
135  buttonLayout->addWidget(closeButton);
136  layout->addItem(buttonLayout, 1, 0, 1, 2);
137  } else {
138  QHBoxLayout* layout = new QHBoxLayout(content);
139  layout->addWidget(iconLabel);
140  layout->addWidget(textLabel);
141 
142  Q_FOREACH(QToolButton* button, buttons) {
143  layout->addWidget(button);
144  }
145 
146  layout->addWidget(closeButton);
147  };
148 
149  if (q->isVisible()) {
150  q->setFixedHeight(content->sizeHint().height());
151  }
152  q->updateGeometry();
153 }
154 
155 void KMessageWidgetPrivate::updateLayout()
156 {
157  if (content->layout()) {
158  createLayout();
159  }
160 }
161 
162 void KMessageWidgetPrivate::updateSnapShot()
163 {
164  contentSnapShot = QPixmap(content->size());
165  contentSnapShot.fill(Qt::transparent);
166  content->render(&contentSnapShot, QPoint(), QRegion(), QWidget::DrawChildren);
167 }
168 
169 void KMessageWidgetPrivate::slotTimeLineChanged(qreal value)
170 {
171  q->setFixedHeight(qMin(value * 2, qreal(1.0)) * content->height());
172  q->update();
173 }
174 
175 void KMessageWidgetPrivate::slotTimeLineFinished()
176 {
177  if (timeLine->direction() == QTimeLine::Forward) {
178  // Show
179  content->move(0, 0);
180  } else {
181  // Hide
182  q->hide();
183  }
184 }
185 
186 int KMessageWidgetPrivate::bestContentHeight() const
187 {
188  int height = content->heightForWidth(q->width());
189  if (height == -1) {
190  height = content->sizeHint().height();
191  }
192  return height;
193 }
194 
195 
196 //---------------------------------------------------------------------
197 // KMessageWidget
198 //---------------------------------------------------------------------
199 KMessageWidget::KMessageWidget(QWidget* parent)
200  : QFrame(parent)
201  , d(new KMessageWidgetPrivate)
202 {
203  d->init(this);
204 }
205 
206 KMessageWidget::KMessageWidget(const QString& text, QWidget* parent)
207  : QFrame(parent)
208  , d(new KMessageWidgetPrivate)
209 {
210  d->init(this);
211  setText(text);
212 }
213 
214 KMessageWidget::~KMessageWidget()
215 {
216  delete d;
217 }
218 
219 QString KMessageWidget::text() const
220 {
221  return d->textLabel->text();
222 }
223 
224 void KMessageWidget::setText(const QString& text)
225 {
226  d->textLabel->setText(text);
227  updateGeometry();
228 }
229 
230 KMessageWidget::MessageType KMessageWidget::messageType() const
231 {
232  return d->messageType;
233 }
234 
235 static void getColorsFromColorScheme(KColorScheme::BackgroundRole bgRole, QColor* bg, QColor* fg)
236 {
237  KColorScheme scheme(QPalette::Active, KColorScheme::Window);
238  *bg = scheme.background(bgRole).color();
239  *fg = scheme.foreground().color();
240 }
241 
242 void KMessageWidget::setMessageType(KMessageWidget::MessageType type)
243 {
244  d->messageType = type;
245  KIcon icon;
246  QColor bg0, bg1, bg2, border, fg;
247  switch (type) {
248  case Positive:
249  icon = KIcon("dialog-ok");
250  getColorsFromColorScheme(KColorScheme::PositiveBackground, &bg1, &fg);
251  break;
252  case Information:
253  icon = KIcon("dialog-information");
254  // There is no "information" background role in KColorScheme, use the
255  // colors of highlighted items instead
256  bg1 = palette().highlight().color();
257  fg = palette().highlightedText().color();
258  break;
259  case Warning:
260  icon = KIcon("dialog-warning");
261  getColorsFromColorScheme(KColorScheme::NeutralBackground, &bg1, &fg);
262  break;
263  case Error:
264  icon = KIcon("dialog-error");
265  getColorsFromColorScheme(KColorScheme::NegativeBackground, &bg1, &fg);
266  break;
267  }
268 
269  // Colors
270  bg0 = bg1.lighter(110);
271  bg2 = bg1.darker(110);
272  border = KColorScheme::shade(bg1, KColorScheme::DarkShade);
273 
274  d->content->setStyleSheet(
275  QString(".QFrame {"
276  "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
277  " stop: 0 %1,"
278  " stop: 0.1 %2,"
279  " stop: 1.0 %3);"
280  "border-radius: 5px;"
281  "border: 1px solid %4;"
282  "}"
283  ".QLabel { color: %5; }"
284  )
285  .arg(bg0.name())
286  .arg(bg1.name())
287  .arg(bg2.name())
288  .arg(border.name())
289  .arg(fg.name())
290  );
291 
292  // Icon
293  const int size = KIconLoader::global()->currentSize(KIconLoader::MainToolbar);
294  d->iconLabel->setPixmap(icon.pixmap(size));
295 }
296 
297 QSize KMessageWidget::sizeHint() const
298 {
299  ensurePolished();
300  return d->content->sizeHint();
301 }
302 
303 QSize KMessageWidget::minimumSizeHint() const
304 {
305  ensurePolished();
306  return d->content->minimumSizeHint();
307 }
308 
309 bool KMessageWidget::event(QEvent* event)
310 {
311  if (event->type() == QEvent::Polish && !d->content->layout()) {
312  d->createLayout();
313  }
314  return QFrame::event(event);
315 }
316 
317 void KMessageWidget::resizeEvent(QResizeEvent* event)
318 {
319  QFrame::resizeEvent(event);
320  int contentHeight = d->bestContentHeight();
321 
322  if (d->timeLine->state() == QTimeLine::NotRunning) {
323  d->content->resize(width(), contentHeight);
324  } else if (event->size().width() != event->oldSize().width()) {
325  d->content->resize(width(), contentHeight);
326  d->updateSnapShot();
327  }
328 }
329 
330 int KMessageWidget::heightForWidth(int width) const
331 {
332  ensurePolished();
333  return d->content->heightForWidth(width);
334 }
335 
336 void KMessageWidget::paintEvent(QPaintEvent* event)
337 {
338  QFrame::paintEvent(event);
339  if (d->timeLine->state() == QTimeLine::Running) {
340  QPainter painter(this);
341  painter.setOpacity(d->timeLine->currentValue() * d->timeLine->currentValue());
342  painter.drawPixmap(0, 0, d->contentSnapShot);
343  }
344 }
345 
346 void KMessageWidget::showEvent(QShowEvent* event)
347 {
348  // Keep this method here to avoid breaking binary compatibility:
349  // QFrame::showEvent() used to be reimplemented.
350  QFrame::showEvent(event);
351 }
352 
353 bool KMessageWidget::wordWrap() const
354 {
355  return d->wordWrap;
356 }
357 
358 void KMessageWidget::setWordWrap(bool wordWrap)
359 {
360  d->wordWrap = wordWrap;
361  d->textLabel->setWordWrap(wordWrap);
362  QSizePolicy policy = sizePolicy();
363  policy.setHeightForWidth(wordWrap);
364  setSizePolicy(policy);
365  d->updateLayout();
366  // Without this, when user does wordWrap -> !wordWrap -> wordWrap, a minimum
367  // height is set, causing the widget to be too high.
368  // Mostly visible in test programs.
369  if (wordWrap) {
370  setMinimumHeight(0);
371  }
372 }
373 
374 bool KMessageWidget::isCloseButtonVisible() const
375 {
376  return d->closeButton->isVisible();
377 }
378 
379 void KMessageWidget::setCloseButtonVisible(bool show)
380 {
381  d->closeButton->setVisible(show);
382  updateGeometry();
383 }
384 
385 void KMessageWidget::addAction(QAction* action)
386 {
387  QFrame::addAction(action);
388  d->updateLayout();
389 }
390 
391 void KMessageWidget::removeAction(QAction* action)
392 {
393  QFrame::removeAction(action);
394  d->updateLayout();
395 }
396 
397 void KMessageWidget::animatedShow()
398 {
399  if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
400  show();
401  return;
402  }
403 
404  if (isVisible()) {
405  return;
406  }
407 
408  QFrame::show();
409  setFixedHeight(0);
410  int wantedHeight = d->bestContentHeight();
411  d->content->setGeometry(0, -wantedHeight, width(), wantedHeight);
412 
413  d->updateSnapShot();
414 
415  d->timeLine->setDirection(QTimeLine::Forward);
416  if (d->timeLine->state() == QTimeLine::NotRunning) {
417  d->timeLine->start();
418  }
419 }
420 
421 void KMessageWidget::animatedHide()
422 {
423  if (!(KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects)) {
424  hide();
425  return;
426  }
427 
428  if (!isVisible()) {
429  return;
430  }
431 
432  d->content->move(0, -d->content->height());
433  d->updateSnapShot();
434 
435  d->timeLine->setDirection(QTimeLine::Backward);
436  if (d->timeLine->state() == QTimeLine::NotRunning) {
437  d->timeLine->start();
438  }
439 }
440 
441 #include "kmessagewidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Dec 10 2012 13:50:34 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdelibs-4.9.4 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