1 
2 /*
3    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4    All rights reserved.
5 
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9 
10    1. Redistributions of source code must retain the above copyright
11       notice, this list of conditions and the following disclaimer.
12    2. Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in the
14       documentation and/or other materials provided with the distribution.
15 
16    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #define DEBUG_KP_TOOL_TEXT 0
29 
30 
31 #include "tools/selection/text/kpToolText.h"
32 #include "kpToolTextPrivate.h"
33 
34 #include "kpLogCategories.h"
35 
36 #include "document/kpDocument.h"
37 #include "layers/selections/text/kpTextSelection.h"
38 #include "views/kpView.h"
39 #include "views/manager/kpViewManager.h"
40 
41 
42 // private
onSelectionToSelectText() const43 bool kpToolText::onSelectionToSelectText () const
44 {
45     kpView *v = viewManager ()->viewUnderCursor ();
46     if (!v) {
47         return false;
48     }
49 
50     return v->mouseOnSelectionToSelectText (currentViewPoint ());
51 }
52 
53 
54 // private
haventBegunDrawUserMessageSelectText() const55 QString kpToolText::haventBegunDrawUserMessageSelectText () const
56 {
57     return i18n ("Left click to change cursor position.");
58 }
59 
60 // private
setCursorSelectText()61 void kpToolText::setCursorSelectText ()
62 {
63     viewManager ()->setCursor (Qt::IBeamCursor);
64 }
65 
66 
67 // private
beginDrawSelectText()68 void kpToolText::beginDrawSelectText ()
69 {
70 #if DEBUG_KP_TOOL_TEXT
71     qCDebug(kpLogTools) << "\t\tis select cursor pos";
72 #endif
73     kpTextSelection *textSel = document ()->textSelection ();
74     Q_ASSERT (textSel);
75 
76     int newRow, newCol;
77 
78     if (textSel->hasContent ())
79     {
80         newRow = textSel->closestTextRowForPoint (currentPoint ());
81         newCol = textSel->closestTextColForPoint (currentPoint ());
82     }
83     else
84     {
85         newRow = newCol = 0;
86     }
87 
88 #if DEBUG_KP_TOOL_TEXT
89     qCDebug(kpLogTools) << "\t\t\told: row=" << viewManager ()->textCursorRow ()
90               << "col=" << viewManager ()->textCursorCol ();
91     qCDebug(kpLogTools) << "\t\t\tnew: row=" << newRow << "col=" << newCol;
92 #endif
93     viewManager ()->setTextCursorPosition (newRow, newCol);
94 }
95 
96 
97 // protected virtual
selectTextOperation(Operation op,const QVariant & data1,const QVariant & data2)98 QVariant kpToolText::selectTextOperation (Operation op,
99         const QVariant &data1, const QVariant &data2)
100 {
101     (void) data1;
102     (void) data2;
103 
104 
105     switch (op)
106     {
107     case HaventBegunDrawUserMessage:
108         return haventBegunDrawUserMessageSelectText ();
109 
110     case SetCursor:
111         setCursorSelectText ();
112         break;
113 
114     case BeginDraw:
115         beginDrawSelectText ();
116         break;
117 
118     case Draw:
119         // Do nothing.
120         break;
121 
122     case Cancel:
123         // Not called.  REFACTOR: Change this?
124         break;
125 
126     case EndDraw:
127         // Do nothing.
128         break;
129 
130     default:
131         Q_ASSERT (!"Unhandled operation");
132         break;
133     }
134 
135 
136     return {};
137 }
138