From 1e36e3917cae1e6b55cce46dc7e0e8a0b336cd01 Mon Sep 17 00:00:00 2001 From: Thierry Bastian Date: Tue, 24 Nov 2009 15:29:37 +0100 Subject: Make paste + undo behave in QLineEdit as it does in QTextEdit On the undo/redo stack, it needs to be treated as a separate command Task-number: QTBUG-5786 Reviewed-by: ogoffart --- src/gui/widgets/qlinecontrol.cpp | 7 ++++++- tests/auto/qlineedit/tst_qlineedit.cpp | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/gui/widgets/qlinecontrol.cpp b/src/gui/widgets/qlinecontrol.cpp index 300a2ea..334a925 100644 --- a/src/gui/widgets/qlinecontrol.cpp +++ b/src/gui/widgets/qlinecontrol.cpp @@ -138,7 +138,12 @@ void QLineControl::copy(QClipboard::Mode mode) const */ void QLineControl::paste() { - insert(QApplication::clipboard()->text(QClipboard::Clipboard)); + QString clip = QApplication::clipboard()->text(QClipboard::Clipboard); + if (!clip.isEmpty() || hasSelectedText()) { + separate(); //make it a separate undo/redo command + insert(clip); + separate(); + } } #endif // !QT_NO_CLIPBOARD diff --git a/tests/auto/qlineedit/tst_qlineedit.cpp b/tests/auto/qlineedit/tst_qlineedit.cpp index b4dfbba..dd5bb29 100644 --- a/tests/auto/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/qlineedit/tst_qlineedit.cpp @@ -51,6 +51,10 @@ #include "qcompleter.h" #include "qstandarditemmodel.h" +#ifndef QT_NO_CLIPBOARD +#include "qclipboard.h" +#endif + #ifdef Q_WS_MAC #include // For the random function. #include // For the random function. @@ -157,6 +161,10 @@ private slots: void undo_keypressevents_data(); void undo_keypressevents(); +#ifndef QT_NO_CLIPBOARD + void QTBUG5786_undoPaste(); +#endif + void clear(); void text_data(); @@ -1406,6 +1414,36 @@ void tst_QLineEdit::undo_keypressevents() QVERIFY(testWidget->text().isEmpty()); } +#ifndef QT_NO_CLIPBOARD +void tst_QLineEdit::QTBUG5786_undoPaste() +{ + QString initial("initial"); + QString string("test"); + QString additional("add"); + QApplication::clipboard()->setText(string); + QLineEdit edit(initial); + QCOMPARE(edit.text(), initial); + edit.paste(); + QCOMPARE(edit.text(), initial + string); + edit.paste(); + QCOMPARE(edit.text(), initial + string + string); + edit.insert(additional); + QCOMPARE(edit.text(), initial + string + string + additional); + edit.undo(); + QCOMPARE(edit.text(), initial + string + string); + edit.undo(); + QCOMPARE(edit.text(), initial + string); + edit.undo(); + QCOMPARE(edit.text(), initial); + edit.selectAll(); + QApplication::clipboard()->setText(QString()); + edit.paste(); + QVERIFY(edit.text().isEmpty()); + +} +#endif + + void tst_QLineEdit::clear() { // checking that clear of empty/nullstring doesn't add to undo history -- cgit v0.12