~ubuntu-branches/ubuntu/jaunty/kde4libs/jaunty-updates

« back to all changes in this revision

Viewing changes to plasma/private/desktoptoolbox.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2008-12-11 18:26:08 UTC
  • mfrom: (1.1.24 upstream)
  • Revision ID: james.westby@ubuntu.com-20081211182608-tsu6p8ncbw1gnqxt
Tags: 4:4.1.85-0ubuntu1
* New upstream release
* Patches:
  + Removed 15_kfreebsd_support.diff from patches/series (doesn't apply and
    has no use for Ubuntu)
  + Redid 20_use_dejavu_as_default_font.diff
  + Completely removed kubuntu_09_fix_application_menu.diff (applied upstream)
  + Refreshed kubuntu_54_use_xdg_menu_prefix.diff
  + Dropped plasma/widgets/toolbutton.cpp from kubuntu_qt_ftbfs.diff (applied
    upstream)
  + Global quilt refresh

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <QPainter>
25
25
#include <QRadialGradient>
26
26
#include <QGraphicsView>
 
27
#include <QAction>
27
28
 
28
29
#include <kcolorscheme.h>
29
30
#include <kdebug.h>
30
31
 
31
32
#include <plasma/theme.h>
32
33
#include <plasma/paintutils.h>
 
34
#include <plasma/framesvg.h>
33
35
 
34
36
#include <plasma/applet.h>
 
37
#include <plasma/containment.h>
 
38
#include <plasma/widgets/iconwidget.h>
35
39
 
36
40
namespace Plasma
37
41
{
40
44
{
41
45
    public:
42
46
        EmptyGraphicsItem(QGraphicsItem *parent)
43
 
            : QGraphicsItem(parent)
 
47
            : QGraphicsItem(parent),
 
48
              m_toolbar(false)
44
49
        {
45
50
            setAcceptsHoverEvents(true);
 
51
            m_background = new Plasma::FrameSvg();
 
52
            m_toolbarBackground = new Plasma::FrameSvg();
 
53
 
 
54
            m_toolbarBackground->setImagePath("widgets/background");
 
55
            m_background->setImagePath("widgets/translucentbackground");
 
56
 
 
57
            m_toolbarBackground->setEnabledBorders(FrameSvg::LeftBorder|FrameSvg::RightBorder|FrameSvg::BottomBorder);
 
58
            m_background->setEnabledBorders(FrameSvg::AllBorders);
 
59
        }
 
60
 
 
61
        ~EmptyGraphicsItem()
 
62
        {
 
63
            delete m_background;
 
64
            delete m_toolbarBackground;
46
65
        }
47
66
 
48
67
        QRectF boundingRect() const
55
74
            return m_rect;
56
75
        }
57
76
 
 
77
        void setIsToolbar(bool toolbar)
 
78
        {
 
79
            m_toolbar = toolbar;
 
80
        }
 
81
 
 
82
        bool isToolbar() const
 
83
        {
 
84
            return m_toolbar;
 
85
        }
 
86
 
 
87
        void getContentsMargins(qreal &left, qreal &top, qreal &right, qreal &bottom)
 
88
        {
 
89
            if (m_toolbar) {
 
90
                m_toolbarBackground->getMargins(left, top, right, bottom);
 
91
            } else {
 
92
                m_background->getMargins(left, top, right, bottom);
 
93
            }
 
94
        }
 
95
 
 
96
        QRectF contentsRect() const
 
97
        {
 
98
            qreal left, top, right, bottom;
 
99
 
 
100
            if (m_toolbar) {
 
101
                m_toolbarBackground->getMargins(left, top, right, bottom);
 
102
            } else {
 
103
                m_background->getMargins(left, top, right, bottom);
 
104
            }
 
105
            return m_rect.adjusted(left, top, -right, -bottom);
 
106
        }
 
107
 
58
108
        void setRect(const QRectF &rect)
59
109
        {
60
110
            //kDebug() << "setting rect to" << rect;
61
111
            prepareGeometryChange();
62
112
            m_rect = rect;
63
 
            setPos(rect.topLeft());
 
113
            setPos(m_rect.topLeft());
 
114
            if (m_toolbar) {
 
115
                m_toolbarBackground->resizeFrame(m_rect.size());
 
116
            } else {
 
117
                m_background->resizeFrame(m_rect.size());
 
118
            }
64
119
        }
65
120
 
66
121
        void paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
67
122
        {
68
 
            Q_UNUSED(p)
69
 
            //p->setPen(Qt::red);
70
 
            //p->drawRect(boundingRect());
71
 
            p->setRenderHints(QPainter::Antialiasing);
72
 
            p->translate(0.5, 0.5);
73
 
            //TODO: use Plasma::Theme, and a gradient for the brush!
74
 
            p->setPen(QPen(Qt::white, 1));
75
 
            p->setBrush(QColor(0, 0, 0, 160));
76
 
            QPainterPath path = PaintUtils::roundedRectangle(boundingRect(), 10);
77
 
            p->drawPath(path);
 
123
            if (m_toolbar) {
 
124
                m_toolbarBackground->paintFrame(p);
 
125
            } else {
 
126
                m_background->paintFrame(p);
 
127
            }
78
128
        }
79
129
 
80
130
    private:
 
131
        bool m_toolbar;
81
132
        QRectF m_rect;
 
133
        Plasma::FrameSvg *m_toolbarBackground;
 
134
        Plasma::FrameSvg *m_background;
82
135
};
83
136
 
