2
* Copyright 2008 Aaron Seigo <aseigo@kde.org>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Library General Public License as
6
* published by the Free Software Foundation; either version 2, or
7
* (at your option) any later version.
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 General Public License for more details
14
* You should have received a copy of the GNU 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.
20
#include "browserhistorycombobox.h"
23
#include <QApplication>
24
#include <QPropertyAnimation>
26
#include <kcombobox.h>
27
#include <kmimetype.h>
28
#include <kiconeffect.h>
29
#include <kiconloader.h>
31
#include <plasma/theme.h>
32
#include <plasma/framesvg.h>
33
#include <plasma/animator.h>
34
#include <plasma/paintutils.h>
35
#include <Plasma/ComboBox>
37
#include <QtGui/QComboBox>
38
#include <QtCore/QSharedData>
39
#include <QtGui/QStyle>
47
ComboBoxPrivate(BrowserHistoryComboBox *comboBox)
59
void syncActiveRect();
61
void animationUpdate(qreal progress);
63
BrowserHistoryComboBox *q;
66
FrameSvg *lineEditBackground;
68
QPropertyAnimation *animation;
74
Plasma::ComboBox *styleParent;
79
void ComboBoxPrivate::syncActiveRect()
81
background->setElementPrefix("normal");
83
qreal left, top, right, bottom;
84
background->getMargins(left, top, right, bottom);
86
background->setElementPrefix("active");
87
qreal activeLeft, activeTop, activeRight, activeBottom;
88
background->getMargins(activeLeft, activeTop, activeRight, activeBottom);
90
activeRect = QRectF(QPointF(0, 0), q->size());
91
activeRect.adjust(left - activeLeft, top - activeTop,
92
-(right - activeRight), -(bottom - activeBottom));
94
background->setElementPrefix("normal");
97
void ComboBoxPrivate::syncBorders()
99
//set margins from the normal element
100
qreal left, top, right, bottom;
102
background->setElementPrefix("normal");
103
background->getMargins(left, top, right, bottom);
104
q->setContentsMargins(left, top, right, bottom);
106
//calc the rect for the over effect
109
KComboBox *native = q->nativeWidget();
111
native->setFont(q->font());
113
native->setFont(Theme::defaultTheme()->font(Theme::DefaultFont));
117
void ComboBoxPrivate::animationUpdate(qreal progress)
125
BrowserHistoryComboBox::BrowserHistoryComboBox(QGraphicsWidget *parent)
126
: QGraphicsProxyWidget(parent),
127
d(new ComboBoxPrivate(this))
129
d->background = new FrameSvg(this);
130
d->background->setImagePath("widgets/button");
131
d->background->setCacheAllRenderedFrames(true);
132
d->background->setElementPrefix("normal");
133
d->lineEditBackground = new FrameSvg(this);
134
d->lineEditBackground->setImagePath("widgets/lineedit");
135
d->lineEditBackground->setCacheAllRenderedFrames(true);
138
setAcceptHoverEvents(true);
140
d->styleParent = new Plasma::ComboBox();
141
d->style = d->styleParent->nativeWidget()->style();
143
setNativeWidget(new KComboBox);
145
d->animation = new QPropertyAnimation(this, "animationUpdate", this);
146
d->animation->setStartValue(0);
147
d->animation->setEndValue(1);
149
connect(Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders()));
151
d->displayProgress = false;
152
d->progressValue = 0;
155
BrowserHistoryComboBox::~BrowserHistoryComboBox()
157
delete d->styleParent;
161
QString BrowserHistoryComboBox::text() const
163
return static_cast<KComboBox*>(widget())->currentText();
166
void BrowserHistoryComboBox::setStyleSheet(const QString &stylesheet)
168
widget()->setStyleSheet(stylesheet);
171
QString BrowserHistoryComboBox::styleSheet()
173
return widget()->styleSheet();
176
void BrowserHistoryComboBox::setNativeWidget(KComboBox *nativeWidget)
179
widget()->deleteLater();
182
connect(nativeWidget, SIGNAL(activated(const QString &)), this, SIGNAL(activated(const QString &)));
183
connect(nativeWidget, SIGNAL(currentIndexChanged(const QString &)),
184
this, SIGNAL(textChanged(const QString &)));
186
setWidget(nativeWidget);
188
nativeWidget->setAttribute(Qt::WA_NoSystemBackground);
189
nativeWidget->setStyle(d->style);
194
KComboBox *BrowserHistoryComboBox::nativeWidget() const
196
return static_cast<KComboBox*>(widget());
199
void BrowserHistoryComboBox::addItem(const QString &text)
201
static_cast<KComboBox*>(widget())->addItem(text);
204
void BrowserHistoryComboBox::clear()
206
static_cast<KComboBox*>(widget())->clear();
209
void BrowserHistoryComboBox::resizeEvent(QGraphicsSceneResizeEvent *event)
212
//resize needed panels
215
d->background->setElementPrefix("focus");
216
d->background->resizeFrame(size());
218
d->background->setElementPrefix("active");
219
d->background->resizeFrame(d->activeRect.size());
221
d->background->setElementPrefix("normal");
222
d->background->resizeFrame(size());
225
QGraphicsProxyWidget::resizeEvent(event);
228
void BrowserHistoryComboBox::paint(QPainter *painter,
229
const QStyleOptionGraphicsItem *option,
232
QAbstractAnimation::State animState = d->animation->state();
234
if (!styleSheet().isNull() ||
235
Theme::defaultTheme()->useNativeWidgetStyle()) {
236
QGraphicsProxyWidget::paint(painter, option, widget);
240
if (nativeWidget()->isEditable()) {
241
if (animState != QAbstractAnimation::Stopped || hasFocus() || d->underMouse) {
243
d->lineEditBackground->setElementPrefix("focus");
245
d->lineEditBackground->setElementPrefix("hover");
247
qreal left, top, right, bottom;
248
d->lineEditBackground->getMargins(left, top, right, bottom);
249
d->lineEditBackground->resizeFrame(size()+QSizeF(left+right, top+bottom));
250
if (qFuzzyCompare(d->opacity, (qreal)1.0)) {
251
d->lineEditBackground->paintFrame(painter, QPoint(-left, -top));
253
QPixmap bufferPixmap = d->lineEditBackground->framePixmap();
254
QPainter buffPainter(&bufferPixmap);
255
buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
256
buffPainter.fillRect(bufferPixmap.rect(), QColor(0, 0, 0, 256*d->opacity));
258
painter->drawPixmap(bufferPixmap.rect().translated(QPoint(-left, -top)), bufferPixmap, bufferPixmap.rect());
261
if (d->displayProgress){
262
painter->fillRect(QRectF(option->rect.x() + 2, option->rect.y() + 3, (int) (((qreal) (option->rect.width() - 4) / 100) * d->progressValue), option->rect.height() - 4), Theme::defaultTheme()->color(Theme::LinkColor));
264
QGraphicsProxyWidget::paint(painter, option, widget);
268
QPixmap bufferPixmap;
272
d->background->setElementPrefix("normal");
274
if (animState == QAbstractAnimation::Stopped) {
275
d->background->paintFrame(painter);
279
bufferPixmap = QPixmap(rect().size().toSize());
280
bufferPixmap.fill(Qt::transparent);
282
QPainter buffPainter(&bufferPixmap);
283
d->background->paintFrame(&buffPainter);
284
buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
285
buffPainter.fillRect(bufferPixmap.rect(), QColor(0, 0, 0, 128));
287
painter->drawPixmap(0, 0, bufferPixmap);
290
//if is under mouse draw the animated glow overlay
291
if (isEnabled() && acceptHoverEvents()) {
292
if (animState!= QAbstractAnimation::Stopped) {
293
d->background->setElementPrefix("normal");
294
QPixmap normalPix = d->background->framePixmap();
295
d->background->setElementPrefix("active");
297
d->activeRect.topLeft(),
298
PaintUtils::transition(d->background->framePixmap(), normalPix, 1 - d->opacity));
299
} else if (isUnderMouse()) {
300
d->background->setElementPrefix("active");
301
d->background->paintFrame(painter, d->activeRect.topLeft());
305
if (nativeWidget()->hasFocus()) {
306
d->background->setElementPrefix("focus");
307
d->background->paintFrame(painter);
310
painter->setPen(Theme::defaultTheme()->color(Theme::ButtonTextColor));
312
QStyleOptionComboBox comboOpt;
314
comboOpt.initFrom(nativeWidget());
316
comboOpt.palette.setColor(
317
QPalette::ButtonText, Theme::defaultTheme()->color(Theme::ButtonTextColor));
318
comboOpt.currentIcon = nativeWidget()->itemIcon(
319
nativeWidget()->currentIndex());
320
comboOpt.currentText = nativeWidget()->itemText(
321
nativeWidget()->currentIndex());
322
comboOpt.editable = false;
324
nativeWidget()->style()->drawControl(
325
QStyle::CE_ComboBoxLabel, &comboOpt, painter, nativeWidget());
326
comboOpt.rect = nativeWidget()->style()->subControlRect(
327
QStyle::CC_ComboBox, &comboOpt, QStyle::SC_ComboBoxArrow, nativeWidget());
328
nativeWidget()->style()->drawPrimitive(
329
QStyle::PE_IndicatorArrowDown, &comboOpt, painter, nativeWidget());
332
void BrowserHistoryComboBox::setAnimationUpdate(qreal progress)
334
d->animationUpdate(progress);
337
qreal BrowserHistoryComboBox::animationUpdate() const
342
void BrowserHistoryComboBox::focusInEvent(QFocusEvent *event)
344
if (nativeWidget()->isEditable() && !d->underMouse) {
345
const int FadeInDuration = 75;
347
if (d->animation->state() != QAbstractAnimation::Stopped) {
348
d->animation->stop();
350
d->animation->setDuration(FadeInDuration);
351
d->animation->setDirection(QAbstractAnimation::Forward);
352
d->animation->start();
355
QGraphicsProxyWidget::focusInEvent(event);
358
void BrowserHistoryComboBox::focusOutEvent(QFocusEvent *event)
360
if (nativeWidget()->isEditable() && !d->underMouse) {
361
const int FadeOutDuration = 150;
363
if (d->animation->state() != QAbstractAnimation::Stopped) {
364
d->animation->stop();
366
d->animation->setDuration(FadeOutDuration);
367
d->animation->setDirection(QAbstractAnimation::Backward);
368
d->animation->start();
372
QGraphicsProxyWidget::focusInEvent(event);
375
void BrowserHistoryComboBox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
377
d->underMouse = true;
378
if (nativeWidget()->isEditable() && hasFocus()) {
381
const int FadeInDuration = 75;
383
if (d->animation->state() != QAbstractAnimation::Stopped) {
384
d->animation->stop();
386
d->animation->setDuration(FadeInDuration);
387
d->animation->setDirection(QAbstractAnimation::Forward);
388
d->animation->start();
390
d->background->setElementPrefix("active");
392
QGraphicsProxyWidget::hoverEnterEvent(event);
395
void BrowserHistoryComboBox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
397
d->underMouse = false;
398
if (nativeWidget()->isEditable() && hasFocus()) {
402
const int FadeOutDuration = 150;
404
if (d->animation->state() != QAbstractAnimation::Stopped) {
405
d->animation->stop();
407
d->animation->setDuration(FadeOutDuration);
408
d->animation->setDirection(QAbstractAnimation::Backward);
409
d->animation->start();
411
d->background->setElementPrefix("active");
413
QGraphicsProxyWidget::hoverLeaveEvent(event);
416
void BrowserHistoryComboBox::changeEvent(QEvent *event)
418
if (event->type() == QEvent::FontChange) {
419
d->customFont = true;
420
nativeWidget()->setFont(font());
423
QGraphicsProxyWidget::changeEvent(event);
426
void BrowserHistoryComboBox::setProgressValue(int value)
428
d->progressValue = value;
432
void BrowserHistoryComboBox::setDisplayProgress(bool enable)
434
d->displayProgress = enable;
438
} // namespace Plasma
440
#include "browserhistorycombobox.moc"