1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the Qt Designer of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "widgetbox.h"
|
---|
43 | #include "widgetboxtreewidget.h"
|
---|
44 | #include "widgetbox_dnditem.h"
|
---|
45 |
|
---|
46 | #include <QtDesigner/QDesignerFormEditorInterface>
|
---|
47 | #include <QtDesigner/QDesignerFormWindowManagerInterface>
|
---|
48 |
|
---|
49 | #include <iconloader_p.h>
|
---|
50 | #include <qdesigner_utils_p.h>
|
---|
51 | #include <filterwidget_p.h>
|
---|
52 |
|
---|
53 | #include <QtGui/QDropEvent>
|
---|
54 | #include <QtGui/QVBoxLayout>
|
---|
55 | #include <QtGui/QApplication>
|
---|
56 | #include <QtGui/QToolBar>
|
---|
57 | #include <QtGui/QIcon>
|
---|
58 |
|
---|
59 | QT_BEGIN_NAMESPACE
|
---|
60 |
|
---|
61 | namespace qdesigner_internal {
|
---|
62 |
|
---|
63 | WidgetBox::WidgetBox(QDesignerFormEditorInterface *core, QWidget *parent, Qt::WindowFlags flags)
|
---|
64 | : QDesignerWidgetBox(parent, flags),
|
---|
65 | m_core(core),
|
---|
66 | m_view(new WidgetBoxTreeWidget(m_core))
|
---|
67 | {
|
---|
68 |
|
---|
69 | QVBoxLayout *l = new QVBoxLayout(this);
|
---|
70 | l->setMargin(0);
|
---|
71 | l->setSpacing(0);
|
---|
72 |
|
---|
73 | // Prevent the filter from grabbing focus since Our view has Qt::NoFocus
|
---|
74 | FilterWidget *filterWidget = new FilterWidget(0, FilterWidget::LayoutAlignNone);
|
---|
75 | filterWidget->setRefuseFocus(true);
|
---|
76 | connect(filterWidget, SIGNAL(filterChanged(QString)), m_view, SLOT(filter(QString)));
|
---|
77 |
|
---|
78 | QToolBar *toolBar = new QToolBar(this);
|
---|
79 | toolBar->addWidget(filterWidget);
|
---|
80 | l->addWidget(toolBar);
|
---|
81 |
|
---|
82 | // View
|
---|
83 | connect(m_view, SIGNAL(pressed(QString,QString,QPoint)),
|
---|
84 | this, SLOT(handleMousePress(QString,QString,QPoint)));
|
---|
85 | l->addWidget(m_view);
|
---|
86 |
|
---|
87 | setAcceptDrops (true);
|
---|
88 | }
|
---|
89 |
|
---|
90 | WidgetBox::~WidgetBox()
|
---|
91 | {
|
---|
92 | }
|
---|
93 |
|
---|
94 | QDesignerFormEditorInterface *WidgetBox::core() const
|
---|
95 | {
|
---|
96 | return m_core;
|
---|
97 | }
|
---|
98 |
|
---|
99 | void WidgetBox::handleMousePress(const QString &name, const QString &xml, const QPoint &global_mouse_pos)
|
---|
100 | {
|
---|
101 | if (QApplication::mouseButtons() != Qt::LeftButton)
|
---|
102 | return;
|
---|
103 |
|
---|
104 | DomUI *ui = xmlToUi(name, xml, true);
|
---|
105 | if (ui == 0)
|
---|
106 | return;
|
---|
107 | QList<QDesignerDnDItemInterface*> item_list;
|
---|
108 | item_list.append(new WidgetBoxDnDItem(core(), ui, global_mouse_pos));
|
---|
109 | m_core->formWindowManager()->dragItems(item_list);
|
---|
110 | }
|
---|
111 |
|
---|
112 | int WidgetBox::categoryCount() const
|
---|
113 | {
|
---|
114 | return m_view->categoryCount();
|
---|
115 | }
|
---|
116 |
|
---|
117 | QDesignerWidgetBoxInterface::Category WidgetBox::category(int cat_idx) const
|
---|
118 | {
|
---|
119 | return m_view->category(cat_idx);
|
---|
120 | }
|
---|
121 |
|
---|
122 | void WidgetBox::addCategory(const Category &cat)
|
---|
123 | {
|
---|
124 | m_view->addCategory(cat);
|
---|
125 | }
|
---|
126 |
|
---|
127 | void WidgetBox::removeCategory(int cat_idx)
|
---|
128 | {
|
---|
129 | m_view->removeCategory(cat_idx);
|
---|
130 | }
|
---|
131 |
|
---|
132 | int WidgetBox::widgetCount(int cat_idx) const
|
---|
133 | {
|
---|
134 | return m_view->widgetCount(cat_idx);
|
---|
135 | }
|
---|
136 |
|
---|
137 | QDesignerWidgetBoxInterface::Widget WidgetBox::widget(int cat_idx, int wgt_idx) const
|
---|
138 | {
|
---|
139 | return m_view->widget(cat_idx, wgt_idx);
|
---|
140 | }
|
---|
141 |
|
---|
142 | void WidgetBox::addWidget(int cat_idx, const Widget &wgt)
|
---|
143 | {
|
---|
144 | m_view->addWidget(cat_idx, wgt);
|
---|
145 | }
|
---|
146 |
|
---|
147 | void WidgetBox::removeWidget(int cat_idx, int wgt_idx)
|
---|
148 | {
|
---|
149 | m_view->removeWidget(cat_idx, wgt_idx);
|
---|
150 | }
|
---|
151 |
|
---|
152 | void WidgetBox::dropWidgets(const QList<QDesignerDnDItemInterface*> &item_list, const QPoint&)
|
---|
153 | {
|
---|
154 | m_view->dropWidgets(item_list);
|
---|
155 | }
|
---|
156 |
|
---|
157 | void WidgetBox::setFileName(const QString &file_name)
|
---|
158 | {
|
---|
159 | m_view->setFileName(file_name);
|
---|
160 | }
|
---|
161 |
|
---|
162 | QString WidgetBox::fileName() const
|
---|
163 | {
|
---|
164 | return m_view->fileName();
|
---|
165 | }
|
---|
166 |
|
---|
167 | bool WidgetBox::load()
|
---|
168 | {
|
---|
169 | return m_view->load(loadMode());
|
---|
170 | }
|
---|
171 |
|
---|
172 | bool WidgetBox::loadContents(const QString &contents)
|
---|
173 | {
|
---|
174 | return m_view->loadContents(contents);
|
---|
175 | }
|
---|
176 |
|
---|
177 | bool WidgetBox::save()
|
---|
178 | {
|
---|
179 | return m_view->save();
|
---|
180 | }
|
---|
181 |
|
---|
182 | static const QDesignerMimeData *checkDragEvent(QDropEvent * event,
|
---|
183 | bool acceptEventsFromWidgetBox)
|
---|
184 | {
|
---|
185 | #ifndef QT_NO_DRAGANDDROP
|
---|
186 | const QDesignerMimeData *mimeData = qobject_cast<const QDesignerMimeData *>(event->mimeData());
|
---|
187 | if (!mimeData) {
|
---|
188 | event->ignore();
|
---|
189 | return 0;
|
---|
190 | }
|
---|
191 | // If desired, ignore a widget box drag and drop, where widget==0.
|
---|
192 | if (!acceptEventsFromWidgetBox) {
|
---|
193 | const bool fromWidgetBox = !mimeData->items().first()->widget();
|
---|
194 | if (fromWidgetBox) {
|
---|
195 | event->ignore();
|
---|
196 | return 0;
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | mimeData->acceptEvent(event);
|
---|
201 | return mimeData;
|
---|
202 | #else
|
---|
203 | return 0;
|
---|
204 | #endif
|
---|
205 | }
|
---|
206 |
|
---|
207 | void WidgetBox::dragEnterEvent (QDragEnterEvent * event)
|
---|
208 | {
|
---|
209 | #ifndef QT_NO_DRAGANDDROP
|
---|
210 | // We accept event originating from the widget box also here,
|
---|
211 | // because otherwise Windows will not show the DnD pixmap.
|
---|
212 | checkDragEvent(event, true);
|
---|
213 | #endif
|
---|
214 | }
|
---|
215 |
|
---|
216 | void WidgetBox::dragMoveEvent(QDragMoveEvent * event)
|
---|
217 | {
|
---|
218 | #ifndef QT_NO_DRAGANDDROP
|
---|
219 | checkDragEvent(event, true);
|
---|
220 | #endif
|
---|
221 | }
|
---|
222 |
|
---|
223 | void WidgetBox::dropEvent(QDropEvent * event)
|
---|
224 | {
|
---|
225 | #ifndef QT_NO_DRAGANDDROP
|
---|
226 | const QDesignerMimeData *mimeData = checkDragEvent(event, false);
|
---|
227 | if (!mimeData)
|
---|
228 | return;
|
---|
229 |
|
---|
230 | dropWidgets(mimeData->items(), event->pos());
|
---|
231 | QDesignerMimeData::removeMovedWidgetsFromSourceForm(mimeData->items());
|
---|
232 | #endif
|
---|
233 | }
|
---|
234 |
|
---|
235 | QIcon WidgetBox::iconForWidget(const QString &className, const QString &category) const
|
---|
236 | {
|
---|
237 | Widget widgetData;
|
---|
238 | if (!findWidget(this, className, category, &widgetData))
|
---|
239 | return QIcon();
|
---|
240 | return m_view->iconForWidget(widgetData.iconName());
|
---|
241 | }
|
---|
242 |
|
---|
243 | } // namespace qdesigner_internal
|
---|
244 |
|
---|
245 | QT_END_NAMESPACE
|
---|