File indexing completed on 2024-12-15 04:03:26
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 "tools/selection/text/kpToolText.h" 0032 #include "kpToolTextPrivate.h" 0033 #include "kpLogCategories.h" 0034 #include "commands/kpCommandHistory.h" 0035 #include "document/kpDocument.h" 0036 #include "layers/selections/text/kpTextSelection.h" 0037 #include "commands/tools/selection/text/kpToolTextBackspaceCommand.h" 0038 #include "commands/tools/selection/text/kpToolTextChangeStyleCommand.h" 0039 #include "commands/tools/selection/kpToolSelectionCreateCommand.h" 0040 #include "environments/tools/selection/kpToolSelectionEnvironment.h" 0041 #include "commands/tools/selection/text/kpToolTextDeleteCommand.h" 0042 #include "commands/tools/selection/text/kpToolTextEnterCommand.h" 0043 #include "commands/tools/selection/text/kpToolTextInsertCommand.h" 0044 #include "widgets/toolbars/options/kpToolWidgetOpaqueOrTransparent.h" 0045 #include "views/kpView.h" 0046 #include "views/manager/kpViewManager.h" 0047 0048 #include <QList> 0049 0050 //--------------------------------------------------------------------- 0051 0052 // protected 0053 void kpToolText::handleBackspaceKeyPress (QKeyEvent *e, 0054 const QList <QString> &textLines, int cursorRow, int cursorCol) 0055 { 0056 #if DEBUG_KP_TOOL_TEXT 0057 qCDebug(kpLogTools) << "\tbackspace pressed"; 0058 #endif 0059 0060 if (!textLines.isEmpty ()) 0061 { 0062 if ((e->modifiers () & Qt::ControlModifier) == 0) 0063 { 0064 if (!d->backspaceCommand) { 0065 addNewBackspaceCommand (&d->backspaceCommand); 0066 } 0067 0068 d->backspaceCommand->addBackspace (); 0069 } 0070 else 0071 { 0072 if (!d->backspaceWordCommand) { 0073 addNewBackspaceCommand (&d->backspaceWordCommand); 0074 } 0075 0076 const int numMoves = MoveCursorToWordStart (textLines, 0077 &cursorRow, &cursorCol); 0078 0079 viewManager ()->setQueueUpdates (); 0080 { 0081 for (int i = 0; i < numMoves; i++) { 0082 d->backspaceWordCommand->addBackspace (); 0083 } 0084 } 0085 viewManager ()->restoreQueueUpdates (); 0086 0087 Q_ASSERT (cursorRow == viewManager ()->textCursorRow ()); 0088 Q_ASSERT (cursorCol == viewManager ()->textCursorCol ()); 0089 } 0090 } 0091 0092 e->accept (); 0093 } 0094 0095 //--------------------------------------------------------------------- 0096 0097 // protected 0098 void kpToolText::handleDeleteKeyPress (QKeyEvent *e, 0099 const QList <QString> & textLines, int cursorRow, int cursorCol) 0100 { 0101 #if DEBUG_KP_TOOL_TEXT 0102 qCDebug(kpLogTools) << "\tdelete pressed"; 0103 #endif 0104 0105 if (!textLines.isEmpty ()) 0106 { 0107 if ((e->modifiers () & Qt::ControlModifier) == 0) 0108 { 0109 if (!d->deleteCommand) { 0110 addNewDeleteCommand (&d->deleteCommand); 0111 } 0112 0113 d->deleteCommand->addDelete (); 0114 } 0115 else 0116 { 0117 if (!d->deleteWordCommand) { 0118 addNewDeleteCommand (&d->deleteWordCommand); 0119 } 0120 0121 // We don't want to know the cursor pos of the next word start 0122 // as delete should keep cursor in same pos. 0123 int cursorRowThrowAway = cursorRow, 0124 cursorColThrowAway = cursorCol; 0125 const int numMoves = MoveCursorToNextWordStart (textLines, 0126 &cursorRowThrowAway, &cursorColThrowAway); 0127 0128 viewManager ()->setQueueUpdates (); 0129 { 0130 for (int i = 0; i < numMoves; i++) { 0131 d->deleteWordCommand->addDelete (); 0132 } 0133 } 0134 viewManager ()->restoreQueueUpdates (); 0135 0136 // Assert unchanged as delete should keep cursor in same pos. 0137 Q_ASSERT (cursorRow == viewManager ()->textCursorRow ()); 0138 Q_ASSERT (cursorCol == viewManager ()->textCursorCol ()); 0139 } 0140 } 0141 0142 e->accept (); 0143 } 0144 0145 //--------------------------------------------------------------------- 0146 0147 // protected 0148 void kpToolText::handleEnterKeyPress (QKeyEvent *e, 0149 const QList <QString> & /*textLines*/, int /*cursorRow*/, int /*cursorCol*/) 0150 { 0151 #if DEBUG_KP_TOOL_TEXT 0152 qCDebug(kpLogTools) << "\tenter pressed"; 0153 #endif 0154 0155 // It's OK for <textLines> to be empty. 0156 0157 if (!d->enterCommand) { 0158 addNewEnterCommand (&d->enterCommand); 0159 } 0160 0161 d->enterCommand->addEnter (); 0162 0163 e->accept (); 0164 } 0165 0166 //--------------------------------------------------------------------- 0167 0168 // protected 0169 void kpToolText::handleTextTyped (QKeyEvent *e, 0170 const QList <QString> & /*textLines*/, int /*cursorRow*/, int /*cursorCol*/) 0171 { 0172 #if DEBUG_KP_TOOL_TEXT 0173 qCDebug(kpLogTools) << "\ttext=" << e->text(); 0174 #endif 0175 QString usableText; 0176 for (int i = 0; i < e->text ().length (); i++) 0177 { 0178 if ( e->text().at(i).category() != QChar::Other_Control ) 0179 usableText += e->text().at(i); 0180 } 0181 #if DEBUG_KP_TOOL_TEXT 0182 qCDebug(kpLogTools) << "\tusableText=" << usableText; 0183 #endif 0184 0185 if (usableText.isEmpty ()) 0186 { 0187 // Don't end the current shape nor accept the event -- the event 0188 // wasn't for us. 0189 return; 0190 } 0191 0192 // --- It's OK for <textLines> to be empty. --- 0193 0194 if (!d->insertCommand) { 0195 addNewInsertCommand (&d->insertCommand); 0196 } 0197 0198 d->insertCommand->addText (usableText); 0199 0200 e->accept (); 0201 }