summaryrefslogtreecommitdiffstats
path: root/ksim/library/led.cpp
blob: f2acc2ba355789226d61634916423146d22175d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*  ksim - a system monitor for kde
 *
 *  Copyright (C) 2001  Robbie Ward <linuxphreak@gmx.co.uk>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "led.h"
#include "led.moc"

#include <tqimage.h>
#include <tqpainter.h>
#include <tqfile.h>
#include <tqbitmap.h>

#include <kdebug.h>
#include <kpixmapsplitter.h>

#include <themeloader.h>
#include "themetypes.h"

class KSim::Led::Private
{
  public:
    KPixmapSplitter splitter;
    TQPixmap pixmap;
    TQString imageName;
    KSim::Led::Type type;
    bool toggled;
};

KSim::Led::Led() : TQPixmap()
{
  d = new Private;

  d->type = First;
  d->toggled = false;

  setPixmap(KSim::ThemeLoader::self().current().ledPixmap(KSim::Types::Net));
  setOff();
}

KSim::Led::Led(Type type, const TQString &imageName) : TQPixmap()
{
  d = new Private;

  d->type = type;
  d->toggled = false;

  setPixmap(imageName);
  setOff();
}

KSim::Led::~Led()
{
  delete d;
}

void KSim::Led::setOn(bool force)
{
  if (isOn() && !force)
    return;

  // Return if our pixmap is null
  TQRect rect = d->splitter.coordinates(d->type == First ? 1 : 3);
  if (d->pixmap.isNull() || rect.isEmpty()) {
    resize(12, 8);
    fill(TQt::white);
    return;
  }

  if (d->pixmap.tqmask() && !d->pixmap.tqmask()->isNull()) {
    TQBitmap tqmask(rect.size());
    bitBlt(&tqmask, TQPoint(0, 0), d->pixmap.tqmask(), rect, CopyROP);
    setMask(tqmask);
  }

  bitBlt(this, TQPoint(0, 0), &d->pixmap, rect, CopyROP);
  d->toggled = true;
}

void KSim::Led::setOff(bool force)
{
  if (!isOn() && !force)
    return;

  // Return if our pixmap is null
  TQRect rect = d->splitter.coordinates(d->type == First ? 0 : 2);
  if (d->pixmap.isNull() || rect.isEmpty()) {
    resize(12, 8);
    fill(TQt::white);
    return;
  }

  if (d->pixmap.tqmask() && !d->pixmap.tqmask()->isNull()) {
    TQBitmap tqmask(rect.size());
    bitBlt(&tqmask, TQPoint(0, 0), d->pixmap.tqmask(), rect, CopyROP);
    setMask(tqmask);
  }

  bitBlt(this, TQPoint(0, 0), &d->pixmap, rect, CopyROP);
  d->toggled = false;
}

void KSim::Led::toggle()
{
  if (d->toggled)
    setOff();
  else
    setOn();
}

void KSim::Led::setPixmap(const TQString &imageName)
{
  if (imageName == d->imageName)
    return;

  TQImage image(imageName);

  if (image.width() >= 19)
    image = image.smoothScale(19, image.height());

  KSim::ThemeLoader::self().reColourImage(image);
  d->pixmap.convertFromImage(image);
  TQSize size(image.width(), image.height() / 4);

  d->splitter.setPixmap(d->pixmap);
  d->splitter.setItemSize(size);

  resize(size);
  setMask(TQBitmap());
}

const TQString &KSim::Led::fileName() const
{
  return d->imageName;
}

bool KSim::Led::isOn() const
{
  return d->toggled;
}

void KSim::Led::setType(Type type)
{
  if (type == d->type)
    return;

  d->type = type;
}

KSim::Led::Type KSim::Led::type() const
{
  return d->type;
}

void KSim::Led::update()
{
  if (isOn())
    setOn(true);
  else
    setOff(true);
}

class KSim::LedLabel::Private
{
  public:
    KSim::Led receiveLed;
    KSim::Led sendLed;
    TQPoint sendPoint;
    TQPoint receivePoint;
};

