File: knotestray.cpp

package info (click to toggle)
kdepim 4%3A4.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 62,764 kB
  • sloc: cpp: 530,022; xml: 5,446; perl: 1,434; sh: 812; ansic: 433; php: 44; makefile: 22
file content (83 lines) | stat: -rw-r--r-- 2,628 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
  Copyright (c) 2013, 2014 Montel Laurent <montel@kde.org>

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License, version 2, as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "knotestray.h"

#include <KIconLoader>
#include <KGlobalSettings>
#include <KLocalizedString>
#include <KColorScheme>

#include <QPainter>
#include <QWidget>

KNotesTray::KNotesTray(QWidget *parent)
    : KStatusNotifierItem(parent)
{
    setToolTipTitle( i18n( "KNotes: Sticky notes for KDE" ) );
    setToolTipIconByName( QLatin1String("knotes") );
    setStatus( KStatusNotifierItem::Active );
    setCategory( KStatusNotifierItem::ApplicationStatus );
    setStandardActionsEnabled(false);
    mIcon = KIcon( QLatin1String("knotes") );
}

KNotesTray::~KNotesTray()
{
}

void KNotesTray::updateNumberOfNotes(int value)
{
    const int overlaySize = KIconLoader::SizeSmallMedium;

    const QString countString = QString::number( value );
    QFont countFont = KGlobalSettings::generalFont();
    countFont.setBold(true);

    // decrease the size of the font for the number of unread messages if the
    // number doesn't fit into the available space
    float countFontSize = countFont.pointSizeF();
    QFontMetrics qfm( countFont );
    const int width = qfm.width( countString );
    if ( width > (overlaySize - 2) ) {
        countFontSize *= float( overlaySize - 2 ) / float( width );
        countFont.setPointSizeF( countFontSize );
    }

    // Paint the number in a pixmap
    QPixmap overlayPixmap( overlaySize, overlaySize );
    overlayPixmap.fill( Qt::transparent );

    QPainter p( &overlayPixmap );
    p.setFont( countFont );
    KColorScheme scheme( QPalette::Active, KColorScheme::View );

    p.setBrush( Qt::NoBrush );
    p.setPen( scheme.foreground( KColorScheme::LinkText ).color() );
    p.setOpacity( 1.0 );
    p.drawText( overlayPixmap.rect(),Qt::AlignCenter, countString );
    p.end();

    QPixmap iconPixmap = mIcon.pixmap(overlaySize, overlaySize);

    QPainter pp(&iconPixmap);
    pp.drawPixmap(0, 0, overlayPixmap);
    pp.end();

    setIconByPixmap( iconPixmap );
}