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

tdeui

  • tdeui
kled.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1998 J�rg Habenicht (j.habenicht@europemail.com)
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 
21 #define PAINT_BENCH
22 #undef PAINT_BENCH
23 
24 #ifdef PAINT_BENCH
25 #include <tqdatetime.h>
26 #include <stdio.h>
27 #endif
28 
29 
30 #include <tqpainter.h>
31 #include <tqimage.h>
32 #include <tqcolor.h>
33 #include <tdeapplication.h>
34 #include <kpixmapeffect.h>
35 #include "kled.h"
36 
37 
38 class KLed::KLedPrivate
39 {
40  friend class KLed;
41 
42  int dark_factor;
43  TQColor offcolor;
44  TQPixmap *off_map;
45  TQPixmap *on_map;
46 };
47 
48 
49 
50 KLed::KLed(TQWidget *parent, const char *name)
51  : TQWidget( parent, name),
52  led_state(On),
53  led_look(Raised),
54  led_shape(Circular)
55 {
56  TQColor col(green);
57  d = new KLed::KLedPrivate;
58  d->dark_factor = 300;
59  d->offcolor = col.dark(300);
60  d->off_map = 0;
61  d->on_map = 0;
62 
63  setColor(col);
64 }
65 
66 
67 KLed::KLed(const TQColor& col, TQWidget *parent, const char *name)
68  : TQWidget( parent, name),
69  led_state(On),
70  led_look(Raised),
71  led_shape(Circular)
72 {
73  d = new KLed::KLedPrivate;
74  d->dark_factor = 300;
75  d->offcolor = col.dark(300);
76  d->off_map = 0;
77  d->on_map = 0;
78 
79  setColor(col);
80  //setShape(Circular);
81 }
82 
83 KLed::KLed(const TQColor& col, KLed::State state,
84  KLed::Look look, KLed::Shape shape, TQWidget *parent, const char *name )
85  : TQWidget(parent, name),
86  led_state(state),
87  led_look(look),
88  led_shape(shape)
89 {
90  d = new KLed::KLedPrivate;
91  d->dark_factor = 300;
92  d->offcolor = col.dark(300);
93  d->off_map = 0;
94  d->on_map = 0;
95 
96  //setShape(shape);
97  setColor(col);
98 }
99 
100 
101 KLed::~KLed()
102 {
103  delete d->off_map;
104  delete d->on_map;
105  delete d;
106 }
107 
108 void
109 KLed::paintEvent(TQPaintEvent *)
110 {
111 #ifdef PAINT_BENCH
112  const int rounds = 1000;
113  TQTime t;
114  t.start();
115  for (int i=0; i<rounds; i++) {
116 #endif
117  switch(led_shape)
118  {
119  case Rectangular:
120  switch (led_look)
121  {
122  case Sunken :
123  paintRectFrame(false);
124  break;
125  case Raised :
126  paintRectFrame(true);
127  break;
128  case Flat :
129  paintRect();
130  break;
131  default :
132  tqWarning("%s: in class KLed: no KLed::Look set",tqApp->argv()[0]);
133  }
134  break;
135  case Circular:
136  switch (led_look)
137  {
138  case Flat :
139  paintFlat();
140  break;
141  case Raised :
142  paintRound();
143  break;
144  case Sunken :
145  paintSunken();
146  break;
147  default:
148  tqWarning("%s: in class KLed: no KLed::Look set",tqApp->argv()[0]);
149  }
150  break;
151  default:
152  tqWarning("%s: in class KLed: no KLed::Shape set",tqApp->argv()[0]);
153  break;
154  }
155 #ifdef PAINT_BENCH
156  }
157  int ready = t.elapsed();
158  tqWarning("elapsed: %d msec. for %d rounds", ready, rounds);
159 #endif
160 }
161 
162 int
163 KLed::ensureRoundLed()
164 {
165  // Initialize coordinates, width, and height of the LED
166  //
167  int width = this->width();
168  // Make sure the LED is round!
169  if (width > this->height())
170  width = this->height();
171  width -= 2; // leave one pixel border
172  if (width < 0)
173  width = 0;
174 
175  return width;
176 }
177 
178 bool
179 KLed::paintCachedPixmap()
180 {
181  if (led_state) {
182  if (d->on_map) {
183  TQPainter paint(this);
184  paint.drawPixmap(0, 0, *d->on_map);
185  return true;
186  }
187  } else {
188  if (d->off_map) {
189  TQPainter paint(this);
190  paint.drawPixmap(0, 0, *d->off_map);
191  return true;
192  }
193  }
194 
195  return false;
196 }
197 
198 void
199 KLed::paintFlat() // paint a ROUND FLAT led lamp
200 {
201  if (paintCachedPixmap()) return;
202 
203  TQPainter paint;
204  TQColor color;
205  TQBrush brush;
206  TQPen pen;
207 
208  int width = ensureRoundLed();
209 
210 
211  int scale = 3;
212  TQPixmap *tmpMap = 0;
213 
214  width *= scale;
215 
216  tmpMap = new TQPixmap(width + 6, width + 6);
217  tmpMap->fill(paletteBackgroundColor());
218 
219  // start painting widget
220  //
221  paint.begin(tmpMap);
222 
223  // Set the color of the LED according to given parameters
224  color = ( led_state ) ? led_color : d->offcolor;
225 
226  // Set the brush to SolidPattern, this fills the entire area
227  // of the ellipse which is drawn with a thin gray "border" (pen)
228  brush.setStyle( TQt::SolidPattern );
229  brush.setColor( color );
230 
231  pen.setWidth( scale );
232  color = colorGroup().dark();
233  pen.setColor( color ); // Set the pen accordingly
234 
235  paint.setPen( pen ); // Select pen for drawing
236  paint.setBrush( brush ); // Assign the brush to the painter
237 
238  // Draws a "flat" LED with the given color:
239  paint.drawEllipse( scale, scale, width - scale * 2, width - scale * 2 );
240 
241  paint.end();
242  //
243  // painting done
244  TQPixmap *&dest = led_state ? d->on_map : d->off_map;
245  TQImage i = tmpMap->convertToImage();
246  width /= 3;
247  i = i.smoothScale(width, width);
248  delete tmpMap;
249  dest = new TQPixmap(i);
250  paint.begin(this);
251  paint.drawPixmap(0, 0, *dest);
252  paint.end();
253 
254 }
255 
256 void
257 KLed::paintRound() // paint a ROUND RAISED led lamp
258 {
259  if (paintCachedPixmap()) return;
260 
261  TQPainter paint;
262  TQColor color;
263  TQBrush brush;
264  TQPen pen;
265 
266  // Initialize coordinates, width, and height of the LED
267  int width = ensureRoundLed();
268 
269  int scale = 3;
270  TQPixmap *tmpMap = 0;
271 
272  width *= scale;
273 
274  tmpMap = new TQPixmap(width + 6, width + 6);
275  tmpMap->fill(paletteBackgroundColor());
276  paint.begin(tmpMap);
277 
278  // Set the color of the LED according to given parameters
279  color = ( led_state ) ? led_color : d->offcolor;
280 
281  // Set the brush to SolidPattern, this fills the entire area
282  // of the ellipse which is drawn first
283  brush.setStyle( TQt::SolidPattern );
284  brush.setColor( color );
285  paint.setBrush( brush ); // Assign the brush to the painter
286 
287  // Draws a "flat" LED with the given color:
288  paint.drawEllipse( scale, scale, width - scale*2, width - scale*2 );
289 
290  // Draw the bright light spot of the LED now, using modified "old"
291  // painter routine taken from TDEUI�s KLed widget:
292 
293  // Setting the new width of the pen is essential to avoid "pixelized"
294  // shadow like it can be observed with the old LED code
295  pen.setWidth( 2 * scale );
296 
297  // shrink the light on the LED to a size about 2/3 of the complete LED
298  int pos = width/5 + 1;
299  int light_width = width;
300  light_width *= 2;
301  light_width /= 3;
302 
303  // Calculate the LED�s "light factor":
304  int light_quote = (130*2/(light_width?light_width:1))+100;
305 
306  // Now draw the bright spot on the LED:
307  while (light_width) {
308  color = color.light( light_quote ); // make color lighter
309  pen.setColor( color ); // set color as pen color
310  paint.setPen( pen ); // select the pen for drawing
311  paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle)
312  light_width--;
313  if (!light_width)
314  break;
315  paint.drawEllipse( pos, pos, light_width, light_width );
316  light_width--;
317  if (!light_width)
318  break;
319  paint.drawEllipse( pos, pos, light_width, light_width );
320  pos++; light_width--;
321  }
322 
323  // Drawing of bright spot finished, now draw a thin gray border
324  // around the LED; it looks nicer that way. We do this here to
325  // avoid that the border can be erased by the bright spot of the LED
326 
327  pen.setWidth( 2 * scale + 1 );
328  color = colorGroup().dark();
329  pen.setColor( color ); // Set the pen accordingly
330  paint.setPen( pen ); // Select pen for drawing
331  brush.setStyle( TQt::NoBrush ); // Switch off the brush
332  paint.setBrush( brush ); // This avoids filling of the ellipse
333 
334  paint.drawEllipse( 2, 2, width, width );
335 
336  paint.end();
337  //
338  // painting done
339  TQPixmap *&dest = led_state ? d->on_map : d->off_map;
340  TQImage i = tmpMap->convertToImage();
341  width /= 3;
342  i = i.smoothScale(width, width);
343  delete tmpMap;
344  dest = new TQPixmap(i);
345  paint.begin(this);
346  paint.drawPixmap(0, 0, *dest);
347  paint.end();
348 
349 }
350 
351 void
352 KLed::paintSunken() // paint a ROUND SUNKEN led lamp
353 {
354  if (paintCachedPixmap()) return;
355 
356  TQPainter paint;
357  TQColor color;
358  TQBrush brush;
359  TQPen pen;
360 
361  // First of all we want to know what area should be updated
362  // Initialize coordinates, width, and height of the LED
363  int width = ensureRoundLed();
364 
365  int scale = 3;
366  TQPixmap *tmpMap = 0;
367 
368  width *= scale;
369 
370  tmpMap = new TQPixmap(width, width);
371  tmpMap->fill(paletteBackgroundColor());
372  paint.begin(tmpMap);
373 
374  // Set the color of the LED according to given parameters
375  color = ( led_state ) ? led_color : d->offcolor;
376 
377  // Set the brush to SolidPattern, this fills the entire area
378  // of the ellipse which is drawn first
379  brush.setStyle( TQt::SolidPattern );
380  brush.setColor( color );
381  paint.setBrush( brush ); // Assign the brush to the painter
382 
383  // Draws a "flat" LED with the given color:
384  paint.drawEllipse( scale, scale, width - scale*2, width - scale*2 );
385 
386  // Draw the bright light spot of the LED now, using modified "old"
387  // painter routine taken from TDEUI�s KLed widget:
388 
389  // Setting the new width of the pen is essential to avoid "pixelized"
390  // shadow like it can be observed with the old LED code
391  pen.setWidth( 2 * scale );
392 
393  // shrink the light on the LED to a size about 2/3 of the complete LED
394  int pos = width/5 + 1;
395  int light_width = width;
396  light_width *= 2;
397  light_width /= 3;
398 
399  // Calculate the LED�s "light factor":
400  int light_quote = (130*2/(light_width?light_width:1))+100;
401 
402  // Now draw the bright spot on the LED:
403  while (light_width) {
404  color = color.light( light_quote ); // make color lighter
405  pen.setColor( color ); // set color as pen color
406  paint.setPen( pen ); // select the pen for drawing
407  paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle)
408  light_width--;
409  if (!light_width)
410  break;
411  paint.drawEllipse( pos, pos, light_width, light_width );
412  light_width--;
413  if (!light_width)
414  break;
415  paint.drawEllipse( pos, pos, light_width, light_width );
416  pos++; light_width--;
417  }
418 
419  // Drawing of bright spot finished, now draw a thin border
420  // around the LED which resembles a shadow with light coming
421  // from the upper left.
422 
423  pen.setWidth( 2 * scale + 1 ); // ### shouldn't this value be smaller for smaller LEDs?
424  brush.setStyle( (TQt::BrushStyle)NoBrush ); // Switch off the brush
425  paint.setBrush( brush ); // This avoids filling of the ellipse
426 
427  // Set the initial color value to colorGroup().light() (bright) and start
428  // drawing the shadow border at 45� (45*16 = 720).
429 
430  int angle = -720;
431  color = colorGroup().light();
432 
433  for ( int arc = 120; arc < 2880; arc += 240 ) {
434  pen.setColor( color );
435  paint.setPen( pen );
436  int w = width - pen.width()/2 - scale + 1;
437  paint.drawArc( pen.width()/2, pen.width()/2, w, w, angle + arc, 240 );
438  paint.drawArc( pen.width()/2, pen.width()/2, w, w, angle - arc, 240 );
439  color = color.dark( 110 ); //FIXME: this should somehow use the contrast value
440  } // end for ( angle = 720; angle < 6480; angle += 160 )
441 
442  paint.end();
443  //
444  // painting done
445 
446  TQPixmap *&dest = led_state ? d->on_map : d->off_map;
447  TQImage i = tmpMap->convertToImage();
448  width /= 3;
449  i = i.smoothScale(width, width);
450  delete tmpMap;
451  dest = new TQPixmap(i);
452  paint.begin(this);
453  paint.drawPixmap(0, 0, *dest);
454  paint.end();
455 
456 }
457 
458 void
459 KLed::paintRect()
460 {
461  TQPainter painter(this);
462  TQBrush lightBrush(led_color);
463  TQBrush darkBrush(d->offcolor);
464  TQPen pen(led_color.dark(300));
465  int w=width();
466  int h=height();
467  // -----
468  switch(led_state)
469  {
470  case On:
471  painter.setBrush(lightBrush);
472  painter.drawRect(0, 0, w, h);
473  break;
474  case Off:
475  painter.setBrush(darkBrush);
476  painter.drawRect(0, 0, w, h);
477  painter.setPen(pen);
478  painter.drawLine(0, 0, w, 0);
479  painter.drawLine(0, h-1, w, h-1);
480  // Draw verticals
481  int i;
482  for(i=0; i < w; i+= 4 /* dx */)
483  painter.drawLine(i, 1, i, h-1);
484  break;
485  default: break;
486  }
487 }
488 
489 void
490 KLed::paintRectFrame(bool raised)
491 {
492  TQPainter painter(this);
493  TQBrush lightBrush(led_color);
494  TQBrush darkBrush(d->offcolor);
495  int w=width();
496  int h=height();
497  TQColor black=TQt::black;
498  TQColor white=TQt::white;
499  // -----
500  if(raised)
501  {
502  painter.setPen(white);
503  painter.drawLine(0, 0, 0, h-1);
504  painter.drawLine(1, 0, w-1, 0);
505  painter.setPen(black);
506  painter.drawLine(1, h-1, w-1, h-1);
507  painter.drawLine(w-1, 1, w-1, h-1);
508  painter.fillRect(1, 1, w-2, h-2,
509  (led_state==On)? lightBrush : darkBrush);
510  } else {
511  painter.setPen(black);
512  painter.drawRect(0,0,w,h);
513  painter.drawRect(0,0,w-1,h-1);
514  painter.setPen(white);
515  painter.drawRect(1,1,w-1,h-1);
516  painter.fillRect(2, 2, w-4, h-4,
517  (led_state==On)? lightBrush : darkBrush);
518  }
519 }
520 
521 KLed::State
522 KLed::state() const
523 {
524  return led_state;
525 }
526 
527 KLed::Shape
528 KLed::shape() const
529 {
530  return led_shape;
531 }
532 
533 TQColor
534 KLed::color() const
535 {
536  return led_color;
537 }
538 
539 TQColor
540 KLed::offColor() const
541 {
542  return led_off_color;
543 }
544 
545 KLed::Look
546 KLed::look() const
547 {
548  return led_look;
549 }
550 
551 void
552 KLed::setState( State state )
553 {
554  if (led_state != state)
555  {
556  led_state = state;
557  update();
558  }
559 }
560 
561 void
562 KLed::toggleState()
563 {
564  toggle();
565 }
566 
567 void
568 KLed::setShape(KLed::Shape s)
569 {
570  if(led_shape!=s)
571  {
572  led_shape = s;
573  update();
574  }
575 }
576 
577 void
578 KLed::setColor(const TQColor& col)
579 {
580  if(led_color!=col) {
581  if(d->on_map) { delete d->on_map; d->on_map = 0; }
582  if(d->off_map) { delete d->off_map; d->off_map = 0; }
583  led_color = col;
584  d->offcolor = col.dark(d->dark_factor);
585  update();
586  }
587 }
588 
589 void
590 KLed::setOffColor(const TQColor& col)
591 {
592  if(led_off_color!=col) {
593  if(d->on_map) { delete d->on_map; d->on_map = 0; }
594  if(d->off_map) { delete d->off_map; d->off_map = 0; }
595  d->offcolor = col;
596  update();
597  }
598 }
599 
600 void
601 KLed::setDarkFactor(int darkfactor)
602 {
603  if (d->dark_factor != darkfactor) {
604  d->dark_factor = darkfactor;
605  d->offcolor = led_color.dark(darkfactor);
606  update();
607  }
608 }
609 
610 int
611 KLed::darkFactor() const
612 {
613  return d->dark_factor;
614 }
615 
616 void
617 KLed::setLook( Look look )
618 {
619  if(led_look!=look)
620  {
621  led_look = look;
622  update();
623  }
624 }
625 
626 void
627 KLed::toggle()
628 {
629  led_state = (led_state == On) ? Off : On;
630  // setColor(led_color);
631  update();
632 }
633 
634 void
635 KLed::on()
636 {
637  setState(On);
638 }
639 
640 void
641 KLed::off()
642 {
643  setState(Off);
644 }
645 
646 TQSize
647 KLed::sizeHint() const
648 {
649  return TQSize(16, 16);
650 }
651 
652 TQSize
653 KLed::minimumSizeHint() const
654 {
655  return TQSize(16, 16 );
656 }
657 
658 void KLed::virtual_hook( int, void* )
659 { /*BASE::virtual_hook( id, data );*/ }
660 
661 #include "kled.moc"
KLed
An LED widget.
Definition: kled.h:46
KLed::on
void on()
Sets the state of the widget to On.
Definition: kled.cpp:635
KLed::State
State
Status of the light is on/off.
Definition: kled.h:62
KLed::Shape
Shape
Shades of the lamp.
Definition: kled.h:68
KLed::paintRectFrame
virtual void paintRectFrame(bool raised)
Paints a rectangular LED, either raised or sunken, depending on its argument.
Definition: kled.cpp:490
KLed::setOffColor
void setOffColor(const TQColor &color)
Set the off color of the widget.
Definition: kled.cpp:590
KLed::setDarkFactor
void setDarkFactor(int darkfactor)
Sets the factor to darken the LED in OFF state.
Definition: kled.cpp:601
KLed::paintRect
virtual void paintRect()
Paints a rectangular, flat LED.
Definition: kled.cpp:459
KLed::~KLed
~KLed()
Destructor.
Definition: kled.cpp:101
KLed::toggleState
void toggleState() TDE_DEPRECATED
Toggle the state of the LED from Off to On and vice versa.
Definition: kled.cpp:562
KLed::ensureRoundLed
int ensureRoundLed()
Compute LED width.
Definition: kled.cpp:163
KLed::Look
Look
Displays a flat, round or sunken LED.
Definition: kled.h:90
KLed::state
State state() const
Returns the current state of the widget (on/off).
Definition: kled.cpp:522
KLed::setColor
void setColor(const TQColor &color)
Set the color of the widget.
Definition: kled.cpp:578
KLed::color
TQColor color() const
Returns the color of the widget.
Definition: kled.cpp:534
KLed::paintRound
virtual void paintRound()
Paints a circular, raised LED.
Definition: kled.cpp:257
KLed::off
void off()
Sets the state of the widget to Off.
Definition: kled.cpp:641
KLed::look
Look look() const
Returns the look of the widget.
Definition: kled.cpp:546
KLed::setState
void setState(State state)
Sets the state of the widget to On or Off.
Definition: kled.cpp:552
KLed::paintFlat
virtual void paintFlat()
Paints a circular, flat LED.
Definition: kled.cpp:199
KLed::paintSunken
virtual void paintSunken()
Paints a circular, sunken LED.
Definition: kled.cpp:352
KLed::toggle
void toggle()
Toggles the state of the led from Off to On or vice versa.
Definition: kled.cpp:627
KLed::KLed
KLed(TQWidget *parent=0, const char *name=0)
Constructs a green, round LED widget which will initially be turned on.
Definition: kled.cpp:50
KLed::offColor
TQColor offColor() const
Returns the off color of the widget.
Definition: kled.cpp:540
KLed::setLook
void setLook(Look look)
Sets the color of the widget.
Definition: kled.cpp:617
KLed::setShape
void setShape(Shape s)
Set the shape of the LED to s.
Definition: kled.cpp:568
KLed::darkFactor
int darkFactor() const
Returns the factor to darken the LED.
Definition: kled.cpp:611
KLed::paintCachedPixmap
bool paintCachedPixmap()
Paint the cached antialiased pixmap corresponding to the state if any.
Definition: kled.cpp:179

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.1
This website is maintained by Timothy Pearson.