Skip to content

Commit

Permalink
Refactor|libcommon|Menu: Applied the fluent API pattern to all menu W…
Browse files Browse the repository at this point in the history
…idgets
  • Loading branch information
danij-deng committed Sep 21, 2014
1 parent 9b700f0 commit 1217118
Show file tree
Hide file tree
Showing 18 changed files with 1,130 additions and 1,504 deletions.
33 changes: 31 additions & 2 deletions doomsday/plugins/common/include/menu/page.h
Expand Up @@ -80,15 +80,44 @@ class Page

virtual ~Page();


/**
* Returns the symbolic name/identifier of the page.
*/
de::String name() const;

Widgets &widgets();
/**
* Adds a widget object as a child widget of the Page and sets up the Widget -> Page
* relationship. The object must be an instance of a class derived from Widget.
*
* @param widget Widget object to add to the Page. The Page takes ownership.
*
* @return Reference to @a widget, for caller convenience.
*/
template <typename WidgetType>
inline WidgetType &addWidget(WidgetType *widget) {
DENG2_ASSERT(widget != 0);
addWidget(static_cast<Widget *>(widget));
return *widget;
}

/**
* Adds a Widget instance as a child widget of the Page and sets up the Widget -> Page
* relationship.
*
* @param widget Widget to add to the Page. The Page takes ownership.
*
* @return Reference to @a widget, for caller convenience.
*/
Widget &addWidget(Widget *widget);

/**
* Provides access to the list of child widgets of the Page, for efficient traversal.
*/
Widgets const &widgets() const;

/**
* Returns the total number of child widgets of the Page.
*/
inline int widgetCount() const { return widgets().count(); }

void setTitle(de::String const &newTitle);
Expand Down
Expand Up @@ -57,8 +57,10 @@ class ColorPreviewWidget : public Widget
* Change the dimensions of the preview area (in fixed 320x200 space).
*
* @param newDimensions New dimensions of the preview area.
*
* @return Reference to this ColorPreviewWidget.
*/
void setPreviewDimensions(de::Vector2i const &newDimensions);
ColorPreviewWidget &setPreviewDimensions(de::Vector2i const &newDimensions);

/**
* Returns the dimensions of the preview area (in fixed 320x200 space).
Expand Down Expand Up @@ -86,14 +88,14 @@ class ColorPreviewWidget : public Widget
* @param newColor New color and alpha.
* @param flags @ref mncolorboxSetColorFlags
*
* @return @c true if the current color changed.
* @return Reference to this ColorPreviewWidget.
*/
bool setColor(de::Vector4f const &newColor, int flags = MNCOLORBOX_SCF_NO_ACTION);
ColorPreviewWidget &setColor(de::Vector4f const &newColor, int flags = MNCOLORBOX_SCF_NO_ACTION);

bool setRed (float red, int flags = MNCOLORBOX_SCF_NO_ACTION);
bool setGreen(float green, int flags = MNCOLORBOX_SCF_NO_ACTION);
bool setBlue (float blue, int flags = MNCOLORBOX_SCF_NO_ACTION);
bool setAlpha(float alpha, int flags = MNCOLORBOX_SCF_NO_ACTION);
ColorPreviewWidget &setRed (float newRed, int flags = MNCOLORBOX_SCF_NO_ACTION);
ColorPreviewWidget &setGreen(float newGreen, int flags = MNCOLORBOX_SCF_NO_ACTION);
ColorPreviewWidget &setBlue (float newBlue, int flags = MNCOLORBOX_SCF_NO_ACTION);
ColorPreviewWidget &setAlpha(float newAlpha, int flags = MNCOLORBOX_SCF_NO_ACTION);

private:
DENG2_PRIVATE(d)
Expand Down
Expand Up @@ -42,13 +42,13 @@ class CVarTextualSliderWidget : public CVarSliderWidget
void draw(Point2Raw const *origin);
void updateGeometry(Page *pagePtr);

void setEmptyText(de::String const &newEmptyText);
CVarTextualSliderWidget &setEmptyText(de::String const &newEmptyText);
de::String emptyText() const;

void setOnethSuffix(de::String const &newOnethSuffix);
CVarTextualSliderWidget &setOnethSuffix(de::String const &newOnethSuffix);
de::String onethSuffix() const;

void setNthSuffix(de::String const &newNthSuffix);
CVarTextualSliderWidget &setNthSuffix(de::String const &newNthSuffix);
de::String nthSuffix() const;

private:
Expand Down
4 changes: 2 additions & 2 deletions doomsday/plugins/common/include/menu/widgets/labelwidget.h
Expand Up @@ -48,8 +48,8 @@ class LabelWidget : public Widget
void draw(Point2Raw const *origin);
void updateGeometry(Page *pagePtr);

void setPatch(patchid_t *newPatch);
void setText(de::String const &newText);
LabelWidget &setPatch(patchid_t *newPatch);
LabelWidget &setText(de::String const &newText);

private:
DENG2_PRIVATE(d)
Expand Down
6 changes: 3 additions & 3 deletions doomsday/plugins/common/include/menu/widgets/lineeditwidget.h
Expand Up @@ -71,22 +71,22 @@ class LineEditWidget : public Widget
int handleEvent(event_t *ev);
int handleCommand(menucommand_e command);

