imagelabel.cpp
00001 /**************************************************************************** 00002 * imagelabel.cpp - ImageLabel meter 00003 * 00004 * Copyright (C) 2003 Hans Karlsson <karlsson.h@home.se> 00005 * Copyright (c) 2004 Petri Damstén <damu@iki.fi> 00006 * 00007 * This file is part of SuperKaramba. 00008 * 00009 * SuperKaramba is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * SuperKaramba is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with SuperKaramba; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 ****************************************************************************/ 00023 00024 #include <tqpixmap.h> 00025 #include <tqtimer.h> 00026 #include <tqtooltip.h> 00027 #include <kpixmapeffect.h> 00028 #include <kdebug.h> 00029 #include <kimageeffect.h> 00030 #include <ktempfile.h> 00031 #include <kio/job.h> 00032 #include "karambaapp.h" 00033 #include "imagelabel.h" 00034 00035 // Effect 00036 Effect::Effect(ImageLabel* img, int msec) : 00037 myImage(img) 00038 { 00039 if (msec > 0) 00040 { 00041 // remove the effect after the given time 00042 //TQTimer::singleShot (millisec, myImage, TQT_SLOT(slotEffectExpired())); 00043 //timer -> changeInterval(millisec); 00044 millisec = msec; 00045 } 00046 else 00047 { 00048 millisec = msec; 00049 } 00050 } 00051 00052 Effect::~Effect() 00053 { 00054 } 00055 00056 void Effect::startTimer() 00057 { 00058 if (millisec > 0) 00059 { 00060 TQTimer::singleShot (millisec, myImage, TQT_SLOT(slotEffectExpired())); 00061 millisec = 0; 00062 } 00063 } 00064 00065 // Intensity 00066 Intensity::Intensity(ImageLabel* img, float r, int millisec) : 00067 Effect(img, millisec) 00068 { 00069 ratio = r; 00070 ratio = (ratio > 1) ? 1 : ratio; 00071 ratio = (ratio < -1) ? -1 : ratio; 00072 } 00073 00074 KPixmap Intensity::apply(KPixmap pixmap) 00075 { 00076 return KPixmapEffect::intensity(pixmap, ratio); 00077 } 00078 00079 // ChannelIntensity 00080 ChannelIntensity::ChannelIntensity(ImageLabel* img, float r, TQString c, 00081 int millisec) : 00082 Effect(img, millisec) 00083 { 00084 ratio = r; 00085 ratio = (ratio > 1) ? 1 : ratio; 00086 ratio = (ratio < -1) ? -1 : ratio; 00087 00088 channel = 0; 00089 if (c.find("red", 0 , false)) 00090 { 00091 channel = 0; 00092 } 00093 else if (c.find("green", 0, false)) 00094 { 00095 channel = 1; 00096 } 00097 else if (c.find("blue", 0, false)) 00098 { 00099 channel = 2; 00100 } 00101 } 00102 00103 KPixmap ChannelIntensity::apply(KPixmap pixmap) 00104 { 00105 return KPixmapEffect::channelIntensity(pixmap, ratio, 00106 (KPixmapEffect::RGBComponent)channel); 00107 } 00108 00109 // ToGray 00110 ToGray::ToGray(ImageLabel* img, int millisec) : Effect(img, millisec) 00111 { 00112 } 00113 00114 KPixmap ToGray::apply(KPixmap pixmap) 00115 { 00116 return KPixmapEffect::toGray(pixmap); 00117 } 00118 00119 /***********************************************************************/ 00120 00121 ImageLabel::ImageLabel(karamba* k, int ix,int iy,int iw,int ih) : 00122 Meter(k, ix,iy,iw,ih), zoomed(false), rollover(false) 00123 { 00124 background = 0; 00125 cblend = 0; 00126 //scaleMat.reset(); 00127 //rotMat.reset(); 00128 scale_w = 1; 00129 scale_h = 1; 00130 rot_angle = 0; 00131 00132 doScale = false; 00133 doRotate = false; 00134 00135 imageEffect = 0; 00136 } 00137 00138 ImageLabel::ImageLabel(karamba* k) : 00139 Meter(k), zoomed(false), rollover(false) 00140 { 00141 cblend = 0; 00142 background = 0; 00143 } 00144 00145 ImageLabel::~ImageLabel() 00146 { 00147 if (imageEffect != 0) 00148 { 00149 delete imageEffect; 00150 imageEffect = 0; 00151 } 00152 if(!old_tip_rect.isNull()) 00153 { 00154 TQToolTip::remove(m_karamba, old_tip_rect); 00155 } 00156 } 00157 00158 void ImageLabel::setValue(long v) 00159 { 00160 setValue( TQString::number( v ) ); 00161 } 00162 00163 void ImageLabel::show() 00164 { 00165 Meter::show(); 00166 setEnabled(true); 00167 } 00168 00169 void ImageLabel::hide() 00170 { 00171 Meter::hide(); 00172 setEnabled(false); 00173 } 00174 00175 void ImageLabel::rotate(int deg) 00176 { 00177 doRotate = !(deg == 0); 00178 00179 rot_angle = deg; 00180 00181 applyTransformations(); 00182 } 00183 00184 void ImageLabel::scale(int w, int h) 00185 { 00186 doScale = !(w == realpixmap.width() && h == realpixmap.height()); 00187 00188 scale_w = w; 00189 scale_h = h; 00190 00191 applyTransformations(); 00192 } 00193 00194 void ImageLabel::smoothScale(int w, int h) 00195 { 00196 doScale = !(w == realpixmap.width() && h == realpixmap.height()); 00197 00198 scale_w = w; 00199 scale_h = h; 00200 00201 applyTransformations(true); 00202 00203 // double widthFactor = ((double)w) / ((double)realpixmap.width()); 00204 // double heightFactor = ((double)h) / ((double)realpixmap.height()); 00205 00206 // pixmap.convertFromImage(realpixmap.convertToImage().smoothScale(w, h)); 00207 00208 // setWidth(pixmap.width()); 00209 // setHeight(pixmap.height()); 00210 00211 } 00212 00213 void ImageLabel::removeImageTransformations() 00214 { 00215 doScale = false; 00216 doRotate = false; 00217 00218 scale_w = 1; 00219 scale_h = 1; 00220 rot_angle = 0; 00221 pixmap = realpixmap; 00222 } 00223 00224 void ImageLabel::applyTransformations(bool useSmoothScale) 00225 { 00226 pixmap = realpixmap; 00227 if (doRotate) 00228 { 00229 // KDE and QT seem to miss a high quality image rotation 00230 TQWMatrix rotMat; 00231 rotMat.rotate(rot_angle); 00232 pixmap = pixmap.xForm(rotMat); 00233 } 00234 if (doScale) 00235 { 00236 if (m_karamba -> useSmoothTransforms() || useSmoothScale) 00237 { 00238 pixmap.convertFromImage( 00239 pixmap.convertToImage().smoothScale(scale_w, scale_h)); 00240 } 00241 else 00242 { 00243 double widthFactor = ((double)scale_w) / ((double)pixmap.width()); 00244 double heightFactor = ((double)scale_h) / ((double)pixmap.height()); 00245 TQWMatrix scaleMat; 00246 scaleMat.scale(widthFactor, heightFactor); 00247 pixmap = pixmap.xForm(scaleMat); 00248 } 00249 } 00250 if (imageEffect != 0) 00251 { 00252 pixmap = imageEffect -> apply(pixmap); 00253 } 00254 setWidth(pixmap.width()); 00255 setHeight(pixmap.height()); 00256 } 00257 00258 void ImageLabel::slotCopyResult(KIO::Job* job) 00259 { 00260 TQString tempFile = ((KIO::FileCopyJob*)job)->destURL().path(); 00261 if(job->error() == 0) 00262 { 00263 setValue(tempFile); 00264 imagePath = ((KIO::FileCopyJob*)job)->srcURL().path(); 00265 emit pixmapLoaded(); 00266 } 00267 else 00268 { 00269 qWarning("Error downloading (%s): %s", job->errorText().ascii(), 00270 tempFile.ascii()); 00271 } 00272 KIO::NetAccess::removeTempFile(tempFile); 00273 } 00274 00275 void ImageLabel::setValue(TQString fn) 00276 { 00277 // use the first line 00278 TQStringList sList = TQStringList::split( "\n", fn ); 00279 TQString fileName = *sList.begin(); 00280 KURL url(fileName); 00281 TQRegExp rx("^[a-zA-Z]{1,5}:/",false); 00282 bool protocol = (rx.search(fileName)!=-1)?true:false; 00283 TQPixmap pm; 00284 00285 if(protocol && url.isLocalFile() == false) 00286 { 00287 KTempFile tmpFile; 00288 KIO::FileCopyJob* copy = KIO::file_copy(fileName, tmpFile.name(), 0600, 00289 true, false, false); 00290 connect(copy, TQT_SIGNAL(result(KIO::Job*)), 00291 this, TQT_SLOT(slotCopyResult(KIO::Job*))); 00292 return; 00293 } 00294 else 00295 { 00296 if(m_karamba->theme().isThemeFile(fileName)) 00297 { 00298 TQByteArray ba = m_karamba->theme().readThemeFile(fileName); 00299 pm.loadFromData(ba); 00300 } 00301 else 00302 { 00303 pm.load(fileName); 00304 } 00305 imagePath = fileName; 00306 } 00307 setValue(pm); 00308 } 00309 00310 //Matthew Kay: a new version of setValue to be used by createTaskIcon() 00314 void ImageLabel::setValue(TQPixmap& pix) 00315 { 00316 realpixmap = KPixmap(pix); 00317 pixmap = realpixmap; 00318 setWidth(pixmap.width()); 00319 setHeight(pixmap.height()); 00320 00321 pixmapWidth = pixmap.width(); 00322 pixmapHeight = pixmap.height(); 00323 rect_off = TQRect(getX(),getY(),pixmapWidth,pixmapHeight); 00324 } 00325 00326 void ImageLabel::mUpdate(TQPainter* p, int backgroundUpdate) 00327 { 00328 if (backgroundUpdate == 1) 00329 { 00330 //only draw image if not hidden 00331 if (hidden == 0) 00332 { 00333 if (cblend == 0) 00334 //draw the pixmap 00335 p->drawPixmap(getX(),getY(),pixmap); 00336 else 00337 { 00338 //Blend this image with a color 00339 00340 TQImage image = pixmap.convertToImage(); 00341 00342 TQImage result = KImageEffect::blend(TQColor(255,0,0), image, 0.5f); 00343 p->drawImage(getX(),getY(),result); 00344 00345 //p->drawRect(boundingBox); 00346 } 00347 } 00348 // start Timer 00349 if (imageEffect != 0) 00350 { 00351 imageEffect -> startTimer(); 00352 } 00353 } 00354 } 00355 00356 void ImageLabel::mUpdate(TQPainter* p) 00357 { 00358 //only draw image if not hidden 00359 if (hidden == 0 && background == 0) 00360 { 00361 if (cblend == 0) 00362 { 00363 //draw the pixmap 00364 p->drawPixmap(getX(),getY(),pixmap); 00365 } 00366 else 00367 { 00368 //Blend this image with a color 00369 00370 TQImage image = pixmap.convertToImage(); 00371 00372 TQImage result = KImageEffect::blend(TQColor(255,0,0), image, 0.5f); 00373 p->drawImage(getX(),getY(),result); 00374 00375 //p->drawRect(boundingBox); 00376 } 00377 } 00378 // start Timer 00379 if (imageEffect != 0) 00380 { 00381 imageEffect -> startTimer(); 00382 } 00383 } 00384 00385 bool ImageLabel::click(TQMouseEvent* e) 00386 { 00387 if (getBoundingBox().contains(e -> x(), e -> y()) && isEnabled()) 00388 { 00389 TQString program; 00390 if (e -> button() == Qt::LeftButton) 00391 { 00392 program = leftButtonAction; 00393 } 00394 else if (e -> button() == Qt::MidButton) 00395 { 00396 program = middleButtonAction; 00397 } 00398 else if (e -> button() == Qt::RightButton) 00399 { 00400 program = rightButtonAction; 00401 } 00402 00403 if( !program.isEmpty() ) 00404 { 00405 KRun::runCommand(program); 00406 } 00407 else 00408 { 00409 return true; 00410 } 00411 } 00412 return false; 00413 } 00414 00415 void ImageLabel::parseImages(TQString fn, TQString fn_roll, int _xoff, 00416 int _yoff, int _xon, int _yon) 00417 { 00418 //fn = filename; 00419 //fn_roll = filename_roll; 00420 00421 xoff = _xoff; 00422 yoff = _yoff; 00423 xon = _xon; 00424 yon = _yon; 00425 00426 // use the first line 00427 TQStringList sList = TQStringList::split( "\n", fn ); 00428 TQString fileName = *sList.begin(); 00429 TQFileInfo fileInfo( fileName ); 00430 TQString path; 00431 00432 TQRegExp rx("^http://",false); 00433 bool fileOnNet = (rx.search(fileName)!=-1)?true:false; 00434 00435 00436 if( fileInfo.isRelative() && !fileOnNet ) 00437 { 00438 path = m_karamba->theme().path() + "/" + fileName; 00439 } 00440 else 00441 { 00442 path = fileName; 00443 } 00444 00445 if ( fileOnNet ) 00446 { 00447 TQString tmpFile; 00448 #if defined(KDE_3_2) 00449 if(KIO::NetAccess::download(KURL(path), tmpFile, karambaApp->parentWindow())) 00450 #else 00451 if(KIO::NetAccess::download(KURL(path), tmpFile)) 00452 #endif 00453 { 00454 pixmap_off = KPixmap(tmpFile); 00455 KIO::NetAccess::removeTempFile(tmpFile); 00456 qDebug( "Downloaded: %s to %s", path.ascii(), tmpFile.ascii() ); 00457 } 00458 else 00459 { 00460 qDebug( "Error Downloading: %s", path.ascii()); 00461 } 00462 } 00463 else 00464 { 00465 pixmap_off = KPixmap( path ); 00466 } 00467 00468 pixmapOffWidth = pixmap.width(); 00469 pixmapOffHeight = pixmap.height(); 00470 00471 rect_off = TQRect(xoff,yoff,pixmapWidth,pixmapHeight); 00473 if (fn_roll.isEmpty()) 00474 return; 00475 00476 rollover=true; 00477 sList = TQStringList::split( "\n", fn_roll ); 00478 fileName = *sList.begin(); 00479 fileInfo = TQFileInfo( fileName ); 00480 00481 fileOnNet = (rx.search(fileName)!=-1)?true:false; 00482 00483 00484 if( fileInfo.isRelative() && !fileOnNet ) 00485 { 00486 path = m_karamba->theme().path() + "/" + fileName; 00487 } 00488 else 00489 { 00490 path = fileName; 00491 } 00492 00493 if ( fileOnNet ) 00494 { 00495 TQString tmpFile; 00496 #if defined(KDE_3_2) 00497 if(KIO::NetAccess::download(KURL(path), tmpFile, karambaApp->parentWindow())) 00498 #else 00499 if(KIO::NetAccess::download(KURL(path), tmpFile)) 00500 #endif 00501 { 00502 pixmap_on = KPixmap(tmpFile); 00503 KIO::NetAccess::removeTempFile(tmpFile); 00504 qDebug( "Downloaded: %s to %s", path.ascii(), tmpFile.ascii()); 00505 } 00506 else 00507 { 00508 qDebug( "Error Downloading: %s", path.ascii()); 00509 } 00510 } 00511 else 00512 { 00513 pixmap_on = KPixmap( path ); 00514 } 00515 pixmapOnWidth = pixmap_on.width(); 00516 pixmapOnHeight = pixmap_on.height(); 00517 00518 rect_on = TQRect(xon,yon,pixmapOnWidth,pixmapOnHeight); 00519 } 00520 00521 void ImageLabel::setBackground(int b) 00522 { 00523 background = b; 00524 } 00525 00526 void ImageLabel::rolloverImage(TQMouseEvent *e) 00527 { 00528 if (!rollover) 00529 return; 00530 00531 if (zoomed) 00532 { 00533 if (!rect_off.contains(e->pos())) 00534 { 00535 // rollover the image to the zoomed image 00536 //setValue(fn_roll); 00537 setX(xoff); 00538 setY(yoff); 00539 pixmap = pixmap_off; 00540 pixmapWidth = pixmapOffWidth; 00541 pixmapHeight = pixmapOffHeight; 00542 zoomed = false; 00543 m_karamba->step(); 00544 } 00545 } 00546 else 00547 { 00548 if (rect_off.contains(e->pos())) 00549 { 00550 // rollover the image to the zoomed image 00551 //setValue(fn_roll); 00552 setX(xon); 00553 setY(yon); 00554 pixmap = pixmap_on; 00555 pixmapWidth = pixmapOnWidth; 00556 pixmapHeight = pixmapOnHeight; 00557 zoomed = true; 00558 m_karamba->step(); 00559 } 00560 } 00561 } 00562 00563 void ImageLabel::setTooltip(TQString txt) 00564 { 00565 TQRect rect(getX(),getY(),pixmapWidth,pixmapHeight); 00566 TQToolTip::add(m_karamba, rect, txt); 00567 old_tip_rect = TQRect(rect.topLeft(), rect.bottomRight()); 00568 } 00569 00570 00571 void ImageLabel::removeEffects() 00572 { 00573 if (imageEffect != 0) 00574 { 00575 delete imageEffect; 00576 imageEffect = 0; 00577 } 00578 applyTransformations(); 00579 } 00580 00581 void ImageLabel::intensity(float ratio, int millisec) 00582 { 00583 if (imageEffect != 0) 00584 { 00585 delete imageEffect; 00586 imageEffect = 0; 00587 } 00588 //KPixmapEffect::intensity(pixmap, ratio); 00589 imageEffect = new Intensity(this, ratio, millisec); 00590 applyTransformations(); 00591 } 00592 00593 void ImageLabel::channelIntensity(float ratio, TQString channel, int millisec) 00594 { 00595 if (imageEffect != 0) 00596 { 00597 delete imageEffect; 00598 imageEffect = 0; 00599 } 00600 //KPixmapEffect::channelIntensity(pixmap, ratio, rgbChannel); 00601 imageEffect = new ChannelIntensity(this, ratio, channel, millisec); 00602 applyTransformations(); 00603 } 00604 00605 void ImageLabel::toGray(int millisec) 00606 { 00607 if (imageEffect != 0) 00608 { 00609 delete imageEffect; 00610 imageEffect = 0; 00611 } 00612 //KPixmapEffect::toGray(pixmap); 00613 imageEffect = new ToGray(this, millisec); 00614 applyTransformations(); 00615 } 00616 00617 void ImageLabel::slotEffectExpired() 00618 { 00619 removeEffects(); 00620 m_karamba -> externalStep(); 00621 } 00622 00623 void ImageLabel::attachClickArea(TQString leftMouseButton, 00624 TQString middleMouseButton, 00625 TQString rightMouseButton) 00626 { 00627 leftButtonAction = leftMouseButton; 00628 middleButtonAction = middleMouseButton; 00629 rightButtonAction = rightMouseButton; 00630 } 00631 00632 #include "imagelabel.moc"