BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
systemtrayicon.cpp
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include "gui/systemtrayicon.h"
32 
33 #include "definitions/definitions.h"
34 #include "miscellaneous/settings.h"
35 #include "miscellaneous/application.h"
36 
37 #include <QPainter>
38 #include <QTimer>
39 
40 
41 #if defined(Q_OS_WIN)
42 TrayIconMenu::TrayIconMenu(const QString &title, QWidget *parent)
43  : QMenu(title, parent) {
44 }
45 
46 TrayIconMenu::~TrayIconMenu() {
47 }
48 
49 bool TrayIconMenu::event(QEvent *event) {
50  if (Application::activeModalWidget() != NULL && event->type() == QEvent::Show) {
51  QTimer::singleShot(0, this, SLOT(hide()));
52  qApp->trayIcon()->showMessage(APP_LONG_NAME,
53  tr("Close opened modal dialogs first."),
54  QSystemTrayIcon::Warning);
55  }
56 
57  return QMenu::event(event);
58 }
59 #endif
60 
61 SystemTrayIcon::SystemTrayIcon(const QString &icon, QObject *parent)
62  : QSystemTrayIcon(parent),
63  m_bubbleClickTarget(NULL),
64  m_bubbleClickSlot(NULL) {
65  qDebug("Creating SystemTrayIcon instance.");
66 
67  setIcon(QIcon(icon));
68 
69  // Create necessary connections.
70  connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
71  this, SLOT(onActivated(QSystemTrayIcon::ActivationReason)));
72 }
73 
74 SystemTrayIcon::~SystemTrayIcon() {
75  qDebug("Destroying SystemTrayIcon instance.");
76 }
77 
78 void SystemTrayIcon::showMessage(const QString &title,
79  const QString &message,
80  QSystemTrayIcon::MessageIcon icon,
81  int milliseconds_timeout_hint,
82  QObject *click_target,
83  const char *click_slot) {
84  if (m_bubbleClickTarget != NULL && m_bubbleClickSlot != NULL) {
85  // Disconnect previous bubble click signalling.
86  disconnect(this, SIGNAL(messageClicked()), m_bubbleClickTarget, m_bubbleClickSlot);
87  }
88 
89  m_bubbleClickSlot = (char*) click_slot;
90  m_bubbleClickTarget = click_target;
91 
92  if (click_target != NULL && click_slot != NULL) {
93  // Establish new connection for bubble click.
94  connect(this, SIGNAL(messageClicked()), click_target, click_slot);
95  }
96 
97  // NOTE: If connections do not work, then use QMetaObject::invokeMethod(...).
98 
99  QSystemTrayIcon::showMessage(title, message, icon, milliseconds_timeout_hint);
100 }
101 
103  return QSystemTrayIcon::isSystemTrayAvailable() && QSystemTrayIcon::supportsMessages();
104 }
105 
106 void SystemTrayIcon::showPrivate() {
107  // Display the tray icon.
108  QSystemTrayIcon::show();
109  qDebug("Tray icon displayed.");
110 }
111 
113 #if defined(Q_OS_WIN)
114  // Show immediately.
115  qDebug("Showing tray icon immediately.");
116  showPrivate();
117 #else
118  // Delay avoids race conditions and tray icon is properly displayed.
119  qDebug("Showing tray icon with 1000 ms delay.");
120  QTimer::singleShot(TRAY_ICON_DELAY, this, SLOT(showPrivate()));
121 #endif
122 
123  qApp->setQuitOnLastWindowClosed(false);
124 }
125 
126 void SystemTrayIcon::onActivated(QSystemTrayIcon::ActivationReason reason) {
127 #if defined(Q_OS_WIN)
128  if (Application::activeModalWidget() != NULL && reason != Unknown) {
129  qApp->trayIcon()->showMessage(APP_LONG_NAME,
130  tr("Close opened modal dialogs first."),
131  QSystemTrayIcon::Warning);
132  return;
133  }
134 #endif
135  switch (reason) {
136  case QSystemTrayIcon::Trigger:
137  emit leftMouseClicked();
138  break;
139 
140  default:
141  break;
142  }
143 }
SystemTrayIcon(const QString &icon, QObject *parent=0)
Constructor.
void leftMouseClicked()
Emitted if user clicks tray icon with left mouse button.
void showMessage(const QString &title, const QString &message, MessageIcon icon=Information, int milliseconds_timeout_hint=TRAY_ICON_BUBBLE_TIMEOUT, QObject *click_target=NULL, const char *click_slot=NULL)
Displays new balloon tip with message.
void show()
Displays tray icon.
static bool isSystemTrayAvailable()
Indicates whether tray icon is supported.