00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "combobox.h"
00021
00022 #include <QPainter>
00023 #include <QApplication>
00024
00025 #include <kcombobox.h>
00026 #include <kmimetype.h>
00027 #include <kiconeffect.h>
00028 #include <kiconloader.h>
00029
00030 #include <plasma/private/style_p.h>
00031 #include "theme.h"
00032 #include "framesvg.h"
00033 #include "animator.h"
00034 #include "paintutils.h"
00035
00036 namespace Plasma
00037 {
00038
00039 class ComboBoxPrivate
00040 {
00041 public:
00042 ComboBoxPrivate(ComboBox *comboBox)
00043 : q(comboBox),
00044 background(0)
00045 {
00046 }
00047
00048 ~ComboBoxPrivate()
00049 {
00050 }
00051
00052 void syncActiveRect();
00053 void syncBorders();
00054 void animationUpdate(qreal progress);
00055
00056 ComboBox *q;
00057
00058 FrameSvg *background;
00059 int animId;
00060 bool fadeIn;
00061 qreal opacity;
00062 QRectF activeRect;
00063 Plasma::Style::Ptr style;
00064 };
00065
00066 void ComboBoxPrivate::syncActiveRect()
00067 {
00068 background->setElementPrefix("normal");
00069
00070 qreal left, top, right, bottom;
00071 background->getMargins(left, top, right, bottom);
00072
00073 background->setElementPrefix("active");
00074 qreal activeLeft, activeTop, activeRight, activeBottom;
00075 background->getMargins(activeLeft, activeTop, activeRight, activeBottom);
00076
00077 activeRect = QRectF(QPointF(0, 0), q->size());
00078 activeRect.adjust(left - activeLeft, top - activeTop,
00079 -(right - activeRight), -(bottom - activeBottom));
00080
00081 background->setElementPrefix("normal");
00082 }
00083
00084 void ComboBoxPrivate::syncBorders()
00085 {
00086
00087 qreal left, top, right, bottom;
00088
00089 background->setElementPrefix("normal");
00090 background->getMargins(left, top, right, bottom);
00091 q->setContentsMargins(left, top, right, bottom);
00092
00093
00094 syncActiveRect();
00095
00096 KComboBox *native = q->nativeWidget();
00097 QColor color = Theme::defaultTheme()->color(Theme::TextColor);
00098 QPalette p = native->palette();
00099
00100 p.setColor(QPalette::Normal, QPalette::Text, color);
00101 p.setColor(QPalette::Inactive, QPalette::Text, color);
00102 native->setPalette(p);
00103 native->setFont(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont));
00104 }
00105
00106 void ComboBoxPrivate::animationUpdate(qreal progress)
00107 {
00108 if (progress == 1) {
00109 animId = -1;
00110 fadeIn = true;
00111 }
00112
00113 opacity = fadeIn ? progress : 1 - progress;
00114
00115
00116 q->update();
00117 }
00118
00119 ComboBox::ComboBox(QGraphicsWidget *parent)
00120 : QGraphicsProxyWidget(parent),
00121 d(new ComboBoxPrivate(this))
00122 {
00123 KComboBox *native = new KComboBox;
00124 connect(native, SIGNAL(activated(const QString &)), this, SIGNAL(activated(const QString &)));
00125 setWidget(native);
00126 native->setAttribute(Qt::WA_NoSystemBackground);
00127
00128 d->background = new FrameSvg(this);
00129 d->background->setImagePath("widgets/button");
00130 d->background->setCacheAllRenderedFrames(true);
00131 d->background->setElementPrefix("normal");
00132
00133 d->syncBorders();
00134 setAcceptHoverEvents(true);
00135 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders()));
00136 d->style = Plasma::Style::sharedStyle();
00137 native->setStyle(d->style.data());
00138 }
00139
00140 ComboBox::~ComboBox()
00141 {
00142 delete d;
00143 Plasma::Style::doneWithSharedStyle();
00144 }
00145
00146 QString ComboBox::text() const
00147 {
00148 return static_cast<KComboBox*>(widget())->currentText();
00149 }
00150
00151 void ComboBox::setStyleSheet(const QString &stylesheet)
00152 {
00153 widget()->setStyleSheet(stylesheet);
00154 }
00155
00156 QString ComboBox::styleSheet()
00157 {
00158 return widget()->styleSheet();
00159 }
00160
00161 KComboBox *ComboBox::nativeWidget() const
00162 {
00163 return static_cast<KComboBox*>(widget());
00164 }
00165
00166 void ComboBox::addItem(const QString &text)
00167 {
00168 static_cast<KComboBox*>(widget())->addItem(text);
00169 }
00170
00171 void ComboBox::clear()
00172 {
00173 static_cast<KComboBox*>(widget())->clear();
00174 }
00175
00176 void ComboBox::resizeEvent(QGraphicsSceneResizeEvent *event)
00177 {
00178 if (d->background) {
00179
00180 d->syncActiveRect();
00181
00182 d->background->setElementPrefix("focus");
00183 d->background->resizeFrame(size());
00184
00185 d->background->setElementPrefix("active");
00186 d->background->resizeFrame(d->activeRect.size());
00187
00188 d->background->setElementPrefix("normal");
00189 d->background->resizeFrame(size());
00190 }
00191
00192 QGraphicsProxyWidget::resizeEvent(event);
00193 }
00194
00195 void ComboBox::paint(QPainter *painter,
00196 const QStyleOptionGraphicsItem *option,
00197 QWidget *widget)
00198 {
00199 if (!styleSheet().isNull() || nativeWidget()->isEditable()) {
00200 QGraphicsProxyWidget::paint(painter, option, widget);
00201 return;
00202 }
00203
00204 QPixmap bufferPixmap;
00205
00206
00207 if (isEnabled()) {
00208 d->background->setElementPrefix("normal");
00209
00210 if (d->animId == -1) {
00211 d->background->paintFrame(painter);
00212 }
00213
00214 } else {
00215 bufferPixmap = QPixmap(rect().size().toSize());
00216 bufferPixmap.fill(Qt::transparent);
00217
00218 QPainter buffPainter(&bufferPixmap);
00219 d->background->paintFrame(&buffPainter);
00220 buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
00221 buffPainter.fillRect(bufferPixmap.rect(), QColor(0, 0, 0, 128));
00222
00223 painter->drawPixmap(0, 0, bufferPixmap);
00224 }
00225
00226
00227 if (isEnabled() && acceptHoverEvents()) {
00228 if (d->animId != -1) {
00229 d->background->setElementPrefix("normal");
00230 QPixmap normalPix = d->background->framePixmap();
00231 d->background->setElementPrefix("active");
00232 painter->drawPixmap(
00233 d->activeRect.topLeft(),
00234 PaintUtils::transition(d->background->framePixmap(), normalPix, 1 - d->opacity));
00235 } else if (isUnderMouse()) {
00236 d->background->setElementPrefix("active");
00237 d->background->paintFrame(painter, d->activeRect.topLeft());
00238 }
00239 }
00240
00241 if (nativeWidget()->hasFocus()) {
00242 d->background->setElementPrefix("focus");
00243 d->background->paintFrame(painter);
00244 }
00245
00246 painter->setPen(Plasma::Theme::defaultTheme()->color(Theme::ButtonTextColor));
00247
00248 QStyleOptionComboBox comboOpt;
00249
00250 comboOpt.initFrom(nativeWidget());
00251
00252 comboOpt.palette.setColor(
00253 QPalette::ButtonText, Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor));
00254 comboOpt.currentIcon = nativeWidget()->itemIcon(
00255 nativeWidget()->currentIndex());
00256 comboOpt.currentText = nativeWidget()->itemText(
00257 nativeWidget()->currentIndex());
00258 comboOpt.editable = false;
00259
00260 nativeWidget()->style()->drawControl(
00261 QStyle::CE_ComboBoxLabel, &comboOpt, painter, nativeWidget());
00262 comboOpt.rect = nativeWidget()->style()->subControlRect(
00263 QStyle::CC_ComboBox, &comboOpt, QStyle::SC_ComboBoxArrow, nativeWidget());
00264 nativeWidget()->style()->drawPrimitive(
00265 QStyle::PE_IndicatorArrowDown, &comboOpt, painter, nativeWidget());
00266 }
00267
00268 void ComboBox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
00269 {
00270 const int FadeInDuration = 75;
00271
00272 if (d->animId != -1) {
00273 Plasma::Animator::self()->stopCustomAnimation(d->animId);
00274 }
00275 d->animId = Plasma::Animator::self()->customAnimation(
00276 40 / (1000 / FadeInDuration), FadeInDuration,
00277 Plasma::Animator::LinearCurve, this, "animationUpdate");
00278
00279 d->background->setElementPrefix("active");
00280
00281 QGraphicsProxyWidget::hoverEnterEvent(event);
00282 }
00283
00284 void ComboBox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
00285 {
00286 const int FadeOutDuration = 150;
00287
00288 if (d->animId != -1) {
00289 Plasma::Animator::self()->stopCustomAnimation(d->animId != -1);
00290 }
00291
00292 d->fadeIn = false;
00293 d->animId = Plasma::Animator::self()->customAnimation(
00294 40 / (1000 / FadeOutDuration),
00295 FadeOutDuration, Plasma::Animator::LinearCurve, this, "animationUpdate");
00296
00297 d->background->setElementPrefix("active");
00298
00299 QGraphicsProxyWidget::hoverLeaveEvent(event);
00300 }
00301
00302 }
00303
00304 #include <combobox.moc>
00305