dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
waypointdelegate.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 <waypointdelegate.h>
28 #include <flightdatamodel.h>
29 
31  : QStyledItemDelegate(parent)
32 {
33 }
34 
41 QWidget *WaypointDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
42  const QModelIndex &index) const
43 {
44  int column = index.column();
45  switch (column) {
46  case FlightDataModel::MODE: {
47  QComboBox *box = new QComboBox(parent);
48  loadComboBox(box);
49  return box;
50  }
51  default:
52  break;
53  }
54 
55  return QStyledItemDelegate::createEditor(parent, option, index);
56 }
57 
63 bool WaypointDelegate::eventFilter(QObject *object, QEvent *event)
64 {
65  QComboBox *comboBox = dynamic_cast<QComboBox *>(object);
66  if (comboBox) {
67  if (event->type() == QEvent::MouseButtonRelease) {
68  comboBox->showPopup();
69  return true;
70  }
71  } else {
72  return QStyledItemDelegate::eventFilter(object, event);
73  }
74  return false;
75 }
76 
83 void WaypointDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
84 {
85  if (!index.isValid())
86  return;
87  if (index.column() == (int)FlightDataModel::MODE) {
88  QComboBox *comboBox = static_cast<QComboBox *>(editor);
89  Q_ASSERT(comboBox != NULL);
90  if (comboBox == NULL)
91  return;
92 
93  int value = index.model()->data(index, Qt::EditRole).toInt();
94  comboBox->setCurrentIndex(value);
95  } else
96  QStyledItemDelegate::setEditorData(editor, index);
97 }
98 
106 void WaypointDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
107  const QModelIndex &index) const
108 {
109  if (!index.isValid())
110  return;
111  if (index.column() == (int)FlightDataModel::MODE) {
112  QComboBox *comboBox = static_cast<QComboBox *>(editor);
113  Q_ASSERT(comboBox != NULL);
114  if (comboBox == NULL)
115  return;
116 
117  int value = comboBox->itemData(comboBox->currentIndex()).toInt();
118  model->setData(index, value, Qt::EditRole);
119  } else
120  QStyledItemDelegate::setModelData(editor, model, index);
121 }
122 
126 void WaypointDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
127  const QModelIndex &index) const
128 {
129  if (index.column() == (int)FlightDataModel::MODE) {
130  QRect r = option.rect;
131  r.setSize(editor->sizeHint());
132  editor->setGeometry(r);
133  } else if (index.column() == (int)FlightDataModel::LOCKED) {
134  // The locked combo box is wider than the column sometimes
135  QRect r = option.rect;
136  r.setSize(editor->sizeHint());
137  editor->setGeometry(r);
138  } else
139  QStyledItemDelegate::updateEditorGeometry(editor, option, index);
140 }
141 
148 QString WaypointDelegate::displayText(const QVariant &value, const QLocale &locale) const
149 {
150  return QStyledItemDelegate::displayText(value, locale);
151 }
152 
158 void WaypointDelegate::loadComboBox(QComboBox *combo) const
159 {
161  foreach (const int k, keys)
162  combo->addItem(FlightDataModel::modeNames.value(k), k);
163 }
static QMap< int, QString > modeNames
void loadComboBox(QComboBox *combo) const
Populate the selections in the mode combo box.
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
Update the size of the editor widget.
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Set data in the model when the UI is changed.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Create the QComboxBox for the mode or pass to the default implementation.
WaypointDelegate(QObject *parent=nullptr)
bool eventFilter(QObject *object, QEvent *event)
This filter is required to make combo boxes work.
void setEditorData(QWidget *editor, const QModelIndex &index) const
Set data in the UI when the model is changed.
QString displayText(const QVariant &value, const QLocale &locale) const
Convert the variant to a string value.