• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
imageviewer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  imageviewer.cpp - An ImageViewer for KStars
3  -------------------
4  begin : Mon Aug 27 2001
5  copyright : (C) 2001 by Thomas Kabelmann
6  email : tk78@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "imageviewer.h"
19 #include "kstars.h"
20 
21 #include <QFont>
22 #include <QPainter>
23 #include <QResizeEvent>
24 #include <QKeyEvent>
25 #include <QPaintEvent>
26 #include <QCloseEvent>
27 #include <QDesktopWidget>
28 #include <QVBoxLayout>
29 #include <QApplication>
30 #include <QLabel>
31 
32 #include <klocale.h>
33 #include <kmessagebox.h>
34 #include <kfiledialog.h>
35 #include <kstatusbar.h>
36 #include <kio/netaccess.h>
37 #include <kio/copyjob.h>
38 #include <kio/jobuidelegate.h>
39 #include <kaction.h>
40 #include <ktemporaryfile.h>
41 #include <kdebug.h>
42 #include <ktoolbar.h>
43 
44 ImageLabel::ImageLabel( QWidget *parent ) : QFrame( parent )
45 {
46  setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
47  setFrameStyle( QFrame::StyledPanel | QFrame::Plain );
48  setLineWidth( 2 );
49 }
50 
51 ImageLabel::~ImageLabel()
52 {}
53 
54 void ImageLabel::paintEvent (QPaintEvent*) {
55  QPainter p;
56  p.begin( this );
57  int x = 0;
58  if( m_Image.width() < width() )
59  x = (width() - m_Image.width())/2;
60  p.drawImage( x, 0, m_Image );
61  p.end();
62 }
63 
64 ImageViewer::ImageViewer (const KUrl &url, const QString &capText, QWidget *parent) :
65  KDialog( parent ),
66  m_ImageUrl(url),
67  fileIsImage(false),
68  downloadJob(0)
69 {
70  init(url.fileName(), capText);
71  // Add save button
72  setButtons( KDialog::User2 | KDialog::User1 | KDialog::Close );
73 
74  KGuiItem saveButton( i18n("Save"), "document-save", i18n("Save the image to disk") );
75  setButtonGuiItem( KDialog::User1, saveButton );
76 
77  // FIXME: Add more options, and do this more nicely
78  KGuiItem invertButton( i18n("Invert colors"), "", i18n("Reverse colors of the image. This is useful to enhance contrast at times. This affects only the display and not the saving.") );
79  setButtonGuiItem( KDialog::User2, invertButton );
80 
81  connect( this, SIGNAL( user1Clicked() ), this, SLOT ( saveFileToDisc() ) );
82  connect( this, SIGNAL( user2Clicked() ), this, SLOT ( invertColors() ) );
83  // check URL
84  if (!m_ImageUrl.isValid())
85  kDebug() << "URL is malformed: " << m_ImageUrl;
86 
87  // FIXME: check the logic with temporary files. Races are possible
88  {
89  KTemporaryFile tempfile;
90  tempfile.open();
91  file.setFileName( tempfile.fileName() );
92  }// we just need the name and delete the tempfile from disc; if we don't do it, a dialog will be show
93 
94  loadImageFromURL();
95 }
96 
97 ImageViewer::ImageViewer ( QString FileName, QWidget *parent ) :
98  KDialog( parent ),
99  fileIsImage(true),
100  downloadJob(0)
101 {
102  init(FileName, QString());
103  file.setFileName( FileName );
104  showImage();
105 }
106 
107 void ImageViewer::init(QString caption, QString capText) {
108  setAttribute( Qt::WA_DeleteOnClose, true );
109  setModal( false );
110  setCaption( i18n( "KStars image viewer: %1", caption ) );
111  setButtons( KDialog::Close | KDialog::User1);
112 
113  // FIXME: Add more options, and do this more nicely
114  KGuiItem invertButton( i18n("Invert colors"), "", i18n("Reverse colors of the image. This is useful to enhance contrast at times. This affects only the display and not the saving.") );
115  setButtonGuiItem( KDialog::User1, invertButton );
116  connect( this, SIGNAL( user1Clicked() ), this, SLOT ( invertColors() ) );
117 
118 
119  // Create widget
120  QFrame* page = new QFrame( this );
121  setMainWidget( page );
122  m_View = new ImageLabel( page );
123  m_View->setAutoFillBackground( true );
124  m_Caption = new QLabel( page );
125  m_Caption->setAutoFillBackground( true );
126  m_Caption->setFrameShape( QFrame::StyledPanel );
127  m_Caption->setText( capText );
128  // Add layout
129  QVBoxLayout* vlay = new QVBoxLayout( page );
130  vlay->setSpacing( 0 );
131  vlay->setMargin( 0 );
132  vlay->addWidget( m_View );
133  vlay->addWidget( m_Caption );
134 
135  //Reverse colors
136  QPalette p = palette();
137  p.setColor( QPalette::Window, palette().color( QPalette::WindowText ) );
138  p.setColor( QPalette::WindowText, palette().color( QPalette::Window ) );
139  m_Caption->setPalette( p );
140  m_View->setPalette( p );
141 
142  //If the caption is wider than the image, try to shrink the font a bit
143  QFont capFont = m_Caption->font();
144  capFont.setPointSize( capFont.pointSize() - 2 );
145  m_Caption->setFont( capFont );
146 }
147 
148 ImageViewer::~ImageViewer() {
149  if ( downloadJob ) {
150  // close job quietly, without emitting a result
151  downloadJob->kill( KJob::Quietly );
152  delete downloadJob;
153  }
154 }
155 
156 void ImageViewer::loadImageFromURL()
157 {
158  KUrl saveURL = KUrl::fromPath( file.fileName() );
159  if (!saveURL.isValid())
160  kDebug()<<"tempfile-URL is malformed\n";
161 
162  downloadJob = KIO::copy (m_ImageUrl, saveURL); // starts the download asynchron
163  connect (downloadJob, SIGNAL (result (KJob *)), SLOT (downloadReady (KJob *)));
164 }
165 
166 void ImageViewer::downloadReady (KJob *job)
167 {
168  // set downloadJob to 0, but don't delete it - the job will be deleted automatically !!!
169  downloadJob = 0;
170 
171  if ( job->error() ) {
172  static_cast<KIO::Job*>(job)->ui()->showErrorMessage();
173  close();
174  return;
175  }
176 
177  file.close(); // to get the newest information from the file and not any information from opening of the file
178 
179  if ( file.exists() ) {
180  showImage();
181  return;
182  }
183  close();
184 }
185 
186 void ImageViewer::showImage()
187 {
188  QImage image;
189  if( !image.load( file.fileName() )) {
190  QString text = i18n ("Loading of the image %1 failed.", m_ImageUrl.prettyUrl());
191  KMessageBox::error (this, text);
192  close();
193  return;
194  }
195  fileIsImage = true; // we loaded the file and know now, that it is an image
196 
197  //If the image is larger than screen width and/or screen height,
198  //shrink it to fit the screen
199  QRect deskRect = QApplication::desktop()->availableGeometry();
200  int w = deskRect.width(); // screen width
201  int h = deskRect.height(); // screen height
202 
203  if ( image.width() <= w && image.height() > h ) //Window is taller than desktop
204  image = image.scaled( int( image.width()*h/image.height() ), h );
205  else if ( image.height() <= h && image.width() > w ) //window is wider than desktop
206  image = image.scaled( w, int( image.height()*w/image.width() ) );
207  else if ( image.width() > w && image.height() > h ) { //window is too tall and too wide
208  //which needs to be shrunk least, width or height?
209  float fx = float(w)/float(image.width());
210  float fy = float(h)/float(image.height());
211  if (fx > fy) //width needs to be shrunk less, so shrink to fit in height
212  image = image.scaled( int( image.width()*fy ), h );
213  else //vice versa
214  image = image.scaled( w, int( image.height()*fx ) );
215  }
216 
217  show(); // hide is default
218 
219  m_View->setImage( image );
220  w = image.width();
221 
222  //If the caption is wider than the image, set the window size
223  //to fit the caption
224  if ( m_Caption->width() > w )
225  w = m_Caption->width();
226  setFixedSize( w, image.height() + m_Caption->height() );
227 
228  update();
229 }
230 
231 void ImageViewer::saveFileToDisc()
232 {
233  KUrl newURL = KFileDialog::getSaveUrl(m_ImageUrl.fileName()); // save-dialog with default filename
234  if (!newURL.isEmpty())
235  {
236  QFile f (newURL.directory() + '/' + newURL.fileName());
237  if (f.exists())
238  {
239  int r=KMessageBox::warningContinueCancel(static_cast<QWidget *>(parent()),
240  i18n( "A file named \"%1\" already exists. "
241  "Overwrite it?" , newURL.fileName()),
242  i18n( "Overwrite File?" ),
243  KStandardGuiItem::overwrite() );
244  if(r==KMessageBox::Cancel) return;
245 
246  f.remove();
247  }
248  saveFile (newURL);
249  }
250 }
251 
252 void ImageViewer::saveFile (KUrl &url) {
253  // synchronous access to prevent segfaults
254  if (!KIO::NetAccess::file_copy (KUrl (file.fileName()), url, (QWidget*) 0))
255  {
256  QString text = i18n ("Saving of the image %1 failed.", url.prettyUrl());
257  KMessageBox::error (this, text);
258  }
259 }
260 
261 void ImageViewer::invertColors() {
262  // Invert colors
263  m_View->m_Image.invertPixels();
264  m_View->update();
265 }
266 
267 #include "imageviewer.moc"
ImageLabel::ImageLabel
ImageLabel(QWidget *parent)
Definition: imageviewer.cpp:44
imageviewer.h
QWidget
ImageLabel::~ImageLabel
~ImageLabel()
Definition: imageviewer.cpp:51
ImageLabel::paintEvent
void paintEvent(QPaintEvent *e)
Definition: imageviewer.cpp:54
KDialog
ImageViewer::~ImageViewer
~ImageViewer()
Destructor.
Definition: imageviewer.cpp:148
NaN::f
const float f
Definition: nan.h:36
ImageLabel::m_Image
QImage m_Image
Definition: imageviewer.h:40
ImageLabel::setImage
void setImage(const QImage &img)
Definition: imageviewer.h:38
QLabel
ImageViewer::ImageViewer
ImageViewer(const KUrl &imageURL, const QString &capText, QWidget *parent=0)
Create image viewer from URL with caption.
Definition: imageviewer.cpp:64
QFrame
kstars.h
ImageLabel
Definition: imageviewer.h:33
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal