• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • utils
systemtrayicon.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  utils/systemtrayicon.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007,2009 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "systemtrayicon.h"
36 
37 #ifndef QT_NO_SYSTEMTRAYICON
38 
39 #include <KIcon>
40 #include <KLocale>
41 #include <KWindowSystem>
42 #include <KDebug>
43 
44 #include <QTimer>
45 #include <QApplication>
46 #include <QPointer>
47 #include <QWidget>
48 
49 #include <cassert>
50 
51 using namespace Kleo;
52 
53 static const int ATTENTION_ANIMATION_FRAMES_PER_SEC = 1;
54 
55 class SystemTrayIcon::Private {
56  friend class ::SystemTrayIcon;
57  SystemTrayIcon * const q;
58 public:
59  explicit Private( SystemTrayIcon * qq );
60  ~Private();
61 
62 private:
63  bool attentionWanted() const {
64  return attentionAnimationTimer.isActive();
65  }
66 
67  void setAttentionWantedImpl( bool on ) {
68  if ( on ) {
69  attentionAnimationTimer.start();
70  } else {
71  attentionAnimationTimer.stop();
72  attentionIconShown = false;
73  q->setIcon( normalIcon );
74  }
75  }
76 
77  void slotActivated( ActivationReason reason ) {
78  if ( reason == QSystemTrayIcon::Trigger )
79  q->doActivated();
80  }
81 
82  void slotAttentionAnimationTimerTimout() {
83  if ( attentionIconShown ) {
84  attentionIconShown = false;
85  q->setIcon( normalIcon );
86  } else {
87  attentionIconShown = true;
88  q->setIcon( attentionIcon );
89  }
90  }
91 
92 private:
93  bool attentionIconShown;
94 
95  QIcon normalIcon, attentionIcon;
96 
97  QTimer attentionAnimationTimer;
98 
99  QPointer<QWidget> mainWindow;
100  QPointer<QWidget> attentionWindow;
101 };
102 
103 SystemTrayIcon::Private::Private( SystemTrayIcon * qq )
104  : q( qq ),
105  attentionIconShown( false ),
106  attentionAnimationTimer(),
107  mainWindow(),
108  attentionWindow()
109 {
110  KDAB_SET_OBJECT_NAME( attentionAnimationTimer );
111 
112  attentionAnimationTimer.setSingleShot( false );
113  attentionAnimationTimer.setInterval( 1000 * ATTENTION_ANIMATION_FRAMES_PER_SEC / 2 );
114 
115  connect( q, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), q, SLOT(slotActivated(QSystemTrayIcon::ActivationReason)) );
116  connect( &attentionAnimationTimer, SIGNAL(timeout()), q, SLOT(slotAttentionAnimationTimerTimout()) );
117 }
118 
119 SystemTrayIcon::Private::~Private() {}
120 
121 SystemTrayIcon::SystemTrayIcon( QObject * p )
122  : QSystemTrayIcon( p ), d( new Private( this ) )
123 {
124 
125 }
126 
127 SystemTrayIcon::SystemTrayIcon( const QIcon & icon, QObject * p )
128  : QSystemTrayIcon( icon, p ), d( new Private( this ) )
129 {
130  d->normalIcon = d->attentionIcon = icon;
131 }
132 
133 SystemTrayIcon::~SystemTrayIcon() {}
134 
135 void SystemTrayIcon::setMainWindow( QWidget * mw ) {
136  if ( d->mainWindow )
137  return;
138  d->mainWindow = mw;
139  if ( mw )
140  mw->installEventFilter( this );
141  doMainWindowSet( mw );
142  slotEnableDisableActions();
143 }
144 
145 QWidget * SystemTrayIcon::mainWindow() const {
146  return d->mainWindow;
147 }
148 
149 void SystemTrayIcon::setAttentionWindow( QWidget * mw ) {
150  if ( d->attentionWindow )
151  return;
152  d->attentionWindow = mw;
153  if ( mw )
154  mw->installEventFilter( this );
155  slotEnableDisableActions();
156 }
157 
158 QWidget * SystemTrayIcon::attentionWindow() const {
159  return d->attentionWindow;
160 }
161 
162 bool SystemTrayIcon::eventFilter( QObject * o, QEvent * e ) {
163  if ( o == d->mainWindow )
164  switch ( e->type() ) {
165  case QEvent::Close:
166  doMainWindowClosed( static_cast<QWidget*>( o ) );
167  // fall through:
168  case QEvent::Show:
169  case QEvent::DeferredDelete:
170  QMetaObject::invokeMethod( this, "slotEnableDisableActions", Qt::QueuedConnection );
171  default: ;
172  }
173  else if ( o == d->attentionWindow )
174  switch ( e->type() ) {
175  case QEvent::Close:
176  doAttentionWindowClosed( static_cast<QWidget*>( o ) );
177  // fall through:
178  case QEvent::Show:
179  case QEvent::DeferredDelete:
180  QMetaObject::invokeMethod( this, "slotEnableDisableActions", Qt::QueuedConnection );
181  default: ;
182  }
183  return false;
184 }
185 
186 void SystemTrayIcon::setAttentionWanted( bool on ) {
187  if ( d->attentionWanted() == on )
188  return;
189  kDebug() << d->attentionWanted() << "->" << on;
190  d->setAttentionWantedImpl( on );
191 }
192 
193 bool SystemTrayIcon::attentionWanted() const {
194  return d->attentionWanted();
195 }
196 
197 void SystemTrayIcon::setNormalIcon( const QIcon & icon ) {
198  if ( d->normalIcon.cacheKey() == icon.cacheKey() )
199  return;
200  d->normalIcon = icon;
201  if ( !d->attentionWanted() || !d->attentionIconShown )
202  setIcon( icon );
203 }
204 
205 QIcon SystemTrayIcon::normalIcon() const {
206  return d->normalIcon;
207 }
208 
209 void SystemTrayIcon::setAttentionIcon( const QIcon & icon ) {
210  if ( d->attentionIcon.cacheKey() == icon.cacheKey() )
211  return;
212  d->attentionIcon = icon;
213  if ( d->attentionWanted() && d->attentionIconShown )
214  setIcon( icon );
215 }
216 
217 QIcon SystemTrayIcon::attentionIcon() const {
218  return d->attentionIcon;
219 }
220 
221 void SystemTrayIcon::doMainWindowSet( QWidget * ) {}
222 void SystemTrayIcon::doMainWindowClosed( QWidget * ) {}
223 void SystemTrayIcon::doAttentionWindowClosed( QWidget * ) {}
224 
225 #include "moc_systemtrayicon.cpp"
226 
227 #endif // QT_NO_SYSTEMTRAYICON
Kleo::SystemTrayIcon::SystemTrayIcon
SystemTrayIcon(QObject *parent=0)
Definition: systemtrayicon.cpp:121
Kleo::SystemTrayIcon::setAttentionWanted
void setAttentionWanted(bool)
Definition: systemtrayicon.cpp:186
Kleo::SystemTrayIcon::slotEnableDisableActions
virtual void slotEnableDisableActions()=0
Kleo::SystemTrayIcon::mainWindow
QWidget * mainWindow() const
Definition: systemtrayicon.cpp:145
Kleo::SystemTrayIcon::setMainWindow
void setMainWindow(QWidget *w)
Definition: systemtrayicon.cpp:135
QWidget
Kleo::SystemTrayIcon::attentionWindow
QWidget * attentionWindow() const
Definition: systemtrayicon.cpp:158
Kleo::SystemTrayIcon::attentionIcon
QIcon attentionIcon() const
Definition: systemtrayicon.cpp:217
systemtrayicon.h
Kleo::SystemTrayIcon::~SystemTrayIcon
~SystemTrayIcon()
Definition: systemtrayicon.cpp:133
d
#define d
Definition: adduseridcommand.cpp:90
Kleo::SystemTrayIcon::normalIcon
QIcon normalIcon() const
Definition: systemtrayicon.cpp:205
KDAB_SET_OBJECT_NAME
#define KDAB_SET_OBJECT_NAME(x)
Definition: kdtoolsglobal.h:66
Kleo::SystemTrayIcon::setNormalIcon
void setNormalIcon(const QIcon &icon)
Definition: systemtrayicon.cpp:197
Kleo::SystemTrayIcon::attentionWanted
bool attentionWanted() const
Definition: systemtrayicon.cpp:193
Kleo::SystemTrayIcon::setAttentionIcon
void setAttentionIcon(const QIcon &icon)
Definition: systemtrayicon.cpp:209
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::SystemTrayIcon
Definition: systemtrayicon.h:44
ATTENTION_ANIMATION_FRAMES_PER_SEC
static const int ATTENTION_ANIMATION_FRAMES_PER_SEC
Definition: systemtrayicon.cpp:53
QSystemTrayIcon
Kleo::SystemTrayIcon::setAttentionWindow
void setAttentionWindow(QWidget *w)
Definition: systemtrayicon.cpp:149
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:42 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal