~ubuntu-branches/ubuntu/maverick/kdegraphics/maverick-proposed

« back to all changes in this revision

Viewing changes to gwenview/lib/messagebubble.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-12-02 14:03:43 UTC
  • mfrom: (1.1.35 upstream)
  • Revision ID: james.westby@ubuntu.com-20091202140343-2732gbkj69g89arq
Tags: 4:4.3.80-0ubuntu1
* New upstream beta release:
  - Add build-depend on shared-desktop-ontologies for nepomuk integration
  - Bump .so versions for libkexiv, libkdcraw and libkipi
  - Update various .install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
2
/*
 
3
Gwenview: an image viewer
 
4
Copyright 2009 Aurélien Gâteau <agateau@kde.org>
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (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, Cambridge, MA 02110-1301, USA.
 
19
 
 
20
*/
 
21
// Self
 
22
#include "messagebubble.moc"
 
23
 
 
24
// Qt
 
25
#include <QHBoxLayout>
 
26
#include <QLabel>
 
27
#include <QPainter>
 
28
#include <QTimeLine>
 
29
#include <QToolButton>
 
30
 
 
31
// KDE
 
32
#include <kdebug.h>
 
33
#include <kguiitem.h>
 
34
 
 
35
// Local
 
36
 
 
37
namespace Gwenview {
 
38
 
 
39
static const int TIMEOUT = 10000;
 
40
 
 
41
class PieWidget : public QWidget {
 
42
public:
 
43
        PieWidget(QWidget* parent=0)
 
44
        : QWidget(parent)
 
45
        , mValue(0)
 
46
        {
 
47
                setMinimumSize(16, 16);
 
48
        }
 
49
 
 
50
        void setValue(qreal value) {
 
51
                mValue = value;
 
52
                update();
 
53
        }
 
54
 
 
55
        int heightForWidth(int width) const {
 
56
                return width;
 
57
        }
 
58
 
 
59
protected:
 
60
        void paintEvent(QPaintEvent*) {
 
61
                QPainter painter(this);
 
62
                painter.setRenderHint(QPainter::Antialiasing);
 
63
                const int circle = 5760;
 
64
                const int start = circle / 4; // Start at 12h, not 3h
 
65
                const int end = int(circle * mValue);
 
66
                painter.setBrush(palette().dark());
 
67
                painter.setPen(palette().light().color());
 
68
 
 
69
                QRectF square = QRectF(rect()).adjusted(.5, .5, -.5, -.5);
 
70
                qreal width = square.width();
 
71
                qreal height= square.height();
 
72
                if (width < height) {
 
73
                        square.setHeight(width);
 
74
                        square.moveTop((height - width) / 2);
 
75
                } else {
 
76
                        square.setWidth(height);
 
77
                        square.moveLeft((width - height) / 2);
 
78
                }
 
79
                painter.drawPie(square, start, end);
 
80
        }
 
81
 
 
82
private:
 
83
        qreal mValue;
 
84
};
 
85
 
 
86
 
 
87
struct MessageBubblePrivate {
 
88
        PieWidget* mCountDownWidget;
 
89
        QWidget* mWidget;
 
90
        QLabel* mLabel;
 
91
};
 
92
 
 
93
 
 
94
MessageBubble::MessageBubble(QWidget* parent)
 
95
: HudWidget(parent)
 
96
, d(new MessageBubblePrivate) {
 
97
        d->mWidget = new QWidget;
 
98
        d->mCountDownWidget = new PieWidget;
 
99
        d->mCountDownWidget->setValue(1);
 
100
        d->mLabel = new QLabel;
 
101
 
 
102
        QTimeLine* timeLine = new QTimeLine(TIMEOUT, this);
 
103
        connect(timeLine, SIGNAL(valueChanged(qreal)),
 
104
                SLOT(slotTimeLineChanged(qreal)));
 
105
        connect(timeLine, SIGNAL(finished()),
 
106
                SLOT(deleteLater()));
 
107
        timeLine->start();
 
108
 
 
109
        QHBoxLayout* layout = new QHBoxLayout(d->mWidget);
 
110
        layout->setMargin(0);
 
111
        layout->addWidget(d->mCountDownWidget);
 
112
        layout->addWidget(d->mLabel);
 
113
 
 
114
        init(d->mWidget, HudWidget::OptionCloseButton);
 
115
}
 
116
 
 
117
 
 
118
MessageBubble::~MessageBubble() {
 
119
        delete d;
 
120
}
 
121
 
 
122
 
 
123
void MessageBubble::setText(const QString& text) {
 
124
        d->mLabel->setText(text);
 
125
        adjustSize();
 
126
}
 
127
 
 
128
 
 
129
QToolButton* MessageBubble::addButton(const KGuiItem& guiItem) {
 
130
        QToolButton* button = new QToolButton;
 
131
        button->setText(guiItem.text());
 
132
        button->setIcon(guiItem.icon());
 
133
        button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
 
134
        d->mWidget->layout()->addWidget(button);
 
135
        adjustSize();
 
136
        return button;
 
137
}
 
138
 
 
139
 
 
140
void MessageBubble::slotTimeLineChanged(qreal value) {
 
141
        d->mCountDownWidget->setValue(1 - value);
 
142
}
 
143
 
 
144
 
 
145
} // namespace