Skip to content

Commit

Permalink
Refactor|Widget: Renamed GuiWidgetRef, moved to libcore
Browse files Browse the repository at this point in the history
Also added Widget::orphan() for making a widget remove itself from
its parent.
  • Loading branch information
skyjake committed Aug 25, 2015
1 parent fe46d6d commit 4d1df86
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 73 deletions.
3 changes: 1 addition & 2 deletions doomsday/apps/client/src/ui/clientwindow.cpp
Expand Up @@ -40,7 +40,6 @@
#include <de/FadeToBlackWidget>
#include <de/SignalAction>
#include <de/VRWindowTransform>
#include <de/GuiWidgetRef>
#include <de/concurrency.h>
#include <doomsday/console/exec.h>
#include "api_console.h"
Expand Down Expand Up @@ -98,7 +97,7 @@ DENG2_PIMPL(ClientWindow)
LabelWidget *background = nullptr;
GuiWidget *iwadNotice = nullptr;
GameSelectionWidget *gameSelMenu = nullptr;
GuiWidgetRef<FadeToBlackWidget> fader;
SafeWidgetPtr<FadeToBlackWidget> fader;
BusyWidget *busy = nullptr;
GuiWidget *sidebar = nullptr;
PrivilegedLogWidget *privLog = nullptr;
Expand Down
1 change: 0 additions & 1 deletion doomsday/sdk/libappfw/include/de/GuiWidgetRef

This file was deleted.

69 changes: 0 additions & 69 deletions doomsday/sdk/libappfw/include/de/framework/guiwidgetref.h

This file was deleted.

50 changes: 49 additions & 1 deletion doomsday/sdk/libcore/include/de/widgets/widget.h
Expand Up @@ -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</small>
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG2_WIDGET_H
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 <typename WidgetType>
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
9 changes: 9 additions & 0 deletions doomsday/sdk/libcore/src/widgets/widget.cpp
Expand Up @@ -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)
{
Expand Down

0 comments on commit 4d1df86

Please sign in to comment.