~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/applets/calendar/calendar.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright 2008 by Davide Bettio <davide.bettio@kdemail.net>           *
 
3
 *   Copyright 2009 by John Layt <john@layt.net>                           *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 
19
 ***************************************************************************/
 
20
 
 
21
#include "calendar.h"
 
22
 
 
23
#include <QGraphicsLayout>
 
24
#include <QPainter>
 
25
#include <QTimer>
 
26
 
 
27
#include <KDebug>
 
28
#include <KSystemTimeZones>
 
29
#include <KConfigDialog>
 
30
#include <KConfigGroup>
 
31
 
 
32
#include <Plasma/Svg>
 
33
#include <Plasma/Theme>
 
34
 
 
35
CalendarApplet::CalendarApplet(QObject *parent, const QVariantList &args)
 
36
    : Plasma::PopupApplet(parent, args),
 
37
    m_calendarWidget(0),
 
38
    m_theme(0)
 
39
{
 
40
    setAspectRatioMode(Plasma::IgnoreAspectRatio);
 
41
    setCacheMode(DeviceCoordinateCache);
 
42
}
 
43
 
 
44
CalendarApplet::~CalendarApplet()
 
45
{
 
46
}
 
47
 
 
48
void CalendarApplet::init()
 
49
{
 
50
    setPopupIcon("view-pim-calendar");
 
51
    m_calendarWidget = new Plasma::Calendar(this);
 
52
    if (m_calendarWidget->isDisplayingDateDetails()) {
 
53
        m_calendarWidget->setPreferredSize(440, 250);
 
54
    } else {
 
55
        m_calendarWidget->setPreferredSize(220, 250);
 
56
    }
 
57
    updateDate();
 
58
    configChanged();
 
59
    setFocusPolicy(Qt::StrongFocus);
 
60
}
 
61
 
 
62
void CalendarApplet::focusInEvent(QFocusEvent* event)
 
63
{
 
64
    Q_UNUSED(event);
 
65
    m_calendarWidget->setFlag(QGraphicsItem::ItemIsFocusable);
 
66
    m_calendarWidget->setFocus();
 
67
}
 
68
 
 
69
void CalendarApplet::configChanged()
 
70
{
 
71
    m_calendarWidget->applyConfiguration(config());
 
72
}
 
73
 
 
74
QGraphicsWidget *CalendarApplet::graphicsWidget()
 
75
{
 
76
    return m_calendarWidget;
 
77
}
 
78
 
 
79
void CalendarApplet::constraintsEvent(Plasma::Constraints constraints)
 
80
{
 
81
    if ((constraints|Plasma::FormFactorConstraint || constraints|Plasma::SizeConstraint) &&
 
82
        layout()->itemAt(0) != m_calendarWidget) {
 
83
        paintIcon();
 
84
    }
 
85
}
 
86
 
 
87
void CalendarApplet::paintIcon()
 
88
{
 
89
    const int iconSize = qMin(size().width(), size().height());
 
90
 
 
91
    if (iconSize <= 0) {
 
92
        return;
 
93
    }
 
94
 
 
95
    QPixmap icon(iconSize, iconSize);
 
96
 
 
97
    if (!m_theme) {
 
98
        m_theme = new Plasma::Svg(this);
 
99
        m_theme->setImagePath("calendar/mini-calendar");
 
100
        m_theme->setContainsMultipleImages(true);
 
101
    }
 
102
 
 
103
    icon.fill(Qt::transparent);
 
104
    QPainter p(&icon);
 
105
 
 
106
    m_theme->paint(&p, icon.rect(), "mini-calendar");
 
107
 
 
108
    QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont);
 
109
    p.setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor));
 
110
    font.setPixelSize(icon.size().height() / 2);
 
111
    p.setFont(font);
 
112
    p.drawText(icon.rect().adjusted(0, icon.size().height()/4, 0, 0), Qt::AlignCenter,
 
113
               QString::number(m_calendarWidget->calendar()->day(m_calendarWidget->date())));
 
114
    m_theme->resize();
 
115
    p.end();
 
116
    setPopupIcon(icon);
 
117
}
 
118
 
 
119
void CalendarApplet::configAccepted()
 
120
{
 
121
    m_calendarWidget->configAccepted(config());
 
122
    update();
 
123
}
 
124
 
 
125
void CalendarApplet::updateDate()
 
126
{
 
127
    QDateTime now = QDateTime::currentDateTime();
 
128
    int updateIn = (24 * 60 * 60) - (now.toTime_t() + KSystemTimeZones::local().currentOffset()) % (24 * 60 * 60);
 
129
    QTimer::singleShot(updateIn * 1000, this, SLOT(updateDate()));
 
130
    paintIcon();
 
131
}
 
132
 
 
133
void CalendarApplet::createConfigurationInterface(KConfigDialog *parent)
 
134
{
 
135
    m_calendarWidget->createConfigurationInterface(parent);
 
136
    parent->setButtons( KDialog::Ok | KDialog::Cancel | KDialog::Apply );
 
137
    connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
 
138
    connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
 
139
}
 
140
 
 
141
#include "calendar.moc"