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

Generated on 22 Feb 2010 for Vidalia by  doxygen 1.6.1