File indexing completed on 2024-05-26 04:24:57

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    Copyright (c) 2005 Kazuki Ohta <mover@hct.zaq.ne.jp>
0005    Copyright (c) 2010 Tasuku Suzuki <stasuku@gmail.com>
0006    All rights reserved.
0007 
0008    Redistribution and use in source and binary forms, with or without
0009    modification, are permitted provided that the following conditions
0010    are met:
0011 
0012    1. Redistributions of source code must retain the above copyright
0013       notice, this list of conditions and the following disclaimer.
0014    2. Redistributions in binary form must reproduce the above copyright
0015       notice, this list of conditions and the following disclaimer in the
0016       documentation and/or other materials provided with the distribution.
0017 
0018    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0019    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0020    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0021    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0022    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0023    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0024    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0025    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0026    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0027    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0028 */
0029 
0030 #define DEBUG_KP_TOOL_TEXT 0
0031 
0032 
0033 #include "tools/selection/text/kpToolText.h"
0034 #include "kpToolTextPrivate.h"
0035 #include "commands/tools/selection/text/kpToolTextInsertCommand.h"
0036 
0037 
0038 #include "kpLogCategories.h"
0039 
0040 #include "document/kpDocument.h"
0041 #include "layers/selections/text/kpTextSelection.h"
0042 #include "views/kpView.h"
0043 #include "views/manager/kpViewManager.h"
0044 
0045 //---------------------------------------------------------------------
0046 
0047 void kpToolText::inputMethodEvent (QInputMethodEvent *e)
0048 {
0049 #if DEBUG_KP_TOOL_TEXT && 1
0050     qCDebug(kpLogTools) << "kpToolText::inputMethodEvent() preeditString='" << e->preeditString ()
0051                << "commitString = " << e->commitString ()
0052                << " replacementStart=" << e->replacementStart ()
0053                << " replacementLength=" << e->replacementLength ();
0054 #endif
0055     kpTextSelection *textSel = document ()->textSelection ();
0056     if (hasBegunDraw() || !textSel)
0057     {
0058         e->ignore();
0059         return;
0060     }
0061 
0062     kpPreeditText previous = textSel->preeditText ();
0063     kpPreeditText next (e);
0064 
0065     int textCursorRow = viewManager ()->textCursorRow ();
0066     int textCursorCol = viewManager ()->textCursorCol ();
0067     if (!next.isEmpty ())
0068     {
0069         if (previous.position().x () < 0 && previous.position().y () < 0)
0070         {
0071             next.setPosition (QPoint(textCursorCol, textCursorRow));
0072         }
0073         else
0074         {
0075             next.setPosition(previous.position ());
0076         }
0077     }
0078     textSel->setPreeditText (next);
0079     textCursorCol = textCursorCol - previous.cursorPosition () + next.cursorPosition ();
0080     viewManager ()->setTextCursorPosition (textCursorRow, textCursorCol);
0081 
0082     QString commitString = e->commitString ();
0083     if (!commitString.isEmpty ())
0084     {
0085         // commit string
0086         if (!d->insertCommand) {
0087             addNewInsertCommand (&d->insertCommand);
0088         }
0089 
0090         d->insertCommand->addText (commitString);
0091     }
0092     e->accept ();
0093 }
0094 
0095 //---------------------------------------------------------------------