-rw-r--r-- | libopie2/opieui/oselector.h | 518 |
1 files changed, 518 insertions, 0 deletions
diff --git a/libopie2/opieui/oselector.h b/libopie2/opieui/oselector.h new file mode 100644 index 0000000..f832239 --- a/dev/null +++ b/libopie2/opieui/oselector.h | |||
@@ -0,0 +1,518 @@ | |||
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., 59 Temple Place - Suite 330, | ||
17 | Boston, MA 02111-1307, USA. | ||
18 | */ | ||
19 | //----------------------------------------------------------------------------- | ||
20 | // Selector widgets for KDE Color Selector, but probably useful for other | ||
21 | // stuff also. | ||
22 | |||
23 | #ifndef __OSELECT_H__ | ||
24 | #define __OSELECT_H__ | ||
25 | |||
26 | #include <qwidget.h> | ||
27 | #include <qrangecontrol.h> | ||
28 | #include <qpixmap.h> | ||
29 | |||
30 | /** | ||
31 | * OXYSelector is the base class for other widgets which | ||
32 | * provides the ability to choose from a two-dimensional | ||
33 | * range of values. The currently chosen value is indicated | ||
34 | * by a cross. An example is the @ref OHSSelector which | ||
35 | * allows to choose from a range of colors, and which is | ||
36 | * used in OColorDialog. | ||
37 | * | ||
38 | * A custom drawing routine for the widget surface has | ||
39 | * to be provided by the subclass. | ||
40 | */ | ||
41 | class OXYSelector : public QWidget | ||
42 | { | ||
43 | Q_OBJECT | ||
44 | |||
45 | public: | ||
46 | /** | ||
47 | * Constructs a two-dimensional selector widget which | ||
48 | * has a value range of [0..100] in both directions. | ||
49 | */ | ||
50 | OXYSelector( QWidget *parent=0, const char *name=0 ); | ||
51 | /** | ||
52 | * Destructs the widget. | ||
53 | */ | ||
54 | ~OXYSelector(); | ||
55 | |||
56 | /** | ||
57 | * Sets the current values in horizontal and | ||
58 | * vertical direction. | ||
59 | */ | ||
60 | void setValues( int xPos, int yPos ); | ||
61 | /** | ||
62 | * Sets the range of possible values. | ||
63 | */ | ||
64 | void setRange( int minX, int minY, int maxX, int maxY ); | ||
65 | |||
66 | /** | ||
67 | * @return the current value in horizontal direction. | ||
68 | */ | ||
69 | int xValue() const {return xPos; } | ||
70 | /** | ||
71 | * @return the current value in vertical direction. | ||
72 | */ | ||
73 | int yValue() const {return yPos; } | ||
74 | |||
75 | /** | ||
76 | * @return the rectangle on which subclasses should draw. | ||
77 | */ | ||
78 | QRect contentsRect() const; | ||
79 | |||
80 | signals: | ||
81 | /** | ||
82 | * This signal is emitted whenever the user chooses a value, | ||
83 | * e.g. by clicking with the mouse on the widget. | ||
84 | */ | ||
85 | void valueChanged( int x, int y ); | ||
86 | |||
87 | protected: | ||
88 | /** | ||
89 | * Override this function to draw the contents of the widget. | ||
90 | * The default implementation does nothing. | ||
91 | * | ||
92 | * Draw within @ref contentsRect() only. | ||
93 | */ | ||
94 | virtual void drawContents( QPainter * ); | ||
95 | /** | ||
96 | * Override this function to draw the cursor which | ||
97 | * indicates the currently selected value pair. | ||
98 | */ | ||
99 | virtual void drawCursor( QPainter *p, int xp, int yp ); | ||
100 | /** | ||
101 | * @reimplemented | ||
102 | */ | ||
103 | virtual void paintEvent( QPaintEvent *e ); | ||
104 | /** | ||
105 | * @reimplemented | ||
106 | */ | ||
107 | virtual void mousePressEvent( QMouseEvent *e ); | ||
108 | /** | ||
109 | * @reimplemented | ||
110 | */ | ||
111 | virtual void mouseMoveEvent( QMouseEvent *e ); | ||
112 | /** | ||
113 | * @reimplemented | ||
114 | */ | ||
115 | virtual void wheelEvent( QWheelEvent * ); | ||
116 | /** | ||
117 | * Converts a pixel position to its corresponding values. | ||
118 | */ | ||
119 | void valuesFromPosition( int x, int y, int& xVal, int& yVal ) const; | ||
120 | |||
121 | private: | ||
122 | void setPosition( int xp, int yp ); | ||
123 | int px; | ||
124 | int py; | ||
125 | int xPos; | ||
126 | int yPos; | ||
127 | int minX; | ||
128 | int maxX; | ||
129 | int minY; | ||
130 | int maxY; | ||
131 | QPixmap store; | ||
132 | |||
133 | private: | ||
134 | class OXYSelectorPrivate; | ||
135 | OXYSelectorPrivate *d; | ||
136 | }; | ||
137 | |||
138 | |||
139 | /** | ||
140 | * OSelector is the base class for other widgets which | ||
141 | * provides the ability to choose from a one-dimensional | ||
142 | * range of values. An example is the @ref OGradientSelector | ||
143 | * which allows to choose from a range of colors. | ||
144 | * | ||
145 | * A custom drawing routine for the widget surface has | ||
146 | * to be provided by the subclass. | ||
147 | */ | ||
148 | class OSelector : public QWidget, public QRangeControl | ||
149 | { | ||
150 | Q_OBJECT | ||
151 | Q_PROPERTY( int value READ value WRITE setValue ) | ||
152 | Q_PROPERTY( int minValue READ minValue WRITE setMinValue ) | ||
153 | Q_PROPERTY( int maxValue READ maxValue WRITE setMaxValue ) | ||
154 | public: | ||
155 | |||
156 | /** | ||
157 | * Constructs a horizontal one-dimensional selection widget. | ||
158 | */ | ||
159 | OSelector( QWidget *parent=0, const char *name=0 ); | ||
160 | /** | ||
161 | * Constructs a one-dimensional selection widget with | ||
162 | * a given orientation. | ||
163 | */ | ||
164 | OSelector( Orientation o, QWidget *parent = 0L, const char *name = 0L ); | ||
165 | /* | ||
166 | * Destructs the widget. | ||
167 | */ | ||
168 | ~OSelector(); | ||
169 | |||
170 | /** | ||
171 | * @return the orientation of the widget. | ||
172 | */ | ||
173 | Orientation orientation() const | ||
174 | {return _orientation; } | ||
175 | |||
176 | /** | ||
177 | * @return the rectangle on which subclasses should draw. | ||
178 | */ | ||
179 | QRect contentsRect() const; | ||
180 | |||
181 | /** | ||
182 | * Sets the indent option of the widget to i. | ||
183 | * This determines whether a shaded frame is drawn. | ||
184 | */ | ||
185 | void setIndent( bool i ) | ||
186 | {_indent = i; } | ||
187 | /** | ||
188 | * @return whether the indent option is set. | ||
189 | */ | ||
190 | bool indent() const | ||
191 | {return _indent; } | ||
192 | |||
193 | /** | ||
194 | * Sets the value. | ||
195 | */ | ||
196 | void setValue(int value) | ||
197 | { QRangeControl::setValue(value); } | ||
198 | |||
199 | /** | ||
200 | * @returns the value. | ||
201 | */ | ||
202 | int value() const | ||
203 | { return QRangeControl::value(); } | ||
204 | |||
205 | /** | ||
206 | * Sets the min value. | ||
207 | */ | ||
208 | #if ( QT_VERSION > 290 ) | ||
209 | void setMinValue(int value) { QRangeControl::setMinValue(value); } | ||
210 | #else | ||
211 | void setMinValue(int value) { QRangeControl::setRange(value,QRangeControl::maxValue()); } | ||
212 | #endif | ||
213 | |||
214 | /** | ||
215 | * @return the min value. | ||
216 | */ | ||
217 | int minValue() const | ||
218 | { return QRangeControl::minValue(); } | ||
219 | |||
220 | /** | ||
221 | * Sets the max value. | ||
222 | */ | ||
223 | #if ( QT_VERSION > 290 ) | ||
224 | void setMaxValue(int value) { QRangeControl::setMaxValue(value); } | ||
225 | #else | ||
226 | void setMaxValue(int value) { QRangeControl::setRange(QRangeControl::minValue(),value); } | ||
227 | #endif | ||
228 | |||
229 | /** | ||
230 | * @return the max value. | ||
231 | */ | ||
232 | int maxValue() const | ||
233 | { return QRangeControl::maxValue(); } | ||
234 | |||
235 | signals: | ||
236 | /** | ||
237 | * This signal is emitted whenever the user chooses a value, | ||
238 | * e.g. by clicking with the mouse on the widget. | ||
239 | */ | ||
240 | void valueChanged( int value ); | ||
241 | |||
242 | protected: | ||
243 | /** | ||
244 | * Override this function to draw the contents of the control. | ||
245 | * The default implementation does nothing. | ||
246 | * | ||
247 | * Draw only within contentsRect(). | ||
248 | */ | ||
249 | virtual void drawContents( QPainter * ); | ||
250 | /** | ||
251 | * Override this function to draw the cursor which | ||
252 | * indicates the current value. This function is | ||
253 | * always called twice, once with argument show=false | ||
254 | * to clear the old cursor, once with argument show=true | ||
255 | * to draw the new one. | ||
256 | */ | ||
257 | virtual void drawArrow( QPainter *painter, bool show, const QPoint &pos ); | ||
258 | |||
259 | /** | ||
260 | * @reimplemented | ||
261 | */ | ||
262 | virtual void valueChange(); | ||
263 | /** | ||
264 | * @reimplemented | ||
265 | */ | ||
266 | virtual void paintEvent( QPaintEvent * ); | ||
267 | /** | ||
268 | * @reimplemented | ||
269 | */ | ||
270 | virtual void mousePressEvent( QMouseEvent *e ); | ||
271 | /** | ||
272 | * @reimplemented | ||
273 | */ | ||
274 | virtual void mouseMoveEvent( QMouseEvent *e ); | ||
275 | /** | ||
276 | * @reimplemented | ||
277 | */ | ||
278 | virtual void wheelEvent( QWheelEvent * ); | ||
279 | |||
280 | private: | ||
281 | QPoint calcArrowPos( int val ); | ||
282 | void moveArrow( const QPoint &pos ); | ||
283 | |||
284 | Orientation _orientation; | ||
285 | bool _indent; | ||
286 | |||
287 | private: | ||
288 | class OSelectorPrivate; | ||
289 | OSelectorPrivate *d; | ||
290 | }; | ||
291 | |||
292 | |||
293 | /** | ||
294 | * The OGradientSelector widget allows the user to choose | ||
295 | * from a one-dimensional range of colors which is given as a | ||
296 | * gradient between two colors provided by the programmer. | ||
297 | */ | ||
298 | class OGradientSelector : public OSelector | ||
299 | { | ||
300 | Q_OBJECT | ||
301 | |||
302 | Q_PROPERTY( QColor firstColor READ firstColor WRITE setFirstColor ) | ||
303 | Q_PROPERTY( QColor secondColor READ secondColor WRITE setSecondColor ) | ||
304 | Q_PROPERTY( QString firstText READ firstText WRITE setFirstText ) | ||
305 | Q_PROPERTY( QString secondText READ secondText WRITE setSecondText ) | ||
306 | |||
307 | public: | ||
308 | /** | ||
309 | * Constructs a horizontal color selector which | ||
310 | * contains a gradient between white and black. | ||
311 | */ | ||
312 | OGradientSelector( QWidget *parent=0, const char *name=0 ); | ||
313 | /** | ||
314 | * Constructs a colors selector with orientation o which | ||
315 | * contains a gradient between white and black. | ||
316 | */ | ||
317 | OGradientSelector( Orientation o, QWidget *parent=0, const char *name=0 ); | ||
318 | /** | ||
319 | * Destructs the widget. | ||
320 | */ | ||
321 | ~OGradientSelector(); | ||
322 | /** | ||
323 | * Sets the two colors which span the gradient. | ||
324 | */ | ||
325 | void setColors( const QColor &col1, const QColor &col2 ) | ||
326 | {color1 = col1; color2 = col2; update();} | ||
327 | void setText( const QString &t1, const QString &t2 ) | ||
328 | {text1 = t1; text2 = t2; update(); } | ||
329 | |||
330 | /** | ||
331 | * Set each color on its own. | ||
332 | */ | ||
333 | void setFirstColor( const QColor &col ) | ||
334 | { color1 = col; update(); } | ||
335 | void setSecondColor( const QColor &col ) | ||
336 | { color2 = col; update(); } | ||
337 | |||
338 | /** | ||
339 | * Set each description on its own | ||
340 | */ | ||
341 | void setFirstText( const QString &t ) | ||
342 | { text1 = t; update(); } | ||
343 | void setSecondText( const QString &t ) | ||
344 | { text2 = t; update(); } | ||
345 | |||
346 | const QColor firstColor() const | ||
347 | { return color1; } | ||
348 | const QColor secondColor() const | ||
349 | { return color2; } | ||
350 | |||
351 | const QString firstText() const | ||
352 | { return text1; } | ||
353 | const QString secondText() const | ||
354 | { return text2; } | ||
355 | |||
356 | protected: | ||
357 | /** | ||
358 | * @reimplemented | ||
359 | */ | ||
360 | virtual void drawContents( QPainter * ); | ||
361 | |||
362 | /** | ||
363 | * @reimplemented | ||
364 | */ | ||
365 | virtual QSize minimumSize() const | ||
366 | { return sizeHint(); } | ||
367 | |||
368 | private: | ||
369 | void init(); | ||
370 | QColor color1; | ||
371 | QColor color2; | ||
372 | QString text1; | ||
373 | QString text2; | ||
374 | |||
375 | private: | ||
376 | class OGradientSelectorPrivate; | ||
377 | OGradientSelectorPrivate *d; | ||
378 | }; | ||
379 | |||
380 | /** | ||
381 | * Widget for Hue/Saturation selection. | ||
382 | * The actual values can be fetched using the inherited xValue and yValue | ||
383 | * methods. | ||
384 | * | ||
385 | * @see OXYSelector, OValueSelector, OColorDialog | ||
386 | * @author Martin Jones (mjones@kde.org) | ||
387 | * @version $Id$ | ||
388 | */ | ||
389 | class OHSSelector : public OXYSelector | ||
390 | { | ||
391 | Q_OBJECT | ||
392 | |||
393 | public: | ||
394 | /** | ||
395 | * Constructs a hue/saturation selection widget. | ||
396 | */ | ||
397 | OHSSelector( QWidget *parent=0, const char *name=0 ); | ||
398 | |||
399 | protected: | ||
400 | /** | ||
401 | * Draws the contents of the widget on a pixmap, | ||
402 | * which is used for buffering. | ||
403 | */ | ||
404 | virtual void drawPalette( QPixmap *pixmap ); | ||
405 | /** | ||
406 | * @reimplemented | ||
407 | */ | ||
408 | virtual void resizeEvent( QResizeEvent * ); | ||
409 | /** | ||
410 | * Reimplemented from OXYSelector. This drawing is | ||
411 | * buffered in a pixmap here. As real drawing | ||
412 | * routine, drawPalette() is used. | ||
413 | */ | ||
414 | virtual void drawContents( QPainter *painter ); | ||
415 | |||
416 | private: | ||
417 | void updateContents(); | ||
418 | QPixmap pixmap; | ||
419 | |||
420 | private: | ||
421 | class OHSSelectorPrivate; | ||
422 | OHSSelectorPrivate *d; | ||
423 | }; | ||
424 | |||
425 | |||
426 | class OValueSelectorPrivate; | ||
427 | /** | ||
428 | * Widget for color value selection. | ||
429 | * | ||
430 | * @see OHSSelector, OColorDialog | ||
431 | * @author Martin Jones (mjones@kde.org) | ||
432 | * @version $Id$ | ||
433 | */ | ||
434 | class OValueSelector : public OSelector | ||
435 | { | ||
436 | Q_OBJECT | ||
437 | |||
438 | public: | ||
439 | /** | ||
440 | * Constructs a widget for color selection. | ||
441 | */ | ||
442 | OValueSelector( QWidget *parent=0, const char *name=0 ); | ||
443 | /** | ||
444 | * Constructs a widget for color selection with a given orientation | ||
445 | */ | ||
446 | OValueSelector( Orientation o, QWidget *parent = 0, const char *name = 0 ); | ||
447 | |||
448 | int hue() const | ||
449 | { return _hue; } | ||
450 | void setHue( int h ) | ||
451 | { _hue = h; } | ||
452 | int saturation() const | ||
453 | { return _sat; } | ||
454 | void setSaturation( int s ) | ||
455 | { _sat = s; } | ||
456 | |||
457 | void updateContents(); | ||
458 | protected: | ||
459 | /** | ||
460 | * Draws the contents of the widget on a pixmap, | ||
461 | * which is used for buffering. | ||
462 | */ | ||
463 | virtual void drawPalette( QPixmap *pixmap ); | ||
464 | /** | ||
465 | * @reimplemented | ||
466 | */ | ||
467 | virtual void resizeEvent( QResizeEvent * ); | ||
468 | /** | ||
469 | * Reimplemented from OSelector. The drawing is | ||
470 | * buffered in a pixmap here. As real drawing | ||
471 | * routine, drawPalette() is used. | ||
472 | */ | ||
473 | virtual void drawContents( QPainter *painter ); | ||
474 | |||
475 | private: | ||
476 | int _hue; | ||
477 | int _sat; | ||
478 | QPixmap pixmap; | ||
479 | |||
480 | private: | ||
481 | class OValueSelectorPrivate; | ||
482 | OValueSelectorPrivate *d; | ||
483 | }; | ||
484 | |||
485 | |||
486 | class OColor : public QColor | ||
487 | { | ||
488 | public: | ||
489 | OColor(); | ||
490 | OColor( const OColor &col); | ||
491 | OColor( const QColor &col); | ||
492 | |||
493 | OColor& operator=( const OColor& col); | ||
494 | |||
495 | bool operator==( const OColor& col) const; | ||
496 | |||
497 | void setHsv(int _h, int _s, int _v); | ||
498 | void setRgb(int _r, int _g, int _b); | ||
499 | |||
500 | void rgb(int *_r, int *_g, int *_b) const; | ||
501 | void hsv(int *_h, int *_s, int *_v) const; | ||
502 | protected: | ||
503 | int h; | ||
504 | int s; | ||
505 | int v; | ||
506 | int r; | ||
507 | int g; | ||
508 | int b; | ||
509 | |||
510 | private: | ||
511 | class OColorPrivate; | ||
512 | OColorPrivate *d; | ||
513 | }; | ||
514 | |||
515 | |||
516 | |||
517 | #endif // __OSELECT_H__ | ||
518 | |||