23 #include <QResizeEvent>
25 #include <QPaintEvent>
26 #include <QCloseEvent>
27 #include <QDesktopWidget>
28 #include <QVBoxLayout>
29 #include <QApplication>
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>
40 #include <ktemporaryfile.h>
46 setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
47 setFrameStyle( QFrame::StyledPanel | QFrame::Plain );
59 x = (width() -
m_Image.width())/2;
70 init(url.fileName(), capText);
72 setButtons( KDialog::User2 | KDialog::User1 | KDialog::Close );
74 KGuiItem saveButton( i18n(
"Save"),
"document-save", i18n(
"Save the image to disk") );
75 setButtonGuiItem( KDialog::User1, saveButton );
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 );
81 connect(
this, SIGNAL( user1Clicked() ),
this, SLOT ( saveFileToDisc() ) );
82 connect(
this, SIGNAL( user2Clicked() ),
this, SLOT ( invertColors() ) );
84 if (!m_ImageUrl.isValid())
85 kDebug() <<
"URL is malformed: " << m_ImageUrl;
89 KTemporaryFile tempfile;
91 file.setFileName( tempfile.fileName() );
102 init(FileName, QString());
103 file.setFileName( FileName );
107 void ImageViewer::init(QString caption, QString capText) {
108 setAttribute( Qt::WA_DeleteOnClose,
true );
110 setCaption( i18n(
"KStars image viewer: %1", caption ) );
111 setButtons( KDialog::Close | KDialog::User1);
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() ) );
121 setMainWidget( 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 );
129 QVBoxLayout* vlay =
new QVBoxLayout( page );
130 vlay->setSpacing( 0 );
131 vlay->setMargin( 0 );
132 vlay->addWidget( m_View );
133 vlay->addWidget( m_Caption );
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 );
143 QFont capFont = m_Caption->font();
144 capFont.setPointSize( capFont.pointSize() - 2 );
145 m_Caption->setFont( capFont );
151 downloadJob->kill( KJob::Quietly );
156 void ImageViewer::loadImageFromURL()
158 KUrl saveURL = KUrl::fromPath( file.fileName() );
159 if (!saveURL.isValid())
160 kDebug()<<
"tempfile-URL is malformed\n";
162 downloadJob = KIO::copy (m_ImageUrl, saveURL);
163 connect (downloadJob, SIGNAL (result (KJob *)), SLOT (downloadReady (KJob *)));
166 void ImageViewer::downloadReady (KJob *job)
171 if ( job->error() ) {
172 static_cast<KIO::Job*
>(job)->ui()->showErrorMessage();
179 if ( file.exists() ) {
186 void ImageViewer::showImage()
189 if( !image.load( file.fileName() )) {
190 QString text = i18n (
"Loading of the image %1 failed.", m_ImageUrl.prettyUrl());
191 KMessageBox::error (
this, text);
199 QRect deskRect = QApplication::desktop()->availableGeometry();
200 int w = deskRect.width();
201 int h = deskRect.height();
203 if ( image.width() <= w && image.height() > h )
204 image = image.scaled(
int( image.width()*h/image.height() ), h );
205 else if ( image.height() <= h && image.width() > w )
206 image = image.scaled( w,
int( image.height()*w/image.width() ) );
207 else if ( image.width() > w && image.height() > h ) {
209 float fx = float(w)/float(image.width());
210 float fy = float(h)/float(image.height());
212 image = image.scaled(
int( image.width()*fy ), h );
214 image = image.scaled( w,
int( image.height()*fx ) );
224 if ( m_Caption->width() > w )
225 w = m_Caption->width();
226 setFixedSize( w, image.height() + m_Caption->height() );
231 void ImageViewer::saveFileToDisc()
233 KUrl newURL = KFileDialog::getSaveUrl(m_ImageUrl.fileName());
234 if (!newURL.isEmpty())
236 QFile
f (newURL.directory() +
'/' + newURL.fileName());
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;
252 void ImageViewer::saveFile (KUrl &url) {
254 if (!KIO::NetAccess::file_copy (KUrl (file.fileName()), url, (
QWidget*) 0))
256 QString text = i18n (
"Saving of the image %1 failed.", url.prettyUrl());
257 KMessageBox::error (
this, text);
261 void ImageViewer::invertColors() {
263 m_View->
m_Image.invertPixels();
267 #include "imageviewer.moc"
ImageLabel(QWidget *parent)
void paintEvent(QPaintEvent *e)
~ImageViewer()
Destructor.
void setImage(const QImage &img)
ImageViewer(const KUrl &imageURL, const QString &capText, QWidget *parent=0)
Create image viewer from URL with caption.