From 4d1df8646cb0670e1c0bc72f2f8e21e38e769185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Tue, 25 Aug 2015 15:02:19 +0300 Subject: [PATCH] Refactor|Widget: Renamed GuiWidgetRef, moved to libcore Also added Widget::orphan() for making a widget remove itself from its parent. --- doomsday/apps/client/src/ui/clientwindow.cpp | 3 +- doomsday/sdk/libappfw/include/de/GuiWidgetRef | 1 - .../include/de/framework/guiwidgetref.h | 69 ------------------- .../sdk/libcore/include/de/widgets/widget.h | 50 +++++++++++++- doomsday/sdk/libcore/src/widgets/widget.cpp | 9 +++ 5 files changed, 59 insertions(+), 73 deletions(-) delete mode 100644 doomsday/sdk/libappfw/include/de/GuiWidgetRef delete mode 100644 doomsday/sdk/libappfw/include/de/framework/guiwidgetref.h diff --git a/doomsday/apps/client/src/ui/clientwindow.cpp b/doomsday/apps/client/src/ui/clientwindow.cpp index c2658704bf..b15ed0d5a9 100644 --- a/doomsday/apps/client/src/ui/clientwindow.cpp +++ b/doomsday/apps/client/src/ui/clientwindow.cpp @@ -40,7 +40,6 @@ #include #include #include -#include #include #include #include "api_console.h" @@ -98,7 +97,7 @@ DENG2_PIMPL(ClientWindow) LabelWidget *background = nullptr; GuiWidget *iwadNotice = nullptr; GameSelectionWidget *gameSelMenu = nullptr; - GuiWidgetRef fader; + SafeWidgetPtr fader; BusyWidget *busy = nullptr; GuiWidget *sidebar = nullptr; PrivilegedLogWidget *privLog = nullptr; diff --git a/doomsday/sdk/libappfw/include/de/GuiWidgetRef b/doomsday/sdk/libappfw/include/de/GuiWidgetRef deleted file mode 100644 index f500fea646..0000000000 --- a/doomsday/sdk/libappfw/include/de/GuiWidgetRef +++ /dev/null @@ -1 +0,0 @@ -#include "framework/guiwidgetref.h" diff --git a/doomsday/sdk/libappfw/include/de/framework/guiwidgetref.h b/doomsday/sdk/libappfw/include/de/framework/guiwidgetref.h deleted file mode 100644 index 62419d897f..0000000000 --- a/doomsday/sdk/libappfw/include/de/framework/guiwidgetref.h +++ /dev/null @@ -1,69 +0,0 @@ -/** @file guiwidgetref.h Smart pointer to a GuiWidget. - * - * @authors Copyright (c) 2015 Jaakko Keränen - * - * @par License - * LGPL: http://www.gnu.org/licenses/lgpl.html - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation; either version 3 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 Lesser - * General Public License for more details. You should have received a copy of - * the GNU Lesser General Public License along with this program; if not, see: - * http://www.gnu.org/licenses - */ - -#ifndef LIBAPPFW_GUIWIDGETREF_H -#define LIBAPPFW_GUIWIDGETREF_H - -#include "../GuiWidget" - -namespace de { - -/** - * Smart pointer to a GuiWidget. Does not own the target widget. - */ -template -class GuiWidgetRef : DENG2_OBSERVES(Widget, Deletion) -{ -public: - GuiWidgetRef(WidgetType *ptr = nullptr) { - reset(ptr); - } - ~GuiWidgetRef() { - reset(nullptr); - } - void reset(WidgetType *ptr) { - if(_ptr) _ptr->Widget::audienceForDeletion() -= this; - _ptr = ptr; - if(_ptr) _ptr->Widget::audienceForDeletion() += this; - } - WidgetType *operator -> () const { - return _ptr; - } - operator WidgetType const * () const { - return _ptr; - } - operator WidgetType * () { - return _ptr; - } - explicit operator bool() const { - return _ptr != nullptr; - } - void widgetBeingDeleted(Widget &widget) { - if(&widget == _ptr) { - _ptr = nullptr; - } - } - -private: - WidgetType *_ptr = nullptr; -}; - -} // namespace de - -#endif // LIBAPPFW_GUIWIDGETREF_H - diff --git a/doomsday/sdk/libcore/include/de/widgets/widget.h b/doomsday/sdk/libcore/include/de/widgets/widget.h index 81fa8b8a4e..7ef7b9c738 100644 --- a/doomsday/sdk/libcore/include/de/widgets/widget.h +++ b/doomsday/sdk/libcore/include/de/widgets/widget.h @@ -15,7 +15,7 @@ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * General Public License for more details. You should have received a copy of * the GNU Lesser General Public License along with this program; if not, see: - * http://www.gnu.org/licenses + * http://www.gnu.org/licenses */ #ifndef LIBDENG2_WIDGET_H @@ -228,6 +228,11 @@ class DENG2_PUBLIC Widget bool isFirstChild() const; bool isLastChild() const; + /** + * Removes the widget from its parent, if it has a parent. + */ + void orphan(); + // Utilities. String uniqueName(String const &name) const; @@ -276,6 +281,49 @@ class DENG2_PUBLIC Widget Q_DECLARE_OPERATORS_FOR_FLAGS(Widget::Behaviors) +/** + * Auto-nulled pointer to a Widget. Does not own the target widget. + */ +template +class SafeWidgetPtr : DENG2_OBSERVES(Widget, Deletion) +{ +public: + SafeWidgetPtr(WidgetType *ptr = nullptr) { + reset(ptr); + } + ~SafeWidgetPtr() { + reset(nullptr); + } + void reset(WidgetType *ptr = nullptr) { + if(_ptr) _ptr->Widget::audienceForDeletion() -= this; + _ptr = ptr; + if(_ptr) _ptr->Widget::audienceForDeletion() += this; + } + WidgetType *operator -> () const { + return _ptr; + } + operator WidgetType const * () const { + return _ptr; + } + operator WidgetType * () { + return _ptr; + } + WidgetType *get() const { + return _ptr; + } + explicit operator bool() const { + return _ptr != nullptr; + } + void widgetBeingDeleted(Widget &widget) { + if(&widget == _ptr) { + _ptr = nullptr; + } + } + +private: + WidgetType *_ptr = nullptr; +}; + } // namespace de #endif // LIBDENG2_WIDGET_H diff --git a/doomsday/sdk/libcore/src/widgets/widget.cpp b/doomsday/sdk/libcore/src/widgets/widget.cpp index 246002bd6a..9b853653aa 100644 --- a/doomsday/sdk/libcore/src/widgets/widget.cpp +++ b/doomsday/sdk/libcore/src/widgets/widget.cpp @@ -349,6 +349,15 @@ Widget *Widget::remove(Widget &child) return &child; } + +void Widget::orphan() +{ + if(d->parent) + { + d->parent->remove(*this); + } + DENG2_ASSERT(d->parent == nullptr); +} Widget *Widget::find(String const &name) {