File indexing completed on 2024-05-12 16:34:02

0001 /* This file is part of the KDE project
0002    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
0003                       Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
0004                  2006 Martin Pfeiffer <hubipete@gmx.net>
0005                  2009 Jeremias Epperlein <jeeree@web.de>
0006 
0007    This library is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This library is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    Library General Public License for more details.
0016 
0017    You should have received a copy of the GNU Library General Public License
0018    along with this library; see the file COPYING.LIB.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020    Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef FORMULACURSOR_H
0024 #define FORMULACURSOR_H
0025 
0026 #include "koformula_export.h"
0027 #include <QString>
0028 #include <QPair>
0029 #include "FormulaData.h"
0030 
0031 class BasicElement;
0032 class QString;
0033 class QPainter;
0034 class QPointF;
0035 
0036 enum CursorDirection {
0037     MoveRight,
0038     MoveLeft,
0039     MoveUp,
0040     MoveDown,
0041     NoDirection
0042 };
0043 
0044 
0045 /**
0046  * @short The cursor being moved through a formula
0047  *
0048  * The FormulaCursor is used to store a cursor position (or selection) in a formula as well
0049  * as to manipulate it. It therefore has a selection state, a starting position
0050  * and (if it is selecting) a selection end position, called mark.
0051  *
0052  */
0053 
0054 class KOFORMULA_EXPORT FormulaCursor {
0055 public:
0056     FormulaCursor(BasicElement* element, bool selecting, int position, int mark);
0057     FormulaCursor(BasicElement* element, int position);
0058     FormulaCursor();
0059     FormulaCursor(const FormulaCursor& other);
0060 
0061     /**
0062      * Draw the cursor to the given QPainter
0063      * @param painter The QPainter the cursor draws itsself to
0064      */
0065     void paint( QPainter &painter ) const;
0066 
0067     /// @return whether the cursor is at the first position
0068     bool isHome() const;
0069 
0070     /// @return whether the cursor is at the last position
0071     bool isEnd() const;
0072 
0073     /// @return The element the FormulaCursor is currently inside
0074     BasicElement* currentElement() const;
0075 
0076     /// @return The current position in m_currentElement
0077     int position() const;
0078 
0079     /// set the position of the cursor in the current element
0080     void setPosition(int position);
0081 
0082     /// set the element, in which the cursor is
0083     void setCurrentElement(BasicElement* element);
0084 
0085     /// @return The current direction the cursor is moving in
0086     CursorDirection direction() const;
0087 
0088     /**
0089      * Make the cursor selecting
0090      * @param selecting When true the cursor is selecting
0091      */
0092     void setSelecting( bool selecting );
0093 
0094     /// @return @c true when the cursor is selecting
0095     bool isSelecting() const;
0096 
0097     /// @return @c true when the cursor is selecting
0098     bool hasSelection() const;
0099 
0100     /// set the start position of the selection
0101     void setMark(int position);
0102 
0103     /// @return the selection starting position
0104     int mark() const;
0105 
0106     /// select the element completely
0107     void selectElement(BasicElement* element);
0108 
0109     /// return the end and beginning of the current selection where the first element is the smaller one
0110     QPair<int,int> selection() const;
0111 
0112     /// @return checks if the cursor is valid were it is
0113     bool isAccepted() const;
0114 
0115     /// Move the cursor in the specified @p direction
0116     void move( CursorDirection direction );
0117 
0118     void moveTo( const FormulaCursor& pos);
0119 
0120     void moveTo(BasicElement* element, int position);
0121 
0122     void moveTo(BasicElement* element);
0123     
0124     /// Put the cursor in @p element, as close as possible to the point where @p cursor is
0125     bool moveCloseTo( BasicElement* element, FormulaCursor& cursor);
0126 
0127     /// Move the cursor to the first position in the current element
0128     void moveHome();
0129 
0130     /// Move the cursor to the last position in the current element
0131     void moveEnd();
0132 
0133     /// @return the midpoint of the current cursorLine in global coordinates
0134     QPointF getCursorPosition();
0135     
0136     /// Set the cursor to the element at @p point
0137     void setCursorTo( const QPointF& point );
0138 
0139     /// @return true if the cursor is inside a token element
0140     bool insideToken() const;
0141 
0142     /// @return true if the cursor is inside a row or inferred row
0143     bool insideInferredRow() const;
0144 
0145     /// @return true if the cursor is inside a element with fixed number of children
0146     bool insideFixedElement() const;
0147 
0148     bool performMovement( FormulaCursor& oldcursor );
0149 
0150     FormulaCursor& operator+=(int step);
0151 
0152     int offset();
0153 
0154 private:
0155     /// The element that is currently left to the cursor
0156     BasicElement* m_currentElement;
0157 
0158     /// The position of the cursor in the current element
0159     int m_position;
0160 
0161     /// The position where the current selection starts in the current element
0162     int m_mark;
0163 
0164     /// Indicates whether the cursor is currently selecting
0165     bool m_selecting;
0166 
0167     CursorDirection m_direction;
0168 };
0169 
0170 #endif // FORMULACURSOR_H