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 0029 #define DEBUG_KP_TOOL_SELECTION 0 0030 0031 0032 #include "kpToolTextGiveContentCommand.h" 0033 0034 #include "environments/commands/kpCommandEnvironment.h" 0035 #include "document/kpDocument.h" 0036 #include "layers/selections/text/kpTextSelection.h" 0037 #include "views/manager/kpViewManager.h" 0038 #include "kpLogCategories.h" 0039 0040 0041 kpToolTextGiveContentCommand::kpToolTextGiveContentCommand ( 0042 const kpTextSelection &originalSelBorder, 0043 const QString &name, 0044 kpCommandEnvironment *environ) 0045 : kpAbstractSelectionContentCommand (originalSelBorder, name, environ) 0046 { 0047 #if DEBUG_KP_TOOL_SELECTION && 1 0048 qCDebug(kpLogCommands) << "kpToolTextGiveContentCommand::<ctor>() environ=" 0049 << environ 0050 #endif 0051 } 0052 0053 kpToolTextGiveContentCommand::~kpToolTextGiveContentCommand () = default; 0054 0055 0056 // public virtual [base kpCommand] 0057 void kpToolTextGiveContentCommand::execute () 0058 { 0059 #if DEBUG_KP_TOOL_SELECTION && 1 0060 qCDebug(kpLogCommands) << "kpToolTextGiveContentCommand::execute()"; 0061 #endif 0062 0063 kpDocument *doc = document (); 0064 Q_ASSERT (doc); 0065 0066 kpViewManager *vm = viewManager (); 0067 Q_ASSERT (vm); 0068 0069 // See API Doc for kpViewManager::textCursorRow() & textCursorCol(). 0070 Q_ASSERT (vm->textCursorRow () == 0 && vm->textCursorCol () == 0); 0071 0072 vm->setQueueUpdates (); 0073 { 0074 // 0075 // Recreate border 0076 // 0077 0078 // The previously executed command is required to have been a 0079 // kpToolSelectionCreateCommand, which must have been given a text 0080 // selection with no content. 0081 // 0082 // However, there is a tricky case. Suppose we are called for the first 0083 // time, where the above precondition holds. We would add content 0084 // to the selection as expected. But the user then undoes (CTRL+Z) the 0085 // operation, calling unexecute(). There is now no content again. 0086 // Since selection is only a border, the user can freely deselect it 0087 // and/or select another region without changing the command history 0088 // or document modified state. Therefore, if they now call us again 0089 // by redoing (CTRL+Shift+Z), there is potentially no selection at all 0090 // or it is at an arbitrary location. 0091 // 0092 // This assertion covers all 3 possibilities: 0093 // 0094 // 1. First call: text selection with no content 0095 // 2. Later calls: 0096 // a) no text selection (due to deselection) 0097 // b) text selection with no content, at an arbitrary location 0098 Q_ASSERT (!textSelection () || !textSelection ()->hasContent ()); 0099 0100 const auto *originalTextSel = dynamic_cast <const kpTextSelection *> 0101 (originalSelection ()); 0102 0103 if (originalTextSel->textStyle () != environ ()->textStyle ()) { 0104 environ ()->setTextStyle (originalTextSel->textStyle ()); 0105 } 0106 0107 doc->setSelection (*originalSelection ()); 0108 0109 0110 // 0111 // Add Content 0112 // 0113 0114 QList <QString> listOfOneEmptyString; 0115 listOfOneEmptyString.append (QString ()); 0116 textSelection ()->setTextLines (listOfOneEmptyString); 0117 } 0118 vm->restoreQueueUpdates (); 0119 0120 // This should not have changed from the start of the method. 0121 Q_ASSERT (vm->textCursorRow () == 0 && vm->textCursorCol () == 0); 0122 } 0123 0124 // public virtual [base kpCommand] 0125 void kpToolTextGiveContentCommand::unexecute () 0126 { 0127 #if DEBUG_KP_TOOL_SELECTION && 1 0128 qCDebug(kpLogCommands) << "kpToolTextGiveContentCommand::unexecute()"; 0129 #endif 0130 0131 kpDocument *doc = document (); 0132 Q_ASSERT (doc); 0133 // Must have selection text content. 0134 Q_ASSERT (doc->textSelection () && doc->textSelection ()->hasContent ()); 0135 0136 kpViewManager *vm = viewManager (); 0137 Q_ASSERT (vm); 0138 // All the commands after us have been unexecuted, so we must be back 0139 // to the state we were after our execute(). 0140 Q_ASSERT (vm->textCursorRow () == 0 && vm->textCursorCol () == 0); 0141 0142 // We can have faith that this is the state of the selection after 0143 // execute(), rather than after the user tried to throw us off by 0144 // simply selecting another region as to do that, a destroy command 0145 // must have been used. 0146 doc->textSelection ()->deleteContent (); 0147 0148 // This should not have changed from the start of the method. 0149 Q_ASSERT (vm->textCursorRow () == 0 && vm->textCursorCol () == 0); 0150 } 0151