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

KDEUI

  • kdeui
  • widgets
kselector.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 1997 Martin Jones (mjones@kde.org)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "kselector.h"
21
22#include <QImage>
23#include <QPainter>
24#include <QPaintEvent>
25#include <QPixmap>
26#include <QStyle>
27#include <QStyleOption>
28
29#include "kcolorhelpers_p.h"
30
31using KDEPrivate::fillOpaqueRect;
32
33//-----------------------------------------------------------------------------
34/*
35 * 1D value selector with contents drawn by derived class.
36 * See KColorDialog for example.
37 */
38
39#define ARROWSIZE 5
40
41class KSelector::Private
42{
43public:
44 Private()
45 {
46 arrowPE = QStyle::PE_IndicatorArrowLeft;
47 m_indent = true;
48 }
49
50 bool m_indent;
51 QStyle::PrimitiveElement arrowPE;
52};
53
54class KGradientSelector::KGradientSelectorPrivate
55{
56public:
57 KGradientSelectorPrivate(KGradientSelector *q): q(q) {}
58
59 KGradientSelector *q;
60 QLinearGradient gradient;
61 QString text1;
62 QString text2;
63};
64
65KSelector::KSelector( QWidget *parent )
66 : QAbstractSlider( parent )
67 , d(new Private)
68{
69 setOrientation(Qt::Horizontal);
70}
71
72KSelector::KSelector( Qt::Orientation o, QWidget *parent )
73 : QAbstractSlider( parent )
74 , d(new Private)
75{
76 setOrientation(o);
77 if(o == Qt::Horizontal)
78 setArrowDirection(Qt::UpArrow);
79}
80
81KSelector::~KSelector()
82{
83 delete d;
84}
85
86void KSelector::setIndent( bool i )
87{
88 d->m_indent = i;
89}
90
91bool KSelector::indent() const
92{
93 return d->m_indent;
94}
95
96QRect KSelector::contentsRect() const
97{
98 int w = indent() ? style()->pixelMetric( QStyle::PM_DefaultFrameWidth ) : 0;
99 //TODO: is the height:width ratio of an indicator arrow always 2:1? hm.
100 int iw = (w < ARROWSIZE) ? ARROWSIZE : w;
101
102 if ( orientation() == Qt::Vertical ) {
103 if ( arrowDirection() == Qt::RightArrow ) {
104 return QRect( w + ARROWSIZE, iw,
105 width() - w*2 - ARROWSIZE,
106 height() - iw*2 );
107 } else {
108 return QRect( w, iw,
109 width() - w*2 - ARROWSIZE,
110 height() - iw*2 );
111 }
112 } else { // Qt::Horizontal
113 if ( arrowDirection() == Qt::UpArrow ) {
114 return QRect( iw, w,
115 width() - 2*iw,
116 height() - w*2 - ARROWSIZE );
117 } else {
118 return QRect( iw, w + ARROWSIZE,
119 width() - 2*iw,
120 height() - w*2 - ARROWSIZE );
121 }
122 }
123}
124
125void KSelector::paintEvent( QPaintEvent * )
126{
127 QPainter painter;
128 int w = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
129 int iw = (w < ARROWSIZE) ? ARROWSIZE : w;
130
131 painter.begin( this );
132
133 drawContents( &painter );
134
135 QBrush brush;
136
137 QPoint pos = calcArrowPos( value() );
138 drawArrow( &painter, pos );
139
140 if ( indent() )
141 {
142 QStyleOptionFrame opt;
143 opt.initFrom( this );
144 opt.state = QStyle::State_Sunken;
145 if ( orientation() == Qt::Vertical )
146 opt.rect.adjust( 0, iw - w, -5, w - iw );
147 else
148 opt.rect.adjust(iw - w, 0, w - iw, -5);
149 QBrush oldBrush = painter.brush();
150 painter.setBrush( Qt::NoBrush );
151 style()->drawPrimitive( QStyle::PE_Frame, &opt, &painter, this );
152 painter.setBrush( oldBrush );
153 }
154
155
156 painter.end();
157}
158
159void KSelector::mousePressEvent( QMouseEvent *e )
160{
161 setSliderDown(true);
162 moveArrow( e->pos() );
163}
164
165void KSelector::mouseMoveEvent( QMouseEvent *e )
166{
167 moveArrow( e->pos() );
168}
169
170void KSelector::mouseReleaseEvent( QMouseEvent *e )
171{
172 moveArrow( e->pos() );
173 setSliderDown(false);
174}
175
176void KSelector::wheelEvent( QWheelEvent *e )
177{
178 int val = value() + e->delta()/120;
179 setSliderDown(true);
180 setValue( val );
181 setSliderDown(false);
182}
183
184void KSelector::moveArrow( const QPoint &pos )
185{
186 int val;
187 int w = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
188 int iw = (w < ARROWSIZE) ? ARROWSIZE : w;
189
190 if ( orientation() == Qt::Vertical )
191 val = ( maximum() - minimum() ) * (height() - pos.y() - iw)
192 / (height() - iw * 2) + minimum();
193 else
194 val = ( maximum() - minimum() ) * ( pos.x() - iw)
195 / (width() - iw * 2) + minimum();
196
197 setValue( val );
198 update();
199}
200
201QPoint KSelector::calcArrowPos( int val )
202{
203 QPoint p;
204 int w = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
205 int iw = ( w < ARROWSIZE ) ? ARROWSIZE : w;
206
207 if ( orientation() == Qt::Vertical )
208 {
209 p.setY( height() - iw - 1 - (height() - 2 * iw - 1) * val / ( maximum() - minimum() ) );
210
211 if ( d->arrowPE == QStyle::PE_IndicatorArrowRight ) {
212 p.setX( 0 );
213 } else {
214 p.setX( width() - 5 );
215 }
216 }
217 else
218 {
219 p.setX( iw + (width() - 2 * iw - 1) * val / ( maximum() - minimum() ) );
220
221 if ( d->arrowPE == QStyle::PE_IndicatorArrowDown ) {
222 p.setY( 0 );
223 } else {
224 p.setY( height() - 5 );
225 }
226 }
227
228 return p;
229}
230
231void KSelector::setArrowDirection( Qt::ArrowType direction )
232{
233 switch ( direction ) {
234 case Qt::UpArrow:
235 if ( orientation() == Qt::Horizontal ) {
236 d->arrowPE = QStyle::PE_IndicatorArrowUp;
237 } else {
238 d->arrowPE = QStyle::PE_IndicatorArrowLeft;
239 }
240 break;
241 case Qt::DownArrow:
242 if ( orientation() == Qt::Horizontal ) {
243 d->arrowPE = QStyle::PE_IndicatorArrowDown;
244 } else {
245 d->arrowPE = QStyle::PE_IndicatorArrowRight;
246 }
247 break;
248 case Qt::LeftArrow:
249 if ( orientation() == Qt::Vertical ) {
250 d->arrowPE = QStyle::PE_IndicatorArrowLeft;
251 } else {
252 d->arrowPE = QStyle::PE_IndicatorArrowDown;
253 }
254 break;
255 case Qt::RightArrow:
256 if ( orientation() == Qt::Vertical ) {
257 d->arrowPE = QStyle::PE_IndicatorArrowRight;
258 } else {
259 d->arrowPE = QStyle::PE_IndicatorArrowUp;
260 }
261 break;
262
263 case Qt::NoArrow:
264 break;
265 }
266}
267
268Qt::ArrowType KSelector::arrowDirection() const
269{
270 switch ( d->arrowPE ) {
271 case QStyle::PE_IndicatorArrowUp:
272 return Qt::UpArrow;
273 break;
274 case QStyle::PE_IndicatorArrowDown:
275 return Qt::DownArrow;
276 break;
277 case QStyle::PE_IndicatorArrowRight:
278 return Qt::RightArrow;
279 break;
280 case QStyle::PE_IndicatorArrowLeft:
281 default:
282 return Qt::LeftArrow;
283 break;
284 }
285}
286
287void KSelector::drawContents( QPainter * )
288{}
289
290void KSelector::drawArrow( QPainter *painter, const QPoint &pos )
291{
292 painter->setPen( QPen() );
293 painter->setBrush( QBrush( palette().color(QPalette::ButtonText) ) );
294
295 QStyleOption o;
296
297 if ( orientation() == Qt::Vertical ) {
298 o.rect = QRect( pos.x(), pos.y() - ARROWSIZE / 2,
299 ARROWSIZE, ARROWSIZE );
300 } else {
301 o.rect = QRect( pos.x() - ARROWSIZE / 2, pos.y(),
302 ARROWSIZE, ARROWSIZE );
303
304 }
305 style()->drawPrimitive( d->arrowPE, &o, painter, this );
306}
307
308//----------------------------------------------------------------------------
309
310KGradientSelector::KGradientSelector( QWidget *parent )
311 : KSelector( parent ), d(new KGradientSelectorPrivate(this))
312{
313}
314
315
316KGradientSelector::KGradientSelector( Qt::Orientation o, QWidget *parent )
317 : KSelector( o, parent ), d(new KGradientSelectorPrivate(this))
318{
319}
320
321
322KGradientSelector::~KGradientSelector()
323{
324 delete d;
325}
326
327
328void KGradientSelector::drawContents( QPainter *painter )
329{
330 d->gradient.setStart(contentsRect().topLeft());
331 if (orientation() == Qt::Vertical) {
332 d->gradient.setFinalStop(contentsRect().bottomLeft());
333 } else {
334 d->gradient.setFinalStop(contentsRect().topRight());
335 }
336 QBrush gradientBrush(d->gradient);
337
338 fillOpaqueRect(painter, contentsRect(), gradientBrush);
339
340 if ( orientation() == Qt::Vertical )
341 {
342 int yPos = contentsRect().top() + painter->fontMetrics().ascent() + 2;
343 int xPos = contentsRect().left() + (contentsRect().width() -
344 painter->fontMetrics().width( d->text2 )) / 2;
345 QPen pen( qGray(firstColor().rgb()) > 180 ? Qt::black : Qt::white );
346 painter->setPen( pen );
347 painter->drawText( xPos, yPos, d->text2 );
348
349 yPos = contentsRect().bottom() - painter->fontMetrics().descent() - 2;
350 xPos = contentsRect().left() + (contentsRect().width() -
351 painter->fontMetrics().width( d->text1 )) / 2;
352 pen.setColor( qGray(secondColor().rgb()) > 180 ? Qt::black : Qt::white );
353 painter->setPen( pen );
354 painter->drawText( xPos, yPos, d->text1 );
355 }
356 else
357 {
358 int yPos = contentsRect().bottom()-painter->fontMetrics().descent()-2;
359
360 QPen pen( qGray(firstColor().rgb()) > 180 ? Qt::black : Qt::white );
361 painter->setPen( pen );
362 painter->drawText( contentsRect().left() + 2, yPos, d->text1 );
363
364 pen.setColor( qGray(secondColor().rgb()) > 180 ? Qt::black : Qt::white );
365 painter->setPen( pen );
366 painter->drawText( contentsRect().right() -
367 painter->fontMetrics().width( d->text2 ) - 2, yPos, d->text2 );
368 }
369}
370
371QSize KGradientSelector::minimumSize() const
372{
373 return sizeHint();
374}
375
376void KGradientSelector::setStops( const QGradientStops &stops )
377{
378 d->gradient.setStops(stops);
379 update();
380}
381
382QGradientStops KGradientSelector::stops() const
383{
384 return d->gradient.stops();
385}
386
387void KGradientSelector::setColors( const QColor &col1, const QColor &col2 )
388{
389 d->gradient.setColorAt(0.0, col1);
390 d->gradient.setColorAt(1.0, col2);
391 update();
392}
393
394void KGradientSelector::setText( const QString &t1, const QString &t2 )
395{
396 d->text1 = t1;
397 d->text2 = t2;
398 update();
399}
400
401void KGradientSelector::setFirstColor( const QColor &col )
402{
403 d->gradient.setColorAt(0.0, col);
404 update();
405}
406
407void KGradientSelector::setSecondColor( const QColor &col )
408{
409 d->gradient.setColorAt(1.0, col);
410 update();
411}
412
413void KGradientSelector::setFirstText( const QString &t )
414{
415 d->text1 = t;
416 update();
417}
418
419void KGradientSelector::setSecondText( const QString &t )
420{
421 d->text2 = t;
422 update();
423}
424
425QColor KGradientSelector::firstColor() const
426{
427 return d->gradient.stops().first().second;
428}
429
430QColor KGradientSelector::secondColor() const
431{
432 return d->gradient.stops().last().second;
433}
434
435QString KGradientSelector::firstText() const
436{
437 return d->text1;
438}
439
440QString KGradientSelector::secondText() const
441{
442 return d->text2;
443}
444
445#include "kselector.moc"
KGradientSelector
The KGradientSelector widget allows the user to choose from a one-dimensional range of colors which i...
Definition: kselector.h:133
KGradientSelector::setSecondColor
void setSecondColor(const QColor &col)
Definition: kselector.cpp:407
KGradientSelector::setFirstText
void setFirstText(const QString &t)
Set each description on its own.
Definition: kselector.cpp:413
KGradientSelector::setSecondText
void setSecondText(const QString &t)
Definition: kselector.cpp:419
KGradientSelector::setStops
void setStops(const QGradientStops &stops)
Sets the colors that make up the gradient.
Definition: kselector.cpp:376
KGradientSelector::setText
void setText(const QString &t1, const QString &t2)
Definition: kselector.cpp:394
KGradientSelector::setColors
void setColors(const QColor &col1, const QColor &col2)
Sets the two colors which span the gradient.
Definition: kselector.cpp:387
KGradientSelector::setFirstColor
void setFirstColor(const QColor &col)
Set each color on its own.
Definition: kselector.cpp:401
KGradientSelector::firstText
QString firstText
Definition: kselector.h:138
KGradientSelector::stops
QGradientStops stops() const
Get the colors that make up the gradient.
Definition: kselector.cpp:382
KGradientSelector::secondColor
QColor secondColor
Definition: kselector.h:137
KGradientSelector::firstColor
QColor firstColor
Definition: kselector.h:136
KGradientSelector::secondText
QString secondText
Definition: kselector.h:139
KGradientSelector::~KGradientSelector
~KGradientSelector()
Destructs the widget.
Definition: kselector.cpp:322
KGradientSelector::minimumSize
virtual QSize minimumSize() const
Definition: kselector.cpp:371
KGradientSelector::drawContents
virtual void drawContents(QPainter *)
Override this function to draw the contents of the control.
Definition: kselector.cpp:328
KGradientSelector::KGradientSelector
KGradientSelector(QWidget *parent=0)
Constructs a horizontal color selector which contains a gradient between white and black.
Definition: kselector.cpp:310
KSelector
KSelector is the base class for other widgets which provides the ability to choose from a one-dimensi...
Definition: kselector.h:42
KSelector::setArrowDirection
void setArrowDirection(Qt::ArrowType direction)
Sets the arrow direction.
Definition: kselector.cpp:231
KSelector::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *e)
Definition: kselector.cpp:170
KSelector::mousePressEvent
virtual void mousePressEvent(QMouseEvent *e)
Definition: kselector.cpp:159
KSelector::paintEvent
virtual void paintEvent(QPaintEvent *)
Definition: kselector.cpp:125
KSelector::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *e)
Definition: kselector.cpp:165
KSelector::wheelEvent
virtual void wheelEvent(QWheelEvent *)
Definition: kselector.cpp:176
KSelector::drawContents
virtual void drawContents(QPainter *)
Override this function to draw the contents of the control.
Definition: kselector.cpp:287
KSelector::value
int value
Definition: kselector.h:44
KSelector::setIndent
void setIndent(bool i)
Sets the indent option of the widget to i.
Definition: kselector.cpp:86
KSelector::arrowDirection
Qt::ArrowType arrowDirection
Definition: kselector.h:48
KSelector::contentsRect
QRect contentsRect() const
Definition: kselector.cpp:96
KSelector::indent
bool indent
Definition: kselector.h:47
KSelector::drawArrow
virtual void drawArrow(QPainter *painter, const QPoint &pos)
Override this function to draw the cursor which indicates the current value.
Definition: kselector.cpp:290
KSelector::KSelector
KSelector(QWidget *parent=0)
Constructs a horizontal one-dimensional selection widget.
Definition: kselector.cpp:65
KSelector::~KSelector
~KSelector()
Definition: kselector.cpp:81
QWidget
ARROWSIZE
#define ARROWSIZE
Definition: kselector.cpp:39
kselector.h
KDEPrivate::fillOpaqueRect
void fillOpaqueRect(QPainter *painter, const QRect &rect, const QBrush &brush)
Definition: kcolorhelpers.cpp:28
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.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • 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