~neon/kdeplasma-addons/trunk

« back to all changes in this revision

Viewing changes to libs/lancelot/widgets/BasicWidget.cpp

  • Committer: asouza
  • Date: 2011-02-01 19:41:58 UTC
  • Revision ID: svn-v4:283d02a7-25f6-0310-bc7c-ecb5cbfe19da:trunk/KDE/kdeplasma-addons:1218275
Move kdeplasma-addons to git

- You can find information about the project in the link below:

https://projects.kde.org/projects/kde/kdeplasma-addons/repository

- And you can clone the repository using:

git clone git://anongit.kde.org/kdeplasma-addons


Thanks eean and all the sysadmins for the help.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *   Copyright (C) 2007, 2008, 2009, 2010 Ivan Cukic <ivan.cukic(at)kde.org>
3
 
 *
4
 
 *   This program is free software; you can redistribute it and/or modify
5
 
 *   it under the terms of the GNU Lesser/Library General Public License version 2,
6
 
 *   or (at your option) any later version, as published by the Free
7
 
 *   Software Foundation
8
 
 *
9
 
 *   This program is distributed in the hope that it will be useful,
10
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 *   GNU Lesser/Library General Public License for more details
13
 
 *
14
 
 *   You should have received a copy of the GNU Lesser/Library General Public
15
 
 *   License along with this program; if not, write to the
16
 
 *   Free Software Foundation, Inc.,
17
 
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
 
 */
19
 
 
20
 
#include "BasicWidget.h"
21
 
#include "Global.h"
22
 
 
23
 
#include <QApplication>
24
 
#include <QtGui/QPainter>
25
 
 
26
 
#include <KGlobalSettings>
27
 
 
28
 
#include <Plasma/PaintUtils>
29
 
 
30
 
#include <cmath>
31
 
 
32
 
#include <lancelot/lancelot.h>
33
 
 
34
 
#define WIDGET_PADDING 8
35
 
 
36
 
#define max(A, B) ((A) >= (B)) ? (A) : (B)
37
 
 
38
 
namespace Lancelot
39
 
