source: trunk/tools/designer/src/components/widgetbox/widgetbox_dnditem.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 7.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt Designer of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "widgetbox_dnditem.h"
43#include "ui4_p.h"
44
45#include <widgetfactory_p.h>
46#include <spacer_widget_p.h>
47#include <qdesigner_formbuilder_p.h>
48#include <qtresourcemodel_p.h>
49#include <formscriptrunner_p.h>
50#include <formwindowbase_p.h>
51#include <qdesigner_utils_p.h>
52#include <qdesigner_dockwidget_p.h>
53
54#include <QtDesigner/QDesignerFormEditorInterface>
55#include <QtDesigner/QDesignerFormWindowManagerInterface>
56
57#include <QtGui/QStyle>
58#include <QtGui/QApplication>
59
60QT_BEGIN_NAMESPACE
61
62namespace qdesigner_internal {
63/*******************************************************************************
64** WidgetBoxResource
65*/
66
67static inline DeviceProfile currentDeviceProfile(const QDesignerFormEditorInterface *core)
68{
69 if (QDesignerFormWindowInterface *cfw = core->formWindowManager()->activeFormWindow())
70 if (const FormWindowBase *fwb = qobject_cast<const FormWindowBase *>(cfw))
71 return fwb->deviceProfile();
72 return DeviceProfile();
73}
74
75class WidgetBoxResource : public QDesignerFormBuilder
76{
77public:
78 WidgetBoxResource(QDesignerFormEditorInterface *core);
79
80 // protected->public
81 QWidget *createUI(DomUI *ui, QWidget *parents) { return QDesignerFormBuilder::create(ui, parents); }
82
83protected:
84
85 virtual QWidget *create(DomWidget *ui_widget, QWidget *parents);
86 virtual QWidget *createWidget(const QString &widgetName, QWidget *parentWidget, const QString &name);
87};
88
89WidgetBoxResource::WidgetBoxResource(QDesignerFormEditorInterface *core) :
90 QDesignerFormBuilder(core, DisableScripts, currentDeviceProfile(core))
91{
92}
93
94
95QWidget *WidgetBoxResource::createWidget(const QString &widgetName, QWidget *parentWidget, const QString &name)
96{
97 if (widgetName == QLatin1String("Spacer")) {
98 Spacer *spacer = new Spacer(parentWidget);
99 spacer->setObjectName(name);
100 return spacer;
101 }
102
103 return QDesignerFormBuilder::createWidget(widgetName, parentWidget, name);
104}
105
106QWidget *WidgetBoxResource::create(DomWidget *ui_widget, QWidget *parent)
107{
108 QWidget *result = QDesignerFormBuilder::create(ui_widget, parent);
109 // It is possible to have a syntax error or something in custom
110 // widget XML, so, try to recover here by creating an artificial
111 // top level + widget.
112 if (!result) {
113 const QString msg = QApplication::translate("qdesigner_internal::WidgetBox", "Warning: Widget creation failed in the widget box. This could be caused by invalid custom widget XML.");
114 qdesigner_internal::designerWarning(msg);
115 result = new QWidget(parent);
116 new QWidget(result);
117 }
118 result->setFocusPolicy(Qt::NoFocus);
119 result->setObjectName(ui_widget->attributeName());
120 return result;
121}
122
123/*******************************************************************************
124** WidgetBoxResource
125*/
126
127static QSize geometryProp(const DomWidget *dw)
128{
129 const QList<DomProperty*> prop_list = dw->elementProperty();
130 const QString geometry = QLatin1String("geometry");
131 foreach (DomProperty *prop, prop_list) {
132 if (prop->attributeName() != geometry)
133 continue;
134 DomRect *dr = prop->elementRect();
135 if (dr == 0)
136 continue;
137 return QSize(dr->elementWidth(), dr->elementHeight());
138 }
139 return QSize();
140}
141
142static QSize domWidgetSize(const DomWidget *dw)
143{
144 QSize size = geometryProp(dw);
145 if (size.isValid())
146 return size;
147
148 foreach (const DomWidget *child, dw->elementWidget()) {
149 size = geometryProp(child);
150 if (size.isValid())
151 return size;
152 }
153
154 foreach (const DomLayout *dl, dw->elementLayout()) {
155 foreach (DomLayoutItem *item, dl->elementItem()) {
156 const DomWidget *child = item->elementWidget();
157 if (child == 0)
158 continue;
159 size = geometryProp(child);
160 if (size.isValid())
161 return size;
162 }
163 }
164
165 return QSize();
166}
167
168static QWidget *decorationFromDomWidget(DomUI *dom_ui, QDesignerFormEditorInterface *core)
169{
170 WidgetBoxResource builder(core);
171 // We have the builder create the articial QWidget fake top level as a tooltip
172 // because the size algorithm works better at weird DPI settings
173 // if the actual widget is created as a child of a container
174 QWidget *fakeTopLevel = builder.createUI(dom_ui, static_cast<QWidget*>(0));
175 fakeTopLevel->setParent(0, Qt::ToolTip); // Container
176 // Actual widget
177 const DomWidget *domW = dom_ui->elementWidget()->elementWidget().front();
178 QWidget *w = qFindChildren<QWidget*>(fakeTopLevel).front();
179 Q_ASSERT(w);
180 // hack begin;
181 // We set _q_dockDrag dynamic property which will be detected in drag enter event of form window.
182 // Dock drop is handled in special way (highlight goes to central widget of main window)
183 if (qobject_cast<QDesignerDockWidget *>(w))
184 fakeTopLevel->setProperty("_q_dockDrag", QVariant(true));
185 // hack end;
186 w->setAutoFillBackground(true); // Different style for embedded
187 QSize size = domWidgetSize(domW);
188 const QSize minimumSize = w->minimumSizeHint();
189 if (!size.isValid())
190 size = w->sizeHint();
191 if (size.width() < minimumSize.width())
192 size.setWidth(minimumSize.width());
193 if (size.height() < minimumSize.height())
194 size.setHeight(minimumSize.height());
195 // A QWidget might have size -1,-1 if no geometry property is specified in the widget box.
196 if (size.isEmpty())
197 size = size.expandedTo(QSize(16, 16));
198 w->setGeometry(QRect(QPoint(0, 0), size));
199 fakeTopLevel->resize(size);
200 return fakeTopLevel;
201}
202
203WidgetBoxDnDItem::WidgetBoxDnDItem(QDesignerFormEditorInterface *core,
204 DomUI *dom_ui,
205 const QPoint &global_mouse_pos) :
206 QDesignerDnDItem(CopyDrop)
207{
208 QWidget *decoration = decorationFromDomWidget(dom_ui, core);
209 decoration->move(global_mouse_pos - QPoint(5, 5));
210
211 init(dom_ui, 0, decoration, global_mouse_pos);
212}
213}
214
215QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.