• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
  • widgets
slider.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2008 Aaron Seigo <aseigo@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "slider.h"
21
22#include <QApplication>
23#include <QPainter>
24#include <QSlider>
25#include <QStyleOptionSlider>
26#include <QGraphicsSceneWheelEvent>
27#include <kmimetype.h>
28
29#include "theme.h"
30#include "framesvg.h"
31
32#include "private/style_p.h"
33#include "private/focusindicator_p.h"
34
35namespace Plasma
36{
37
38class SliderPrivate
39{
40public:
41 SliderPrivate()
42 {
43 }
44
45 ~SliderPrivate()
46 {
47 }
48
49 Plasma::FrameSvg *background;
50 Plasma::Style::Ptr style;
51 FocusIndicator *focusIndicator;
52};
53
54Slider::Slider(QGraphicsWidget *parent)
55 : QGraphicsProxyWidget(parent),
56 d(new SliderPrivate)
57{
58 QSlider *native = new QSlider;
59
60 connect(native, SIGNAL(sliderMoved(int)), this, SIGNAL(sliderMoved(int)));
61 connect(native, SIGNAL(valueChanged(int)), this, SIGNAL(valueChanged(int)));
62
63
64 setWidget(native);
65 native->setWindowIcon(QIcon());
66 native->setAttribute(Qt::WA_NoSystemBackground);
67
68 d->background = new Plasma::FrameSvg(this);
69 d->background->setImagePath("widgets/slider");
70 d->focusIndicator = new FocusIndicator(this, d->background);
71
72 d->style = Plasma::Style::sharedStyle();
73 native->setStyle(d->style.data());
74}
75
76Slider::~Slider()
77{
78 delete d;
79 Plasma::Style::doneWithSharedStyle();
80}
81
82void Slider::paint(QPainter *painter,
83 const QStyleOptionGraphicsItem *option,
84 QWidget *widget)
85{
86 if (!styleSheet().isNull() || Theme::defaultTheme()->useNativeWidgetStyle()) {
87 QGraphicsProxyWidget::paint(painter, option, widget);
88 return;
89 }
90
91 QSlider *slider = nativeWidget();
92 QStyle *style = slider->style();
93 QStyleOptionSlider sliderOpt;
94 sliderOpt.initFrom(slider);
95
96 //init the other stuff in the slider, taken from initStyleOption()
97 sliderOpt.subControls = QStyle::SC_None;
98 sliderOpt.activeSubControls = QStyle::SC_None;
99 sliderOpt.orientation = slider->orientation();
100 sliderOpt.maximum = slider->maximum();
101 sliderOpt.minimum = slider->minimum();
102 sliderOpt.tickPosition = (QSlider::TickPosition)slider->tickPosition();
103 sliderOpt.tickInterval = slider->tickInterval();
104 sliderOpt.upsideDown = (slider->orientation() == Qt::Horizontal) ?
105 (slider->invertedAppearance() != (sliderOpt.direction == Qt::RightToLeft))
106 : (!slider->invertedAppearance());
107 sliderOpt.direction = Qt::LeftToRight; // we use the upsideDown option instead
108 sliderOpt.sliderPosition = slider->sliderPosition();
109 sliderOpt.sliderValue = slider->value();
110 sliderOpt.singleStep = slider->singleStep();
111 sliderOpt.pageStep = slider->pageStep();
112 if (slider->orientation() == Qt::Horizontal) {
113 sliderOpt.state |= QStyle::State_Horizontal;
114 }
115
116 QRect backgroundRect =
117 style->subControlRect(QStyle::CC_Slider, &sliderOpt, QStyle::SC_SliderGroove, slider);
118
119 if (sliderOpt.orientation == Qt::Horizontal &&
120 d->background->hasElement("horizontal-background-center")) {
121 d->background->setElementPrefix("horizontal-background");
122 d->background->resizeFrame(backgroundRect.size());
123 d->background->paintFrame(painter, backgroundRect.topLeft());
124 } else if (sliderOpt.orientation == Qt::Vertical &&
125 d->background->hasElement("vertical-background-center")) {
126 d->background->setElementPrefix("vertical-background");
127 d->background->resizeFrame(backgroundRect.size());
128 d->background->paintFrame(painter, backgroundRect.topLeft());
129 } else if (sliderOpt.orientation == Qt::Horizontal) {
130 QRect elementRect = d->background->elementRect("horizontal-slider-line").toRect();
131 elementRect.setWidth(sliderOpt.rect.width());
132 elementRect.moveCenter(sliderOpt.rect.center());
133 d->background->paint(painter, elementRect, "horizontal-slider-line");
134 } else {
135 QRect elementRect = d->background->elementRect("vertical-slider-line").toRect();
136 elementRect.setHeight(sliderOpt.rect.height());
137 elementRect.moveCenter(sliderOpt.rect.center());
138 d->background->paint(painter, elementRect, "vertical-slider-line");
139 }
140
141 //Tickmarks
142 if (sliderOpt.tickPosition != QSlider::NoTicks) {
143 sliderOpt.subControls = QStyle::SC_SliderTickmarks;
144 sliderOpt.palette.setColor(
145 QPalette::WindowText, Plasma::Theme::defaultTheme()->color(Theme::TextColor));
146 style->drawComplexControl(QStyle::CC_Slider, &sliderOpt, painter, slider);
147 }
148
149 QRect handleRect = style->subControlRect(QStyle::CC_Slider, &sliderOpt, QStyle::SC_SliderHandle, slider);
150
151 QString handle;
152 if (sliderOpt.orientation == Qt::Horizontal) {
153 handle = "horizontal-slider-handle";
154 } else {
155 handle = "vertical-slider-handle";
156 }
157
158 QRect elementRect = d->background->elementRect(handle).toRect();
159 elementRect.moveCenter(handleRect.center());
160 if (elementRect.right() > rect().right()) {
161 elementRect.moveRight(rect().right());
162 }
163
164 if (elementRect.left() < rect().left()) {
165 elementRect.moveLeft(rect().left());
166 }
167
168 if (elementRect.top() < rect().top()) {
169 elementRect.moveTop(rect().top());
170 }
171
172 if (elementRect.bottom() > rect().bottom()) {
173 elementRect.moveBottom(rect().bottom());
174 }
175
176 if (orientation() == Qt::Vertical) {
177 d->focusIndicator->setCustomPrefix("vertical-slider-");
178 } else {
179 d->focusIndicator->setCustomPrefix("horizontal-slider-");
180 }
181 d->focusIndicator->setCustomGeometry(elementRect);
182 d->background->paint(painter, elementRect, handle);
183}
184
185void Slider::wheelEvent(QGraphicsSceneWheelEvent *event)
186{
187 QWheelEvent e(event->pos().toPoint(), event->delta(),event->buttons(),event->modifiers(),event->orientation());
188 QApplication::sendEvent(widget(), &e);
189 event->accept();
190}
191
192void Slider::setMaximum(int max)
193{
194 static_cast<QSlider*>(widget())->setMaximum(max);
195}
196
197int Slider::maximum() const
198{
199 return static_cast<QSlider*>(widget())->maximum();
200}
201
202void Slider::setMinimum(int min)
203{
204 static_cast<QSlider*>(widget())->setMinimum(min);
205}
206
207int Slider::minimum() const
208{
209 return static_cast<QSlider*>(widget())->minimum();
210}
211
212void Slider::setRange(int min, int max)
213{
214 static_cast<QSlider*>(widget())->setRange(min, max);
215}
216
217void Slider::setValue(int value)
218{
219 static_cast<QSlider*>(widget())->setValue(value);
220}
221
222int Slider::value() const
223{
224 return static_cast<QSlider*>(widget())->value();
225}
226
227void Slider::setOrientation(Qt::Orientation orientation)
228{
229 static_cast<QSlider*>(widget())->setOrientation(orientation);
230}
231
232Qt::Orientation Slider::orientation() const
233{
234 return static_cast<QSlider*>(widget())->orientation();
235}
236
237void Slider::setStyleSheet(const QString &stylesheet)
238{
239 widget()->setStyleSheet(stylesheet);
240}
241
242QString Slider::styleSheet()
243{
244 return widget()->styleSheet();
245}
246
247QSlider *Slider::nativeWidget() const
248{
249 return static_cast<QSlider*>(widget());
250}
251
252} // namespace Plasma
253
254#include <slider.moc>
255
Plasma::FrameSvg
Provides an SVG with borders.
Definition: framesvg.h:77
Plasma::Slider::setMaximum
void setMaximum(int maximum)
Sets the maximum value the slider can take.
Definition: slider.cpp:192
Plasma::Slider::value
int value
Definition: slider.h:46
Plasma::Slider::orientation
Qt::Orientation orientation
Definition: slider.h:47
Plasma::Slider::minimum
int minimum
Definition: slider.h:45
Plasma::Slider::sliderMoved
void sliderMoved(int value)
This signal is emitted when the user drags the slider.
Plasma::Slider::setMinimum
void setMinimum(int minimum)
Sets the minimum value the slider can take.
Definition: slider.cpp:202
Plasma::Slider::setOrientation
void setOrientation(Qt::Orientation orientation)
Sets the orientation of the slider.
Definition: slider.cpp:227
Plasma::Slider::maximum
int maximum
Definition: slider.h:44
Plasma::Slider::setRange
void setRange(int minimum, int maximum)
Sets the minimum and maximum values the slider can take.
Definition: slider.cpp:212
Plasma::Slider::styleSheet
QString styleSheet
Definition: slider.h:48
Plasma::Slider::valueChanged
void valueChanged(int value)
This signal is emitted when the slider value has changed, with the new slider value as argument.
Plasma::Slider::nativeWidget
QSlider * nativeWidget
Definition: slider.h:49
Plasma::Slider::setValue
void setValue(int value)
Sets the value of the slider.
Definition: slider.cpp:217
Plasma::Theme::color
Q_INVOKABLE QColor color(ColorRole role) const
Returns the text color to be used by items resting on the background.
Definition: theme.cpp:918
Plasma::Theme::defaultTheme
static Theme * defaultTheme()
Singleton pattern accessor.
Definition: theme.cpp:544
Plasma::Theme::TextColor
@ TextColor
the text color to be used by items resting on the background
Definition: theme.h:63
QGraphicsProxyWidget
QGraphicsWidget
QStyleOptionGraphicsItem
QWidget
framesvg.h
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
slider.h
theme.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal