From: Kevin Krammer Date: Fri, 2 Nov 2012 14:23:21 +0000 (+0100) Subject: Add base class for attached "static methods" helper objects X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=7894b6d5ff07a6bc673c8516e246a1a357765a4d;p=web%2Fkonrad%2FDeclarativeWidgets.git Add base class for attached "static methods" helper objects --- diff --git a/declarativewidgets.pro b/declarativewidgets.pro index 06f34a6..b01d1a8 100644 --- a/declarativewidgets.pro +++ b/declarativewidgets.pro @@ -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 index 0000000..cbd38d6 --- /dev/null +++ b/lib/staticdialogmethodattached.cpp @@ -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 +#include + +class StaticDialogMethodAttached::Private +{ + public: + QPointer 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(parent); + if (declarativeObject) + parent = declarativeObject->object(); + + while (parent) { + QWidget *widget = qobject_cast(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 index 0000000..853d957 --- /dev/null +++ b/lib/staticdialogmethodattached_p.h @@ -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 + +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