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

akregator

  • sources
  • kde-4.14
  • kdepim
  • akregator
  • src
trayicon.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2004 Stanislav Karchebny <Stanislav.Karchebny@kdemail.net>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of Qt, and distribute the resulting executable,
22  without including the source code for Qt in the source distribution.
23 */
24 
25 #include "trayicon.h"
26 #include "akregatorconfig.h"
27 
28 #include <kapplication.h>
29 #include <kwindowsystem.h>
30 #include <kdebug.h>
31 #include <klocale.h>
32 #include <kglobalsettings.h>
33 #include <kmenu.h>
34 #include <kicon.h>
35 #include <kiconloader.h>
36 #include <KColorScheme>
37 
38 #include <QPainter>
39 
40 
41 namespace Akregator {
42 
43 TrayIcon* TrayIcon::m_instance = 0;
44 
45 TrayIcon* TrayIcon::getInstance()
46 {
47  return m_instance;
48 }
49 
50 void TrayIcon::setInstance(TrayIcon* trayIcon)
51 {
52  m_instance = trayIcon;
53 }
54 
55 
56 TrayIcon::TrayIcon(QObject* parent)
57  : KStatusNotifierItem(parent), m_unread(0)
58 {
59  setToolTipTitle( i18n("Akregator") );
60  setToolTipIconByName( i18n("Akregator") );
61  setIconByName( QLatin1String("akregator") );
62  m_defaultIcon = KIcon( QLatin1String("akregator") );
63 }
64 
65 
66 TrayIcon::~TrayIcon()
67 {}
68 
69 void TrayIcon::slotSetUnread(int unread)
70 {
71  m_unread = unread;
72 
73  this->setToolTip( m_defaultIcon.name(), i18n("Akregator"), unread == 0 ? i18n("There are no unread articles") : i18np( "1 unread article", "%1 unread articles", unread ) );
74  setStatus( unread > 0 ? KStatusNotifierItem::Active : KStatusNotifierItem::Passive );
75 
76  if (unread <= 0 || !Settings::enableTrayIconUnreadArticleCount())
77  {
78  setIconByName( m_defaultIcon.name() );
79  }
80  else
81  {
82  // adapted from KMSystemTray::updateCount()
83  int oldWidth = KIconLoader::SizeSmallMedium;
84 
85  QString countStr = QString::number( unread );
86  QFont f = KGlobalSettings::generalFont();
87  f.setBold(true);
88 
89  float pointSize = f.pointSizeF();
90  QFontMetrics fm(f);
91  int w = fm.width(countStr);
92  if( w > (oldWidth - 2) )
93  {
94  pointSize *= float(oldWidth - 2) / float(w);
95  f.setPointSizeF(pointSize);
96  }
97 
98  // overlay
99  QPixmap overlayImg( oldWidth, oldWidth );
100  overlayImg.fill( Qt::transparent );
101 
102  QPainter p(&overlayImg);
103  p.setFont(f);
104  KColorScheme scheme(QPalette::Active, KColorScheme::View);
105 
106  fm = QFontMetrics(f);
107  QRect boundingRect = fm.tightBoundingRect(countStr);
108  boundingRect.adjust(0, 0, 0, 2);
109  boundingRect.setHeight(qMin(boundingRect.height(), oldWidth));
110  boundingRect.moveTo((oldWidth - boundingRect.width()) / 2,
111  ((oldWidth - boundingRect.height()) / 2) - 1);
112  p.setOpacity(0.7);
113  p.setBrush(scheme.background(KColorScheme::LinkBackground));
114  p.setPen(scheme.background(KColorScheme::LinkBackground).color());
115  p.drawRoundedRect(boundingRect, 2.0, 2.0);
116 
117  p.setBrush(Qt::NoBrush);
118  p.setPen(scheme.foreground(KColorScheme::LinkText).color());
119  p.setOpacity(1.0);
120  p.drawText(overlayImg.rect(), Qt::AlignCenter, countStr);
121  p.end();
122 
123  QPixmap iconPixmap = m_defaultIcon.pixmap( oldWidth, oldWidth );
124 
125  QPainter pp( &iconPixmap );
126  pp.drawPixmap( 0, 0, overlayImg );
127  pp.end();
128 
129  setIconByPixmap( iconPixmap );
130  }
131 }
132 
133 void TrayIcon::viewButtonClicked()
134 {
135  QWidget* p = static_cast<QWidget*>(parent());
136  KWindowSystem::activateWindow(p->winId());
137 }
138 
139 void TrayIcon::settingsChanged()
140 {
141  slotSetUnread(m_unread);
142 }
143 
144 } // namespace Akregator
145 
QPainter::setOpacity
void setOpacity(qreal opacity)
QWidget
Akregator::TrayIcon
Definition: trayicon.h:37
QPainter::end
bool end()
QPixmap::fill
void fill(const QColor &color)
QFont::pointSizeF
qreal pointSizeF() const
QFontMetrics::tightBoundingRect
QRect tightBoundingRect(const QString &text) const
QFont
QRect::height
int height() const
QFontMetrics
Akregator::TrayIcon::slotSetUnread
void slotSetUnread(int unread)
Definition: trayicon.cpp:69
QRect::moveTo
void moveTo(int x, int y)
QFont::setBold
void setBold(bool enable)
QRect
QPainter::setFont
void setFont(const QFont &font)
QString::number
QString number(int n, int base)
QObject
QPainter::setPen
void setPen(const QColor &color)
Akregator::TrayIcon::setInstance
static void setInstance(TrayIcon *trayIcon)
Definition: trayicon.cpp:50
QPainter::drawRoundedRect
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter
Akregator::TrayIcon::~TrayIcon
~TrayIcon()
Definition: trayicon.cpp:66
QPainter::setBrush
void setBrush(const QBrush &brush)
QPainter::drawText
void drawText(const QPointF &position, const QString &text)
QWidget::winId
WId winId() const
QString
trayicon.h
QPixmap
Akregator::TrayIcon::settingsChanged
void settingsChanged()
Definition: trayicon.cpp:139
QFontMetrics::width
int width(const QString &text, int len) const
QFont::setPointSizeF
void setPointSizeF(qreal pointSize)
Akregator::TrayIcon::getInstance
static TrayIcon * getInstance()
Definition: trayicon.cpp:45
Akregator::TrayIcon::viewButtonClicked
void viewButtonClicked()
Definition: trayicon.cpp:133
QRect::width
int width() const
QLatin1String
QRect::setHeight
void setHeight(int height)
QRect::adjust
void adjust(int dx1, int dy1, int dx2, int dy2)
Akregator::TrayIcon::TrayIcon
TrayIcon(QObject *parent=0)
Definition: trayicon.cpp:56
KStatusNotifierItem
QPixmap::rect
QRect rect() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akregator

Skip menu "akregator"
  • 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