1 /* This file is part of the KDE project
2  * Copyright (C) 2006-2009 Thomas Zander <zander@kde.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 #ifndef KOTEXTRANGE_H
20 #define KOTEXTRANGE_H
21 
22 #include "kritatext_export.h"
23 
24 #include <QObject>
25 #include <KoXmlReaderForward.h>
26 
27 class QTextDocument;
28 class QTextCursor;
29 
30 class KoTextRangeManager;
31 class KoTextRangePrivate;
32 class KoShapeSavingContext;
33 class KoTextInlineRdf;
34 class KoShapeLoadingContext;
35 
36 /**
37  * Base class for all text ranges.
38  *
39  * They are essentially anchored to a specific position or range in the text
40  *
41  * @see KoTextRangeManager
42  */
43 class KRITATEXT_EXPORT KoTextRange : public QObject
44 {
45     Q_OBJECT
46 public:
47     enum TagType {StartTag = 0, EndTag = 1};
48 
49     /**
50      * constructor
51      */
52     explicit KoTextRange(const QTextCursor &cursor);
53     ~KoTextRange() override;
54 
55     /**
56      * Will be called by the manager when this variable is added.
57      * Remember that inheriting classes should not use the manager() in the constructor, since it will be 0
58      * @param manager the object manager for this object.
59      */
60     void setManager(KoTextRangeManager *manager);
61 
62     /**
63      * Return the object manager set on this inline object.
64      */
65     KoTextRangeManager *manager() const;
66 
67      /**
68      * Return the textdocument the range points to.
69      */
70    QTextDocument *document() const;
71 
72     /**
73      * Save the part of this text range corresponding to position as ODF
74      * This may save a beginning tag, ending tag, or nothing at all
75      * @param context the context for saving.
76      * @param position a position in the qtextdocument we are currently saving for.
77      * @param tagType the type of tag we are interested in
78      */
79     virtual void saveOdf(KoShapeSavingContext &context, int position, TagType tagType) const = 0;
80 
81     bool positionOnlyMode() const;
82     void setPositionOnlyMode(bool m);
83 
84     bool hasRange() const;
85     int rangeStart() const;
86     int rangeEnd() const;
87 
88     void setRangeStart(int position);
89     void setRangeEnd(int position);
90 
91     QString text() const;
92 
93     /**
94      * A text range might have some Rdf metadata associated with it
95      * in content.xml
96      * Ownership of the rdf object is taken by the text range, and you should not
97      * delete it.
98      */
99     void setInlineRdf(KoTextInlineRdf *rdf);
100 
101     /**
102      * Get any Rdf which was stored in content.xml for this text range
103      */
104     KoTextInlineRdf *inlineRdf() const;
105 
106     /**
107      * Load a variable from odf.
108      *
109      * @param element element which represents the shape in odf
110      * @param context the KoShapeLoadingContext used for loading
111      *
112      * @return false if loading failed
113      */
114     virtual bool loadOdf(const KoXmlElement &element, KoShapeLoadingContext &context) =  0;
115 
116     void snapshot();
117     void restore();
118 
119 protected:
120     KoTextRangePrivate *d_ptr;
121 
122 private:
123     Q_DECLARE_PRIVATE(KoTextRange)
124 };
125 
126 KRITATEXT_EXPORT QDebug operator<<(QDebug dbg, const KoTextRange *o);
127 
128 #endif
129