2008-11-04 00:08:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2008 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "combobox.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QApplication>
|
|
|
|
|
2008-11-04 03:55:37 +01:00
|
|
|
#include <kcombobox.h>
|
|
|
|
#include <kmimetype.h>
|
|
|
|
#include <kiconeffect.h>
|
|
|
|
#include <kiconloader.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-17 19:00:33 +01:00
|
|
|
#include <plasma/private/style_p.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include "theme.h"
|
|
|
|
#include "framesvg.h"
|
|
|
|
#include "animator.h"
|
|
|
|
#include "paintutils.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class ComboBoxPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ComboBoxPrivate(ComboBox *comboBox)
|
|
|
|
: q(comboBox),
|
2009-09-13 15:13:55 +02:00
|
|
|
background(0),
|
2009-09-23 19:25:53 +02:00
|
|
|
customFont(0),
|
|
|
|
underMouse(false)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ComboBoxPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void syncActiveRect();
|
|
|
|
void syncBorders();
|
|
|
|
void animationUpdate(qreal progress);
|
2009-09-23 19:25:53 +02:00
|
|
|
void animationFinished(int id);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
ComboBox *q;
|
|
|
|
|
|
|
|
FrameSvg *background;
|
2009-09-23 19:25:53 +02:00
|
|
|
FrameSvg *lineEditBackground;
|
2008-11-04 00:08:39 +01:00
|
|
|
int animId;
|
|
|
|
bool fadeIn;
|
|
|
|
qreal opacity;
|
|
|
|
QRectF activeRect;
|
2009-02-24 23:42:06 +01:00
|
|
|
Style::Ptr style;
|
2009-09-13 15:13:55 +02:00
|
|
|
bool customFont;
|
2009-09-23 19:25:53 +02:00
|
|
|
bool underMouse;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void ComboBoxPrivate::syncActiveRect()
|
|
|
|
{
|
|
|
|
background->setElementPrefix("normal");
|
|
|
|
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
background->getMargins(left, top, right, bottom);
|
|
|
|
|
|
|
|
background->setElementPrefix("active");
|
|
|
|
qreal activeLeft, activeTop, activeRight, activeBottom;
|
|
|
|
background->getMargins(activeLeft, activeTop, activeRight, activeBottom);
|
|
|
|
|
|
|
|
activeRect = QRectF(QPointF(0, 0), q->size());
|
|
|
|
activeRect.adjust(left - activeLeft, top - activeTop,
|
|
|
|
-(right - activeRight), -(bottom - activeBottom));
|
|
|
|
|
|
|
|
background->setElementPrefix("normal");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComboBoxPrivate::syncBorders()
|
|
|
|
{
|
|
|
|
//set margins from the normal element
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
|
|
|
|
background->setElementPrefix("normal");
|
|
|
|
background->getMargins(left, top, right, bottom);
|
|
|
|
q->setContentsMargins(left, top, right, bottom);
|
|
|
|
|
|
|
|
//calc the rect for the over effect
|
|
|
|
syncActiveRect();
|
2009-01-17 19:00:33 +01:00
|
|
|
|
|
|
|
KComboBox *native = q->nativeWidget();
|
2009-09-13 15:13:55 +02:00
|
|
|
if (customFont) {
|
|
|
|
native->setFont(Theme::defaultTheme()->font(Theme::DefaultFont));
|
|
|
|
} else {
|
|
|
|
native->setFont(q->font());
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ComboBoxPrivate::animationUpdate(qreal progress)
|
|
|
|
{
|
|
|
|
if (progress == 1) {
|
|
|
|
animId = -1;
|
|
|
|
fadeIn = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
opacity = fadeIn ? progress : 1 - progress;
|
|
|
|
|
|
|
|
// explicit update
|
|
|
|
q->update();
|
|
|
|
}
|
|
|
|
|
2009-09-23 19:25:53 +02:00
|
|
|
|
|
|
|
void ComboBoxPrivate::animationFinished(int id)
|
|
|
|
{
|
|
|
|
if (id == animId) {
|
|
|
|
animId = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
ComboBox::ComboBox(QGraphicsWidget *parent)
|
|
|
|
: QGraphicsProxyWidget(parent),
|
|
|
|
d(new ComboBoxPrivate(this))
|
|
|
|
{
|
|
|
|
d->background = new FrameSvg(this);
|
|
|
|
d->background->setImagePath("widgets/button");
|
|
|
|
d->background->setCacheAllRenderedFrames(true);
|
|
|
|
d->background->setElementPrefix("normal");
|
2009-09-23 19:25:53 +02:00
|
|
|
d->lineEditBackground = new FrameSvg(this);
|
|
|
|
d->lineEditBackground->setImagePath("widgets/lineedit");
|
|
|
|
d->lineEditBackground->setCacheAllRenderedFrames(true);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
setAcceptHoverEvents(true);
|
2009-09-23 20:42:19 +02:00
|
|
|
|
|
|
|
d->style = Style::sharedStyle();
|
|
|
|
|
|
|
|
setNativeWidget(new KComboBox);
|
|
|
|
|
2009-02-24 23:42:06 +01:00
|
|
|
connect(Theme::defaultTheme(), SIGNAL(themeChanged()), SLOT(syncBorders()));
|
2009-09-23 19:25:53 +02:00
|
|
|
connect(Plasma::Animator::self(), SIGNAL(customAnimationFinished(int)), this, SLOT(animationFinished(int)));
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ComboBox::~ComboBox()
|
|
|
|
{
|
|
|
|
delete d;
|
2009-02-24 23:42:06 +01:00
|
|
|
Style::doneWithSharedStyle();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ComboBox::text() const
|
|
|
|
{
|
|
|
|
return static_cast<KComboBox*>(widget())->currentText();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComboBox::setStyleSheet(const QString &stylesheet)
|
|
|
|
{
|
|
|
|
widget()->setStyleSheet(stylesheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ComboBox::styleSheet()
|
|
|
|
{
|
|
|
|
return widget()->styleSheet();
|
|
|
|
}
|
|
|
|
|
2009-09-23 20:42:19 +02:00
|
|
|
void ComboBox::setNativeWidget(KComboBox *nativeWidget)
|
|
|
|
{
|
|
|
|
if (widget()) {
|
|
|
|
widget()->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(nativeWidget, SIGNAL(activated(const QString &)), this, SIGNAL(activated(const QString &)));
|
|
|
|
connect(nativeWidget, SIGNAL(currentIndexChanged(const QString &)),
|
|
|
|
this, SIGNAL(textChanged(const QString &)));
|
|
|
|
|
|
|
|
setWidget(nativeWidget);
|
|
|
|
|
|
|
|
nativeWidget->setAttribute(Qt::WA_NoSystemBackground);
|
|
|
|
nativeWidget->setStyle(d->style.data());
|
|
|
|
|
|
|
|
d->syncBorders();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
KComboBox *ComboBox::nativeWidget() const
|
|
|
|
{
|
|
|
|
return static_cast<KComboBox*>(widget());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComboBox::addItem(const QString &text)
|
|
|
|
{
|
|
|
|
static_cast<KComboBox*>(widget())->addItem(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComboBox::clear()
|
|
|
|
{
|
|
|
|
static_cast<KComboBox*>(widget())->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComboBox::resizeEvent(QGraphicsSceneResizeEvent *event)
|
|
|
|
{
|
|
|
|
if (d->background) {
|
|
|
|
//resize needed panels
|
|
|
|
d->syncActiveRect();
|
|
|
|
|
|
|
|
d->background->setElementPrefix("focus");
|
|
|
|
d->background->resizeFrame(size());
|
|
|
|
|
|
|
|
d->background->setElementPrefix("active");
|
|
|
|
d->background->resizeFrame(d->activeRect.size());
|
|
|
|
|
|
|
|
d->background->setElementPrefix("normal");
|
|
|
|
d->background->resizeFrame(size());
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsProxyWidget::resizeEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComboBox::paint(QPainter *painter,
|
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
QWidget *widget)
|
|
|
|
{
|
2009-02-24 23:42:06 +01:00
|
|
|
if (!styleSheet().isNull() ||
|
|
|
|
Theme::defaultTheme()->useNativeWidgetStyle()) {
|
2008-11-04 00:08:39 +01:00
|
|
|
QGraphicsProxyWidget::paint(painter, option, widget);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-23 19:25:53 +02:00
|
|
|
if (nativeWidget()->isEditable()) {
|
|
|
|
if (d->animId != -1 || hasFocus() || d->underMouse) {
|
|
|
|
if (hasFocus()) {
|
|
|
|
d->lineEditBackground->setElementPrefix("focus");
|
|
|
|
} else {
|
|
|
|
d->lineEditBackground->setElementPrefix("hover");
|
|
|
|
}
|
|
|
|
qreal left, top, right, bottom;
|
|
|
|
d->lineEditBackground->getMargins(left, top, right, bottom);
|
|
|
|
d->lineEditBackground->resizeFrame(size()+QSizeF(left+right, top+bottom));
|
|
|
|
if (qFuzzyCompare(d->opacity, (qreal)1.0)) {
|
|
|
|
d->lineEditBackground->paintFrame(painter, QPoint(-left, -top));
|
|
|
|
} else {
|
|
|
|
QPixmap bufferPixmap = d->lineEditBackground->framePixmap();
|
|
|
|
QPainter buffPainter(&bufferPixmap);
|
|
|
|
buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
|
|
|
buffPainter.fillRect(bufferPixmap.rect(), QColor(0, 0, 0, 256*d->opacity));
|
|
|
|
buffPainter.end();
|
|
|
|
painter->drawPixmap(bufferPixmap.rect().translated(QPoint(-left, -top)), bufferPixmap, bufferPixmap.rect());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QGraphicsProxyWidget::paint(painter, option, widget);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QPixmap bufferPixmap;
|
|
|
|
|
|
|
|
//normal button
|
|
|
|
if (isEnabled()) {
|
|
|
|
d->background->setElementPrefix("normal");
|
|
|
|
|
|
|
|
if (d->animId == -1) {
|
|
|
|
d->background->paintFrame(painter);
|
|
|
|
}
|
|
|
|
//disabled widget
|
|
|
|
} else {
|
|
|
|
bufferPixmap = QPixmap(rect().size().toSize());
|
|
|
|
bufferPixmap.fill(Qt::transparent);
|
|
|
|
|
|
|
|
QPainter buffPainter(&bufferPixmap);
|
|
|
|
d->background->paintFrame(&buffPainter);
|
|
|
|
buffPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
|
|
|
|
buffPainter.fillRect(bufferPixmap.rect(), QColor(0, 0, 0, 128));
|
|
|
|
|
|
|
|
painter->drawPixmap(0, 0, bufferPixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
//if is under mouse draw the animated glow overlay
|
|
|
|
if (isEnabled() && acceptHoverEvents()) {
|
|
|
|
if (d->animId != -1) {
|
|
|
|
d->background->setElementPrefix("normal");
|
|
|
|
QPixmap normalPix = d->background->framePixmap();
|
|
|
|
d->background->setElementPrefix("active");
|
|
|
|
painter->drawPixmap(
|
|
|
|
d->activeRect.topLeft(),
|
|
|
|
PaintUtils::transition(d->background->framePixmap(), normalPix, 1 - d->opacity));
|
|
|
|
} else if (isUnderMouse()) {
|
|
|
|
d->background->setElementPrefix("active");
|
|
|
|
d->background->paintFrame(painter, d->activeRect.topLeft());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nativeWidget()->hasFocus()) {
|
|
|
|
d->background->setElementPrefix("focus");
|
|
|
|
d->background->paintFrame(painter);
|
|
|
|
}
|
|
|
|
|
2009-02-24 23:42:06 +01:00
|
|
|
painter->setPen(Theme::defaultTheme()->color(Theme::ButtonTextColor));
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QStyleOptionComboBox comboOpt;
|
|
|
|
|
|
|
|
comboOpt.initFrom(nativeWidget());
|
|
|
|
|
|
|
|
comboOpt.palette.setColor(
|
2009-02-24 23:42:06 +01:00
|
|
|
QPalette::ButtonText, Theme::defaultTheme()->color(Theme::ButtonTextColor));
|
2008-11-04 00:08:39 +01:00
|
|
|
comboOpt.currentIcon = nativeWidget()->itemIcon(
|
|
|
|
nativeWidget()->currentIndex());
|
|
|
|
comboOpt.currentText = nativeWidget()->itemText(
|
|
|
|
nativeWidget()->currentIndex());
|
|
|
|
comboOpt.editable = false;
|
|
|
|
|
|
|
|
nativeWidget()->style()->drawControl(
|
|
|
|
QStyle::CE_ComboBoxLabel, &comboOpt, painter, nativeWidget());
|
|
|
|
comboOpt.rect = nativeWidget()->style()->subControlRect(
|
|
|
|
QStyle::CC_ComboBox, &comboOpt, QStyle::SC_ComboBoxArrow, nativeWidget());
|
|
|
|
nativeWidget()->style()->drawPrimitive(
|
|
|
|
QStyle::PE_IndicatorArrowDown, &comboOpt, painter, nativeWidget());
|
|
|
|
}
|
|
|
|
|
2009-09-23 19:25:53 +02:00
|
|
|
void ComboBox::focusInEvent(QFocusEvent *event)
|
|
|
|
{
|
|
|
|
if (nativeWidget()->isEditable() && !d->underMouse) {
|
|
|
|
const int FadeInDuration = 75;
|
|
|
|
|
|
|
|
if (d->animId != -1) {
|
|
|
|
Animator::self()->stopCustomAnimation(d->animId);
|
|
|
|
}
|
|
|
|
d->animId = Animator::self()->customAnimation(
|
|
|
|
40 / (1000 / FadeInDuration), FadeInDuration,
|
|
|
|
Animator::LinearCurve, this, "animationUpdate");
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsProxyWidget::focusInEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComboBox::focusOutEvent(QFocusEvent *event)
|
|
|
|
{
|
|
|
|
if (nativeWidget()->isEditable() && !d->underMouse) {
|
|
|
|
const int FadeOutDuration = 150;
|
|
|
|
|
|
|
|
if (d->animId != -1) {
|
|
|
|
Animator::self()->stopCustomAnimation(d->animId != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
d->fadeIn = false;
|
|
|
|
d->animId = Animator::self()->customAnimation(
|
|
|
|
40 / (1000 / FadeOutDuration),
|
|
|
|
FadeOutDuration, Animator::LinearCurve, this, "animationUpdate");
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsProxyWidget::focusInEvent(event);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void ComboBox::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2009-09-23 19:25:53 +02:00
|
|
|
d->underMouse = true;
|
|
|
|
if (nativeWidget()->isEditable() && hasFocus()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
const int FadeInDuration = 75;
|
|
|
|
|
|
|
|
if (d->animId != -1) {
|
2009-02-24 23:42:06 +01:00
|
|
|
Animator::self()->stopCustomAnimation(d->animId);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-02-24 23:42:06 +01:00
|
|
|
d->animId = Animator::self()->customAnimation(
|
2008-11-04 00:08:39 +01:00
|
|
|
40 / (1000 / FadeInDuration), FadeInDuration,
|
2009-02-24 23:42:06 +01:00
|
|
|
Animator::LinearCurve, this, "animationUpdate");
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
d->background->setElementPrefix("active");
|
|
|
|
|
|
|
|
QGraphicsProxyWidget::hoverEnterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComboBox::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2009-09-23 19:25:53 +02:00
|
|
|
d->underMouse = false;
|
|
|
|
if (nativeWidget()->isEditable() && hasFocus()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
const int FadeOutDuration = 150;
|
|
|
|
|
|
|
|
if (d->animId != -1) {
|
2009-02-24 23:42:06 +01:00
|
|
|
Animator::self()->stopCustomAnimation(d->animId != -1);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d->fadeIn = false;
|
2009-02-24 23:42:06 +01:00
|
|
|
d->animId = Animator::self()->customAnimation(
|
2008-11-04 00:08:39 +01:00
|
|
|
40 / (1000 / FadeOutDuration),
|
2009-02-24 23:42:06 +01:00
|
|
|
FadeOutDuration, Animator::LinearCurve, this, "animationUpdate");
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
d->background->setElementPrefix("active");
|
|
|
|
|
|
|
|
QGraphicsProxyWidget::hoverLeaveEvent(event);
|
|
|
|
}
|
|
|
|
|
2009-09-13 15:13:55 +02:00
|
|
|
void ComboBox::changeEvent(QEvent *event)
|
|
|
|
{
|
|
|
|
if (event->type() == QEvent::FontChange) {
|
|
|
|
d->customFont = true;
|
2009-09-13 21:18:29 +02:00
|
|
|
nativeWidget()->setFont(font());
|
2009-09-13 15:13:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsProxyWidget::changeEvent(event);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include <combobox.moc>
|
|
|
|
|