• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Plasma

slider.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2008 Aaron Seigo <aseigo@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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     //init the other stuff in the slider, taken from initStyleOption()
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; // we use the upsideDown option instead
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     //Tickmarks
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 } // namespace Plasma
00228 
00229 #include <slider.moc>
00230 

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal