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 #include "private/focusindicator_p.h" 00034 00035 namespace Plasma 00036 { 00037 00038 class SliderPrivate 00039 { 00040 public: 00041 SliderPrivate() 00042 { 00043 } 00044 00045 ~SliderPrivate() 00046 { 00047 } 00048 00049 Plasma::FrameSvg *background; 00050 Plasma::Style::Ptr style; 00051 FocusIndicator *focusIndicator; 00052 }; 00053 00054 Slider::Slider(QGraphicsWidget *parent) 00055 : QGraphicsProxyWidget(parent), 00056 d(new SliderPrivate) 00057 { 00058 QSlider *native = new QSlider; 00059 00060 connect(native, SIGNAL(sliderMoved(int)), this, SIGNAL(sliderMoved(int))); 00061 connect(native, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int))); 00062 00063 00064 setWidget(native); 00065 native->setWindowIcon(QIcon()); 00066 native->setAttribute(Qt::WA_NoSystemBackground); 00067 00068 d->background = new Plasma::FrameSvg(this); 00069 d->background->setImagePath("widgets/slider"); 00070 d->focusIndicator = new FocusIndicator(this, d->background); 00071 00072 d->style = Plasma::Style::sharedStyle(); 00073 native->setStyle(d->style.data()); 00074 } 00075 00076 Slider::~Slider() 00077 { 00078 delete d; 00079 Plasma::Style::doneWithSharedStyle(); 00080 } 00081 00082 void Slider::paint(QPainter *painter, 00083 const QStyleOptionGraphicsItem *option, 00084 QWidget *widget) 00085 { 00086 if (!styleSheet().isNull() || Theme::defaultTheme()->useNativeWidgetStyle()) { 00087 QGraphicsProxyWidget::paint(painter, option, widget); 00088 return; 00089 } 00090 00091 QSlider *slider = nativeWidget(); 00092 QStyle *style = slider->style(); 00093 QStyleOptionSlider sliderOpt; 00094 sliderOpt.initFrom(slider); 00095 00096 //init the other stuff in the slider, taken from initStyleOption() 00097 sliderOpt.subControls = QStyle::SC_None; 00098 sliderOpt.activeSubControls = QStyle::SC_None; 00099 sliderOpt.orientation = slider->orientation(); 00100 sliderOpt.maximum = slider->maximum(); 00101 sliderOpt.minimum = slider->minimum(); 00102 sliderOpt.tickPosition = (QSlider::TickPosition)slider->tickPosition(); 00103 sliderOpt.tickInterval = slider->tickInterval(); 00104 sliderOpt.upsideDown = (slider->orientation() == Qt::Horizontal) ? 00105 (slider->invertedAppearance() != (sliderOpt.direction == Qt::RightToLeft)) 00106 : (!slider->invertedAppearance()); 00107 sliderOpt.direction = Qt::LeftToRight; // we use the upsideDown option instead 00108 sliderOpt.sliderPosition = slider->sliderPosition(); 00109 sliderOpt.sliderValue = slider->value(); 00110 sliderOpt.singleStep = slider->singleStep(); 00111 sliderOpt.pageStep = slider->pageStep(); 00112 if (slider->orientation() == Qt::Horizontal) { 00113 sliderOpt.state |= QStyle::State_Horizontal; 00114 } 00115 00116 QRect backgroundRect = 00117 style->subControlRect(QStyle::CC_Slider, &sliderOpt, QStyle::SC_SliderGroove, slider); 00118 00119 if (sliderOpt.orientation == Qt::Horizontal && 00120 d->background->hasElement("horizontal-background-center")) { 00121 d->background->setElementPrefix("horizontal-background"); 00122 d->background->resizeFrame(backgroundRect.size()); 00123 d->background->paintFrame(painter, backgroundRect.topLeft()); 00124 } else if (sliderOpt.orientation == Qt::Vertical && 00125 d->background->hasElement("vertical-background-center")) { 00126 d->background->setElementPrefix("vertical-background"); 00127 d->background->resizeFrame(backgroundRect.size()); 00128 d->background->paintFrame(painter, backgroundRect.topLeft()); 00129 } else if (sliderOpt.orientation == Qt::Horizontal) { 00130 QRect elementRect = d->background->elementRect("horizontal-slider-line").toRect(); 00131 elementRect.setWidth(sliderOpt.rect.width()); 00132 elementRect.moveCenter(sliderOpt.rect.center()); 00133 d->background->paint(painter, elementRect, "horizontal-slider-line"); 00134 } else { 00135 QRect elementRect = d->background->elementRect("vertical-slider-line").toRect(); 00136 elementRect.setHeight(sliderOpt.rect.height()); 00137 elementRect.moveCenter(sliderOpt.rect.center()); 00138 d->background->paint(painter, elementRect, "vertical-slider-line"); 00139 } 00140 00141 //Tickmarks 00142 if (sliderOpt.tickPosition != QSlider::NoTicks) { 00143 sliderOpt.subControls = QStyle::SC_SliderTickmarks; 00144 sliderOpt.palette.setColor( 00145 QPalette::WindowText, Plasma::Theme::defaultTheme()->color(Theme::TextColor)); 00146 style->drawComplexControl(QStyle::CC_Slider, &sliderOpt, painter, slider); 00147 } 00148 00149 QRect handleRect = style->subControlRect(QStyle::CC_Slider, &sliderOpt, QStyle::SC_SliderHandle, slider); 00150 00151 QString handle; 00152 if (sliderOpt.orientation == Qt::Horizontal) { 00153 handle = "horizontal-slider-handle"; 00154 } else { 00155 handle = "vertical-slider-handle"; 00156 } 00157 00158 QRect elementRect = d->background->elementRect(handle).toRect(); 00159 elementRect.moveCenter(handleRect.center()); 00160 if (elementRect.right() > rect().right()) { 00161 elementRect.moveRight(rect().right()); 00162 } 00163 00164 if (elementRect.left() < rect().left()) { 00165 elementRect.moveLeft(rect().left()); 00166 } 00167 00168 if (elementRect.top() < rect().top()) { 00169 elementRect.moveTop(rect().top()); 00170 } 00171 00172 if (elementRect.bottom() > rect().bottom()) { 00173 elementRect.moveBottom(rect().bottom()); 00174 } 00175 00176 if (orientation() == Qt::Vertical) { 00177 d->focusIndicator->setCustomPrefix("vertical-slider-"); 00178 } else { 00179 d->focusIndicator->setCustomPrefix("horizontal-slider-"); 00180 } 00181 d->focusIndicator->setCustomGeometry(elementRect); 00182 d->background->paint(painter, elementRect, handle); 00183 } 00184 00185 void Slider::wheelEvent(QGraphicsSceneWheelEvent *event) 00186 { 00187 QWheelEvent e(event->pos().toPoint(), event->delta(),event->buttons(),event->modifiers(),event->orientation()); 00188 QApplication::sendEvent(widget(), &e); 00189 event->accept(); 00190 } 00191 00192 void Slider::setMaximum(int max) 00193 { 00194 static_cast<QSlider*>(widget())->setMaximum(max); 00195 } 00196 00197 int Slider::maximum() const 00198 { 00199 return static_cast<QSlider*>(widget())->maximum(); 00200 } 00201 00202 void Slider::setMinimum(int min) 00203 { 00204 static_cast<QSlider*>(widget())->setMinimum(min); 00205 } 00206 00207 int Slider::minimum() const 00208 { 00209 return static_cast<QSlider*>(widget())->minimum(); 00210 } 00211 00212 void Slider::setRange(int min, int max) 00213 { 00214 static_cast<QSlider*>(widget())->setRange(min, max); 00215 } 00216 00217 void Slider::setValue(int value) 00218 { 00219 static_cast<QSlider*>(widget())->setValue(value); 00220 } 00221 00222 int Slider::value() const 00223 { 00224 return static_cast<QSlider*>(widget())->value(); 00225 } 00226 00227 void Slider::setOrientation(Qt::Orientation orientation) 00228 { 00229 static_cast<QSlider*>(widget())->setOrientation(orientation); 00230 } 00231 00232 Qt::Orientation Slider::orientation() const 00233 { 00234 return static_cast<QSlider*>(widget())->orientation(); 00235 } 00236 00237 void Slider::setStyleSheet(const QString &stylesheet) 00238 { 00239 widget()->setStyleSheet(stylesheet); 00240 } 00241 00242 QString Slider::styleSheet() 00243 { 00244 return widget()->styleSheet(); 00245 } 00246 00247 QSlider *Slider::nativeWidget() const 00248 { 00249 return static_cast<QSlider*>(widget()); 00250 } 00251 00252 } // namespace Plasma 00253 00254 #include <slider.moc> 00255
KDE 4.6 API Reference