File: kmessagewidget.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (369 lines) | stat: -rw-r--r-- 9,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* This file is part of the KDE libraries
 *
 * Copyright (c) 2011 Aurélien Gâteau <agateau@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */
#include "kmessagewidget.h"
#include "flattoolbutton.h"
#include "icon.h"
#include "squeezedtextlabel.h"
#include "utils.h"
#include <QAction>
#include <QEvent>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <QShowEvent>
#include <QStyle>
#include <QTimeLine>
#include <QToolButton>

void KMsgWidgetPrivate::init(KMsgWidget* q_ptr)
{
	q = q_ptr;

	q->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);

	timeLine = new QTimeLine(500, q);
	QObject::connect(timeLine, SIGNAL(valueChanged(qreal)), q, SLOT(slotTimeLineChanged(qreal)));
	QObject::connect(timeLine, SIGNAL(finished()), q, SLOT(slotTimeLineFinished()));

	content = new QFrame(q);
	content->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

	wordWrap = false;

	textLabel = new SqueezedTextLabel(content);
	textLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
	textLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
	textLabel->setOpenExternalLinks(true);

	QAction* closeAction = new QAction(q);
	QVariantMap iconOpt;
	iconOpt.insert("color", Icon::constRed);
	closeAction->setIcon(Icon::fa(fa::fa_solid, fa::fa_close, iconOpt));
	closeAction->setToolTip(QObject::tr("Close"));
	QObject::connect(closeAction, SIGNAL(triggered()), q, SLOT(animatedHide()));

	closeButton = new FlatToolButton(content);
	closeButton->setDefaultAction(closeAction);
#ifdef Q_OS_MAC
	closeButton->setStyleSheet("QToolButton {border: 0}");
#endif
	q->setMessageType(KMsgWidget::Information);
}

void KMsgWidgetPrivate::createLayout()
{
	delete content->layout();

	content->resize(q->size());

	qDeleteAll(buttons);
	buttons.clear();

	Q_FOREACH (QAction* action, q->actions()) {
		QToolButton* button = new QToolButton(content);
		button->setDefaultAction(action);
		if (content->style()->styleHint(QStyle::SH_DialogButtonBox_ButtonsHaveIcons)) {
			button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
		}
		else {
			button->setToolButtonStyle(Qt::ToolButtonTextOnly);
		}
		buttons.append(button);
	}

	// AutoRaise reduces visual clutter, but we don't want to turn it on if
	// there are other buttons, otherwise the close button will look different
	// from the others.
	closeButton->setAutoRaise(buttons.isEmpty());

	if (wordWrap) {
		QGridLayout* layout = new QGridLayout(content);
		layout->addWidget(textLabel, 0, 1);
		layout->setContentsMargins(4, 4, 4, 4);

		QHBoxLayout* buttonLayout = new QHBoxLayout;
		buttonLayout->addStretch();
		Q_FOREACH (QToolButton* button, buttons) {
			// For some reason, calling show() is necessary if wordwrap is true,
			// otherwise the buttons do not show up. It is not needed if
			// wordwrap is false.
			button->show();
			buttonLayout->addWidget(button);
		}
		buttonLayout->addWidget(closeButton);
		layout->addItem(buttonLayout, 1, 0, 1, 2);
	}
	else {
		QHBoxLayout* layout = new QHBoxLayout(content);
		layout->addWidget(textLabel);

		Q_FOREACH (QToolButton* button, buttons) {
			layout->addWidget(button);
		}

		layout->addWidget(closeButton);
		layout->setContentsMargins(4, 4, 4, 4);
	};

	if (q->isVisible()) {
		q->setFixedHeight(content->sizeHint().height());
	}
	q->updateGeometry();
}

void KMsgWidgetPrivate::updateLayout()
{
	if (content->layout()) {
		createLayout();
	}
}

void KMsgWidgetPrivate::updateSnapShot()
{
	// Attention: updateSnapShot calls QWidget::render(), which causes the whole
	// window layouts to be activated. Calling this method from resizeEvent()
	// can lead to infinite recursion, see:
	// https://bugs.kde.org/show_bug.cgi?id=311336
	contentSnapShot = QPixmap(content->size());
	contentSnapShot.fill(Qt::transparent);
	content->render(&contentSnapShot, QPoint(), QRegion(), QWidget::DrawChildren);
}

void KMsgWidgetPrivate::slotTimeLineChanged(qreal value)
{
	q->setFixedHeight(qMin(value * 2, qreal(1.0)) * content->height());
	q->update();
}

void KMsgWidgetPrivate::slotTimeLineFinished()
{
	if (timeLine->direction() == QTimeLine::Forward) {
		// Show
		// We set the whole geometry here, because it may be wrong if a
		// KMessageWidget is shown right when the toplevel window is created.
		content->setGeometry(0, 0, q->width(), bestContentHeight());
	}
	else {
		// Hide
		q->hide();
	}
}

