File: messageoverlay.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (158 lines) | stat: -rw-r--r-- 4,436 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
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "messageoverlay.h"
#include "support/utils.h"
#include "toolbutton.h"
#include <QApplication>
#include <QPaintEvent>
#include <QPainter>
#include <QTimer>

MessageOverlay::MessageOverlay(QObject* p)
	: QWidget(nullptr), timer(nullptr)
#ifdef Q_OS_MAC
	  ,
	  closeOnLeft(true)
#else
	  ,
	  closeOnLeft(Utils::Unity == Utils::currentDe())
#endif
{
	Q_UNUSED(p)
	spacing = fontMetrics().height();
	setVisible(false);
	setMinimumHeight(spacing * 2);
	setMaximumHeight(spacing * 2);
	cancelButton = new ToolButton(this);
	Icon::init(cancelButton);
	cancelButton->setToolTip(tr("Cancel"));
	QVariantMap redOpt;
	redOpt.insert("color", Icon::constRed);
	cancelButton->setIcon(Icon::fa(fa::fa_solid, fa::fa_close, redOpt));
	cancelButton->adjustSize();
	connect(cancelButton, SIGNAL(clicked()), SIGNAL(cancel()));
}

void MessageOverlay::setWidget(QWidget* widget)
{
	setParent(widget);
	widget->installEventFilter(this);
}

void MessageOverlay::setText(const QString& txt, int timeout, bool allowCancel)
{
	if (txt == text) {
		return;
	}

	text = txt;
	cancelButton->setVisible(allowCancel);
	setAttribute(Qt::WA_TransparentForMouseEvents, !allowCancel);
	setVisible(!text.isEmpty());
	if (!text.isEmpty()) {
		setSizeAndPosition();
		update();
		if (-1 != timeout) {
			if (!timer) {
				timer = new QTimer(this);
				connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
			}
			timer->start(timeout);
		}
	}
}

void MessageOverlay::paintEvent(QPaintEvent*)
{
	QPainter p(this);
	QRect r(rect());
	QRectF rf(r.x() + 0.5, r.y() + 0.5, r.width() - 1, r.height() - 1);
	QColor borderCol = palette().color(QPalette::Highlight).darker(120);
	QColor col = palette().color(QPalette::Window);
	QPainterPath path = Utils::buildPath(rf, r.height() / 4.0);
	col.setAlphaF(0.8);
	p.setRenderHint(QPainter::Antialiasing, true);
	p.fillPath(path, col);
	p.setPen(QPen(borderCol, qMax(2, r.height() / 16)));
	p.drawPath(path);

	int pad = r.height() / 4;
	if (cancelButton->isVisible()) {
		if (Qt::LeftToRight == layoutDirection() && !closeOnLeft) {
			rf.adjust(pad, pad, -((pad * 2) + cancelButton->width()), -pad);
		}
		else {
			rf.adjust(((pad * 2) + cancelButton->width()), pad, -pad, -pad);
		}
	}
	else {
		rf.adjust(pad, pad, -pad, -pad);
	}
	QFont fnt(QApplication::font());
	fnt.setBold(true);
	QFontMetrics fm(fnt);
	col = palette().color(QPalette::WindowText);

	p.setPen(col);
	p.setFont(fnt);
	p.drawText(rf, fm.elidedText(text, Qt::ElideRight, r.width(), QPalette::WindowText), QTextOption(Qt::LeftToRight == layoutDirection() ? Qt::AlignLeft : Qt::AlignRight));
	p.end();
}

void MessageOverlay::timeout()
{
	setVisible(false);
	text = QString();
}

bool MessageOverlay::eventFilter(QObject* o, QEvent* e)
{
	if (o == parentWidget() && isVisible() && QEvent::Resize == e->type()) {
		setSizeAndPosition();
	}
	return QObject::eventFilter(o, e);
}

void MessageOverlay::setSizeAndPosition()
{
	int currentWidth = width();
	int desiredWidth = parentWidget()->width() - (spacing * 2);
	QPoint currentPos = pos();
	QPoint desiredPos = QPoint(spacing, parentWidget()->height() - (height() + spacing));

	if (currentWidth != desiredWidth) {
		int pad = height() / 4;
		resize(desiredWidth, height());
		cancelButton->move(QPoint(Qt::LeftToRight == layoutDirection() && !closeOnLeft
		                                  ? desiredWidth - (cancelButton->width() + pad)
		                                  : pad,
		                          (height() - cancelButton->height()) / 2));
	}

	if (currentPos != desiredPos) {
		move(desiredPos);
	}
}

#include "moc_messageoverlay.cpp"