TrayIcon.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file TrayIcon.cpp
00013 ** \version $Id: TrayIcon.cpp 3735 2009-04-28 20:28:01Z edmanm $
00014 ** \brief Places an icon with context menu in the system notification area
00015 */
00016 
00017 #include "TrayIcon.h"
00018 
00019 #include <QSysInfo>
00020 
00021 
00022 #if defined(Q_WS_MAC)
00023 /* Exported by Qt, but not declared in the header files. 
00024  * See http://doc.trolltech.com/exportedfunctions.html  */
00025 void qt_mac_set_dock_menu(QMenu *menu);
00026 #endif
00027 
00028 
00029 /** Default constructor. */
00030 TrayIcon::TrayIcon(QWidget *parent)
00031   : TrayIconImpl(parent)
00032 {
00033 }
00034 
00035 /** Catches and handles mouse-related events. */
00036 bool
00037 TrayIcon::event(QEvent *event)
00038 {
00039   switch (event->type()) {
00040     case QEvent::MouseButtonDblClick:
00041       mouseButtonDblClick((QMouseEvent *)event);
00042       break;
00043 
00044     default:
00045       return TrayIconImpl::event(event);
00046   }
00047   event->accept();
00048   return true;
00049 }
00050 
00051 /** Responds to a mouse button double-click. On all platforms, we just emit a
00052  * signal and let the owner of the tray icon decide if they want to do
00053  * anything. */
00054 void
00055 TrayIcon::mouseButtonDblClick(QMouseEvent *event)
00056 {
00057   if (event->button() == Qt::LeftButton) {
00058     emit doubleClicked();
00059   }
00060 }
00061 
00062 /** Update the tray icon's image and tooltip. */
00063 void
00064 TrayIcon::update(const QString &iconFile, const QString &toolTip)
00065 {
00066   setIcon(iconFile);
00067   setToolTip(toolTip);
00068 }
00069 
00070 /** Call the platform's tray icon implementation to show the tray icon. */
00071 void
00072 TrayIcon::show()
00073 {
00074   TrayIconImpl::show();
00075 }
00076 
00077 /** Call the platform's tray icon implementation to hide the tray icon. */
00078 void
00079 TrayIcon::hide()
00080 {
00081   TrayIconImpl::hide();
00082 }
00083 
00084 /** Call the platform's tray icon implementation to update the icon's tooltip.*/
00085 void
00086 TrayIcon::setToolTip(const QString &toolTip)
00087 {
00088   TrayIconImpl::setToolTip(toolTip);
00089 }
00090 
00091 /** Call the platform's tray icon implementation to update the icon image. */
00092 void
00093 TrayIcon::setIcon(const QString &iconFile)
00094 {
00095   TrayIconImpl::setIcon(iconFile);
00096 }
00097 
00098 /** Sets the context menu displayed when the tray icon is selected. On Mac,
00099  * the context menu is displayed when the dock icon is clicked. */
00100 void
00101 TrayIcon::setContextMenu(QMenu *menu)
00102 {
00103 #if defined(Q_WS_MAC)
00104   qt_mac_set_dock_menu(menu);
00105 #else
00106   TrayIconImpl::setContextMenu(menu);
00107 #endif
00108 }
00109 
00110 /** Displays a balloon message next to the tray icon. */
00111 void
00112 TrayIcon::showBalloonMessage(const QString &title, const QString &message,
00113                              BalloonMessageIcon balloonIcon)
00114 {
00115 #if defined(Q_WS_MAC)
00116   Q_UNUSED(title)
00117   Q_UNUSED(message)
00118   Q_UNUSED(balloonIcon)
00119 #else
00120   QSystemTrayIcon::MessageIcon icon;
00121   switch (balloonIcon) {
00122     case NoIcon:   icon = QSystemTrayIcon::NoIcon; break;
00123     case Warning:  icon = QSystemTrayIcon::Warning; break;
00124     case Critical: icon = QSystemTrayIcon::Critical; break;
00125     default:  icon = QSystemTrayIcon::Information; break;
00126   }
00127   TrayIconImpl::showMessage(title, message, icon);
00128 #endif
00129 }
00130 
00131 /** Returns true if the current platform and tray icon implementation supports
00132  * tray icons. */
00133 bool
00134 TrayIcon::isTrayIconSupported()
00135 {
00136 #if defined(Q_WS_WIN) || defined(Q_WS_MAC)
00137   /* We always have a tray on Win32 or a dock on OS X */
00138   return true;
00139 #else
00140   /* Ask Qt if there is a tray available */
00141   return QSystemTrayIcon::isSystemTrayAvailable();
00142 #endif
00143 }
00144 
00145 /** Returns true if the current platform and tray icon implementation supports
00146  * tray icon balloon messages. */
00147 bool
00148 TrayIcon::supportsBalloonMessages()
00149 {
00150 #if defined(Q_WS_WIN)
00151   return (QSystemTrayIcon::supportsMessages()
00152             && QSysInfo::WindowsVersion > QSysInfo::WV_2000);
00153 #elif defined(Q_WS_MAC)
00154   return false;
00155 #else
00156   return QSystemTrayIcon::supportsMessages();
00157 #endif
00158 }
00159 

Generated on 31 Mar 2010 for Vidalia by  doxygen 1.6.1