kalarm Library API Documentation

traywindow.cpp

00001 /*
00002  *  traywindow.cpp  -  the KDE system tray applet
00003  *  Program:  kalarm
00004  *  (C) 2002 by David Jarvie  software@astrojar.org.uk
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 #include "kalarm.h"
00021 #include <stdlib.h>
00022 
00023 #include <qtooltip.h>
00024 #include <qlabel.h>
00025 
00026 #include <kapplication.h>
00027 #include <klocale.h>
00028 #include <kiconloader.h>
00029 #include <kstdaction.h>
00030 #include <kaboutdata.h>
00031 #include <kpopupmenu.h>
00032 #include <kmessagebox.h>
00033 #include <kstandarddirs.h>
00034 #include <kdebug.h>
00035 
00036 #include "kalarmapp.h"
00037 #include "mainwindow.h"
00038 #include "messagewin.h"
00039 #include "daemongui.h"
00040 #include "prefsettings.h"
00041 #include "traywindow.moc"
00042 
00043 
00044 /*=============================================================================
00045 = Class: TrayWindow
00046 = The KDE system tray window.
00047 =============================================================================*/
00048 
00049 TrayWindow::TrayWindow(KAlarmMainWindow* parent, const char* name)
00050         : KSystemTray((theApp()->runInSystemTray() ? parent : 0), name),
00051           mAssocMainWindow(parent),
00052           mQuitReplaced(false),
00053           mActionCollection(this)
00054 {
00055         kdDebug(5950) << "TrayWindow::TrayWindow()\n";
00056         // Set up GUI icons
00057 //      KGlobal::iconLoader()->addAppDir(kapp->aboutData()->appName());
00058 //      mPixmapEnabled  = KGlobal::iconLoader()->loadIcon("kalarm", KIcon::Panel);
00059 //      mPixmapDisabled = KGlobal::iconLoader()->loadIcon("kalarm_disabled", KIcon::Panel);
00060         mPixmapEnabled  = BarIcon("kalarm");
00061         mPixmapDisabled = BarIcon("kalarm_disabled");
00062         if (mPixmapEnabled.isNull() || mPixmapDisabled.isNull())
00063                 KMessageBox::sorry(this, i18n("Can't load system tray icon!"),
00064                                          i18n("%1 Error").arg(kapp->aboutData()->programName()));
00065 
00066         mActionQuit = KStdAction::quit(this, SLOT(slotQuit()), &mActionCollection);
00067 
00068         // Set up the context menu
00069         ActionAlarmsEnabled* a = theApp()->actionAlarmEnable();
00070         mAlarmsEnabledId = a->itemId(a->plug(contextMenu()));
00071         connect(a, SIGNAL(alarmsEnabledChange(bool)), this, SLOT(setEnabledStatus(bool)));
00072         theApp()->actionPreferences()->plug(contextMenu());
00073         theApp()->actionDaemonControl()->plug(contextMenu());
00074 
00075         // Set icon to correspond with the alarms enabled menu status
00076         DaemonGuiHandler* daemonGui = theApp()->daemonGuiHandler();
00077         daemonGui->checkStatus();
00078         setEnabledStatus(daemonGui->monitoringAlarms());
00079 
00080         QToolTip::add(this, kapp->aboutData()->programName());
00081 }
00082 
00083 TrayWindow::~TrayWindow()
00084 {
00085         kdDebug(5950) << "TrayWindow::~TrayWindow()\n";
00086         theApp()->removeWindow(this);
00087         emit deleted();
00088 }
00089 
00090 /******************************************************************************
00091 * Called just before the context menu is displayed.
00092 * Modify the Quit context menu item to only close the system tray widget.
00093 */
00094 void TrayWindow::contextMenuAboutToShow(KPopupMenu* menu)
00095 {
00096         if (!mQuitReplaced)
00097         {
00098                 // Prevent Quit from quitting the program
00099                 QString quitText = mActionQuit->text();
00100                 for (unsigned n = 0;  n < menu->count();  ++n)
00101                 {
00102                         QString txt = menu->text(menu->idAt(n));
00103                         if (txt.startsWith(quitText))
00104                         {
00105                                 menu->removeItemAt(n);
00106                                 break;
00107                         }
00108                 }
00109                 mActionQuit->plug(menu);
00110                 mQuitReplaced = true;
00111         }
00112 
00113         // Update the Alarms Enabled item status
00114         theApp()->daemonGuiHandler()->checkStatus();
00115 }
00116 
00117 /******************************************************************************
00118 * Called when the Quit context menu item is selected.
00119 * Closes the system tray window, and if in 'run in system tray' mode closes all
00120 * main windows, but does not exit the program if other windows are still open.
00121 */
00122 void TrayWindow::slotQuit()
00123 {
00124         kdDebug(5950)<<"TrayWindow::slotQuit()\n";
00125         if (theApp()->runInSystemTray())
00126         {
00127                 if (!MessageWin::instanceCount())
00128                         theApp()->quit();
00129                 else
00130                         KAlarmMainWindow::closeAll();
00131         }
00132         theApp()->displayTrayIcon(false);
00133 }
00134 
00135 /******************************************************************************
00136 * Called when the Alarms Enabled action status has changed.
00137 * Updates the alarms enabled menu item check state, and the icon pixmap.
00138 */
00139 void TrayWindow::setEnabledStatus(bool status)
00140 {
00141         kdDebug(5950)<<"TrayWindow::setEnabledStatus(" << (int)status << ")\n";
00142         setPixmap(status ? mPixmapEnabled : mPixmapDisabled);
00143         contextMenu()->setItemChecked(mAlarmsEnabledId, status);
00144 }
00145 
00146 /******************************************************************************
00147 *  Called when the mouse is clicked over the panel icon.
00148 *  A left click displays the KAlarm main window.
00149 */
00150 void TrayWindow::mousePressEvent(QMouseEvent* e)
00151 {
00152 
00153         if (e->button() == LeftButton  &&  !theApp()->runInSystemTray())
00154         {
00155                 // Left click: display/hide the first main window
00156                 mAssocMainWindow = KAlarmMainWindow::toggleWindow(mAssocMainWindow);
00157         }
00158         else
00159                 KSystemTray::mousePressEvent(e);
00160 }
00161 
00162 /******************************************************************************
00163 *  Called when the mouse is released over the panel icon.
00164 *  The main window (if not hidden) is raised and made the active window.
00165 *  If this is done in mousePressEvent(), it doesn't work.
00166 */
00167 void TrayWindow::mouseReleaseEvent(QMouseEvent* e)
00168 {
00169 
00170         if (e->button() == LeftButton  &&  mAssocMainWindow  &&  mAssocMainWindow->isVisible())
00171         {
00172                 mAssocMainWindow->raise();
00173                 mAssocMainWindow->setActiveWindow();
00174         }
00175         else
00176                 KSystemTray::mouseReleaseEvent(e);
00177 }
00178 
00179 /******************************************************************************
00180 * Called when the associated main window is closed.
00181 */
00182 void TrayWindow::removeWindow(KAlarmMainWindow* win)
00183 {
00184         if (win == mAssocMainWindow)
00185                 mAssocMainWindow = 0;
00186 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:41:03 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001