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

KDEUI

  • kdeui
  • widgets
kxyselector.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 "kxyselector.h"
21#include <QStyle>
22#include <QPainter>
23#include <QStyleOptionFrame>
24#include <QMouseEvent>
25#include <kdebug.h>
26
27//-----------------------------------------------------------------------------
28/*
29 * 2D value selector.
30 * The contents of the selector are drawn by derived class.
31 */
32
33class KXYSelector::Private
34{
35public:
36 Private(KXYSelector *q):
37 q(q),
38 xPos(0),
39 yPos(0),
40 minX(0),
41 maxX(100),
42 minY(0),
43 maxY(100),
44 m_markerColor(Qt::white)
45 {}
46
47 void setValues(int _xPos, int _yPos);
48
49 KXYSelector *q;
50 int px;
51 int py;
52 int xPos;
53 int yPos;
54 int minX;
55 int maxX;
56 int minY;
57 int maxY;
58 QColor m_markerColor;
59};
60
61KXYSelector::KXYSelector( QWidget *parent )
62 : QWidget( parent )
63 , d(new Private(this))
64{
65}
66
67
68KXYSelector::~KXYSelector()
69{
70 delete d;
71}
72
73int KXYSelector::xValue() const
74{
75 return d->xPos;
76}
77
78int KXYSelector::yValue() const
79{
80 return d->yPos;
81}
82
83void KXYSelector::setRange( int _minX, int _minY, int _maxX, int _maxY )
84{
85 if (_maxX == _minX) {
86 kWarning() << "KXYSelector::setRange invalid range: " << _maxX << " == " << _minX << " (for X) ";
87 return;
88 }
89 if (_maxY == _minY) {
90 kWarning() << "KXYSelector::setRange invalid range: " << _maxY << " == " << _minY << " (for Y) ";
91 return;
92 }
93
94
95 int w = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
96 d->px = w;
97 d->py = w;
98 d->minX = _minX;
99 d->minY = _minY;
100 d->maxX = _maxX;
101 d->maxY = _maxY;
102}
103
104void KXYSelector::setXValue( int _xPos )
105{
106 setValues(_xPos, d->yPos);
107}
108
109void KXYSelector::setYValue( int _yPos )
110{
111 setValues(d->xPos, _yPos);
112}
113
114void KXYSelector::setValues( int _xPos, int _yPos )
115{
116 d->setValues(_xPos, _yPos);
117}
118
119void KXYSelector::Private::setValues(int _xPos, int _yPos )
120{
121 int w = q->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
122
123 xPos = _xPos;
124 yPos = _yPos;
125
126 if ( xPos > maxX )
127 xPos = maxX;
128 else if ( xPos < minX )
129 xPos = minX;
130
131 if ( yPos > maxY )
132 yPos = maxY;
133 else if ( yPos < minY )
134 yPos = minY;
135
136 Q_ASSERT(maxX != minX);
137 int xp = w + (q->width() - 2 * w) * xPos / (maxX - minX);
138
139 Q_ASSERT(maxY != minY);
140 int yp = q->height() - w - (q->height() - 2 * w) * yPos / (maxY - minY);
141
142 q->setPosition( xp, yp );
143}
144
145void KXYSelector::setMarkerColor( const QColor &col )
146{
147 d->m_markerColor = col;
148}
149
150QRect KXYSelector::contentsRect() const
151{
152 int w = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
153 return rect().adjusted(w, w, -w, -w);
154}
155
156QSize KXYSelector::minimumSizeHint() const
157{
158 int w = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
159 return QSize( 2 * w, 2 * w );
160}
161
162void KXYSelector::paintEvent( QPaintEvent * /* ev */ )
163{
164 QStyleOptionFrame opt;
165 opt.initFrom(this);
166
167 QPainter painter;
168 painter.begin( this );
169
170 drawContents( &painter );
171 drawMarker( &painter, d->px, d->py );
172
173 style()->drawPrimitive( QStyle::PE_Frame, &opt, &painter, this );
174
175 painter.end();
176}
177
178void KXYSelector::mousePressEvent( QMouseEvent *e )
179{
180 mouseMoveEvent( e );
181}
182
183void KXYSelector::mouseMoveEvent( QMouseEvent *e )
184{
185 int xVal, yVal;
186 int w = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
187 valuesFromPosition( e->pos().x() - w, e->pos().y() - w, xVal, yVal );
188 setValues( xVal, yVal );
189
190 emit valueChanged( d->xPos, d->yPos );
191}
192
193void KXYSelector::wheelEvent( QWheelEvent *e )
194{
195 if ( e->orientation() == Qt::Horizontal )
196 setValues( xValue() + e->delta()/120, yValue() );
197 else
198 setValues( xValue(), yValue() + e->delta()/120 );
199
200 emit valueChanged( d->xPos, d->yPos );
201}
202
203void KXYSelector::valuesFromPosition( int x, int y, int &xVal, int &yVal ) const
204{
205 int w = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
206
207 xVal = ( ( d->maxX - d->minX ) * ( x - w ) ) / ( width() - 2 * w );
208 yVal = d->maxY - ( ( ( d->maxY - d->minY ) * (y - w) ) / ( height() - 2 * w ) );
209
210 if ( xVal > d->maxX )
211 xVal = d->maxX;
212 else if ( xVal < d->minX )
213 xVal = d->minX;
214
215 if ( yVal > d->maxY )
216 yVal = d->maxY;
217 else if ( yVal < d->minY )
218 yVal = d->minY;
219}
220
221void KXYSelector::setPosition( int xp, int yp )
222{
223 int w = style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
224
225 if ( xp < w )
226 xp = w;
227 else if ( xp > width() - w )
228 xp = width() - w;
229
230 if ( yp < w )
231 yp = w;
232 else if ( yp > height() - w )
233 yp = height() - w;
234
235 d->px = xp;
236 d->py = yp;
237
238 update();
239}
240
241void KXYSelector::drawContents( QPainter * )
242{}
243
244
245void KXYSelector::drawMarker( QPainter *p, int xp, int yp )
246{
247 QPen pen( d->m_markerColor );
248 p->setPen( pen );
249
250/*
251 p->drawLine( xp - 6, yp - 6, xp - 2, yp - 2 );
252 p->drawLine( xp - 6, yp + 6, xp - 2, yp + 2 );
253 p->drawLine( xp + 6, yp - 6, xp + 2, yp - 2 );
254 p->drawLine( xp + 6, yp + 6, xp + 2, yp + 2 );
255*/
256 p->drawEllipse(xp - 4, yp - 4, 8, 8);
257}
258
259
260
261#include "kxyselector.moc"
KXYSelector
KXYSelector is the base class for other widgets which provides the ability to choose from a two-dimen...
Definition: kxyselector.h:40
KXYSelector::drawContents
virtual void drawContents(QPainter *)
Override this function to draw the contents of the widget.
Definition: kxyselector.cpp:241
KXYSelector::xValue
int xValue
Definition: kxyselector.h:42
KXYSelector::contentsRect
QRect contentsRect() const
Definition: kxyselector.cpp:150
KXYSelector::drawMarker
virtual void drawMarker(QPainter *p, int xp, int yp)
Override this function to draw the marker which indicates the currently selected value pair.
Definition: kxyselector.cpp:245
KXYSelector::setRange
void setRange(int minX, int minY, int maxX, int maxY)
Sets the range of possible values.
Definition: kxyselector.cpp:83
KXYSelector::wheelEvent
virtual void wheelEvent(QWheelEvent *)
Definition: kxyselector.cpp:193
KXYSelector::yValue
int yValue
Definition: kxyselector.h:43
KXYSelector::setValues
void setValues(int xPos, int yPos)
Sets the current values in horizontal and vertical direction.
Definition: kxyselector.cpp:114
KXYSelector::setMarkerColor
void setMarkerColor(const QColor &col)
Sets the color used to draw the marker.
Definition: kxyselector.cpp:145
KXYSelector::minimumSizeHint
virtual QSize minimumSizeHint() const
Reimplemented to give the widget a minimum size.
Definition: kxyselector.cpp:156
KXYSelector::setXValue
void setXValue(int xPos)
Sets the current horizontal value.
Definition: kxyselector.cpp:104
KXYSelector::valuesFromPosition
void valuesFromPosition(int x, int y, int &xVal, int &yVal) const
Converts a pixel position to its corresponding values.
Definition: kxyselector.cpp:203
KXYSelector::paintEvent
virtual void paintEvent(QPaintEvent *e)
Definition: kxyselector.cpp:162
KXYSelector::~KXYSelector
~KXYSelector()
Destructs the widget.
Definition: kxyselector.cpp:68
KXYSelector::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *e)
Definition: kxyselector.cpp:183
KXYSelector::mousePressEvent
virtual void mousePressEvent(QMouseEvent *e)
Definition: kxyselector.cpp:178
KXYSelector::KXYSelector
KXYSelector(QWidget *parent=0)
Constructs a two-dimensional selector widget which has a value range of [0..100] in both directions.
Definition: kxyselector.cpp:61
KXYSelector::valueChanged
void valueChanged(int x, int y)
This signal is emitted whenever the user chooses a value, e.g.
KXYSelector::setYValue
void setYValue(int yPos)
Sets the current vertical value.
Definition: kxyselector.cpp:109
QWidget
kWarning
#define kWarning
kdebug.h
kxyselector.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.

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