[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: KLineEdit handling URL drops
From:       Matthias Kiefer <matthias.kiefer () gmx ! net>
Date:       2001-01-31 14:24:06
[Download RAW message or body]

Am Mittwoch, 31. Januar 2001 14:10 schrieb Carsten Pfeiffer:
> On Tue, Jan 30, 2001 at 10:48:50AM +0100, Matthias Kiefer wrote:
> yes. You can reimplement virtual methods without breaking BC (dropEvent is
> already virtual in QWidget).
OK, if you say it ;-)


> > Ok, done. But I had to use the eventFilter here instead of dropEvent.
>
> Because the lineedit receives the dropEvent, right?
Yes. 


> > I have also changed two other things: URLs are not inserted at the
> > current cursor position but at the end of the text and a whitespace is
> > added between the URLs, because that is how QLineEdit does it.
>
> I think you should also add whitespace before the very first URL, if the
> lineedit is not empty before the drop.
Good idea.
But I was wrong with the handling of drops by QLineEdit. It sets the cursor 
position to the position of the mouse cursor also when URL are dropped. The 
problem now is, that it uses a private method to convert the pixel position 
to the cursor position. I can't see a solution to this problem, so I left it 
to insert the string at the end of the text.


>
> One other thing: you should use KURLDrag instead of QUriDrag, because of
> encoding differences. It even simplifies the code a bit, because you get a
> KURL::List instead of QStrList.
Ah! I didn't know of that one.

Ok, here's the next try ;-)

kind regards,
   Mattias
-- 
Matthias Kiefer
E-Mail: kiefer@kde.org
KBabel: http://i18n.kde.org/tools/kbabel/




["klineedit.diff" (text/x-c++)]

Index: kcombobox.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kcombobox.cpp,v
retrieving revision 1.91
diff -u -r1.91 kcombobox.cpp
--- kcombobox.cpp	2001/01/19 12:13:24	1.91
+++ kcombobox.cpp	2001/01/31 14:22:21
@@ -32,6 +32,7 @@
 #include <kdebug.h>
 #include <kcompletionbox.h>
 #include <kurl.h>
+#include <kurldrag.h>
 #include <knotifyclient.h>
 #include <kiconloader.h>
 
@@ -44,6 +45,7 @@
 public:
     KCompletionBox *completionBox;
     QString origText;
+    bool handleURLDrops;
 };
 
 KComboBox::KComboBox( QWidget *parent, const char *name )
@@ -79,6 +81,7 @@
 {
     d = new KComboBoxPrivate;
     d->completionBox = 0L;
+    d->handleURLDrops=true;
 
     // Permanently set some parameters in the parent object.
     QComboBox::setAutoCompletion( false );
@@ -128,6 +131,18 @@
         m_bEnableMenu = showMenu;
 }
 
+
+void KComboBox::setURLDropsEnabled(bool enable)
+{
+    d->handleURLDrops=enable;
+}
+
+bool KComboBox::isURLDropsEnabled() const
+{
+    return d->handleURLDrops;
+}
+
+
 void KComboBox::setCompletedText( const QString& text, bool marked )
 {
     int pos = cursorPosition();
@@ -328,6 +343,7 @@
     QComboBox::keyPressEvent( e );
 }
 
+
 bool KComboBox::eventFilter( QObject* o, QEvent* ev )
 {
     if ( o == m_pEdit )
@@ -435,6 +451,31 @@
                         d->completionBox->hide();
                     emit completionModeChanged( completionMode() );
                 }
+                return true;
+            }
+        }
+        else if(type == QEvent::Drop)
+        { 
+            QDropEvent *e = static_cast<QDropEvent *>( ev );
+            KURL::List urlList;
+            if(d->handleURLDrops && KURLDrag::decode( e, urlList ))
+            {
+                QString dropText;
+                KURL::List::ConstIterator it;
+                for( it = urlList.begin() ; it != urlList.end() ; ++it)
+                {
+                    if(!dropText.isEmpty())
+                        dropText+=" ";
+            
+                    dropText += (*it).prettyURL();
+                }
+
+                if(!m_pEdit->text().isEmpty())
+                    dropText=" "+dropText;
+
+                m_pEdit->end(false);
+                m_pEdit->insert(dropText);
+
                 return true;
             }
         }
Index: kcombobox.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/kcombobox.h,v
retrieving revision 1.81
diff -u -r1.81 kcombobox.h
--- kcombobox.h	2001/01/29 12:23:30	1.81
+++ kcombobox.h	2001/01/31 14:22:23
@@ -127,6 +127,7 @@
   Q_OBJECT
   Q_PROPERTY( bool autoCompletion READ autoCompletion WRITE setAutoCompletion )
   Q_PROPERTY( bool contextMenuEnabled READ isContextMenuEnabled WRITE setContextMenuEnabled )
+  Q_PROPERTY( bool urlDropsEnabled READ isURLDropsEnabled WRITE setURLDropsEnabled )
 
 public:
 	
