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

superkaramba

  • superkaramba
  • src
imagelabel.cpp
1/****************************************************************************
2* imagelabel.cpp - ImageLabel meter
3*
4* Copyright (C) 2003 Hans Karlsson <karlsson.h@home.se>
5* Copyright (c) 2004 Petri Damstén <damu@iki.fi>
6*
7* This file is part of SuperKaramba.
8*
9* SuperKaramba is free software; you can redistribute it and/or modify
10* it under the terms of the GNU General Public License as published by
11* the Free Software Foundation; either version 2 of the License, or
12* (at your option) any later version.
13*
14* SuperKaramba is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License
20* along with SuperKaramba; if not, write to the Free Software
21* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22****************************************************************************/
23
24#include <tqpixmap.h>
25#include <tqtimer.h>
26#include <tqtooltip.h>
27#include <kpixmapeffect.h>
28#include <kdebug.h>
29#include <kimageeffect.h>
30#include <tdetempfile.h>
31#include <tdeio/job.h>
32#include "karambaapp.h"
33#include "imagelabel.h"
34
35// Effect
36Effect::Effect(ImageLabel* img, int msec) :
37 myImage(img)
38{
39 if (msec > 0)
40 {
41 // remove the effect after the given time
42 //TQTimer::singleShot (millisec, myImage, TQ_SLOT(slotEffectExpired()));
43 //timer -> changeInterval(millisec);
44 millisec = msec;
45 }
46 else
47 {
48 millisec = msec;
49 }
50}
51
52Effect::~Effect()
53{
54}
55
56void Effect::startTimer()
57{
58 if (millisec > 0)
59 {
60 TQTimer::singleShot (millisec, myImage, TQ_SLOT(slotEffectExpired()));
61 millisec = 0;
62 }
63}
64
65// Intensity
66Intensity::Intensity(ImageLabel* img, float r, int millisec) :
67 Effect(img, millisec)
68{
69 ratio = r;
70 ratio = (ratio > 1) ? 1 : ratio;
71 ratio = (ratio < -1) ? -1 : ratio;
72}
73
74KPixmap Intensity::apply(KPixmap pixmap)
75{
76 return KPixmapEffect::intensity(pixmap, ratio);
77}
78
79// ChannelIntensity
80ChannelIntensity::ChannelIntensity(ImageLabel* img, float r, TQString c,
81 int millisec) :
82 Effect(img, millisec)
83{
84 ratio = r;
85 ratio = (ratio > 1) ? 1 : ratio;
86 ratio = (ratio < -1) ? -1 : ratio;
87
88 channel = 0;
89 if (c.find("red", 0 , false))
90 {
91 channel = 0;
92 }
93 else if (c.find("green", 0, false))
94 {
95 channel = 1;
96 }
97 else if (c.find("blue", 0, false))
98 {
99 channel = 2;
100 }
101}
102
103KPixmap ChannelIntensity::apply(KPixmap pixmap)
104{
105 return KPixmapEffect::channelIntensity(pixmap, ratio,
106 (KPixmapEffect::RGBComponent)channel);
107}
108
109// ToGray
110ToGray::ToGray(ImageLabel* img, int millisec) : Effect(img, millisec)
111{
112}
113
114KPixmap ToGray::apply(KPixmap pixmap)
115{
116 return KPixmapEffect::toGray(pixmap);
117}
118
119/***********************************************************************/
120
121ImageLabel::ImageLabel(karamba* k, int ix,int iy,int iw,int ih) :
122 Meter(k, ix,iy,iw,ih), zoomed(false), rollover(false)
123{
124 background = 0;
125 cblend = 0;
126 //scaleMat.reset();
127 //rotMat.reset();
128 scale_w = 1;
129 scale_h = 1;
130 rot_angle = 0;
131
132 doScale = false;
133 doRotate = false;
134
135 imageEffect = 0;
136}
137
138ImageLabel::ImageLabel(karamba* k) :
139 Meter(k), zoomed(false), rollover(false)
140{
141 cblend = 0;
142 background = 0;
143}
144
145ImageLabel::~ImageLabel()
146{
147 if (imageEffect != 0)
148 {
149 delete imageEffect;
150 imageEffect = 0;
151 }
152 if(!old_tip_rect.isNull())
153 {
154 TQToolTip::remove(m_karamba, old_tip_rect);
155 }
156}
157
158void ImageLabel::setValue(long v)
159{
160 setValue( TQString::number( v ) );
161}
162
163void ImageLabel::show()
164{
165 Meter::show();
166 setEnabled(true);
167}
168
169void ImageLabel::hide()
170{
171 Meter::hide();
172 setEnabled(false);
173}
174
175void ImageLabel::rotate(int deg)
176{
177 doRotate = !(deg == 0);
178
179 rot_angle = deg;
180
181 applyTransformations();
182}
183
184void ImageLabel::scale(int w, int h)
185{
186 doScale = !(w == realpixmap.width() && h == realpixmap.height());
187
188 scale_w = w;
189 scale_h = h;
190
191 applyTransformations();
192}
193
194void ImageLabel::smoothScale(int w, int h)
195{
196 doScale = !(w == realpixmap.width() && h == realpixmap.height());
197
198 scale_w = w;
199 scale_h = h;
200
201 applyTransformations(true);
202
203// double widthFactor = ((double)w) / ((double)realpixmap.width());
204// double heightFactor = ((double)h) / ((double)realpixmap.height());
205
206// pixmap.convertFromImage(realpixmap.convertToImage().smoothScale(w, h));
207
208// setWidth(pixmap.width());
209// setHeight(pixmap.height());
210
211}
212
213void ImageLabel::removeImageTransformations()
214{
215 doScale = false;
216 doRotate = false;
217
218 scale_w = 1;
219 scale_h = 1;
220 rot_angle = 0;
221 pixmap = realpixmap;
222}
223
224void ImageLabel::applyTransformations(bool useSmoothScale)
225{
226 pixmap = realpixmap;
227 if (doRotate)
228 {
229 // KDE and QT seem to miss a high quality image rotation
230 TQWMatrix rotMat;
231 rotMat.rotate(rot_angle);
232 pixmap = pixmap.xForm(rotMat);
233 }
234 if (doScale)
235 {
236 if (m_karamba -> useSmoothTransforms() || useSmoothScale)
237 {
238 pixmap.convertFromImage(
239 pixmap.convertToImage().smoothScale(scale_w, scale_h));
240 }
241 else
242 {
243 double widthFactor = ((double)scale_w) / ((double)pixmap.width());
244 double heightFactor = ((double)scale_h) / ((double)pixmap.height());
245 TQWMatrix scaleMat;
246 scaleMat.scale(widthFactor, heightFactor);
247 pixmap = pixmap.xForm(scaleMat);
248 }
249 }
250 if (imageEffect != 0)
251 {
252 pixmap = imageEffect -> apply(pixmap);
253 }
254 setWidth(pixmap.width());
255 setHeight(pixmap.height());
256}
257
258void ImageLabel::slotCopyResult(TDEIO::Job* job)
259{
260 TQString tempFile = ((TDEIO::FileCopyJob*)job)->destURL().path();
261 if(job->error() == 0)
262 {
263 setValue(tempFile);
264 imagePath = ((TDEIO::FileCopyJob*)job)->srcURL().path();
265 emit pixmapLoaded();
266 }
267 else
268 {
269 tqWarning("Error downloading (%s): %s", job->errorText().ascii(),
270 tempFile.ascii());
271 }
272 TDEIO::NetAccess::removeTempFile(tempFile);
273}
274
275void ImageLabel::setValue(TQString fn)
276{
277 // use the first line
278 TQStringList sList = TQStringList::split( "\n", fn );
279 TQString fileName = *sList.begin();
280 KURL url(fileName);
281 TQRegExp rx("^[a-zA-Z]{1,5}:/",false);
282 bool protocol = (rx.search(fileName)!=-1)?true:false;
283 TQPixmap pm;
284
285 if(protocol && url.isLocalFile() == false)
286 {
287 KTempFile tmpFile;
288 TDEIO::FileCopyJob* copy = TDEIO::file_copy(fileName, tmpFile.name(), 0600,
289 true, false, false);
290 connect(copy, TQ_SIGNAL(result(TDEIO::Job*)),
291 this, TQ_SLOT(slotCopyResult(TDEIO::Job*)));
292 return;
293 }
294 else
295 {
296 if(m_karamba->theme().isThemeFile(fileName))
297 {
298 TQByteArray ba = m_karamba->theme().readThemeFile(fileName);
299 pm.loadFromData(ba);
300 }
301 else
302 {
303 pm.load(fileName);
304 }
305 imagePath = fileName;
306 }
307 setValue(pm);
308}
309
310//Matthew Kay: a new version of setValue to be used by createTaskIcon()
314void ImageLabel::setValue(TQPixmap& pix)
315{
316 realpixmap = KPixmap(pix);
317 pixmap = realpixmap;
318 setWidth(pixmap.width());
319 setHeight(pixmap.height());
320
321 pixmapWidth = pixmap.width();
322 pixmapHeight = pixmap.height();
323 rect_off = TQRect(getX(),getY(),pixmapWidth,pixmapHeight);
324}
325
326void ImageLabel::mUpdate(TQPainter* p, int backgroundUpdate)
327{
328 if (backgroundUpdate == 1)
329 {
330 //only draw image if not hidden
331 if (hidden == 0)
332 {
333 if (cblend == 0)
334 //draw the pixmap
335 p->drawPixmap(getX(),getY(),pixmap);
336 else
337 {
338 //Blend this image with a color
339
340 TQImage image = pixmap.convertToImage();
341
342 TQImage result = KImageEffect::blend(TQColor(255,0,0), image, 0.5f);
343 p->drawImage(getX(),getY(),result);
344
345 //p->drawRect(boundingBox);
346 }
347 }
348 // start Timer
349 if (imageEffect != 0)
350 {
351 imageEffect -> startTimer();
352 }
353 }
354}
355
356void ImageLabel::mUpdate(TQPainter* p)
357{
358 //only draw image if not hidden
359 if (hidden == 0 && background == 0)
360 {
361 if (cblend == 0)
362 {
363 //draw the pixmap
364 p->drawPixmap(getX(),getY(),pixmap);
365 }
366 else
367 {
368 //Blend this image with a color
369
370 TQImage image = pixmap.convertToImage();
371
372 TQImage result = KImageEffect::blend(TQColor(255,0,0), image, 0.5f);
373 p->drawImage(getX(),getY(),result);
374
375 //p->drawRect(boundingBox);
376 }
377 }
378 // start Timer
379 if (imageEffect != 0)
380 {
381 imageEffect -> startTimer();
382 }
383}
384
385bool ImageLabel::click(TQMouseEvent* e)
386{
387 if (getBoundingBox().contains(e -> x(), e -> y()) && isEnabled())
388 {
389 TQString program;
390 if (e -> button() == TQt::LeftButton)
391 {
392 program = leftButtonAction;
393 }
394 else if (e -> button() == TQt::MidButton)
395 {
396 program = middleButtonAction;
397 }
398 else if (e -> button() == TQt::RightButton)
399 {
400 program = rightButtonAction;
401 }
402
403 if( !program.isEmpty() )
404 {
405 KRun::runCommand(program);
406 }
407 else
408 {
409 return true;
410 }
411 }
412 return false;
413}
414
415void ImageLabel::parseImages(TQString fn, TQString fn_roll, int _xoff,
416 int _yoff, int _xon, int _yon)
417{
418 //fn = filename;
419 //fn_roll = filename_roll;
420
421 xoff = _xoff;
422 yoff = _yoff;
423 xon = _xon;
424 yon = _yon;
425
426 // use the first line
427 TQStringList sList = TQStringList::split( "\n", fn );
428 TQString fileName = *sList.begin();
429 TQFileInfo fileInfo( fileName );
430 TQString path;
431
432 TQRegExp rx("^http://",false);
433 bool fileOnNet = (rx.search(fileName)!=-1)?true:false;
434
435
436 if( fileInfo.isRelative() && !fileOnNet )
437 {
438 path = m_karamba->theme().path() + "/" + fileName;
439 }
440 else
441 {
442 path = fileName;
443 }
444
445 if ( fileOnNet )
446 {
447 TQString tmpFile;
448#if defined(KDE_3_2)
449 if(TDEIO::NetAccess::download(KURL(path), tmpFile, karambaApp->parentWindow()))
450#else
451 if(TDEIO::NetAccess::download(KURL(path), tmpFile))
452#endif
453 {
454 pixmap_off = KPixmap(tmpFile);
455 TDEIO::NetAccess::removeTempFile(tmpFile);
456 tqDebug( "Downloaded: %s to %s", path.ascii(), tmpFile.ascii() );
457 }
458 else
459 {
460 tqDebug( "Error Downloading: %s", path.ascii());
461 }
462 }
463 else
464 {
465 pixmap_off = KPixmap( path );
466 }
467
468 pixmapOffWidth = pixmap.width();
469 pixmapOffHeight = pixmap.height();
470
471 rect_off = TQRect(xoff,yoff,pixmapWidth,pixmapHeight);
473 if (fn_roll.isEmpty())
474 return;
475
476 rollover=true;
477 sList = TQStringList::split( "\n", fn_roll );
478 fileName = *sList.begin();
479 fileInfo = TQFileInfo( fileName );
480
481 fileOnNet = (rx.search(fileName)!=-1)?true:false;
482
483
484 if( fileInfo.isRelative() && !fileOnNet )
485 {
486 path = m_karamba->theme().path() + "/" + fileName;
487 }
488 else
489 {
490 path = fileName;
491 }
492
493 if ( fileOnNet )
494 {
495 TQString tmpFile;
496#if defined(KDE_3_2)
497 if(TDEIO::NetAccess::download(KURL(path), tmpFile, karambaApp->parentWindow()))
498#else
499 if(TDEIO::NetAccess::download(KURL(path), tmpFile))
500#endif
501 {
502 pixmap_on = KPixmap(tmpFile);
503 TDEIO::NetAccess::removeTempFile(tmpFile);
504 tqDebug( "Downloaded: %s to %s", path.ascii(), tmpFile.ascii());
505 }
506 else
507 {
508 tqDebug( "Error Downloading: %s", path.ascii());
509 }
510 }
511 else
512 {
513 pixmap_on = KPixmap( path );
514 }
515 pixmapOnWidth = pixmap_on.width();
516 pixmapOnHeight = pixmap_on.height();
517
518 rect_on = TQRect(xon,yon,pixmapOnWidth,pixmapOnHeight);
519}
520
521void ImageLabel::setBackground(int b)
522{
523 background = b;
524}
525
526void ImageLabel::rolloverImage(TQMouseEvent *e)
527{
528 if (!rollover)
529 return;
530
531 if (zoomed)
532 {
533 if (!rect_off.contains(e->pos()))
534 {
535 // rollover the image to the zoomed image
536 //setValue(fn_roll);
537 setX(xoff);
538 setY(yoff);
539 pixmap = pixmap_off;
540 pixmapWidth = pixmapOffWidth;
541 pixmapHeight = pixmapOffHeight;
542 zoomed = false;
543 m_karamba->step();
544 }
545 }
546 else
547 {
548 if (rect_off.contains(e->pos()))
549 {
550 // rollover the image to the zoomed image
551 //setValue(fn_roll);
552 setX(xon);
553 setY(yon);
554 pixmap = pixmap_on;
555 pixmapWidth = pixmapOnWidth;
556 pixmapHeight = pixmapOnHeight;
557 zoomed = true;
558 m_karamba->step();
559 }
560 }
561}
562
563void ImageLabel::setTooltip(TQString txt)
564{
565 TQRect rect(getX(),getY(),pixmapWidth,pixmapHeight);
566 TQToolTip::add(m_karamba, rect, txt);
567 old_tip_rect = TQRect(rect.topLeft(), rect.bottomRight());
568}
569
570
571void ImageLabel::removeEffects()
572{
573 if (imageEffect != 0)
574 {
575 delete imageEffect;
576 imageEffect = 0;
577 }
578 applyTransformations();
579}
580
581void ImageLabel::intensity(float ratio, int millisec)
582{
583 if (imageEffect != 0)
584 {
585 delete imageEffect;
586 imageEffect = 0;
587 }
588 //KPixmapEffect::intensity(pixmap, ratio);
589 imageEffect = new Intensity(this, ratio, millisec);
590 applyTransformations();
591}
592
593void ImageLabel::channelIntensity(float ratio, TQString channel, int millisec)
594{
595 if (imageEffect != 0)
596 {
597 delete imageEffect;
598 imageEffect = 0;
599 }
600 //KPixmapEffect::channelIntensity(pixmap, ratio, rgbChannel);
601 imageEffect = new ChannelIntensity(this, ratio, channel, millisec);
602 applyTransformations();
603}
604
605void ImageLabel::toGray(int millisec)
606{
607 if (imageEffect != 0)
608 {
609 delete imageEffect;
610 imageEffect = 0;
611 }
612 //KPixmapEffect::toGray(pixmap);
613 imageEffect = new ToGray(this, millisec);
614 applyTransformations();
615}
616
617void ImageLabel::slotEffectExpired()
618{
619 removeEffects();
620 m_karamba -> externalStep();
621}
622
623void ImageLabel::attachClickArea(TQString leftMouseButton,
624 TQString middleMouseButton,
625 TQString rightMouseButton)
626{
627 leftButtonAction = leftMouseButton;
628 middleButtonAction = middleMouseButton;
629 rightButtonAction = rightMouseButton;
630}
631
632#include "imagelabel.moc"

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • File List
  • Class Members

superkaramba

Skip menu "superkaramba"
  • kcalc
  •   knumber
  • superkaramba
Generated for superkaramba by doxygen 1.9.4
This website is maintained by Timothy Pearson.