Skip to content

Commit

Permalink
libshell: TextCanvas interface improvements, handling Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jan 29, 2013
1 parent c66911b commit 073ead0
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 27 deletions.
4 changes: 3 additions & 1 deletion doomsday/libshell/include/de/shell/textcanvas.h
Expand Up @@ -102,7 +102,9 @@ class LIBSHELL_PUBLIC TextCanvas

virtual ~TextCanvas();

Size size() const;
Size size() const;
int width() const;
int height() const;

void resize(Size const &newSize);

Expand Down
53 changes: 52 additions & 1 deletion doomsday/libshell/include/de/shell/textwidget.h
Expand Up @@ -23,16 +23,35 @@
#include <de/Widget>
#include <de/RectangleRule>
#include <QObject>
#include <QFlags>
#include "TextCanvas"

namespace de {
namespace shell {

class TextRootWidget;
class Action;

/**
* Flags for specifying alignment.
*/
enum AlignmentFlag
{
AlignTop = 0x1,
AlignBottom = 0x2,
AlignLeft = 0x4,
AlignRight = 0x8
};
Q_DECLARE_FLAGS(Alignment, AlignmentFlag)
Q_DECLARE_OPERATORS_FOR_FLAGS(Alignment)

/**
* Generic widget with a text-based visual.
*
* TextWidget is the base class for all widgets in libshell, because they are
* intended to be device-independent and compatible with all character-based
* UIs, regardless of whether the underlying device is text-only or graphical.
*
* It is assumed that the root widget under which text widgets are used is
* derived from TextRootWidget.
*
Expand All @@ -48,9 +67,21 @@ class LIBSHELL_PUBLIC TextWidget : public QObject, public Widget

TextRootWidget &root() const;

/**
* Sets the text canvas on which this widget is to be drawn. Calling this
* is optional; by default all widgets use the root widget's canvas.
*
* @param canvas Canvas for drawing. Ownership not taken.
*/
void setTargetCanvas(TextCanvas *canvas);

TextCanvas *targetCanvas() const;
/**
* Returns the text canvas on which this widget is to be drawn. Derived
* classes can use this method to find out where to draw themselves.
*
* @return Destination canvas for drawing.
*/
TextCanvas &targetCanvas() const;

/**
* Draw this widget and all its children, and show the target canvas
Expand Down Expand Up @@ -78,6 +109,26 @@ class LIBSHELL_PUBLIC TextWidget : public QObject, public Widget
*/
virtual Vector2i cursorPosition();

/**
* Adds a new action for the widget. During event processing actions are
* checked in the order they have been added to the widget.
*
* @param action Action instance. Ownership taken.
*/
void addAction(Action *action);

/**
* Removes an action from the widget.
*
* @param action Action instance.
*/
void removeAction(Action &action);

/**
* Checks actions and triggers them when suitable event is received.
*/
bool handleEvent(Event const *event);

private:
struct Instance;
Instance *d;
Expand Down
6 changes: 6 additions & 0 deletions doomsday/libshell/src/libshell.cpp
Expand Up @@ -16,3 +16,9 @@
* http://www.gnu.org/licenses</small>
*/

#include "de/shell/libshell.h"

namespace de {
namespace shell {
} // namespace shell
} // namespace de
15 changes: 8 additions & 7 deletions doomsday/libshell/src/lineeditwidget.cpp
Expand Up @@ -22,8 +22,8 @@
#include "de/shell/TextRootWidget"
#include "de/shell/KeyEvent"

using namespace de;
using namespace de::shell;
namespace de {
namespace shell {

struct LineEditWidget::Instance
{
Expand Down Expand Up @@ -217,7 +217,7 @@ LineEditWidget::~LineEditWidget()
delete d;
}

void LineEditWidget::setPrompt(const String &promptText)
void LineEditWidget::setPrompt(String const &promptText)
{
d->prompt = promptText;

Expand All @@ -241,9 +241,7 @@ void LineEditWidget::viewResized()

void LineEditWidget::draw()
{
TextCanvas *cv = targetCanvas();
if(!cv) return;

TextCanvas &cv = targetCanvas();
Rectanglei pos = rule().recti();

// Temporary buffer for drawing.
Expand All @@ -262,7 +260,7 @@ void LineEditWidget::draw()
buf.drawText(Vector2i(d->prompt.size(), y), part, attr);
}

cv->draw(buf, pos.topLeft);
cv.draw(buf, pos.topLeft);
}

bool LineEditWidget::handleEvent(Event const *event)
Expand Down Expand Up @@ -368,3 +366,6 @@ int LineEditWidget::cursor() const
{
return d->cursor;
}

} // namespace shell
} // namespace de
2 changes: 1 addition & 1 deletion doomsday/libshell/src/link.cpp
Expand Up @@ -109,7 +109,7 @@ Packet *Link::nextPacket()

std::auto_ptr<Message> data(d->socket->receive());
Packet *packet = d->protocol.interpret(*data.get());
if(packet) packet->setFrom(d->peerAddress);
if(packet) packet->setFrom(data->address());
return packet;
}

Expand Down
5 changes: 3 additions & 2 deletions doomsday/libshell/src/protocol.cpp
Expand Up @@ -98,6 +98,7 @@ Protocol::PacketType Protocol::recognize(Packet const *packet)
return LogEntries;
}