@@ -248,6 +249,22 @@
     * @return @p true if context menu is enabled.
     */
     bool isContextMenuEnabled() const { return m_bEnableMenu; }
+
+    /**
+     * Enables/Disables handling of URL drops. If enabled and the user
+     * drops an URL, the decoded URL will be inserted. Otherwise the default
+     * behaviour of QComboBox is used, which inserts the encoded URL.
+     *
+     * @param enable If @p true, insert decoded URLs
+     */
+    void setURLDropsEnabled( bool enable );
+
+    /**
+     * Returns @p true when decoded URL drops are enabled
+     * 
+     * @return @p true if decoded URL drops are enabled
+     */
+    bool isURLDropsEnabled() const;
 
     /**
     * Returns @p true if the combo-box is editable.
Index: klineedit.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/klineedit.cpp,v
retrieving revision 1.70
diff -u -r1.70 klineedit.cpp
--- klineedit.cpp	2001/01/19 12:13:24	1.70
+++ klineedit.cpp	2001/01/31 14:22:24
@@ -33,6 +33,7 @@
 #include <kdebug.h>
 #include <kcompletionbox.h>
 #include <kurl.h>
+#include <kurldrag.h>
 #include <kiconloader.h>
 
 #include "klineedit.h"
@@ -43,6 +44,7 @@
 {
 public:
     bool grabReturnKeyEvents;
+    bool handleURLDrops;
     KCompletionBox *completionBox;
     QString origText ;
 };
@@ -70,6 +72,7 @@
 {
     d = new KLineEditPrivate;
     d->grabReturnKeyEvents = false;
+    d->handleURLDrops = true;
     d->completionBox = 0L;
 
     // Enable the context menu by default.
@@ -356,6 +359,37 @@
     QLineEdit::mousePressEvent( e );
 }
 
+
+void KLineEdit::dropEvent(QDropEvent *e)
+{
+    KURL::List urlList;
+    if(d->handleURLDrops && KURLDrag::decode( e, urlList ))
+    {
+        QString dropText;
+        KURL::List::ConstIterator it;
+        for( it = urlList.begin() ; it != urlList.end() ; ++it)
+        {
+            if(!dropText.isEmpty())
+                dropText+=" ";
+            
+            dropText += (*it).prettyURL();
+        }
+
+        if(!text().isEmpty())
+            dropText=" "+dropText;
+
+        end(false);
+        insert(dropText);
+
+        e->accept();
+    }
+    else
+    {
+        QLineEdit::dropEvent(e);
+    }
+}
+
+
 bool KLineEdit::eventFilter( QObject* o, QEvent* ev )
 {
     if ( o == this )
@@ -379,6 +413,16 @@
     return QLineEdit::eventFilter( o, ev );
 }
 
+
+void KLineEdit::setURLDropsEnabled(bool enable)
+{
+    d->handleURLDrops=enable;
+}
+
+bool KLineEdit::isURLDropsEnabled() const
+{
+    return d->handleURLDrops;
+}
 
 void KLineEdit::setTrapReturnKey( bool grab )
 {
Index: klineedit.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/klineedit.h,v
retrieving revision 1.59
diff -u -r1.59 klineedit.h
--- klineedit.h	2000/12/27 18:45:38	1.59
+++ klineedit.h	2001/01/31 14:22:25
@@ -134,6 +134,7 @@
 {
 	Q_OBJECT
 	Q_PROPERTY( bool contextMenuEnabled READ isContextMenuEnabled WRITE setContextMenuEnabled )
+	Q_PROPERTY( bool urlDropsEnabled READ isURLDropsEnabled WRITE setURLDropsEnabled )
 
 public:
 
@@ -212,6 +213,22 @@
     bool isContextMenuEnabled() const { return m_bEnableMenu; }
 
     /**
+     * Enables/Disables handling of URL drops. If enabled and the user
+     * drops an URL, the decoded URL will be inserted. Otherwise the default
+     * behaviour of QLineEdit is used, which inserts the encoded URL.
+     *
+     * @param enable If @p true, insert decoded URLs
+     */
+    void setURLDropsEnabled( bool enable );
+
+    /**
+     * Returns @p true when decoded URL drops are enabled
+     * 
+     * @return @p true if decoded URL drops are enabled
+     */
+    bool isURLDropsEnabled() const;
+    
+    /**
      * By default, @ref KComboBox recognizes @p Key_Return and @p Key_Enter and emits
      * the @ref returnPressed() signals, but it also lets the event pass,
      * for example causing a dialog's default-button to be called.
@@ -348,6 +365,14 @@
     * See @ref QLineEdit::mousePressEvent().
     */
     virtual void mousePressEvent( QMouseEvent * );
+
+    /**
+    * Re-implemented to handle URI drops.
+    *
+    * See @ref QLineEdit::dropEvent().
+    */
+    virtual void dropEvent( QDropEvent * );
+
 
     /*
     * This function simply sets the lineedit text and


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic