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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007,2011 Jan Hambrecht <jaham@gmx.net>
0003  * Copyright (C) 2008 Rob Buis <buis@kde.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 "AddTextRangeCommand.h"
0022 #include "ArtisticTextShape.h"
0023 #include <klocalizedstring.h>
0024 
0025 AddTextRangeCommand::AddTextRangeCommand(ArtisticTextTool *tool, ArtisticTextShape *shape, const QString &text, int from)
0026     : m_tool(tool), m_shape(shape), m_plainText(text), m_formattedText(QString(), QFont()), m_from(from)
0027 {
0028     setText( kundo2_i18n("Add text range") );
0029     m_oldFormattedText = shape->text();
0030 }
0031 
0032 AddTextRangeCommand::AddTextRangeCommand(ArtisticTextTool *tool, ArtisticTextShape *shape, const ArtisticTextRange &text, int from)
0033     : m_tool(tool), m_shape(shape), m_formattedText(text), m_from(from)
0034 {
0035     setText( kundo2_i18n("Add text range") );
0036     m_oldFormattedText = shape->text();
0037 }
0038 
0039 void AddTextRangeCommand::redo()
0040 {
0041     KUndo2Command::redo();
0042 
0043     if ( !m_shape )
0044         return;
0045 
0046     if (m_plainText.isEmpty())
0047         m_shape->insertText(m_from, m_formattedText);
0048     else
0049         m_shape->insertText(m_from, m_plainText);
0050 
0051     if (m_tool) {
0052         if (m_plainText.isEmpty())
0053             m_tool->setTextCursor(m_shape, m_from + m_formattedText.text().length());
0054         else
0055             m_tool->setTextCursor(m_shape, m_from + m_plainText.length());
0056     }
0057 }
0058 
0059 void AddTextRangeCommand::undo()
0060 {
0061     KUndo2Command::undo();
0062 
0063     if ( ! m_shape )
0064         return;
0065 
0066     m_shape->clear();
0067     foreach(const ArtisticTextRange &range, m_oldFormattedText) {
0068         m_shape->appendText(range);
0069     }
0070     if (m_tool) {
0071         m_tool->setTextCursor(m_shape, m_from);
0072     }
0073 }