Skip to content

Commit

Permalink
Add undo and redo functions to TextInput and TextEdit.
Browse files Browse the repository at this point in the history
The functionality already existed and was usable through keyboard short
cuts but was not accessible through API.

Task-number: QTBUG-16191
Change-Id: I080fa2ddb76668a7a632aa7477004f99037ea68b
Reviewed-by: Martin Jones <martin.jones@nokia.com>
  • Loading branch information
Andrew den Exter authored and Qt by Nokia committed Jan 12, 2012
1 parent 316e5db commit 9d73bf2
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 47 deletions.
6 changes: 0 additions & 6 deletions src/quick/items/qquicktextcontrol.cpp
Expand Up @@ -309,12 +309,6 @@ void QQuickTextControlPrivate::setContent(Qt::TextFormat format, const QString &
QObject::connect(doc, SIGNAL(contentsChanged()), q, SLOT(_q_updateCurrentCharFormatAndSelection()));
QObject::connect(doc, SIGNAL(cursorPositionChanged(QTextCursor)), q, SLOT(_q_emitCursorPosChanged(QTextCursor)));
QObject::connect(doc, SIGNAL(documentLayoutChanged()), q, SLOT(_q_documentLayoutChanged()));

// convenience signal forwards
QObject::connect(doc, SIGNAL(undoAvailable(bool)), q, SIGNAL(undoAvailable(bool)));
QObject::connect(doc, SIGNAL(redoAvailable(bool)), q, SIGNAL(redoAvailable(bool)));
QObject::connect(doc, SIGNAL(modificationChanged(bool)), q, SIGNAL(modificationChanged(bool)));
QObject::connect(doc, SIGNAL(blockCountChanged(int)), q, SIGNAL(blockCountChanged(int)));
}

bool previousUndoRedoState = doc->isUndoRedoEnabled();
Expand Down
2 changes: 0 additions & 2 deletions src/quick/items/qquicktextcontrol_p.h
Expand Up @@ -174,11 +174,9 @@ public Q_SLOTS:
void updateCursorRequest(const QRectF &rect = QRectF());
void updateRequest(const QRectF &rect = QRectF());
void documentSizeChanged(const QSizeF &);
void blockCountChanged(int newBlockCount);
void cursorRectangleChanged();
void linkActivated(const QString &link);
void linkHovered(const QString &);
void modificationChanged(bool m);

public:
// control properties
Expand Down
52 changes: 52 additions & 0 deletions src/quick/items/qquicktextedit.cpp
Expand Up @@ -56,6 +56,7 @@
#include <QtCore/qmath.h>

#include <private/qdeclarativeglobal_p.h>
#include <private/qdeclarativeproperty_p.h>
#include <private/qtextengine_p.h>
#include <QtQuick/private/qsgtexture_p.h>
#include <private/qsgadaptationlayer_p.h>
Expand Down Expand Up @@ -1402,6 +1403,29 @@ void QQuickTextEdit::paste()
}
#endif // QT_NO_CLIPBOARD


/*!
Undoes the last operation if undo is \l {canUndo}{available}. Deselects any
current selection, and updates the selection start to the current cursor
position.
*/

void QQuickTextEdit::undo()
{
Q_D(QQuickTextEdit);
d->control->undo();
}

/*!
Redoes the last operation if redo is \l {canRedo}{available}.
*/

void QQuickTextEdit::redo()
{
Q_D(QQuickTextEdit);
d->control->redo();
}

/*!
\overload
Handles the given mouse \a event.
Expand Down Expand Up @@ -1652,6 +1676,32 @@ bool QQuickTextEdit::canPaste() const
return d->canPaste;
}

/*!
\qmlproperty bool QtQuick2::TextEdit::canUndo
Returns true if the TextEdit is writable and there are previous operations
that can be undone.
*/

bool QQuickTextEdit::canUndo() const
{
Q_D(const QQuickTextEdit);
return d->document->isUndoAvailable();
}

/*!
\qmlproperty bool QtQuick2::TextEdit::canRedo
Returns true if the TextEdit is writable and there are \l {undo}{undone}
operations that can be redone.
*/

bool QQuickTextEdit::canRedo() const
{
Q_D(const QQuickTextEdit);
return d->document->isRedoAvailable();
}

