-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathballoontip.cpp
More file actions
479 lines (448 loc) · 12.8 KB
/
balloontip.cpp
File metadata and controls
479 lines (448 loc) · 12.8 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "balloontip.h"
#include "customlabel.h"
#include <QPushButton>
#include <QGridLayout>
#include <QStyle>
#include <QApplication>
#include <QDesktopWidget>
#include <QBitmap>
#include <QPainter>
#include <QMouseEvent>
#include <QTimer>
#include <QStyleOption>
//#include <QDebug>
// TODO: move all magic numbers to settings
BalloonTip * BalloonTip::theSolitaryBalloonTip = NULL;
bool BalloonTip::isBalloonVisible()
{
return theSolitaryBalloonTip;
}
QWidget *BalloonTip::showBalloon(QIcon icon, const QString& title, const QString& message,
const QPoint& pos, int timeout, bool showArrow, ArrowPosition arrowPosition, QWidget * p)
{
BalloonTip::hideBalloon();
if (!(message.isEmpty() && title.isEmpty()))
{
theSolitaryBalloonTip = new BalloonTip(icon, title, message, p);
theSolitaryBalloonTip->drawBalloon(pos, timeout, showArrow, arrowPosition);
}
return theSolitaryBalloonTip;
}
QWidget *BalloonTip::showBalloon(QIcon icon, QWidget * messageWidget,
const QPoint& pos, int timeout, bool showArrow, ArrowPosition arrowPosition, QWidget * p)
{
BalloonTip::hideBalloon();
if (messageWidget)
{
theSolitaryBalloonTip = new BalloonTip(icon, messageWidget, p);
theSolitaryBalloonTip->drawBalloon(pos, timeout, showArrow, arrowPosition);
}
return theSolitaryBalloonTip;
}
void BalloonTip::hideBalloon()
{
if (theSolitaryBalloonTip)
{
theSolitaryBalloonTip->hide();
theSolitaryBalloonTip->close();
//QTimer::singleShot(10, theSolitaryBalloonTip, SLOT(hide()));
QTimer::singleShot(10, theSolitaryBalloonTip, SLOT(close()));
//QTimer::singleShot(10, theSolitaryBalloonTip, SLOT(deleteLater()));
theSolitaryBalloonTip = NULL;
}
}
void BalloonTip::init()
{
#ifdef Q_WS_MAC
setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
#else
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
#endif
setFocusPolicy(Qt::NoFocus);
setAttribute(Qt::WA_DeleteOnClose, true);
setAttribute(Qt::WA_TranslucentBackground, true);
setMaximumWidth(250);
setMaximumHeight(200);
setMinimumSize(230, 38);
if (_p)
{
_p->installEventFilter(this);
}
QPalette pal = palette();
pal.setColor(QPalette::Window, pal.toolTipBase().color());
pal.setColor(QPalette::WindowText, pal.toolTipText().color());
setPalette(pal);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
widget = NULL;
}
BalloonTip::BalloonTip(QIcon icon, const QString& title, const QString& message, QWidget * p) : QWidget(0), timerId(-1), _p(p)
{
init();
CustomLabel *titleLabel = new CustomLabel;
titleLabel->installEventFilter(this);
titleLabel->setText(title);
QFont font = titleLabel->font();
font.setBold(true);
#ifdef Q_WS_WINCE
font.setPointSize(font.pointSize() - 2);
#endif
titleLabel->setFont(font);
titleLabel->setTextFormat(Qt::PlainText);
#ifdef Q_WS_WINCE
const int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
#else
const int iconSize = 18;
#endif
CustomLabel *msgLabel = new CustomLabel;
#ifdef Q_WS_WINCE
font.setBold(false);
msgLabel->setFont(font);
#endif
msgLabel->installEventFilter(this);
msgLabel->setText(message);
msgLabel->setTextFormat(Qt::PlainText);
msgLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);
// size for the message label
int limit = 230; // QApplication::desktop()->availableGeometry(msgLabel).size().width() / 4;
if (msgLabel->sizeHint().width() > limit)
{
msgLabel->setWordWrap(true);
#ifdef Q_WS_WINCE
// Make sure that the text isn't wrapped "somewhere" in the balloon widget
// in the case that we have a long title label.
setMaximumWidth(limit);
#else
// Here we allow the text being much smaller than the balloon widget
// to emulate the weird standard windows behavior.
//msgLabel->setFixedSize(limit, msgLabel->heightForWidth(limit));
msgLabel->setMinimumSize(limit, msgLabel->heightForWidth(limit));
#endif
}
QGridLayout *layout = new QGridLayout;
if (!icon.isNull())
{
CustomLabel *iconLabel = new CustomLabel;
iconLabel->setPixmap(icon.pixmap(iconSize, iconSize));
iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
iconLabel->setMargin(2);
layout->addWidget(iconLabel, 0, 0);
if (!title.isEmpty())
layout->addWidget(titleLabel, 0, 1);
}
else
{
if (!title.isEmpty())
layout->addWidget(titleLabel, 0, 0, 1, 2);
}
if (title.isEmpty())
{
layout->addWidget(msgLabel, 0, 1, 2, 1);
layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding), 1, 0);
}
else
layout->addWidget(msgLabel, 1, 0, 1, 3);
layout->setSizeConstraint(QLayout::SetMinimumSize);
layout->setMargin(3);
setGeometry(QRect(geometry().topLeft(), sizeHint()));
layout->setGeometry(QRect(geometry().topLeft(), sizeHint()));
setMinimumSize(sizeHint());
setFixedSize(sizeHint());
setLayout(layout);
}
BalloonTip::BalloonTip(QIcon icon, QWidget * messageWidget, QWidget * p) : QWidget(0), timerId(-1), _p(p)
{
init();
widget = messageWidget;
widget->installEventFilter(this);
QIcon si = icon;
QGridLayout *layout = new QGridLayout;
#ifdef Q_WS_WINCE
const int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize);
#else
const int iconSize = 18;
#endif
if (!si.isNull())
{
CustomLabel *iconLabel = new CustomLabel;
iconLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
iconLabel->setPixmap(si.pixmap(iconSize, iconSize));
iconLabel->setMargin(2);
layout->addWidget(iconLabel, 0, 0);
layout->addWidget(widget, 0, 1);
}
else
{
layout->addWidget(widget, 0, 0, 1, 2);
}
layout->setSizeConstraint(QLayout::SetFixedSize);
layout->setMargin(3);
setLayout(layout);
}
BalloonTip::~BalloonTip()
{
if (widget)
{
widget->setParent(0);
widget = NULL;
}
emit closed();
}
void BalloonTip::drawBalloon(const QPoint& pos, int msecs, bool showArrow, ArrowPosition arrowPosition)
{
QRect scr = QApplication::desktop()->screenGeometry(pos);
QSize sh = sizeHint();
const int border = 0;
int ah = 18, ao = 18, aw = 18, rc = 7;
if (arrowPosition == ArrowLeft || arrowPosition == ArrowRight)
{
ah = 10, ao = 14, aw = 5;
}
// determining arrow position (only top and bottom are auto)
if (arrowPosition == AutoArrow)
{
if (pos.y() + sh.height() + ah < scr.height())
arrowPosition = ArrowTop;
else
arrowPosition = ArrowBottom;
}
// vertical alignment for left and right arrow position
bool arrowAtTop = (pos.y() + sh.height() + ah < scr.height());
// horisontal alignment for top and bottom arrow position
bool arrowAtLeft = (pos.x() + sh.width() - ao < scr.width());
setContentsMargins(border + (arrowPosition == ArrowLeft ? aw : 0) + 3, border + (arrowPosition == ArrowTop ? ah : 0) + 2, border + (arrowPosition == ArrowRight ? aw : 0) + 3, border + (arrowPosition == ArrowBottom ? ah : 0) + 2);
updateGeometry();
sh = sizeHint();
if (sh.height() < 38)
sh.setHeight(38);
int ml=0, mr=0, mt=0, mb=0;
QSize sz = sh;
//qDebug() << "drawBalloon: sz = " << sz;
switch (arrowPosition)
{
case ArrowLeft:
ml = aw;
mt = 0;
mr = sz.width() - 1;
mb = sz.height() - 1;
break;
case ArrowRight:
ml = mt = 0;
mr = sz.width() - aw - 1;
mb = sz.height() - 1;
break;
case ArrowTop:
ml = 0;
mt = ah;
mr = sz.width() - 1;
mb = sz.height() - 1;
break;
case ArrowBottom:
ml = mt = 0;
mr = sz.width() - 1;
mb = sz.height() - ah - 1;
break;
default:
break;
}
QPainterPath path;
#if defined(QT_NO_XSHAPE) && defined(Q_WS_X11)
// XShape is required for setting the mask, so we just
// draw an ugly square when its not available
path.moveTo(0, 0);
path.lineTo(sz.width() - 1, 0);
path.lineTo(sz.width() - 1, sz.height() - 1);
path.lineTo(0, sz.height() - 1);
path.lineTo(0, 0);
move(qMax(pos.x() - sz.width(), scr.left()), pos.y());
#else
path.moveTo(ml + rc, mt);
if ((arrowPosition == ArrowTop) && arrowAtLeft)
{
if (showArrow)
{
path.lineTo(ml + ao, mt);
path.lineTo(ml + ao, mt - ah);
path.lineTo(ml + ao + aw, mt);
}
move(qMax(pos.x() - ao, scr.left() + 2), pos.y());
}
else if ((arrowPosition == ArrowTop) && !arrowAtLeft)
{
if (showArrow)
{
path.lineTo(mr - ao - aw, mt);
path.lineTo(mr - ao, mt - ah);
path.lineTo(mr - ao, mt);
}
move(qMin(pos.x() - sh.width() + ao, scr.right() - sh.width() - 2), pos.y());
}
path.lineTo(mr - rc, mt);
path.arcTo(QRect(mr - rc*2, mt, rc*2, rc*2), 90, -90);
if ((arrowPosition == ArrowRight) && arrowAtTop)
{
if (showArrow)
{
path.lineTo(mr, mt + rc + ao);
path.lineTo(mr + aw, mt + rc + ao + ah/2);
path.lineTo(mr, mt + rc + ao + ah);
}
move(pos.x() - sh.width(), pos.y() - ao - rc - ah/2);
}
else if ((arrowPosition == ArrowRight) && !arrowAtTop)
{
if (showArrow)
{
path.lineTo(mr, mb - rc - ao - ah);
path.lineTo(mr + aw, mb - rc - ao - ah/2);
path.lineTo(mr, mb - rc - ao);
}
move(pos.x() - sh.width(), pos.y() - (sh.height() - ao - rc - ah/2));
}
path.lineTo(mr, mb - rc);
path.arcTo(QRect(mr - rc*2, mb - rc*2, rc*2, rc*2), 0, -90);
if ((arrowPosition == ArrowBottom) && !arrowAtLeft)
{
if (showArrow)
{
path.lineTo(mr - ao, mb);
path.lineTo(mr - ao, mb + ah);
path.lineTo(mr - ao - aw, mb);
}
move(qMin(pos.x() - sh.width() + ao, scr.right() - sh.width() - 2),
pos.y() - sh.height());
}
else if ((arrowPosition == ArrowBottom) && arrowAtLeft)
{
if (showArrow)
{
path.lineTo(ao + aw, mb);
path.lineTo(ao, mb + ah);
path.lineTo(ao, mb);
}
move(qMax(pos.x() - ao, scr.x() + 2), pos.y() - sh.height());
}
path.lineTo(ml + rc, mb);
path.arcTo(QRect(ml, mb - rc*2, rc*2, rc*2), -90, -90);
if ((arrowPosition == ArrowLeft) && arrowAtTop)
{
if (showArrow)
{
path.lineTo(ml, mt + rc + ao + ah);
path.lineTo(ml - aw, mt + rc + ao + ah/2);
path.lineTo(ml, mt + rc + ao);
}
move(pos.x(), pos.y() - ao - rc - ah/2);
}
else if ((arrowPosition == ArrowLeft) && !arrowAtTop)
{
if (showArrow)
{
path.lineTo(ml, mb - rc - ao);
path.lineTo(ml - aw, mb - rc - ao - ah/2);
path.lineTo(ml, mb - rc - ao - ah);
}
move(pos.x(), pos.y() - (sh.height() - ao - rc - ah/2));
}
path.lineTo(ml, mt + rc);
path.arcTo(QRect(ml, mt, rc*2, rc*2), 180, -90);
// Set the mask
QBitmap bitmap = QBitmap(path.boundingRect().size().toSize());
bitmap.fill(Qt::color0);
QPainter painter1(&bitmap);
painter1.setPen(QPen(Qt::color1, border));
painter1.setBrush(QBrush(Qt::color1));
painter1.drawPath(path);
//setMask(bitmap);
#endif
// Draw the border
pixmap = QPixmap(sz);
pixmap.fill(QColor(0, 0, 0, 0));
QPainter painter2(&pixmap);
painter2.setPen(QPen(palette().color(QPalette::Window).darker(160), border));
painter2.setBrush(palette().color(QPalette::Window));
painter2.drawPath(path);
//qDebug() << "drawBalloon: path.boundingRect() = " << path.boundingRect() << "mask().boundingRect(): " << mask().boundingRect();
if (msecs > 0)
timerId = startTimer(msecs);
show();
}
void BalloonTip::paintEvent(QPaintEvent *evt)
{
Q_UNUSED(evt);
// qDebug() << "paint event: " << evt->rect() << rect() << pixmap.size() << sizeHint() << minimumSize();
QPainter painter(this);
//painter.setClipRect(rect());
//painter.drawPixmap(rect(), pixmap);
//painter.fillRect(rect(), QColor(255, 0, 0));
painter.drawPixmap(0, 0, pixmap);
// QStyleOption opt;
// opt.init(this);
// QPainter p(this);
// style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
//QWidget::paintEvent(evt);
}
void BalloonTip::mousePressEvent(QMouseEvent *ev)
{
QWidget::mousePressEvent(ev);
if(ev->button() == Qt::LeftButton)
emit messageClicked();
BalloonTip::hideBalloon();
//close();
}
void BalloonTip::timerEvent(QTimerEvent *ev)
{
if (ev->timerId() == timerId)
{
if (!underMouse())
{
killTimer(timerId);
BalloonTip::hideBalloon();
return;
}
}
else
QWidget::timerEvent(ev);
}
bool BalloonTip::event(QEvent * ev)
{
if (ev->type() == QEvent::ActivationChange)
{
ev->accept();
return true;
}
return QWidget::event(ev);
}
bool BalloonTip::eventFilter(QObject * obj, QEvent * evt)
{
if (obj == _p)
{
if (evt->type() == QEvent::ActivationChange)
{
if (!_p->isActiveWindow() && !isActiveWindow())
{
BalloonTip::hideBalloon();
return true;
}
}
if (evt->type() == QEvent::Move || evt->type() == QEvent::Resize || evt->type() == QEvent::MouseButtonPress)
{
BalloonTip::hideBalloon();
}
}
if (obj == widget)
{
if (evt->type() == QEvent::MouseButtonPress)
{
return false;
}
}
return QWidget::eventFilter(obj, evt);
}
QSize BalloonTip::sizeHint()
{
QSize sh = QWidget::sizeHint();
if (sh.height() < 38)
sh.setHeight(38);
// qDebug() << "size hint: " << sh;
return sh;
}