Add base class for attached "static methods" helper objects
authorKevin Krammer <kevin.krammer@kdab.com>
Fri, 2 Nov 2012 14:23:21 +0000 (15:23 +0100)
committerKevin Krammer <kevin.krammer@kdab.com>
Fri, 2 Nov 2012 15:20:24 +0000 (16:20 +0100)
declarativewidgets.pro
lib/staticdialogmethodattached.cpp [new file with mode: 0644]
lib/staticdialogmethodattached_p.h [new file with mode: 0644]

index 06f34a6..b01d1a8 100644 (file)
@@ -69,7 +69,8 @@ LIB_HEADERS = \
   lib/declarativewidgetproxy_p.h \
   lib/declarativewidgetsdocument.h \
   lib/objectadaptors_p.h \
-  lib/qmetaobjectbuilder_p.h
+  lib/qmetaobjectbuilder_p.h \
+  lib/staticdialogmethodattached_p.h
 
 LIB_SOURCES = \
   lib/abstractdeclarativeobject.cpp \
@@ -129,7 +130,8 @@ LIB_SOURCES = \
   lib/declarativewidget.cpp \
   lib/declarativewidgetsdocument.cpp \
   lib/objectadaptors.cpp \
-  lib/qmetaobjectbuilder.cpp
+  lib/qmetaobjectbuilder.cpp \
+  lib/staticdialogmethodattached.cpp
 
 HEADERS += \
     $$LIB_HEADERS
diff --git a/lib/staticdialogmethodattached.cpp b/lib/staticdialogmethodattached.cpp
new file mode 100644 (file)
index 0000000..cbd38d6
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+  Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
+  Author: Kevin Krammer, krake@kdab.com
+  Author: Tobias Koenig, tokoe@kdab.com
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License along
+  with this program; if not, write to the Free Software Foundation, Inc.,
+  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#include "staticdialogmethodattached_p.h"
+
+#include "abstractdeclarativeobject_p.h"
+
+#include <QPointer>
+#include <QWidget>
+
+class StaticDialogMethodAttached::Private
+{
+  public:
+    QPointer<QObject> dialogParent;
+};
+
+StaticDialogMethodAttached::StaticDialogMethodAttached(QObject *parent)
+  : QObject(parent), d(new Private)
+{
+}
+
+StaticDialogMethodAttached::~StaticDialogMethodAttached()
+{
+  delete d;
+}
+
+void StaticDialogMethodAttached::setDialogParent(QObject *parent)
+{
+  if (parent == d->dialogParent)
+    return;
+
+  d->dialogParent = parent;
+  emit dialogParentChanged(parent);
+}
+
+QObject *StaticDialogMethodAttached::dialogParent() const
+{
+  return d->dialogParent;
+}
+
+QWidget *StaticDialogMethodAttached::bestParentWindow() const
+{
+  QObject *parent = d->dialogParent;
+
+  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;
+}
diff --git a/lib/staticdialogmethodattached_p.h b/lib/staticdialogmethodattached_p.h
new file mode 100644 (file)
index 0000000..853d957
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+  Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
+  Author: Kevin Krammer, krake@kdab.com
+  Author: Tobias Koenig, tokoe@kdab.com
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License along
+  with this program; if not, write to the Free Software Foundation, Inc.,
+  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#ifndef STATICDIALOGMETHODATTACHED_P_H
+#define STATICDIALOGMETHODATTACHED_P_H
+
+#include <QObject>
+
+class StaticDialogMethodAttached : public QObject
+{
+  Q_OBJECT
+  Q_PROPERTY(QObject* parent READ dialogParent WRITE setDialogParent NOTIFY dialogParentChanged)
+
+  protected:
+    explicit StaticDialogMethodAttached(QObject *parent = 0);
+
+  public:
+    ~StaticDialogMethodAttached();
+
+    void setDialogParent(QObject *parent);
+    QObject *dialogParent() const;
+
+  Q_SIGNALS:
+    void dialogParentChanged(QObject *parent);
+
+  protected:
+    QWidget *bestParentWindow() const;
+
+  private:
+    class Private;
+    Private *const d;
+};
+
+#endif // STATICDIALOGMETHODATTACHED_P_H