/*!
\qmlproperty bool QtQuick2::TextEdit::inputMethodComposing
Expand Down Expand Up @@ -1709,6 +1759,8 @@ void QQuickTextEditPrivate::init()
#ifndef QT_NO_CLIPBOARD
QObject::connect(QGuiApplication::clipboard(), SIGNAL(dataChanged()), q, SLOT(q_canPasteChanged()));
#endif
FAST_CONNECT(document, SIGNAL(undoAvailable(bool)), q, SIGNAL(canUndoChanged()));
FAST_CONNECT(document, SIGNAL(redoAvailable(bool)), q, SIGNAL(canRedoChanged()));

document->setDefaultFont(font);
document->setDocumentMargin(textMargin);
Expand Down
9 changes: 9 additions & 0 deletions src/quick/items/qquicktextedit_p.h
Expand Up @@ -90,6 +90,8 @@ class Q_AUTOTEST_EXPORT QQuickTextEdit : public QQuickImplicitSizeItem
Q_PROPERTY(bool selectByMouse READ selectByMouse WRITE setSelectByMouse NOTIFY selectByMouseChanged)
Q_PROPERTY(SelectionMode mouseSelectionMode READ mouseSelectionMode WRITE setMouseSelectionMode NOTIFY mouseSelectionModeChanged)
Q_PROPERTY(bool canPaste READ canPaste NOTIFY canPasteChanged)
Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged)
Q_PROPERTY(bool canRedo READ canRedo NOTIFY canRedoChanged)
Q_PROPERTY(bool inputMethodComposing READ isInputMethodComposing NOTIFY inputMethodComposingChanged)

public:
Expand Down Expand Up @@ -193,6 +195,9 @@ class Q_AUTOTEST_EXPORT QQuickTextEdit : public QQuickImplicitSizeItem

bool canPaste() const;

bool canUndo() const;
bool canRedo() const;

virtual void componentComplete();

/* FROM EDIT */
Expand Down Expand Up @@ -248,6 +253,8 @@ class Q_AUTOTEST_EXPORT QQuickTextEdit : public QQuickImplicitSizeItem
void mouseSelectionModeChanged(SelectionMode mode);
void linkActivated(const QString &link);
void canPasteChanged();
void canUndoChanged();
void canRedoChanged();
void inputMethodComposingChanged();
void effectiveHorizontalAlignmentChanged();

Expand All @@ -262,6 +269,8 @@ public Q_SLOTS:
void copy();
void paste();
#endif
void undo();
void redo();
void insert(int position, const QString &text);
void remove(int start, int end);

Expand Down
77 changes: 73 additions & 4 deletions src/quick/items/qquicktextinput.cpp
Expand Up @@ -571,6 +571,7 @@ void QQuickTextInput::setReadOnly(bool ro)
if (!ro)
d->setCursorPosition(d->end());
q_canPasteChanged();
d->emitUndoRedoChanged();
emit readOnlyChanged(ro);
}

Expand Down Expand Up @@ -1753,6 +1754,34 @@ void QQuickTextInput::paste()
}
#endif // QT_NO_CLIPBOARD

/*!
Undoes the last operation if undo is \l {canUndo}{available}. Deselects any
current selection, and updates the selection start to the current cursor
position.
*/

void QQuickTextInput::undo()
{
Q_D(QQuickTextInput);
if (!d->m_readOnly) {
d->internalUndo();
d->finishChange(-1, true);
}
}

/*!
Redoes the last operation if redo is \l {canRedo}{available}.
*/

void QQuickTextInput::redo()
{
Q_D(QQuickTextInput);
if (!d->m_readOnly) {
d->internalRedo();
d->finishChange();
}
}

/*!
\qmlmethod void QtQuick2::TextInput::insert(int position, string text)
Expand Down Expand Up @@ -2043,6 +2072,32 @@ bool QQuickTextInput::canPaste() const
return d->canPaste;
}

/*!
\qmlproperty bool QtQuick2::TextInput::canUndo
Returns true if the TextInput is writable and there are previous operations
that can be undone.
*/

bool QQuickTextInput::canUndo() const
{
Q_D(const QQuickTextInput);
return d->canUndo;
}

/*!
\qmlproperty bool QtQuick2::TextInput::canRedo
Returns true if the TextInput is writable and there are \l {undo}{undone}
operations that can be redone.
*/

bool QQuickTextInput::canRedo() const
{
Q_D(const QQuickTextInput);
return d->canRedo;
}

void QQuickTextInput::moveCursorSelection(int position)
{
Q_D(QQuickTextInput);
Expand Down Expand Up @@ -2988,6 +3043,7 @@ bool QQuickTextInputPrivate::finishChange(int validateFromState, bool update, bo
notifyInputPanel |= (m_cursor == m_lastCursorPos);
if (notifyInputPanel)
q->updateMicroFocus();
emitUndoRedoChanged();
emitCursorPositionChanged();

return true;
Expand Down Expand Up @@ -3557,7 +3613,6 @@ void QQuickTextInputPrivate::internalUndo(int until)
}
}
m_textDirty = true;
emitCursorPositionChanged();
}

void QQuickTextInputPrivate::internalRedo()
Expand Down Expand Up @@ -3600,7 +3655,21 @@ void QQuickTextInputPrivate::internalRedo()
}
}
m_textDirty = true;
emitCursorPositionChanged();
}

