kstars
imageviewer.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00037
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())
00052 kdDebug()<<"URL is malformed"<<endl;
00053 setCaption (imageURL.fileName());
00054 loadImageFromURL();
00055 }
00056
00057 ImageViewer::~ImageViewer() {
00058
00059 checkJob();
00060
00061 if (!file->remove())
00062 {
00063 kdDebug()<<QString("remove of %1 failed").arg(file->name())<<endl;
00064 file->setName (file->name() + ".part");
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 *)
00074 {
00075 bitBlt (this, 0, toolBar()->height() + 1, &pix);
00076 }
00077
00078 void ImageViewer::resizeEvent (QResizeEvent *)
00079 {
00080 if ( !downloadJob )
00081 pix = kpix.convertToPixmap ( image.smoothScale ( size().width() , size().height() - toolBar()->height() - statusBar()->height() ) );
00082
00083 update();
00084 }
00085
00086 void ImageViewer::closeEvent (QCloseEvent *ev)
00087 {
00088 if (ev)
00089 ev->accept();
00090 this->~ImageViewer();
00091 }
00092
00093
00094 void ImageViewer::keyPressEvent (QKeyEvent *ev)
00095 {
00096 ev->accept();
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();
00130 KURL saveURL (file->name());
00131 if (!saveURL.isValid())
00132 kdDebug()<<"tempfile-URL is malformed\n";
00133
00134 downloadJob = KIO::copy (imageURL, saveURL);
00135 connect (downloadJob, SIGNAL (result (KIO::Job *)), SLOT (downloadReady (KIO::Job *)));
00136 }
00137
00138 void ImageViewer::downloadReady (KIO::Job *job)
00139 {
00140
00141 downloadJob = 0;
00142
00143 if ( job->error() )
00144 {
00145 job->showErrorDialog();
00146 closeEvent (0);
00147 return;
00148 }
00149
00150 file->close();
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()))
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;
00170
00171
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();
00178 int h = deskRect.height();
00179 int h2 = image.height() + toolBar()->height() + statusBar()->height();
00180 if (image.width() <= w && h2 <= h)
00181 resize ( image.width(), h2 );
00182
00183
00184
00185
00186 else if (image.width() <= w)
00187 resize ( int( image.width()*h/h2 ), h );
00188 else if (image.height() <= h)
00189 resize ( w, int( h2*w/image.width() ) );
00190 else {
00191 float fx = float(w)/float(image.width());
00192 float fy = float(h)/float(h2);
00193 if (fx > fy)
00194 resize ( int( image.width()*fy ), h );
00195 else
00196 resize ( w, int( h2*fx ) );
00197 }
00198
00199 show();
00200
00201 }
00202
00203 void ImageViewer::saveFileToDisc()
00204 {
00205 KURL newURL = KFileDialog::getSaveURL(imageURL.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
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 ) {
00239 downloadJob->kill( true );
00240 kdDebug() << "Download job killed";
00241 }
00242 }
00243
00244 #include "imageviewer.moc"