• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kstars

imageviewer.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           imageviewer.cpp  -  An ImageViewer for KStars
00003                              -------------------
00004     begin                : Mon Aug 27 2001
00005     copyright            : (C) 2001 by Thomas Kabelmann
00006     email                : tk78@gmx.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include <qfont.h>
00019 
00020 #include <klocale.h>
00021 #include <kmessagebox.h>
00022 #include <kfiledialog.h>
00023 #include <kstatusbar.h>
00024 #include <kio/netaccess.h>
00025 #include <kaction.h>
00026 #include <kaccel.h>
00027 #include <kdebug.h>
00028 #include "imageviewer.h"
00029 
00030 #include <kapplication.h>
00031 
00032 ImageViewer::ImageViewer (const KURL *url, const QString &capText, QWidget *parent, const char *name)
00033     : KMainWindow (parent, name), imageURL (*url), fileIsImage (false),
00034       ctrl (false), key_s (false), key_q (false), downloadJob(0)
00035 {
00036 // toolbar can dock only on top-position and can't be minimized
00037 // JH: easier to just disable its mobility
00038     toolBar()->setMovingEnabled( false );
00039 
00040     KAction *action = new KAction (i18n ("Close Window"), "fileclose", CTRL+Key_Q, this, SLOT (close()), actionCollection());
00041     action->plug (toolBar());
00042     action = new KAction (i18n ("Save Image"), "filesave", CTRL+Key_S, this, SLOT (saveFileToDisc()), actionCollection());
00043     action->plug (toolBar());
00044 
00045     statusBar()->insertItem( capText, 0, 1, true );
00046     statusBar()->setItemAlignment( 0, AlignLeft | AlignVCenter );
00047     QFont fnt = statusBar()->font();
00048     fnt.setPointSize( fnt.pointSize() - 2 );
00049     statusBar()->setFont( fnt );
00050     
00051     if (!imageURL.isValid())        //check URL
00052         kdDebug()<<"URL is malformed"<<endl;
00053     setCaption (imageURL.fileName()); // the title of the window
00054     loadImageFromURL();
00055 }
00056 
00057 ImageViewer::~ImageViewer() {
00058 // check if download job is running
00059     checkJob();
00060 
00061     if (!file->remove())        // if the file was not complete downloaded the suffix is  ".part"
00062     {
00063             kdDebug()<<QString("remove of %1 failed").arg(file->name())<<endl;
00064         file->setName (file->name() + ".part");     // set new suffix to filename
00065                 kdDebug()<<QString("try to remove %1").arg( file->name())<<endl;
00066         if (file->remove())
00067                     kdDebug()<<"file removed\n";
00068         else
00069                     kdDebug()<<"file not removed\n";
00070     }
00071 }
00072 
00073 void ImageViewer::paintEvent (QPaintEvent */*ev*/)
00074 {
00075     bitBlt (this, 0, toolBar()->height() + 1, &pix);
00076 }
00077 
00078 void ImageViewer::resizeEvent (QResizeEvent */*ev*/)
00079 {
00080     if ( !downloadJob )  // only if image is loaded
00081         pix = kpix.convertToPixmap ( image.smoothScale ( size().width() , size().height() - toolBar()->height() - statusBar()->height() ) );    // convert QImage to QPixmap (fastest method)
00082 
00083     update();
00084 }
00085 
00086 void ImageViewer::closeEvent (QCloseEvent *ev)
00087 {
00088     if (ev) // not if closeEvent (0) is called, because segfault
00089         ev->accept();   // parent-widgets should not get this event, so it will be filtered
00090     this->~ImageViewer();   // destroy the object, so the object can only allocated with operator new, not as a global/local variable
00091 }
00092 
00093 
00094 void ImageViewer::keyPressEvent (QKeyEvent *ev)
00095 {
00096     ev->accept();  //make sure key press events are captured.
00097     switch (ev->key())
00098     {
00099         case Key_Control : ctrl = true; break;
00100         case Key_Q : key_q = true; break;
00101         case Key_S : key_s = true; break;
00102         default : ev->ignore();
00103     }
00104     if (ctrl && key_q)
00105         close();
00106     if (ctrl && key_s)
00107     {
00108         ctrl = false;
00109         key_s = false;
00110         saveFileToDisc();
00111     }
00112 }
00113 
00114 void ImageViewer::keyReleaseEvent (QKeyEvent *ev)
00115 {
00116     ev->accept();
00117     switch (ev->key())
00118     {
00119         case Key_Control : ctrl = false; break;
00120         case Key_Q : key_q = false; break;
00121         case Key_S : key_s = false; break;
00122         default : ev->ignore();
00123     }
00124 }
00125 
00126 void ImageViewer::loadImageFromURL()
00127 {
00128     file = tempfile.file();
00129     tempfile.unlink();      // we just need the name and delete the tempfile from disc; if we don't do it, a dialog will be shown
00130     KURL saveURL (file->name());
00131     if (!saveURL.isValid())
00132             kdDebug()<<"tempfile-URL is malformed\n";
00133 
00134     downloadJob = KIO::copy (imageURL, saveURL);    // starts the download asynchron
00135     connect (downloadJob, SIGNAL (result (KIO::Job *)), SLOT (downloadReady (KIO::Job *)));
00136 }
00137 
00138 void ImageViewer::downloadReady (KIO::Job *job)
00139 {
00140 // set downloadJob to 0, but don't delete it - the job will automatically deleted !!!
00141     downloadJob = 0;
00142 
00143     if ( job->error() )
00144     {
00145         job->showErrorDialog();
00146         closeEvent (0);
00147         return;     // exit this function
00148     }
00149 
00150     file->close(); // to get the newest informations of the file and not any informations from opening of the file
00151 
00152     if ( file->exists() )
00153     {
00154         showImage();
00155         return;
00156     }
00157     closeEvent (0);
00158 }
00159 
00160 void ImageViewer::showImage()
00161 {
00162     if (!image.load (file->name()))     // if loading failed
00163     {
00164         QString text = i18n ("Loading of the image %1 failed.");
00165         KMessageBox::error (this, text.arg (imageURL.prettyURL() ));
00166         closeEvent (0);
00167         return;
00168     }
00169     fileIsImage = true; // we loaded the file and know now, that it is an image
00170 
00171     //First, if the image is less wide than the statusBar, we have to scale it up.
00172     if ( image.width() < statusBar()->width() ) {
00173         image.smoothScale ( statusBar()->width() , image.height() * statusBar()->width() / image.width() );
00174     }
00175     
00176     QRect deskRect = kapp->desktop()->availableGeometry();
00177     int w = deskRect.width(); // screen width
00178     int h = deskRect.height(); // screen height
00179     int h2 = image.height() + toolBar()->height() + statusBar()->height(); //height required for ImageViewer
00180     if (image.width() <= w && h2 <= h)
00181         resize ( image.width(), h2 );
00182 
00183 //If the image is larger than screen width and/or screen height, shrink it to fit the screen
00184 //while preserving the original aspect ratio
00185 
00186     else if (image.width() <= w) //only the height is too large
00187         resize ( int( image.width()*h/h2 ), h );
00188     else if (image.height() <= h) //only the width is too large
00189         resize ( w, int( h2*w/image.width() ) );
00190     else { //uh-oh...both width and height are too large.  which needs to be shrunk least?
00191         float fx = float(w)/float(image.width());
00192         float fy = float(h)/float(h2);
00193     if (fx > fy) //width needs to be shrunk less, so shrink to fit in height
00194             resize ( int( image.width()*fy ), h );
00195         else //vice versa
00196             resize ( w, int( h2*fx ) );
00197     }
00198 
00199     show(); // hide is default
00200 // pix will be initialized in resizeEvent(), which will automatically called first time
00201 }
00202 
00203 void ImageViewer::saveFileToDisc()
00204 {
00205     KURL newURL = KFileDialog::getSaveURL(imageURL.fileName());  // save-dialog with default filename
00206     if (!newURL.isEmpty())
00207     {
00208         QFile f (newURL.directory() + "/" +  newURL.fileName());
00209         if (f.exists())
00210         {
00211             int r=KMessageBox::warningContinueCancel(static_cast<QWidget *>(parent()),
00212                                     i18n( "A file named \"%1\" already exists. "
00213                                             "Overwrite it?" ).arg(newURL.fileName()),
00214                                     i18n( "Overwrite File?" ),
00215                                     i18n( "&Overwrite" ) );
00216             if(r==KMessageBox::Cancel) return;
00217             
00218             f.remove();
00219         }
00220         saveFile (newURL);
00221     }
00222 }
00223 
00224 void ImageViewer::saveFile (KURL &url) {
00225 // synchronous Access to prevent segfaults
00226     if (!KIO::NetAccess::copy (KURL (file->name()), url, (QWidget*) 0))
00227     {
00228         QString text = i18n ("Saving of the image %1 failed.");
00229         KMessageBox::error (this, text.arg (url.prettyURL() ));
00230     }
00231 }
00232 
00233 void ImageViewer::close() {
00234     closeEvent (0);
00235 }
00236 
00237 void ImageViewer::checkJob() {
00238     if ( downloadJob ) {  // if download job is running
00239         downloadJob->kill( true );  // close job quietly, without emitting a result
00240         kdDebug() << "Download job killed";
00241     }
00242 }
00243 
00244 #include "imageviewer.moc"

kstars

Skip menu "kstars"
  • Main Page
  • Modules
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • keduca
  • kstars
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal