Skip to content

Commit

Permalink
Shell: Added menu items, fixed MenuWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jan 30, 2013
1 parent 8511f61 commit 667d83a
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 35 deletions.
2 changes: 1 addition & 1 deletion doomsday/libdeng2/src/widgets/rootwidget.cpp
Expand Up @@ -93,7 +93,7 @@ void RootWidget::setViewSize(Vector2i const &size)

void RootWidget::setFocus(Widget *widget)
{
d->focus = widget;
d->focus = widget;
}

Widget *RootWidget::focus() const
Expand Down
2 changes: 1 addition & 1 deletion doomsday/libdeng2/src/widgets/widget.cpp
Expand Up @@ -130,7 +130,7 @@ bool Widget::isHidden() const

void Widget::show(bool doShow)
{
d->hidden = doShow;
d->hidden = !doShow;
}

void Widget::clear()
Expand Down
4 changes: 4 additions & 0 deletions doomsday/libshell/include/de/shell/action.h
Expand Up @@ -37,6 +37,8 @@ class Action : public QObject

Action(String const &label, KeyEvent const &event, QObject *target = 0, char const *slot = 0);

Action(String const &label);

~Action();

String label() const;
Expand All @@ -50,6 +52,8 @@ class Action : public QObject
*/
bool tryTrigger(KeyEvent const &ev);

void trigger();

signals:
void triggered();

Expand Down
9 changes: 9 additions & 0 deletions doomsday/libshell/include/de/shell/menuwidget.h
Expand Up @@ -35,6 +35,8 @@ namespace shell {
*/
class MenuWidget : public TextWidget
{
Q_OBJECT

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

Expand Down Expand Up @@ -72,6 +74,13 @@ class MenuWidget : public TextWidget
void draw();
bool handleEvent(Event const *);

public slots:
void open();
void close();

signals:
void closed();

private:
struct Instance;
Instance *d;
Expand Down
10 changes: 9 additions & 1 deletion doomsday/libshell/src/action.cpp
Expand Up @@ -39,6 +39,9 @@ Action::Action(String const &label, KeyEvent const &event, QObject *target, char
}
}

Action::Action(String const &label) : _event(KeyEvent("")), _label(label)
{}

Action::~Action()
{}

Expand All @@ -51,11 +54,16 @@ bool Action::tryTrigger(KeyEvent const &ev)
{
if(ev == _event)
{
emit triggered();
trigger();
return true;
}
return false;
}

void Action::trigger()
{
emit triggered();
}

} // namespace shell
} // namespace de
97 changes: 81 additions & 16 deletions doomsday/libshell/src/menuwidget.cpp
Expand Up @@ -38,9 +38,9 @@ struct MenuWidget::Instance
{
Action *action;
String shortcutLabel;
bool separatorBefore;
bool separatorAfter;

Item() : action(0), separatorBefore(false)
Item() : action(0), separatorAfter(false)
{}
};

Expand Down Expand Up @@ -82,7 +82,7 @@ struct MenuWidget::Instance
foreach(Item const &item, items)
{
lines++;
if(item.separatorBefore) lines++;
if(item.separatorAfter) lines++;

int w = item.action->label().size();
if(!item.shortcutLabel.isEmpty())
Expand Down Expand Up @@ -137,7 +137,7 @@ void MenuWidget::appendSeparator()
{
if(d->items.isEmpty()) return;

d->items.last().separatorBefore = true;
d->items.last().separatorAfter = true;
d->updateSize();
redraw();
}
Expand All @@ -158,7 +158,7 @@ void MenuWidget::insertSeparator(int pos)
{
if(pos < 0 || pos >= d->items.size()) return;

d->items[pos].separatorBefore = true;
d->items[pos].separatorAfter = true;
d->updateSize();
redraw();
}
Expand Down Expand Up @@ -214,6 +214,19 @@ Vector2i MenuWidget::cursorPosition() const
return d->cursorPos;
}

void MenuWidget::open()
{
show();
redraw();
}

void MenuWidget::close()
{
emit closed();
hide();
redraw();
}

void MenuWidget::draw()
{
Rectanglei pos = rule().recti();
Expand All @@ -225,22 +238,14 @@ void MenuWidget::draw()
{
Instance::Item const &item = d->items[i];

// Draw a separator.
if(item.separatorBefore)
{
buf.fill(Rectanglei(Vector2i(1, y), Vector2i(pos.width() - 1, 2)),
TextCanvas::Char('-', d->borderAttr));
y++;
}

// Determine style.
TextCanvas::Char::Attribs itemAttr = (d->cursor == i && hasFocus()?
d->selectionAttr : d->backgroundAttr);

// Cursor.
if(d->cursor == i)
{
buf.fill(Rectanglei(Vector2i(1, y), Vector2i(pos.width() - 1, 2)),
buf.fill(Rectanglei(Vector2i(1, y), Vector2i(pos.width() - 1, y + 1)),
TextCanvas::Char(' ', itemAttr));

d->cursorPos = Vector2i(2, y);
Expand All @@ -258,6 +263,14 @@ void MenuWidget::draw()
}

y++;

// Draw a separator.
if(item.separatorAfter)
{
buf.fill(Rectanglei(Vector2i(1, y), Vector2i(pos.width() - 1, y + 1)),
TextCanvas::Char('-', d->borderAttr));
y++;
}
}

// Draw a frame.
Expand All @@ -270,10 +283,62 @@ bool MenuWidget::handleEvent(Event const *event)
{
if(event->type() != Event::KeyPress) return false;

// Check registered actions.
if(TextWidget::handleEvent(event))
return true;

KeyEvent const *ev = static_cast<KeyEvent const *>(event);

// Fall back to default handling.
TextWidget::handleEvent(event);
if(ev->text() == " ")
{
itemAction(d->cursor).trigger();
close();
return true;
}

if(ev->text().isEmpty())
{
switch(ev->key())
{
case Qt::Key_Up:
if(d->cursor > 0)
{
d->cursor--;
redraw();
}
return true;

case Qt::Key_Down:
if(d->cursor < itemCount() - 1)
{
d->cursor++;
redraw();
}
return true;

case Qt::Key_Home:
case Qt::Key_PageUp:
d->cursor = 0;
redraw();
return true;

case Qt::Key_End:
case Qt::Key_PageDown:
d->cursor = itemCount() - 1;
redraw();
return true;

case Qt::Key_Enter:
itemAction(d->cursor).trigger();
close();
return true;

default:
// Any other control key closes the menu.
close();
return true;
}
}

// When open, a menu eats all key events.
return true;
Expand Down
4 changes: 4 additions & 0 deletions doomsday/tools/shell/shell-text/src/cursesapp.cpp
Expand Up @@ -285,6 +285,10 @@ struct CursesApp::Instance
mods = KeyEvent::Control;
break;

case 0x1b:
code = Qt::Key_Escape;
break;

default:
if(key & KEY_CODE_YES)
qDebug() << "CURSES" << QString("0%1").arg(key, 0, 8).toAscii().constData();
Expand Down

0 comments on commit 667d83a

Please sign in to comment.