aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick
diff options
context:
space:
mode:
authorAndrew den Exter <andrew.den-exter@nokia.com>2012-01-10 11:52:43 +1000
committerQt by Nokia <qt-info@nokia.com>2012-01-12 09:33:32 +0100
commit9d73bf289e2719d781670baad1733989a56d7f40 (patch)
tree468c8a6258343874f0781b30c07287485fa4cbaf /src/quick
parent316e5db8a403ce8493e22361c446a003a5c3770c (diff)
Add undo and redo functions to TextInput and TextEdit.
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>
Diffstat (limited to 'src/quick')
-rw-r--r--src/quick/items/qquicktextcontrol.cpp6
-rw-r--r--src/quick/items/qquicktextcontrol_p.h2
-rw-r--r--src/quick/items/qquicktextedit.cpp52
-rw-r--r--src/quick/items/qquicktextedit_p.h9
-rw-r--r--src/quick/items/qquicktextinput.cpp77
-rw-r--r--src/quick/items/qquicktextinput_p.h9
-rw-r--r--src/quick/items/qquicktextinput_p_p.h7
7 files changed, 148 insertions, 14 deletions
diff --git a/src/quick/items/qquicktextcontrol.cpp b/src/quick/items/qquicktextcontrol.cpp
index 1b3e35bcef..4f9475f583 100644
--- a/src/quick/items/qquicktextcontrol.cpp
+++ b/src/quick/items/qquicktextcontrol.cpp
@@ -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();
diff --git a/src/quick/items/qquicktextcontrol_p.h b/src/quick/items/qquicktextcontrol_p.h
index 6b4c1a869c..0ea21169c9 100644
--- a/src/quick/items/qquicktextcontrol_p.h
+++ b/src/quick/items/qquicktextcontrol_p.h
@@ -174,11 +174,9 @@ Q_SIGNALS:
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
diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp
index a11acd7afe..eba85828bc 100644
--- a/src/quick/items/qquicktextedit.cpp
+++ b/src/quick/items/qquicktextedit.cpp
@@ -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>
@@ -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.
@@ -1653,6 +1677,32 @@ bool QQuickTextEdit::canPaste() const
}
/*!
+ \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
@@ -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);
diff --git a/src/quick/items/qquicktextedit_p.h b/src/quick/items/qquicktextedit_p.h
index ef16cfe129..f37b7cd8ff 100644
--- a/src/quick/items/qquicktextedit_p.h
+++ b/src/quick/items/qquicktextedit_p.h
@@ -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:
@@ -193,6 +195,9 @@ public:
bool canPaste() const;
+ bool canUndo() const;
+ bool canRedo() const;
+
virtual void componentComplete();
/* FROM EDIT */
@@ -248,6 +253,8 @@ Q_SIGNALS:
void mouseSelectionModeChanged(SelectionMode mode);
void linkActivated(const QString &link);
void canPasteChanged();
+ void canUndoChanged();
+ void canRedoChanged();
void inputMethodComposingChanged();
void effectiveHorizontalAlignmentChanged();
@@ -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);
diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp
index 7858345198..df536f4f59 100644
--- a/src/quick/items/qquicktextinput.cpp
+++ b/src/quick/items/qquicktextinput.cpp
@@ -571,6 +571,7 @@ void QQuickTextInput::setReadOnly(bool ro)
if (!ro)
d->setCursorPosition(d->end());
q_canPasteChanged();
+ d->emitUndoRedoChanged();
emit readOnlyChanged(ro);
}
@@ -1754,6 +1755,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)
Inserts \a text into the TextInput at position.
@@ -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);
@@ -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;
@@ -3557,7 +3613,6 @@ void QQuickTextInputPrivate::internalUndo(int until)
}
}
m_textDirty = true;
- emitCursorPositionChanged();
}
void QQuickTextInputPrivate::internalRedo()
@@ -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();
}
/*!
@@ -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();
diff --git a/src/quick/items/qquicktextinput_p.h b/src/quick/items/qquicktextinput_p.h
index eadd0ccc1b..535b1af266 100644
--- a/src/quick/items/qquicktextinput_p.h
+++ b/src/quick/items/qquicktextinput_p.h
@@ -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:
@@ -238,6 +240,9 @@ public:
QRectF boundingRect() const;
bool canPaste() const;
+ bool canUndo() const;
+ bool canRedo() const;
+
bool isInputMethodComposing() const;
Qt::InputMethodHints imHints() const;
@@ -275,6 +280,8 @@ Q_SIGNALS:
void selectByMouseChanged(bool selectByMouse);
void mouseSelectionModeChanged(SelectionMode mode);
void canPasteChanged();
+ void canUndoChanged();
+ void canRedoChanged();
void inputMethodComposingChanged();
void effectiveHorizontalAlignmentChanged();
@@ -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);
diff --git a/src/quick/items/qquicktextinput_p_p.h b/src/quick/items/qquicktextinput_p_p.h
index b20b8c44f2..03d825d108 100644
--- a/src/quick/items/qquicktextinput_p_p.h
+++ b/src/quick/items/qquicktextinput_p_p.h
@@ -113,6 +113,8 @@ public:
, selectByMouse(false)
, canPaste(false)
, canPasteValid(false)
+ , canUndo(false)
+ , canRedo(false)
, hAlignImplicit(true)
, selectPressed(false)
, textLayoutDirty(true)
@@ -237,6 +239,8 @@ public:
bool selectByMouse:1;
bool canPaste:1;
bool canPasteValid:1;
+ bool canUndo:1;
+ bool canRedo:1;
bool hAlignImplicit:1;
bool selectPressed:1;
bool textLayoutDirty:1;
@@ -349,8 +353,6 @@ public:
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)); }
@@ -422,6 +424,7 @@ private:
void internalUndo(int until = -1);
void internalRedo();
+ void emitUndoRedoChanged();
void emitCursorPositionChanged();