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 KOINLINETEXTOBJECTMANAGER_H
20 #define KOINLINETEXTOBJECTMANAGER_H
21 
22 #include "KoInlineObject.h"
23 #include "KoVariableManager.h"
24 #include "kotext_export.h"
25 
26 // Qt
27 #include <QHash>
28 #include <QTextBlock>
29 
30 class KoCanvasBase;
31 class KoTextLocator;
32 class KoInlineNote;
33 class KoInlineCite;
34 
35 class QTextCharFormat;
36 class QAction;
37 
38 /**
39  * A container to register all the inlineTextObjects with.
40  * Inserting an inline-object in a QTextDocument should be done via this manager which will
41  * insert a placeholder in the text and you should add the KoInlineTextObjectManager to the
42  * KoTextDocument.
43  */
44 class KOTEXT_EXPORT KoInlineTextObjectManager : public QObject
45 {
46     Q_OBJECT
47 public:
48     enum Properties {
49         InlineInstanceId = 577297549 // If you change this, don't forget to change KoCharacterStyle.h
50     };
51 
52     /// Constructor
53     explicit KoInlineTextObjectManager(QObject *parent = 0);
54     ~KoInlineTextObjectManager() override;
55 
56     /**
57      * Retrieve a formerly added inline object based on the format.
58      * @param format the textCharFormat
59      */
60     KoInlineObject *inlineTextObject(const QTextCharFormat &format) const;
61 
62     /**
63      * Retrieve a formerly added inline object based on the cursor position.
64      * @param cursor the cursor which position is used. The anchor is ignored.
65      */
66     KoInlineObject *inlineTextObject(const QTextCursor &cursor) const;
67 
68     /**
69      * Retrieve a formerly added inline object based on the KoInlineObject::id() of the object.
70      * @param id the id assigned to the inline text object when it was added.
71      */
72     KoInlineObject *inlineTextObject(int id) const;
73 
74     QList<KoInlineObject*> inlineTextObjects() const;
75 
76     /**
77      * Insert a new inline object into the manager as well as the document.
78      * This method will cause a placeholder to be inserted into the text at cursor position,
79      *  possibly replacing a selection.  The object will then be used as an inline
80      * character and painted at the specified location in the text.
81      * @param cursor the cursor which indicated the document and the position in that document
82      *      where the inline object will be inserted.
83      * @param object the inline object to insert.
84      */
85     void insertInlineObject(QTextCursor &cursor, KoInlineObject *object);
86 
87     /**
88      * Add inline object into the manager.
89      *
90      * This methods add the inline object into the manager. This is useful if you have a command
91      * that removes and adds a inline object to the manager. If the object already was inserted before
92      * (the object id is already set) it keeps the old id, otherwise a new id will be generated.
93      *
94      * @param object the inline object to insert.
95      */
96     void addInlineObject(KoInlineObject* object);
97 
98     /**
99      * Remove an inline object from this manager. The object will also be removed from
100      * the bookmarkmanager if it is a bookmark. This is not done smart: you might end up
101      * with dangling start or end bookmarks.
102      * Should really only be called by KoTextEditor's delete commands
103      * @param object the object to be removed
104      */
105     void removeInlineObject(KoInlineObject *object);
106 
107     /**
108      * Set a property that may have changed which will be forwarded to all registered textObjects.
109      * If the key has changed then all registered InlineObject instances that have stated to want
110      * updates will get called with the change.
111      * The property will be stored to allow it to be retrieved via the intProperty() and friends.
112      * @see KoInlineObject::propertyChangeListener()
113      */
114     void setProperty(KoInlineObject::Property key, const QVariant &value);
115 
116     /// retrieve a property
117     QVariant property(KoInlineObject::Property key) const;
118 
119     /// retrieve an int property
120     int intProperty(KoInlineObject::Property key) const;
121 
122     /// retrieve a bool property
123     bool boolProperty(KoInlineObject::Property key) const;
124 
125     /// retrieve a string property
126     QString stringProperty(KoInlineObject::Property key) const;
127 
128     /// remove a property from the store.
129     void removeProperty(KoInlineObject::Property key);
130 
131     /**
132      * Return the variableManager.
133      */
134     const KoVariableManager *variableManager() const;
135     /**
136      * Return the variableManager.
137      */
138     KoVariableManager *variableManager();
139 
140     /**
141      * Create a list of actions that can be used to plug into a menu, for example.
142      * This method internally uses KoInlineObjectRegistry::createInsertVariableActions() but extends
143      * the list with all registered variable-names.
144      * Each of these actions, when executed, will insert the relevant variable in the current text-position.
145      * The actions assume that the text tool is selected, if thats not the case then they will silently fail.
146      * @param host the canvas for which these actions are created.  Note that the actions will get these
147      *  actions as a parent (for memory management purposes) as well.
148      * @see KoVariableManager
149      */
150     QList<QAction*> createInsertVariableActions(KoCanvasBase *host) const;
151 
152     QList<KoTextLocator*> textLocators() const;
153 
154     /**
155       * It returns a list of all end notes in the document
156       */
157     QList<KoInlineNote*> endNotes() const;
158 
159     QMap<QString, KoInlineCite*> citations(bool duplicatesEnabled = true) const;
160 
161     QList<KoInlineCite*> citationsSortedByPosition(bool duplicatesEnabled = true,
162                                                            QTextBlock block = QTextBlock()) const;
163 
164 public Q_SLOTS:
165     void documentInformationUpdated(const QString &info, const QString &data);
166 
167 Q_SIGNALS:
168     /**
169      * Emitted whenever a property is set and it turns out to be changed.
170      */
171     void propertyChanged(int, const QVariant &variant);
172 
173 private:
174     void insertObject(KoInlineObject *object);
175 
176     QHash<int, KoInlineObject*> m_objects;
177     QHash<int, KoInlineObject*> m_deletedObjects;
178     QList<KoInlineObject*> m_listeners; // holds objects also in m_objects, but which want propertyChanges
179     int m_lastObjectId;
180     QHash<int, QVariant> m_properties;
181 
182     KoVariableManager m_variableManager;
183 };
184 
185 Q_DECLARE_METATYPE(KoInlineTextObjectManager*)
186 #endif
187