void QQuickTextInputPrivate::emitUndoRedoChanged()
{
Q_Q(QQuickTextInput);
const bool previousUndo = canUndo;
const bool previousRedo = canRedo;

canUndo = isUndoAvailable();
canRedo = isRedoAvailable();

if (previousUndo != canUndo)
emit q->canUndoChanged();
if (previousRedo != canRedo)
emit q->canRedoChanged();
}

/*!
Expand Down Expand Up @@ -3725,11 +3794,11 @@ void QQuickTextInputPrivate::processKeyEvent(QKeyEvent* event)
#ifndef QT_NO_SHORTCUT
else if (event == QKeySequence::Undo) {
if (!m_readOnly)
undo();
q->undo();
}
else if (event == QKeySequence::Redo) {
if (!m_readOnly)
redo();
q->redo();
}
else if (event == QKeySequence::SelectAll) {
selectAll();
Expand Down
9 changes: 9 additions & 0 deletions src/quick/items/qquicktextinput_p.h
Expand Up @@ -99,6 +99,8 @@ class Q_AUTOTEST_EXPORT QQuickTextInput : public QQuickImplicitSizeItem
Q_PROPERTY(bool selectByMouse READ selectByMouse WRITE setSelectByMouse NOTIFY selectByMouseChanged)
Q_PROPERTY(SelectionMode mouseSelectionMode READ mouseSelectionMode WRITE setMouseSelectionMode NOTIFY mouseSelectionModeChanged)
Q_PROPERTY(bool canPaste READ canPaste NOTIFY canPasteChanged)
Q_PROPERTY(bool canUndo READ canUndo NOTIFY canUndoChanged)
Q_PROPERTY(bool canRedo READ canRedo NOTIFY canRedoChanged)
Q_PROPERTY(bool inputMethodComposing READ isInputMethodComposing NOTIFY inputMethodComposingChanged)

public:
Expand Down Expand Up @@ -238,6 +240,9 @@ class Q_AUTOTEST_EXPORT QQuickTextInput : public QQuickImplicitSizeItem
QRectF boundingRect() const;
bool canPaste() const;

bool canUndo() const;
bool canRedo() const;

bool isInputMethodComposing() const;

Qt::InputMethodHints imHints() const;
Expand Down Expand Up @@ -275,6 +280,8 @@ class Q_AUTOTEST_EXPORT QQuickTextInput : public QQuickImplicitSizeItem
void selectByMouseChanged(bool selectByMouse);
void mouseSelectionModeChanged(SelectionMode mode);
void canPasteChanged();
void canUndoChanged();
void canRedoChanged();
void inputMethodComposingChanged();
void effectiveHorizontalAlignmentChanged();

Expand Down Expand Up @@ -306,6 +313,8 @@ public Q_SLOTS:
void copy();
void paste();
#endif
void undo();
void redo();
void insert(int position, const QString &text);
void remove(int start, int end);

Expand Down
7 changes: 5 additions & 2 deletions src/quick/items/qquicktextinput_p_p.h
Expand Up @@ -113,6 +113,8 @@ class Q_AUTOTEST_EXPORT QQuickTextInputPrivate : public QQuickImplicitSizeItemPr
, selectByMouse(false)
, canPaste(false)
, canPasteValid(false)
, canUndo(false)
, canRedo(false)
, hAlignImplicit(true)
, selectPressed(false)
, textLayoutDirty(true)
Expand Down Expand Up @@ -237,6 +239,8 @@ class Q_AUTOTEST_EXPORT QQuickTextInputPrivate : public QQuickImplicitSizeItemPr
bool selectByMouse:1;
bool canPaste:1;
bool canPasteValid:1;
bool canUndo:1;
bool canRedo:1;
bool hAlignImplicit:1;
bool selectPressed:1;
bool textLayoutDirty:1;
Expand Down Expand Up @@ -349,8 +353,6 @@ class Q_AUTOTEST_EXPORT QQuickTextInputPrivate : public QQuickImplicitSizeItemPr

void insert(const QString &);
void clear();
void undo() { internalUndo(); finishChange(-1, true); }
void redo() { internalRedo(); finishChange(); }
void selectWordAtPos(int);

void setCursorPosition(int pos) { if (pos <= m_text.length()) moveCursor(qMax(0, pos)); }
Expand Down Expand Up @@ -422,6 +424,7 @@ class Q_AUTOTEST_EXPORT QQuickTextInputPrivate : public QQuickImplicitSizeItemPr

void internalUndo(int until = -1);
void internalRedo();
void emitUndoRedoChanged();

void emitCursorPositionChanged();

Expand Down

0 comments on commit 9d73bf2

Please sign in to comment.