KSim::LedLabel::LedLabel(int max, int type, const TQString &label,
   TQWidget *tqparent, const char *name, WFlags fl)
   : KSim::Progress(max, type, Panel, tqparent, name, fl)
{
  init();
  setText(label);
}

KSim::LedLabel::LedLabel(int max, int type,
   TQWidget *tqparent, const char *name, WFlags fl)
   : KSim::Progress(max, type, Panel, tqparent, name, fl)
{
  init();
}

KSim::LedLabel::LedLabel(int max, 
   TQWidget *tqparent, const char *name, WFlags fl)
   : KSim::Progress(max, KSim::Types::None,
   Panel, tqparent, name, fl)
{
  init();
}

KSim::LedLabel::~LedLabel()
{
  delete d;
}

void KSim::LedLabel::configureObject(bool reapaintWidget)
{
  KSim::Progress::configureObject(false);

  TQPixmap pixmap = themeLoader().current().splitPixmap(KSim::Theme::KrellPanel, 0, false);
  if (pixmap.isNull())
    pixmap = themeLoader().current().splitPixmap(KSim::Theme::KrellSlider);

  setMeterPixmap(pixmap);
  d->receiveLed.setPixmap(themeLoader().current().ledPixmap(KSim::Types::Net));
  d->sendLed.setPixmap(themeLoader().current().ledPixmap(KSim::Types::Net));

  // Update the leds to make sure they get painted correctly
  d->receiveLed.update();
  d->sendLed.update();

  setConfigValues();
  layoutLeds();

  if (reapaintWidget)
    update();
}

TQSize KSim::LedLabel::tqsizeHint() const
{
  TQSize hint(Progress::tqsizeHint());

  if (d->sendLed.height() > hint.height())
    hint.setHeight(d->sendLed.height());

  return hint;
}

void KSim::LedLabel::reset()
{
  KSim::Progress::reset();
  setOff(Led::First);
  setOff(Led::Second);
}

void KSim::LedLabel::setOn(Led::Type type)
{
  if (type == Led::First) {
    if (d->receiveLed.isOn())
      return;

    d->receiveLed.setOn();
  }
  else {
    if (d->sendLed.isOn())
      return;

    d->sendLed.setOn();
  }

  update();
}

void KSim::LedLabel::setOff(Led::Type type)
{
  if (type == Led::First) {
    if (!d->receiveLed.isOn())
      return;

    d->receiveLed.setOff();
  }
  else {
    if (!d->sendLed.isOn())
      return;

    d->sendLed.setOff();
  }

  update();
}

void KSim::LedLabel::toggle(Led::Type type)
{
  if (type == Led::First)
    d->receiveLed.toggle();
  else
    d->sendLed.toggle();

  update();
}

void KSim::LedLabel::drawLeds()
{
  bitBlt(this, d->sendPoint, &d->sendLed);
  bitBlt(this, d->receivePoint, &d->receiveLed);
}

void KSim::LedLabel::paintEvent(TQPaintEvent *ev)
{
  KSim::Label::paintEvent(ev);
  drawLeds();
  KSim::Progress::drawMeter();
}

void KSim::LedLabel::resizeEvent(TQResizeEvent *ev)
{
  KSim::Progress::resizeEvent(ev);
  layoutLeds();
}

void KSim::LedLabel::layoutLeds()
{
  int ledHeight = height() / 2;

  // get the location of the sendLed
  d->sendPoint.setX((width() - d->sendLed.width()) - 2);
  d->sendPoint.setY(ledHeight - (d->sendLed.height() / 2));

  // get the location of the receiveLed
  d->receivePoint.setX((d->sendPoint.x() - d->receiveLed.width()) - 3);
  d->receivePoint.setY(ledHeight - (d->receiveLed.height() / 2));

  TQRect location = textLocation();
  location.setWidth(d->receivePoint.x());
  setTextLocation(location);

  TQRect shadow = shadowLocation();
  shadow.setWidth(d->receivePoint.x() + 3);
  setShadowLocation(shadow);

  setOrigin(rect());
}

void KSim::LedLabel::init()
{
  d = new Private;
  d->receiveLed.setType(KSim::Led::First);
  d->sendLed.setType(KSim::Led::Second);
  setConfigString("StylePanel");
  configureObject();
}