Refactor color dialog attached to common base class
authorKevin Krammer <kevin.krammer@kdab.com>
Fri, 2 Nov 2012 14:29:44 +0000 (15:29 +0100)
committerKevin Krammer <kevin.krammer@kdab.com>
Fri, 2 Nov 2012 15:20:24 +0000 (16:20 +0100)
lib/declarativecolordialog.cpp
lib/declarativecolordialog_p.h

index 84a3bd4..8f5e46a 100644 (file)
@@ -26,13 +26,12 @@ class DeclarativeColorDialogAttached::Private
     Private() : options(0) {}
 
   public:
-    QPointer<QObject> dialogParent;
     QString title;
     QColorDialog::ColorDialogOptions options;
 };
 
 DeclarativeColorDialogAttached::DeclarativeColorDialogAttached(QObject *parent)
-  : QObject(parent), d(new Private)
+  : StaticDialogMethodAttached(parent), d(new Private)
 {
 }
 
@@ -41,20 +40,6 @@ DeclarativeColorDialogAttached::~DeclarativeColorDialogAttached()
   delete d;
 }
 
-void DeclarativeColorDialogAttached::setDialogParent(QObject *parent)
-{
-  if (parent == d->dialogParent)
-    return;
-
-  d->dialogParent = parent;
-  emit dialogParentChanged(parent);
-}
-
-QObject *DeclarativeColorDialogAttached::dialogParent() const
-{
-  return d->dialogParent;
-}
-
 void DeclarativeColorDialogAttached::setTitle(const QString &title)
 {
   if (title == d->title)
@@ -71,34 +56,13 @@ QString DeclarativeColorDialogAttached::title() const
 
 QColor DeclarativeColorDialogAttached::getColor(const QColor &initialColor)
 {
-  QWidget *parent = bestParentWindow(d->dialogParent);
+  QWidget *parent = bestParentWindow();
   if (!d->title.isEmpty() || d->options != 0)
     return QColorDialog::getColor(initialColor, parent, d->title, d->options);
   else
     return QColorDialog::getColor(initialColor, parent);
 }
 
-QWidget *DeclarativeColorDialogAttached::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;
-}
-
 DeclarativeColorDialog::DeclarativeColorDialog(QObject *parent) : DeclarativeWidgetProxy<QColorDialog>(parent)
 {
   connectAllSignals(m_proxiedObject, this);
index abdd429..2986a15 100644 (file)
 
 #include "declarativewidgetproxy_p.h"
 
+#include "staticdialogmethodattached_p.h"
+
 #include <QColorDialog>
 
-class DeclarativeColorDialogAttached : public QObject
+class DeclarativeColorDialogAttached : public StaticDialogMethodAttached
 {
   Q_OBJECT
-  Q_PROPERTY(QObject* parent READ dialogParent WRITE setDialogParent NOTIFY dialogParentChanged)
   Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
   // TODO option
 
@@ -36,21 +37,15 @@ class DeclarativeColorDialogAttached : public QObject
     DeclarativeColorDialogAttached(QObject *parent = 0);
     ~DeclarativeColorDialogAttached();
 
-    void setDialogParent(QObject *parent);
-    QObject *dialogParent() const;
-
     void setTitle(const QString &title);
     QString title() const;
 
     Q_INVOKABLE QColor getColor(const QColor &initialColor);
 
   Q_SIGNALS:
-    void dialogParentChanged(QObject *parent);
     void titleChanged(const QString &title);
 
   private:
-    QWidget *bestParentWindow(QObject *parent) const;
-
     class Private;
     Private *const d;
 };