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 "slider.h"
|
|
|
|
|
2008-11-29 01:03:08 +01:00
|
|
|
#include <QApplication>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QPainter>
|
2008-11-29 01:03:08 +01:00
|
|
|
#include <QSlider>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QStyleOptionSlider>
|
2008-11-29 01:03:08 +01:00
|
|
|
#include <QGraphicsSceneWheelEvent>
|
2008-11-04 03:55:37 +01:00
|
|
|
#include <kmimetype.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "theme.h"
|
|
|
|
#include "framesvg.h"
|
|
|
|
|
2009-05-01 19:10:14 +02:00
|
|
|
#include "private/style_p.h"
|
2009-11-12 20:31:39 +01:00
|
|
|
#include "private/focusindicator_p.h"
|
2009-05-01 19:10:14 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class SliderPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SliderPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~SliderPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::FrameSvg *background;
|
2009-05-01 19:10:14 +02:00
|
|
|
Plasma::Style::Ptr style;
|
2009-11-12 20:31:39 +01:00
|
|
|
FocusIndicator *focusIndicator;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Slider::Slider(QGraphicsWidget *parent)
|
|
|
|
: QGraphicsProxyWidget(parent),
|
|
|
|
d(new SliderPrivate)
|
|
|
|
{
|
|
|
|
QSlider *native = new QSlider;
|
|
|
|
|
|
|
|
connect(native, SIGNAL(sliderMoved(int)), this, SIGNAL(sliderMoved(int)));
|
|
|
|
connect(native, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));
|
|
|
|
|
2009-11-12 20:31:39 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
setWidget(native);
|
2010-07-13 23:32:07 +02:00
|
|
|
native->setWindowIcon(QIcon());
|
2008-11-04 00:08:39 +01:00
|
|
|
native->setAttribute(Qt::WA_NoSystemBackground);
|
|
|
|
|
|
|
|
d->background = new Plasma::FrameSvg(this);
|
2009-05-01 19:44:31 +02:00
|
|
|
d->background->setImagePath("widgets/slider");
|
2010-09-22 11:02:09 +02:00
|
|
|
d->focusIndicator = new FocusIndicator(this, d->background);
|
2009-05-01 19:10:14 +02:00
|
|
|
|
|
|
|
d->style = Plasma::Style::sharedStyle();
|
|
|
|
native->setStyle(d->style.data());
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Slider::~Slider()
|
|
|
|
{
|
|
|
|
delete d;
|
2009-05-01 19:10:14 +02:00
|
|
|
Plasma::Style::doneWithSharedStyle();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::paint(QPainter *painter,
|
2009-02-24 23:42:06 +01:00
|
|
|
const QStyleOptionGraphicsItem *option,
|
|
|
|
QWidget *widget)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSlider *slider = nativeWidget();
|
|
|
|
QStyle *style = slider->style();
|
|
|
|
QStyleOptionSlider sliderOpt;
|
|
|
|
sliderOpt.initFrom(slider);
|
|
|
|
|
|
|
|
//init the other stuff in the slider, taken from initStyleOption()
|
|
|
|
sliderOpt.subControls = QStyle::SC_None;
|
|
|
|
sliderOpt.activeSubControls = QStyle::SC_None;
|
|
|
|
sliderOpt.orientation = slider->orientation();
|
|
|
|
sliderOpt.maximum = slider->maximum();
|
|
|
|
sliderOpt.minimum = slider->minimum();
|
|
|
|
sliderOpt.tickPosition = (QSlider::TickPosition)slider->tickPosition();
|
|
|
|
sliderOpt.tickInterval = slider->tickInterval();
|
|
|
|
sliderOpt.upsideDown = (slider->orientation() == Qt::Horizontal) ?
|
|
|
|
(slider->invertedAppearance() != (sliderOpt.direction == Qt::RightToLeft))
|
|
|
|
: (!slider->invertedAppearance());
|
|
|
|
sliderOpt.direction = Qt::LeftToRight; // we use the upsideDown option instead
|
|
|
|
sliderOpt.sliderPosition = slider->sliderPosition();
|
|
|
|
sliderOpt.sliderValue = slider->value();
|
|
|
|
sliderOpt.singleStep = slider->singleStep();
|
|
|
|
sliderOpt.pageStep = slider->pageStep();
|
|
|
|
if (slider->orientation() == Qt::Horizontal) {
|
|
|
|
sliderOpt.state |= QStyle::State_Horizontal;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect backgroundRect =
|
|
|
|
style->subControlRect(QStyle::CC_Slider, &sliderOpt, QStyle::SC_SliderGroove, slider);
|
2009-05-01 19:44:31 +02:00
|
|
|
|
|
|
|
if (sliderOpt.orientation == Qt::Horizontal &&
|
|
|
|
d->background->hasElement("horizontal-background-center")) {
|
|
|
|
d->background->setElementPrefix("horizontal-background");
|
|
|
|
d->background->resizeFrame(backgroundRect.size());
|
|
|
|
d->background->paintFrame(painter, backgroundRect.topLeft());
|
|
|
|
} else if (sliderOpt.orientation == Qt::Vertical &&
|
|
|
|
d->background->hasElement("vertical-background-center")) {
|
|
|
|
d->background->setElementPrefix("vertical-background");
|
|
|
|
d->background->resizeFrame(backgroundRect.size());
|
|
|
|
d->background->paintFrame(painter, backgroundRect.topLeft());
|
|
|
|
} else if (sliderOpt.orientation == Qt::Horizontal) {
|
|
|
|
QRect elementRect = d->background->elementRect("horizontal-slider-line").toRect();
|
|
|
|
elementRect.setWidth(sliderOpt.rect.width());
|
|
|
|
elementRect.moveCenter(sliderOpt.rect.center());
|
|
|
|
d->background->paint(painter, elementRect, "horizontal-slider-line");
|
|
|
|
} else {
|
|
|
|
QRect elementRect = d->background->elementRect("vertical-slider-line").toRect();
|
|
|
|
elementRect.setHeight(sliderOpt.rect.height());
|
|
|
|
elementRect.moveCenter(sliderOpt.rect.center());
|
|
|
|
d->background->paint(painter, elementRect, "vertical-slider-line");
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-02-24 23:42:06 +01:00
|
|
|
//Tickmarks
|
2008-11-04 00:08:39 +01:00
|
|
|
if (sliderOpt.tickPosition != QSlider::NoTicks) {
|
|
|
|
sliderOpt.subControls = QStyle::SC_SliderTickmarks;
|
|
|
|
sliderOpt.palette.setColor(
|
|
|
|
QPalette::WindowText, Plasma::Theme::defaultTheme()->color(Theme::TextColor));
|
|
|
|
style->drawComplexControl(QStyle::CC_Slider, &sliderOpt, painter, slider);
|
|
|
|
}
|
|
|
|
|
2010-06-04 19:20:57 +02:00
|
|
|
QRect handleRect = style->subControlRect(QStyle::CC_Slider, &sliderOpt, QStyle::SC_SliderHandle, slider);
|
2009-05-01 19:44:31 +02:00
|
|
|
|
|
|
|
QString handle;
|
|
|
|
if (sliderOpt.orientation == Qt::Horizontal) {
|
|
|
|
handle = "horizontal-slider-handle";
|
|
|
|
} else {
|
|
|
|
handle = "vertical-slider-handle";
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect elementRect = d->background->elementRect(handle).toRect();
|
|
|
|
elementRect.moveCenter(handleRect.center());
|
2010-06-04 19:20:57 +02:00
|
|
|
if (elementRect.right() > rect().right()) {
|
|
|
|
elementRect.moveRight(rect().right());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elementRect.left() < rect().left()) {
|
|
|
|
elementRect.moveLeft(rect().left());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elementRect.top() < rect().top()) {
|
|
|
|
elementRect.moveTop(rect().top());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (elementRect.bottom() > rect().bottom()) {
|
|
|
|
elementRect.moveBottom(rect().bottom());
|
|
|
|
}
|
|
|
|
|
2009-11-27 14:42:00 +01:00
|
|
|
if (orientation() == Qt::Vertical) {
|
2010-09-22 11:02:09 +02:00
|
|
|
d->focusIndicator->setCustomPrefix("vertical-slider-");
|
2009-11-27 14:42:00 +01:00
|
|
|
} else {
|
2010-09-22 11:02:09 +02:00
|
|
|
d->focusIndicator->setCustomPrefix("horizontal-slider-");
|
2009-11-27 14:42:00 +01:00
|
|
|
}
|
2009-11-12 20:31:39 +01:00
|
|
|
d->focusIndicator->setCustomGeometry(elementRect);
|
2009-05-01 19:44:31 +02:00
|
|
|
d->background->paint(painter, elementRect, handle);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2008-11-29 01:03:08 +01:00
|
|
|
void Slider::wheelEvent(QGraphicsSceneWheelEvent *event)
|
|
|
|
{
|
|
|
|
QWheelEvent e(event->pos().toPoint(), event->delta(),event->buttons(),event->modifiers(),event->orientation());
|
|
|
|
QApplication::sendEvent(widget(), &e);
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void Slider::setMaximum(int max)
|
|
|
|
{
|
|
|
|
static_cast<QSlider*>(widget())->setMaximum(max);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Slider::maximum() const
|
|
|
|
{
|
|
|
|
return static_cast<QSlider*>(widget())->maximum();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setMinimum(int min)
|
|
|
|
{
|
|
|
|
static_cast<QSlider*>(widget())->setMinimum(min);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Slider::minimum() const
|
|
|
|
{
|
|
|
|
return static_cast<QSlider*>(widget())->minimum();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setRange(int min, int max)
|
|
|
|
{
|
|
|
|
static_cast<QSlider*>(widget())->setRange(min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setValue(int value)
|
|
|
|
{
|
|
|
|
static_cast<QSlider*>(widget())->setValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Slider::value() const
|
|
|
|
{
|
|
|
|
return static_cast<QSlider*>(widget())->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setOrientation(Qt::Orientation orientation)
|
|
|
|
{
|
|
|
|
static_cast<QSlider*>(widget())->setOrientation(orientation);
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Orientation Slider::orientation() const
|
|
|
|
{
|
|
|
|
return static_cast<QSlider*>(widget())->orientation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Slider::setStyleSheet(const QString &stylesheet)
|
|
|
|
{
|
|
|
|
widget()->setStyleSheet(stylesheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Slider::styleSheet()
|
|
|
|
{
|
|
|
|
return widget()->styleSheet();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSlider *Slider::nativeWidget() const
|
|
|
|
{
|
|
|
|
return static_cast<QSlider*>(widget());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include <slider.moc>
|
|
|
|
|