84
137
// used with QGrahphicsItem::setData
87
140
class DesktopToolBoxPrivate
88
141
{
89
142
public:
90
 
    DesktopToolBoxPrivate()
91
 
      : icon("plasma"),
 
143
    DesktopToolBoxPrivate(DesktopToolBox *toolbox)
 
144
      : q(toolbox),
 
145
        containment(0),
 
146
        icon("plasma"),
92
147
        toolBacker(0),
93
148
        animCircleId(0),
94
149
        animHighlightId(0),
97
152
        hovering(0)
98
153
    {}
99
154
 
 
155
 
 
156
    DesktopToolBox *q;
 
157
    Plasma::FrameSvg *background;
 
158
    Containment *containment;
100
159
    KIcon icon;
101
160
    EmptyGraphicsItem *toolBacker;
102
161
    int animCircleId;
111
170
 
112
171
DesktopToolBox::DesktopToolBox(Containment *parent)
113
172
    : ToolBox(parent),
114
 
      d(new DesktopToolBoxPrivate)
 
173
      d(new DesktopToolBoxPrivate(this))
115
174
{
 
175
    d->containment = parent;
116
176
    setZValue(10000000);
117
177
    setFlag(ItemClipsToShape, true);
118
178
    setFlag(ItemClipsChildrenToShape, false);
120
180
    setIsMovable(true);
121
181
    assignColors();
122
182
 
 
183
    d->background = new Plasma::FrameSvg();
 
184
    d->background->setImagePath("widgets/translucentbackground");
 
185
 
123
186
    connect(Plasma::Animator::self(), SIGNAL(movementFinished(QGraphicsItem*)),
124
187
            this, SLOT(toolMoved(QGraphicsItem*)));
125
188
    connect(this, SIGNAL(toggled()), this, SLOT(toggle()));
148
211
    Q_UNUSED(option)
149
212
    Q_UNUSED(widget)
150
213
 
 
214
    if (isToolbar()){
 
215
        return;
 
216
    }
 
217
 
151
218
    QPainterPath p = shape();
152
219
 
153
220
    QPoint iconPos;
154
221
    QPointF gradientCenter;
155
222
    const QRectF rect = boundingRect();
156
223
    const QSize icons = iconSize();
 
224
    bool atCorner;
157
225
 
158
226
    switch (corner()) {
159
227
    case TopRight:
160
228
        iconPos = QPoint((int)rect.right() - icons.width() + 2, 2);
161
229
        gradientCenter = rect.topRight();
 
230
        atCorner = true;
162
231
        break;
163
232
    case Top:
164
233
        iconPos = QPoint(rect.center().x() - icons.width() / 2, 2);
165
234
        gradientCenter = QPoint(rect.center().x(), rect.y());
 
235
        atCorner = false;
 
236
        d->background->setEnabledBorders(FrameSvg::BottomBorder|FrameSvg::LeftBorder|FrameSvg::RightBorder);
166
237
        break;
167
238
    case TopLeft:
168
239
        iconPos = QPoint(2, 2);
169
240
        gradientCenter = rect.topLeft();
 
241
        atCorner = true;
170
242
        break;
171
243
    case Left:
172
244
        iconPos = QPoint(2, rect.center().y() - icons.height() / 2);
173
245
        gradientCenter = QPointF(rect.left(), rect.center().y());
 
246
        atCorner = false;
 
247
        d->background->setEnabledBorders(FrameSvg::BottomBorder|FrameSvg::TopBorder|FrameSvg::RightBorder);
174
248
        break;
175
249
    case Right:
176
250
        iconPos = QPoint((int)rect.right() - icons.width() + 2,
177
251
                         rect.center().y() - icons.height() / 2);
178
252
        gradientCenter = QPointF(rect.right(), rect.center().y());
 
253
        atCorner = false;
 
254
        d->background->setEnabledBorders(FrameSvg::BottomBorder|FrameSvg::TopBorder|FrameSvg::LeftBorder);
179
255
        break;
180
256
    case BottomLeft:
181
257
        iconPos = QPoint(2, rect.bottom() - icons.height() - 2);
182
258
        gradientCenter = rect.bottomLeft();
 
259
        atCorner = true;
183
260
        break;
184
261
    case Bottom:
185
262
        iconPos = QPoint(rect.center().x() - icons.width() / 2,
186
263
                         rect.bottom() - icons.height() - 2);
187
264
        gradientCenter = QPointF(rect.center().x(), rect.bottom());
 
265
        atCorner = false;
 
266
        d->background->setEnabledBorders(FrameSvg::TopBorder|FrameSvg::LeftBorder|FrameSvg::RightBorder);
188
267
        break;
189
268
    case BottomRight:
190
269
    default:
191
270
        iconPos = QPoint((int)rect.right() - icons.width() + 2,
192
271
                         (int)rect.bottom() - icons.height() - 2);
193
272
        gradientCenter = rect.bottomRight();
 
273
        atCorner = true;
194
274
        break;
195
275
    }
196
276
 
197
 
    d->bgColor.setAlpha(64);
198
 
    d->fgColor.setAlpha(64);
199
 
    QRadialGradient gradient(gradientCenter, size() + d->animCircleFrame);
200
 
    gradient.setFocalPoint(gradientCenter);
201
 
    gradient.setColorAt(0, d->bgColor);
202
 
    gradient.setColorAt(.87, d->bgColor);
203
 
    gradient.setColorAt(.97, d->fgColor);
204
 
    d->fgColor.setAlpha(0);
205
 
    gradient.setColorAt(1, d->fgColor);
206
 
    painter->save();
207
 
    painter->setPen(Qt::NoPen);
208
 
    painter->setRenderHint(QPainter::Antialiasing, true);
209
 
    painter->setBrush(gradient);
210
 
    painter->drawPath(p);
211
 
    painter->restore();
 
277
    if (atCorner) {
 
278
        d->bgColor.setAlpha(64);
 
279
        d->fgColor.setAlpha(64);
 
280
        QRadialGradient gradient(gradientCenter, size() + d->animCircleFrame);
 
281
        gradient.setFocalPoint(gradientCenter);
 
282
        gradient.setColorAt(0, d->bgColor);
 
283
        gradient.setColorAt(.87, d->bgColor);
 
284
        gradient.setColorAt(.97, d->fgColor);
 
285
        d->fgColor.setAlpha(0);
 
286
        gradient.setColorAt(1, d->fgColor);
 
287
        painter->save();
 
288
        painter->setPen(Qt::NoPen);
 
289
        painter->setRenderHint(QPainter::Antialiasing, true);
 
290
        painter->setBrush(gradient);
 
291
        painter->drawPath(p);
 
292
        painter->restore();
 
293
    } else {
 
294
        d->background->resizeFrame(rect.size());
 
295
        d->background->paintFrame(painter);
 
296
    }
212
297
 
213
298
    const qreal progress = d->animHighlightFrame;
214
299
 
278
363
 
279
364
void DesktopToolBox::showToolBox()
280
365
{
281
 
    if (showing()) {
 
366
    if (showing() && !isToolbar()) {
282
367
        return;
283
368
    }
284
369
 
285
 
    int maxwidth = 0;
286
 
    foreach (QGraphicsItem *tool, QGraphicsItem::children()) {
287
 
        if (tool->isEnabled()) {
288
 
            maxwidth = qMax(static_cast<int>(tool->boundingRect().width()), maxwidth);
289
 
        }
290
 
    }
291
 
 
292
370
    // put tools 5px from icon edge
293
371
    const int iconWidth = 32;
294
372
    int x;
295
373
    int y;
296
374
    switch (corner()) {
297
375
    case TopRight:
298
 
        x = (int)boundingRect().right() - maxwidth - iconWidth - 5;
 
376
        x = (int)boundingRect().right() - iconWidth - 5;
299
377
        y = (int)boundingRect().top() + 10;
300
378
        break;
301
379
    case Top:
311
389
        y = (int)boundingRect().center().y() - iconWidth;
312
390
        break;
313
391
    case Right:
314
 
        x = (int)boundingRect().right() - maxwidth - iconWidth - 5;
 
392
        x = (int)boundingRect().right() - iconWidth - 5;
315
393
        y = (int)boundingRect().center().y() - iconWidth;
316
394
        break;
317
395
    case BottomLeft:
324
402
        break;
325
403
    case BottomRight:
326
404
    default:
327
 
        x = (int)boundingRect().right() - maxwidth - iconWidth - 5;
 
405
        x = (int)boundingRect().right() - iconWidth - 5;
328
406
        y = (int)boundingRect().bottom() - iconWidth - 5;
329
407
        break;
330
408
    }
331
409
 
332
 
    int startX = x;
333
410
    int startY = y;
334
 
    x += 5;
335
411
 
336
 
    //kDebug() << "starting at" << startX << startY;
337
412
    // find our theoretical X and Y end coordinates
 
413
 
 
414
    int maxWidth = 0;
 
415
    int maxHeight = 0;
 
416
    int totalWidth = 0;
 
417
 
338
418
    foreach (QGraphicsItem *tool, QGraphicsItem::children()) {
339
419
        if (tool == d->toolBacker) {
340
420
            continue;
341
421
        }
342
422
 
 
423
        Plasma::IconWidget *icon = qgraphicsitem_cast<Plasma::IconWidget *>(tool);
 
424
        if (icon) {
 
425
            if (viewTransform().m11() != Plasma::scalingFactor(Plasma::OverviewZoom) &&
 
426
                (viewTransform().m11() == Plasma::scalingFactor(Plasma::DesktopZoom) ||
 
427
                 icon->action() == d->containment->action("add sibling containment") ||
 
428
                 icon->action() == d->containment->action("add widgets"))) {
 
429
                icon->setText(icon->action()->text());
 
430
                icon->resize(icon->sizeFromIconSize(22));
 
431
            } else {
 
432
                icon->setText(QString());
 
433
                icon->resize(icon->sizeFromIconSize(22));
 
434
            }
 
435
        }
 
436
 
343
437
        if (tool->isEnabled()) {
344
438
            //kDebug() << tool << "is enabled";
345
439
            y += 5;
346
 
            //kDebug() << "let's show and move" << tool << tool->boundingRect();
347
 
            tool->show();
348
 
            //x += 0;
 
440
            QSize toolSize = tool->boundingRect().size().toSize();
 
441
            totalWidth += toolSize.width() + 5;
 
442
 
 
443
            maxWidth = qMax(toolSize.width(), maxWidth);
 
444
            maxHeight = qMax(toolSize.height(), maxHeight);
349
445
            y += static_cast<int>(tool->boundingRect().height());
350
446
        }
351
447
    }
352
448
 
 
449
    if (corner() == TopRight || corner() == Right || corner() == BottomRight) {
 
450
        x -= maxWidth;
 
451
    }
 
452
 
 
453
    y += 5;
353
454
    // the rect the tools back should have
354
 
    QRectF backerRect = QRectF(QPointF(startX, startY), QSizeF(maxwidth + 10, y - startY));
355
 
 
356
 
    // now check that is actually fits within the parent's boundaries
357
 
    backerRect = mapToParent(backerRect).boundingRect();
358
 
    QSizeF parentSize = parentWidget()->size();
359
 
    if (backerRect.x() < 5) {
360
 
        backerRect.moveLeft(5);
361
 
    } else if (backerRect.right() > parentSize.width() - 5) {
362
 
        backerRect.moveRight(parentSize.width() - 5);
363
 
    }
364
 
 
365
 
    if (backerRect.y() < 5) {
366
 
        backerRect.moveTop(5);
367
 
    } else if (backerRect.bottom() > parentSize.height() - 5) {
368
 
        backerRect.moveBottom(parentSize.height() - 5);
369
 
    }
370
 
 
371
 
    // re-map our starting points back to our coordinate system
372
 
    backerRect = mapFromParent(backerRect).boundingRect();
 
455
    QRectF backerRect = QRectF(QPointF(x, startY), QSizeF(maxWidth + 10, y - startY));
 
456
 
 
457
    if (!d->toolBacker) {
 
458
        d->toolBacker = new EmptyGraphicsItem(this);
 
459
    }
 
460
 
 
461
    d->toolBacker->setIsToolbar(isToolbar());
 
462
 
 
463
    if (isToolbar()) {
 
464
        QPointF topRight;
 
465
 
 
466
        //could that cast ever fail?
 
467
        if (d->containment) {
 
468
            topRight = viewTransform().map(mapFromParent(d->containment->boundingRect().bottomRight()));
 
469
        } else {
 
470
            topRight = boundingRect().topRight();
 
471
        }
 
472
 
 
473
        qreal left, top, right, bottom;
 
474
        d->toolBacker->getContentsMargins(left, top, right, bottom);
 
475
        x -= left;
 
476
 
 
477
        backerRect.setSize(QSize(totalWidth+left+right, maxHeight+top+bottom));
 
478
        backerRect.moveTopRight(topRight);
 
479
    } else {
 
480
        //kDebug() << "starting at" <<  x << startY;
 
481
 
 
482
        // now check that is actually fits within the parent's boundaries
 
483
        backerRect = mapToParent(backerRect).boundingRect();
 
484
        QSizeF parentSize = parentWidget()->size();
 
485
        if (backerRect.x() < 5) {
 
486
            backerRect.moveLeft(5);
 
487
        } else if (backerRect.right() > parentSize.width() - 5) {
 
488
            backerRect.moveRight(parentSize.width() - 5);
 
489
        }
 
490
 
 
491
        if (backerRect.y() < 5) {
 
492
            backerRect.moveTop(5);
 
493
        } else if (backerRect.bottom() > parentSize.height() - 5) {
 
494
            backerRect.moveBottom(parentSize.height() - 5);
 
495
        }
 
496
 
 
497
        // re-map our starting points back to our coordinate system
 
498
        backerRect = mapFromParent(backerRect).boundingRect();
 
499
    }
373
500
    x = backerRect.x() + 5;
374
501
    y = backerRect.y();
375
502
 
381
508
        }
382
509
 
383
510
        if (tool->isEnabled()) {
384
 
            //kDebug() << tool << "is enabled";
385
 
            y += 5;
386
 
            //kDebug() << "let's show and move" << tool << tool->boundingRect();
387
 
            tool->show();
388
 
            animdriver->moveItem(tool, Plasma::Animator::SlideInMovement, QPoint(x, y));
389
 
            //x += 0;
390
 
            y += static_cast<int>(tool->boundingRect().height());
 
511
            if (isToolbar()) {
 
512
                //kDebug() << tool << "is enabled";
 
513
                x += 5;
 
514
                //kDebug() << "let's show and move" << tool << tool->boundingRect();
 
515
                tool->show();
 
516
                animdriver->moveItem(tool, Plasma::Animator::SlideInMovement, QPoint(x, y));
 
517
 
 
518
                x += static_cast<int>(tool->boundingRect().width());
 
519
            } else {
 
520
                //kDebug() << tool << "is enabled";
 
521
                y += 5;
 
522
                //kDebug() << "let's show and move" << tool << tool->boundingRect();
 
523
                tool->show();
 
524
                animdriver->moveItem(tool, Plasma::Animator::SlideInMovement, QPoint(x, y));
 
525
                //x += 0;
 
526
                y += static_cast<int>(tool->boundingRect().height());
 
527
            }
391
528
        } else if (tool->isVisible()) {
392
529
            // disabled, but visible, so hide it!
393
530
            const int height = static_cast<int>(tool->boundingRect().height());
396
533
        }
397
534
    }
398
535
 
399
 
    if (!d->toolBacker) {
400
 
        d->toolBacker = new EmptyGraphicsItem(this);
401
 
    }
402
536
 
403
537
    d->toolBacker->setRect(backerRect);
404
538
    d->toolBacker->show();
414
548
{
415
549
    //kDebug() << event->pos() << event->scenePos()
416
550
    //         << d->toolBacker->rect().contains(event->scenePos().toPoint());
417
 
    if (! d->hovering) {
 
551
    if (!d->hovering || isToolbar()) {
418
552
        QGraphicsItem::hoverLeaveEvent(event);
419
553
        return;
420
554
    }
485
619
 
486
620
void DesktopToolBox::toggle()
487
621
{
 
622
    if (isToolbar()) {
 
623
        return;
 
624
    }
 
625
 
488
626
    if (showing()) {
489
627
        hideToolBox();
490
628
    } else {