Skip to content

Commit

Permalink
Cleanup|Client|libdeng2|libshell: Various fixes and cleanup after ui:…
Browse files Browse the repository at this point in the history
…:Context refactoring
  • Loading branch information
skyjake committed Aug 16, 2013
1 parent c81f5d6 commit b5a6973
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 221 deletions.
16 changes: 8 additions & 8 deletions doomsday/client/include/ui/widgets/contextwidgetorganizer.h
Expand Up @@ -54,12 +54,12 @@ class ContextWidgetOrganizer
* appropriately.
*
* After construction, the widget is automatically updated with
* updateWidgetForItem().
* updateitemWidget().
*
* @param item Item that has the content.
* @param parent Future parent of the widget, if any (can be @c NULL).
*/
virtual GuiWidget *makeWidgetForItem(ui::Item const &item, GuiWidget const *parent) = 0;
virtual GuiWidget *makeitemWidget(ui::Item const &item, GuiWidget const *parent) = 0;

/**
* Called whenever the item's content changes and this should be reflected
Expand All @@ -68,7 +68,7 @@ class ContextWidgetOrganizer
* @param widget Widget that represents the item.
* @param item Item that has the content.
*/
virtual void updateWidgetForItem(GuiWidget &widget, ui::Item const &item) = 0;
virtual void updateitemWidget(GuiWidget &widget, ui::Item const &item) = 0;
};

/**
Expand Down Expand Up @@ -108,13 +108,13 @@ class ContextWidgetOrganizer
*
* @param context Context with items.
*/
void setContext(ui::Context &context);
void setContext(ui::Context const &context);

void unsetContext();

ui::Context &context() const;
ui::Context const &context() const;

GuiWidget *widgetForItem(ui::Item const &item) const;
GuiWidget *itemWidget(ui::Item const &item) const;

private:
DENG2_PRIVATE(d)
Expand All @@ -127,8 +127,8 @@ class ContextWidgetOrganizer
class DefaultWidgetFactory : public ContextWidgetOrganizer::IWidgetFactory
{
public:
GuiWidget *makeWidgetForItem(ui::Item const &item, GuiWidget const *parent);
void updateWidgetForItem(GuiWidget &widget, ui::Item const &item);
GuiWidget *makeitemWidget(ui::Item const &item, GuiWidget const *parent);
void updateitemWidget(GuiWidget &widget, ui::Item const &item);
};

