• 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(TDEIO::NetAccess::download(KURL(path), tmpFile, karambaApp->parentWindow()))
449 {
450 pixmap_off = KPixmap(tmpFile);
451 TDEIO::NetAccess::removeTempFile(tmpFile);
452 tqDebug( "Downloaded: %s to %s", path.ascii(), tmpFile.ascii() );
453 }
454 else
455 {
456 tqDebug( "Error Downloading: %s", path.ascii());
457 }
458 }
459 else
460 {
461 pixmap_off = KPixmap( path );
462 }
463
464 pixmapOffWidth = pixmap.width();
465 pixmapOffHeight = pixmap.height();
466
467 rect_off = TQRect(xoff,yoff,pixmapWidth,pixmapHeight);
469 if (fn_roll.isEmpty())
470 return;
471
472 rollover=true;
473 sList = TQStringList::split( "\n", fn_roll );
474 fileName = *sList.begin();
475 fileInfo = TQFileInfo( fileName );
476
477 fileOnNet = (rx.search(fileName)!=-1)?true:false;
478
479
480 if( fileInfo.isRelative() && !fileOnNet )
481 {
482 path = m_karamba->theme().path() + "/" + fileName;
483 }
484 else
485 {
486 path = fileName;
487 }
488
489 if ( fileOnNet )
490 {
491 TQString tmpFile;
492 if(TDEIO::NetAccess::download(KURL(path), tmpFile, karambaApp->parentWindow()))
493 {
494 pixmap_on = KPixmap(tmpFile);
495 TDEIO::NetAccess::removeTempFile(tmpFile);
496 tqDebug( "Downloaded: %s to %s", path.ascii(), tmpFile.ascii());
497 }
498 else
499 {
500 tqDebug( "Error Downloading: %s", path.ascii());
501 }
502 }
503 else
504 {
505 pixmap_on = KPixmap( path );
506 }
507 pixmapOnWidth = pixmap_on.width();
508 pixmapOnHeight = pixmap_on.height();
509
510 rect_on = TQRect(xon,yon,pixmapOnWidth,pixmapOnHeight);
511}
512
513void ImageLabel::setBackground(int b)
514{
515 background = b;
516}
517
518void ImageLabel::rolloverImage(TQMouseEvent *e)
519{
520 if (!rollover)
521 return;
522
523 if (zoomed)
524 {
525 if (!rect_off.contains(e->pos()))
526 {
527 // rollover the image to the zoomed image
528 //setValue(fn_roll);
529 setX(xoff);
530 setY(yoff);
531 pixmap = pixmap_off;
532 pixmapWidth = pixmapOffWidth;
533 pixmapHeight = pixmapOffHeight;
534 zoomed = false;
535 m_karamba->step();
536 }
537 }
538 else
539 {
540 if (rect_off.contains(e->pos()))
541 {
542 // rollover the image to the zoomed image
543 //setValue(fn_roll);
544 setX(xon);
545 setY(yon);
546 pixmap = pixmap_on;
547 pixmapWidth = pixmapOnWidth;
548 pixmapHeight = pixmapOnHeight;
549 zoomed = true;
550 m_karamba->step();
551 }
552 }
553}
554
555void ImageLabel::setTooltip(TQString txt)
556{
557 TQRect rect(getX(),getY(),pixmapWidth,pixmapHeight);
558 TQToolTip::add(m_karamba, rect, txt);
559 old_tip_rect = TQRect(rect.topLeft(), rect.bottomRight());
560}
561
562
563void ImageLabel::removeEffects()
564{
565 if (imageEffect != 0)
566 {
567 delete imageEffect;
568 imageEffect = 0;
569 }
570 applyTransformations();
571}
572
573void ImageLabel::intensity(float ratio, int millisec)
574{
575 if (imageEffect != 0)
576 {
577 delete imageEffect;
578 imageEffect = 0;
579 }
580 //KPixmapEffect::intensity(pixmap, ratio);
581 imageEffect = new Intensity(this, ratio, millisec);
582 applyTransformations();
583}
584
585void ImageLabel::channelIntensity(float ratio, TQString channel, int millisec)
586{
587 if (imageEffect != 0)
588 {
589 delete imageEffect;
590 imageEffect = 0;
591 }
592 //KPixmapEffect::channelIntensity(pixmap, ratio, rgbChannel);
593 imageEffect = new ChannelIntensity(this, ratio, channel, millisec);
594 applyTransformations();
595}
596
597void ImageLabel::toGray(int millisec)
598{
599 if (imageEffect != 0)
600 {
601 delete imageEffect;
602 imageEffect = 0;
603 }
604 //KPixmapEffect::toGray(pixmap);
605 imageEffect = new ToGray(this, millisec);
606 applyTransformations();
607}
608
609void ImageLabel::slotEffectExpired()
610{
611 removeEffects();
612 m_karamba -> externalStep();
613}
614
615void ImageLabel::attachClickArea(TQString leftMouseButton,
616 TQString middleMouseButton,
617 TQString rightMouseButton)
618{
619 leftButtonAction = leftMouseButton;
620 middleButtonAction = middleMouseButton;
621 rightButtonAction = rightMouseButton;
622}
623
624#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.8
This website is maintained by Timothy Pearson.