dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
waypointdialog.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 "waypointdialog.h"
28 #include <waypointdelegate.h>
29 #include <waypoint.h>
30 #include "ui_waypoint_dialog.h"
31 
32 WaypointDialog::WaypointDialog(QWidget *parent, QAbstractItemModel *model,
33  QItemSelectionModel *selection)
34  : QDialog(parent, Qt::Window)
35  , ui(new Ui_waypoint_dialog)
36  , model(model)
37  , itemSelection(selection)
38 {
39  ui->setupUi(this);
40  connect(ui->cbMode, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
41  &WaypointDialog::setupModeWidgets);
42 
43  // Connect up the buttons
44  connect(ui->pushButtonOK, &QAbstractButton::clicked, this, &WaypointDialog::onOkButton_clicked);
45  connect(ui->pushButtonCancel, &QAbstractButton::clicked, this,
46  &WaypointDialog::onCancelButton_clicked);
47  connect(ui->pushButtonPrevious, &QAbstractButton::clicked, this,
48  &WaypointDialog::onPreviousButton_clicked);
49  connect(ui->pushButtonNext, &QAbstractButton::clicked, this,
50  &WaypointDialog::onNextButton_clicked);
51 
52  mapper = new QDataWidgetMapper(this);
53 
54  WaypointDelegate *delegate = new WaypointDelegate(this);
55  delegate->loadComboBox(ui->cbMode);
56 
57  mapper->setItemDelegate(delegate);
58  connect(mapper, &QDataWidgetMapper::currentIndexChanged, this,
59  &WaypointDialog::currentIndexChanged);
60  mapper->setModel(model);
61  mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
62  mapper->addMapping(ui->doubleSpinBoxLatitude, FlightDataModel::LATPOSITION);
63  mapper->addMapping(ui->doubleSpinBoxLongitude, FlightDataModel::LNGPOSITION);
64  mapper->addMapping(ui->doubleSpinBoxAltitude, FlightDataModel::ALTITUDE);
65  mapper->addMapping(ui->doubleSpinBoxNorth, FlightDataModel::NED_NORTH);
66  mapper->addMapping(ui->doubleSpinBoxEast, FlightDataModel::NED_EAST);
67  mapper->addMapping(ui->doubleSpinBoxDown, FlightDataModel::NED_DOWN);
68  mapper->addMapping(ui->lineEditDescription, FlightDataModel::WPDESCRIPTION);
69  mapper->addMapping(ui->doubleSpinBoxVelocity, FlightDataModel::VELOCITY);
70  mapper->addMapping(ui->cbMode, FlightDataModel::MODE);
71  mapper->addMapping(ui->dsb_modeParams, FlightDataModel::MODE_PARAMS);
72  mapper->addMapping(ui->checkBoxLocked, FlightDataModel::LOCKED);
73 
74  // Make sure the model catches updates from the check box
75  connect(ui->checkBoxLocked, &QCheckBox::stateChanged, mapper, &QDataWidgetMapper::submit);
76 
77  mapper->setCurrentIndex(selection->currentIndex().row());
78 
79  // Support locking the controls when locked
80  enableEditWidgets();
81  connect(model, &QAbstractItemModel::dataChanged, this, &WaypointDialog::enableEditWidgets);
82 
83  // This means whenever the model changes we show those changes. Since the update is on
84  // auto submit changes are still permitted.
85  connect(model, &QAbstractItemModel::dataChanged, mapper, &QDataWidgetMapper::revert);
86 
87  connect(itemSelection, &QItemSelectionModel::currentRowChanged, this,
88  &WaypointDialog::currentRowChanged);
89 
90  setModal(true);
91 }
92 
98 void WaypointDialog::currentIndexChanged(int index)
99 {
100  ui->lbNumber->setText(QString::number(index + 1));
101  QModelIndex idx = mapper->model()->index(index, 0);
102  if (index == itemSelection->currentIndex().row())
103  return;
104  itemSelection->clear();
105  itemSelection->setCurrentIndex(idx, QItemSelectionModel::Select | QItemSelectionModel::Rows);
106 }
107 
109 {
110  delete ui;
111 }
112 
117 void WaypointDialog::setupModeWidgets()
118 {
119  int mode = ui->cbMode->itemData(ui->cbMode->currentIndex()).toInt();
120  switch (mode) {
121  case Waypoint::MODE_CIRCLERIGHT:
122  case Waypoint::MODE_CIRCLELEFT:
123  ui->modeParams->setVisible(true);
124  ui->modeParams->setText(tr("Radius"));
125  ui->dsb_modeParams->setVisible(true);
126  break;
127  default:
128  ui->modeParams->setVisible(false);
129  ui->dsb_modeParams->setVisible(false);
130  break;
131  }
132 }
133 
139 {
140  if (!isVisible())
141  show();
142  if (isMinimized())
143  showNormal();
144  if (!isActiveWindow())
145  activateWindow();
146  raise();
147  setFocus(Qt::OtherFocusReason);
148  mapper->setCurrentIndex(number);
149 }
150 
152 void WaypointDialog::onOkButton_clicked()
153 {
154  mapper->submit();
155  close();
156 }
157 
159 void WaypointDialog::onCancelButton_clicked()
160 {
161  mapper->revert();
162  close();
163 }
164 
166 void WaypointDialog::onPreviousButton_clicked()
167 {
168  mapper->toPrevious();
169 }
170 
172 void WaypointDialog::onNextButton_clicked()
173 {
174  mapper->toNext();
175 }
176 
182 void WaypointDialog::currentRowChanged(QModelIndex current, QModelIndex previous)
183 {
184  Q_UNUSED(previous);
185 
186  mapper->setCurrentIndex(current.row());
187 }
188 
194 void WaypointDialog::enableEditWidgets()
195 {
196  int row = itemSelection->currentIndex().row();
197  bool value = model->data(model->index(row, FlightDataModel::LOCKED)).toBool();
198  QWidget *w;
199  foreach (QWidget *obj, this->findChildren<QWidget *>()) {
200  w = qobject_cast<QComboBox *>(obj);
201  if (w)
202  w->setEnabled(!value);
203  w = qobject_cast<QLineEdit *>(obj);
204  if (w)
205  w->setEnabled(!value);
206  w = qobject_cast<QDoubleSpinBox *>(obj);
207  if (w)
208  w->setEnabled(!value);
209  w = qobject_cast<QSpinBox *>(obj);
210  if (w)
211  w->setEnabled(!value);
212  }
213 }
void loadComboBox(QComboBox *combo) const
Populate the selections in the mode combo box.
The WaypointDelegate class is used to handle updating the values in the mode combo box to the data mo...
void editWaypoint(int number)
Edit the requested waypoint, show dialog if it is not showing.
WaypointDialog(QWidget *parent, QAbstractItemModel *model, QItemSelectionModel *selection)