dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
ipconnectionoptionspage.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
30 #include <QComboBox>
31 #include <QLineEdit>
32 #include <QSpinBox>
33 
34 #include "ui_ipconnectionoptionspage.h"
35 
37  : IOptionsPage(parent)
38  , m_config(config)
39 {
40 }
41 
43 {
44 }
45 
46 QWidget *IPConnectionOptionsPage::createPage(QWidget *parent)
47 {
48 
49  m_page = new Ui::IPconnectionOptionsPage();
50  QWidget *w = new QWidget(parent);
51  m_page->setupUi(w);
52 
53  m_model = new IPConnectionOptionsModel(this);
54 
55  for (const auto &host : m_config->hosts()) {
56  m_model->insertRow(m_model->rowCount());
57  int row = m_model->rowCount() - 1;
58  m_model->setData(m_model->index(row, ColumnProtocol), QVariant::fromValue(host.protocol));
59  m_model->setData(m_model->index(row, ColumnHostname), host.hostname);
60  m_model->setData(m_model->index(row, ColumnPort), host.port);
61  }
62 
63  // make the columns take up full width
64  m_page->tblHosts->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
65  m_page->tblHosts->setItemDelegate(new IPConnectionOptionsDelegate(m_page->tblHosts));
66  m_page->tblHosts->setModel(m_model);
67 
68  connect(m_page->btnAdd, &QPushButton::clicked, this,
69  [this]() { m_model->insertRow(m_model->rowCount()); });
70 
71  connect(m_page->btnRemove, &QPushButton::clicked, this, [this]() {
72  const auto selection = m_page->tblHosts->selectionModel()->selectedIndexes();
73  if (selection.count() && selection.at(0).isValid())
74  m_page->tblHosts->model()->removeRow(selection.at(0).row());
75  });
76 
77  return w;
78 }
79 
81 {
82  // discard invalid rows
83  for (int row = 0; row < m_model->rowCount();) {
84  auto hostname = m_model->data(m_model->index(row, ColumnHostname)).toString();
85  auto port = m_model->data(m_model->index(row, ColumnPort)).toInt();
86  if (!hostname.length() || port < 1 || port > 65535)
87  m_model->removeRow(row);
88  else
89  row++;
90  }
91  m_config->setHosts(m_model->hosts());
92  m_config->saveConfig();
93  emit availableDevChanged();
94 }
95 
97 {
98  delete m_page;
99 }
100 
102  const QStyleOptionViewItem &option,
103  const QModelIndex &index) const
104 {
105  Q_UNUSED(option)
106 
107  switch (index.column()) {
109  auto editor = new QComboBox(parent);
110  editor->addItem("TCP", IPConnectionConfiguration::ProtocolTcp);
111  editor->addItem("UDP", IPConnectionConfiguration::ProtocolUdp);
112  editor->setFrame(false);
113  return editor;
114  }
116  auto editor = new QLineEdit(parent);
117  editor->setFrame(false);
118  return editor;
119  }
121  auto editor = new QSpinBox(parent);
122  editor->setMinimum(1);
123  editor->setMaximum(65535);
124  editor->setFrame(false);
125  return editor;
126  }
127  default:
128  Q_ASSERT(false);
129  }
130  return nullptr;
131 }
132 
133 void IPConnectionOptionsDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
134 {
135  QVariant val = index.model()->data(index, Qt::EditRole);
136 
137  switch (index.column()) {
139  auto comboBox = static_cast<QComboBox *>(editor);
140  for (int i = 0; i < comboBox->count(); i++) {
141  if (comboBox->itemData(i) == val.value<IPConnectionConfiguration::Protocol>())
142  comboBox->setCurrentIndex(i);
143  }
144  break;
145  }
147  auto lineEdit = static_cast<QLineEdit *>(editor);
148  lineEdit->setText(val.toString());
149  break;
150  }
152  auto spinBox = static_cast<QSpinBox *>(editor);
153  spinBox->setValue(val.toInt());
154  break;
155  }
156  default:
157  Q_ASSERT(false);
158  }
159 }
160 
161 void IPConnectionOptionsDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
162  const QModelIndex &index) const
163 {
164  QVariant val;
165 
166  switch (index.column()) {
168  auto comboBox = static_cast<QComboBox *>(editor);
169  val.setValue(QVariant::fromValue(comboBox->currentData()));
170  break;
171  }
173  auto lineEdit = static_cast<QLineEdit *>(editor);
174  val.setValue(lineEdit->text());
175  break;
176  }
178  auto spinBox = static_cast<QSpinBox *>(editor);
179  spinBox->interpretText();
180  val.setValue(spinBox->value());
181  break;
182  }
183  default:
184  Q_ASSERT(false);
185  return;
186  }
187 
188  model->setData(index, val, Qt::EditRole);
189 }
190 
192  const QStyleOptionViewItem &option,
193  const QModelIndex &index) const
194 {
195  Q_UNUSED(index)
196  editor->setGeometry(option.rect);
197 }
198 
199 int IPConnectionOptionsModel::rowCount(const QModelIndex &parent) const
200 {
201  if (parent.isValid())
202  return 0;
203  return m_hosts.count();
204 }
205 
206 int IPConnectionOptionsModel::columnCount(const QModelIndex &parent) const
207 {
208  if (parent.isValid())
209  return 0;
211 }
212 
213 QVariant IPConnectionOptionsModel::data(const QModelIndex &index, int role) const
214 {
215  if (!index.isValid() || index.row() >= m_hosts.count())
216  return QVariant();
217 
218  const IPConnectionConfiguration::Host host = m_hosts.at(index.row());
219 
220  if (index.column() == IPConnectionOptionsPage::ColumnProtocol && role == Qt::DisplayRole)
221  return host.protocol == IPConnectionConfiguration::ProtocolTcp ? "TCP" : "UDP";
222 
223  if (role == Qt::DisplayRole || role == Qt::EditRole) {
224  switch (index.column()) {
226  return QVariant::fromValue(host.protocol);
228  return host.hostname;
230  return host.port;
231  default:
232  Q_ASSERT(false);
233  }
234  }
235 
236  return QVariant();
237 }
238 
239 QVariant IPConnectionOptionsModel::headerData(int section, Qt::Orientation orientation,
240  int role) const
241 {
242  if (role != Qt::DisplayRole)
243  return QVariant();
244 
245  if (orientation == Qt::Horizontal) {
246  switch (section) {
248  return tr("Protocol");
250  return tr("IP/Hostname");
252  return tr("Port");
253  default:
254  return QVariant();
255  }
256  }
257  return QString::number(section + 1);
258 }
259 
260 Qt::ItemFlags IPConnectionOptionsModel::flags(const QModelIndex &index) const
261 {
262  if (!index.isValid())
263  return Qt::ItemIsEnabled;
264  return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
265 }
266 
267 bool IPConnectionOptionsModel::setData(const QModelIndex &index, const QVariant &value, int role)
268 {
269  if (index.isValid() && role == Qt::EditRole) {
270  auto &host = m_hosts[index.row()];
271  switch (index.column()) {
273  host.protocol = value.value<IPConnectionConfiguration::Protocol>();
274  break;
276  host.hostname = value.toString();
277  break;
279  host.port = value.toInt();
280  break;
281  default:
282  return false;
283  }
284  emit dataChanged(index, index);
285  return true;
286  }
287  return false;
288 }
289 
290 bool IPConnectionOptionsModel::insertRows(int position, int rows, const QModelIndex &index)
291 {
292  Q_UNUSED(index)
293 
294  beginInsertRows(QModelIndex(), position, position + rows - 1);
295 
296  for (int row = 0; row < rows; row++)
297  m_hosts.insert(position, IPConnectionConfiguration::Host());
298 
299  endInsertRows();
300  return true;
301 }
302 
303 bool IPConnectionOptionsModel::removeRows(int position, int rows, const QModelIndex &index)
304 {
305  Q_UNUSED(index)
306 
307  beginRemoveRows(QModelIndex(), position, position + rows - 1);
308 
309  for (int row = 0; row < rows; row++)
310  m_hosts.removeAt(position);
311 
312  endRemoveRows();
313  return true;
314 }
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
int columnCount(const QModelIndex &parent=QModelIndex()) const
IPConnectionOptionsPage(IPConnectionConfiguration *config, QObject *parent=nullptr)
void setHosts(QVector< Host > &hosts)
for i
Definition: OPPlots.m:140
QVariant data(const QModelIndex &index, int role=Qt::EditRole) const
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QWidget * createPage(QWidget *parent)
bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex())
void setEditorData(QWidget *editor, const QModelIndex &index) const override
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
QVector< IPConnectionConfiguration::Host > & hosts()
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex())
int rowCount(const QModelIndex &parent=QModelIndex()) const
Qt::ItemFlags flags(const QModelIndex &index) const