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

« back to all changes in this revision

Viewing changes to plasma/desktop/shell/toolbox/paneltoolbox.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 2007 by Aaron Seigo <aseigo@kde.org>
 
3
 *   Copyright 2008 by Marco Martin <notmart@gmail.com>
 
4
 *
 
5
 *   This program is free software; you can redistribute it and/or modify
 
6
 *   it under the terms of the GNU Library General Public License as
 
7
 *   published by the Free Software Foundation; either version 2, 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 Library General Public
 
16
 *   License 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 "paneltoolbox_p.h"
 
22
 
 
23
#include <QGraphicsSceneHoverEvent>
 
24
#include <QPainter>
 
25
#include <QRadialGradient>
 
26
#include <QApplication>
 
27
#include <QPropertyAnimation>
 
28
#include <QWeakPointer>
 
29
 
 
30
#include <kcolorscheme.h>
 
31
#include <kdebug.h>
 
32
#include <kiconloader.h>
 
33
 
 
34
#include <plasma/applet.h>
 
35
#include <plasma/paintutils.h>
 
36
#include <plasma/svg.h>
 
37
#include <plasma/theme.h>
 
38
#include <plasma/tooltipcontent.h>
 
39
#include <plasma/tooltipmanager.h>
 
40
 
 
41
 
 
42
class PanelToolBoxPrivate
 
43
{
 
44
public:
 
45
    PanelToolBoxPrivate()
 
46
      : icon("plasma"),
 
47
        animFrame(0),
 
48
        highlighting(false)
 
49
    {
 
50
    }
 
51
 
 
52
    KIcon icon;
 
53
    QWeakPointer<QPropertyAnimation> anim;
 
54
    qreal animFrame;
 
55
    QColor fgColor;
 
56
    QColor bgColor;
 
57
    Plasma::Svg *background;
 
58
    bool highlighting;
 
59
};
 
60
 
 
61
PanelToolBox::PanelToolBox(Plasma::Containment *parent)
 
62
    : InternalToolBox(parent),
 
63
      d(new PanelToolBoxPrivate)
 
64
{
 
65
    init();
 
66
}
 
67
 
 
68
PanelToolBox::PanelToolBox(QObject *parent, const QVariantList &args)
 
69
   : InternalToolBox(parent, args),
 
70
     d(new PanelToolBoxPrivate)
 
71
{
 
72
    init();
 
73
}
 
74
 
 
75
PanelToolBox::~PanelToolBox()
 
76
{
 
77
    d->anim.clear();
 
78
    delete d;
 
79
}
 
80
 
 
81
void PanelToolBox::init()
 
82
{
 
83
    setIconSize(QSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall));
 
84
    setSize(KIconLoader::SizeSmallMedium);
 
85
    connect(this, SIGNAL(toggled()), this, SLOT(toggle()));
 
86
 
 
87
    setZValue(10000000);
 
88
    setFlag(ItemClipsChildrenToShape, false);
 
89
    //panel toolbox is allowed to zoom, otherwise a part of it will be displayed behind the desktop
 
90
    //toolbox when the desktop is zoomed out
 
91
    setFlag(ItemIgnoresTransformations, false);
 
92
    assignColors();
 
93
    connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
 
94
            this, SLOT(assignColors()));
 
95
 
 
96
    d->background = new Plasma::Svg(this);
 
97
    d->background->setImagePath("widgets/toolbox");
 
98
    d->background->setContainsMultipleImages(true);
 
99
 
 
100
    Plasma::ToolTipManager::self()->registerWidget(this);
 
101
 
 
102
    if (containment()) {
 
103
        QObject::connect(containment(), SIGNAL(immutabilityChanged(Plasma::ImmutabilityType)),
 
104
                         this, SLOT(immutabilityChanged(Plasma::ImmutabilityType)));
 
105
    }
 
106
}
 
107
 
 
108
void PanelToolBox::assignColors()
 
109
{
 
110
    d->bgColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor);
 
111
    d->fgColor = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
 
112
    update();
 
113
}
 
114
 
 
115
QRectF PanelToolBox::boundingRect() const
 