{
40
 
 
41
 
class BasicWidget::Private {
42
 
    public:
43
 
    Private(BasicWidget * parent, QString title = QString(), QString description = QString())
44
 
      : icon(QIcon()), iconSize(32, 32),
45
 
        innerOrientation(Qt::Horizontal), alignment(Qt::AlignCenter),
46
 
        title(title), description(description)
47
 
    {
48
 
        init(parent);
49
 
    }
50
 
 
51
 
    Private(BasicWidget * parent, QIcon icon, QString title, QString description)
52
 
      : icon(icon), iconSize(32, 32),
53
 
        innerOrientation(Qt::Horizontal), alignment(Qt::AlignCenter),
54
 
        title(title), description(description)
55
 
    {
56
 
        init(parent);
57
 
    }
58
 
 
59
 
    Private(BasicWidget * parent, const Plasma::Svg & icon, QString title, QString description)
60
 
      : icon(QIcon()), iconSize(32, 32),
61
 
        innerOrientation(Qt::Horizontal), alignment(Qt::AlignCenter),
62
 
        title(title), description(description)
63
 
    {
64
 
        iconInSvg.setImagePath(icon.imagePath());
65
 
        init(parent);
66
 
    }
67
 
 
68
 
    void init(BasicWidget * parent)
69
 
    {
70
 
        parent->setAcceptsHoverEvents(true);
71
 
        // parent->resize(140, 38);
72
 
        parent->setGroupByName("BasicWidget");
73
 
    }
74
 
 
75
 
    int shortcutPosition(QString & text)
76
 
    {
77
 
        Q_UNUSED(text);
78
 
 
79
 
        int index = 0;
80
 
        while ((index = text.indexOf('&', index)) != -1) {
81
 
            if (index == text.size() - 1) {
82
 
                return -1;
83
 
            }
84
 
 
85
 
            if (text.at(index + 1) != '&') {
86
 
                return index + 1;
87
 
            }
88
 
 
89
 
            index++;
90
 
            text.remove(index, 1);
91
 
        }
92
 
        return -1;
93
 
    }
94
 
 
95
 
    void rotatePainterForIcon(QPainter * painter, qreal angle, QRect & iconRect)
96
 
    {
97
 
        // TODO: Make this work for other angles
98
 
        if (angle != 90 && angle != -90) return;
99
 
 
100
 
        painter->rotate(angle);
101
 
 
102
 
        iconRect.moveLeft(iconRect.width() / 2);
103
 
        iconRect.moveTop(WIDGET_PADDING - iconRect.height() * 1.5);
104
 
    }
105
 
 
106
 
    QIcon icon;
107
 
    Plasma::Svg iconInSvg;
108
 
    QSize iconSize;
109
 
    Qt::Orientation innerOrientation;
110
 
 
111
 
    Qt::Alignment alignment;
112
 
 
113
 
    QString title;
114
 
    QString description;
115
 
};
116
 
 
117
 
BasicWidget::BasicWidget(QGraphicsItem * parent)
118
 
  : Widget(),
119
 
    d(new Private(this))
120
 
{
121
 
    setParentItem(parent);
122
 
}
123
 
 
124
 
BasicWidget::BasicWidget(QString title, QString description,
125
 
        QGraphicsItem * parent)
126
 
  : Widget(),
127
 
    d(new Private(this, title, description))
128
 
{
129
 
    setParentItem(parent);
130
 
}
131
 
 
132
 
BasicWidget::BasicWidget(QIcon icon, QString title,
133
 
        QString description, QGraphicsItem * parent)
134
 
  : Widget(),
135
 
    d(new Private(this, icon, title, description))
136
 
{
137
 
    setParentItem(parent);
138
 
}
139
 
 
140
 
BasicWidget::BasicWidget(const Plasma::Svg & icon, QString title,
141
 
        QString description, QGraphicsItem * parent)
142
 
  : Widget(),
143
 
    d(new Private(this, icon, title, description))
144
 
{
145
 
    setParentItem(parent);
146
 
}
147
 
 
148
 
BasicWidget::~BasicWidget()
149
 
{
150
 
    delete d;
151
 
}
152
 
 
153
 
void BasicWidget::paint(QPainter * painter,
154
 
        const QStyleOptionGraphicsItem * option, QWidget * widget)
155
 
{
156
 
    Q_UNUSED(widget);
157
 
    Q_UNUSED(option);
158
 
 
159
 
    paintBackground(painter);
160
 
    paintForeground(painter);
161
 
}
162
 
 
163
 
// macro for setting the left coordinate of items
164
 
// relative to the parent and with alignment
165
 
// taken into consideration
166
 
#define setLeft(itemRect, parentRect, alignment) \
167
 
    if ((parentRect).width() > (itemRect).width()) { \
168
 
        if ((alignment) & Qt::AlignHCenter) \
169
 
        (itemRect).moveLeft(WIDGET_PADDING + ((parentRect).width() - (itemRect).width()) / 2); \
170
 
        else if ((alignment) & Qt::AlignRight) \
171
 
        (itemRect).moveLeft(WIDGET_PADDING + (parentRect).width() - (itemRect).width()); \
172
 
    } else { \
173
 
        (itemRect).setWidth((parentRect).width()); \
174
 
        (itemRect).moveLeft(WIDGET_PADDING); \
175
 
    };
176
 
 
177
 
void BasicWidget::paintForeground(QPainter * painter)
178
 
{
179
 
    bool rtl = QApplication::isRightToLeft();
180
 
 
181
 
    QPainter * _painter = painter;
182
 
 
183
 
    QPixmap foreground(size().toSize().width(), size().toSize().height());
184
 
    foreground.fill(Qt::transparent);
185
 
 
186
 
    // Replacing painter with QImage painter
187
 
    QPainter fpainter(&foreground);
188
 
    painter = &fpainter;
189
 
 
190
 
    QColor fgColor;
191
 
    if (!isEnabled()) {
192
 
        fgColor = group()->foregroundColor()->disabled;
193
 
    } else if (isHovered()) {
194
 
        fgColor = group()->foregroundColor()->active;
195
 
    } else {
196
 
        fgColor = group()->foregroundColor()->normal;
197
 
    }
198
 
    painter->setPen(QPen(fgColor));
199
 
 
200
 
    QFont titleFont = painter->font();
201
 
    QFont descriptionFont = KGlobalSettings::smallestReadableFont();
202
 
 
203
 
    QRectF widgetRect     = QRectF(0, 0, size().width() - 2 * WIDGET_PADDING, size().height() - 2 * WIDGET_PADDING);
204
 
    QRectF iconRect;
205
 
 
206
 
    if (!d->icon.isNull() || d->iconInSvg.isValid()) {
207
 
        iconRect = QRectF(QPointF(), d->iconSize);
208
 
        if (iconRect.width() > geometry().width()) {
209
 
            iconRect.setWidth(geometry().width());
210
 
        }
211
 
        if (iconRect.height() > geometry().height()) {
212
 
            iconRect.setHeight(geometry().height());
213
 
        }
214
 
    }
215
 
 
216
 
    // painter->setFont(titleFont)); // NOT NEEDED
217
 
    QRectF titleRect        = painter->boundingRect(widgetRect,
218
 
            Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine, d->title);
219
 
 
220
 
    painter->setFont(descriptionFont);
221
 
    QRectF descriptionRect  = painter->boundingRect(widgetRect,
222
 
            Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine, d->description);
223
 
 
224
 
    if (d->innerOrientation == Qt::Vertical || (d->title.isEmpty() && d->description.isEmpty())) {
225
 
 
226
 
        // Modified setLeft macro for icon since we can not cut it if it's larger than needed
227
 
        // setLeft(iconRect, widgetRect, d->alignment);
228
 
 
229
 
        if (d->alignment & Qt::AlignHCenter) {
230
 
            iconRect.moveLeft(WIDGET_PADDING + (widgetRect.width() - iconRect.width()) / 2);
231
 
        } else if (d->alignment & Qt::AlignRight) {
232
 
            iconRect.moveLeft(WIDGET_PADDING + widgetRect.width() - iconRect.width());
233
 
        }
234
 
 
235
 
        setLeft(titleRect, widgetRect, d->alignment);
236
 
        setLeft(descriptionRect, widgetRect, d->alignment);
237
 
 
238
 
        qreal top = WIDGET_PADDING, height =
239
 
            iconRect.height() + titleRect.height() + descriptionRect.height();
240
 
 
241
 
        if ((!d->icon.isNull() || d->iconInSvg.isValid()) && !(d->title.isEmpty() && d->description.isEmpty()))
242
 
            height += WIDGET_PADDING;
243
 
 
244
 
        if (d->alignment & Qt::AlignVCenter)
245
 
            top = (widgetRect.height() - height) / 2 + WIDGET_PADDING;
246
 
        if (d->alignment & Qt::AlignBottom)
247
 
            top = widgetRect.height() - height + WIDGET_PADDING;
248
 
 
249
 
        if (!d->icon.isNull() || d->iconInSvg.isValid()) { // using real painter...
250
 
 
251
 
            iconRect.moveTop(top);
252
 
            QRect rect(QPoint(lround(iconRect.left()), lround(iconRect.top())), d->iconSize);
253
 
 
254
 
            d->rotatePainterForIcon(_painter, -rotation(), rect);
255
 
 
256
 
            if (!d->icon.isNull()) {
257
 
                d->icon.paint(_painter, rect);
258
 
            } else {
259
 
                d->iconInSvg.resize(rect.size());
260
 
                d->iconInSvg.paint(_painter, rect.left(), rect.top(), isHovered()?"active":"inactive");
261
 
            }
262
 
            top += d->iconSize.height() + WIDGET_PADDING;
263
 
 
264
 
            d->rotatePainterForIcon(_painter, rotation(), rect);
265
 
        }
266
 
 
267
 
        if (!d->title.isEmpty()) {
268
 
            titleRect.moveTop(top);
269
 
            painter->setFont(titleFont);
270
 
            // painter->drawText(titleRect,
271
 
            //        Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine | Qt::ElideRight, d->title);
272
 
            drawText(painter, titleRect,
273
 
                   Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine | Qt::ElideRight, d->title, true);
274
 
            top += titleRect.height();
275
 
        }
276
 
 
277
 
        if (!d->description.isEmpty()) {
278
 
            descriptionRect.moveTop(top);
279
 
 
280
 
            painter->setFont(descriptionFont);
281
 
            // painter->drawText(descriptionRect,
282
 
            //         Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine | Qt::ElideRight, d->description);
283
 
            drawText(painter, descriptionRect,
284
 
                    Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine | Qt::ElideRight, d->description, false);
285
 
        }
286
 
    } else {
287
 
        // Horizontal layout
288
 
        qreal /*left = WIDGET_PADDING,*/ width =
289
 
            iconRect.width() + fmaxf(titleRect.width(), descriptionRect.width()) +
290
 
            WIDGET_PADDING;
291
 
 
292
 
        if (d->alignment & Qt::AlignTop) {
293
 
            iconRect.moveTop(WIDGET_PADDING);
294
 
            titleRect.moveTop(WIDGET_PADDING);
295
 
            descriptionRect.moveTop(titleRect.bottom());
296
 
        } else if (d->alignment & (Qt::AlignVCenter | Qt::AlignBottom)) {
297
 
            iconRect.moveTop(WIDGET_PADDING +
298
 
                    ((d->alignment & Qt::AlignVCenter) ? 0.5 : 1) * (widgetRect.height() - iconRect.height()));
299
 
            titleRect.moveTop(WIDGET_PADDING +
300
 
                    ((d->alignment & Qt::AlignVCenter) ? 0.5 : 1) * (widgetRect.height() -
301
 
                        ((d->description.isEmpty())?0:descriptionRect.height()) - titleRect.height()));
302
 
            descriptionRect.moveTop(titleRect.bottom());
303
 
        }
304
 
 
305
 
        if ((widgetRect.width() < width) || (d->alignment & Qt::AlignLeft)) {
306
 
            if (rtl) {
307
 
                iconRect.moveRight(widgetRect.right() + WIDGET_PADDING);
308
 
                titleRect.setWidth(widgetRect.width() - ((!d->icon.isNull() || d->iconInSvg.isValid()) ? iconRect.width() + WIDGET_PADDING : 0));
309
 
                descriptionRect.setWidth(titleRect.width());
310
 
            } else {
311
 
                iconRect.moveLeft(WIDGET_PADDING);
312
 
                titleRect.setWidth(widgetRect.width() - ((!d->icon.isNull() || d->iconInSvg.isValid()) ? iconRect.width() + WIDGET_PADDING : 0));
313
 
                descriptionRect.setWidth(titleRect.width());
314
 
            }
315
 
        } else if (d->alignment & Qt::AlignHCenter) {
316
 
            if (rtl) {
317
 
                iconRect.moveRight(WIDGET_PADDING + (widgetRect.width() + width) / 2);
318
 
            } else {
319
 
                iconRect.moveLeft(WIDGET_PADDING + (widgetRect.width() - width) / 2);
320
 
            }
321
 
        } else {
322
 
            if (rtl) {
323
 
                iconRect.moveRight(widgetRect.width() - WIDGET_PADDING - (widgetRect.width() - width));
324
 
            } else {
325
 
                iconRect.moveLeft(WIDGET_PADDING + (widgetRect.width() - width));
326
 
            }
327
 
        }
328
 
 
329
 
        if (rtl) {
330
 
            titleRect.moveRight(- WIDGET_PADDING + iconRect.left());
331
 
            descriptionRect.moveRight(- WIDGET_PADDING + iconRect.left());
332
 
        } else {
333
 
            titleRect.moveLeft(WIDGET_PADDING + iconRect.right());
334
 
            descriptionRect.moveLeft(WIDGET_PADDING + iconRect.right());
335
 
        }
336
 
 
337
 
        if (!d->icon.isNull() || d->iconInSvg.isValid()) {  // using real painter...
338
 
            QRect rect(QPoint(lround(iconRect.left()), lround(iconRect.top())), d->iconSize);
339
 
 
340
 
            d->rotatePainterForIcon(_painter, -rotation(), rect);
341
 
 
342
 
            if (!d->icon.isNull()) {
343
 
                QIcon::Mode mode;
344
 
                if (!isEnabled()) {
345
 
                    mode = QIcon::Disabled;
346
 
                } else if (isHovered()) {
347
 
                    mode = QIcon::Active;
348
 
                } else {
349
 
                    mode = QIcon::Normal;
350
 
                }
351
 
 
352
 
                d->icon.paint(_painter, rect, Qt::AlignCenter, mode, QIcon::Off);
353
 
            } else {
354
 
                d->iconInSvg.resize(d->iconSize);
355
 
                d->iconInSvg.paint(_painter, rect.left(), rect.top(), isHovered()?"active":"inactive"); //TODO: add disabled state
356
 
            }
357
 
 
358
 
            d->rotatePainterForIcon(_painter, rotation(), rect);
359
 
 
360
 
        }
361
 
 
362
 
        if (!d->title.isEmpty()) {
363
 
            painter->setFont(titleFont);
364
 
            // painter->drawText(titleRect,
365
 
            //         Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine, d->title);
366
 
            drawText(painter, titleRect,
367
 
                    Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine, d->title, true);
368
 
        }
369
 
 
370
 
        if (!d->description.isEmpty()) {
371
 
            if (!isHovered()) {
372
 
                QPen pen = painter->pen();
373
 
                QColor clr = painter->pen().color();
374
 
                clr.setAlphaF(0.3);
375
 
                painter->setPen(QPen(clr));
376
 
            }
377
 
            painter->setFont(descriptionFont);
378
 
            // painter->drawText(descriptionRect,
379
 
            //         Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine, d->description);
380
 
            drawText(painter, descriptionRect,
381
 
                    Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine, d->description, false);
382
 
        }
383
 
    }
384
 
 
385
 
    QLinearGradient gradient;
386
 
    if (QApplication::isRightToLeft()) {
387
 
        gradient = QLinearGradient(
388
 
                QPointF(WIDGET_PADDING, 0),
389
 
                QPointF(WIDGET_PADDING + 20, 0)
390
 
                );
391
 
        gradient.setColorAt(0, Qt::transparent);
392
 
        gradient.setColorAt(1, Qt::black);
393
 
    } else {
394
 
        gradient = QLinearGradient(
395
 
                QPointF(size().width() - WIDGET_PADDING - 20, 0),
396
 
                QPointF(size().width() - WIDGET_PADDING, 0)
397
 
                );
398
 
        gradient.setColorAt(1, Qt::transparent);
399
 
        gradient.setColorAt(0, Qt::black);
400
 
    }
401
 
    painter->setCompositionMode(QPainter::CompositionMode_DestinationIn);
402
 
    painter->fillRect(
403
 
            0, 0, (int)ceil(size().width()), (int)ceil(size().height()),
404
 
            gradient);
405
 
    //
406
 
 
407
 
    painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
408
 
    _painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
409
 
 
410
 
    _painter->drawPixmap(0, 0, foreground);
411
 
}
412
 
 
413
 
void BasicWidget::setIconSize(QSize size)
414
 
{
415
 
    d->iconSize = size;
416
 
    update();
417
 
    updateGeometry();
418
 
}
419
 
 
420
 
QSize BasicWidget::iconSize() const
421
 
{
422
 
    return d->iconSize;
423
 
}
424
 
 
425
 
void BasicWidget::setIcon(QIcon icon)
426
 
{
427
 
    d->icon = icon;
428
 
    update();
429
 
    updateGeometry();
430
 
}
431
 
 
432
 
QIcon BasicWidget::icon() const
433
 
{
434
 
    return d->icon;
435
 
}
436
 
 
437
 
void BasicWidget::setIconInSvg(const Plasma::Svg & icon)
438
 
{
439
 
    d->iconInSvg.setImagePath(icon.imagePath());
440
 
    update();
441
 
    updateGeometry();
442
 
}
443
 
 
444
 
Plasma::Svg & BasicWidget::iconInSvg() const
445
 
{
446
 
    return d->iconInSvg;
447
 
}
448
 
 
449
 
void BasicWidget::setTitle(const QString & value)
450
 
{
451
 
    d->title = value;
452
 
 
453
 
    QString title(value);
454
 
    int pos = d->shortcutPosition(title);
455
 
    if (pos > -1) {
456
 
        setShortcutKey(title.at(pos));
457
 
    }
458
 
 
459
 
    update();
460
 
    updateGeometry();
461
 
}
462
 
 
463
 
void BasicWidget::setShortcutKey(const QString & key)
464
 
{
465
 
  Q_UNUSED(key);
466
 
}
467
 
 
468
 
QString BasicWidget::title() const
469
 
{
470
 
    return d->title;
471
 
}
472
 
 
473
 
void BasicWidget::setDescription(const QString & description)
474
 
{
475
 
    d->description = description;
476
 
    update();
477
 
    updateGeometry();
478
 
}
479
 
 
480
 
QString BasicWidget::description() const
481
 
{
482
 
    return d->description;
483
 
}
484
 
 
485
 
void BasicWidget::setInnerOrientation(Qt::Orientation position) {
486
 
    d->innerOrientation = position;
487
 
    update();
488
 
    updateGeometry();
489
 
}
490
 
 
491
 
Qt::Orientation BasicWidget::innerOrientation() const
492
 
{
493
 
    return d->innerOrientation;
494
 
}
495
 
 
496
 
void BasicWidget::setAlignment(Qt::Alignment alignment)
497
 
{
498
 
    d->alignment = alignment;
499
 
    update();
500
 
}
501
 
 
502
 
Qt::Alignment BasicWidget::alignment() const
503
 
{
504
 
    return d->alignment;
505
 
}
506
 
 
507
 
QSizeF BasicWidget::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
508
 
{
509
 
    QSizeF result = QSizeF();
510
 
 
511
 
    switch (which) {
512
 
        case Qt::MinimumSize:
513
 
            result = d->iconSize;
514
 
            break;
515
 
        case Qt::MaximumSize:
516
 
            result = MAX_WIDGET_SIZE;
517
 
            break;
518
 
        default:
519
 
            // Do we need a more precise sizeHint?
520
 
            // result = d->iconSize + QSizeF(2 * WIDGET_PADDING, 2 * WIDGET_PADDING);
521
 
            result = d->iconSize;
522
 
            QFontMetrics titleMetrics = QFontMetrics(font());
523
 
            QFontMetrics desctiprionMetrics =
524
 
                    QFontMetrics(KGlobalSettings::smallestReadableFont());
525
 
            QSizeF textSize = QSizeF(
526
 
                    qMax(
527
 
                        titleMetrics.width(d->title),
528
 
                        desctiprionMetrics.width(d->description)
529
 
                    ),
530
 
                    (titleMetrics.height()) +
531
 
                    (d->description.isEmpty()?0:desctiprionMetrics.height())
532
 
                );
533
 
 
534
 
            if (d->innerOrientation == Qt::Horizontal) {
535
 
                result.rwidth() += textSize.width();
536
 
 
537
 
                if (result.height() < textSize.height()) {
538
 
                    result.setHeight(textSize.height());
539
 
                }
540
 
            } else {
541
 
                result.rheight() += textSize.height();
542
 
 
543
 
                if (result.width() < textSize.width()) {
544
 
                    result.setWidth(textSize.width());
545
 
                }
546
 
            }
547
 
            result += Widget::sizeHint(which, constraint) +
548
 
                QSizeF(3 * WIDGET_PADDING, 2 * WIDGET_PADDING);
549
 
    }
550
 
 
551
 
    if (constraint.isValid()) {
552
 
        result = result.boundedTo(constraint);
553
 
    }
554
 
 
555
 
    return result;
556
 
}
557
 
 
558
 
void BasicWidget::drawText(QPainter * painter, const QRectF & rectangle, int flags, const QString & txt, bool shortcutEnabled)
559
 
{
560
 
    Q_UNUSED(flags);
561
 
 
562
 
    if (txt.isEmpty()) {
563
 
        return;
564
 
    }
565
 
 
566
 
    QString text = txt;
567
 
    int shortcutPosition = d->shortcutPosition(text);
568
 
    if (shortcutPosition > -1 && shortcutEnabled) {
569
 
        text = text.remove(shortcutPosition - 1, 1);
570
 
    } else {
571
 
        shortcutEnabled = false;
572
 
    }
573
 
 
574
 
    static const int radius = 2;
575
 
    if (group()->hasProperty("BlurTextShadow")) {
576
 
        QColor textColor = painter->pen().color();
577
 
        QColor shadowColor;
578
 
        if (textColor.valueF() * textColor.alphaF() > 0.4) {
579
 
            shadowColor = Qt::black;
580
 
        } else {
581
 
            shadowColor = Qt::white;
582
 
        }
583
 
 
584
 
        QPixmap result = Plasma::PaintUtils::shadowText(
585
 
                text, textColor, shadowColor,
586
 
                QPoint(0, 0), radius);
587
 
 
588
 
        if (group()->hasProperty("TextColorBackground")) {
589
 
            QColor bgColor;
590
 
            if (!isEnabled()) {
591
 
                bgColor = group()->backgroundColor()->disabled;
592
 
            } else if (isHovered()) {
593
 
                bgColor = group()->backgroundColor()->active;
594
 
            } else {
595
 
                bgColor = group()->backgroundColor()->normal;
596
 
            }
597
 
            painter->setRenderHint(QPainter::Antialiasing);
598
 
            QRectF frect = QRectF(rectangle.topLeft(), result.size());
599
 
            painter->fillPath(
600
 
                    Plasma::PaintUtils::roundedRectangle(
601
 
                        frect, 2 * radius), QBrush(bgColor)
602
 
                    );
603
 
        }
604
 
 
605
 
        painter->drawPixmap(rectangle.topLeft(), result);
606
 
 
607
 
        if (shortcutEnabled) {
608
 
            int width = painter->boundingRect(
609
 
                    rectangle,
610
 
                    Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine,
611
 
                    text.left(shortcutPosition - 1)).width();
612
 
            QPixmap result = Plasma::PaintUtils::shadowText(
613
 
                    "_", textColor, shadowColor,
614
 
                    QPoint(0, 0), radius);
615
 
            painter->drawPixmap(rectangle.topLeft() + QPoint(width, 0), result);
616
 
        }
617
 
    } else {
618
 
        if (group()->hasProperty("TextColorBackground")) {
619
 
            QColor bgColor;
620
 
            if (!isEnabled()) {
621
 
                bgColor = group()->backgroundColor()->disabled;
622
 
            } else if (isHovered()) {
623
 
                bgColor = group()->backgroundColor()->active;
624
 
            } else {
625
 
                bgColor = group()->backgroundColor()->normal;
626
 
            }
627
 
            painter->setRenderHint(QPainter::Antialiasing);
628
 
            QRectF frect = painter->boundingRect(rectangle,
629
 
                    Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine, text);
630
 
            frect.adjust(- radius, - radius, radius, radius);
631
 
            painter->fillPath(
632
 
                    Plasma::PaintUtils::roundedRectangle(
633
 
                        frect, 2*radius), QBrush(bgColor)
634
 
                    );
635
 
        }
636
 
        painter->drawText(rectangle,
637
 
                Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine, text);
638
 
 
639
 
        if (shortcutEnabled) {
640
 
            int width = painter->boundingRect(
641
 
                    rectangle,
642
 
                    Qt::AlignLeft | Qt::AlignTop | Qt::TextSingleLine,
643
 
                    text.left(shortcutPosition - 1)).width();
644
 
            painter->drawText(
645
 
                    QRectF(rectangle.topLeft() + QPoint(width, 0), rectangle.size()),
646
 
                    QString('_'));
647
 
        }
648
 
    }
649
 
}
650
 
 
651
 
} // namespace Lancelot
652
 
 
653
 
#include "BasicWidget.moc"
654