1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #ifndef CUSTOMWIDGET
27 #define CUSTOMWIDGET
28 
29 #include <QDesignerCustomWidgetInterface>
30 
31 #include <QString>
32 #include <QSize>
33 #include <QTextStream>
34 #include <QIcon>
35 
36 // Parametrizable Template for custom widgets.
37 
38 template <class Widget>
39 class CustomWidget : public QDesignerCustomWidgetInterface
40 {
41 
42 public:
43     explicit CustomWidget(const QString &includeFile,
44                           bool isContainer,
45                           const QString &group,
46                           const QIcon &icon,
47                           const QString &toolTip,
48                           const QSize &size = QSize());
49 
isContainer()50     bool isContainer() const        { return m_isContainer; }
isInitialized()51     bool isInitialized() const      { return m_initialized; }
icon()52     QIcon icon() const              { return m_icon; }
53     QString domXml() const;
group()54     QString group() const           { return m_group; }
includeFile()55     QString includeFile() const     { return m_includeFile; }
56     QString name() const;
toolTip()57     QString toolTip() const         { return m_toolTip; }
whatsThis()58     QString whatsThis() const       { return m_toolTip; }
59     QWidget *createWidget(QWidget *parent);
60     void initialize(QDesignerFormEditorInterface *core);
61 
62 protected:
initialized()63     bool initialized() const        { return m_initialized; }
64 
65 private:
66     QString displayName() const;
67     QString geometryProperty() const;
68 
69     const QString m_includeFile;
70     const bool m_isContainer;
71     const QString m_group;
72     const QIcon m_icon;
73     const QString m_toolTip;
74     const QSize m_size;
75 
76     bool m_initialized;
77 };
78 
79 
80 template <class Widget>
CustomWidget(const QString & includeFile,bool isContainer,const QString & group,const QIcon & icon,const QString & toolTip,const QSize & size)81     CustomWidget<Widget>::CustomWidget(const QString &includeFile,
82                                        bool isContainer,
83                                        const QString &group,
84                                        const QIcon &icon,
85                                        const QString &toolTip,
86                                        const QSize &size) :
87     m_includeFile(includeFile),
88     m_isContainer(isContainer),
89     m_group(group),
90     m_icon(icon),
91     m_toolTip(toolTip),
92     m_size(size),
93     m_initialized(false)
94 {
95 }
96 
97 // Determine the name to be displayed in the widget box: Strip
98 // namespaces.
99 template <class Widget>
displayName()100     QString CustomWidget<Widget>::displayName() const
101 {
102     QString rc = name();
103     const int index = rc.lastIndexOf(QLatin1Char(':'));
104     if (index != -1)
105         rc.remove(0, index + 1);
106     return rc;
107 }
108 
109 template <class Widget>
geometryProperty()110     QString CustomWidget<Widget>::geometryProperty() const
111 {
112     QString rc;
113     if (m_size.isEmpty())
114         return rc;
115     QTextStream(&rc) << "<property name=\"geometry\"><rect><x>0</x><y>0</y><width>"
116         << m_size.width() << "</width><height>" <<  m_size.height()
117             << "</height></rect></property>";
118     return rc;
119 }
120 
121 template <class Widget>
domXml()122     QString CustomWidget<Widget>::domXml() const
123 {
124     const QString className = name();
125     QString rc;
126     // Name: 'Namespace::QClass' -> 'class'
127     QString name = className;
128     const int lastColonPos = name.lastIndexOf(QLatin1Char(':'));
129     if (lastColonPos != -1)
130         name.remove(0, lastColonPos + 1);
131     if (name.startsWith(QLatin1Char('Q')))
132         name.remove(0, 1);
133     name[0] = name.at(0).toLower();
134     // Format
135     QTextStream str(&rc);
136     str << "<ui displayname=\"" << displayName()
137         << "\" language=\"c++\"><widget class=\"" << className << "\" name=\""
138         << name  << "\">" << geometryProperty()  << "</widget></ui>";
139     return rc;
140 }
141 
142 template <class Widget>
name()143     QString CustomWidget<Widget>::name() const
144 {
145     return QLatin1String(Widget::staticMetaObject.className());
146 }
147 
148 template <class Widget>
createWidget(QWidget * parent)149     QWidget *CustomWidget<Widget>::createWidget(QWidget *parent)
150 {
151     return new Widget(parent);
152 }
153 
154 template <class Widget>
initialize(QDesignerFormEditorInterface *)155     void CustomWidget<Widget>::initialize(QDesignerFormEditorInterface *)
156 {
157     if (!m_initialized) {
158         m_initialized = true;
159     }
160 }
161 
162 #endif // CUSTOMWIDGET
163