File indexing completed on 2024-06-16 04:10:05

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 
0034 #include <QList>
0035 
0036 #include "kpLogCategories.h"
0037 #include "commands/kpCommandHistory.h"
0038 #include "document/kpDocument.h"
0039 #include "layers/selections/text/kpTextSelection.h"
0040 #include "commands/tools/selection/text/kpToolTextBackspaceCommand.h"
0041 #include "commands/tools/selection/text/kpToolTextChangeStyleCommand.h"
0042 #include "commands/tools/selection/kpToolSelectionCreateCommand.h"
0043 #include "environments/tools/selection/kpToolSelectionEnvironment.h"
0044 #include "commands/tools/selection/text/kpToolTextDeleteCommand.h"
0045 #include "commands/tools/selection/text/kpToolTextEnterCommand.h"
0046 #include "commands/tools/selection/text/kpToolTextInsertCommand.h"
0047 #include "widgets/toolbars/options/kpToolWidgetOpaqueOrTransparent.h"
0048 #include "views/kpView.h"
0049 #include "views/manager/kpViewManager.h"
0050 
0051 // protected
0052 void kpToolText::handleUpKeyPress (QKeyEvent *e,
0053     const QList <QString> &textLines, int cursorRow, int cursorCol)
0054 {
0055 #if DEBUG_KP_TOOL_TEXT
0056     qCDebug(kpLogTools) << "\tup pressed";
0057 #endif
0058 
0059     if (hasBegunShape ()) {
0060         endShape (currentPoint (), normalizedRect ());
0061     }
0062 
0063     if (!textLines.isEmpty () && cursorRow > 0)
0064     {
0065         cursorRow--;
0066         cursorCol = qMin (cursorCol, textLines [cursorRow].length ());
0067         viewManager ()->setTextCursorPosition (cursorRow, cursorCol);
0068     }
0069 
0070     e->accept ();
0071 }
0072 
0073 // protected
0074 void kpToolText::handleDownKeyPress (QKeyEvent *e,
0075     const QList <QString> &textLines, int cursorRow, int cursorCol)
0076 {
0077 #if DEBUG_KP_TOOL_TEXT
0078     qCDebug(kpLogTools) << "\tdown pressed";
0079 #endif
0080 
0081     if (hasBegunShape ()) {
0082         endShape (currentPoint (), normalizedRect ());
0083     }
0084 
0085     if (!textLines.isEmpty () && cursorRow < textLines.size () - 1)
0086     {
0087         cursorRow++;
0088         cursorCol = qMin (cursorCol, textLines [cursorRow].length ());
0089         viewManager ()->setTextCursorPosition (cursorRow, cursorCol);
0090     }
0091 
0092     e->accept ();
0093 }
0094 
0095 // protected
0096 void kpToolText::handleLeftKeyPress (QKeyEvent *e,
0097     const QList <QString> &textLines, int cursorRow, int cursorCol)
0098 {
0099 #if DEBUG_KP_TOOL_TEXT
0100     qCDebug(kpLogTools) << "\tleft pressed";
0101 #endif
0102 
0103     if (hasBegunShape ()) {
0104         endShape (currentPoint (), normalizedRect ());
0105     }
0106 
0107     if (!textLines.isEmpty ())
0108     {
0109         if ((e->modifiers () & Qt::ControlModifier) == 0)
0110         {
0111         #if DEBUG_KP_TOOL_TEXT
0112             qCDebug(kpLogTools) << "\tmove single char";
0113         #endif
0114     
0115             MoveCursorLeft (textLines, &cursorRow, &cursorCol);
0116             viewManager ()->setTextCursorPosition (cursorRow, cursorCol);
0117         }
0118         else
0119         {
0120         #if DEBUG_KP_TOOL_TEXT
0121             qCDebug(kpLogTools) << "\tmove to start of word";
0122         #endif
0123     
0124             MoveCursorToWordStart (textLines, &cursorRow, &cursorCol);
0125             viewManager ()->setTextCursorPosition (cursorRow, cursorCol);
0126         }
0127     }
0128 
0129     e->accept ();
0130 }
0131 
0132 // protected
0133 void kpToolText::handleRightKeyPress (QKeyEvent *e,
0134     const QList <QString> &textLines, int cursorRow, int cursorCol)
0135 {
0136 #if DEBUG_KP_TOOL_TEXT
0137     qCDebug(kpLogTools) << "\tright pressed";
0138 #endif
0139 
0140     if (hasBegunShape ()) {
0141         endShape (currentPoint (), normalizedRect ());
0142     }
0143 
0144     if (!textLines.isEmpty ())
0145     {
0146         if ((e->modifiers () & Qt::ControlModifier) == 0)
0147         {
0148         #if DEBUG_KP_TOOL_TEXT
0149             qCDebug(kpLogTools) << "\tmove single char";
0150         #endif
0151     
0152             MoveCursorRight (textLines, &cursorRow, &cursorCol);
0153             viewManager ()->setTextCursorPosition (cursorRow, cursorCol);
0154         }
0155         else
0156         {
0157         #if DEBUG_KP_TOOL_TEXT
0158             qCDebug(kpLogTools) << "\tmove to start of next word";
0159         #endif
0160     
0161             MoveCursorToNextWordStart (textLines, &cursorRow, &cursorCol);
0162             viewManager ()->setTextCursorPosition (cursorRow, cursorCol);
0163         }
0164     }
0165 
0166     e->accept ();
0167 }
0168 
0169 
0170 // protected
0171 void kpToolText::handleHomeKeyPress (QKeyEvent *e,
0172     const QList <QString> &textLines, int cursorRow, int cursorCol)
0173 {
0174 #if DEBUG_KP_TOOL_TEXT
0175     qCDebug(kpLogTools) << "\thome pressed";
0176 #endif
0177 
0178     if (hasBegunShape ()) {
0179         endShape (currentPoint (), normalizedRect ());
0180     }
0181 
0182     if (!textLines.isEmpty ())
0183     {
0184         if (e->modifiers () & Qt::ControlModifier) {
0185             cursorRow = 0;
0186         }
0187     
0188         cursorCol = 0;
0189     
0190         viewManager ()->setTextCursorPosition (cursorRow, cursorCol);
0191     }
0192 
0193     e->accept ();
0194 }
0195 
0196 // protected
0197 void kpToolText::handleEndKeyPress (QKeyEvent *e,
0198     const QList <QString> &textLines, int cursorRow, int cursorCol)
0199 {
0200 #if DEBUG_KP_TOOL_TEXT
0201     qCDebug(kpLogTools) << "\tend pressed";
0202 #endif
0203 
0204     if (hasBegunShape ()) {
0205         endShape (currentPoint (), normalizedRect ());
0206     }
0207 
0208     if (!textLines.isEmpty ())
0209     {
0210         if (e->modifiers () & Qt::ControlModifier) {
0211             cursorRow = textLines.size () - 1;
0212         }
0213     
0214         cursorCol = textLines [cursorRow].length ();
0215     
0216         viewManager ()->setTextCursorPosition (cursorRow, cursorCol);
0217     }
0218 
0219     e->accept ();
0220 }