[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-30 9:48:50
[Download RAW message or body]

Am Dienstag, 30. Januar 2001 00:02 schrieb Carsten Pfeiffer:
> And could you use dropEvent() instead of the eventFilter, this
> one is already so crowded ;(
I am not sure if this keeps binary compatibility. On developer.kde.org it reads
"You can reimplement virtual functions defined in one of the base classes _if_...
This is tricky and might be dangerous"
Is this ok here?

Am Dienstag, 30. Januar 2001 00:24 schrieb Dawit Alemayehu:
> On Monday 29 January 2001 16:47, Matthias Kiefer wrote:
> > I would like to commit the attached patch to KLineEdit. It correctly
> > handles URL drops, because I think it is more likely that users want to
> > have the decoded url pasted instead the encoded url.

> Well the patch looks good with a few exceptions.  I do not like the
> distinction made for the "mailto" url.  There are/might/will be other
> protocols that have the same scheme as the mailto and they would
> not be treated the same :(
Yes, that's right. I thought about dragging email addresses from kmail to somewhere else,
but since one can not know, if the URL is wanted or only the mail address, I left it out now.


> 2.) The same patch needs to be made for KCombobox so that the behavior
> is consistent across both of these widgets.
Ok, done. But I had to use the eventFilter here instead of dropEvent.
I am not sure, if I have to do something special when things are dropped onto
a KComboBox. Maybe checking if the text already exists as a different item?
On the other hand when text is dropped it is not checked, too.


> 3.) Make this a configurable option by adding something like the following
> two methods:
>
> setEnableDecodedURLDrop( bool );
> isDecodedURLDropEnabled() const;
>
> so that people would not complain about the reverse it not being to handle
> the previous behavior...
Right. I have added now setURLDropsEnabled(bool) and isURLDropsEnabled()
to both KLineEdit und KComboBox. Default is enabled.

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.

Greetings,
    Matthias
-- 
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/30 09:23:50
@@ -21,6 +21,7 @@
 */
 
 #include <qclipboard.h>
+#include <qdragobject.h>
 #include <qlistbox.h>
 
 #include <kcursor.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,28 @@
                         d->completionBox->hide();
                     emit completionModeChanged( completionMode() );
                 }
+                return true;
+            }
+        }
+        else if(type == QEvent::Drop)
+        { 
+            QDropEvent *e = static_cast<QDropEvent *>( ev );
+            if(d->handleURLDrops && QUriDrag::canDecode(e))
+            {
+                QStrList uriList;
+                QUriDrag::decode( e, uriList );
+
+                QStrListIterator it(uriList);
+                for(; it.current(); ++it)
+                {
+                    m_pEdit->end(false);
+                    KURL u(it.current());
+                    m_pEdit->insert(u.prettyURL());
+                    
+                    if(!it.atLast())
+                        m_pEdit->insert(" ");
+                }
+
                 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/30 09:23:52
@@ -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/30 09:23:53
@@ -25,6 +25,7 @@
 */
 
 #include <qclipboard.h>
+#include <qdragobject.h>
 
 #include <kcursor.h>
 #include <klocale.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,33 @@
     QLineEdit::mousePressEvent( e );
 }
 
+
+void KLineEdit::dropEvent(QDropEvent *e)
+{
+    if(d->handleURLDrops && QUriDrag::canDecode(e))
+    {
+        QStrList uriList;
+        QUriDrag::decode( e, uriList );
+
+        QStrListIterator it(uriList);
+        for(; it.current(); ++it)
+        {
+            end(false);
+            
+            KURL u(it.current());
+            insert(u.prettyURL());
+
+            if(!it.atLast())
+                insert(" ");
+        }
+    }
+    else
+    {
+        QLineEdit::dropEvent(e);
+    }
+}
+
+
 bool KLineEdit::eventFilter( QObject* o, QEvent* ev )
 {
     if ( o == this )
@@ -379,6 +409,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/30 09:23:54
@@ -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