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

kleopatra

  • sources
  • kde-4.14
  • 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 <KDebug>
40 
41 #include <QTimer>
42 #include <QPointer>
43 #include <QWidget>
44 #include <QEvent>
45 
46 #include <cassert>
47 
48 using namespace Kleo;
49 
50 static const int ATTENTION_ANIMATION_FRAMES_PER_SEC = 1;
51 
52 class SystemTrayIcon::Private {
53  friend class ::SystemTrayIcon;
54  SystemTrayIcon * const q;
55 public:
56  explicit Private( SystemTrayIcon * qq );
57  ~Private();
58 
59 private:
60  bool attentionWanted() const {
61  return attentionAnimationTimer.isActive();
62  }
63 
64  void setAttentionWantedImpl( bool on ) {
65  if ( on ) {
66  attentionAnimationTimer.start();
67  } else {
68  attentionAnimationTimer.stop();
69  attentionIconShown = false;
70  q->setIcon( normalIcon );
71  }
72  }
73 
74  void slotActivated( ActivationReason reason ) {
75  if ( reason == QSystemTrayIcon::Trigger )
76  q->doActivated();
77  }
78 
79  void slotAttentionAnimationTimerTimout() {
80  if ( attentionIconShown ) {
81  attentionIconShown = false;
82  q->setIcon( normalIcon );
83  } else {
84  attentionIconShown = true;
85  q->setIcon( attentionIcon );
86  }
87  }
88 
89 private:
90  bool attentionIconShown;
91 
92  QIcon normalIcon, attentionIcon;
93 
94  QTimer attentionAnimationTimer;
95 
96  QPointer<QWidget> mainWindow;
97  QPointer<QWidget> attentionWindow;
98 };
99 
100 SystemTrayIcon::Private::Private( SystemTrayIcon * qq )
101  : q( qq ),
102  attentionIconShown( false ),
103  attentionAnimationTimer(),
104  mainWindow(),
105  attentionWindow()
106 {
107  KDAB_SET_OBJECT_NAME( attentionAnimationTimer );
108 
109  attentionAnimationTimer.setSingleShot( false );
110  attentionAnimationTimer.setInterval( 1000 * ATTENTION_ANIMATION_FRAMES_PER_SEC / 2 );
111 
112  connect( q, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), q, SLOT(slotActivated(QSystemTrayIcon::ActivationReason)) );
113  connect( &attentionAnimationTimer, SIGNAL(timeout()), q, SLOT(slotAttentionAnimationTimerTimout()) );
114 }
115 
116 SystemTrayIcon::Private::~Private() {}
117 
118 SystemTrayIcon::SystemTrayIcon( QObject * p )
119  : QSystemTrayIcon( p ), d( new Private( this ) )
120 {
121 
122 }
123 
124 SystemTrayIcon::SystemTrayIcon( const QIcon & icon, QObject * p )
125  : QSystemTrayIcon( icon, p ), d( new Private( this ) )
126 {
127  d->normalIcon = d->attentionIcon = icon;
128 }
129 
130 SystemTrayIcon::~SystemTrayIcon() {}
131 
132 void SystemTrayIcon::setMainWindow( QWidget * mw ) {
133  if ( d->mainWindow )
134  return;
135  d->mainWindow = mw;
136  if ( mw )
137  mw->installEventFilter( this );
138  doMainWindowSet( mw );
139  slotEnableDisableActions();
140 }
141 
142 QWidget * SystemTrayIcon::mainWindow() const {
143  return d->mainWindow;
144 }
145 
146 void SystemTrayIcon::setAttentionWindow( QWidget * mw ) {
147  if ( d->attentionWindow )
148  return;
149  d->attentionWindow = mw;
150  if ( mw )
151  mw->installEventFilter( this );
152  slotEnableDisableActions();
153 }
154 
155 QWidget * SystemTrayIcon::attentionWindow() const {
156  return d->attentionWindow;
157 }
158 
159 bool SystemTrayIcon::eventFilter( QObject * o, QEvent * e ) {
160  if ( o == d->mainWindow )
161  switch ( e->type() ) {
162  case QEvent::Close:
163  doMainWindowClosed( static_cast<QWidget*>( o ) );
164  // fall through:
165  case QEvent::Show:
166  case QEvent::DeferredDelete:
167  QMetaObject::invokeMethod( this, "slotEnableDisableActions", Qt::QueuedConnection );
168  default: ;
169  }
170  else if ( o == d->attentionWindow )
171  switch ( e->type() ) {
172  case QEvent::Close:
173  doAttentionWindowClosed( static_cast<QWidget*>( o ) );
174  // fall through:
175  case QEvent::Show:
176  case QEvent::DeferredDelete:
177  QMetaObject::invokeMethod( this, "slotEnableDisableActions", Qt::QueuedConnection );
178  default: ;
179  }
180  return false;
181 }
182 
183 void SystemTrayIcon::setAttentionWanted( bool on ) {
184  if ( d->attentionWanted() == on )
185  return;
186  kDebug() << d->attentionWanted() << "->" << on;
187  d->setAttentionWantedImpl( on );
188 }
189 
190 bool SystemTrayIcon::attentionWanted() const {
191  return d->attentionWanted();
192 }
193 
194 void SystemTrayIcon::setNormalIcon( const QIcon & icon ) {
195  if ( d->normalIcon.cacheKey() == icon.cacheKey() )
196  return;
197  d->normalIcon = icon;
198  if ( !d->attentionWanted() || !d->attentionIconShown )
199  setIcon( icon );
200 }
201 
202 QIcon SystemTrayIcon::normalIcon() const {
203  return d->normalIcon;
204 }
205 
206 void SystemTrayIcon::setAttentionIcon( const QIcon & icon ) {
207  if ( d->attentionIcon.cacheKey() == icon.cacheKey() )
208  return;
209  d->attentionIcon = icon;
210  if ( d->attentionWanted() && d->attentionIconShown )
211  setIcon( icon );
212 }
213 
214 QIcon SystemTrayIcon::attentionIcon() const {
215  return d->attentionIcon;
216 }
217 
218 void SystemTrayIcon::doMainWindowSet( QWidget * ) {}
219 void SystemTrayIcon::doMainWindowClosed( QWidget * ) {}
220 void SystemTrayIcon::doAttentionWindowClosed( QWidget * ) {}
221 
222 #include "moc_systemtrayicon.cpp"
223 
224 #endif // QT_NO_SYSTEMTRAYICON
Kleo::SystemTrayIcon::SystemTrayIcon
SystemTrayIcon(QObject *parent=0)
Definition: systemtrayicon.cpp:118
QIcon::cacheKey
qint64 cacheKey() const
QEvent
Kleo::SystemTrayIcon::setAttentionWanted
void setAttentionWanted(bool)
Definition: systemtrayicon.cpp:183
QWidget
Kleo::SystemTrayIcon::slotEnableDisableActions
virtual void slotEnableDisableActions()=0
QEvent::type
Type type() const
Kleo::SystemTrayIcon::mainWindow
QWidget * mainWindow() const
Definition: systemtrayicon.cpp:142
Kleo::SystemTrayIcon::setMainWindow
void setMainWindow(QWidget *w)
Definition: systemtrayicon.cpp:132
Kleo::SystemTrayIcon::attentionWindow
QWidget * attentionWindow() const
Definition: systemtrayicon.cpp:155
QPointer< QWidget >
Kleo::SystemTrayIcon::attentionIcon
QIcon attentionIcon() const
Definition: systemtrayicon.cpp:214
systemtrayicon.h
Kleo::SystemTrayIcon::~SystemTrayIcon
~SystemTrayIcon()
Definition: systemtrayicon.cpp:130
d
#define d
Definition: adduseridcommand.cpp:89
QObject::installEventFilter
void installEventFilter(QObject *filterObj)
QTimer
Kleo::SystemTrayIcon::normalIcon
QIcon normalIcon() const
Definition: systemtrayicon.cpp:202
QObject
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:194
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
Kleo::SystemTrayIcon::attentionWanted
bool attentionWanted() const
Definition: systemtrayicon.cpp:190
Kleo::SystemTrayIcon::setAttentionIcon
void setAttentionIcon(const QIcon &icon)
Definition: systemtrayicon.cpp:206
q
#define q
Definition: adduseridcommand.cpp:90
Kleo::SystemTrayIcon
Definition: systemtrayicon.h:44
ATTENTION_ANIMATION_FRAMES_PER_SEC
static const int ATTENTION_ANIMATION_FRAMES_PER_SEC
Definition: systemtrayicon.cpp:50
QSystemTrayIcon
QSystemTrayIcon::icon
QIcon icon() const
QIcon
Kleo::SystemTrayIcon::setAttentionWindow
void setAttentionWindow(QWidget *w)
Definition: systemtrayicon.cpp:146
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:11 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
  • pimprint

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