1 /* This file is part of the KDE project
2    Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>
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 
20 #ifndef __rdf_KoRdfSemanticItem_h__
21 #define __rdf_KoRdfSemanticItem_h__
22 
23 #include "kordf_export.h"
24 
25 #include <KoRdfBasicSemanticItem.h>
26 #include <KoSemanticStylesheet.h>
27 // Qt
28 #include <QMimeData>
29 
30 class KoCanvasBase;
31 class KDateTime;
32 class QTreeWidgetItem;
33 
34 /**
35  * @short Base class for C++ objects which represent Rdf at a higher level.
36  * @author Ben Martin <ben.martin@kogmbh.com>
37  *
38  * Base class for Semantic Items (semitems). A semantic item is
39  * created from one or more Rdf triples and brings that related
40  * information together into a C++ object. For example, for contact
41  * information, many Rdf triples conforming to the FOAF specification
42  * might be present.
43  *
44  * Code can call createQTreeWidgetItem() to create an item that can be
45  * displayed to the user without needing to know about triples or Rdf.
46  *
47  * @see KoRdfSemanticTreeWidgetItem
48  * @see KoDocumentRdf
49  *
50  */
51 class KORDF_EXPORT KoRdfSemanticItem : public KoRdfBasicSemanticItem
52 {
53     Q_OBJECT
54 
55 public:
56     explicit KoRdfSemanticItem(QObject *parent);
57     KoRdfSemanticItem(QObject *parent, const KoDocumentRdf *rdf);
58     KoRdfSemanticItem(QObject *parent, const KoDocumentRdf *rdf, Soprano::QueryResultIterator &it);
59     virtual ~KoRdfSemanticItem();
60 
61     static QList<hKoRdfSemanticItem> fromList(const QList< hKoRdfBasicSemanticItem > &lst);
62 
63 protected:
64     /**
65      * The importFromData() method can use this method to finish an
66      * import. Text is also inserted into the document to show the
67      * user the new semantic object. The semanticObjectAdded signal is
68      * emitted so that dockers have a chance to update themselves to
69      * reflect the newly added SemanticItem in the document.
70      *
71      * This method uses createEditor() followed by
72      * updateFromEditorData() to actually put the Rdf triples into the
73      * store. So a subclass does not have to explicitly handle the
74      * import if it can present a GUI to edit itself. The GUI is not
75      * shown to the user.
76      *
77      */
78     virtual void importFromDataComplete(const QByteArray &ba, const KoDocumentRdf *rdf = 0, KoCanvasBase *host = 0);
79 
80     friend class KoSemanticStylesheetsEditor;
81     friend class KoSemanticStylesheet;
82     virtual void setupStylesheetReplacementMapping(QMap<QString, QString> &m);
83 
84 public:
85     /**
86      * Create a QTreeWidgetItem to display this SemanticItem. This
87      * method should be used if you want to present a QTree of
88      * SemanticItems because the returned widgetItem can also create a
89      * menu and perform other actions for the SemanticItem.
90      */
91     virtual KoRdfSemanticTreeWidgetItem *createQTreeWidgetItem(QTreeWidgetItem *parent = 0);
92 
93     /**
94      * Insert the SemanticItem into the document at the current cursor
95      * position. The appearance of each semantic item is likely to be
96      * different depending on the user's current formatting
97      * preferences. For example, a contact might show one or more of
98      * the person's names and their phone number.
99      *
100      * This method inserts markers and other book keeping and uses
101      * the default stylesheet to insert a representation of the
102      * SemanticItem.
103      */
104     virtual void insert(KoCanvasBase *host);
105 
106     /**
107      * Export the SemanticItem to MimeData. This method is used by
108      * Drag and Drop to allow the item to move to another application
109      * or possibly onto the clipboard. Subclasses might like to use a
110      * QTemporaryFile and call their exportToFile() method to export
111      * themselves to MIME data. For maximum usability a plain text
112      * representation should also be set with md->setText() so items
113      * can be dragged to text editors and consoles.
114      */
115     virtual void exportToMime(QMimeData *md) const;
116 
117     /**
118      * Export to a file in whatever format is the most useful for the
119      * semantic item. Prompt for a filename if none is given.
120      */
121     virtual void exportToFile(const QString &fileName = QString()) const = 0;
122 
123     /**
124      * Import the data in ba to the semantic item. This is used for
125      * D&D Drop events to create a new semantic item. Subclasses
126      * should set their internal state based on the data in 'ba' and
127      * then call importFromDataComplete() at the end of the method to
128      * update the Rdf and insert the semantic item into the document.
129      *
130      * This method calls also insert() which links the semanticItem with the
131      * KoDocumentRdf object m_rdf.
132      */
133     virtual void importFromData(const QByteArray &ba, const KoDocumentRdf *rdf = 0, KoCanvasBase *host = 0) = 0;
134 
135     /**
136      * A simple description of the semantic item that can be shown to the user
137      */
138     virtual QString name() const = 0;
139 
140     /**
141      * Get the system semantic stylesheets that are supported for this
142      * particular semantic item subclass.
143      */
144     virtual QList<hKoSemanticStylesheet> stylesheets() const = 0;
145 
146     /**
147      * Get the user created/editable semantic stylesheets that are
148      * supported for this particular semantic item subclass.
149      */
150     QList<hKoSemanticStylesheet> userStylesheets() const;
151 
152     /**
153      * Unambiguiously find a stylesheet by its UUID. The sheet can
154      * be either user or system as long as it has the uuid you want.
155      */
156     hKoSemanticStylesheet findStylesheetByUuid(const QString &uuid) const;
157 
158     /**
159      * Find a user/system stylesheet by name.
160      * sheetType is one of TYPE_SYSTEM/TYPE_USER.
161      * n is the name of the stylesheet you want.
162      */
163     hKoSemanticStylesheet findStylesheetByName(const QString &sheetType, const QString &n) const;
164     /**
165      * Find a user/system stylesheet by name.
166      * ssl is either stylesheets() or userStylesheets()
167      * n is the name of the stylesheet you want.
168      */
169     hKoSemanticStylesheet findStylesheetByName(const QList<hKoSemanticStylesheet> &ssl, const QString &n) const;
170 
171     /**
172      * Get the default stylesheet for this subclass of Semantic Item.
173      *
174      * If you want to know which stylesheet is in use by a particular
175      * reference to a semantic item, use KoRdfSemanticItemViewSite::stylesheet()
176      *
177      * @see KoRdfSemanticItemViewSite
178      * @see KoRdfSemanticItemViewSite::stylesheet()
179      */
180     hKoSemanticStylesheet defaultStylesheet() const;
181     /**
182      * Set the default stylesheet for this subclass of Semantic Item.
183      *
184      * If you want to set the stylesheet for a particular reference to a
185      * semantic item, use KoRdfSemanticItemViewSite::applyStylesheet().
186      * @see KoRdfSemanticItemViewSite::applyStylesheet()
187      */
188     void defaultStylesheet(hKoSemanticStylesheet ss);
189 
190     /**
191      * Create a new user stylesheet
192      */
193     hKoSemanticStylesheet createUserStylesheet(const QString &name, const QString &templateString = QString());
194 
195     /**
196      * Destroy a user stylesheet
197      */
198     void destroyUserStylesheet(hKoSemanticStylesheet ss);
199 
200     /**
201      * Load the user stylesheets from the given Rdf model. They are
202      * assumed to be in the format saved by saveUserStylesheets()
203      *
204      * @see saveUserStylesheets()
205      */
206     void loadUserStylesheets(QSharedPointer<Soprano::Model> model);
207 
208     /**
209      * Save the user stylesheets to the Rdf model given.
210      *
211      * Stylesheets are saved as an Rdf list of stylesheet bnodes. If
212      * there is already a list in the model it is removed first and
213      * then the current collection of user stylesheets is added into
214      * the Rdf model. Note that as the collection of user stylesheets
215      * exists on a per subclass basis, this method saves the list to
216      * the Rdf model using a list head that depends on which subclass
217      * you are saving the user stylesheets of. As such, you should
218      * save and load the user stylesheets for each class in
219      * classNames().
220      *
221      * @see loadUserStylesheets()
222      * @see classNames()
223      */
224     void saveUserStylesheets(QSharedPointer<Soprano::Model> model, const Soprano::Node &context) const;
225 
226 protected:
227     /**
228      * Create a new system stylesheet
229      */
230     hKoSemanticStylesheet createSystemStylesheet(const QString &uuid, const QString &name, const QString &templateString) const;
231 
232 protected Q_SLOTS:
233     /**
234      * In case the stylesheets move to using a QMap<String,sheet> or
235      * we want to know when a stylesheet has been renamed.
236      */
237     void onUserStylesheetRenamed(hKoSemanticStylesheet ss, const QString &oldName, const QString &newName);
238 };
239 
240 #endif //__rdf_KoRdfSemanticItem_h__
241