• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
tdeselect.cpp
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 <tqimage.h>
21#include <tqpainter.h>
22#include <tqdrawutil.h>
23#include <tqstyle.h>
24#include <kimageeffect.h>
25#include "tdeselect.h"
26
27#define STORE_W 8
28#define STORE_W2 STORE_W * 2
29
30//-----------------------------------------------------------------------------
31/*
32 * 2D value selector.
33 * The contents of the selector are drawn by derived class.
34 */
35
36KXYSelector::KXYSelector( TQWidget *parent, const char *name )
37 : TQWidget( parent, name )
38{
39 xPos = 0;
40 yPos = 0;
41 minX = 0;
42 minY = 0;
43 maxX = 100;
44 maxY = 100;
45 store.setOptimization( TQPixmap::BestOptim );
46 store.resize( STORE_W2, STORE_W2 );
47}
48
49
50KXYSelector::~KXYSelector()
51{}
52
53
54void KXYSelector::setRange( int _minX, int _minY, int _maxX, int _maxY )
55{
56 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
57 px = w;
58 py = w;
59 minX = _minX;
60 minY = _minY;
61 maxX = _maxX;
62 maxY = _maxY;
63}
64
65void KXYSelector::setXValue( int _xPos )
66{
67 setValues(_xPos, yPos);
68}
69
70void KXYSelector::setYValue( int _yPos )
71{
72 setValues(xPos, _yPos);
73}
74
75void KXYSelector::setValues( int _xPos, int _yPos )
76{
77 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
78 if (w < 5) w = 5;
79
80 xPos = _xPos;
81 yPos = _yPos;
82
83 if ( xPos > maxX )
84 xPos = maxX;
85 else if ( xPos < minX )
86 xPos = minX;
87
88 if ( yPos > maxY )
89 yPos = maxY;
90 else if ( yPos < minY )
91 yPos = minY;
92
93 int xp = w + (width() - 2 * w) * xPos / (maxX - minX);
94 int yp = height() - w - (height() - 2 * w) * yPos / (maxY - minY);
95
96 setPosition( xp, yp );
97}
98
99TQRect KXYSelector::contentsRect() const
100{
101 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
102 if (w < 5) {
103 w = 5;
104 }
105 TQRect contents(rect());
106 contents.addCoords(w, w, -w, -w);
107 return contents;
108}
109
110void KXYSelector::paintEvent( TQPaintEvent *ev )
111{
112 TQRect cursorRect( px - STORE_W, py - STORE_W, STORE_W2, STORE_W2);
113 TQRect paintRect = ev->rect();
114 TQRect borderRect = rect();
115
116 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
117 if (w < 5) {
118 w = 5 - w;
119 }
120 borderRect.addCoords(w, w, -w, -w);
121
122 TQPainter painter;
123 painter.begin( this );
124
125 style().drawPrimitive(TQStyle::PE_Panel, &painter,
126 borderRect, colorGroup(),
127 TQStyle::Style_Sunken);
128
129 drawContents( &painter );
130 if (paintRect.contains(cursorRect))
131 {
132 bitBlt( &store, 0, 0, this, px - STORE_W, py - STORE_W,
133 STORE_W2, STORE_W2, CopyROP );
134 drawCursor( &painter, px, py );
135 }
136 else if (paintRect.intersects(cursorRect))
137 {
138 repaint( cursorRect, false);
139 }
140
141 painter.end();
142}
143
144void KXYSelector::mousePressEvent( TQMouseEvent *e )
145{
146 mouseMoveEvent(e);
147}
148
149void KXYSelector::mouseMoveEvent( TQMouseEvent *e )
150{
151 int xVal, yVal;
152
153 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
154 valuesFromPosition( e->pos().x() - w, e->pos().y() - w, xVal, yVal );
155
156 setValues( xVal, yVal );
157
158 emit valueChanged( xPos, yPos );
159}
160
161void KXYSelector::wheelEvent( TQWheelEvent *e )
162{
163 if ( e->orientation() == TQt::Horizontal )
164 setValues( xValue() + e->delta()/120, yValue() );
165 else
166 setValues( xValue(), yValue() + e->delta()/120 );
167
168 emit valueChanged( xPos, yPos );
169}
170
171void KXYSelector::valuesFromPosition( int x, int y, int &xVal, int &yVal ) const
172{
173 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
174 if (w < 5) w = 5;
175 xVal = ( (maxX-minX) * (x-w) ) / ( width()-2*w );
176 yVal = maxY - ( ( (maxY-minY) * (y-w) ) / ( height()-2*w ) );
177
178 if ( xVal > maxX )
179 xVal = maxX;
180 else if ( xVal < minX )
181 xVal = minX;
182
183 if ( yVal > maxY )
184 yVal = maxY;
185 else if ( yVal < minY )
186 yVal = minY;
187}
188
189void KXYSelector::setPosition( int xp, int yp )
190{
191 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
192 if (w < 5) w = 5;
193 if ( xp < w )
194 xp = w;
195 else if ( xp > width() - w )
196 xp = width() - w;
197
198 if ( yp < w )
199 yp = w;
200 else if ( yp > height() - w )
201 yp = height() - w;
202
203 TQPainter painter;
204 painter.begin( this );
205
206 bitBlt( this, px - STORE_W, py - STORE_W, &store, 0, 0,
207 STORE_W2, STORE_W2, CopyROP );
208 bitBlt( &store, 0, 0, this, xp - STORE_W, yp - STORE_W,
209 STORE_W2, STORE_W2, CopyROP );
210 drawCursor( &painter, xp, yp );
211 px = xp;
212 py = yp;
213
214 painter.end();
215}
216
217void KXYSelector::drawContents( TQPainter * )
218{}
219
220
221void KXYSelector::drawCursor( TQPainter *p, int xp, int yp )
222{
223 p->setPen( TQPen( white ) );
224
225 p->drawLine( xp - 6, yp - 6, xp - 2, yp - 2 );
226 p->drawLine( xp - 6, yp + 6, xp - 2, yp + 2 );
227 p->drawLine( xp + 6, yp - 6, xp + 2, yp - 2 );
228 p->drawLine( xp + 6, yp + 6, xp + 2, yp + 2 );
229}
230
231//-----------------------------------------------------------------------------
232/*
233 * 1D value selector with contents drawn by derived class.
234 * See KColorDialog for example.
235 */
236
237
238TDESelector::TDESelector( TQWidget *parent, const char *name )
239 : TQWidget( parent, name ), TQRangeControl()
240{
241 _orientation = TQt::Horizontal;
242 _indent = true;
243}
244
245TDESelector::TDESelector( Orientation o, TQWidget *parent, const char *name )
246 : TQWidget( parent, name ), TQRangeControl()
247{
248 _orientation = o;
249 _indent = true;
250}
251
252
253TDESelector::~TDESelector()
254{}
255
256
257TQRect TDESelector::contentsRect() const
258{
259 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
260 int iw = (w < 5) ? 5 : w;
261 if ( orientation() == TQt::Vertical )
262 return TQRect( w, iw, width() - w * 2 - 5, height() - 2 * iw );
263 else
264 return TQRect( iw, w, width() - 2 * iw, height() - w * 2 - 5 );
265}
266
267void TDESelector::paintEvent( TQPaintEvent * )
268{
269 TQPainter painter;
270 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
271 int iw = (w < 5) ? 5 : w;
272
273 painter.begin( this );
274
275 drawContents( &painter );
276
277 if ( indent() )
278 {
279 TQRect r = rect();
280 if ( orientation() == TQt::Vertical )
281 r.addCoords(0, iw - w, -iw, w - iw);
282 else
283 r.addCoords(iw - w, 0, w - iw, -iw);
284 style().drawPrimitive(TQStyle::PE_Panel, &painter,
285 r, colorGroup(),
286 TQStyle::Style_Sunken);
287 }
288
289 TQPoint pos = calcArrowPos( value() );
290 drawArrow( &painter, true, pos );
291
292 painter.end();
293}
294
295void TDESelector::mousePressEvent( TQMouseEvent *e )
296{
297 moveArrow( e->pos() );
298}
299
300void TDESelector::mouseMoveEvent( TQMouseEvent *e )
301{
302 moveArrow( e->pos() );
303}
304
305void TDESelector::wheelEvent( TQWheelEvent *e )
306{
307 int val = value() + e->delta()/120;
308 setValue( val );
309}
310
311void TDESelector::valueChange()
312{
313 TQPainter painter;
314 TQPoint pos;
315
316 painter.begin( this );
317
318 pos = calcArrowPos( prevValue() );
319 drawArrow( &painter, false, pos );
320
321 pos = calcArrowPos( value() );
322 drawArrow( &painter, true, pos );
323
324 painter.end();
325
326 emit valueChanged( value() );
327}
328
329void TDESelector::moveArrow( const TQPoint &pos )
330{
331 int val;
332 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
333 int iw = (w < 5) ? 5 : w;
334
335 if ( orientation() == TQt::Vertical )
336 val = ( maxValue() - minValue() ) * (height()-pos.y()-5+w)
337 / (height()-iw*2) + minValue();
338 else
339 val = ( maxValue() - minValue() ) * (width()-pos.x()-5+w)
340 / (width()-iw*2) + minValue();
341
342 setValue( val );
343}
344
345TQPoint TDESelector::calcArrowPos( int val )
346{
347 TQPoint p;
348
349 int w = style().pixelMetric(TQStyle::PM_DefaultFrameWidth);
350 int iw = (w < 5) ? 5 : w;
351 if ( orientation() == TQt::Vertical )
352 {
353 p.setY( height() - ( (height()-2*iw) * val
354 / ( maxValue() - minValue() ) + 5 ) );
355 p.setX( width() - 5 );
356 }
357 else
358 {
359 p.setX( width() - ( (width()-2*iw) * val
360 / ( maxValue() - minValue() ) + 5 ) );
361 p.setY( height() - 5 );
362 }
363
364 return p;
365}
366
367void TDESelector::drawContents( TQPainter * )
368{}
369
370void TDESelector::drawArrow( TQPainter *painter, bool show, const TQPoint &pos )
371{
372 if ( show )
373 {
374 TQPointArray array(3);
375
376 painter->setPen( TQPen() );
377 painter->setBrush( TQBrush( colorGroup().buttonText() ) );
378 array.setPoint( 0, pos.x()+0, pos.y()+0 );
379 array.setPoint( 1, pos.x()+5, pos.y()+5 );
380 if ( orientation() == TQt::Vertical )
381 {
382 array.setPoint( 2, pos.x()+5, pos.y()-5 );
383 }
384 else
385 {
386 array.setPoint( 2, pos.x()-5, pos.y()+5 );
387 }
388
389 painter->drawPolygon( array );
390 }
391 else
392 {
393 if ( orientation() == TQt::Vertical )
394 {
395 repaint(pos.x(), pos.y()-5, 6, 11, true);
396 }
397 else
398 {
399 repaint(pos.x()-5, pos.y(), 11, 6, true);
400 }
401 }
402}
403
404//----------------------------------------------------------------------------
405
406KGradientSelector::KGradientSelector( TQWidget *parent, const char *name )
407 : TDESelector( parent, name )
408{
409 init();
410}
411
412
413KGradientSelector::KGradientSelector( Orientation o, TQWidget *parent,
414 const char *name )
415 : TDESelector( o, parent, name )
416{
417 init();
418}
419
420
421KGradientSelector::~KGradientSelector()
422{}
423
424
425void KGradientSelector::init()
426{
427 color1.setRgb( 0, 0, 0 );
428 color2.setRgb( 255, 255, 255 );
429
430 text1 = text2 = "";
431}
432
433
434void KGradientSelector::drawContents( TQPainter *painter )
435{
436 TQImage image( contentsRect().width(), contentsRect().height(), 32 );
437
438 TQColor col;
439 float scale;
440
441 int redDiff = color2.red() - color1.red();
442 int greenDiff = color2.green() - color1.green();
443 int blueDiff = color2.blue() - color1.blue();
444
445 if ( orientation() == TQt::Vertical )
446 {
447 for ( int y = 0; y < image.height(); y++ )
448 {
449 scale = 1.0 * y / image.height();
450 col.setRgb( color1.red() + int(redDiff*scale),
451 color1.green() + int(greenDiff*scale),
452 color1.blue() + int(blueDiff*scale) );
453
454 unsigned int *p = (uint *) image.scanLine( y );
455 for ( int x = 0; x < image.width(); x++ )
456 *p++ = col.rgb();
457 }
458 }
459 else
460 {
461 unsigned int *p = (uint *) image.scanLine( 0 );
462
463 for ( int x = 0; x < image.width(); x++ )
464 {
465 scale = 1.0 * x / image.width();
466 col.setRgb( color1.red() + int(redDiff*scale),
467 color1.green() + int(greenDiff*scale),
468 color1.blue() + int(blueDiff*scale) );
469 *p++ = col.rgb();
470 }
471
472 for ( int y = 1; y < image.height(); y++ )
473 memcpy( image.scanLine( y ), image.scanLine( y - 1),
474 sizeof( unsigned int ) * image.width() );
475 }
476
477 TQColor ditherPalette[8];
478
479 for ( int s = 0; s < 8; s++ )
480 ditherPalette[s].setRgb( color1.red() + redDiff * s / 8,
481 color1.green() + greenDiff * s / 8,
482 color1.blue() + blueDiff * s / 8 );
483
484 KImageEffect::dither( image, ditherPalette, 8 );
485
486 TQPixmap p;
487 p.convertFromImage( image );
488
489 painter->drawPixmap( contentsRect().x(), contentsRect().y(), p );
490
491 if ( orientation() == TQt::Vertical )
492 {
493 int yPos = contentsRect().top() + painter->fontMetrics().ascent() + 2;
494 int xPos = contentsRect().left() + (contentsRect().width() -
495 painter->fontMetrics().width( text2 )) / 2;
496 TQPen pen( color2 );
497 painter->setPen( pen );
498 painter->drawText( xPos, yPos, text2 );
499
500 yPos = contentsRect().bottom() - painter->fontMetrics().descent() - 2;
501 xPos = contentsRect().left() + (contentsRect().width() -
502 painter->fontMetrics().width( text1 )) / 2;
503 pen.setColor( color1 );
504 painter->setPen( pen );
505 painter->drawText( xPos, yPos, text1 );
506 }
507 else
508 {
509 int yPos = contentsRect().bottom()-painter->fontMetrics().descent()-2;
510
511 TQPen pen( color2 );
512 painter->setPen( pen );
513 painter->drawText( contentsRect().left() + 2, yPos, text1 );
514
515 pen.setColor( color1 );
516 painter->setPen( pen );
517 painter->drawText( contentsRect().right() -
518 painter->fontMetrics().width( text2 ) - 2, yPos, text2 );
519 }
520}
521
522//-----------------------------------------------------------------------------
523
524void KXYSelector::virtual_hook( int, void* )
525{ /*BASE::virtual_hook( id, data );*/ }
526
527void TDESelector::virtual_hook( int, void* )
528{ /*BASE::virtual_hook( id, data );*/ }
529
530void KGradientSelector::virtual_hook( int id, void* data )
531{ TDESelector::virtual_hook( id, data ); }
532
533#include "tdeselect.moc"
534
KGradientSelector::KGradientSelector
KGradientSelector(TQWidget *parent=0, const char *name=0)
Constructs a horizontal color selector which contains a gradient between white and black.
Definition: tdeselect.cpp:406
KGradientSelector::~KGradientSelector
~KGradientSelector()
Destructs the widget.
Definition: tdeselect.cpp:421
KGradientSelector::drawContents
virtual void drawContents(TQPainter *)
Override this function to draw the contents of the control.
Definition: tdeselect.cpp:434
KImageEffect::dither
static TQImage & dither(TQImage &image, const TQColor *palette, int size)
KXYSelector::setRange
void setRange(int minX, int minY, int maxX, int maxY)
Sets the range of possible values.
Definition: tdeselect.cpp:54
KXYSelector::contentsRect
TQRect contentsRect() const
Definition: tdeselect.cpp:99
KXYSelector::drawCursor
virtual void drawCursor(TQPainter *p, int xp, int yp)
Override this function to draw the cursor which indicates the currently selected value pair.
Definition: tdeselect.cpp:221
KXYSelector::setValues
void setValues(int xPos, int yPos)
Sets the current values in horizontal and vertical direction.
Definition: tdeselect.cpp:75
KXYSelector::setXValue
void setXValue(int xPos)
Sets the current horizontal value.
Definition: tdeselect.cpp:65
KXYSelector::valuesFromPosition
void valuesFromPosition(int x, int y, int &xVal, int &yVal) const
Converts a pixel position to its corresponding values.
Definition: tdeselect.cpp:171
KXYSelector::xValue
int xValue() const
Definition: tdeselect.h:88
KXYSelector::~KXYSelector
~KXYSelector()
Destructs the widget.
Definition: tdeselect.cpp:50
KXYSelector::drawContents
virtual void drawContents(TQPainter *)
Override this function to draw the contents of the widget.
Definition: tdeselect.cpp:217
KXYSelector::KXYSelector
KXYSelector(TQWidget *parent=0, const char *name=0)
Constructs a two-dimensional selector widget which has a value range of [0..100] in both directions.
Definition: tdeselect.cpp:36
KXYSelector::yValue
int yValue() const
Definition: tdeselect.h:92
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: tdeselect.cpp:70
TDESelector
TDESelector is the base class for other widgets which provides the ability to choose from a one-dimen...
Definition: tdeselect.h:160
TDESelector::orientation
Orientation orientation() const
Definition: tdeselect.h:184
TDESelector::valueChanged
void valueChanged(int value)
This signal is emitted whenever the user chooses a value, e.g.
TDESelector::value
int value() const
Definition: tdeselect.h:213
TDESelector::drawContents
virtual void drawContents(TQPainter *)
Override this function to draw the contents of the control.
Definition: tdeselect.cpp:367
TDESelector::setValue
void setValue(int value)
Sets the value.
Definition: tdeselect.h:207
TDESelector::indent
bool indent() const
Definition: tdeselect.h:201
TDESelector::TDESelector
TDESelector(TQWidget *parent=0, const char *name=0)
Constructs a horizontal one-dimensional selection widget.
Definition: tdeselect.cpp:238
TDESelector::contentsRect
TQRect contentsRect() const
Definition: tdeselect.cpp:257
TDESelector::maxValue
int maxValue() const
Definition: tdeselect.h:237
TDESelector::drawArrow
virtual void drawArrow(TQPainter *painter, bool show, const TQPoint &pos)
Override this function to draw the cursor which indicates the current value.
Definition: tdeselect.cpp:370
TDESelector::minValue
int minValue() const
Definition: tdeselect.h:225

tdeui

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

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.4
This website is maintained by Timothy Pearson.