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 
0034 #include <QEvent>
0035 #include <QList>
0036 
0037 #include "kpLogCategories.h"
0038 #include "commands/kpCommandHistory.h"
0039 #include "document/kpDocument.h"
0040 #include "layers/selections/text/kpTextSelection.h"
0041 #include "commands/tools/selection/text/kpToolTextBackspaceCommand.h"
0042 #include "commands/tools/selection/text/kpToolTextChangeStyleCommand.h"
0043 #include "commands/tools/selection/kpToolSelectionCreateCommand.h"
0044 #include "environments/tools/selection/kpToolSelectionEnvironment.h"
0045 #include "commands/tools/selection/text/kpToolTextDeleteCommand.h"
0046 #include "commands/tools/selection/text/kpToolTextEnterCommand.h"
0047 #include "commands/tools/selection/text/kpToolTextInsertCommand.h"
0048 #include "widgets/toolbars/options/kpToolWidgetOpaqueOrTransparent.h"
0049 #include "views/kpView.h"
0050 #include "views/manager/kpViewManager.h"
0051 
0052 #include <KLocalizedString>
0053 
0054 // protected virtual [base kpTool]
0055 bool kpToolText::viewEvent (QEvent *e)
0056 {
0057     const bool isShortcutOverrideEvent =
0058         (e->type () == QEvent::ShortcutOverride);
0059     const bool haveTextSelection = document ()->textSelection ();
0060 
0061 #if DEBUG_KP_TOOL_TEXT && 0
0062     qCDebug(kpLogTools) << "kpToolText::viewEvent() type=" << e->type ()
0063               << " isShortcutOverrideEvent=" << isShortcutOverrideEvent
0064               << " haveTextSel=" << haveTextSelection;
0065 #endif
0066 
0067     if (!isShortcutOverrideEvent || !haveTextSelection) {
0068         return kpAbstractSelectionTool::viewEvent (e);
0069     }
0070 
0071     auto *ke = dynamic_cast <QKeyEvent *> (e);
0072 #if DEBUG_KP_TOOL_TEXT
0073     qCDebug(kpLogTools) << "kpToolText::viewEvent() key=" << ke->key ()
0074               << " modifiers=" << ke->modifiers ()
0075               << " QChar.isPrint()=" << QChar (ke->key ()).isPrint ();
0076 #endif
0077 
0078     // Can't be shortcut?
0079     if (ke->key () == 0 && ke->key () == Qt::Key_unknown)
0080     {
0081     #if DEBUG_KP_TOOL_TEXT
0082         qCDebug(kpLogTools) << "\tcan't be shortcut - safe to not react";
0083     #endif
0084     }
0085     // Normal letter (w/ or w/o shift, keypad button ok)?
0086     // TODO: don't like this check
0087     else if ((ke->modifiers () &
0088                 (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)) == 0 &&
0089              !( ke->text ().isEmpty ()))
0090     {
0091     #if DEBUG_KP_TOOL_TEXT
0092         qCDebug(kpLogTools) << "\tis text - grab";
0093     #endif
0094         e->accept ();
0095     }
0096     else
0097     {
0098         // Strictly speaking, we should grab stuff like the arrow keys
0099         // and enter.  In any case, should be done down in kpTool (as that
0100         // uses arrow keys too).
0101     }
0102 
0103     return kpAbstractSelectionTool::event (e);
0104 }
0105 
0106 
0107 // protected virtual [base kpAbstractSelectionTool]
0108 void kpToolText::keyPressEvent (QKeyEvent *e)
0109 {
0110 #if DEBUG_KP_TOOL_TEXT
0111     qCDebug(kpLogTools) << "kpToolText::keyPressEvent(e->text='" << e->text () << "')";
0112 #endif
0113 
0114 
0115     e->ignore ();
0116 
0117 
0118     if (hasBegunDraw ())
0119     {
0120     #if DEBUG_KP_TOOL_TEXT
0121         qCDebug(kpLogTools) << "\talready began draw with mouse - passing on event to kpTool";
0122     #endif
0123         kpAbstractSelectionTool::keyPressEvent (e);
0124         return;
0125     }
0126 
0127 
0128     kpTextSelection * const textSel = document ()->textSelection ();
0129 
0130     if (!textSel)
0131     {
0132     #if DEBUG_KP_TOOL_TEXT
0133         qCDebug(kpLogTools) << "\tno text sel - passing on event to kpTool";
0134     #endif
0135         //if (hasBegunShape ())
0136         //    endShape (currentPoint (), normalizedRect ());
0137 
0138         kpAbstractSelectionTool::keyPressEvent (e);
0139         return;
0140     }
0141 
0142 
0143     // (All handle.+()'s require this info)
0144     const QList <QString> textLines = textSel->textLines ();
0145     const int cursorRow = viewManager ()->textCursorRow ();
0146     const int cursorCol = viewManager ()->textCursorCol ();
0147 
0148 
0149     // TODO: KTextEdit::keyPressEvent() uses KStandardShortcut instead of hardcoding; same fix for kpTool?
0150     switch (e->key ())
0151     {
0152     case Qt::Key_Up:
0153         handleUpKeyPress (e, textLines, cursorRow, cursorCol);
0154         break;
0155 
0156     case Qt::Key_Down:
0157         handleDownKeyPress (e, textLines, cursorRow, cursorCol);
0158         break;
0159 
0160     case Qt::Key_Left:
0161         handleLeftKeyPress (e, textLines, cursorRow, cursorCol);
0162         break;
0163 
0164     case Qt::Key_Right:
0165         handleRightKeyPress (e, textLines, cursorRow, cursorCol);
0166         break;
0167 
0168 
0169     case Qt::Key_Home:
0170         handleHomeKeyPress (e, textLines, cursorRow, cursorCol);
0171         break;
0172 
0173     case Qt::Key_End:
0174         handleEndKeyPress (e, textLines, cursorRow, cursorCol);
0175         break;
0176 
0177 
0178     case Qt::Key_Backspace:
0179         handleBackspaceKeyPress (e, textLines, cursorRow, cursorCol);
0180         break;
0181 
0182     case Qt::Key_Delete:
0183         handleDeleteKeyPress (e, textLines, cursorRow, cursorCol);
0184         break;
0185 
0186 
0187     case Qt::Key_Enter:
0188     case Qt::Key_Return:
0189         handleEnterKeyPress (e, textLines, cursorRow, cursorCol);
0190         break;
0191 
0192 
0193     default:
0194         handleTextTyped (e, textLines, cursorRow, cursorCol);
0195         break;
0196     }
0197 
0198 
0199     if (!e->isAccepted ())
0200     {
0201     #if DEBUG_KP_TOOL_TEXT
0202         qCDebug(kpLogTools) << "\tkey processing did not accept (text was '"
0203                    << e->text ()
0204                    << "') - passing on event to kpAbstractSelectionTool";
0205     #endif
0206         //if (hasBegunShape ())
0207         //    endShape (currentPoint (), normalizedRect ());
0208 
0209         kpAbstractSelectionTool::keyPressEvent (e);
0210         return;
0211     }
0212 }