21 #include <QApplication>
22 #include <QDesktopWidget>
23 #include <QTextStream>
25 #include <kio/copyjob.h>
27 #include <kdeversion.h>
28 #include <kpushbutton.h>
29 #include <klineedit.h>
30 #include <kmessagebox.h>
32 #include <kurlrequester.h>
34 #include <kstandarddirs.h>
45 :
KDialog( parent ), SelectedImageIndex(-1), dd((
DetailDialog*)parent), Object(o), bImageFound( false )
49 Image =
new QPixmap( current );
50 ImageRect =
new QRect( 0, 0, 200, 200 );
55 setButtons( KDialog::Ok|KDialog::Cancel );
57 ui->CurrentImage->setPixmap( *Image );
59 connect( ui->EditButton, SIGNAL( clicked() ),
this, SLOT( slotEditImage() ) );
60 connect( ui->UnsetButton, SIGNAL( clicked() ),
this, SLOT( slotUnsetImage() ) );
61 connect( ui->ImageList, SIGNAL( currentRowChanged(
int ) ),
62 this, SLOT( slotSetFromList(
int ) ) );
63 connect( ui->ImageURLBox, SIGNAL( urlSelected(
const KUrl& ) ),
64 this, SLOT( slotSetFromURL() ) );
65 connect( ui->ImageURLBox, SIGNAL( returnPressed() ),
66 this, SLOT( slotSetFromURL() ) );
68 ui->ImageURLBox->lineEdit()->setTrapReturnKey(
true );
69 ui->EditButton->setEnabled(
false );
75 while ( ! PixList.isEmpty() )
delete PixList.takeFirst();
79 void ThumbnailPicker::slotFillList() {
81 QStringList ImageList( Object->
ImageList() );
84 KUrl gURL(
"http://images.google.com/images" );
86 QString sName = QString(
"\"%1\"").arg( Object->
name() );
88 sName = QString(
"\"%1\" ").arg( Object->
longname() ) + sName;
90 gURL.addQueryItem(
"q", sName );
93 parseGooglePage( ImageList, gURL.prettyUrl() );
96 int nImages = ImageList.count();
98 ui->SearchProgress->setMinimum( 0 );
99 ui->SearchProgress->setMaximum( nImages-1 );
100 ui->SearchLabel->setText( i18n(
"Loading images..." ) );
104 for (
int i=0; i<ImageList.size(); ++i ) {
105 QString s( ImageList[i] );
106 KUrl u( ImageList[i] );
109 KIO::StoredTransferJob *j = KIO::storedGet( u, KIO::NoReload, KIO::HideProgressInfo );
111 connect( j, SIGNAL( result(KJob*) ), SLOT( slotJobResult(KJob*) ) );
116 void ThumbnailPicker::slotJobResult( KJob *job ) {
117 KIO::StoredTransferJob *stjob = (KIO::StoredTransferJob*)job;
120 if ( ! ui->SearchProgress->isHidden() ) {
121 ui->SearchProgress->setValue(ui->SearchProgress->value()+1);
122 if ( ui->SearchProgress->value() == ui->SearchProgress->maximum() ) {
123 ui->SearchProgress->hide();
124 ui->SearchLabel->setText( i18n(
"Search results:" ) );
129 if ( job->error() ) {
130 kDebug() <<
" error=" << job->error();
135 QPixmap *pm =
new QPixmap();
136 pm->loadFromData( stjob->data() );
138 uint w = pm->width();
139 uint h = pm->height();
141 uint hDesk = QApplication::desktop()->availableGeometry().height() - pad;
144 *pm = pm->scaled( w*hDesk/h, hDesk, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
146 PixList.append( pm );
151 ui->ImageList->addItem(
new QListWidgetItem ( QIcon(shrinkImage( PixList.last(), 50 )), stjob->url().prettyUrl() ));
154 void ThumbnailPicker::parseGooglePage( QStringList &ImList,
const QString &URL ) {
159 if ( KIO::NetAccess::exists(URL, KIO::NetAccess::SourceSide,
this) && KIO::NetAccess::download( URL, tmpFile,
this ) ) {
160 QFile file( tmpFile );
161 if ( file.open( QIODevice::ReadOnly ) ) {
163 PageHTML = instream.readAll();
165 KIO::NetAccess::removeTempFile( tmpFile );
167 kDebug() <<
"Could not read local copy of google image page";
168 KIO::NetAccess::removeTempFile( tmpFile );
172 kDebug() << KIO::NetAccess::lastErrorString();
176 int index = PageHTML.indexOf(
"?imgurl=", 0 );
177 while ( index >= 0 ) {
181 ImList.append( PageHTML.mid( index, PageHTML.indexOf(
"&", index ) - index ) );
183 index = PageHTML.indexOf(
"?imgurl=", index );
187 QPixmap ThumbnailPicker::shrinkImage( QPixmap *pm,
int size,
bool setImage ) {
188 int w( pm->width() ), h( pm->height() );
190 int rx(0), ry(0), sx(0), sy(0), bx(0), by(0);
191 if ( size == 0 )
return QPixmap();
194 if ( w > size && w >= h ) {
196 w = size*pm->width()/pm->height();
197 }
else if ( h > size && h > w ) {
199 h = size*pm->height()/pm->width();
203 if ( sx < 0 ) { rx = -sx; sx = 0; }
204 if ( sy < 0 ) { ry = -sy; sy = 0; }
206 if ( setImage ) bigSize = int( 200.*
float(pm->width())/
float(w) );
208 QPixmap result( size, size );
209 result.fill( QColor(
"white" ) );
211 if ( pm->width() > size || pm->height() > size ) {
213 QImage im( pm->toImage() );
214 im = im.scaled( w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
219 p.drawImage( rx, ry, im, sx, sy, size, size );
223 bx = int( sx*
float(pm->width())/
float(w) );
224 by = int( sy*
float(pm->width())/
float(w) );
225 ImageRect->setRect( bx, by, bigSize, bigSize );
231 p.drawImage( rx, ry, pm->toImage() );
235 bx = int( rx*
float(pm->width())/
float(w) );
236 by = int( ry*
float(pm->width())/
float(w) );
237 ImageRect->setRect( bx, by, bigSize, bigSize );
244 void ThumbnailPicker::slotEditImage() {
246 if ( te->exec() == QDialog::Accepted ) {
247 QPixmap pm = te->thumbnail();
249 ui->CurrentImage->setPixmap( pm );
250 ui->CurrentImage->update();
255 void ThumbnailPicker::slotUnsetImage() {
259 Image->load( file.fileName(),
"PNG" );
262 Image->fill( dd->palette().color( QPalette::Window ) );
265 ui->EditButton->setEnabled(
false );
266 ui->CurrentImage->setPixmap( *Image );
267 ui->CurrentImage->update();
272 void ThumbnailPicker::slotSetFromList(
int i ) {
275 pm = shrinkImage( PixList[i], 200,
true );
276 SelectedImageIndex = i;
278 ui->CurrentImage->setPixmap( pm );
279 ui->CurrentImage->update();
280 ui->EditButton->setEnabled(
true );
282 *Image = PixList[i]->scaled( wid, ht, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
286 void ThumbnailPicker::slotSetFromURL() {
288 KUrl u = ui->ImageURLBox->url();
291 if ( u.isLocalFile() ) {
292 QFile localFile( u.toLocalFile() );
296 QImage im( localFile.fileName() );
299 KMessageBox::sorry( 0,
300 i18n(
"Failed to load image at %1", localFile.fileName() ),
301 i18n(
"Failed to load image") );
306 uint h = im.height();
308 uint hDesk = QApplication::desktop()->availableGeometry().height() - pad;
311 im = im.scaled( w*hDesk/h, hDesk, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
314 PixList.insert( 0,
new QPixmap( QPixmap::fromImage( im ) ) );
315 ui->ImageList->insertItem( 0,
new QListWidgetItem ( QIcon(shrinkImage( PixList.last(), 50 )), u.prettyUrl() ));
318 ui->ImageList->setCurrentRow( 0 );
322 KIO::StoredTransferJob *j = KIO::storedGet( u, KIO::NoReload, KIO::HideProgressInfo );
324 connect( j, SIGNAL( result(KJob*) ), SLOT( slotJobResult(KJob*) ) );
329 #include "thumbnailpicker.moc"
window showing detailed information for a selected object.
ThumbnailPickerUI(QWidget *p)
virtual QString longname(void) const
bool openDataFile(QFile &file, const QString &filename)
Attempt to open the data file named filename, using the QFile object "file".
QStringList & ImageList()
ThumbnailPicker(SkyObject *o, const QPixmap ¤t, QWidget *parent=0, double w=200, double h=200, QString cap=i18n("Choose Thumbnail Image"))
virtual QString name(void) const
Provides all necessary information about an object in the sky: its coordinates, name(s), type, magnitude, and QStringLists of URLs for images and webpages regarding the object.