[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [calligra/kotext-undoRedo-PierreSt] libs/kotext: tempo5
From:       Pierre Stirnweiss <pstirnweiss () googlemail ! com>
Date:       2012-02-20 19:56:55
Message-ID: 20120220195655.C886CA60A6 () git ! kde ! org
[Download RAW message or body]

Git commit 0ff1bc7cc43391f93d8648cfbf3733054540d83a by Pierre Stirnweiss.
Committed on 20/02/2012 at 20:55.
Pushed by pstirnweiss into branch 'kotext-undoRedo-PierreSt'.

tempo5

M  +37   -14   libs/kotext/KoTextEditor.cpp
M  +6    -2    libs/kotext/KoTextEditor.h
M  +1    -0    libs/kotext/KoTextEditor_p.h
M  +5    -4    libs/kotext/commands/TextPasteCommand.cpp

http://commits.kde.org/calligra/0ff1bc7cc43391f93d8648cfbf3733054540d83a

diff --git a/libs/kotext/KoTextEditor.cpp b/libs/kotext/KoTextEditor.cpp
index 1abd339..246e38c 100644
--- a/libs/kotext/KoTextEditor.cpp
+++ b/libs/kotext/KoTextEditor.cpp
@@ -96,6 +96,7 @@ KoTextEditor::Private::Private(KoTextEditor *qq, QTextDocument \
*document)  , document (document)
     , headCommand(0)
     , addNewCommand(true)
+    , inCustomCommand(0)
     , editProtectionCached(false)
 {
     caret = QTextCursor(document);
@@ -180,7 +181,9 @@ void KoTextEditor::Private::documentCommandAdded()
 void KoTextEditor::Private::updateState(KoTextEditor::Private::State newState, \
QString title)  {
     kDebug() << "updateState. editorState: " << editorState << " newState: " << \
                newState << " command title: " << title;
-    if (editorState == Custom && newState ==Custom) {
+    kDebug() << "commandStack count: " << commandStack.count();
+    if (editorState == Custom && newState != NoOp) {
+        kDebug() << "custom and !NoOp";
         addNewCommand = true;
         if (!title.isEmpty())
             commandTitle = title;
@@ -190,21 +193,26 @@ void \
KoTextEditor::Private::updateState(KoTextEditor::Private::State newState, Q  return;
     }
     if (newState == NoOp && !commandStack.isEmpty()) {
-                kDebug() << "reset bck to nopop headCommand ";
+        kDebug() << "reset bck to NoOp";
         commandStack.pop();
+        kDebug() << "commandStack count now: " << commandStack.count();
         addNewCommand = true;
+        if (commandStack.isEmpty()) {
+            editorState = NoOp;
+        }
+        kDebug() << "returning";
         return;
     }
-    kDebug() << "updateState from: " << editorState << " to: " << newState;
     if (editorState != newState || commandTitle != title) {
+        kDebug() << "editorState != newState...";
 //        if (headCommand /*&& headCommand->childCount() && \
                KoTextDocument(document).undoStack()*/) {
-            if (!commandStack.isEmpty()) {
-                kDebug() << "reset headCommand ";
-                commandStack.pop();
-                addNewCommand = true;
-            }
-            //            KoTextDocument(document).undoStack()->push(headCommand);
-            //headCommand = 0;
+        if (!commandStack.isEmpty()) {
+            kDebug() << "reset headCommand: ";
+            commandStack.pop();
+            addNewCommand = true;
+        }
+        //            KoTextDocument(document).undoStack()->push(headCommand);
+        //headCommand = 0;
 //        }
     }
     editorState = newState;
@@ -387,8 +395,11 @@ QTextCursor* KoTextEditor::cursor()
 void KoTextEditor::addCommand(KUndo2Command *command)
 {
     kDebug() << "will push the custom command: " << command->text();
+    kDebug() << "commandStack count before updating state: " << \
d->commandStack.count(); +    ++d->inCustomCommand;
     d->updateState(KoTextEditor::Private::Custom, \
(!command->text().isEmpty())?command->text():i18n("Text"));  kDebug() << "will push \
the custom command: " << command->text(); +    kDebug() << "commandStack count after \
updateState: " << d->commandStack.count();  //    d->headCommand = command;
     //TODO this is ugly. clean up
     d->addNewCommand = false;
@@ -405,9 +416,17 @@ void KoTextEditor::addCommand(KUndo2Command *command)
     }
     else {
         d->commandStack.push(command);
+        kDebug() << "commandStack count after push: " << d->commandStack.count();
         command->redo();
     }
+    kDebug() << "finished with: " << command->text();
+    kDebug() << "commandStack count before updateState to NoOp: " << \
d->commandStack.count(); +    while(d->commandStack.top() != command) { //clean auto \
generated commands which at that point should not be here anymore. in particular \
insertText being open ended will not have been cleared. +        \
d->commandStack.pop(); +    }
     d->updateState(KoTextEditor::Private::NoOp);
+    kDebug() << "commandStack count after updateState to NoOp: " << \
d->commandStack.count(); +    --d->inCustomCommand;
 }
 
 void KoTextEditor::instantlyExecuteCommand(KUndo2Command *command)
@@ -1126,6 +1145,8 @@ KoInlineObject *KoTextEditor::insertIndexMarker()
         return 0;
     }
 
+    d->updateState(KoTextEditor::Private::Custom, i18n("Insert Index"));
+
     int startPosition = d->caret.position();
 
     if (d->caret.blockFormat().hasProperty(KoParagraphStyle::HiddenByTable)) {
@@ -1149,7 +1170,6 @@ KoInlineObject *KoTextEditor::insertIndexMarker()
     if (block.text()[ d->caret.position() - block.position()].isSpace())
         return 0; // can't insert one on a whitespace as that does not indicate a \
word.  
-    d->updateState(KoTextEditor::Private::Custom, i18n("Insert Index"));
     KoTextLocator *tl = new KoTextLocator();
     KoTextDocument(d->document).inlineTextObjectManager()->insertInlineObject(d->caret, \
tl);  d->updateState(KoTextEditor::Private::NoOp);
@@ -2436,11 +2456,12 @@ const QTextTable *KoTextEditor::currentTable () const
     return d->caret.currentTable();
 }
 
-
 void KoTextEditor::beginEditBlock()
 {
     kDebug() << "beginEditBlock";
-    d->updateState(KoTextEditor::Private::Custom);
+    if (!d->inCustomCommand) {
+        d->updateState(KoTextEditor::Private::Custom);
+    }
     d->caret.beginEditBlock();
 }
 
@@ -2449,7 +2470,9 @@ void KoTextEditor::endEditBlock()
     kDebug() << "endEditBlock";
     d->caret.endEditBlock();
     kDebug() << "doc end edit done";
-    d->updateState(KoTextEditor::Private::NoOp);
+    if (!d->inCustomCommand) {
+        d->updateState(KoTextEditor::Private::NoOp);
+    }
     kDebug() << "done reseting style to 0. endEditBlock finished";
 }
 
diff --git a/libs/kotext/KoTextEditor.h b/libs/kotext/KoTextEditor.h
index 689051b..2a525a7 100644
--- a/libs/kotext/KoTextEditor.h
+++ b/libs/kotext/KoTextEditor.h
@@ -307,8 +307,6 @@ public slots:
 
     bool atStart() const;
 
-    void beginEditBlock();
-
     QTextBlock block() const;
 
     QTextCharFormat blockCharFormat() const;
@@ -329,6 +327,12 @@ public slots:
 
     const QTextDocument *document() const;
 
+    //Starts a new custom command. Everything between these two is one custom \
command. These should not be called from whithin a KUndo2Command +//    void \
beginCustomCommand(); +//    void endCustomCommand();
+
+    //Same as Qt, only to be used inside KUndo2Commands
+    void beginEditBlock();
     void endEditBlock();
 
     bool hasComplexSelection() const;
diff --git a/libs/kotext/KoTextEditor_p.h b/libs/kotext/KoTextEditor_p.h
index 9b19bbc..d99128c 100644
--- a/libs/kotext/KoTextEditor_p.h
+++ b/libs/kotext/KoTextEditor_p.h
@@ -66,6 +66,7 @@ public:
     KUndo2Command *headCommand;
     QStack<KUndo2Command*> commandStack;
     bool addNewCommand;
+    int inCustomCommand;
     QString commandTitle;
 
     State editorState;
diff --git a/libs/kotext/commands/TextPasteCommand.cpp \
b/libs/kotext/commands/TextPasteCommand.cpp index 6b9de68..684dd69 100644
--- a/libs/kotext/commands/TextPasteCommand.cpp
+++ b/libs/kotext/commands/TextPasteCommand.cpp
@@ -70,6 +70,7 @@ void TextPasteCommand::undo()
 
 void TextPasteCommand::redo()
 {
+    kDebug() << "textPasteCommand redo";
     if (m_document.isNull()) return;
 
     KoTextDocument textDocument(m_document);
@@ -79,15 +80,15 @@ void TextPasteCommand::redo()
         KUndo2Command::redo();
     } else {
         //kDebug() << "begin paste command";
-        editor->beginEditBlock();
+        editor->beginEditBlock(); //this is needed so Qt does not merge successive \
paste actions together  m_first = false;
         if (editor->hasSelection()) { //TODO
             // XXX: this was m_tool->m_actionShowChanges.isChecked -- but shouldn't \
we check  // whether we should record changes here, instead of showing?
             if (textDocument.changeTracker()->recordChanges()) {
-                editor->addCommand(new \
ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, m_document.data(), \
m_shapeController)); +                editor->addCommand(new \
ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, m_document.data(), \
m_shapeController, this));  } else {
-                editor->addCommand(new DeleteCommand(DeleteCommand::NextChar, \
m_document.data(), m_shapeController)); +                editor->addCommand(new \
DeleteCommand(DeleteCommand::NextChar, m_document.data(), m_shapeController, this));  \
}  }
 
@@ -137,6 +138,6 @@ void TextPasteCommand::redo()
             editor->insertText(m_mimeData->text());
             //kDebug() << "done with pasting";
         }
-        editor->endEditBlock();
+        editor->endEditBlock(); //see above beginEditBlock
     }
 }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic