[PATCH] Add image thumbnail when drag-and-dropping images.

Michael Pyne pynm0001 at comcast.net
Sun Jan 2 19:00:40 GMT 2005


On Sunday 02 January 2005 01:29 pm, Leo Savernik wrote:
> Am Freitag, 31. Dezember 2004 22:47 schrieb Michael Pyne:
> > I'm including a patch to set a thumbnail image when dragging an image
> > (that doesn't link to something) from KHTML.  You can see a screenshot of
> > the effect at
> > http://grammarian.homelinux.net/~kde-cvs/khtml-drag-drop-screenie.png
> > (51.7 KB).
>
> Nice, but isn't that waaay too big? Wouldn't an icon-sized icon be better
> (with 64x64 at the maximum)?

Well it's that large on purpose. ;-)

However, making it smaller would have the added benefit of making the 
rendering operations take almost no time at all (except for the resizing, 
which is completely dependant on the base image size), so I'll probably go 
ahead and make it smaller. ;)

The patch I'm including has the Q_WS_X11 change, and the maximum size is now 
128x96.  It looks real bad on my monitor any lower.  If it's still too big I 
could probably make the size adjustable based on the width of the current 
screen, but that simply strikes me as overkill. ;)

Also, I tried using QImage::scale() instead of QImage::smoothScale(), but 
there wasn't too much of a speedup, and the end result looked ridiculous.

Regards,
 - Michael Pyne

Index: khtml_part.cpp
===================================================================
RCS file: /home/kde/kdelibs/khtml/khtml_part.cpp,v
retrieving revision 1.1070
diff -u -3 -p -r1.1070 khtml_part.cpp
--- khtml_part.cpp      31 Dec 2004 21:42:23 -0000      1.1070
+++ khtml_part.cpp      2 Jan 2005 18:56:37 -0000
@@ -60,11 +60,14 @@ using namespace DOM;
 #include <kjs/function.h>
 #include <kjs/interpreter.h>

+#include <kimageeffect.h>
+
 #include "htmlpageinfo.h"

 #include <sys/types.h>
 #include <assert.h>
 #include <unistd.h>
+#include <stdlib.h>

 #include <config.h>

@@ -6024,9 +6027,36 @@ void KHTMLPart::khtmlMouseMoveEvent( kht
     // Normal image...
     if ( url.length() == 0 && innerNode.handle() && innerNode.handle()->id() 
== ID_IMG )
     {
-      img = static_cast<HTMLImageElementImpl *>(innerNode.handle());
-      u = 
KURL( completeURL( khtml::parseURL(img->getAttribute(ATTR_SRC)).string() ) );
-      pix = KMimeType::mimeType("image/png")->pixmap(KIcon::Desktop);
+      // Initialize pix to the default value.  Although this is possibly a
+      // slight performance detriment, it makes the code much cleaner.
+      pix = KMimeType::mimeType( "image/png" )->pixmap( KIcon::Desktop );
+
+#ifdef Q_WS_X11
+      img = static_cast<HTMLImageElementImpl *>( innerNode.handle() );
+      u = 
KURL( completeURL( khtml::parseURL( img->getAttribute( ATTR_SRC ) ).string() ) );
+      char *displayName = getenv ( "DISPLAY" );
+
+      const int maxWidth = 128;
+      const int maxHeight = 96;
+
+      // Check if the X Server is running locally.  I wish I knew a better 
way
+      // to do this.  Also make sure the image has actually downloaded.
+      if ( displayName && displayName[0] == ':' && img->complete() )
+      {
+        QImage image = img->currentImage();
+        if ( image.width() > maxWidth || image.height() > maxHeight )
+          image = image.smoothScale ( maxWidth, maxHeight, 
QImage::ScaleMin );
+
+        // Only use the thumbnail if the image is "large enough", as there's
+        // no reason to thumbnail e.g. a 1x1 invisible gif.
+        if ( image.width() > 6 && image.height() > 6 )
+        {
+          KImageEffect::fade ( image, 0.37, gray );
+          pix = QPixmap ( image );
+        }
+      }
+#endif // Q_WS_X11
+
     }
     else
     {




More information about the kfm-devel mailing list