int KMsgWidgetPrivate::bestContentHeight() const
{
	int height = content->heightForWidth(q->width());
	if (height == -1) {
		height = content->sizeHint().height();
	}
	return height;
}

//---------------------------------------------------------------------
// KMsgWidget
//---------------------------------------------------------------------
KMsgWidget::KMsgWidget(QWidget* parent)
	: QFrame(parent), d(new KMsgWidgetPrivate)
{
	d->init(this);
}

KMsgWidget::KMsgWidget(const QString& text, QWidget* parent)
	: QFrame(parent), d(new KMsgWidgetPrivate)
{
	d->init(this);
	setText(text);
}

KMsgWidget::~KMsgWidget()
{
	delete d;
}

QString KMsgWidget::text() const
{
	return d->textLabel->text();
}

void KMsgWidget::setText(const QString& text)
{
	d->textLabel->setText(text);
	updateGeometry();
}

KMsgWidget::MessageType KMsgWidget::messageType() const
{
	return d->messageType;
}

void KMsgWidget::setMessageType(KMsgWidget::MessageType type)
{
	d->messageType = type;
	QColor bg0, bg1, bg2, border, fg;
	switch (type) {
	case Positive:
		bg1 = QColor(0xAB, 0xC7, 0xED);
		fg = Qt::black;
		border = Qt::blue;
		break;
	case Information:
		// There is no "information" background role in KColorScheme, use the
		// colors of highlighted items instead
		bg1 = QColor(0x98, 0xBC, 0xE3);
		fg = Qt::black;
		border = Qt::blue;
		break;
	case Warning:
		bg1 = QColor(0xED, 0xC6, 0x62);
		fg = Qt::black;
		border = Qt::red;
		break;
	case Error:
		bg1 = QColor(0xeb, 0xbb, 0xbb);
		fg = Qt::black;
		border = Qt::red;
		break;
	}

	// Colors
	bg0 = bg1.lighter(110);
	bg2 = bg1.darker(110);

	d->content->setStyleSheet(
			QString(".QFrame {"
	                "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
	                "    stop: 0 %1,"
	                "    stop: 0.1 %2,"
	                "    stop: 1.0 %3);"
	                "border: 1px solid %4;"
	                "margin: %5px;"
	                "}"
	                ".QLabel { color: %6; }")
					.arg(bg0.name())
					.arg(bg1.name())
					.arg(bg2.name())
					.arg(border.name())
					// 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
					.arg(style()->pixelMetric(QStyle::PM_DefaultFrameWidth, nullptr, this) - 1)
					.arg(fg.name()));
}

bool KMsgWidget::event(QEvent* event)
{
	if (event->type() == QEvent::Polish && !d->content->layout()) {
		d->createLayout();
	}
	return QFrame::event(event);
}

void KMsgWidget::resizeEvent(QResizeEvent* event)
{
	QFrame::resizeEvent(event);

	if (d->timeLine->state() == QTimeLine::NotRunning) {
		d->content->resize(width(), d->bestContentHeight());
	}
}

int KMsgWidget::heightForWidth(int width) const
{
	ensurePolished();
	return d->content->heightForWidth(width);
}

void KMsgWidget::paintEvent(QPaintEvent* event)
{
	QFrame::paintEvent(event);
	if (d->timeLine->state() == QTimeLine::Running) {
		QPainter painter(this);
		painter.setOpacity(d->timeLine->currentValue() * d->timeLine->currentValue());
		painter.drawPixmap(0, 0, d->contentSnapShot);
	}
}

bool KMsgWidget::wordWrap() const
{
	return d->wordWrap;
}

void KMsgWidget::setWordWrap(bool wordWrap)
{
	d->wordWrap = wordWrap;
	d->textLabel->setWordWrap(wordWrap);
	d->updateLayout();
}

bool KMsgWidget::isCloseButtonVisible() const
{
	return d->closeButton->isVisible();
}

void KMsgWidget::setCloseButtonVisible(bool show)
{
	d->closeButton->setVisible(show);
}

void KMsgWidget::addAction(QAction* action)
{
	QFrame::addAction(action);
	d->updateLayout();
}

void KMsgWidget::removeAction(QAction* action)
{
	QFrame::removeAction(action);
	d->updateLayout();
}

void KMsgWidget::animatedShow()
{
	if (isVisible()) {
		return;
	}

	QFrame::show();
	setFixedHeight(0);
	int wantedHeight = d->bestContentHeight();
	d->content->setGeometry(0, -wantedHeight, width(), wantedHeight);

	d->updateSnapShot();

	d->timeLine->setDirection(QTimeLine::Forward);
	if (d->timeLine->state() == QTimeLine::NotRunning) {
		d->timeLine->start();
	}
}

void KMsgWidget::animatedHide()
{
	if (!isVisible()) {
		return;
	}

	d->content->move(0, -d->content->height());
	d->updateSnapShot();

	d->timeLine->setDirection(QTimeLine::Backward);
	if (d->timeLine->state() == QTimeLine::NotRunning) {
		d->timeLine->start();
	}
}

#include "moc_kmessagewidget.cpp"