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

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