From 5dad337a1716af2a2b1134514296163c2c87373d Mon Sep 17 00:00:00 2001 From: skyjake Date: Wed, 4 Sep 2013 18:52:20 +0300 Subject: [PATCH] UI|Client: Added Stylist and DialogContentStylist A Stylist is an object that configures widgets with a particular context in mind. DialogContentStylist adjusts margins and backgrounds for widgets in a dialog. --- doomsday/client/client.pro | 5 ++ doomsday/client/include/DialogContentStylist | 1 + doomsday/client/include/ui/Stylist | 1 + .../ui/framework/dialogcontentstylist.h | 51 ++++++++++++ .../client/include/ui/framework/stylist.h | 39 ++++++++++ .../src/ui/framework/dialogcontentstylist.cpp | 78 +++++++++++++++++++ .../client/src/ui/widgets/dialogwidget.cpp | 28 +------ 7 files changed, 178 insertions(+), 25 deletions(-) create mode 100644 doomsday/client/include/DialogContentStylist create mode 100644 doomsday/client/include/ui/Stylist create mode 100644 doomsday/client/include/ui/framework/dialogcontentstylist.h create mode 100644 doomsday/client/include/ui/framework/stylist.h create mode 100644 doomsday/client/src/ui/framework/dialogcontentstylist.cpp diff --git a/doomsday/client/client.pro b/doomsday/client/client.pro index 604b1dd730..3abe31203e 100644 --- a/doomsday/client/client.pro +++ b/doomsday/client/client.pro @@ -119,6 +119,7 @@ DENG_CONVENIENCE_HEADERS += \ include/BspNode \ include/CommandAction \ include/ContextWidgetOrganizer \ + include/DialogContentStylist \ include/EntityDatabase \ include/Face \ include/FontLineWrapping \ @@ -164,6 +165,7 @@ DENG_CONVENIENCE_HEADERS += \ include/ui/Item \ include/ui/ListData \ include/ui/Margins \ + include/ui/Stylist \ include/ui/SubmenuItem \ include/ui/VariableToggleItem \ include/Vertex \ @@ -365,6 +367,7 @@ DENG_HEADERS += \ include/ui/framework/commandaction.h \ include/ui/framework/contextwidgetorganizer.h \ include/ui/framework/data.h \ + include/ui/framework/dialogcontentstylist.h \ include/ui/framework/fontlinewrapping.h \ include/ui/framework/gltextcomposer.h \ include/ui/framework/gridlayout.h \ @@ -377,6 +380,7 @@ DENG_HEADERS += \ include/ui/framework/proceduralimage.h \ include/ui/framework/sequentiallayout.h \ include/ui/framework/signalaction.h \ + include/ui/framework/stylist.h \ include/ui/framework/submenuitem.h \ include/ui/framework/textdrawable.h \ include/ui/framework/variabletoggleitem.h \ @@ -696,6 +700,7 @@ SOURCES += \ src/ui/framework/commandaction.cpp \ src/ui/framework/contextwidgetorganizer.cpp \ src/ui/framework/data.cpp \ + src/ui/framework/dialogcontentstylist.cpp \ src/ui/framework/fontlinewrapping.cpp \ src/ui/framework/gltextcomposer.cpp \ src/ui/framework/gridlayout.cpp \ diff --git a/doomsday/client/include/DialogContentStylist b/doomsday/client/include/DialogContentStylist new file mode 100644 index 0000000000..d3be1632f8 --- /dev/null +++ b/doomsday/client/include/DialogContentStylist @@ -0,0 +1 @@ +#include "ui/framework/dialogcontentstylist.h" diff --git a/doomsday/client/include/ui/Stylist b/doomsday/client/include/ui/Stylist new file mode 100644 index 0000000000..abb79170ea --- /dev/null +++ b/doomsday/client/include/ui/Stylist @@ -0,0 +1 @@ +#include "framework/stylist.h" diff --git a/doomsday/client/include/ui/framework/dialogcontentstylist.h b/doomsday/client/include/ui/framework/dialogcontentstylist.h new file mode 100644 index 0000000000..3ab83b9029 --- /dev/null +++ b/doomsday/client/include/ui/framework/dialogcontentstylist.h @@ -0,0 +1,51 @@ +/** @file dialogcontentstylist.h Sets the style for widgets in a dialog. + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * 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, see: + * http://www.gnu.org/licenses + */ + +#ifndef DENG_CLIENT_DIALOGCONTENTSTYLIST_H +#define DENG_CLIENT_DIALOGCONTENTSTYLIST_H + +#include "ui/Stylist" +#include + +class DialogWidget; + +/** + * Sets the style for widgets in a dialog. + */ +class DialogContentStylist : public ui::Stylist, + DENG2_OBSERVES(de::Widget, ChildAddition) +{ +public: + DialogContentStylist(); + DialogContentStylist(DialogWidget &dialog); + DialogContentStylist(GuiWidget &container); + + ~DialogContentStylist(); + + void setContainer(GuiWidget &container); + + void applyStyle(GuiWidget &widget); + + // Observes when new children are added. + void widgetChildAdded(de::Widget &child); + +private: + GuiWidget *_container; +}; + +#endif // DENG_CLIENT_DIALOGCONTENTSTYLIST_H diff --git a/doomsday/client/include/ui/framework/stylist.h b/doomsday/client/include/ui/framework/stylist.h new file mode 100644 index 0000000000..b1485922f2 --- /dev/null +++ b/doomsday/client/include/ui/framework/stylist.h @@ -0,0 +1,39 @@ +/** @file stylist.h Widget stylist. + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * 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, see: + * http://www.gnu.org/licenses + */ + +#ifndef DENG_CLIENT_UI_STYLIST_H +#define DENG_CLIENT_UI_STYLIST_H + +class GuiWidget; + +namespace ui { + +/** + * Widget stylist. + */ +class Stylist +{ +public: + virtual ~Stylist() {} + + virtual void applyStyle(GuiWidget &widget) = 0; +}; + +} // namespace ui + +#endif // DENG_CLIENT_UI_STYLIST_H diff --git a/doomsday/client/src/ui/framework/dialogcontentstylist.cpp b/doomsday/client/src/ui/framework/dialogcontentstylist.cpp new file mode 100644 index 0000000000..e699234b9f --- /dev/null +++ b/doomsday/client/src/ui/framework/dialogcontentstylist.cpp @@ -0,0 +1,78 @@ +/** @file dialogcontentstylist.cpp Sets the style for widgets in a dialog. + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * 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, see: + * http://www.gnu.org/licenses + */ + +#include "DialogContentStylist" +#include "ui/widgets/dialogwidget.h" +#include "ui/widgets/togglewidget.h" +#include "ui/widgets/labelwidget.h" + +using namespace de; + +DialogContentStylist::DialogContentStylist() : _container(0) +{} + +DialogContentStylist::DialogContentStylist(DialogWidget &dialog) : _container(0) +{ + setContainer(dialog.area()); +} + +DialogContentStylist::DialogContentStylist(GuiWidget &container) : _container(0) +{ + setContainer(container); +} + +DialogContentStylist::~DialogContentStylist() +{ + if(_container) + { + _container->audienceForChildAddition -= this; + } +} + +void DialogContentStylist::setContainer(GuiWidget &container) +{ + if(_container) + { + _container->audienceForChildAddition -= this; + } + + _container = &container; + _container->audienceForChildAddition += this; +} + +void DialogContentStylist::widgetChildAdded(Widget &child) +{ + applyStyle(child.as()); +} + +void DialogContentStylist::applyStyle(GuiWidget &w) +{ + w.margins().set("dialog.gap"); + + // All label-based widgets should expand on their own. + if(LabelWidget *lab = w.maybeAs()) + { + lab->setSizePolicy(ui::Expand, ui::Expand); + } + + // Toggles should have no background. + if(ToggleWidget *tog = w.maybeAs()) + { + tog->set(GuiWidget::Background()); + } +} diff --git a/doomsday/client/src/ui/widgets/dialogwidget.cpp b/doomsday/client/src/ui/widgets/dialogwidget.cpp index 4caa565d1b..b03e7a5b0c 100644 --- a/doomsday/client/src/ui/widgets/dialogwidget.cpp +++ b/doomsday/client/src/ui/widgets/dialogwidget.cpp @@ -21,6 +21,7 @@ #include "ui/widgets/choicewidget.h" #include "GuiRootWidget" #include "SignalAction" +#include "DialogContentStylist" #include "dd_main.h" #include @@ -78,7 +79,6 @@ static bool dialogButtonOrder(ui::Item const &a, ui::Item const &b) DENG_GUI_PIMPL(DialogWidget), DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation), DENG2_OBSERVES(ContextWidgetOrganizer, WidgetUpdate), -DENG2_OBSERVES(Widget, ChildAddition), // for styling the contents DENG2_OBSERVES(ui::Data, Addition), DENG2_OBSERVES(ui::Data, Removal) { @@ -92,6 +92,7 @@ DENG2_OBSERVES(ui::Data, Removal) bool needButtonUpdate; float normalGlow; bool animatingGlow; + DialogContentStylist stylist; Instance(Public *i, Flags const &dialogFlags) : Base(i), @@ -110,7 +111,6 @@ DENG2_OBSERVES(ui::Data, Removal) GuiWidget *container = new GuiWidget("container"); area = new ScrollAreaWidget("area"); - area->audienceForChildAddition += this; buttons = new MenuWidget("buttons"); buttons->items().audienceForAddition += this; @@ -303,29 +303,6 @@ DENG2_OBSERVES(ui::Data, Removal) } } - /** - * Applies the default dialog style for child widgets added to - * the content area. - */ - void widgetChildAdded(Widget &areaChild) - { - GuiWidget &w = areaChild.as(); - - w.margins().set("dialog.gap"); - - // All label-based widgets should expand on their own. - if(LabelWidget *lab = w.maybeAs()) - { - lab->setSizePolicy(ui::Expand, ui::Expand); - } - - // Toggles should have no background. - if(ToggleWidget *tog = w.maybeAs()) - { - tog->set(Background()); - } - } - ui::ActionItem const *findDefaultAction() const { for(ui::Data::Pos i = 0; i < buttons->items().size(); ++i) @@ -384,6 +361,7 @@ DENG2_OBSERVES(ui::Data, Removal) DialogWidget::DialogWidget(String const &name, Flags const &flags) : PopupWidget(name), d(new Instance(this, flags)) { + d->stylist.setContainer(area()); setOpeningDirection(ui::NoDirection); d->updateBackground(); }