[Kstars-devel] kdeedu/kstars/kstars

Jason Harris kstars at 30doradus.org
Wed Mar 9 09:06:05 CET 2005


CVS commit by harris: 

Added Google image search to the Thumbnail image selector tool.  Pretty neat!

It isn't perfect; try opening the details window for IC 440, and click on the 
image box to load the image search.  None of the images found are actually of 
the object.  Any ideas how to improve this?  It currently searches for '"long 
name" "primary name"' if both exist, and "primary name" if there is no long 
name.

Do you think we need to inform/warn users that an image search is taking 
place here?  Is it important to provide an option to [dis|en]able image 
search in this tool?

TODO: if an image isn't found, I'd like to suppress the message box.
TODO: if the image is smaller than 200x200, need to fill the background.

CCMAIL: kstars-devel at kde.org


  M +54 -5     thumbnailpicker.cpp   1.4
  M +1 -0      thumbnailpicker.h   1.2


--- kdeedu/kstars/kstars/thumbnailpicker.cpp  #1.3:1.4
@@ -70,6 +70,21 @@ ThumbnailPicker::~ThumbnailPicker()
 //Query online sources for images of the object
 void ThumbnailPicker::slotFillList() {
-        //Number of images to be loaded:
-        int nImages = Object->ImageList.count();
+        //Preload list with object's ImageList:
+        QStringList ImageList( Object->ImageList );
+
+        //Query Google Image Search:
+        KURL gURL( "http://images.google.com/images" );
+        //Search for the primary name, or longname and primary name
+        QString sName = QString("\"%1\"").arg( Object->name() );
+        if ( Object->longname() != Object->name() ) {
+                sName = QString("\"%1\" ").arg( Object->longname() ) + sName;
+        }
+        gURL.addQueryItem( "q", sName ); //add the Google-image query string
+
+        //Download the google page and parse it for image URLs
+        parseGooglePage( ImageList, gURL.prettyURL() );
+
+        //Total Number of images to be loaded:
+        int nImages = ImageList.count();
         if ( nImages ) {
                 ui->SearchProgress->setTotalSteps( nImages );
@@ -77,7 +92,7 @@ void ThumbnailPicker::slotFillList() {
         }
 
-        //First add images from object's ImageList
-        QStringList::Iterator itList  = Object->ImageList.begin();
-        QStringList::Iterator itListEnd = Object->ImageList.end();
+        //Add images from the ImageList
+        QStringList::Iterator itList  = ImageList.begin();
+        QStringList::Iterator itListEnd = ImageList.end();
         for ( ; itList != itListEnd; ++itList ) {
                 QString s( *itList );
@@ -94,4 +109,38 @@ void ThumbnailPicker::slotFillList() {
 }
 
+void ThumbnailPicker::parseGooglePage( QStringList &ImList, QString URL ) {
+        QString tmpFile;
+        QString PageHTML;
+
+        //Read the google image page's HTML into the PageHTML QString:
+        if ( KIO::NetAccess::download( URL, tmpFile ) ) {
+                QFile file( tmpFile );
+                if ( file.open( IO_ReadOnly ) ) {
+                        QTextStream instream(&file);
+                        PageHTML = instream.read();
+                        file.close();
+                } else {
+                        kdDebug() << "Could not read local copy of google image page" << endl;
+                        return;
+                }
+        } else {
+                kdDebug() << KIO::NetAccess::lastErrorString() << endl;
+                return;
+        }
+
+        int index = PageHTML.find( "?imgurl=", 0 );
+        while ( index >= 0 ) {
+                index += 8; //move to end of "?imgurl=" marker
+
+                //Image URL is everything from index to next occurence of "&"
+                ImList.append( PageHTML.mid( index, PageHTML.find( "&", index ) - index ) );
+
+//              //DEBUG
+//              kdDebug() << index << "::" << ImList.last() << endl;
+
+                index = PageHTML.find( "?imgurl=", index );
+        }
+}
+
 void ThumbnailPicker::downloadReady(KIO::Job *job) {
         //Note: no need to delete the job, it is automatically deleted !

--- kdeedu/kstars/kstars/thumbnailpicker.h  #1.1:1.2
@@ -58,4 +58,5 @@ private slots:
 private:
         QPixmap shrinkImage( QPixmap *original, int newWidth, int newHieght );
+        void parseGooglePage( QStringList &ImList, QString URL );
 
         ThumbnailPickerUI *ui;




More information about the Kstars-devel mailing list