/***************************************************************************
                          imageviewer.h  -  An ImageViewer for KStars
                             -------------------
    begin                : Mon Aug 27 2001
    copyright            : (C) 2001 by Thomas Kabelmann
    email                : tk78@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef IMAGEVIEWER_H
#define IMAGEVIEWER_H

#include <qwidget.h>
#include <qstring.h>
#include <qimage.h>
#include <qpixmap.h>

#include <kpixmapio.h>
#include <kio/job.h>
#include <ktempfile.h>
#include <kdialog.h>
#include <kmainwindow.h>

/**
  *@short Image viewer widget for KStars
  *@author Thomas Kabelmann
  *@version 0.9

  This image-viewer resizes automatically the picture. The output works with kio-slaves and not directly with the
  QImage save-routines because normally the image-files are in GIF-format and QT does not save these files. For other
  formats, like PNG, this is not so important because they can directly saved by QImage.

  The download-slave works asynchron so the parent-widget can be used at this time. The save-slave works synchron,
  but this is not important because the files are at this time local saved and this works not so long.
*/

class KURL;
class QFile;

class ImageViewer : public KMainWindow  {
	Q_OBJECT

	public:
/**Constructor. */
		ImageViewer (const KURL *imageName, QWidget *parent, const char *name = 0);

/**Destructor. If there is a partially downloaded image file, delete it.*/
		~ImageViewer();

	signals:
/**The download has begun. */
		void StartDownload( void );
/**The download has finished. */
		void DownloadComplete( void );
		
	protected:
/**Bitblt the image onto the viewer widget */
		void paintEvent (QPaintEvent *ev);

/**The window is resized when a file finishes downloading, before it is displayed.
  *The resizeEvent converts the downloaded QImage to a QPixmap (JH: not sure why it's not done in showImage)
  */
		void resizeEvent (QResizeEvent *ev);

/**Make sure all events have been processed before closing the dialog */
		void closeEvent (QCloseEvent *ev);

/**Keyboard shortcuts for saving files and closing the window
  *(this should be deprecated; instead just assign KAccel to the close/save buttons)
  */
		void keyPressEvent (QKeyEvent *ev);

/**Unset the bool variables that indicate keys were pressed.
	*(this should be deprecated; see above)
	*/
		void keyReleaseEvent (QKeyEvent *ev);

	private:
/**Display the downloaded image.  Resize the window to fit the image,  If the image is
  *larger than the screen, make the image as large as possible while preserving the
  *original aspect ratio
	*/
		void showImage( void );

/**Download the image file pointed to by the URL string.
	*/
		void loadImageFromURL( void );

/**Save the downloaded image to a local file.
	*/
		void saveFile (KURL &url);
		
		QImage image;
		QPixmap pix;
		KPixmapIO kpix;
		KTempFile tempfile;
		QFile *file;
				
		const KURL imageURL;
		bool downloadComplete, fileIsImage;
		QString filename;
		bool ctrl, key_s, key_q;	// the keys
		
	private slots:
/**Make sure download has finished, then make sure file exists, then display the image */
		void downloadReady (KIO::Job *);

/**Saves. File. To. Disc. */
		void saveFileToDisc( void );

/**Close the window */
		void close( void );
};

#endif
