Skip to content

Commit

Permalink
libshell|Refactor: Added InputDialog, base class for text query dialogs
Browse files Browse the repository at this point in the history
Generalized the OpenConnectionDialog class.
  • Loading branch information
skyjake committed Feb 1, 2013
1 parent 3601488 commit aedae17
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 116 deletions.
5 changes: 5 additions & 0 deletions doomsday/libdeng2/include/de/widgets/widget.h
Expand Up @@ -56,6 +56,11 @@ class DENG2_PUBLIC Widget
Widget(String const &name = "");
virtual ~Widget();

/**
* Returns the automatically generated, unique identifier of the widget.
*/
duint32 id() const;

String name() const;
void setName(String const &name);
bool hasRoot() const;
Expand Down
13 changes: 12 additions & 1 deletion doomsday/libdeng2/src/widgets/widget.cpp
Expand Up @@ -24,9 +24,12 @@

namespace de {

static duint32 widgetIdGen = 0;

struct Widget::Instance
{
Widget &self;
duint32 id;
String name;
Widget *parent;
Behaviors behavior;
Expand All @@ -38,7 +41,10 @@ struct Widget::Instance
NamedChildren index;

Instance(Widget &w, String const &n) : self(w), name(n), parent(0)
{}
{
// A unique ID is generated for each Widget instance.
id = ++widgetIdGen;
}

~Instance()
{
Expand Down Expand Up @@ -70,6 +76,11 @@ Widget::~Widget()
delete d;
}

duint32 Widget::id() const
{
return d->id;
}

String Widget::name() const
{
return d->name;
Expand Down
1 change: 1 addition & 0 deletions doomsday/libshell/include/de/shell/InputDialog
@@ -0,0 +1 @@
#include "inputdialog.h"
2 changes: 2 additions & 0 deletions doomsday/libshell/include/de/shell/action.h
Expand Up @@ -43,6 +43,8 @@ class Action : public QObject

~Action();

void setLabel(String const &label);

String label() const;

/**
Expand Down
71 changes: 71 additions & 0 deletions doomsday/libshell/include/de/shell/inputdialog.h
@@ -0,0 +1,71 @@
/** @file inputdialog.h Dialog for querying text from the user.
*
* @authors Copyright © 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/

#ifndef LIBSHELL_INPUTDIALOG_H
#define LIBSHELL_INPUTDIALOG_H

#include "DialogWidget"

namespace de {
namespace shell {

/**
* Dialog for querying text from the user.
*/
class InputDialog : public de::shell::DialogWidget
{
public:
InputDialog(de::String const &name = "");
~InputDialog();

/**
* Sets the width of the dialog. The default width is 50.
*
* @param width Width of the dialog.
*/
void setWidth(int width);

void setDescription(String const &desc);
void setPrompt(String const &prompt);
void setText(String const &text);
void setAcceptLabel(String const &label);
void setRejectLabel(String const &label);

void prepare();
void finish(int result);

/**
* Returns the text that the user entered in the dialog. If the dialog
* was rejected, the returned string is empy.
*/
de::String text() const;

/**
* Returns the result from the DialogWidget.
*/
int result() const;

private:
struct Instance;
Instance *d;
};

} // namespace shell
} // namespace de

#endif // LIBSHELL_INPUTDIALOG_H
20 changes: 18 additions & 2 deletions doomsday/libshell/include/de/shell/menuwidget.h
Expand Up @@ -32,6 +32,9 @@ namespace shell {
* The width of the widget is automatically determined based on how much space
* is needed for the items and their possible shortcut labels. The height of
* the widget depends on the number of items in the menu.
*
* Actions added to the menu are considered shortcuts and triggering them will
* cause the menu to close (if it is closable).
*/
class LIBSHELL_PUBLIC MenuWidget : public TextWidget
{
Expand All @@ -57,12 +60,25 @@ class LIBSHELL_PUBLIC MenuWidget : public TextWidget

int itemCount() const;

/**
* Appends an item into the menu as the last item.
*
* @param action Action to add as a shortcut for triggering the item.
* @param shortcutLabel Label to show, representing the action shortcut to the user.
*/
void appendItem(Action *action, String const &shortcutLabel = "");

void appendSeparator();

/**
* Inserts an item into the menu.
*
* @param pos Index of the new item.
* @param action Action to add as a shortcut for triggering the item.
* @param shortcutLabel Label to show, representing the action shortcut to the user.
*/
void insertItem(int pos, Action *action, String const &shortcutLabel = "");

void appendSeparator();

void insertSeparator(int pos);

void clear();
Expand Down
15 changes: 9 additions & 6 deletions doomsday/libshell/libshell.pro
Expand Up @@ -26,6 +26,7 @@ INCLUDEPATH += include
HEADERS += \
include/de/shell/Action \
include/de/shell/DialogWidget \
include/de/shell/InputDialog \
include/de/shell/KeyEvent \
include/de/shell/LabelWidget \
include/de/shell/LineEditWidget \
Expand All @@ -37,31 +38,33 @@ HEADERS += \
include/de/shell/TextWidget \
\
include/de/shell/action.h \
include/de/shell/dialogwidget.h \
include/de/shell/inputdialog.h \
include/de/shell/keyevent.h \
include/de/shell/labelwidget.h \
include/de/shell/libshell.h \
include/de/shell/lineeditwidget.h \
include/de/shell/link.h \
include/de/shell/menuwidget.h \
include/de/shell/protocol.h \
include/de/shell/textcanvas.h \
include/de/shell/textrootwidget.h \
include/de/shell/textwidget.h \
include/de/shell/menuwidget.h \
include/de/shell/dialogwidget.h
include/de/shell/textwidget.h

# Sources and private headers.
SOURCES += \
src/action.cpp \
src/dialogwidget.cpp \
src/inputdialog.cpp \
src/labelwidget.cpp \
src/libshell.cpp \
src/lineeditwidget.cpp \
src/link.cpp \
src/menuwidget.cpp \
src/protocol.cpp \
src/textcanvas.cpp \
src/textrootwidget.cpp \
src/textwidget.cpp \
src/menuwidget.cpp \
src/dialogwidget.cpp
src/textwidget.cpp

# Installation ---------------------------------------------------------------

Expand Down
5 changes: 5 additions & 0 deletions doomsday/libshell/src/action.cpp
Expand Up @@ -54,6 +54,11 @@ Action::Action(String const &label) : _event(KeyEvent("")), _label(label)
Action::~Action()
{}

void Action::setLabel(String const &label)
{
_label = label;
}

String Action::label() const
{
return _label;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/src/dialogwidget.cpp
Expand Up @@ -52,7 +52,6 @@ void DialogWidget::finish(int /*result*/)
{
hide();
root().setFocus(0);
redraw();
}

int DialogWidget::exec(TextRootWidget &root)
Expand All @@ -73,6 +72,7 @@ int DialogWidget::exec(TextRootWidget &root)

// No longer in the root.
root.remove(*this);
root.requestDraw();
return result;
}

Expand Down

0 comments on commit aedae17

Please sign in to comment.