// One of the generic-format packets?
RecordPacket const *rec = dynamic_cast<RecordPacket const *>(packet);
if(rec)
{
Expand All @@ -110,7 +111,7 @@ Protocol::PacketType Protocol::recognize(Packet const *packet)
RecordPacket *Protocol::newCommand(String const &command)
{
RecordPacket *cmd = new RecordPacket(PT_COMMAND);
cmd->record().addText("cmd", command);
cmd->record().addText("execute", command);
return cmd;
}

Expand All @@ -121,7 +122,7 @@ String Protocol::command(Packet const &commandPacket)
RecordPacket const *rec = dynamic_cast<RecordPacket const *>(&commandPacket);
DENG2_ASSERT(rec != 0);

return (*rec)["cmd"].value().asText();
return (*rec)["execute"].value().asText();
}

} // namespace shell
Expand Down
10 changes: 10 additions & 0 deletions doomsday/libshell/src/textcanvas.cpp
Expand Up @@ -111,6 +111,16 @@ TextCanvas::Size TextCanvas::size() const
return d->size;
}

int TextCanvas::width() const
{
return d->size.x;
}

int TextCanvas::height() const
{
return d->size.y;
}

void TextCanvas::resize(Size const &newSize)
{
d->resize(newSize);
Expand Down
47 changes: 38 additions & 9 deletions doomsday/libshell/src/textwidget.cpp
Expand Up @@ -18,6 +18,8 @@

#include "de/shell/TextWidget"
#include "de/shell/TextRootWidget"
#include "de/shell/Action"
#include <QList>

namespace de {
namespace shell {
Expand All @@ -26,13 +28,15 @@ struct TextWidget::Instance
{
TextCanvas *canvas;
RectangleRule *rule;
QList<Action *> actions;

Instance() : canvas(0), rule(new RectangleRule)
{}

~Instance()
{
releaseRef(rule);
foreach(Action *act, actions) delete act;
}
};

Expand All @@ -56,24 +60,21 @@ void TextWidget::setTargetCanvas(TextCanvas *canvas)
d->canvas = canvas;
}

TextCanvas *TextWidget::targetCanvas() const
TextCanvas &TextWidget::targetCanvas() const
{
if(!d->canvas)
{
// A specific target not defined, use the root canvas.
return &root().rootCanvas();
return root().rootCanvas();
}
return d->canvas;
return *d->canvas;
}

void TextWidget::drawAndShow()
{
if(targetCanvas())
{
draw();
notifyTree(&Widget::draw);
targetCanvas()->show();
}
draw();
notifyTree(&Widget::draw);
targetCanvas().show();
}

void TextWidget::setRule(RectangleRule *rule)
Expand All @@ -94,5 +95,33 @@ Vector2i TextWidget::cursorPosition()
floor(rule().top()->value()));
}

void TextWidget::addAction(Action *action)
{
d->actions.append(action);
}

void TextWidget::removeAction(Action &action)
{
d->actions.removeAll(&action);
}

bool TextWidget::handleEvent(Event const *event)
{
// We only support KeyEvents.
if(event->type() == Event::KeyPress)
{
DENG2_ASSERT(dynamic_cast<KeyEvent const *>(event) != 0);
KeyEvent const *keyEvent = static_cast<KeyEvent const *>(event);

foreach(Action *act, d->actions)
{
// Event will be used by actions.
if(act->tryTrigger(*keyEvent)) return true;
}
}

return Widget::handleEvent(event);
}

} // namespace shell
} // namespace de
4 changes: 1 addition & 3 deletions doomsday/tools/shell/shell-text/src/logwidget.cpp
Expand Up @@ -111,8 +111,6 @@ LogSink &LogWidget::logSink()

void LogWidget::draw()
{
if(!targetCanvas()) return;

Rectanglei pos = rule().recti();
TextCanvas buf(pos.size());

Expand Down Expand Up @@ -160,7 +158,7 @@ void LogWidget::draw()

d->sink.unlock();

targetCanvas()->draw(buf, pos.topLeft);
targetCanvas().draw(buf, pos.topLeft);
}

void LogWidget::redraw()
Expand Down
4 changes: 1 addition & 3 deletions doomsday/tools/shell/shell-text/src/statuswidget.cpp
Expand Up @@ -62,8 +62,6 @@ void StatusWidget::setShellLink(Link *link)

void StatusWidget::draw()
{
if(!targetCanvas()) return;

Rectanglei pos = rule().recti();
TextCanvas buf(pos.size());

Expand Down Expand Up @@ -93,7 +91,7 @@ void StatusWidget::draw()
buf.drawText(x, host);
}

targetCanvas()->draw(buf, pos.topLeft);
targetCanvas().draw(buf, pos.topLeft);
}

void StatusWidget::redraw()
Expand Down

0 comments on commit 073ead0

Please sign in to comment.