Update NEWS for 0.3.1
[nomnom.git] / src / systray / nsystray.cpp
blob8180a1b629f9d422d47f0b1bc769a0c17bf51e29
1 /* NomNom
2 * Copyright (C) 2011 Toni Gundogdu <legatvs@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "config.h"
20 #include <QMenu>
22 #ifdef ENABLE_VERBOSE
23 #include <QDebug>
24 #endif
26 #include <NSysTray>
28 namespace nn
32 * Constructor: NSystray
34 * Initializes the object.
36 NSysTray::NSysTray(QObject *parent/*=NULL*/, const QString& text/*=""*/)
37 : QSystemTrayIcon(parent), trayMenu(NULL)
39 trayMenu = new QMenu;
40 if (!text.isEmpty())
41 *this << text;
45 * Operator: <<
47 * Shorthand for setting the tooltip message.
49 * Parameters:
50 * text - Text to be used a a new tooltip message
52 * Returns:
53 * Instance reference
55 NSysTray& NSysTray::operator<<(const QString& text)
57 setToolTip(text);
58 return *this;
62 * Function: addTrayMenuAction
64 * Adds a new action to the tray menu.
66 * Parameters:
67 * signal - Signal to connect to
68 * receiver - Receiver
69 * method - Method that handles the signal
70 * text - Action text
71 * checkable - Whether the action should be checkable
73 void NSysTray::addTrayMenuAction(const char *signal,
74 QObject *receiver,
75 const char *method,
76 const QString& text,
77 const bool checkable/*=false*/)
79 QAction *action = new QAction(text, this);
80 action->setCheckable(checkable);
81 connect(action, signal, receiver, method);
82 trayMenu->addAction(action);
86 * Function: addTrayMenuSeparator
88 * Adds a separator to the tray menu.
90 void NSysTray::addTrayMenuSeparator()
92 QAction *a = new QAction(this);
93 a->setSeparator(true);
94 trayMenu->addAction(a);
98 * Function: findTrayMenuAction
100 * Returns a matching tray menu action.
102 * Parameters:
103 * text - Action text to match
105 * Returns:
106 * Pointer to an action or NULL if nothing was matched
108 QAction *NSysTray::findTrayMenuAction(const QString& text)
110 foreach (QAction *action, trayMenu->actions())
112 if (action->text() == text)
113 return action;
115 #ifdef ENABLE_VERBOSE
116 qWarning() << __PRETTY_FUNCTION__ << __LINE__
117 << "Action \""
118 << text
119 << "\" not found in tray menu";
120 #endif
121 return NULL;
125 * Function: setTrayMenu()
127 * Makes the created tray menu the context menu for the tray icon.
129 void NSysTray::setTrayMenu()
131 setContextMenu(trayMenu);
134 } // namespace nn
136 /* vim: set ts=2 sw=2 tw=72 expandtab: */