source: trunk/tools/designer/src/components/widgetbox/widgetbox.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: 6.5 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.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
59QT_BEGIN_NAMESPACE
60
61namespace qdesigner_internal {
62
63WidgetBox::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 l->addWidget(filterWidget);
78
79 // View
80 connect(m_view, SIGNAL(pressed(QString,QString,QPoint)),
81 this, SLOT(handleMousePress(QString,QString,QPoint)));
82 l->addWidget(m_view);
83
84 setAcceptDrops (true);
85}
86
87WidgetBox::~WidgetBox()
88{
89}
90
91QDesignerFormEditorInterface *WidgetBox::core() const
92{
93 return m_core;
94}
95
96void WidgetBox::handleMousePress(const QString &name, const QString &xml, const QPoint &global_mouse_pos)
97{
98 if (QApplication::mouseButtons() != Qt::LeftButton)
99 return;
100
101 DomUI *ui = xmlToUi(name, xml, true);
102 if (ui == 0)
103 return;
104 QList<QDesignerDnDItemInterface*> item_list;
105 item_list.append(new WidgetBoxDnDItem(core(), ui, global_mouse_pos));
106 m_core->formWindowManager()->dragItems(item_list);
107}
108
109int WidgetBox::categoryCount() const
110{
111 return m_view->categoryCount();
112}
113
114QDesignerWidgetBoxInterface::Category WidgetBox::category(int cat_idx) const
115{
116 return m_view->category(cat_idx);
117}
118
119void WidgetBox::addCategory(const Category &cat)
120{
121 m_view->addCategory(cat);
122}
123
124void WidgetBox::removeCategory(int cat_idx)
125{
126 m_view->removeCategory(cat_idx);
127}
128
129int WidgetBox::widgetCount(int cat_idx) const
130{
131 return m_view->widgetCount(cat_idx);
132}
133
134QDesignerWidgetBoxInterface::Widget WidgetBox::widget(int cat_idx, int wgt_idx) const
135{
136 return m_view->widget(cat_idx, wgt_idx);
137}
138
139void WidgetBox::addWidget(int cat_idx, const Widget &wgt)
140{
141 m_view->addWidget(cat_idx, wgt);
142}
143
144void WidgetBox::removeWidget(int cat_idx, int wgt_idx)
145{
146 m_view->removeWidget(cat_idx, wgt_idx);
147}
148
149void WidgetBox::dropWidgets(const QList<QDesignerDnDItemInterface*> &item_list, const QPoint&)
150{
151 m_view->dropWidgets(item_list);
152}
153
154void WidgetBox::setFileName(const QString &file_name)
155{
156 m_view->setFileName(file_name);
157}
158
159QString WidgetBox::fileName() const
160{
161 return m_view->fileName();
162}
163
164bool WidgetBox::load()
165{
166 return m_view->load(loadMode());
167}
168
169bool WidgetBox::loadContents(const QString &contents)
170{
171 return m_view->loadContents(contents);
172}
173
174bool WidgetBox::save()
175{
176 return m_view->save();
177}
178
179static const QDesignerMimeData *checkDragEvent(QDropEvent * event,
180 bool acceptEventsFromWidgetBox)
181{
182 const QDesignerMimeData *mimeData = qobject_cast<const QDesignerMimeData *>(event->mimeData());
183 if (!mimeData) {
184 event->ignore();
185 return 0;
186 }
187 // If desired, ignore a widget box drag and drop, where widget==0.
188 if (!acceptEventsFromWidgetBox) {
189 const bool fromWidgetBox = !mimeData->items().first()->widget();
190 if (fromWidgetBox) {
191 event->ignore();
192 return 0;
193 }
194 }
195
196 mimeData->acceptEvent(event);
197 return mimeData;
198}
199
200void WidgetBox::dragEnterEvent (QDragEnterEvent * event)
201{
202 // We accept event originating from the widget box also here,
203 // because otherwise Windows will not show the DnD pixmap.
204 checkDragEvent(event, true);
205}
206
207void WidgetBox::dragMoveEvent(QDragMoveEvent * event)
208{
209 checkDragEvent(event, true);
210}
211
212void WidgetBox::dropEvent(QDropEvent * event)
213{
214 const QDesignerMimeData *mimeData = checkDragEvent(event, false);
215 if (!mimeData)
216 return;
217
218 dropWidgets(mimeData->items(), event->pos());
219 QDesignerMimeData::removeMovedWidgetsFromSourceForm(mimeData->items());
220}
221
222QIcon WidgetBox::iconForWidget(const QString &className, const QString &category) const
223{
224 Widget widgetData;
225 if (!findWidget(this, className, category, &widgetData))
226 return QIcon();
227 return m_view->iconForWidget(widgetData.iconName());
228}
229
230} // namespace qdesigner_internal
231
232QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.