1 /* This file is part of the KDE project
2    Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
3    Copyright (C) 2003-2014 Jarosław Staniek <staniek@kde.org>
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public License
16    along with this library; see the file COPYING.LIB.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19 */
20 
21 #ifndef KEXIPART_H
22 #define KEXIPART_H
23 
24 #include <QObject>
25 #include <QMap>
26 
27 #include <kexiutils/InternalPropertyMap.h>
28 #include "kexipartbase.h"
29 
30 #include <KDbTristate>
31 
32 class KActionCollection;
33 class KexiWindow;
34 class KexiWindowData;
35 class KexiView;
36 class KDbQuerySchema;
37 class QAction;
38 class QKeySequence;
39 
40 namespace KexiPart
41 {
42 class Item;
43 class GUIClient;
44 class StaticPartInfo;
45 
46 /*! Official (registered) type IDs for objects like table, query, form... */
47 enum ObjectType {
48     UnknownObjectType = KDb::UnknownObjectType, //!< -1, helper
49     AnyObjectType = KDb::AnyObjectType,         //!<  0, helper
50     TableObjectType = KDb::TableObjectType,     //!<  1, like in KDb::ObjectType
51     QueryObjectType = KDb::QueryObjectType,     //!<  2, like in KDb::ObjectType
52     FormObjectType = 3,
53     ReportObjectType = 4,
54     ScriptObjectType = 5,
55     WebObjectType = 6,
56     MacroObjectType = 7,
57     LastObjectType = 7, //ALWAYS UPDATE THIS
58 
59     UserObjectType = 100 //!< external types
60 };
61 
62 //! @return Kexi Part API version: "major.minor"
63 //! @since 3.1
64 KEXICORE_EXPORT QString version();
65 
66 //! @short The main class for kexi frontend parts (plugins) like tables, queries, forms and reports
67 /*!
68   Plugins create windows (KexiWindow) for a given type of object.
69 
70   Notes for plugins implementors:  This class supports InternalPropertyMap interface,
71   so supported internal properties affecting its behaviour are:
72   - newObjectsAreDirty: True if newly created, unsaved objects are dirty. False by default.
73   - textViewModeCaption: custum i18n'd action text replacing standard "Text View" text.
74     Used in for query's "SQL View".
75   In general: a whole set of i18n'd action names, initialised on KexiPart::Part subclass ctor.
76   The names are useful because the same action can have other name for each part,
77   e.g. "New table" vs "New query" can have different forms for some languages.
78   So this is a flexible way for customizing translatable strings.
79  */
80 class KEXICORE_EXPORT Part : public PartBase
81 {
82     Q_OBJECT
83 
84 public:
85     virtual ~Part();
86 
87 //! @todo make it protected, outside world should use KexiProject
88     /*! Try to execute the part. Implementations of this \a Part
89     are able to overwrite this method to offer execution.
90     \param item The \a KexiPart::Item that should be executed.
91     \param sender The sender QObject which likes to execute this \a Part or
92     NULL if there is no sender. The KFormDesigner uses this to pass
93     the actual widget (e.g. the button that was pressed).
94     \return true if execution was successfully else false.
95                 */
96     virtual bool execute(KexiPart::Item* item, QObject* sender = 0) {
97         Q_UNUSED(item);
98         Q_UNUSED(sender);
99         return false;
100     }
101 
102 //! @todo make it protected, outside world should use KexiProject
103     /*! "Opens" an instance that the part provides, pointed by \a item in a mode \a viewMode.
104      \a viewMode is one of Kexi::ViewMode enum.
105      \a staticObjectArgs can be passed for static Kexi Parts.
106      The new widget will be a child of \a parent. */
107     KexiWindow* openInstance(QWidget* parent, KexiPart::Item *item,
108                              Kexi::ViewMode viewMode = Kexi::DataViewMode, QMap<QString, QVariant>* staticObjectArgs = 0);
109 
110 //! @todo make it protected, outside world should use KexiProject
111     /*! Removes any stored data pointed by \a item (example: table is dropped for table part).
112      From now this data is inaccesible, and \a item disappear.
113      You do not need to remove \a item, or remove object's schema stored in the database,
114      beacuse this will be done automatically by KexiProject after successful
115      call of this method. All object's data blocks are also automatically removed from database
116      (from "kexi__objectdata" table).
117      For this, a database connection associated with kexi project owned by \a win can be used.
118 
119      Database transaction is started by KexiProject before calling this method,
120      and it will be rolled back if you return false here.
121      You shouldn't use by hand transactions here.
122 
123      Default implementation just removes object from kexi__* system structures
124      at the database backend using KDbConnection::removeObject(). */
125     virtual tristate remove(KexiPart::Item *item);
126 
127     /*! Renames stored data pointed by \a item to \a newName
128      (example: table name is altered in the database).
129      For this, a database connection associated with kexi project owned by \a win can be used.
130      You do not need to change \a item, and change object's schema stored in the database,
131      beacuse this is automatically handled by KexiProject.
132 
133      Database transaction is started by KexiProject before calling this method,
134      and it will be rolled back if you return false here.
135      You shouldn't use by hand transactions here.
136 
137      Default implementation does nothing and returns true. */
138     virtual tristate rename(KexiPart::Item *item, const QString& newName);
139 
140     /*! Creates and returns a new temporary data for a window  \a window.
141      This method is called on openInstance() once per dialog.
142      Reimplement this to return KexiWindowData subclass instance.
143      Default implemention just returns empty KexiWindowData object. */
144     virtual KexiWindowData* createWindowData(KexiWindow *window) Q_REQUIRED_RESULT;
145 
146     /*! Creates a new view for mode \a viewMode, \a item and \a parent. The view will be
147      used inside \a dialog. */
148     virtual KexiView *createView(QWidget *parent, KexiWindow *window, KexiPart::Item *item,
149                                  Kexi::ViewMode viewMode = Kexi::DataViewMode,
150                                  QMap<QString, QVariant> *staticObjectArgs = nullptr) /*Q_REQUIRED_RESULT*/ = 0;
151 
152     //virtual void initTabs();
153 
154     /*! @return i18n'd instance name usable for displaying in gui as object's name,
155      e.g. "table".
156      The name is valid identifier - contains latin-1 lowercase characters only. */
157     QString instanceName() const;
158 
159     /*! @return i18n'd tooltip that can also act as descriptive name of the action.
160      Example: "Create new table". */
161     QString toolTip() const;
162 
163     /*! @return i18n'd "what's this" string. Example: "Creates new table." */
164     QString whatsThis() const;
165 
166     /*! \return part's GUI Client, so you can
167      create part-wide actions using this client. */
168     GUIClient *guiClient() const;
169 
170     /*! \return part's GUI Client, so you can
171      create instance-wide actions using this client. */
172     GUIClient *instanceGuiClient(Kexi::ViewMode mode = Kexi::AllViewModes) const;
173 
174     /*! \return action collection for mode \a viewMode. */
175     KActionCollection* actionCollectionForMode(Kexi::ViewMode viewMode) const;
176 
177     const Kexi::ObjectStatus& lastOperationStatus() const;
178 
179     /*! \return query schema currently edited in the \a view.
180      * It may be the original/saved query if user has no unsaved changes so far
181      * or a temporary unsaved query if there are unsaved modifications.
182      * The query can be used for example by data exporting routines so user can
183      * export result of a running unsaved query without prior saving it. For implementation in plugins. */
184     virtual KDbQuerySchema *currentQuery(KexiView* view);
185 
186     /*! @internal
187      Creates GUIClients for this part, attached to the main window.
188      This method is called by KexiMainWindow. */
189     void createGUIClients();
190 
191 Q_SIGNALS:
192     void newObjectRequest(KexiPart::Info *info);
193 
194 protected:
195     /*!
196      Creates new Plugin
197      @param parent parent of this plugin
198      @param instanceName i18n'd instance name written using only lowercase alphanumeric
199             characters (a..z, 0..9).
200             Use '_' character instead of spaces. First character should be a..z character.
201             If you cannot use latin characters in your language, use english word.
202             Example: "table".
203      @param toolTip i18n'd tooltip that can also act as descriptive name of the action.
204                     Example: "Create new table".
205      @param whatsThis i18n'd "what's this" string. Example: "Creates new table."
206      @param list extra arguments passed to the plugin
207     */
208     Part(QObject *parent,
209         const QString& instanceName,
210         const QString& toolTip,
211         const QString& whatsThis,
212         const QVariantList& list);
213 
214     //! Used by StaticPart
215     Part(QObject* parent, StaticPartInfo *info);
216 
217     virtual void initPartActions();
218     virtual void initInstanceActions();
219 
220     /*! Can be reimplemented if object data is extended beyond the default set of properties. This
221      is the case for table and query schema objects, where object of KDbObject subclass is returned.
222      In this case value pointed by @a ownedByWindow is set to false. Default implemenatation returns
223      owned KDbObject object (value pointed by @a ownedByWindow is set to true).
224      @a ownedByWindow is required. */
225     virtual KDbObject *loadSchemaObject(KexiWindow *window, const KDbObject &object,
226                                         Kexi::ViewMode viewMode, bool *ownedByWindow) Q_REQUIRED_RESULT;
227 
228     bool loadDataBlock(KexiWindow *window, QString *dataString, const QString& dataID = QString());
229 
230     /*! Creates shared action for action collection declared
231      for 'instance actions' of this part.
232      See KexiSharedActionHost::createSharedAction() for details.
233      Pass desired QAction subclass with \a subclassName (e.g. "KToggleAction") to have
234      that subclass allocated instead just QAction (what is the default). */
235     QAction * createSharedAction(Kexi::ViewMode mode, const QString &text,
236                                 const QString &pix_name, const QKeySequence &cut, const char *name,
237                                 const char *subclassName = 0);
238 
239     /*! Convenience version of above method - creates shared toggle action. */
240     QAction * createSharedToggleAction(Kexi::ViewMode mode, const QString &text,
241                                       const QString &pix_name, const QKeySequence &cut, const char *name);
242 
243     /*! Creates shared action for action collection declared
244      for 'part actions' of this part.
245      See KexiSharedActionHost::createSharedAction() for details.
246      Pass desired QAction subclass with \a subclassName (e.g. "KToggleAction") to have
247      that subclass allocated instead just QAction (what is the default). */
248     QAction * createSharedPartAction(const QString &text,
249                                     const QString &pix_name, const QKeySequence &cut, const char *name,
250                                     const char *subclassName = 0);
251 
252     /*! Convenience version of above method - creates shared toggle action
253      for 'part actions' of this part. */
254     QAction * createSharedPartToggleAction(const QString &text,
255                                           const QString &pix_name, const QKeySequence &cut, const char *name);
256 
257     void setActionAvailable(const char *action_name, bool avail);
258 
259 private:
260     //! Calls loadSchemaObject() (virtual), updates ownership of object data for @a window
261     //! and assigns the created data to @a window.
262     void loadAndSetSchemaObject(KexiWindow *window, const KDbObject& object,
263                                Kexi::ViewMode viewMode);
264 
265     Q_DISABLE_COPY(Part)
266 
267     class Private;
268     Private * const d;
269 
270     friend class Manager;
271     friend class ::KexiWindow;
272     friend class GUIClient;
273 };
274 
275 /*! \return full caption for item \a item and part \a part.
276  If \a part is provided, the captions will be in a form of "name : inctancetype", e.g. "Employees : Table",
277  otherwise it will be in a form of "name", e.g. "Employees". */
278 KEXICORE_EXPORT QString fullCaptionForItem(KexiPart::Item *item, KexiPart::Part *part);
279 
280 } // namespace KexiPart
281 
282 #endif
283