Plasma
slider.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "slider.h"
00021
00022 #include <QApplication>
00023 #include <QPainter>
00024 #include <QSlider>
00025 #include <QStyleOptionSlider>
00026 #include <QGraphicsSceneWheelEvent>
00027 #include <kmimetype.h>
00028
00029 #include "theme.h"
00030 #include "framesvg.h"
00031
00032 #include "private/style_p.h"
00033
00034 namespace Plasma
00035 {
00036
00037 class SliderPrivate
00038 {
00039 public:
00040 SliderPrivate()
00041 {
00042 }
00043
00044 ~SliderPrivate()
00045 {
00046 }
00047
00048 Plasma::FrameSvg *background;
00049 Plasma::Style::Ptr style;
00050 };
00051
00052 Slider::Slider(QGraphicsWidget *parent)
00053 : QGraphicsProxyWidget(parent),
00054 d(new SliderPrivate)
00055 {
00056 QSlider *native = new QSlider;
00057
00058 connect(native, SIGNAL(sliderMoved(int)), this, SIGNAL(sliderMoved(int)));
00059 connect(native, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));
00060
00061 setWidget(native);
00062 native->setAttribute(Qt::WA_NoSystemBackground);
00063
00064 d->background = new Plasma::FrameSvg(this);
00065 d->background->setImagePath("widgets/slider");
00066
00067 d->style = Plasma::Style::sharedStyle();
00068 native->setStyle(d->style.data());
00069 }
00070
00071 Slider::~Slider()
00072 {
00073 delete d;
00074 Plasma::Style::doneWithSharedStyle();
00075 }
00076
00077 void Slider::paint(QPainter *painter,
00078 const QStyleOptionGraphicsItem *option,
00079 QWidget *widget)
00080 {
00081 if (!styleSheet().isNull() || Theme::defaultTheme()->useNativeWidgetStyle()) {
00082 QGraphicsProxyWidget::paint(painter, option, widget);
00083 return;
00084 }
00085
00086 QSlider *slider = nativeWidget();
00087 QStyle *style = slider->style();
00088 QStyleOptionSlider sliderOpt;
00089 sliderOpt.initFrom(slider);
00090
00091
00092 sliderOpt.subControls = QStyle::SC_None;
00093 sliderOpt.activeSubControls = QStyle::SC_None;
00094 sliderOpt.orientation = slider->orientation();
00095 sliderOpt.maximum = slider->maximum();
00096 sliderOpt.minimum = slider->minimum();
00097 sliderOpt.tickPosition = (QSlider::TickPosition)slider->tickPosition();
00098 sliderOpt.tickInterval = slider->tickInterval();
00099 sliderOpt.upsideDown = (slider->orientation() == Qt::Horizontal) ?
00100 (slider->invertedAppearance() != (sliderOpt.direction == Qt::RightToLeft))
00101 : (!slider->invertedAppearance());
00102 sliderOpt.direction = Qt::LeftToRight;
00103 sliderOpt.sliderPosition = slider->sliderPosition();
00104 sliderOpt.sliderValue = slider->value();
00105 sliderOpt.singleStep = slider->singleStep();
00106 sliderOpt.pageStep = slider->pageStep();
00107 if (slider->orientation() == Qt::Horizontal) {
00108 sliderOpt.state |= QStyle::State_Horizontal;
00109 }
00110
00111 QRect backgroundRect =
00112 style->subControlRect(QStyle::CC_Slider, &sliderOpt, QStyle::SC_SliderGroove, slider);
00113
00114 if (sliderOpt.orientation == Qt::Horizontal &&
00115 d->background->hasElement("horizontal-background-center")) {
00116 d->background->setElementPrefix("horizontal-background");
00117 d->background->resizeFrame(backgroundRect.size());
00118 d->background->paintFrame(painter, backgroundRect.topLeft());
00119 } else if (sliderOpt.orientation == Qt::Vertical &&
00120 d->background->hasElement("vertical-background-center")) {
00121 d->background->setElementPrefix("vertical-background");
00122 d->background->resizeFrame(backgroundRect.size());
00123 d->background->paintFrame(painter, backgroundRect.topLeft());
00124 } else if (sliderOpt.orientation == Qt::Horizontal) {
00125 QRect elementRect = d->background->elementRect("horizontal-slider-line").toRect();
00126 elementRect.setWidth(sliderOpt.rect.width());
00127 elementRect.moveCenter(sliderOpt.rect.center());
00128 d->background->paint(painter, elementRect, "horizontal-slider-line");
00129 } else {
00130 QRect elementRect = d->background->elementRect("vertical-slider-line").toRect();
00131 elementRect.setHeight(sliderOpt.rect.height());
00132 elementRect.moveCenter(sliderOpt.rect.center());
00133 d->background->paint(painter, elementRect, "vertical-slider-line");
00134 }
00135
00136
00137 if (sliderOpt.tickPosition != QSlider::NoTicks) {
00138 sliderOpt.subControls = QStyle::SC_SliderTickmarks;
00139 sliderOpt.palette.setColor(
00140 QPalette::WindowText, Plasma::Theme::defaultTheme()->color(Theme::TextColor));
00141 style->drawComplexControl(QStyle::CC_Slider, &sliderOpt, painter, slider);
00142 }
00143
00144 QRect handleRect =
00145 style->subControlRect(QStyle::CC_Slider, &sliderOpt, QStyle::SC_SliderHandle, slider);
00146
00147 QString handle;
00148 if (sliderOpt.orientation == Qt::Horizontal) {
00149 handle = "horizontal-slider-handle";
00150 } else {
00151 handle = "vertical-slider-handle";
00152 }
00153
00154 QRect elementRect = d->background->elementRect(handle).toRect();
00155 elementRect.moveCenter(handleRect.center());
00156 d->background->paint(painter, elementRect, handle);
00157
00158 }
00159
00160 void Slider::wheelEvent(QGraphicsSceneWheelEvent *event)
00161 {
00162 QWheelEvent e(event->pos().toPoint(), event->delta(),event->buttons(),event->modifiers(),event->orientation());
00163 QApplication::sendEvent(widget(), &e);
00164 event->accept();
00165 }
00166
00167 void Slider::setMaximum(int max)
00168 {
00169 static_cast<QSlider*>(widget())->setMaximum(max);
00170 }
00171
00172 int Slider::maximum() const
00173 {
00174 return static_cast<QSlider*>(widget())->maximum();
00175 }
00176
00177 void Slider::setMinimum(int min)
00178 {
00179 static_cast<QSlider*>(widget())->setMinimum(min);
00180 }
00181
00182 int Slider::minimum() const
00183 {
00184 return static_cast<QSlider*>(widget())->minimum();
00185 }
00186
00187 void Slider::setRange(int min, int max)
00188 {
00189 static_cast<QSlider*>(widget())->setRange(min, max);
00190 }
00191
00192 void Slider::setValue(int value)
00193 {
00194 static_cast<QSlider*>(widget())->setValue(value);
00195 }
00196
00197 int Slider::value() const
00198 {
00199 return static_cast<QSlider*>(widget())->value();
00200 }
00201
00202 void Slider::setOrientation(Qt::Orientation orientation)
00203 {
00204 static_cast<QSlider*>(widget())->setOrientation(orientation);
00205 }
00206
00207 Qt::Orientation Slider::orientation() const
00208 {
00209 return static_cast<QSlider*>(widget())->orientation();
00210 }
00211
00212 void Slider::setStyleSheet(const QString &stylesheet)
00213 {
00214 widget()->setStyleSheet(stylesheet);
00215 }
00216
00217 QString Slider::styleSheet()
00218 {
00219 return widget()->styleSheet();
00220 }
00221
00222 QSlider *Slider::nativeWidget() const
00223 {
00224 return static_cast<QSlider*>(widget());
00225 }
00226
00227 }
00228
00229 #include <slider.moc>
00230