1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Designer of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "layoutinfo_p.h"
43
44 #include <QtDesigner/QDesignerFormEditorInterface>
45 #include <QtDesigner/QDesignerContainerExtension>
46 #include <QtDesigner/QDesignerMetaDataBaseInterface>
47 #include <QtDesigner/QExtensionManager>
48
49 #include <QtGui/QHBoxLayout>
50 #include <QtGui/QFormLayout>
51 #include <QtGui/QSplitter>
52 #include <QtCore/QDebug>
53 #include <QtCore/QHash>
54 #include <QtCore/QRect>
55
56 QT_BEGIN_NAMESPACE
57
58 namespace qdesigner_internal {
59 /*!
60 \overload
61 */
layoutType(const QDesignerFormEditorInterface * core,const QLayout * layout)62 LayoutInfo::Type LayoutInfo::layoutType(const QDesignerFormEditorInterface *core, const QLayout *layout)
63 {
64 Q_UNUSED(core)
65 if (!layout)
66 return NoLayout;
67 else if (qobject_cast<const QHBoxLayout*>(layout))
68 return HBox;
69 else if (qobject_cast<const QVBoxLayout*>(layout))
70 return VBox;
71 else if (qobject_cast<const QGridLayout*>(layout))
72 return Grid;
73 else if (qobject_cast<const QFormLayout*>(layout))
74 return Form;
75 return UnknownLayout;
76 }
77
layoutNameTypeMap()78 static const QHash<QString, LayoutInfo::Type> &layoutNameTypeMap()
79 {
80 static QHash<QString, LayoutInfo::Type> nameTypeMap;
81 if (nameTypeMap.empty()) {
82 nameTypeMap.insert(QLatin1String("QVBoxLayout"), LayoutInfo::VBox);
83 nameTypeMap.insert(QLatin1String("QHBoxLayout"), LayoutInfo::HBox);
84 nameTypeMap.insert(QLatin1String("QGridLayout"), LayoutInfo::Grid);
85 nameTypeMap.insert(QLatin1String("QFormLayout"), LayoutInfo::Form);
86 }
87 return nameTypeMap;
88 }
89
layoutType(const QString & typeName)90 LayoutInfo::Type LayoutInfo::layoutType(const QString &typeName)
91 {
92 return layoutNameTypeMap().value(typeName, NoLayout);
93 }
94
layoutName(Type t)95 QString LayoutInfo::layoutName(Type t)
96 {
97 return layoutNameTypeMap().key(t);
98 }
99
100 /*!
101 \overload
102 */
layoutType(const QDesignerFormEditorInterface * core,const QWidget * w)103 LayoutInfo::Type LayoutInfo::layoutType(const QDesignerFormEditorInterface *core, const QWidget *w)
104 {
105 if (const QSplitter *splitter = qobject_cast<const QSplitter *>(w))
106 return splitter->orientation() == Qt::Horizontal ? HSplitter : VSplitter;
107 return layoutType(core, w->layout());
108 }
109
managedLayoutType(const QDesignerFormEditorInterface * core,const QWidget * w,QLayout ** ptrToLayout)110 LayoutInfo::Type LayoutInfo::managedLayoutType(const QDesignerFormEditorInterface *core,
111 const QWidget *w,
112 QLayout **ptrToLayout)
113 {
114 if (ptrToLayout)
115 *ptrToLayout = 0;
116 if (const QSplitter *splitter = qobject_cast<const QSplitter *>(w))
117 return splitter->orientation() == Qt::Horizontal ? HSplitter : VSplitter;
118 QLayout *layout = managedLayout(core, w);
119 if (!layout)
120 return NoLayout;
121 if (ptrToLayout)
122 *ptrToLayout = layout;
123 return layoutType(core, layout);
124 }
125
layoutParent(const QDesignerFormEditorInterface * core,QLayout * layout)126 QWidget *LayoutInfo::layoutParent(const QDesignerFormEditorInterface *core, QLayout *layout)
127 {
128 Q_UNUSED(core)
129
130 QObject *o = layout;
131 while (o) {
132 if (QWidget *widget = qobject_cast<QWidget*>(o))
133 return widget;
134
135 o = o->parent();
136 }
137 return 0;
138 }
139
deleteLayout(const QDesignerFormEditorInterface * core,QWidget * widget)140 void LayoutInfo::deleteLayout(const QDesignerFormEditorInterface *core, QWidget *widget)
141 {
142 if (QDesignerContainerExtension *container = qt_extension<QDesignerContainerExtension*>(core->extensionManager(), widget))
143 widget = container->widget(container->currentIndex());
144
145 Q_ASSERT(widget != 0);
146
147 QLayout *layout = managedLayout(core, widget);
148
149 if (layout == 0 || core->metaDataBase()->item(layout) != 0) {
150 delete layout;
151 widget->updateGeometry();
152 return;
153 }
154
155 qDebug() << "trying to delete an unmanaged layout:" << "widget:" << widget << "layout:" << layout;
156 }
157
laidoutWidgetType(const QDesignerFormEditorInterface * core,QWidget * widget,bool * isManaged,QLayout ** ptrToLayout)158 LayoutInfo::Type LayoutInfo::laidoutWidgetType(const QDesignerFormEditorInterface *core,
159 QWidget *widget,
160 bool *isManaged,
161 QLayout **ptrToLayout)
162 {
163 if (isManaged)
164 *isManaged = false;
165 if (ptrToLayout)
166 *ptrToLayout = 0;
167
168 QWidget *parent = widget->parentWidget();
169 if (!parent)
170 return NoLayout;
171
172 // 1) Splitter
173 if (QSplitter *splitter = qobject_cast<QSplitter*>(parent)) {
174 if (isManaged)
175 *isManaged = core->metaDataBase()->item(splitter);
176 return splitter->orientation() == Qt::Horizontal ? HSplitter : VSplitter;
177 }
178
179 // 2) Layout of parent
180 QLayout *parentLayout = parent->layout();
181 if (!parentLayout)
182 return NoLayout;
183
184 if (parentLayout->indexOf(widget) != -1) {
185 if (isManaged)
186 *isManaged = core->metaDataBase()->item(parentLayout);
187 if (ptrToLayout)
188 *ptrToLayout = parentLayout;
189 return layoutType(core, parentLayout);
190 }
191
192 // 3) Some child layout (see below comment about Q3GroupBox)
193 const QList<QLayout*> childLayouts = parentLayout->findChildren<QLayout*>();
194 if (childLayouts.empty())
195 return NoLayout;
196 const QList<QLayout*>::const_iterator lcend = childLayouts.constEnd();
197 for (QList<QLayout*>::const_iterator it = childLayouts.constBegin(); it != lcend; ++it) {
198 QLayout *layout = *it;
199 if (layout->indexOf(widget) != -1) {
200 if (isManaged)
201 *isManaged = core->metaDataBase()->item(layout);
202 if (ptrToLayout)
203 *ptrToLayout = layout;
204 return layoutType(core, layout);
205 }
206 }
207
208 return NoLayout;
209 }
210
internalLayout(const QWidget * widget)211 QLayout *LayoutInfo::internalLayout(const QWidget *widget)
212 {
213 QLayout *widgetLayout = widget->layout();
214 if (widgetLayout && widget->inherits("Q3GroupBox")) {
215 if (widgetLayout->count()) {
216 widgetLayout = widgetLayout->itemAt(0)->layout();
217 } else {
218 widgetLayout = 0;
219 }
220 }
221 return widgetLayout;
222 }
223
224
managedLayout(const QDesignerFormEditorInterface * core,const QWidget * widget)225 QLayout *LayoutInfo::managedLayout(const QDesignerFormEditorInterface *core, const QWidget *widget)
226 {
227 if (widget == 0)
228 return 0;
229
230 QLayout *layout = widget->layout();
231 if (!layout)
232 return 0;
233
234 return managedLayout(core, layout);
235 }
236
managedLayout(const QDesignerFormEditorInterface * core,QLayout * layout)237 QLayout *LayoutInfo::managedLayout(const QDesignerFormEditorInterface *core, QLayout *layout)
238 {
239 QDesignerMetaDataBaseInterface *metaDataBase = core->metaDataBase();
240
241 if (!metaDataBase)
242 return layout;
243 /* This code exists mainly for the Q3GroupBox class, for which
244 * widget->layout() returns an internal VBoxLayout. */
245 const QDesignerMetaDataBaseItemInterface *item = metaDataBase->item(layout);
246 if (item == 0) {
247 layout = layout->findChild<QLayout*>();
248 item = metaDataBase->item(layout);
249 }
250 if (!item)
251 return 0;
252 return layout;
253 }
254
255 // Is it a a dummy grid placeholder created by Designer?
isEmptyItem(QLayoutItem * item)256 bool LayoutInfo::isEmptyItem(QLayoutItem *item)
257 {
258 if (item == 0) {
259 qDebug() << "** WARNING Zero-item passed on to isEmptyItem(). This indicates a layout inconsistency.";
260 return true;
261 }
262 return item->spacerItem() != 0;
263 }
264
getFormLayoutItemPosition(const QFormLayout * formLayout,int index,int * rowPtr,int * columnPtr,int * rowspanPtr,int * colspanPtr)265 QDESIGNER_SHARED_EXPORT void getFormLayoutItemPosition(const QFormLayout *formLayout, int index, int *rowPtr, int *columnPtr, int *rowspanPtr, int *colspanPtr)
266 {
267 int row;
268 QFormLayout::ItemRole role;
269 formLayout->getItemPosition(index, &row, &role);
270 const int columnspan = role == QFormLayout::SpanningRole ? 2 : 1;
271 const int column = (columnspan > 1 || role == QFormLayout::LabelRole) ? 0 : 1;
272 if (rowPtr)
273 *rowPtr = row;
274 if (columnPtr)
275 *columnPtr = column;
276 if (rowspanPtr)
277 *rowspanPtr = 1;
278 if (colspanPtr)
279 *colspanPtr = columnspan;
280 }
281
formLayoutRole(int column,int colspan)282 static inline QFormLayout::ItemRole formLayoutRole(int column, int colspan)
283 {
284 if (colspan > 1)
285 return QFormLayout::SpanningRole;
286 return column == 0 ? QFormLayout::LabelRole : QFormLayout::FieldRole;
287 }
288
formLayoutAddWidget(QFormLayout * formLayout,QWidget * w,const QRect & r,bool insert)289 QDESIGNER_SHARED_EXPORT void formLayoutAddWidget(QFormLayout *formLayout, QWidget *w, const QRect &r, bool insert)
290 {
291 // Consistent API galore...
292 if (insert) {
293 const bool spanning = r.width() > 1;
294 if (spanning) {
295 formLayout->insertRow(r.y(), w);
296 } else {
297 QWidget *label = 0, *field = 0;
298 if (r.x() == 0) {
299 label = w;
300 } else {
301 field = w;
302 }
303 formLayout->insertRow(r.y(), label, field);
304 }
305 } else {
306 formLayout->setWidget(r.y(), formLayoutRole(r.x(), r.width()), w);
307 }
308 }
309
310 } // namespace qdesigner_internal
311
312 QT_END_NAMESPACE
313