#endif // DENG_CLIENT_CONTEXTWIDGETORGANIZER_H
46 changes: 8 additions & 38 deletions doomsday/client/include/ui/widgets/menuwidget.h
Expand Up @@ -38,25 +38,6 @@
*/
class MenuWidget : public ScrollAreaWidget
{
public:
#if 0
class ISortOrder
{
public:
virtual ~ISortOrder() {}

/**
* Determines the sort order for a pair of menu items.
*
* @param a First menu item.
* @param b Second menu item.
*
* @return -1 if a < b; +1 if a > b; 0 if equal.
*/
virtual int compareMenuItemsForSorting(Widget const &a, Widget const &b) const = 0;
};
#endif

public:
MenuWidget(de::String const &name = "");

Expand All @@ -83,30 +64,19 @@ class MenuWidget : public ScrollAreaWidget
void setGridSize(int columns, ui::SizePolicy columnPolicy,
int rows, ui::SizePolicy rowPolicy);

/**
* Sets the sort order for item layout.
*
* @param sorting Sort order object. MenuWidget takes ownership.
*/
//void setLayoutSortOrder(ISortOrder *sorting);

ui::Context &items();

ui::Context const &items() const;

ContextWidgetOrganizer const &organizer() const;

/*
GuiWidget *addItem(GuiWidget *anyWidget);
ButtonWidget *addItem(de::String const &styledText, de::Action *action = 0);
ButtonWidget *addItem(de::Image const &image, de::String const &styledText, de::Action *action = 0);
GuiWidget *addSeparator(de::String const &labelText = "");
/**
* Sets the data context of the menu to some existing context. The context
* must remain in existence until the MenuWidget is deleted.
*
* @param items Ownership not taken.
*/
void setItems(ui::Context const &items);

void removeItem(GuiWidget *child);
*/
ContextWidgetOrganizer const &organizer() const;

/**
* Returns the number of visible items in the menu. Hidden items are not
Expand Down
8 changes: 0 additions & 8 deletions doomsday/client/include/ui/widgets/popupmenuwidget.h
Expand Up @@ -32,14 +32,6 @@ class PopupMenuWidget : public PopupWidget

MenuWidget &menu() const;

/*
ButtonWidget *addItem(de::String const &styledText, de::Action *action = 0,
bool dismissOnTriggered = true);
LabelWidget *addItem(LabelWidget *anyLabelBasedWidget);
*/

//GuiWidget *addSeparator(de::String const &optionalLabel = "");

protected:
void glMakeGeometry(DefaultVertexBuf::Builder &verts);
void preparePopupForOpening();
Expand Down
3 changes: 3 additions & 0 deletions doomsday/client/include/ui/widgets/popupwidget.h
Expand Up @@ -31,6 +31,9 @@ class PopupWidget : public GuiWidget
{
Q_OBJECT

public:
DENG2_DEFINE_AUDIENCE(Close, void popupBeingClosed(PopupWidget &))

public:
PopupWidget(de::String const &name = "");

Expand Down
43 changes: 26 additions & 17 deletions doomsday/client/src/ui/widgets/contextwidgetorganizer.cpp
Expand Up @@ -34,7 +34,7 @@ DENG2_OBSERVES(ui::Context, OrderChange),
DENG2_OBSERVES(ui::Item, Change )
{
GuiWidget *container;
ui::Context *context;
ui::Context const *context;
IWidgetFactory *factory;

typedef QMap<ui::Item const *, GuiWidget *> Mapping;
Expand All @@ -48,7 +48,15 @@ DENG2_OBSERVES(ui::Item, Change )
factory(&defaultWidgetFactory)
{}

void set(ui::Context *ctx)
~Instance()
{
DENG2_FOR_EACH_CONST(Mapping, i, mapping)
{
i.value()->audienceForDeletion -= this;
}
}

void set(ui::Context const *ctx)
{
if(context)
{
Expand All @@ -72,12 +80,12 @@ DENG2_OBSERVES(ui::Item, Change )
}
}

void addWidgetForItem(ui::Context::Pos pos, bool alwaysAppend = false)
void additemWidget(ui::Context::Pos pos, bool alwaysAppend = false)
{
DENG2_ASSERT(factory != 0);

ui::Item const &item = context->at(pos);
GuiWidget *w = factory->makeWidgetForItem(item, container);
GuiWidget *w = factory->makeitemWidget(item, container);
if(!w) return; // Unpresentable.

// Others may alter the widget in some way.
Expand All @@ -87,6 +95,7 @@ DENG2_OBSERVES(ui::Item, Change )
}

// Update the widget immediately.
mapping.insert(&item, w);
itemChanged(item);

if(alwaysAppend || pos == context->size() - 1)
Expand All @@ -99,9 +108,7 @@ DENG2_OBSERVES(ui::Item, Change )
container->insertBefore(w, *mapping[&context->at(pos + 1)]);
}

mapping.insert(&item, w);

// Observe:
// Observe.
w->audienceForDeletion += this; // in case it's manually deleted
item.audienceForChange += this;
}
Expand All @@ -113,7 +120,7 @@ DENG2_OBSERVES(ui::Item, Change )

for(ui::Context::Pos i = 0; i < context->size(); ++i)
{
addWidgetForItem(i, true /*always append*/);
additemWidget(i, true /*always append*/);
}
}

Expand All @@ -136,8 +143,10 @@ DENG2_OBSERVES(ui::Item, Change )

