File indexing completed on 2024-12-22 04:07:23
0001 0002 /* 0003 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org> 0004 All rights reserved. 0005 0006 Redistribution and use in source and binary forms, with or without 0007 modification, are permitted provided that the following conditions 0008 are met: 0009 0010 1. Redistributions of source code must retain the above copyright 0011 notice, this list of conditions and the following disclaimer. 0012 2. Redistributions in binary form must reproduce the above copyright 0013 notice, this list of conditions and the following disclaimer in the 0014 documentation and/or other materials provided with the distribution. 0015 0016 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 0017 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 0018 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 0019 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 0020 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 0021 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0022 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0023 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 0025 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0026 */ 0027 0028 #define DEBUG_KP_TOOL_TEXT 0 0029 0030 0031 #include "kpToolTextDeleteCommand.h" 0032 0033 #include "layers/selections/text/kpTextSelection.h" 0034 #include "views/manager/kpViewManager.h" 0035 0036 #include <QList> 0037 0038 0039 kpToolTextDeleteCommand::kpToolTextDeleteCommand (const QString &name, 0040 int row, int col, Action action, 0041 kpCommandEnvironment *environ) 0042 : kpNamedCommand (name, environ), 0043 m_row (row), m_col (col), 0044 m_numDeletes (0) 0045 { 0046 viewManager ()->setTextCursorPosition (m_row, m_col); 0047 0048 if (action == AddDeleteNow) { 0049 addDelete (); 0050 } 0051 } 0052 0053 kpToolTextDeleteCommand::~kpToolTextDeleteCommand () = default; 0054 0055 0056 // public 0057 void kpToolTextDeleteCommand::addDelete () 0058 { 0059 QList <QString> textLines = textSelection ()->textLines (); 0060 0061 if (m_col < static_cast<int> (textLines [m_row].length ())) 0062 { 0063 m_deletedText.prepend (textLines [m_row][m_col]); 0064 0065 textLines [m_row] = textLines [m_row].left (m_col) + 0066 textLines [m_row].mid (m_col + 1); 0067 } 0068 else 0069 { 0070 if (m_row < static_cast<int> (textLines.size () - 1)) 0071 { 0072 m_deletedText.prepend (QLatin1Char('\n')); 0073 0074 textLines [m_row] += textLines [m_row + 1]; 0075 textLines.erase (textLines.begin () + m_row + 1); 0076 } 0077 } 0078 0079 textSelection ()->setTextLines (textLines); 0080 0081 viewManager ()->setTextCursorPosition (m_row, m_col); 0082 0083 m_numDeletes++; 0084 } 0085 0086 0087 // public virtual [base kpCommand] 0088 kpCommandSize::SizeType kpToolTextDeleteCommand::size () const 0089 { 0090 return static_cast<kpCommandSize::SizeType> 0091 (static_cast<unsigned int> (m_deletedText.length ()) * sizeof (QChar)); 0092 } 0093 0094 0095 // public virtual [base kpCommand] 0096 void kpToolTextDeleteCommand::execute () 0097 { 0098 viewManager ()->setTextCursorPosition (m_row, m_col); 0099 0100 m_deletedText.clear (); 0101 int oldNumDeletes = m_numDeletes; 0102 m_numDeletes = 0; 0103 0104 for (int i = 0; i < oldNumDeletes; i++) { 0105 addDelete (); 0106 } 0107 } 0108 0109 // public virtual [base kpCommand] 0110 void kpToolTextDeleteCommand::unexecute () 0111 { 0112 viewManager ()->setTextCursorPosition (m_row, m_col); 0113 0114 QList <QString> textLines = textSelection ()->textLines (); 0115 0116 for (auto && i : m_deletedText) 0117 { 0118 if (i == QLatin1Char('\n')) 0119 { 0120 const QString rightHalf = textLines [m_row].mid (m_col); 0121 0122 textLines [m_row].truncate (m_col); 0123 textLines.insert (textLines.begin () + m_row + 1, rightHalf); 0124 } 0125 else 0126 { 0127 const QString leftHalf = textLines [m_row].left (m_col); 0128 const QString rightHalf = textLines [m_row].mid (m_col); 0129 0130 textLines [m_row] = leftHalf + i + rightHalf; 0131 } 0132 } 0133 0134 m_deletedText.clear (); 0135 0136 textSelection ()->setTextLines (textLines); 0137 0138 viewManager ()->setTextCursorPosition (m_row, m_col); 0139 } 0140