File indexing completed on 2024-05-12 16:33:20

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2011 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "ReplaceTextRangeCommand.h"
0021 #include "ArtisticTextShape.h"
0022 #include <klocalizedstring.h>
0023 
0024 ReplaceTextRangeCommand::ReplaceTextRangeCommand(ArtisticTextShape *shape, const QString &text, int from, int count, ArtisticTextTool *tool, KUndo2Command *parent)
0025     : KUndo2Command(parent), m_tool(tool), m_shape(shape), m_from(from), m_count(count)
0026 {
0027     setText( kundo2_i18n("Replace text range") );
0028     m_newFormattedText.append(ArtisticTextRange(text, shape->fontAt(m_from)));
0029     m_oldFormattedText = shape->text();
0030 }
0031 
0032 ReplaceTextRangeCommand::ReplaceTextRangeCommand(ArtisticTextShape *shape, const ArtisticTextRange &text, int from, int count, ArtisticTextTool *tool, KUndo2Command *parent)
0033     : KUndo2Command(parent), m_tool(tool), m_shape(shape), m_from(from), m_count(count)
0034 {
0035     setText( kundo2_i18n("Replace text range") );
0036     m_newFormattedText.append(text);
0037     m_oldFormattedText = shape->text();
0038 }
0039 
0040 ReplaceTextRangeCommand::ReplaceTextRangeCommand(ArtisticTextShape *shape, const QList<ArtisticTextRange> &text, int from, int count, ArtisticTextTool *tool, KUndo2Command *parent)
0041     : KUndo2Command(parent), m_tool(tool), m_shape(shape), m_from(from), m_count(count)
0042 {
0043     setText( kundo2_i18n("Replace text range") );
0044     m_newFormattedText = text;
0045     m_oldFormattedText = shape->text();
0046 }
0047 
0048 void ReplaceTextRangeCommand::redo()
0049 {
0050     KUndo2Command::redo();
0051 
0052     if ( !m_shape )
0053         return;
0054 
0055     m_shape->replaceText(m_from, m_count, m_newFormattedText);
0056 
0057     if (m_tool) {
0058         int length = 0;
0059         foreach(const ArtisticTextRange &range, m_newFormattedText) {
0060             length += range.text().length();
0061         }
0062         m_tool->setTextCursor(m_shape, m_from + length);
0063     }
0064 }
0065 
0066 void ReplaceTextRangeCommand::undo()
0067 {
0068     KUndo2Command::undo();
0069 
0070     if ( ! m_shape )
0071         return;
0072 
0073     m_shape->clear();
0074     foreach(const ArtisticTextRange &range, m_oldFormattedText) {
0075         m_shape->appendText(range);
0076     }
0077     if (m_tool) {
0078         m_tool->setTextCursor(m_shape, m_from);
0079     }
0080 }