|
| 1 | +#include "client_dialogscannerrequest.h" |
| 2 | +#include "ui_client_dialogscannerrequest.h" |
| 3 | + |
| 4 | +#include <mbcore.h> |
| 5 | + |
| 6 | +mbClientDialogScannerRequest::Strings::Strings() : |
| 7 | + prefix (QStringLiteral("Ui.Scanner.Dialogs.Request.")), |
| 8 | + func (prefix+QStringLiteral("func")), |
| 9 | + offset1 (prefix+QStringLiteral("offset1")), |
| 10 | + offset2 (prefix+QStringLiteral("offset2")), |
| 11 | + count2 (prefix+QStringLiteral("count2")), |
| 12 | + wGeometry (prefix+QStringLiteral("geometry")), |
| 13 | + wSplitterState(prefix+QStringLiteral("splitterState")) |
| 14 | +{ |
| 15 | +} |
| 16 | + |
| 17 | +const mbClientDialogScannerRequest::Strings &mbClientDialogScannerRequest::Strings::instance() |
| 18 | +{ |
| 19 | + static Strings s; |
| 20 | + return s; |
| 21 | +} |
| 22 | + |
| 23 | +mbClientDialogScannerRequest::mbClientDialogScannerRequest(QWidget *parent) : |
| 24 | + QDialog(parent), |
| 25 | + ui(new Ui::mbClientDialogScannerRequest) |
| 26 | +{ |
| 27 | + ui->setupUi(this); |
| 28 | + |
| 29 | + m_model = new Model(this); |
| 30 | + |
| 31 | + ui->lsRequest->setModel(m_model); |
| 32 | + ui->lsRequest->setSelectionMode(QAbstractItemView::SingleSelection); |
| 33 | + |
| 34 | + QSpinBox *sp; |
| 35 | + QComboBox *cmb; |
| 36 | + |
| 37 | + sp = ui->spOffset1; |
| 38 | + sp->setMinimum(0); |
| 39 | + sp->setMaximum(USHRT_MAX); |
| 40 | + sp->setValue(0); |
| 41 | + |
| 42 | + sp = ui->spOffset2; |
| 43 | + sp->setMinimum(0); |
| 44 | + sp->setMaximum(USHRT_MAX); |
| 45 | + sp->setValue(0); |
| 46 | + |
| 47 | + sp = ui->spCount2; |
| 48 | + sp->setMinimum(0); |
| 49 | + sp->setMaximum(USHRT_MAX); |
| 50 | + sp->setValue(1); |
| 51 | + |
| 52 | + m_funcNums.append(MBF_READ_COILS ); |
| 53 | + m_funcNums.append(MBF_READ_DISCRETE_INPUTS ); |
| 54 | + m_funcNums.append(MBF_READ_HOLDING_REGISTERS ); |
| 55 | + m_funcNums.append(MBF_READ_INPUT_REGISTERS ); |
| 56 | + m_funcNums.append(MBF_WRITE_SINGLE_COIL ); |
| 57 | + m_funcNums.append(MBF_WRITE_SINGLE_REGISTER ); |
| 58 | + m_funcNums.append(MBF_READ_EXCEPTION_STATUS ); |
| 59 | + m_funcNums.append(MBF_WRITE_MULTIPLE_COILS ); |
| 60 | + m_funcNums.append(MBF_WRITE_MULTIPLE_REGISTERS ); |
| 61 | + m_funcNums.append(MBF_MASK_WRITE_REGISTER ); |
| 62 | + m_funcNums.append(MBF_READ_WRITE_MULTIPLE_REGISTERS ); |
| 63 | + |
| 64 | + cmb = ui->cmbFunction; |
| 65 | + connect(cmb, SIGNAL(currentIndexChanged(int)), this, SLOT(setCurrentFuncIndex(int))); |
| 66 | + Q_FOREACH (uint8_t funcNum, m_funcNums) |
| 67 | + { |
| 68 | + cmb->addItem(QString("%1 - %2") |
| 69 | + .arg(funcNum, 2, 10, QChar('0')) |
| 70 | + .arg(mb::ModbusFunctionString(funcNum)) |
| 71 | + ); |
| 72 | + } |
| 73 | + cmb->setCurrentIndex(2); |
| 74 | + |
| 75 | + connect(ui->lsRequest->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &mbClientDialogScannerRequest::selectionChanged); |
| 76 | + connect(ui->btnAdd , &QPushButton::clicked, this, &mbClientDialogScannerRequest::addFunc ); |
| 77 | + connect(ui->btnModify, &QPushButton::clicked, this, &mbClientDialogScannerRequest::modifyFunc); |
| 78 | + connect(ui->btnDelete, &QPushButton::clicked, this, &mbClientDialogScannerRequest::deleteFunc); |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +mbClientDialogScannerRequest::~mbClientDialogScannerRequest() |
| 83 | +{ |
| 84 | + delete ui; |
| 85 | +} |
| 86 | + |
| 87 | +MBSETTINGS mbClientDialogScannerRequest::cachedSettings() const |
| 88 | +{ |
| 89 | + MBSETTINGS m; |
| 90 | + const Strings &s = Strings::instance(); |
| 91 | + |
| 92 | + m[s.func ] = getCurrentFuncNum(); |
| 93 | + m[s.offset1 ] = ui->spOffset1 ->value (); |
| 94 | + m[s.offset2 ] = ui->spOffset2 ->value (); |
| 95 | + m[s.count2 ] = ui->spCount2 ->value (); |
| 96 | + m[s.wGeometry ] = this->saveGeometry(); |
| 97 | + m[s.wSplitterState] = ui->splitter->saveState(); |
| 98 | + |
| 99 | + return m; |
| 100 | +} |
| 101 | + |
| 102 | +void mbClientDialogScannerRequest::setCachedSettings(const MBSETTINGS &m) |
| 103 | +{ |
| 104 | + const Strings &s = Strings::instance(); |
| 105 | + |
| 106 | + MBSETTINGS::const_iterator it; |
| 107 | + MBSETTINGS::const_iterator end = m.end(); |
| 108 | + //bool ok; |
| 109 | + |
| 110 | + it = m.find(s.func ); if (it != end) setCurrentFuncNum(static_cast<uint8_t>(it.value().toUInt())); |
| 111 | + it = m.find(s.offset1 ); if (it != end) ui->spOffset1 ->setValue (it.value().toInt() ); |
| 112 | + it = m.find(s.offset2 ); if (it != end) ui->spOffset2 ->setValue (it.value().toInt() ); |
| 113 | + it = m.find(s.count2 ); if (it != end) ui->spCount2 ->setValue (it.value().toInt() ); |
| 114 | + it = m.find(s.wGeometry ); if (it != end) this ->restoreGeometry(it.value().toByteArray()); |
| 115 | + it = m.find(s.wSplitterState); if (it != end) ui->splitter ->restoreState (it.value().toByteArray()); |
| 116 | +} |
| 117 | + |
| 118 | +bool mbClientDialogScannerRequest::getRequest(mbClientScanner::Request_t &req) |
| 119 | +{ |
| 120 | + m_model->setRequest(req); |
| 121 | + if (req.count()) |
| 122 | + ui->lsRequest->selectionModel()->select(m_model->index(0), QItemSelectionModel::Select); |
| 123 | + switch (QDialog::exec()) |
| 124 | + { |
| 125 | + case QDialog::Accepted: |
| 126 | + req = m_model->request(); |
| 127 | + return true; |
| 128 | + } |
| 129 | + return false; |
| 130 | +} |
| 131 | + |
| 132 | +void mbClientDialogScannerRequest::selectionChanged(const QModelIndex ¤t, const QModelIndex &) |
| 133 | +{ |
| 134 | + mbClientScanner::FuncParams func = m_model->func(current.row()); |
| 135 | + setCurrentFunc(func); |
| 136 | +} |
| 137 | + |
| 138 | +void mbClientDialogScannerRequest::addFunc() |
| 139 | +{ |
| 140 | + mbClientScanner::FuncParams func = getCurrentFunc(); |
| 141 | + m_model->addFunc(func); |
| 142 | +} |
| 143 | + |
| 144 | +void mbClientDialogScannerRequest::modifyFunc() |
| 145 | +{ |
| 146 | + QModelIndexList ls = ui->lsRequest->selectionModel()->selectedRows(); |
| 147 | + if (ls.count()) |
| 148 | + { |
| 149 | + mbClientScanner::FuncParams func = getCurrentFunc(); |
| 150 | + m_model->modifyFunc(ls.first().row(), func); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +void mbClientDialogScannerRequest::deleteFunc() |
| 155 | +{ |
| 156 | + QModelIndexList ls = ui->lsRequest->selectionModel()->selectedRows(); |
| 157 | + if (ls.count()) |
| 158 | + { |
| 159 | + m_model->deleteFunc(ls.first().row()); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +void mbClientDialogScannerRequest::setCurrentFuncIndex(int i) |
| 164 | +{ |
| 165 | + uint8_t funcNum = m_funcNums.value(i); |
| 166 | + setCurrentFuncNum(funcNum); |
| 167 | +} |
| 168 | + |
| 169 | +mbClientScanner::FuncParams mbClientDialogScannerRequest::getCurrentFunc() const |
| 170 | +{ |
| 171 | + uint8_t funcNum = getCurrentFuncNum(); |
| 172 | + mbClientScanner::FuncParams func; |
| 173 | + switch(funcNum) |
| 174 | + { |
| 175 | + case MBF_READ_COILS: |
| 176 | + case MBF_READ_DISCRETE_INPUTS: |
| 177 | + case MBF_READ_HOLDING_REGISTERS: |
| 178 | + case MBF_READ_INPUT_REGISTERS: |
| 179 | + case MBF_WRITE_MULTIPLE_COILS: |
| 180 | + case MBF_WRITE_MULTIPLE_REGISTERS: |
| 181 | + case MBF_READ_WRITE_MULTIPLE_REGISTERS: |
| 182 | + func.func = funcNum; |
| 183 | + func.offset = static_cast<uint16_t>(ui->spOffset2->value()); |
| 184 | + func.count = static_cast<uint16_t>(ui->spCount2 ->value()); |
| 185 | + break; |
| 186 | + case MBF_WRITE_SINGLE_COIL: |
| 187 | + case MBF_WRITE_SINGLE_REGISTER: |
| 188 | + case MBF_MASK_WRITE_REGISTER: |
| 189 | + func.func = funcNum; |
| 190 | + func.offset = static_cast<uint16_t>(ui->spOffset1->value()); |
| 191 | + func.count = 1; |
| 192 | + break; |
| 193 | + case MBF_READ_EXCEPTION_STATUS: |
| 194 | + func.func = funcNum; |
| 195 | + break; |
| 196 | + } |
| 197 | + return func; |
| 198 | +} |
| 199 | + |
| 200 | +void mbClientDialogScannerRequest::setCurrentFunc(const mbClientScanner::FuncParams &f) |
| 201 | +{ |
| 202 | + int index = m_funcNums.indexOf(f.func); |
| 203 | + if (index < 0) |
| 204 | + return; |
| 205 | + switch(f.func) |
| 206 | + { |
| 207 | + case MBF_READ_COILS: |
| 208 | + case MBF_READ_DISCRETE_INPUTS: |
| 209 | + case MBF_READ_HOLDING_REGISTERS: |
| 210 | + case MBF_READ_INPUT_REGISTERS: |
| 211 | + case MBF_WRITE_MULTIPLE_COILS: |
| 212 | + case MBF_WRITE_MULTIPLE_REGISTERS: |
| 213 | + case MBF_READ_WRITE_MULTIPLE_REGISTERS: |
| 214 | + ui->spOffset2->setValue(f.offset); |
| 215 | + ui->spCount2 ->setValue(f.count); |
| 216 | + break; |
| 217 | + case MBF_WRITE_SINGLE_COIL: |
| 218 | + case MBF_WRITE_SINGLE_REGISTER: |
| 219 | + case MBF_MASK_WRITE_REGISTER: |
| 220 | + ui->spOffset1->setValue(f.offset); |
| 221 | + break; |
| 222 | + case MBF_READ_EXCEPTION_STATUS: |
| 223 | + break; |
| 224 | + default: |
| 225 | + return; |
| 226 | + } |
| 227 | + ui->cmbFunction->setCurrentIndex(index); |
| 228 | +} |
| 229 | + |
| 230 | +uint8_t mbClientDialogScannerRequest::getCurrentFuncNum() const |
| 231 | +{ |
| 232 | + return m_funcNums.value(ui->cmbFunction->currentIndex()); |
| 233 | +} |
| 234 | + |
| 235 | +void mbClientDialogScannerRequest::setCurrentFuncNum(uint8_t funcNum) |
| 236 | +{ |
| 237 | + switch(funcNum) |
| 238 | + { |
| 239 | + case MBF_READ_COILS: |
| 240 | + case MBF_READ_DISCRETE_INPUTS: |
| 241 | + case MBF_READ_HOLDING_REGISTERS: |
| 242 | + case MBF_READ_INPUT_REGISTERS: |
| 243 | + case MBF_WRITE_MULTIPLE_COILS: |
| 244 | + case MBF_WRITE_MULTIPLE_REGISTERS: |
| 245 | + case MBF_READ_WRITE_MULTIPLE_REGISTERS: |
| 246 | + ui->swFuncParams->setCurrentWidget(ui->pgOffsetCount); |
| 247 | + break; |
| 248 | + case MBF_WRITE_SINGLE_COIL: |
| 249 | + case MBF_WRITE_SINGLE_REGISTER: |
| 250 | + case MBF_MASK_WRITE_REGISTER: |
| 251 | + ui->swFuncParams->setCurrentWidget(ui->pgOffset); |
| 252 | + break; |
| 253 | + default: |
| 254 | + ui->swFuncParams->setCurrentWidget(ui->pgEmpty); |
| 255 | + break; |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +/* ============================================================= |
| 260 | + * =========================== MODEL =========================== |
| 261 | + */ |
| 262 | +int mbClientDialogScannerRequest::Model::rowCount(const QModelIndex &) const |
| 263 | +{ |
| 264 | + return m_req.count(); |
| 265 | +} |
| 266 | + |
| 267 | +QVariant mbClientDialogScannerRequest::Model::data(const QModelIndex &index, int role) const |
| 268 | +{ |
| 269 | + if (role == Qt::DisplayRole) |
| 270 | + return mbClientScanner::toString(m_req.value(index.row())); |
| 271 | + return QVariant(); |
| 272 | +} |
| 273 | + |
| 274 | +void mbClientDialogScannerRequest::Model::setRequest(const mbClientScanner::Request_t &req) |
| 275 | +{ |
| 276 | + beginResetModel(); |
| 277 | + m_req = req; |
| 278 | + endResetModel(); |
| 279 | +} |
| 280 | + |
| 281 | +mbClientScanner::FuncParams mbClientDialogScannerRequest::Model::func(int i) |
| 282 | +{ |
| 283 | + return m_req.value(i); |
| 284 | +} |
| 285 | + |
| 286 | +void mbClientDialogScannerRequest::Model::addFunc(const mbClientScanner::FuncParams &f) |
| 287 | +{ |
| 288 | + int c = m_req.count(); |
| 289 | + beginInsertRows(QModelIndex(), c, c); |
| 290 | + m_req.append(f); |
| 291 | + endInsertRows(); |
| 292 | +} |
| 293 | + |
| 294 | +void mbClientDialogScannerRequest::Model::modifyFunc(int i, const mbClientScanner::FuncParams &f) |
| 295 | +{ |
| 296 | + if ((0 <= i) && (i < m_req.size())) |
| 297 | + { |
| 298 | + m_req[i] = f; |
| 299 | + QModelIndex ind = index(i); |
| 300 | + Q_EMIT dataChanged(ind, ind); |
| 301 | + } |
| 302 | +} |
| 303 | + |
| 304 | +void mbClientDialogScannerRequest::Model::deleteFunc(int i) |
| 305 | +{ |
| 306 | + if ((0 <= i) && (i < m_req.size())) |
| 307 | + { |
| 308 | + beginRemoveRows(QModelIndex(), i, i); |
| 309 | + m_req.removeAt(i); |
| 310 | + endRemoveRows(); |
| 311 | + } |
| 312 | +} |
0 commit comments