Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/util/qsystemtrayicon.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "qsystemtrayicon.h" | - | ||||||
41 | #include "qsystemtrayicon_p.h" | - | ||||||
42 | - | |||||||
43 | #ifndef QT_NO_SYSTEMTRAYICON | - | ||||||
44 | - | |||||||
45 | #include "qmenu.h" | - | ||||||
46 | #include "qlist.h" | - | ||||||
47 | #include "qevent.h" | - | ||||||
48 | #include "qpoint.h" | - | ||||||
49 | #include "qlabel.h" | - | ||||||
50 | #include "qpushbutton.h" | - | ||||||
51 | #include "qpainterpath.h" | - | ||||||
52 | #include "qpainter.h" | - | ||||||
53 | #include "qstyle.h" | - | ||||||
54 | #include "qgridlayout.h" | - | ||||||
55 | #include "qapplication.h" | - | ||||||
56 | #include "qdesktopwidget.h" | - | ||||||
57 | #include "qbitmap.h" | - | ||||||
58 | #include "private/qlabel_p.h" | - | ||||||
59 | - | |||||||
60 | QT_BEGIN_NAMESPACE | - | ||||||
61 | - | |||||||
62 | /*! | - | ||||||
63 | \class QSystemTrayIcon | - | ||||||
64 | \brief The QSystemTrayIcon class provides an icon for an application in the system tray. | - | ||||||
65 | \since 4.2 | - | ||||||
66 | \ingroup desktop | - | ||||||
67 | \inmodule QtWidgets | - | ||||||
68 | - | |||||||
69 | Modern operating systems usually provide a special area on the desktop, | - | ||||||
70 | called the \e{system tray} or \e{notification area}, where long-running | - | ||||||
71 | applications can display icons and short messages. | - | ||||||
72 | - | |||||||
73 | \image system-tray.png The system tray on Windows XP. | - | ||||||
74 | - | |||||||
75 | The QSystemTrayIcon class can be used on the following platforms: | - | ||||||
76 | - | |||||||
77 | \list | - | ||||||
78 | \li All supported versions of Windows. | - | ||||||
79 | \li All window managers and independent tray implementations for X11 that implement the | - | ||||||
80 | \l{http://standards.freedesktop.org/systemtray-spec/systemtray-spec-0.2.html freedesktop.org} | - | ||||||
81 | XEmbed system tray specification. | - | ||||||
82 | \li All X11 desktop environments that implement the D-Bus | - | ||||||
83 | \l{http://www.freedesktop.org/wiki/Specifications/StatusNotifierItem/StatusNotifierItem} | - | ||||||
84 | specification, including recent versions of KDE and Unity. | - | ||||||
85 | \li All supported versions of \macos. Note that the Growl | - | ||||||
86 | notification system must be installed for | - | ||||||
87 | QSystemTrayIcon::showMessage() to display messages on \macos prior to 10.8 (Mountain Lion). | - | ||||||
88 | \endlist | - | ||||||
89 | - | |||||||
90 | To check whether a system tray is present on the user's desktop, | - | ||||||
91 | call the QSystemTrayIcon::isSystemTrayAvailable() static function. | - | ||||||
92 | - | |||||||
93 | To add a system tray entry, create a QSystemTrayIcon object, call setContextMenu() | - | ||||||
94 | to provide a context menu for the icon, and call show() to make it visible in the | - | ||||||
95 | system tray. Status notification messages ("balloon messages") can be displayed at | - | ||||||
96 | any time using showMessage(). | - | ||||||
97 | - | |||||||
98 | If the system tray is unavailable when a system tray icon is constructed, but | - | ||||||
99 | becomes available later, QSystemTrayIcon will automatically add an entry for the | - | ||||||
100 | application in the system tray if the icon is \l visible. | - | ||||||
101 | - | |||||||
102 | The activated() signal is emitted when the user activates the icon. | - | ||||||
103 | - | |||||||
104 | Only on X11, when a tooltip is requested, the QSystemTrayIcon receives a QHelpEvent | - | ||||||
105 | of type QEvent::ToolTip. Additionally, the QSystemTrayIcon receives wheel events of | - | ||||||
106 | type QEvent::Wheel. These are not supported on any other platform. | - | ||||||
107 | - | |||||||
108 | \sa QDesktopServices, QDesktopWidget, {Desktop Integration}, {System Tray Icon Example} | - | ||||||
109 | */ | - | ||||||
110 | - | |||||||
111 | /*! | - | ||||||
112 | \enum QSystemTrayIcon::MessageIcon | - | ||||||
113 | - | |||||||
114 | This enum describes the icon that is shown when a balloon message is displayed. | - | ||||||
115 | - | |||||||
116 | \value NoIcon No icon is shown. | - | ||||||
117 | \value Information An information icon is shown. | - | ||||||
118 | \value Warning A standard warning icon is shown. | - | ||||||
119 | \value Critical A critical warning icon is shown. | - | ||||||
120 | - | |||||||
121 | \sa QMessageBox | - | ||||||
122 | */ | - | ||||||
123 | - | |||||||
124 | /*! | - | ||||||
125 | Constructs a QSystemTrayIcon object with the given \a parent. | - | ||||||
126 | - | |||||||
127 | The icon is initially invisible. | - | ||||||
128 | - | |||||||
129 | \sa visible | - | ||||||
130 | */ | - | ||||||
131 | QSystemTrayIcon::QSystemTrayIcon(QObject *parent) | - | ||||||
132 | : QObject(*new QSystemTrayIconPrivate(), parent) | - | ||||||
133 | { | - | ||||||
134 | } | - | ||||||
135 | - | |||||||
136 | /*! | - | ||||||
137 | Constructs a QSystemTrayIcon object with the given \a icon and \a parent. | - | ||||||
138 | - | |||||||
139 | The icon is initially invisible. | - | ||||||
140 | - | |||||||
141 | \sa visible | - | ||||||
142 | */ | - | ||||||
143 | QSystemTrayIcon::QSystemTrayIcon(const QIcon &icon, QObject *parent) | - | ||||||
144 | : QObject(*new QSystemTrayIconPrivate(), parent) | - | ||||||
145 | { | - | ||||||
146 | setIcon(icon); | - | ||||||
147 | } | - | ||||||
148 | - | |||||||
149 | /*! | - | ||||||
150 | Removes the icon from the system tray and frees all allocated resources. | - | ||||||
151 | */ | - | ||||||
152 | QSystemTrayIcon::~QSystemTrayIcon() | - | ||||||
153 | { | - | ||||||
154 | Q_D(QSystemTrayIcon); | - | ||||||
155 | d->remove_sys(); | - | ||||||
156 | } | - | ||||||
157 | - | |||||||
158 | #ifndef QT_NO_MENU | - | ||||||
159 | - | |||||||
160 | /*! | - | ||||||
161 | Sets the specified \a menu to be the context menu for the system tray icon. | - | ||||||
162 | - | |||||||
163 | The menu will pop up when the user requests the context menu for the system | - | ||||||
164 | tray icon by clicking the mouse button. | - | ||||||
165 | - | |||||||
166 | On \macos, this is currenly converted to a NSMenu, so the | - | ||||||
167 | aboutToHide() signal is not emitted. | - | ||||||
168 | - | |||||||
169 | \note The system tray icon does not take ownership of the menu. You must | - | ||||||
170 | ensure that it is deleted at the appropriate time by, for example, creating | - | ||||||
171 | the menu with a suitable parent object. | - | ||||||
172 | */ | - | ||||||
173 | void QSystemTrayIcon::setContextMenu(QMenu *menu) | - | ||||||
174 | { | - | ||||||
175 | Q_D(QSystemTrayIcon); | - | ||||||
176 | d->menu = menu; | - | ||||||
177 | d->updateMenu_sys(); | - | ||||||
178 | } | - | ||||||
179 | - | |||||||
180 | /*! | - | ||||||
181 | Returns the current context menu for the system tray entry. | - | ||||||
182 | */ | - | ||||||
183 | QMenu* QSystemTrayIcon::contextMenu() const | - | ||||||
184 | { | - | ||||||
185 | Q_D(const QSystemTrayIcon); | - | ||||||
186 | return d->menu; | - | ||||||
187 | } | - | ||||||
188 | - | |||||||
189 | #endif // QT_NO_MENU | - | ||||||
190 | - | |||||||
191 | /*! | - | ||||||
192 | \property QSystemTrayIcon::icon | - | ||||||
193 | \brief the system tray icon | - | ||||||
194 | - | |||||||
195 | On Windows, the system tray icon size is 16x16; on X11, the preferred size is | - | ||||||
196 | 22x22. The icon will be scaled to the appropriate size as necessary. | - | ||||||
197 | */ | - | ||||||
198 | void QSystemTrayIcon::setIcon(const QIcon &icon) | - | ||||||
199 | { | - | ||||||
200 | Q_D(QSystemTrayIcon); | - | ||||||
201 | d->icon = icon; | - | ||||||
202 | d->updateIcon_sys(); | - | ||||||
203 | } | - | ||||||
204 | - | |||||||
205 | QIcon QSystemTrayIcon::icon() const | - | ||||||
206 | { | - | ||||||
207 | Q_D(const QSystemTrayIcon); | - | ||||||
208 | return d->icon; | - | ||||||
209 | } | - | ||||||
210 | - | |||||||
211 | /*! | - | ||||||
212 | \property QSystemTrayIcon::toolTip | - | ||||||
213 | \brief the tooltip for the system tray entry | - | ||||||
214 | - | |||||||
215 | On some systems, the tooltip's length is limited. The tooltip will be truncated | - | ||||||
216 | if necessary. | - | ||||||
217 | */ | - | ||||||
218 | void QSystemTrayIcon::setToolTip(const QString &tooltip) | - | ||||||
219 | { | - | ||||||
220 | Q_D(QSystemTrayIcon); | - | ||||||
221 | d->toolTip = tooltip; | - | ||||||
222 | d->updateToolTip_sys(); | - | ||||||
223 | } | - | ||||||
224 | - | |||||||
225 | QString QSystemTrayIcon::toolTip() const | - | ||||||
226 | { | - | ||||||
227 | Q_D(const QSystemTrayIcon); | - | ||||||
228 | return d->toolTip; | - | ||||||
229 | } | - | ||||||
230 | - | |||||||
231 | /*! | - | ||||||
232 | \fn void QSystemTrayIcon::show() | - | ||||||
233 | - | |||||||
234 | Shows the icon in the system tray. | - | ||||||
235 | - | |||||||
236 | \sa hide(), visible | - | ||||||
237 | */ | - | ||||||
238 | - | |||||||
239 | /*! | - | ||||||
240 | \fn void QSystemTrayIcon::hide() | - | ||||||
241 | - | |||||||
242 | Hides the system tray entry. | - | ||||||
243 | - | |||||||
244 | \sa show(), visible | - | ||||||
245 | */ | - | ||||||
246 | - | |||||||
247 | /*! | - | ||||||
248 | \since 4.3 | - | ||||||
249 | Returns the geometry of the system tray icon in screen coordinates. | - | ||||||
250 | - | |||||||
251 | \sa visible | - | ||||||
252 | */ | - | ||||||
253 | QRect QSystemTrayIcon::geometry() const | - | ||||||
254 | { | - | ||||||
255 | Q_D(const QSystemTrayIcon); | - | ||||||
256 | if (!d->visible) | - | ||||||
257 | return QRect(); | - | ||||||
258 | return d->geometry_sys(); | - | ||||||
259 | } | - | ||||||
260 | - | |||||||
261 | /*! | - | ||||||
262 | \property QSystemTrayIcon::visible | - | ||||||
263 | \brief whether the system tray entry is visible | - | ||||||
264 | - | |||||||
265 | Setting this property to true or calling show() makes the system tray icon | - | ||||||
266 | visible; setting this property to false or calling hide() hides it. | - | ||||||
267 | */ | - | ||||||
268 | void QSystemTrayIcon::setVisible(bool visible) | - | ||||||
269 | { | - | ||||||
270 | Q_D(QSystemTrayIcon); | - | ||||||
271 | if (visible == d->visible)
| 0 | ||||||
272 | return; never executed: return; | 0 | ||||||
273 | if (Q_UNLIKELY(visible && d->icon.isNull() && visible)()))
| 0 | ||||||
274 | qWarning("QSystemTrayIcon::setVisible: No Icon set"); never executed: QMessageLogger(__FILE__, 274, __PRETTY_FUNCTION__).warning("QSystemTrayIcon::setVisible: No Icon set"); | 0 | ||||||
275 | d->visible = visible; | - | ||||||
276 | if (d->visible)
| 0 | ||||||
277 | d->install_sys(); never executed: d->install_sys(); | 0 | ||||||
278 | else | - | ||||||
279 | d->remove_sys(); never executed: d->remove_sys(); | 0 | ||||||
280 | } | - | ||||||
281 | - | |||||||
282 | bool QSystemTrayIcon::isVisible() const | - | ||||||
283 | { | - | ||||||
284 | Q_D(const QSystemTrayIcon); | - | ||||||
285 | return d->visible; | - | ||||||
286 | } | - | ||||||
287 | - | |||||||
288 | /*! | - | ||||||
289 | \reimp | - | ||||||
290 | */ | - | ||||||
291 | bool QSystemTrayIcon::event(QEvent *e) | - | ||||||
292 | { | - | ||||||
293 | return QObject::event(e); | - | ||||||
294 | } | - | ||||||
295 | - | |||||||
296 | /*! | - | ||||||
297 | \enum QSystemTrayIcon::ActivationReason | - | ||||||
298 | - | |||||||
299 | This enum describes the reason the system tray was activated. | - | ||||||
300 | - | |||||||
301 | \value Unknown Unknown reason | - | ||||||
302 | \value Context The context menu for the system tray entry was requested | - | ||||||
303 | \value DoubleClick The system tray entry was double clicked | - | ||||||
304 | \value Trigger The system tray entry was clicked | - | ||||||
305 | \value MiddleClick The system tray entry was clicked with the middle mouse button | - | ||||||
306 | - | |||||||
307 | \sa activated() | - | ||||||
308 | */ | - | ||||||
309 | - | |||||||
310 | /*! | - | ||||||
311 | \fn void QSystemTrayIcon::activated(QSystemTrayIcon::ActivationReason reason) | - | ||||||
312 | - | |||||||
313 | This signal is emitted when the user activates the system tray icon. \a reason | - | ||||||
314 | specifies the reason for activation. QSystemTrayIcon::ActivationReason enumerates | - | ||||||
315 | the various reasons. | - | ||||||
316 | - | |||||||
317 | \sa QSystemTrayIcon::ActivationReason | - | ||||||
318 | */ | - | ||||||
319 | - | |||||||
320 | /*! | - | ||||||
321 | \fn void QSystemTrayIcon::messageClicked() | - | ||||||
322 | - | |||||||
323 | This signal is emitted when the message displayed using showMessage() | - | ||||||
324 | was clicked by the user. | - | ||||||
325 | - | |||||||
326 | Currently this signal is not sent on \macos. | - | ||||||
327 | - | |||||||
328 | \note We follow Microsoft Windows XP/Vista behavior, so the | - | ||||||
329 | signal is also emitted when the user clicks on a tray icon with | - | ||||||
330 | a balloon message displayed. | - | ||||||
331 | - | |||||||
332 | \sa activated() | - | ||||||
333 | */ | - | ||||||
334 | - | |||||||
335 | - | |||||||
336 | /*! | - | ||||||
337 | Returns \c true if the system tray is available; otherwise returns \c false. | - | ||||||
338 | - | |||||||
339 | If the system tray is currently unavailable but becomes available later, | - | ||||||
340 | QSystemTrayIcon will automatically add an entry in the system tray if it | - | ||||||
341 | is \l visible. | - | ||||||
342 | */ | - | ||||||
343 | - | |||||||
344 | bool QSystemTrayIcon::isSystemTrayAvailable() | - | ||||||
345 | { | - | ||||||
346 | return QSystemTrayIconPrivate::isSystemTrayAvailable_sys(); | - | ||||||
347 | } | - | ||||||
348 | - | |||||||
349 | /*! | - | ||||||
350 | Returns \c true if the system tray supports balloon messages; otherwise returns \c false. | - | ||||||
351 | - | |||||||
352 | \sa showMessage() | - | ||||||
353 | */ | - | ||||||
354 | bool QSystemTrayIcon::supportsMessages() | - | ||||||
355 | { | - | ||||||
356 | return QSystemTrayIconPrivate::supportsMessages_sys(); | - | ||||||
357 | } | - | ||||||
358 | - | |||||||
359 | /*! | - | ||||||
360 | \fn void QSystemTrayIcon::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint) | - | ||||||
361 | \since 4.3 | - | ||||||
362 | - | |||||||
363 | Shows a balloon message for the entry with the given \a title, \a message and | - | ||||||
364 | \a icon for the time specified in \a millisecondsTimeoutHint. \a title and \a message | - | ||||||
365 | must be plain text strings. | - | ||||||
366 | - | |||||||
367 | Message can be clicked by the user; the messageClicked() signal will emitted when | - | ||||||
368 | this occurs. | - | ||||||
369 | - | |||||||
370 | Note that display of messages are dependent on the system configuration and user | - | ||||||
371 | preferences, and that messages may not appear at all. Hence, it should not be | - | ||||||
372 | relied upon as the sole means for providing critical information. | - | ||||||
373 | - | |||||||
374 | On Windows, the \a millisecondsTimeoutHint is usually ignored by the system | - | ||||||
375 | when the application has focus. | - | ||||||
376 | - | |||||||
377 | On \macos, the Growl notification system must be installed for this function to | - | ||||||
378 | display messages. | - | ||||||
379 | - | |||||||
380 | Has been turned into a slot in Qt 5.2. | - | ||||||
381 | - | |||||||
382 | \sa show(), supportsMessages() | - | ||||||
383 | */ | - | ||||||
384 | void QSystemTrayIcon::showMessage(const QString& title, const QString& msg, | - | ||||||
385 | QSystemTrayIcon::MessageIcon icon, int msecs) | - | ||||||
386 | { | - | ||||||
387 | Q_D(QSystemTrayIcon); | - | ||||||
388 | if (d->visible) | - | ||||||
389 | d->showMessage_sys(title, msg, icon, msecs); | - | ||||||
390 | } | - | ||||||
391 | - | |||||||
392 | void QSystemTrayIconPrivate::_q_emitActivated(QPlatformSystemTrayIcon::ActivationReason reason) | - | ||||||
393 | { | - | ||||||
394 | Q_Q(QSystemTrayIcon); | - | ||||||
395 | emit q->activated(static_cast<QSystemTrayIcon::ActivationReason>(reason)); | - | ||||||
396 | } | - | ||||||
397 | - | |||||||
398 | ////////////////////////////////////////////////////////////////////// | - | ||||||
399 | static QBalloonTip *theSolitaryBalloonTip = 0; | - | ||||||
400 | - | |||||||
401 | void QBalloonTip::showBalloon(QSystemTrayIcon::MessageIcon icon, const QString& title, | - | ||||||
402 | const QString& message, QSystemTrayIcon *trayIcon, | - | ||||||
403 | const QPoint& pos, int timeout, bool showArrow) | - | ||||||
404 | { | - | ||||||
405 | hideBalloon(); | - | ||||||
406 | if (message.isEmpty() && title.isEmpty()) | - | ||||||
407 | return; | - | ||||||
408 | - | |||||||
409 | theSolitaryBalloonTip = new QBalloonTip(icon, title, message, trayIcon); | - | ||||||
410 | if (timeout < 0) | - | ||||||
411 | timeout = 10000; //10 s default | - | ||||||
412 | theSolitaryBalloonTip->balloon(pos, timeout, showArrow); | - | ||||||
413 | } | - | ||||||
414 | - | |||||||
415 | void QBalloonTip::hideBalloon() | - | ||||||
416 | { | - | ||||||
417 | if (!theSolitaryBalloonTip) | - | ||||||
418 | return; | - | ||||||
419 | theSolitaryBalloonTip->hide(); | - | ||||||
420 | delete theSolitaryBalloonTip; | - | ||||||
421 | theSolitaryBalloonTip = 0; | - | ||||||
422 | } | - | ||||||
423 | - | |||||||
424 | void QBalloonTip::updateBalloonPosition(const QPoint& pos) | - | ||||||
425 | { | - | ||||||
426 | if (!theSolitaryBalloonTip) | - | ||||||
427 | return; | - | ||||||
428 | theSolitaryBalloonTip->hide(); | - | ||||||
429 | theSolitaryBalloonTip->balloon(pos, 0, theSolitaryBalloonTip->showArrow); | - | ||||||
430 | } | - | ||||||
431 | - | |||||||
432 | bool QBalloonTip::isBalloonVisible() | - | ||||||
433 | { | - | ||||||
434 | return theSolitaryBalloonTip; | - | ||||||
435 | } | - | ||||||
436 | - | |||||||
437 | QBalloonTip::QBalloonTip(QSystemTrayIcon::MessageIcon icon, const QString& title, | - | ||||||
438 | const QString& message, QSystemTrayIcon *ti) | - | ||||||
439 | : QWidget(0, Qt::ToolTip), trayIcon(ti), timerId(-1), | - | ||||||
showArrow(true) | ||||||||
440 | { | - | ||||||
441 | setAttribute(Qt::WA_DeleteOnClose); | - | ||||||
442 | QObject::connect(ti, SIGNAL(destroyed()), this, SLOT(close())); | - | ||||||
443 | - | |||||||
444 | QLabel *titleLabel = new QLabel; | - | ||||||
445 | titleLabel->installEventFilter(this); | - | ||||||
446 | titleLabel->setText(title); | - | ||||||
447 | QFont f = titleLabel->font(); | - | ||||||
448 | f.setBold(true); | - | ||||||
449 | #ifdef Q_OS_WINCE | - | ||||||
450 | f.setPointSize(f.pointSize() - 2); | - | ||||||
451 | #endif | - | ||||||
452 | titleLabel->setFont(f); | - | ||||||
453 | titleLabel->setTextFormat(Qt::PlainText); // to maintain compat with windows | - | ||||||
454 | - | |||||||
455 | #ifdef Q_OS_WINCE | - | ||||||
456 | const int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize); | - | ||||||
457 | const int closeButtonSize = style()->pixelMetric(QStyle::PM_SmallIconSize) - 2; | - | ||||||
458 | #else | - | ||||||
459 | const int iconSize = 18; | - | ||||||
460 | const int closeButtonSize = 15; | - | ||||||
461 | #endif | - | ||||||
462 | - | |||||||
463 | QPushButton *closeButton = new QPushButton; | - | ||||||
464 | closeButton->setIcon(style()->standardIcon(QStyle::SP_TitleBarCloseButton)); | - | ||||||
465 | closeButton->setIconSize(QSize(closeButtonSize, closeButtonSize)); | - | ||||||
466 | closeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); | - | ||||||
467 | closeButton->setFixedSize(closeButtonSize, closeButtonSize); | - | ||||||
468 | QObject::connect(closeButton, SIGNAL(clicked()), this, SLOT(close())); | - | ||||||
469 | - | |||||||
470 | QLabel *msgLabel = new QLabel; | - | ||||||
471 | #ifdef Q_OS_WINCE | - | ||||||
472 | f.setBold(false); | - | ||||||
473 | msgLabel->setFont(f); | - | ||||||
474 | #endif | - | ||||||
475 | msgLabel->installEventFilter(this); | - | ||||||
476 | msgLabel->setText(message); | - | ||||||
477 | msgLabel->setTextFormat(Qt::PlainText); | - | ||||||
478 | msgLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft); | - | ||||||
479 | - | |||||||
480 | // smart size for the message label | - | ||||||
481 | #ifdef Q_OS_WINCE | - | ||||||
482 | int limit = QApplication::desktop()->availableGeometry(msgLabel).size().width() / 2; | - | ||||||
483 | #else | - | ||||||
484 | int limit = QApplication::desktop()->availableGeometry(msgLabel).size().width() / 3; | - | ||||||
485 | #endif | - | ||||||
486 | if (msgLabel->sizeHint().width() > limit) {
| 0 | ||||||
487 | msgLabel->setWordWrap(true); | - | ||||||
488 | if (msgLabel->sizeHint().width() > limit) {
| 0 | ||||||
489 | msgLabel->d_func()->ensureTextControl(); | - | ||||||
490 | if (QWidgetTextControl *control = msgLabel->d_func()->control) {
| 0 | ||||||
491 | QTextOption opt = control->document()->defaultTextOption(); | - | ||||||
492 | opt.setWrapMode(QTextOption::WrapAnywhere); | - | ||||||
493 | control->document()->setDefaultTextOption(opt); | - | ||||||
494 | } never executed: end of block | 0 | ||||||
495 | } never executed: end of block | 0 | ||||||
496 | #ifdef Q_OS_WINCE | - | ||||||
497 | // Make sure that the text isn't wrapped "somewhere" in the balloon widget | - | ||||||
498 | // in the case that we have a long title label. | - | ||||||
499 | setMaximumWidth(limit); | - | ||||||
500 | #else | - | ||||||
501 | // Here we allow the text being much smaller than the balloon widget | - | ||||||
502 | // to emulate the weird standard windows behavior. | - | ||||||
503 | msgLabel->setFixedSize(limit, msgLabel->heightForWidth(limit)); | - | ||||||
504 | #endif | - | ||||||
505 | } never executed: end of block | 0 | ||||||
506 | - | |||||||
507 | QIcon si; | - | ||||||
508 | switch (icon) { | - | ||||||
509 | case QSystemTrayIcon::Warning: never executed: case QSystemTrayIcon::Warning: | 0 | ||||||
510 | si = style()->standardIcon(QStyle::SP_MessageBoxWarning); | - | ||||||
511 | break; never executed: break; | 0 | ||||||
512 | case QSystemTrayIcon::Critical: never executed: case QSystemTrayIcon::Critical: | 0 | ||||||
513 | si = style()->standardIcon(QStyle::SP_MessageBoxCritical); | - | ||||||
514 | break; never executed: break; | 0 | ||||||
515 | case QSystemTrayIcon::Information: never executed: case QSystemTrayIcon::Information: | 0 | ||||||
516 | si = style()->standardIcon(QStyle::SP_MessageBoxInformation); | - | ||||||
517 | break; never executed: break; | 0 | ||||||
518 | case QSystemTrayIcon::NoIcon: never executed: case QSystemTrayIcon::NoIcon: | 0 | ||||||
519 | default: never executed: default: | 0 | ||||||
520 | break; never executed: break; | 0 | ||||||
521 | } | - | ||||||
522 | - | |||||||
523 | QGridLayout *layout = new QGridLayout; | - | ||||||
524 | if (!si.isNull()) {
| 0 | ||||||
525 | QLabel *iconLabel = new QLabel; | - | ||||||
526 | iconLabel->setPixmap(si.pixmap(iconSize, iconSize)); | - | ||||||
527 | iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); | - | ||||||
528 | iconLabel->setMargin(2); | - | ||||||
529 | layout->addWidget(iconLabel, 0, 0); | - | ||||||
530 | layout->addWidget(titleLabel, 0, 1); | - | ||||||
531 | } else { never executed: end of block | 0 | ||||||
532 | layout->addWidget(titleLabel, 0, 0, 1, 2); | - | ||||||
533 | } never executed: end of block | 0 | ||||||
534 | - | |||||||
535 | layout->addWidget(closeButton, 0, 2); | - | ||||||
536 | layout->addWidget(msgLabel, 1, 0, 1, 3); | - | ||||||
537 | layout->setSizeConstraint(QLayout::SetFixedSize); | - | ||||||
538 | layout->setMargin(3); | - | ||||||
539 | setLayout(layout); | - | ||||||
540 | - | |||||||
541 | QPalette pal = palette(); | - | ||||||
542 | pal.setColor(QPalette::Window, QColor(0xff, 0xff, 0xe1)); | - | ||||||
543 | pal.setColor(QPalette::WindowText, Qt::black); | - | ||||||
544 | setPalette(pal); | - | ||||||
545 | } never executed: end of block | 0 | ||||||
546 | - | |||||||
547 | QBalloonTip::~QBalloonTip() | - | ||||||
548 | { | - | ||||||
549 | theSolitaryBalloonTip = 0; | - | ||||||
550 | } | - | ||||||
551 | - | |||||||
552 | void QBalloonTip::paintEvent(QPaintEvent *) | - | ||||||
553 | { | - | ||||||
554 | QPainter painter(this); | - | ||||||
555 | painter.drawPixmap(rect(), pixmap); | - | ||||||
556 | } | - | ||||||
557 | - | |||||||
558 | void QBalloonTip::resizeEvent(QResizeEvent *ev) | - | ||||||
559 | { | - | ||||||
560 | QWidget::resizeEvent(ev); | - | ||||||
561 | } | - | ||||||
562 | - | |||||||
563 | void QBalloonTip::balloon(const QPoint& pos, int msecs, bool showArrow) | - | ||||||
564 | { | - | ||||||
565 | this->showArrow = showArrow; | - | ||||||
566 | QRect scr = QApplication::desktop()->screenGeometry(pos); | - | ||||||
567 | QSize sh = sizeHint(); | - | ||||||
568 | const int border = 1; | - | ||||||
569 | const int ah = 18, ao = 18, aw = 18, rc = 7; | - | ||||||
570 | bool arrowAtTop = (pos.y() + sh.height() + ah < scr.height()); | - | ||||||
571 | bool arrowAtLeft = (pos.x() + sh.width() - ao < scr.width()); | - | ||||||
572 | setContentsMargins(border + 3, border + (arrowAtTop ? ah : 0) + 2, border + 3, border + (arrowAtTop ? 0 : ah) + 2); | - | ||||||
573 | updateGeometry(); | - | ||||||
574 | sh = sizeHint(); | - | ||||||
575 | - | |||||||
576 | int ml, mr, mt, mb; | - | ||||||
577 | QSize sz = sizeHint(); | - | ||||||
578 | if (!arrowAtTop) { | - | ||||||
579 | ml = mt = 0; | - | ||||||
580 | mr = sz.width() - 1; | - | ||||||
581 | mb = sz.height() - ah - 1; | - | ||||||
582 | } else { | - | ||||||
583 | ml = 0; | - | ||||||
584 | mt = ah; | - | ||||||
585 | mr = sz.width() - 1; | - | ||||||
586 | mb = sz.height() - 1; | - | ||||||
587 | } | - | ||||||
588 | - | |||||||
589 | QPainterPath path; | - | ||||||
590 | #if defined(QT_NO_XSHAPE) && defined(Q_DEAD_CODE_FROM_QT4_X11) | - | ||||||
591 | // XShape is required for setting the mask, so we just | - | ||||||
592 | // draw an ugly square when its not available | - | ||||||
593 | path.moveTo(0, 0); | - | ||||||
594 | path.lineTo(sz.width() - 1, 0); | - | ||||||
595 | path.lineTo(sz.width() - 1, sz.height() - 1); | - | ||||||
596 | path.lineTo(0, sz.height() - 1); | - | ||||||
597 | path.lineTo(0, 0); | - | ||||||
598 | move(qMax(pos.x() - sz.width(), scr.left()), pos.y()); | - | ||||||
599 | #else | - | ||||||
600 | path.moveTo(ml + rc, mt); | - | ||||||
601 | if (arrowAtTop && arrowAtLeft) { | - | ||||||
602 | if (showArrow) { | - | ||||||
603 | path.lineTo(ml + ao, mt); | - | ||||||
604 | path.lineTo(ml + ao, mt - ah); | - | ||||||
605 | path.lineTo(ml + ao + aw, mt); | - | ||||||
606 | } | - | ||||||
607 | move(qMax(pos.x() - ao, scr.left() + 2), pos.y()); | - | ||||||
608 | } else if (arrowAtTop && !arrowAtLeft) { | - | ||||||
609 | if (showArrow) { | - | ||||||
610 | path.lineTo(mr - ao - aw, mt); | - | ||||||
611 | path.lineTo(mr - ao, mt - ah); | - | ||||||
612 | path.lineTo(mr - ao, mt); | - | ||||||
613 | } | - | ||||||
614 | move(qMin(pos.x() - sh.width() + ao, scr.right() - sh.width() - 2), pos.y()); | - | ||||||
615 | } | - | ||||||
616 | path.lineTo(mr - rc, mt); | - | ||||||
617 | path.arcTo(QRect(mr - rc*2, mt, rc*2, rc*2), 90, -90); | - | ||||||
618 | path.lineTo(mr, mb - rc); | - | ||||||
619 | path.arcTo(QRect(mr - rc*2, mb - rc*2, rc*2, rc*2), 0, -90); | - | ||||||
620 | if (!arrowAtTop && !arrowAtLeft) { | - | ||||||
621 | if (showArrow) { | - | ||||||
622 | path.lineTo(mr - ao, mb); | - | ||||||
623 | path.lineTo(mr - ao, mb + ah); | - | ||||||
624 | path.lineTo(mr - ao - aw, mb); | - | ||||||
625 | } | - | ||||||
626 | move(qMin(pos.x() - sh.width() + ao, scr.right() - sh.width() - 2), | - | ||||||
627 | pos.y() - sh.height()); | - | ||||||
628 | } else if (!arrowAtTop && arrowAtLeft) { | - | ||||||
629 | if (showArrow) { | - | ||||||
630 | path.lineTo(ao + aw, mb); | - | ||||||
631 | path.lineTo(ao, mb + ah); | - | ||||||
632 | path.lineTo(ao, mb); | - | ||||||
633 | } | - | ||||||
634 | move(qMax(pos.x() - ao, scr.x() + 2), pos.y() - sh.height()); | - | ||||||
635 | } | - | ||||||
636 | path.lineTo(ml + rc, mb); | - | ||||||
637 | path.arcTo(QRect(ml, mb - rc*2, rc*2, rc*2), -90, -90); | - | ||||||
638 | path.lineTo(ml, mt + rc); | - | ||||||
639 | path.arcTo(QRect(ml, mt, rc*2, rc*2), 180, -90); | - | ||||||
640 | - | |||||||
641 | // Set the mask | - | ||||||
642 | QBitmap bitmap = QBitmap(sizeHint()); | - | ||||||
643 | bitmap.fill(Qt::color0); | - | ||||||
644 | QPainter painter1(&bitmap); | - | ||||||
645 | painter1.setPen(QPen(Qt::color1, border)); | - | ||||||
646 | painter1.setBrush(QBrush(Qt::color1)); | - | ||||||
647 | painter1.drawPath(path); | - | ||||||
648 | setMask(bitmap); | - | ||||||
649 | #endif | - | ||||||
650 | - | |||||||
651 | // Draw the border | - | ||||||
652 | pixmap = QPixmap(sz); | - | ||||||
653 | QPainter painter2(&pixmap); | - | ||||||
654 | painter2.setPen(QPen(palette().color(QPalette::Window).darker(160), border)); | - | ||||||
655 | painter2.setBrush(palette().color(QPalette::Window)); | - | ||||||
656 | painter2.drawPath(path); | - | ||||||
657 | - | |||||||
658 | if (msecs > 0) | - | ||||||
659 | timerId = startTimer(msecs); | - | ||||||
660 | show(); | - | ||||||
661 | } | - | ||||||
662 | - | |||||||
663 | void QBalloonTip::mousePressEvent(QMouseEvent *e) | - | ||||||
664 | { | - | ||||||
665 | close(); | - | ||||||
666 | if(e->button() == Qt::LeftButton) | - | ||||||
667 | emit trayIcon->messageClicked(); | - | ||||||
668 | } | - | ||||||
669 | - | |||||||
670 | void QBalloonTip::timerEvent(QTimerEvent *e) | - | ||||||
671 | { | - | ||||||
672 | if (e->timerId() == timerId) { | - | ||||||
673 | killTimer(timerId); | - | ||||||
674 | if (!underMouse()) | - | ||||||
675 | close(); | - | ||||||
676 | return; | - | ||||||
677 | } | - | ||||||
678 | QWidget::timerEvent(e); | - | ||||||
679 | } | - | ||||||
680 | - | |||||||
681 | ////////////////////////////////////////////////////////////////////// | - | ||||||
682 | void QSystemTrayIconPrivate::install_sys_qpa() | - | ||||||
683 | { | - | ||||||
684 | qpa_sys->init(); | - | ||||||
685 | QObject::connect(qpa_sys, SIGNAL(activated(QPlatformSystemTrayIcon::ActivationReason)), | - | ||||||
686 | q_func(), SLOT(_q_emitActivated(QPlatformSystemTrayIcon::ActivationReason))); | - | ||||||
687 | QObject::connect(qpa_sys, &QPlatformSystemTrayIcon::messageClicked, | - | ||||||
688 | q_func(), &QSystemTrayIcon::messageClicked); | - | ||||||
689 | updateMenu_sys(); | - | ||||||
690 | updateIcon_sys(); | - | ||||||
691 | updateToolTip_sys(); | - | ||||||
692 | } | - | ||||||
693 | - | |||||||
694 | void QSystemTrayIconPrivate::remove_sys_qpa() | - | ||||||
695 | { | - | ||||||
696 | QObject::disconnect(qpa_sys, SIGNAL(activated(QPlatformSystemTrayIcon::ActivationReason)), | - | ||||||
697 | q_func(), SLOT(_q_emitActivated(QPlatformSystemTrayIcon::ActivationReason))); | - | ||||||
698 | QObject::disconnect(qpa_sys, &QPlatformSystemTrayIcon::messageClicked, | - | ||||||
699 | q_func(), &QSystemTrayIcon::messageClicked); | - | ||||||
700 | qpa_sys->cleanup(); | - | ||||||
701 | } | - | ||||||
702 | - | |||||||
703 | QRect QSystemTrayIconPrivate::geometry_sys_qpa() const | - | ||||||
704 | { | - | ||||||
705 | return qpa_sys->geometry(); | - | ||||||
706 | } | - | ||||||
707 | - | |||||||
708 | void QSystemTrayIconPrivate::updateIcon_sys_qpa() | - | ||||||
709 | { | - | ||||||
710 | qpa_sys->updateIcon(icon); | - | ||||||
711 | } | - | ||||||
712 | - | |||||||
713 | void QSystemTrayIconPrivate::updateMenu_sys_qpa() | - | ||||||
714 | { | - | ||||||
715 | if (menu) { | - | ||||||
716 | addPlatformMenu(menu); | - | ||||||
717 | qpa_sys->updateMenu(menu->platformMenu()); | - | ||||||
718 | } | - | ||||||
719 | } | - | ||||||
720 | - | |||||||
721 | void QSystemTrayIconPrivate::updateToolTip_sys_qpa() | - | ||||||
722 | { | - | ||||||
723 | qpa_sys->updateToolTip(toolTip); | - | ||||||
724 | } | - | ||||||
725 | - | |||||||
726 | void QSystemTrayIconPrivate::showMessage_sys_qpa(const QString &title, | - | ||||||
727 | const QString &message, | - | ||||||
728 | QSystemTrayIcon::MessageIcon icon, | - | ||||||
729 | int msecs) | - | ||||||
730 | { | - | ||||||
731 | QIcon notificationIcon; | - | ||||||
732 | switch (icon) { | - | ||||||
733 | case QSystemTrayIcon::Information: | - | ||||||
734 | notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxInformation); | - | ||||||
735 | break; | - | ||||||
736 | case QSystemTrayIcon::Warning: | - | ||||||
737 | notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxWarning); | - | ||||||
738 | break; | - | ||||||
739 | case QSystemTrayIcon::Critical: | - | ||||||
740 | notificationIcon = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical); | - | ||||||
741 | break; | - | ||||||
742 | default: | - | ||||||
743 | break; | - | ||||||
744 | } | - | ||||||
745 | qpa_sys->showMessage(title, message, notificationIcon, | - | ||||||
746 | static_cast<QPlatformSystemTrayIcon::MessageIcon>(icon), msecs); | - | ||||||
747 | } | - | ||||||
748 | - | |||||||
749 | void QSystemTrayIconPrivate::addPlatformMenu(QMenu *menu) const | - | ||||||
750 | { | - | ||||||
751 | if (menu->platformMenu()) | - | ||||||
752 | return; // The platform menu already exists. | - | ||||||
753 | - | |||||||
754 | // The recursion depth is the same as menu depth, so should not | - | ||||||
755 | // be higher than 3 levels. | - | ||||||
756 | QListIterator<QAction *> it(menu->actions()); | - | ||||||
757 | while (it.hasNext()) { | - | ||||||
758 | QAction *action = it.next(); | - | ||||||
759 | if (action->menu()) | - | ||||||
760 | addPlatformMenu(action->menu()); | - | ||||||
761 | } | - | ||||||
762 | - | |||||||
763 | // This menu should be processed *after* its children, otherwise | - | ||||||
764 | // setMenu() is not called on respective QPlatformMenuItems. | - | ||||||
765 | QPlatformMenu *platformMenu = qpa_sys->createMenu(); | - | ||||||
766 | if (platformMenu) | - | ||||||
767 | menu->setPlatformMenu(platformMenu); | - | ||||||
768 | } | - | ||||||
769 | - | |||||||
770 | QT_END_NAMESPACE | - | ||||||
771 | - | |||||||
772 | #endif // QT_NO_SYSTEMTRAYICON | - | ||||||
773 | - | |||||||
774 | #include "moc_qsystemtrayicon.cpp" | - | ||||||
775 | #include "moc_qsystemtrayicon_p.cpp" | - | ||||||
Source code | Switch to Preprocessed file |