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 "kpToolTextBackspaceCommand.h"
0032 
0033 #include "layers/selections/text/kpTextSelection.h"
0034 #include "views/manager/kpViewManager.h"
0035 
0036 #include <QList>
0037 
0038 
0039 kpToolTextBackspaceCommand::kpToolTextBackspaceCommand (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_numBackspaces (0)
0045 {
0046     viewManager ()->setTextCursorPosition (m_row, m_col);
0047 
0048     if (action == AddBackspaceNow) {
0049         addBackspace ();
0050     }
0051 }
0052 
0053 kpToolTextBackspaceCommand::~kpToolTextBackspaceCommand () = default;
0054 
0055 
0056 // public
0057 void kpToolTextBackspaceCommand::addBackspace ()
0058 {
0059     QList <QString> textLines = textSelection ()->textLines ();
0060 
0061     if (m_col > 0)
0062     {
0063         m_deletedText.prepend (textLines [m_row][m_col - 1]);
0064 
0065         textLines [m_row] = textLines [m_row].left (m_col - 1) +
0066                             textLines [m_row].mid (m_col);
0067         m_col--;
0068     }
0069     else
0070     {
0071         if (m_row > 0)
0072         {
0073             int newCursorRow = m_row - 1;
0074             int newCursorCol = textLines [newCursorRow].length ();
0075 
0076             m_deletedText.prepend (QLatin1Char('\n'));
0077 
0078             textLines [newCursorRow] += textLines [m_row];
0079 
0080             textLines.erase (textLines.begin () + m_row);
0081 
0082             m_row = newCursorRow;
0083             m_col = newCursorCol;
0084         }
0085     }
0086 
0087     textSelection ()->setTextLines (textLines);
0088 
0089     viewManager ()->setTextCursorPosition (m_row, m_col);
0090 
0091     m_numBackspaces++;
0092 }
0093 
0094 
0095 // public virtual [base kpCommand]
0096 kpCommandSize::SizeType kpToolTextBackspaceCommand::size () const
0097 {
0098     return static_cast<kpCommandSize::SizeType>
0099             (static_cast<unsigned int> (m_deletedText.length ()) * sizeof (QChar));
0100 }
0101 
0102 
0103 // public virtual [base kpCommand]
0104 void kpToolTextBackspaceCommand::execute ()
0105 {
0106     viewManager ()->setTextCursorPosition (m_row, m_col);
0107 
0108     m_deletedText.clear ();
0109     int oldNumBackspaces = m_numBackspaces;
0110     m_numBackspaces = 0;
0111 
0112     for (int i = 0; i < oldNumBackspaces; i++) {
0113         addBackspace ();
0114     }
0115 }
0116 
0117 // public virtual [base kpCommand]
0118 void kpToolTextBackspaceCommand::unexecute ()
0119 {
0120     viewManager ()->setTextCursorPosition (m_row, m_col);
0121 
0122     QList <QString> textLines = textSelection ()->textLines ();
0123 
0124     for (auto && i : m_deletedText)
0125     {
0126         if (i == QLatin1Char('\n'))
0127         {
0128             const QString rightHalf = textLines [m_row].mid (m_col);
0129 
0130             textLines [m_row].truncate (m_col);
0131             textLines.insert (textLines.begin () + m_row + 1, rightHalf);
0132 
0133             m_row++;
0134             m_col = 0;
0135         }
0136         else
0137         {
0138             const QString leftHalf = textLines [m_row].left (m_col);
0139             const QString rightHalf = textLines [m_row].mid (m_col);
0140 
0141             textLines [m_row] = leftHalf + i + rightHalf;
0142             m_col++;
0143         }
0144     }
0145 
0146     m_deletedText.clear ();
0147 
0148     textSelection ()->setTextLines (textLines);
0149 
0150     viewManager ()->setTextCursorPosition (m_row, m_col);
0151 }
0152