BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
widgetbox.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
45 
46 #include <QtDesigner/QDesignerFormEditorInterface>
47 #include <QtDesigner/QDesignerFormWindowManagerInterface>
48 
50 
51 #include <QApplication>
52 #include <QDrag>
53 #include <QLineEdit>
54 #include <QToolBar>
55 #include <QVBoxLayout>
56 #include <QtGui/QDropEvent>
57 
58 #include <QtGui/QIcon>
59 
61 
63 
64 #include <iostream>
65 QT_BEGIN_NAMESPACE
66 
67 namespace qdesigner_internal {
68 
69 class WidgetBoxFilterLineEdit : public QLineEdit {
70 public:
71  explicit WidgetBoxFilterLineEdit(QWidget* parent = 0)
72  : QLineEdit(parent), m_defaultFocusPolicy(focusPolicy())
73  {
74  setFocusPolicy(Qt::NoFocus);
75  }
76 
77 protected:
78  virtual void mousePressEvent(QMouseEvent* event);
79  virtual void focusInEvent(QFocusEvent* e);
80 
81 private:
82  const Qt::FocusPolicy m_defaultFocusPolicy;
83 };
84 
86 {
87  if (!hasFocus()) // Explicitly focus on click.
88  setFocus(Qt::OtherFocusReason);
89  QLineEdit::mousePressEvent(e);
90 }
91 
93 {
94  // Refuse the focus if the mouse it outside. In addition to the mouse
95  // press logic, this prevents a re-focussing which occurs once
96  // we actually had focus
97  const Qt::FocusReason reason = e->reason();
98  if (reason == Qt::ActiveWindowFocusReason || reason == Qt::PopupFocusReason) {
99  const QPoint mousePos = mapFromGlobal(QCursor::pos());
100  const bool refuse = !geometry().contains(mousePos);
101  if (refuse) {
102  e->ignore();
103  return;
104  }
105  }
106  QLineEdit::focusInEvent(e);
107 }
108 
109 // WidgetBox::WidgetBox(QDesignerFormEditorInterface *core, QWidget *parent, Qt::WindowFlags flags)
110 WidgetBox::WidgetBox(SampleDesignerInterface* core, QWidget* parent, Qt::WindowFlags flags)
111  : QDesignerWidgetBox(parent, flags), m_core(core), m_view(new WidgetBoxTreeWidget(m_core))
112 {
113  QVBoxLayout* l = new QVBoxLayout(this);
114  l->setMargin(0);
115  l->setSpacing(0);
116 
117  // Prevent the filter from grabbing focus since Our view has Qt::NoFocus
118  QToolBar* toolBar = new QToolBar(this);
119  QLineEdit* filterWidget = new WidgetBoxFilterLineEdit(toolBar);
120  filterWidget->setPlaceholderText("Filter");
121  filterWidget->setClearButtonEnabled(true);
122  connect(filterWidget, SIGNAL(textChanged(QString)), m_view, SLOT(filter(QString)));
123  toolBar->addWidget(filterWidget);
124  l->addWidget(toolBar);
125 
126  // View
127  connect(m_view, SIGNAL(pressed(QString, QString, QPoint)), this,
128  SLOT(handleMousePress(QString, QString, QPoint)));
129  l->addWidget(m_view);
130 
131  setAcceptDrops(true);
132 
133  // QVBoxLayout *l = new QVBoxLayout(this);
134  // l->setMargin(0);
135  // l->setSpacing(0);
136 
137  // // Prevent the filter from grabbing focus since Our view has Qt::NoFocus
138  // FilterWidget *filterWidget = new FilterWidget(0, FilterWidget::LayoutAlignNone);
139  // filterWidget->setRefuseFocus(true);
140  // connect(filterWidget, SIGNAL(filterChanged(QString)), m_view, SLOT(filter(QString)));
141 
142  // QToolBar *toolBar = new QToolBar(this);
143  // toolBar->addWidget(filterWidget);
144  // l->addWidget(toolBar);
145 
146  // // View
147  // connect(m_view, SIGNAL(pressed(QString,QString,QPoint)),
148  // this, SLOT(handleMousePress(QString,QString,QPoint)));
149  // l->addWidget(m_view);
150 
151  // setAcceptDrops (true);
152 }
153 
154 WidgetBox::~WidgetBox() = default;
155 
156 // QDesignerFormEditorInterface *WidgetBox::core() const
157 //{
158 // return m_core;
159 //}
160 
162 {
163  return m_core;
164 }
165 
166 void WidgetBox::handleMousePress(const QString& name, const QString& xml,
167  const QPoint& global_mouse_pos)
168 {
169  Q_UNUSED(global_mouse_pos);
170  if (QApplication::mouseButtons() != Qt::LeftButton)
171  return;
172 
173  // std::cout << "WidgetBox::handleMousePress() -> name:" << name.toStdString() << std::endl;
174  DesignerMimeData::execDrag(name, xml, this);
175 }
176 
178 {
179  return m_view->categoryCount();
180 }
181 
182 QDesignerWidgetBoxInterface::Category WidgetBox::category(int cat_idx) const
183 {
184  return m_view->category(cat_idx);
185 }
186 
187 void WidgetBox::addCategory(const Category& cat)
188 {
189  m_view->addCategory(cat);
190 }
191 
192 void WidgetBox::removeCategory(int cat_idx)
193 {
194  m_view->removeCategory(cat_idx);
195 }
196 
197 int WidgetBox::widgetCount(int cat_idx) const
198 {
199  return m_view->widgetCount(cat_idx);
200 }
201 
202 QDesignerWidgetBoxInterface::Widget WidgetBox::widget(int cat_idx, int wgt_idx) const
203 {
204  return m_view->widget(cat_idx, wgt_idx);
205 }
206 
207 void WidgetBox::addWidget(int cat_idx, const Widget& wgt)
208 {
209  m_view->addWidget(cat_idx, wgt);
210 }
211 
212 void WidgetBox::removeWidget(int cat_idx, int wgt_idx)
213 {
214  m_view->removeWidget(cat_idx, wgt_idx);
215 }
216 
217 void WidgetBox::dropWidgets(const QList<QDesignerDnDItemInterface*>& item_list, const QPoint&)
218 {
219  m_view->dropWidgets(item_list);
220 }
221 
222 void WidgetBox::setFileName(const QString& file_name)
223 {
224  m_view->setFileName(file_name);
225 }
226 
227 QString WidgetBox::fileName() const
228 {
229  return m_view->fileName();
230 }
231 
233 {
234  // std::cout << "WidgetBox::load() -> We are here" << std::endl;
235  return m_view->load(loadMode());
236 }
237 
238 bool WidgetBox::loadContents(const QString& contents)
239 {
240  return m_view->loadContents(contents);
241 }
242 
244 {
245  return m_view->save();
246 }
247 
248 static const QDesignerMimeData* checkDragEvent(QDropEvent* event, bool acceptEventsFromWidgetBox)
249 {
250  // std::cout << "QDesignerMimeData *checkDragEvent() -> ?" << std::endl;
251  const QDesignerMimeData* mimeData = qobject_cast<const QDesignerMimeData*>(event->mimeData());
252  if (!mimeData) {
253  event->ignore();
254  return 0;
255  }
256  // If desired, ignore a widget box drag and drop, where widget==0.
257  if (!acceptEventsFromWidgetBox) {
258  const bool fromWidgetBox = !mimeData->items().first()->widget();
259  if (fromWidgetBox) {
260  event->ignore();
261  return 0;
262  }
263  }
264 
265  mimeData->acceptEvent(event);
266  return mimeData;
267 }
268 
269 void WidgetBox::dragEnterEvent(QDragEnterEvent* event)
270 {
271  // We accept event originating from the widget box also here,
272  // because otherwise Windows will not show the DnD pixmap.
273  checkDragEvent(event, true);
274 }
275 
276 void WidgetBox::dragMoveEvent(QDragMoveEvent* event)
277 {
278  checkDragEvent(event, true);
279 }
280 
281 void WidgetBox::dropEvent(QDropEvent* event)
282 {
283  const QDesignerMimeData* mimeData = checkDragEvent(event, false);
284  if (!mimeData)
285  return;
286 
287  dropWidgets(mimeData->items(), event->pos());
289 }
290 
291 QIcon WidgetBox::iconForWidget(const QString& className, const QString& category) const
292 {
293  Widget widgetData;
294  if (!findWidget(this, className, category, &widgetData))
295  return QIcon();
296  return m_view->iconForWidget(widgetData.iconName());
297 }
298 
299 } // namespace qdesigner_internal
300 
301 QT_END_NAMESPACE
Defines class DesignerMimeData.
Defines class SampleDesigner.
static Qt::DropAction execDrag(const QString &name, const QString &xmldescr, QWidget *dragSource)
Execute a drag and drop operation.
sample designer interface
void acceptEvent(QDropEvent *e) const
const QDesignerDnDItems & items() const
static void removeMovedWidgetsFromSourceForm(const QDesignerDnDItems &items)
static bool findWidget(const QDesignerWidgetBoxInterface *wbox, const QString &className, const QString &category, Widget *widgetData)
virtual void mousePressEvent(QMouseEvent *event)
Definition: widgetbox.cpp:85
virtual void focusInEvent(QFocusEvent *e)
Definition: widgetbox.cpp:92
const Qt::FocusPolicy m_defaultFocusPolicy
Definition: widgetbox.cpp:82
WidgetBoxTreeWidget: A tree of categories.
Widget widget(int cat_idx, int wgt_idx) const
void setFileName(const QString &file_name)
bool loadContents(const QString &contents)
QIcon iconForWidget(QString iconName) const
void addWidget(int cat_idx, const Widget &wgt)
bool load(QDesignerWidgetBox::LoadMode loadMode)
void removeWidget(int cat_idx, int wgt_idx)
void dropWidgets(const QList< QDesignerDnDItemInterface * > &item_list)
SampleDesignerInterface * core() const
Definition: widgetbox.cpp:161
virtual Category category(int cat_idx) const
Definition: widgetbox.cpp:182
WidgetBox(SampleDesignerInterface *core, QWidget *parent=nullptr, Qt::WindowFlags flags={})
Definition: widgetbox.cpp:110
virtual int categoryCount() const
Definition: widgetbox.cpp:177
SampleDesignerInterface * m_core
Definition: widgetbox.h:104
virtual void dragEnterEvent(QDragEnterEvent *event)
Definition: widgetbox.cpp:269
virtual void addWidget(int cat_idx, const Widget &wgt)
Definition: widgetbox.cpp:207
virtual void removeCategory(int cat_idx)
Definition: widgetbox.cpp:192
virtual void dragMoveEvent(QDragMoveEvent *event)
Definition: widgetbox.cpp:276
WidgetBoxTreeWidget * m_view
Definition: widgetbox.h:106
virtual void dropEvent(QDropEvent *event)
Definition: widgetbox.cpp:281
virtual int widgetCount(int cat_idx) const
Definition: widgetbox.cpp:197
virtual void setFileName(const QString &file_name)
Definition: widgetbox.cpp:222
virtual QIcon iconForWidget(const QString &className, const QString &category="") const
Definition: widgetbox.cpp:291
virtual void removeWidget(int cat_idx, int wgt_idx)
Definition: widgetbox.cpp:212
virtual Widget widget(int cat_idx, int wgt_idx) const
Definition: widgetbox.cpp:202
void handleMousePress(const QString &name, const QString &xml, const QPoint &global_mouse_pos)
Definition: widgetbox.cpp:166
virtual bool loadContents(const QString &contents)
Definition: widgetbox.cpp:238
void dropWidgets(const QList< QDesignerDnDItemInterface * > &item_list, const QPoint &global_mouse_pos)
Definition: widgetbox.cpp:217
virtual QString fileName() const
Definition: widgetbox.cpp:227
virtual void addCategory(const Category &cat)
Definition: widgetbox.cpp:187
QString const & name(EShape k)
Definition: particles.cpp:21
static const QDesignerMimeData * checkDragEvent(QDropEvent *event, bool acceptEventsFromWidgetBox)
Definition: widgetbox.cpp:248