File indexing completed on 2024-05-12 16:34:57

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Thomas Zander <zander@kde.org>
0003  * Copyright (C) 2008 Pierre Stirnweiss \pierre.stirnweiss_calligra@gadz.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "ChangeTracker.h"
0022 #include "TextTool.h"
0023 #include "TextShapeDebug.h"
0024 
0025 ChangeTracker::ChangeTracker(TextTool *parent)
0026         : QObject(parent),
0027         m_document(0),
0028         m_tool(parent),
0029         m_enableSignals(true),
0030         m_reverseUndo(false)
0031 {
0032 }
0033 
0034 void ChangeTracker::setDocument(QTextDocument * document)
0035 {
0036     m_reverseUndo = false;
0037     if (m_document)
0038         disconnect(m_document, SIGNAL(contentsChange(int,int,int)), this, SLOT(contentsChange(int,int,int)));
0039     m_document = document;
0040     if (m_document)
0041         connect(m_document, SIGNAL(contentsChange(int,int,int)), this, SLOT(contentsChange(int,int,int)));
0042 }
0043 
0044 int ChangeTracker::getChangeId(QString title, int existingChangeId)
0045 {
0046     Q_UNUSED(title);
0047     Q_UNUSED(existingChangeId);
0048     debugTextShape << "ChangeTracker::changeId :" << m_changeId;
0049     return m_changeId++;
0050 }
0051 
0052 void ChangeTracker::contentsChange(int from, int charsRemoves, int charsAdded)
0053 {
0054     Q_UNUSED(from);
0055     Q_UNUSED(charsRemoves);
0056     Q_UNUSED(charsAdded);
0057 /*
0058     if (! m_enableSignals) return;
0059     m_enableSignals = false;
0060     debugTextShape << "ChangeTracker::contentsChange" << from << "," << charsRemoves << "," << charsAdded;
0061 
0062     if (charsRemoves == 0 && charsAdded == 0) {
0063         // I think we can quietly ignore this.
0064     } else if (charsRemoves == 0) { // easy
0065         QTextCursor cursor(m_document);
0066         cursor.setPosition(from);
0067         cursor.setPosition(from + charsAdded, QTextCursor::KeepAnchor);
0068         debugTextShape << "   added text:" << cursor.selectedText();
0069     } else {
0070         bool prev = m_tool->m_allowAddUndoCommand;
0071         m_tool->m_allowAddUndoCommand = false;
0072         m_reverseUndo ? m_document->redo() : m_document->undo();
0073         QTextCursor cursor(m_document);
0074         cursor.setPosition(from);
0075         cursor.setPosition(from + charsRemoves, QTextCursor::KeepAnchor);
0076         QString previousText = cursor.selectedText();
0077         m_reverseUndo ? m_document->undo() : m_document->redo();
0078         m_tool->m_allowAddUndoCommand = prev;
0079 
0080         cursor.setPosition(from);
0081         cursor.setPosition(from + charsAdded, QTextCursor::KeepAnchor);
0082 
0083         debugTextShape << "   - " << previousText;
0084         debugTextShape << "   + " << cursor.selectedText();
0085     }
0086 
0087     m_enableSignals = true;
0088     m_reverseUndo = false;
0089 */
0090 }
0091 
0092 void ChangeTracker::notifyForUndo()
0093 {
0094     m_reverseUndo = true;
0095 }