File indexing completed on 2024-05-12 16:39:52

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003 Lucijan Busch <lucijan@gmx.at>
0003    Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
0004    Copyright (C) 2004-2015 Jarosław Staniek <staniek@kde.org>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #ifndef KFORMDESIGNERWIDGETLIBRARY_H
0023 #define KFORMDESIGNERWIDGETLIBRARY_H
0024 
0025 #include <QObject>
0026 #include <QList>
0027 
0028 #include <KDbResult>
0029 
0030 #include "widgetfactory.h"
0031 
0032 template<class type> class QList;
0033 template<class type> class QVector;
0034 class KActionCollection;
0035 class QAction;
0036 class QMenu;
0037 class QWidget;
0038 class QVariant;
0039 class QDomDocument;
0040 class QDomElement;
0041 class KexiFormWidgetsPluginMetaData;
0042 
0043 namespace KFormDesigner
0044 {
0045 
0046 class Container;
0047 class ObjectTreeItem;
0048 class ActionGroup;
0049 class WidgetInfo;
0050 
0051 typedef QList<QAction *> ActionList;
0052 
0053 /**
0054  * A manager class for searching and loading form widget factories
0055  *
0056  * It also provides actions for widget creation.
0057  * Every widget can be located using this library.
0058  * You call WidgetLibrary functions instead of calling directly factories.
0059  * See WidgetFactory for a description of the functions.
0060  */
0061 class KFORMDESIGNER_EXPORT WidgetLibrary : public QObject, public KDbResultable
0062 {
0063     Q_OBJECT
0064 
0065 public:
0066     /*! Constructs WidgetLibrary object.
0067      In \a supportedFactoryGroups you can provide
0068      factory group list to be supported. Factory groups are defined by
0069      "X-Kexi-FormWidgetsFactoryGroup" field in every factory serviece's .desktop file.
0070      By default (when supportedFactoryGroups is empty) only factories having empty
0071      "X-Kexi-FormWidgetsFactoryGroup" field will be loaded.
0072      Factory group names are case-insensitive. */
0073     explicit WidgetLibrary(QObject *parent = 0,
0074                            const QStringList& supportedFactoryGroups = QStringList());
0075 
0076     virtual ~WidgetLibrary();
0077 
0078     /**
0079      * creates actions for widget creating
0080      */
0081     void createWidgetActions(ActionGroup *group);
0082 
0083     void addCustomWidgetActions(KActionCollection *col);
0084 
0085     /**
0086      * searches the right factory and creates a widget.
0087      * \return the widget or 0 if something falid
0088      */
0089     Q_REQUIRED_RESULT QWidget *createWidget(
0090                           const QByteArray &classname, QWidget *parent, const char *name,
0091                           Container *c,
0092                           WidgetFactory::CreateWidgetOptions options = WidgetFactory::DefaultOptions);
0093 
0094     bool createMenuActions(const QByteArray &c, QWidget *w, QMenu *menu,
0095                            KFormDesigner::Container *container);
0096 
0097     /**
0098      * Shows orientation selection popup.
0099      * \return one of the following values:
0100      * - WidgetFactory::AnyOrientation (means no selection has been made, i.e. it was cancelled)
0101      * - WidgetFactory::HorizontalOrientation
0102      * - WidgetFactory::VerticalOrientation
0103      */
0104     WidgetFactory::CreateWidgetOption showOrientationSelectionPopup(
0105         const QByteArray &classname, QWidget* parent, const QPoint& pos);
0106 
0107     QVariant internalProperty(const QByteArray& classname, const QByteArray& property);
0108 
0109     QString displayName(const QByteArray &classname);
0110     QString namePrefix(const QByteArray &classname);
0111     QString textForWidgetName(const QByteArray &name, const QByteArray &className);
0112 
0113     /*! Checks if the \a classname is an alternate classname,
0114      and returns the good classname.
0115      If \a classname is not alternate, \a classname is returned. */
0116     QByteArray classNameForAlternate(const QByteArray &classname);
0117     QString iconName(const QByteArray &classname);
0118     QString includeFileName(const QByteArray &classname);
0119     QString savingName(const QByteArray &classname);
0120 
0121     bool startInlineEditing(const QByteArray &classname, QWidget *w, Container *container);
0122     bool previewWidget(const QByteArray &classname, QWidget *widget, Container *container);
0123     bool clearWidgetContent(const QByteArray &classname, QWidget *w);
0124 
0125     bool saveSpecialProperty(const QByteArray &classname, const QString &name,
0126                              const QVariant &value, QWidget *w, QDomElement &parentNode, QDomDocument &parent);
0127     bool readSpecialProperty(const QByteArray &classname, QDomElement &node, QWidget *w,
0128                              ObjectTreeItem *item);
0129     bool isPropertyVisible(const QByteArray &classname, QWidget *w,
0130                            const QByteArray &property, bool multiple = false, bool isTopLevel = false);
0131 
0132     /*! @return list of the properties that should automatically be saved
0133     for a widget of @a classname class.
0134     Examples are: custom properties "text" for label or button, "contents" for combobox... */
0135     QList<QByteArray> autoSaveProperties(const QByteArray &classname);
0136 
0137     WidgetInfo* widgetInfoForClassName(const char* classname);
0138 
0139     WidgetFactory* factoryForClassName(const char* className);
0140 
0141     WidgetFactory* factory(const char* factoryName) const;
0142 
0143     /*! \return true if advanced properties such as "mouseTracking" should
0144      be visible to the user. True by default but Kexi sets it to false.
0145      See WidgetLibrary::Private() for a complete list of advanced properties (regardless of class)
0146      @see WidgetFactory::setAdvancedPropertiesVisible() WidgetLibrary::isPropertyVisible() */
0147     bool advancedPropertiesVisible() const;
0148 
0149     /*! Sets advanced properties to be visible or not. */
0150     void setAdvancedPropertiesVisible(bool set);
0151 
0152     /*! \return The i18n'ed name of the property \a propertyName
0153      for a class described by \a winfo. The name can be displayed in
0154      PropertyEditor. The name is retrieved from class' widget library.
0155      If this library doesn't define description for such property,
0156      and there is a parent library for \a winfo defined, parent library
0157      is asked for returning description string.
0158      Eventually, if even this failed, empty string is returned.
0159      @see WidgetFactory::propertyDescForName() */
0160     QString propertyDescForName(WidgetInfo *winfo, const QByteArray& propertyName);
0161 
0162     /*! \return The i18n'ed name of the property's value whose name is \a name.
0163      Works in the same way as propertyDescForName(): if actual library
0164      does not define a description we are looking for, parent factory is asked
0165      to return such description.
0166      Eventually, if even this failed, empty string is returned.
0167      @see WidgetFactory::propertyDescForValue() */
0168     QString propertyDescForValue(WidgetInfo *winfo, const QByteArray& name);
0169 
0170     /*! Used by Form::createPropertiesForWidget() after creating properties. */
0171     void setPropertyOptions(KPropertySet& set, const WidgetInfo& winfo, QWidget* w);
0172 
0173     /*! \return true if property sets should be reloaded for \a property property,
0174      \a classname class and widget \a w when a given property value changed. */
0175     bool propertySetShouldBeReloadedAfterPropertyChange(const QByteArray& classname, QWidget *w,
0176             const QByteArray& property);
0177 
0178     //! @return selectable item for @a item item.
0179     //! In most cases it is equal to @a item.
0180     //! See WidgetFactory::selectableItem(ObjectTreeItem*).
0181     ObjectTreeItem* selectableItem(ObjectTreeItem* item);
0182 
0183 Q_SIGNALS:
0184     void widgetActionToggled(const QByteArray& _class);
0185 
0186     //! Received by KexiFormPart::slotWidgetCreatedByFormsLibrary() so we can add drag/drop
0187     //! connection for the new widget
0188     void widgetCreated(QWidget *widget);
0189 
0190 protected:
0191     /**
0192      * Adds a factory to the library, creates actions for widgets in the added factory.
0193      * This function is not called directly but by the factory locater.
0194      */
0195     void loadFactoryWidgets(WidgetFactory *f);
0196 
0197     /**
0198      * Lookups widget factories list (note that this function get called once in ctor)
0199      */
0200     bool lookup();
0201 
0202 #if 0
0203     /**
0204      * Loads widget factories found in lookupFactories(). This is called once.
0205      */
0206     void loadFactories();
0207 #endif
0208 
0209     void setErrorMessage(KexiFormWidgetsPluginMetaData *pluginMetaData, const QString& errorMessage);
0210 
0211     class Private;
0212     Private * const d;
0213 };
0214 
0215 }
0216 #endif