Skip to content

Commit

Permalink
Refactor|libshell: Word jumping and key modifiers in AbstractLineEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jul 24, 2013
1 parent 5817be2 commit 887dc30
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
13 changes: 12 additions & 1 deletion doomsday/libshell/include/de/shell/abstractlineeditor.h
Expand Up @@ -96,7 +96,16 @@ class LIBSHELL_PUBLIC AbstractLineEditor : public ITextEditor

EchoMode echoMode() const;

virtual bool handleControlKey(int qtKey, bool controlMod = true);
enum KeyModifier {
Unmodified = 0,
Shift = 0x1,
Control = 0x2,
Alt = 0x4,
Meta = 0x8
};
Q_DECLARE_FLAGS(KeyModifiers, KeyModifier)

virtual bool handleControlKey(int qtKey, KeyModifiers const &mods = Unmodified);

/**
* Inserts a fragment of text at the cursor position. The cursor moves
Expand Down Expand Up @@ -129,6 +138,8 @@ class LIBSHELL_PUBLIC AbstractLineEditor : public ITextEditor
DENG2_PRIVATE(d)
};

Q_DECLARE_OPERATORS_FOR_FLAGS(AbstractLineEditor::KeyModifiers)

} // namespace shell
} // namespace de

Expand Down
2 changes: 1 addition & 1 deletion doomsday/libshell/include/de/shell/lineeditwidget.h
Expand Up @@ -54,7 +54,7 @@ class LIBSHELL_PUBLIC LineEditWidget : public TextWidget, public AbstractLineEdi

Vector2i cursorPosition() const;

bool handleControlKey(int qtKey, bool controlMod = true);
bool handleControlKey(int qtKey, KeyModifiers const &mods = Unmodified);

// Events.
void viewResized();
Expand Down
73 changes: 69 additions & 4 deletions doomsday/libshell/src/abstractlineeditor.cpp
Expand Up @@ -201,6 +201,37 @@ DENG2_PIMPL(AbstractLineEditor)
}
}

void doWordLeft()
{
acceptCompletion();

// First jump over any non-word chars.
while(cursor > 0 && !text[cursor].isLetterOrNumber()) cursor--;

// At least move one character.
if(cursor > 0) cursor--;

// We're inside a word, jump to its beginning.
while(cursor > 0 && text[cursor - 1].isLetterOrNumber()) cursor--;

self.cursorMoved();
}

void doWordRight()
{
int const last = text.size() - 1;

acceptCompletion();

// If inside a word, jump to its end.
while(cursor < last && text[cursor].isLetterOrNumber()) cursor++;

// Jump over any non-word chars.
while(cursor < last && !text[cursor].isLetterOrNumber()) cursor++;

self.cursorMoved();
}

void doHome()
{
acceptCompletion();
Expand Down Expand Up @@ -434,8 +465,14 @@ AbstractLineEditor::EchoMode AbstractLineEditor::echoMode() const
return d->echoMode;
}

bool AbstractLineEditor::handleControlKey(int qtKey, bool controlMod)
bool AbstractLineEditor::handleControlKey(int qtKey, KeyModifiers const &mods)
{
#ifdef MACOSX
# define WORD_JUMP_MODIFIER Alt
#else
# define WORD_JUMP_MODIFIER Control
#endif

switch(qtKey)
{
case Qt::Key_Backspace:
Expand All @@ -447,11 +484,39 @@ bool AbstractLineEditor::handleControlKey(int qtKey, bool controlMod)
return true;

case Qt::Key_Left:
d->doLeft();
#ifdef MACOSX
if(mods.testFlag(Control))
{
d->doHome();
return true;
}
#endif
if(mods.testFlag(WORD_JUMP_MODIFIER))
{
d->doWordLeft();
}
else
{
d->doLeft();
}
return true;

case Qt::Key_Right:
d->doRight();
#ifdef MACOSX
if(mods.testFlag(Control))
{
d->doEnd();
return true;
}
#endif
if(mods.testFlag(WORD_JUMP_MODIFIER))
{
d->doWordRight();
}
else
{
d->doRight();
}
return true;

case Qt::Key_Home:
Expand All @@ -471,7 +536,7 @@ bool AbstractLineEditor::handleControlKey(int qtKey, bool controlMod)
break;

case Qt::Key_K:
if(controlMod)
if(mods.testFlag(Control))
{
d->killEndOfLine();
return true;
Expand Down
4 changes: 2 additions & 2 deletions doomsday/libshell/src/lineeditwidget.cpp
Expand Up @@ -130,9 +130,9 @@ bool LineEditWidget::handleEvent(Event const &event)
return TextWidget::handleEvent(event);
}

bool LineEditWidget::handleControlKey(int qtKey, bool controlMod)
bool LineEditWidget::handleControlKey(int qtKey, KeyModifiers const &mods)
{
if(AbstractLineEditor::handleControlKey(qtKey, controlMod))
if(AbstractLineEditor::handleControlKey(qtKey, mods))
{
if(qtKey == Qt::Key_Enter)
{
Expand Down

0 comments on commit 887dc30

Please sign in to comment.