Refactor input dialog attached to common base class
authorKevin Krammer <kevin.krammer@kdab.com>
Fri, 2 Nov 2012 14:35:01 +0000 (15:35 +0100)
committerKevin Krammer <kevin.krammer@kdab.com>
Fri, 2 Nov 2012 15:20:24 +0000 (16:20 +0100)
lib/declarativeinputdialog.cpp
lib/declarativeinputdialog_p.h

index d23aacd..244bd2f 100644 (file)
@@ -30,7 +30,6 @@ class DeclarativeInputDialogAttached::Private
     {}
 
   public:
-    QPointer<QObject> dialogParent;
     QString title;
     QString label;
     bool dialogAccepted;
@@ -49,7 +48,7 @@ class DeclarativeInputDialogAttached::Private
 };
 
 DeclarativeInputDialogAttached::DeclarativeInputDialogAttached(QObject *parent)
-  : QObject(parent), d(new Private)
+  : StaticDialogMethodAttached(parent), d(new Private)
 {
 }
 
@@ -58,20 +57,6 @@ DeclarativeInputDialogAttached::~DeclarativeInputDialogAttached()
   delete d;
 }
 
-void DeclarativeInputDialogAttached::setDialogParent(QObject *parent)
-{
-  if (parent == d->dialogParent)
-    return;
-
-  d->dialogParent = parent;
-  emit dialogParentChanged(parent);
-}
-
-QObject *DeclarativeInputDialogAttached::dialogParent() const
-{
-  return d->dialogParent;
-}
-
 void DeclarativeInputDialogAttached::setTitle(const QString &title)
 {
   if (title == d->title)
@@ -233,7 +218,7 @@ QString DeclarativeInputDialogAttached::text() const
 
 double DeclarativeInputDialogAttached::getDouble()
 {
-  QWidget *parent = bestParentWindow(d->dialogParent);
+  QWidget *parent = bestParentWindow();
   bool ok = false;
   const double value = d->value.canConvert<double>() ? d->value.value<double>() : 0.0;
   const double min = d->min.canConvert<double>() ? d->min.value<double>() : -2147483647;
@@ -247,7 +232,7 @@ double DeclarativeInputDialogAttached::getDouble()
 
 int DeclarativeInputDialogAttached::getInt()
 {
-  QWidget *parent = bestParentWindow(d->dialogParent);
+  QWidget *parent = bestParentWindow();
   bool ok = false;
   const int value = d->value.canConvert<int>() ? d->value.value<int>() : 0;
   const int min = d->min.canConvert<int>() ? d->min.value<int>() : -2147483647;
@@ -261,7 +246,7 @@ int DeclarativeInputDialogAttached::getInt()
 
 QString DeclarativeInputDialogAttached::getItem(const QStringList &items)
 {
-  QWidget *parent = bestParentWindow(d->dialogParent);
+  QWidget *parent = bestParentWindow();
   bool ok = false;
 
   const QString retVal = QInputDialog::getItem(parent, d->title, d->label, items, d->currentItem, d->itemsEditable, &ok);
@@ -272,7 +257,7 @@ QString DeclarativeInputDialogAttached::getItem(const QStringList &items)
 
 QString DeclarativeInputDialogAttached::getText()
 {
-  QWidget *parent = bestParentWindow(d->dialogParent);
+  QWidget *parent = bestParentWindow();
   bool ok = false;
 
   const QString retVal = QInputDialog::getText(parent, d->title, d->label, d->echoMode, d->text, &ok);
@@ -290,27 +275,6 @@ void DeclarativeInputDialogAttached::setDialogAccepted(bool accepted)
   emit dialogAcceptedChanged(accepted);
 }
 
-QWidget *DeclarativeInputDialogAttached::bestParentWindow(QObject *parent) const
-{
-  if (!parent)
-    parent = this->parent();
-
-  // if parent is a Declarative Object, search the proxied hierarchy
-  AbstractDeclarativeObject *declarativeObject = dynamic_cast<AbstractDeclarativeObject*>(parent);
-  if (declarativeObject)
-    parent = declarativeObject->object();
-
-  while (parent) {
-    QWidget *widget = qobject_cast<QWidget*>(parent);
-    if (widget)
-      return widget->topLevelWidget();
-
-    parent = parent->parent();
-  }
-
-  return 0;
-}
-
 DeclarativeInputDialog::DeclarativeInputDialog(QObject *parent) : DeclarativeWidgetProxy<InputDialog>(parent)
 {
   connectAllSignals(m_proxiedObject, this);
index d2705d4..7d5a12b 100644 (file)
 #include "declarativewidgetproxy_p.h"
 
 #include "objectadaptors_p.h"
+#include "staticdialogmethodattached_p.h"
 
-class DeclarativeInputDialogAttached : public QObject
+class DeclarativeInputDialogAttached : public StaticDialogMethodAttached
 {
   Q_OBJECT
-  Q_PROPERTY(QObject* parent READ dialogParent WRITE setDialogParent NOTIFY dialogParentChanged)
   Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
   Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
   Q_PROPERTY(bool ok READ dialogAccepted NOTIFY dialogAcceptedChanged)
@@ -49,9 +49,6 @@ class DeclarativeInputDialogAttached : public QObject
     DeclarativeInputDialogAttached(QObject *parent = 0);
     ~DeclarativeInputDialogAttached();
 
-    void setDialogParent(QObject *parent);
-    QObject *dialogParent() const;
-
     void setTitle(const QString &title);
     QString title() const;
 
@@ -96,7 +93,6 @@ class DeclarativeInputDialogAttached : public QObject
     Q_INVOKABLE QString getText();
 
   Q_SIGNALS:
-    void dialogParentChanged(QObject *parent);
     void titleChanged(const QString &title);
     void labelChanged(const QString &label);
     void dialogAcceptedChanged(bool accepted);
@@ -115,7 +111,6 @@ class DeclarativeInputDialogAttached : public QObject
 
   private:
     void setDialogAccepted(bool accepted);
-    QWidget *bestParentWindow(QObject *parent) const;
 
     class Private;
     Private *const d;