1 /* This file is part of the KDE project
2    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
3                       Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
4                  2006 Martin Pfeiffer <hubipete@gmx.net>
5                  2009 Jeremias Epperlein <jeeree@web.de>
6 
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11 
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16 
17    You should have received a copy of the GNU Library General Public License
18    along with this library; see the file COPYING.LIB.  If not, write to
19    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20    Boston, MA 02110-1301, USA.
21 */
22 
23 #ifndef FORMULACURSOR_H
24 #define FORMULACURSOR_H
25 
26 #include "koformula_export.h"
27 #include <QString>
28 #include <QPair>
29 #include "FormulaData.h"
30 
31 class BasicElement;
32 class QString;
33 class QPainter;
34 class QPointF;
35 
36 enum CursorDirection {
37     MoveRight,
38     MoveLeft,
39     MoveUp,
40     MoveDown,
41     NoDirection
42 };
43 
44 
45 /**
46  * @short The cursor being moved through a formula
47  *
48  * The FormulaCursor is used to store a cursor position (or selection) in a formula as well
49  * as to manipulate it. It therefore has a selection state, a starting position
50  * and (if it is selecting) a selection end position, called mark.
51  *
52  */
53 
54 class KOFORMULA_EXPORT FormulaCursor {
55 public:
56     FormulaCursor(BasicElement* element, bool selecting, int position, int mark);
57     FormulaCursor(BasicElement* element, int position);
58     FormulaCursor();
59     FormulaCursor(const FormulaCursor& other);
60 
61     /**
62      * Draw the cursor to the given QPainter
63      * @param painter The QPainter the cursor draws itsself to
64      */
65     void paint( QPainter &painter ) const;
66 
67     /// @return whether the cursor is at the first position
68     bool isHome() const;
69 
70     /// @return whether the cursor is at the last position
71     bool isEnd() const;
72 
73     /// @return The element the FormulaCursor is currently inside
74     BasicElement* currentElement() const;
75 
76     /// @return The current position in m_currentElement
77     int position() const;
78 
79     /// set the position of the cursor in the current element
80     void setPosition(int position);
81 
82     /// set the element, in which the cursor is
83     void setCurrentElement(BasicElement* element);
84 
85     /// @return The current direction the cursor is moving in
86     CursorDirection direction() const;
87 
88     /**
89      * Make the cursor selecting
90      * @param selecting When true the cursor is selecting
91      */
92     void setSelecting( bool selecting );
93 
94     /// @return @c true when the cursor is selecting
95     bool isSelecting() const;
96 
97     /// @return @c true when the cursor is selecting
98     bool hasSelection() const;
99 
100     /// set the start position of the selection
101     void setMark(int position);
102 
103     /// @return the selection starting position
104     int mark() const;
105 
106     /// select the element completely
107     void selectElement(BasicElement* element);
108 
109     /// return the end and beginning of the current selection where the first element is the smaller one
110     QPair<int,int> selection() const;
111 
112     /// @return checks if the cursor is valid were it is
113     bool isAccepted() const;
114 
115     /// Move the cursor in the specified @p direction
116     void move( CursorDirection direction );
117 
118     void moveTo( const FormulaCursor& pos);
119 
120     void moveTo(BasicElement* element, int position);
121 
122     void moveTo(BasicElement* element);
123 
124     /// Put the cursor in @p element, as close as possible to the point where @p cursor is
125     bool moveCloseTo( BasicElement* element, FormulaCursor& cursor);
126 
127     /// Move the cursor to the first position in the current element
128     void moveHome();
129 
130     /// Move the cursor to the last position in the current element
131     void moveEnd();
132 
133     /// @return the midpoint of the current cursorLine in global coordinates
134     QPointF getCursorPosition();
135 
136     /// Set the cursor to the element at @p point
137     void setCursorTo( const QPointF& point );
138 
139     /// @return true if the cursor is inside a token element
140     bool insideToken() const;
141 
142     /// @return true if the cursor is inside a row or inferred row
143     bool insideInferredRow() const;
144 
145     /// @return true if the cursor is inside a element with fixed number of children
146     bool insideFixedElement() const;
147 
148     bool performMovement( FormulaCursor& oldcursor );
149 
150     FormulaCursor& operator+=(int step);
151 
152     int offset();
153 
154 private:
155     /// The element that is currently left to the cursor
156     BasicElement* m_currentElement;
157 
158     /// The position of the cursor in the current element
159     int m_position;
160 
161     /// The position where the current selection starts in the current element
162     int m_mark;
163 
164     /// Indicates whether the cursor is currently selecting
165     bool m_selecting;
166 
167     CursorDirection m_direction;
168 };
169 
170 #endif // FORMULACURSOR_H
171