void setMaxLength(int newMaxLength);
LineEditWidget &setMaxLength(int newMaxLength);
int maxLength() const;

/**
* Change the current contents of the edit field.
* @param newText New text value.
* @param flags @ref mneditSetTextFlags
*/
void setText(de::String const &newText, int flags = MNEDIT_STF_NO_ACTION);
LineEditWidget &setText(de::String const &newText, int flags = MNEDIT_STF_NO_ACTION);

/**
* Returns a copy of the current editable value.
*/
de::String text() const;

void setEmptyText(de::String const &newEmptyText);
LineEditWidget &setEmptyText(de::String const &newEmptyText);
de::String emptyText() const;

public:
Expand Down
11 changes: 10 additions & 1 deletion doomsday/plugins/common/include/menu/widgets/listwidget.h
Expand Up @@ -72,7 +72,16 @@ class ListWidget : public Widget
void updateGeometry(Page *pagePtr);
int handleCommand(menucommand_e command);

Items &items();
/**
* Add an Item to the ListWidget. Ownership of the Item is given to ListWidget.
*/
ListWidget &addItem(Item *item);

/**
* Add set of Items to the ListWidget in order. Ownership of the Items is given to ListWidget.
*/
ListWidget &addItems(Items const &itemsToAdd);

Items const &items() const;

inline int itemCount() const { return items().count(); }
Expand Down
6 changes: 3 additions & 3 deletions doomsday/plugins/common/include/menu/widgets/sliderwidget.h
Expand Up @@ -73,15 +73,15 @@ class SliderWidget : public Widget
* @param value New value.
* @param flags @ref mnsliderSetValueFlags
*/
void setValue(float value, int flags = MNSLIDER_SVF_NO_ACTION);
SliderWidget &setValue(float value, int flags = MNSLIDER_SVF_NO_ACTION);
float value() const;

void setRange(float newMin, float newMax, float newStep);
SliderWidget &setRange(float newMin, float newMax, float newStep);
float min() const;
float max() const;
float step() const;

void setFloatMode(bool yes = true);
SliderWidget &setFloatMode(bool yes = true);
bool floatMode() const;

public:
Expand Down
26 changes: 14 additions & 12 deletions doomsday/plugins/common/include/menu/widgets/widget.h
Expand Up @@ -133,7 +133,7 @@ class Widget
virtual int handleCommand(menucommand_e command);

/// Configure a custom command responder to override the default mechanism.
void setCommandResponder(CommandResponder newResponder);
Widget &setCommandResponder(CommandResponder newResponder);

/// Delegate handling of @a command to the relevant responder.

Expand All @@ -143,7 +143,7 @@ class Widget
/// Process time (the "tick") for this object.
virtual void tick();

void setOnTickCallback(OnTickCallback newCallback);
Widget &setOnTickCallback(OnTickCallback newCallback);

bool hasPage() const;

Expand All @@ -153,7 +153,7 @@ class Widget
*
* @param newPage New Page to attribute. Use @c 0 to clear. Ownership unaffected.
*/
void setPage(Page *newPage);
Widget &setPage(Page *newPage);

Page &page() const;

Expand All @@ -164,8 +164,10 @@ class Widget
*
* @param flags Flags to modify.
* @param operation Operation to perform on the flags.
*
* @return Reference to this Widget.
*/
void setFlags(Flags flagsToChange, de::FlagOp operation = de::SetFlags);
Widget &setFlags(Flags flagsToChange, de::FlagOp operation = de::SetFlags);
Flags flags() const;

inline bool isActive() const { return flags() & Active; }
Expand Down Expand Up @@ -212,22 +214,22 @@ class Widget
*
* @param ob MNObject-derived instance.
* @param origin New origin coordinates.
* @return Same as @a ob for caller convenience.
* @return Reference to this Widget.
*/
Widget &setFixedOrigin(Point2Raw const *origin);
Widget &setFixedX(int x);
Widget &setFixedY(int y);

int group() const;
Widget &setGroup(int newGroup);
int group() const;

int shortcut();
Widget &setShortcut(int ddkey);
int shortcut();

void setColor(int newPageColor);
Widget &setColor(int newPageColor);
int color() const;

void setFont(int newPageFont);
Widget &setFont(int newPageFont);
int font() const;

de::String const &helpInfo() const;
Expand All @@ -238,7 +240,7 @@ class Widget
/// associated with the unique identifier @a action.
bool hasAction(mn_actionid_t action);

void setAction(mn_actionid_t action, mn_actioninfo_t::ActionCallback callback);
Widget &setAction(mn_actionid_t action, mn_actioninfo_t::ActionCallback callback);

/**
* Lookup the unique ActionInfo associated with the identifier @a id.
Expand All @@ -253,10 +255,10 @@ class Widget
*/
void execAction(mn_actionid_t action);

void setUserValue(QVariant const &newValue);
Widget &setUserValue(QVariant const &newValue);
QVariant const &userValue() const;

void setUserValue2(QVariant const &newValue);
Widget &setUserValue2(QVariant const &newValue);
QVariant const &userValue2() const;

private:
Expand Down

0 comments on commit 1217118

Please sign in to comment.