dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
browseritemdelegate.cpp
Go to the documentation of this file.
1 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 
27 #include "uavobjectbrowserwidget.h"
28 #include "browseritemdelegate.h"
29 #include "fieldtreeitem.h"
30 
32  : QStyledItemDelegate(parent)
33 {
34  this->proxyModel = proxyModel;
35 }
36 
37 QWidget *BrowserItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
38  const QModelIndex &proxyIndex) const
39 {
40  Q_UNUSED(option)
41  QModelIndex index = proxyModel->mapToSource(proxyIndex);
42  FieldTreeItem *item = static_cast<FieldTreeItem *>(index.internalPointer());
43  QWidget *editor = item->createEditor(parent);
44  Q_ASSERT(editor);
45  return editor;
46 }
47 
53 bool BrowserItemDelegate::eventFilter(QObject *object, QEvent *event)
54 {
55  QComboBox *comboBox = dynamic_cast<QComboBox *>(object);
56  if (comboBox) {
57  if (event->type() == QEvent::MouseButtonRelease) {
58  comboBox->showPopup();
59  return true;
60  }
61  }
62 
63  return QStyledItemDelegate::eventFilter(object, event);
64 }
65 
66 void BrowserItemDelegate::setEditorData(QWidget *editor, const QModelIndex &proxyIndex) const
67 {
68  QModelIndex index = proxyModel->mapToSource(proxyIndex);
69  FieldTreeItem *item = static_cast<FieldTreeItem *>(index.internalPointer());
70  QVariant value = proxyIndex.model()->data(proxyIndex, Qt::EditRole);
71  item->setEditorValue(editor, value);
72 }
73 
74 void BrowserItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
75  const QModelIndex &proxyIndex) const
76 {
77  QModelIndex index = proxyModel->mapToSource(proxyIndex);
78  FieldTreeItem *item = static_cast<FieldTreeItem *>(index.internalPointer());
79  QVariant value = item->getEditorValue(editor);
80  bool ret = model->setData(proxyIndex, value, Qt::EditRole);
81  Q_ASSERT(ret);
82 }
83 
84 void BrowserItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
85  const QModelIndex & /* index */) const
86 {
87  editor->setGeometry(option.rect);
88 }
89 
90 QSize BrowserItemDelegate::sizeHint(const QStyleOptionViewItem &option,
91  const QModelIndex &index) const
92 {
93  Q_UNUSED(option);
94  Q_UNUSED(index);
95  return QSpinBox().sizeHint();
96 }
BrowserItemDelegate(TreeSortFilterProxyModel *proxyModel, QObject *parent=nullptr)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual QVariant getEditorValue(QWidget *editor)=0
virtual void setEditorValue(QWidget *editor, QVariant value)=0
virtual QWidget * createEditor(QWidget *parent)=0
void setEditorData(QWidget *editor, const QModelIndex &index) const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
bool eventFilter(QObject *object, QEvent *event)
This filter is required to make combo boxes work.
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
QVariant data(int column=1) const
Definition: treeitem.cpp:168
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const