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

List:       kde-core-devel
Subject:    [PATCH] KEditListBox::setCustomEditor
From:       Sebastian =?iso-8859-1?q?Tr=FCg?= <strueg () mandriva ! com>
Date:       2008-01-17 21:02:49
Message-ID: 200801172202.50125.strueg () mandriva ! com
[Download RAW message or body]

Hi guys,

Attached is a patch that introduces a new method in KEditListBox to set the 
custom editor after creation. This is very much needed for usage of 
KEditListBox with QDesigner.
The patch is BC as it only introduces two new non-virtual methods (one 
private, one public).

May I commit?

Cheers,
Sebastian

["keditlistbox-custom-editor.diff" (text/x-diff)]

Index: widgets/keditlistbox.h
===================================================================
--- widgets/keditlistbox.h	(revision 762115)
+++ widgets/keditlistbox.h	(working copy)
@@ -144,7 +144,7 @@
        * KLineEdit as child-widget for example, so the KComboBox is used as
        * the representation widget.
        *
-       * @see KUrlRequester::customEditor()
+       * @see KUrlRequester::customEditor(), setCustomEditor
        */
       KEditListBox( const QString& title,
                     const CustomEditor &customEditor,
@@ -245,6 +245,17 @@
        */
       bool checkAtEntering();
 
+      /**
+       * Allows to use a custom editing widget
+       * instead of the standard KLineEdit widget. E.g. you can use a
+       * KUrlRequester or a KComboBox as input widget. The custom
+       * editor must consist of a lineedit and optionally another widget that
+       * is used as representation. A KComboBox or a KUrlRequester have a
+       * KLineEdit as child-widget for example, so the KComboBox is used as
+       * the representation widget.
+       */
+      void setCustomEditor( const CustomEditor& editor );
+
    Q_SIGNALS:
       void changed();
 
@@ -272,6 +283,7 @@
       //this is called in both ctors, to avoid code duplication
       void init( bool checkAtEntering = false, Buttons buttons = All,
                  QWidget *representationWidget = 0 );
+      void setEditor( KLineEdit* lineEdit, QWidget* representationWidget = 0 );
 
    private:
       friend class KEditListBoxPrivate;
Index: widgets/keditlistbox.cpp
===================================================================
--- widgets/keditlistbox.cpp	(revision 762115)
+++ widgets/keditlistbox.cpp	(working copy)
@@ -38,10 +38,16 @@
 class KEditListBoxPrivate
 {
 public:
+    KEditListBoxPrivate()
+        : m_lineEdit(0),
+          editingWidget(0) {
+    }
     QListView *m_listView;
     QPushButton *servUpButton, *servDownButton;
     QPushButton *servNewButton, *servRemoveButton;
     KLineEdit *m_lineEdit;
+    QWidget* editingWidget;
+    QGridLayout* mainLayout;
     QStringListModel *m_model;
 
     bool m_checkAtEntering;
@@ -158,39 +164,58 @@
     setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,
                               QSizePolicy::MinimumExpanding));
 
-    QGridLayout * grid = new QGridLayout(this);
-    grid->setMargin( KDialog::marginHint() );
-    grid->setSpacing( KDialog::spacingHint() );
-    grid->setRowStretch( 6, 1 );
+    d->mainLayout = new QGridLayout(this);
+    d->mainLayout->setMargin( KDialog::marginHint() );
+    d->mainLayout->setSpacing( KDialog::spacingHint() );
+    d->mainLayout->setRowStretch( 6, 1 );
 
-    if ( representationWidget )
-        representationWidget->setParent(this);
-    else
-        d->m_lineEdit = new KLineEdit(this);
-
     d->m_model = new QStringListModel();
     d->m_listView = new QListView(this);
 
     d->m_listView->setModel(d->m_model);
 
-    QWidget *editingWidget = representationWidget ?
-                             representationWidget : d->m_lineEdit;
-    grid->addWidget(editingWidget,1,0,1,2);
-    grid->addWidget(d->m_listView, 2, 0, 4, 1);
+    d->mainLayout->addWidget(d->m_listView, 2, 0, 4, 1);
 
+    setEditor( d->m_lineEdit, representationWidget );
+
     d->buttons = 0;
     setButtons( buttons );
 
+    connect(d->m_listView->selectionModel(), SIGNAL(currentChanged(const \
QModelIndex&, const QModelIndex&)), +            this,SLOT(enableMoveButtons(const \
QModelIndex&, const QModelIndex&))); +}
+
+void KEditListBox::setEditor( KLineEdit* lineEdit, QWidget* representationWidget )
+{
+    if (d->editingWidget != d->m_lineEdit &&
+        d->editingWidget != representationWidget) {
+        delete d->editingWidget;
+    }
+    if (d->m_lineEdit != lineEdit) {
+        delete d->m_lineEdit;
+    }
+    d->m_lineEdit = lineEdit ? lineEdit : new KLineEdit(this);
+    d->editingWidget = representationWidget ?
+                       representationWidget : d->m_lineEdit;
+
+    if ( representationWidget )
+        representationWidget->setParent(this);
+
+    d->mainLayout->addWidget(d->editingWidget,1,0,1,2);
+
     connect(d->m_lineEdit,SIGNAL(textChanged(const \
QString&)),this,SLOT(typedSomething(const QString&)));  \
d->m_lineEdit->setTrapReturnKey(true);  \
                connect(d->m_lineEdit,SIGNAL(returnPressed()),this,SLOT(addItem()));
-    connect(d->m_listView->selectionModel(), SIGNAL(currentChanged(const \
                QModelIndex&, const QModelIndex&)),
-            this,SLOT(enableMoveButtons(const QModelIndex&, const QModelIndex&)));
 
     // maybe supplied lineedit has some text already
     typedSomething( d->m_lineEdit->text() );
 }
 
+void KEditListBox::setCustomEditor( const CustomEditor& editor )
+{
+    setEditor( editor.lineEdit(), editor.representationWidget() );
+}
+
 QListView *KEditListBox::listView() const
 {
     return d->m_listView;



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

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