void widgetBeingDeleted(Widget &widget)
{
// Note: this should not occur normally, as the widgets created by the
// Organizer are not manually deleted.
/*
* Note: this should not occur normally, as the widgets created by the
* Organizer are not usually manually deleted.
*/
MutableMappingIterator iter(mapping);
while(iter.hasNext())
{
Expand All @@ -152,7 +161,7 @@ DENG2_OBSERVES(ui::Item, Change )

void contextItemAdded(ui::Context::Pos pos, ui::Item const &)
{
addWidgetForItem(pos);
additemWidget(pos);
}

void contextItemBeingRemoved(ui::Context::Pos, ui::Item const &item)
Expand Down Expand Up @@ -183,7 +192,7 @@ DENG2_OBSERVES(ui::Item, Change )
DENG2_ASSERT(mapping.contains(&item));

GuiWidget &w = *mapping[&item];
factory->updateWidgetForItem(w, item);
factory->updateitemWidget(w, item);

// Notify.
DENG2_FOR_PUBLIC_AUDIENCE(WidgetUpdate, i)
Expand All @@ -204,7 +213,7 @@ ContextWidgetOrganizer::ContextWidgetOrganizer(GuiWidget &container)
: d(new Instance(this, &container))
{}

void ContextWidgetOrganizer::setContext(ui::Context &context)
void ContextWidgetOrganizer::setContext(ui::Context const &context)
{
d->set(&context);
}
Expand All @@ -214,7 +223,7 @@ void ContextWidgetOrganizer::unsetContext()
d->set(0);
}

ui::Context &ContextWidgetOrganizer::context() const
ui::Context const &ContextWidgetOrganizer::context() const
{
DENG2_ASSERT(d->context != 0);
return *d->context;
Expand All @@ -231,20 +240,20 @@ ContextWidgetOrganizer::IWidgetFactory &ContextWidgetOrganizer::widgetFactory()
return *d->factory;
}

GuiWidget *ContextWidgetOrganizer::widgetForItem(ui::Item const &item) const
GuiWidget *ContextWidgetOrganizer::itemWidget(ui::Item const &item) const
{
return d->find(item);
}

GuiWidget *DefaultWidgetFactory::makeWidgetForItem(ui::Item const &item, GuiWidget const *)
GuiWidget *DefaultWidgetFactory::makeitemWidget(ui::Item const &item, GuiWidget const *)
{
// The default implementation uses simple labels.
LabelWidget *w = new LabelWidget;
w->setText(item.label());
return w;
}

void DefaultWidgetFactory::updateWidgetForItem(GuiWidget &, ui::Item const &)
void DefaultWidgetFactory::updateitemWidget(GuiWidget &, ui::Item const &)
{
// nothing to do
}
40 changes: 16 additions & 24 deletions doomsday/client/src/ui/widgets/gameselectionwidget.cpp
Expand Up @@ -32,30 +32,17 @@ DENG2_OBSERVES(Games, Addition),
DENG2_OBSERVES(App, StartupComplete),
DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation)
{
typedef QMap<Game *, ButtonWidget *> Buttons;
Buttons buttons;

#if 0
/**
* Sorts the game buttons by label text.
*/
struct Sorting : public ISortOrder
{
int compareMenuItemsForSorting(Widget const &a, Widget const &b) const
{
ButtonWidget const &x = a.as<ButtonWidget>();
ButtonWidget const &y = b.as<ButtonWidget>();
return x.text().compareWithoutCase(y.text());
}
struct GameItem : public ui::ActionItem {
GameItem(Game const &gameRef, de::String const &label, de::Action *action)
: ui::ActionItem(label, action), game(gameRef) {}
Game const &game;
};
#endif

Instance(Public *i) : Base(i)
{
App_Games().audienceForAddition += this;
App::app().audienceForStartupComplete += this;

//self.setLayoutSortOrder(new Sorting);
self.organizer().audienceForWidgetCreation += this;
}

Expand All @@ -81,7 +68,7 @@ DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation)
.arg(Str_Text(game.author()))
.arg(idKey);

ui::ActionItem *item = new ui::ActionItem(label, loadAction);
GameItem *item = new GameItem(game, label, loadAction);

/// @todo The name of the plugin should be accessible via the plugin loader.
String plugName;
Expand Down Expand Up @@ -123,17 +110,22 @@ DENG2_OBSERVES(ContextWidgetOrganizer, WidgetCreation)

void updateGameAvailability()
{
DENG2_FOR_EACH(Buttons, i, buttons)
for(uint i = 0; i < self.items().size(); ++i)
{
if(i.key()->allStartupFilesFound())
GameItem const &item = self.items().at(i).as<GameItem>();

GuiWidget *w = self.organizer().itemWidget(item);
DENG2_ASSERT(w != 0);

if(item.game.allStartupFilesFound())
{
i.value()->setOpacity(1.f, .5f);
i.value()->enable();
w->setOpacity(1.f, .5f);
w->enable();
}
else
{
i.value()->setOpacity(.3f, .5f);
i.value()->disable();
w->setOpacity(.3f, .5f);
w->disable();
}
}

Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/ui/widgets/item.cpp
Expand Up @@ -50,7 +50,7 @@ String Item::label() const

String Item::sortKey() const
{
return "";
return _label;
}

void Item::notifyChange()
Expand Down

0 comments on commit b5a6973

Please sign in to comment.