116
{
 
117
    QRectF r;
 
118
 
 
119
    //Only Left,Right and Bottom supported, default to Right
 
120
    if (corner() == InternalToolBox::Bottom) {
 
121
        r = QRectF(0, 0, size() * 2, size());
 
122
    } else if (corner() == InternalToolBox::Left) {
 
123
        r = QRectF(0, 0, size(), size() * 2);
 
124
    } else {
 
125
        r = QRectF(0, 0, size(), size() * 2);
 
126
    }
 
127
 
 
128
    if (parentItem()) {
 
129
        QSizeF s = parentItem()->boundingRect().size();
 
130
 
 
131
        if (r.height() > s.height()) {
 
132
            r.setHeight(s.height());
 
133
        }
 
134
 
 
135
        if (r.width() > s.width()) {
 
136
            r.setWidth(s.width());
 
137
        }
 
138
    }
 
139
 
 
140
    return r;
 
141
}
 
142
 
 
143
void PanelToolBox::immutabilityChanged(Plasma::ImmutabilityType immutability)
 
144
{
 
145
    setVisible(immutability == Plasma::Mutable);
 
146
}
 
147
 
 
148
void PanelToolBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
 
149
{
 
150
    Q_UNUSED(option)
 
151
    Q_UNUSED(widget)
 
152
 
 
153
    const qreal progress = d->animFrame / size();
 
154
 
 
155
    QRect backgroundRect;
 
156
    QPoint gradientCenter;
 
157
    QRectF rect = boundingRect();
 
158
    QString cornerElement;
 
159
 
 
160
    if (corner() == InternalToolBox::Bottom) {
 
161
        gradientCenter = QPoint(rect.center().x(), rect.bottom());
 
162
        cornerElement = "panel-south";
 
163
 
 
164
        backgroundRect = d->background->elementRect(cornerElement).toRect();
 
165
        backgroundRect.moveBottomLeft(shape().boundingRect().bottomLeft().toPoint());
 
166
    } else if (corner() == InternalToolBox::Right) {
 
167
        gradientCenter = QPoint(rect.right(), rect.center().y());
 
168
        cornerElement = "panel-east";
 
169
 
 
170
        backgroundRect = d->background->elementRect(cornerElement).toRect();
 
171
        backgroundRect.moveTopRight(shape().boundingRect().topRight().toPoint());
 
172
    } else {
 
173
        gradientCenter = QPoint(rect.right(), rect.center().y());
 
174
        cornerElement = "panel-west";
 
175
 
 
176
        backgroundRect = d->background->elementRect(cornerElement).toRect();
 
177
        backgroundRect.moveTopLeft(shape().boundingRect().topLeft().toPoint());
 
178
    }
 
179
 
 
180
 
 
181
    d->background->paint(painter, backgroundRect, cornerElement);
 
182
 
 
183
 
 
184
    QRect iconRect;
 
185
 
 
186
    //Only Left,Right and Bottom supported, default to Right
 
187
    if (corner() == InternalToolBox::Bottom) {
 
188
        iconRect = QRect(QPoint(gradientCenter.x() - iconSize().width() / 2,
 
189
                                (int)rect.bottom() - iconSize().height() - 2), iconSize());
 
190
    } else if (corner() == InternalToolBox::Left) {
 
191
        iconRect = QRect(QPoint(2, gradientCenter.y() - iconSize().height() / 2), iconSize());
 
192
    } else {
 
193
        iconRect = QRect(QPoint((int)rect.right() - iconSize().width() + 1,
 
194
                                gradientCenter.y() - iconSize().height() / 2), iconSize());
 
195
    }
 
196
 
 
197
    if (qFuzzyCompare(qreal(1.0), progress)) {
 
198
        d->icon.paint(painter, iconRect);
 
199
    } else if (qFuzzyCompare(qreal(1.0), 1 + progress)) {
 
200
        d->icon.paint(painter, iconRect, Qt::AlignCenter, QIcon::Disabled, QIcon::Off);
 
201
    } else {
 
202
        QPixmap disabled = d->icon.pixmap(iconSize(), QIcon::Disabled, QIcon::Off);
 
203
        QPixmap enabled = d->icon.pixmap(iconSize());
 
204
        QPixmap result = Plasma::PaintUtils::transition(
 
205
            d->icon.pixmap(iconSize(), QIcon::Disabled, QIcon::Off),
 
206
            d->icon.pixmap(iconSize()), progress);
 
207
        painter->drawPixmap(iconRect, result);
 
208
    }
 
209
}
 
210
 
 
211
QPainterPath PanelToolBox::shape() const
 
