File indexing completed on 2024-05-12 16:39:49
0001 /* This file is part of the KDE project 0002 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr> 0003 Copyright (C) 2005-2007 Jarosław Staniek <staniek@kde.org> 0004 0005 This library is free software; you can redistribute it and/or 0006 modify it under the terms of the GNU Library General Public 0007 License as published by the Free Software Foundation; either 0008 version 2 of the License, or (at your option) any later version. 0009 0010 This library is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 Library General Public License for more details. 0014 0015 You should have received a copy of the GNU Library General Public License 0016 along with this library; see the file COPYING.LIB. If not, write to 0017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #ifndef FORMIO_H 0022 #define FORMIO_H 0023 0024 #include "kformdesigner_export.h" 0025 0026 #include <QHash> 0027 #include <QPixmap> 0028 #include <QLabel> 0029 #include <QPaintEvent> 0030 0031 class QString; 0032 class QDomElement; 0033 class QDomNode; 0034 class QDomDocument; 0035 class QVariant; 0036 class QLabel; 0037 0038 //! A blank widget displayed when class is not supported 0039 class KFORMDESIGNER_EXPORT CustomWidget : public QWidget 0040 { 0041 Q_OBJECT 0042 0043 public: 0044 CustomWidget(const QByteArray &className, QWidget *parent); 0045 virtual ~CustomWidget(); 0046 0047 virtual void paintEvent(QPaintEvent *ev) override; 0048 0049 private: 0050 QByteArray m_className; 0051 }; 0052 0053 namespace KFormDesigner 0054 { 0055 0056 class Form; 0057 class ObjectTreeItem; 0058 class Container; 0059 0060 //! KFormDesigner API major version number. Increased on every breaking of backward compatibility. 0061 //! Use KFormDesigner::version() to get real version number of the library. 0062 #define KFORMDESIGNER_VERSION_MAJOR KEXI_STABLE_VERSION_MAJOR 0063 0064 //! KFormDesigner API minor version number. Increased on every compatible change. 0065 //! Use KFormDesigner::version() to get real version number of the library. 0066 #define KFORMDESIGNER_VERSION_MINOR KEXI_STABLE_VERSION_MINOR 0067 0068 //! \return KFormDesigner API version number for this library: "major.minor" 0069 KFORMDESIGNER_EXPORT QString version(); 0070 0071 /** This class act as a namespace for all .ui files related functions, ie saving/loading .ui files. 0072 You don't need to create a FormIO object, as all methods are static.\n 0073 This class is able to read and write Forms to .ui files, and to save each type of properties, including set and enum 0074 properties, and pixmaps(pixmap-related code was taken from Qt Designer). 0075 **/ 0076 //! A class to save/load forms from .ui files 0077 class KFORMDESIGNER_EXPORT FormIO : public QObject 0078 { 0079 Q_OBJECT 0080 0081 public: 0082 FormIO(); 0083 ~FormIO(); 0084 0085 /*! Save the Form in the \a domDoc QDomDocument. Called by saveForm(). 0086 \return true if saving succeeded. 0087 \sa saveForm() */ 0088 static bool saveFormToDom(Form *form, QDomDocument &domDoc); 0089 0090 /*! Save the Form \a form to the file \a filename. If \a filename is null or not given, 0091 a Save File dialog will be shown to choose dest file. 0092 \return true if saving succeeded. 0093 \todo Add errors code and error dialog 0094 */ 0095 static bool saveFormToFile(Form *form, const QString &filename = QString()); 0096 0097 /*! Saves the Form to the \a dest string. \a indent can be specified to apply indentation. 0098 \return true if saving succeeded. 0099 \sa saveForm() 0100 */ 0101 static bool saveFormToString(Form *form, QString &dest, int indent = 0); 0102 0103 /*! Saves the \a form inside the \a dest QByteArray. 0104 \return true if saving succeeded. 0105 \sa saveFormToDom(), saveForm() 0106 */ 0107 static bool saveFormToByteArray(Form *form, QByteArray &dest); 0108 0109 /*! Loads a form from the \a domDoc QDomDocument. Called by loadForm() and loadFormData(). 0110 \return true if loading succeeded. */ 0111 static bool loadFormFromDom(Form *form, QWidget *container, const QDomDocument &domDoc); 0112 0113 /*! Loads a form from the \a src QByteArray. 0114 \sa loadFormFromDom(), loadForm(). 0115 \return true if loading succeeded. 0116 */ 0117 static bool loadFormFromByteArray(Form *form, QWidget *container, QByteArray &src, 0118 bool preview = false); 0119 0120 static bool loadFormFromString(Form *form, QWidget *container, const QString &src, 0121 bool preview = false); 0122 0123 /*! Loads the .ui file \a filename in the Form \a form. If \a filename is null or not given, 0124 a Open File dialog will be shown to select the file to open. 0125 createToplevelWidget() is used to load the Form's toplevel widget. 0126 \return true if loading succeeded. 0127 \todo Add errors code and error dialog 0128 */ 0129 static bool loadFormFromFile(Form *form, QWidget *container, 0130 const QString &filename = QString()); 0131 0132 /*! Saves the widget associated to the ObjectTreeItem \a item into DOM document \a domDoc, 0133 with \a parent as parent node. 0134 It calls readPropertyValue() for each object property, readAttribute() for each 0135 attribute and itself to save child widgets. 0136 \return true if saving succeeded. 0137 This is used to copy/paste widgets. 0138 */ 0139 static void saveWidget(ObjectTreeItem *item, QDomElement &parent, QDomDocument &domDoc, 0140 bool insideGridLayout = false); 0141 0142 /*! Cleans the "UI" QDomElement after saving widget. It deletes the "includes" element 0143 not needed when pasting, and make sure all the "widget" elements are at the beginning. 0144 Call this after copying a widget, before pasting.*/ 0145 static void cleanClipboard(QDomElement &uiElement); 0146 0147 /*! Loads the widget associated to the QDomElement \a el into the Container \a container, 0148 with \a parent as parent widget. 0149 If parent = 0, the Container::widget() is used as parent widget. 0150 This is used to copy/paste widgets. 0151 */ 0152 static void loadWidget(Container *container, const QDomElement &el, 0153 QWidget *parent, QHash<QString, QLabel*> *buddies); 0154 0155 /*! Save an element in the \a domDoc as child of \a parentNode. 0156 The element will be saved like this : 0157 \code <$(tagName) name = "$(property)">< value_as_XML ><$(tagName)/> 0158 \endcode 0159 */ 0160 static void savePropertyElement(QDomElement &parentNode, QDomDocument &domDoc, const QString &tagName, 0161 const QString &property, const QVariant &value); 0162 0163 /*! Read an object property in the DOM doc. 0164 \param form the Form of the property 0165 \param node the QDomNode of the property 0166 \param obj the widget whose property is being read 0167 \param name the name of the property being read 0168 */ 0169 static QVariant readPropertyValue(Form *form, QDomNode node, QObject *obj, const QString &name); 0170 0171 /*! Write an object property in the DOM doc. 0172 \param item the widget item whose property is being saved 0173 \param parentNode the DOM element to write to 0174 \param parent the parent QDomDocument 0175 \param name the name of the property being saved 0176 \param value the value of this property 0177 0178 Properties of subwidget are saved with subwidget="true" attribute added 0179 to 'property' XML element. 0180 */ 0181 static void savePropertyValue(ObjectTreeItem *item, QDomElement &parentNode, 0182 QDomDocument &parent, const char *name, 0183 const QVariant &value); 0184 0185 protected: 0186 /*! Saves the QVariant \a value as text to be included in an xml file, with \a parentNode.*/ 0187 static void writeVariant(QDomDocument &parent, QDomElement &parentNode, const QVariant& value); 0188 0189 /*! Creates a toplevel widget from the QDomElement \a element in the Form \a form, 0190 with \a parent as parent widget. 0191 It calls readPropertyValue() and loadWidget() to load child widgets. 0192 */ 0193 static void createToplevelWidget(Form *form, QWidget *container, QDomElement &element); 0194 0195 /*! \return the name of the pixmap saved, to use to access it 0196 This function save the QPixmap \a pixmap into the DOM document \a domDoc. 0197 The pixmap is converted to XPM and compressed for compatibility with Qt Designer. 0198 Encoding code is taken from Designer. 0199 */ 0200 static QString saveImage(QDomDocument &domDoc, const QPixmap &pixmap); 0201 0202 /*! \return the loaded pixmap 0203 This function loads the pixmap named \a name in the DOM document \a domDoc. 0204 Decoding code is taken from QT Designer. 0205 */ 0206 //! @todo handle result of loading 0207 static QPixmap loadImage(QDomDocument domDoc, const QString& name); 0208 0209 /*! Reads the child nodes of a "widget" element. */ 0210 static void readChildNodes(ObjectTreeItem *tree, Container *container, 0211 const QDomElement &el, QWidget *w, 0212 QHash<QString, QLabel*> *buddies); 0213 0214 /*! Adds an include file name to be saved in the "includehints" part of .ui file, 0215 which is needed by uic. */ 0216 static void addIncludeFileName(const QString &include, QDomDocument &domDoc); 0217 0218 private: 0219 //! This hash stores buddies associations until the Form is completely loaded. 0220 static QHash<QString, QLabel*> *m_buddies; 0221 }; 0222 0223 } 0224 0225 #endif