212
{
 
213
    QPainterPath path;
 
214
    int toolSize = size();// + (int)d->animFrame;
 
215
    QRectF rect = boundingRect();
 
216
 
 
217
    //Only Left,Right and Bottom supported, default to Right
 
218
    if (corner() == InternalToolBox::Bottom) {
 
219
        path.moveTo(rect.bottomLeft());
 
220
        path.arcTo(QRectF(rect.center().x() - toolSize,
 
221
                          rect.bottom() - toolSize,
 
222
                          toolSize * 2,
 
223
                          toolSize * 2), 0, 180);
 
224
    } else if (corner() == InternalToolBox::Left) {
 
225
        path.arcTo(QRectF(rect.left(),
 
226
                          rect.center().y() - toolSize,
 
227
                          toolSize * 2,
 
228
                          toolSize * 2), 90, -180);
 
229
    } else {
 
230
        path.moveTo(rect.topRight());
 
231
        path.arcTo(QRectF(rect.left(),
 
232
                          rect.center().y() - toolSize,
 
233
                          toolSize * 2,
 
234
                          toolSize * 2), 90, 180);
 
235
    }
 
236
 
 
237
    return path;
 
238
}
 
239
 
 
240
void PanelToolBox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
 
241
{
 
242
    highlight(true);
 
243
    QGraphicsItem::hoverEnterEvent(event);
 
244
}
 
245
 
 
246
void PanelToolBox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
 
247
{
 
248
    //kDebug() << event->pos() << event->scenePos()
 
249
    if (!isShowing()) {
 
250
        highlight(false);
 
251
    }
 
252
 
 
253
    QGraphicsItem::hoverLeaveEvent(event);
 
254
}
 
255
 
 
256
void PanelToolBox::showToolBox()
 
257
{
 
258
}
 
259
 
 
260
void PanelToolBox::hideToolBox()
 
261
{
 
262
}
 
263
 
 
264
void PanelToolBox::setShowing(bool show)
 
265
{
 
266
    InternalToolBox::setShowing(show);
 
267
    highlight(show);
 
268
}
 
269
 
 
270
void PanelToolBox::toolTipAboutToShow()
 
271
{
 
272
    if (isShowing()) {
 
273
        return;
 
274
    }
 
275
 
 
276
    Plasma::ToolTipContent c(i18n("Panel Tool Box"),
 
277
                     i18n("Click to access size, location and hiding controls as well as to add "
 
278
                          "new widgets to the panel."),
 
279
                     KIcon("plasma"));
 
280
    c.setAutohide(false);
 
281
    Plasma::ToolTipManager::self()->setContent(this, c);
 
282
}
 
283
 
 
284
void PanelToolBox::toolTipHidden()
 
285
{
 
286
    Plasma::ToolTipManager::self()->clearContent(this);
 
287
}
 
288
 
 
289
void PanelToolBox::highlight(bool highlighting)
 
290
{
 
291
    if (d->highlighting == highlighting) {
 
292
        return;
 
293
    }
 
294
 
 
295
    d->highlighting = highlighting;
 
296
 
 
297
    QPropertyAnimation *anim = d->anim.data();
 
298
    if (d->highlighting) {
 
299
        if (anim) {
 
300
            anim->stop();
 
301
            d->anim.clear();
 
302
        }
 
303
        anim = new QPropertyAnimation(this, "highlight", this);
 
304
        d->anim = anim;
 
305
    }
 
306
 
 
307
    if (anim->state() != QAbstractAnimation::Stopped) {
 
308
        anim->stop();
 
309
    }
 
310
 
 
311
    anim->setDuration(240);
 
312
    anim->setStartValue(0);
 
313
    anim->setEndValue(size());
 
314
 
 
315
    if(d->highlighting) {
 
316
        anim->start();
 
317
    } else {
 
318
        anim->setDirection(QAbstractAnimation::Backward);
 
319
        anim->start(QAbstractAnimation::DeleteWhenStopped);
 
320
 
 
321
    }
 
322
}
 
323
 
 
324
void PanelToolBox::setHighlightValue(qreal progress)
 
325
{
 
326
    d->animFrame = progress;
 
327
    update();
 
328
}
 
329
 
 
330
qreal PanelToolBox::highlightValue() const
 
331
{
 
332
    return d->animFrame;
 
333
}
 
334
 
 
335
void PanelToolBox::toggle()
 
336
{
 
337
    setShowing(!isShowing());
 
338
}
 
339
 
 
340
 
 
341
#include "paneltoolbox_p.moc"
 
342