diff --git a/src/cdlgmdfregisterbit.cpp b/src/cdlgmdfregisterbit.cpp index dcbb622b..f6a31945 100644 --- a/src/cdlgmdfregisterbit.cpp +++ b/src/cdlgmdfregisterbit.cpp @@ -63,7 +63,8 @@ CDlgMdfRegisterBit::CDlgMdfRegisterBit(QWidget* parent) // m_type = mdf_type_unknown; m_pbit = nullptr; - + m_index = 0; + m_type = mdf_type_unknown; vscpworks* pworks = (vscpworks*)QCoreApplication::instance(); setInitialFocus(); @@ -84,7 +85,7 @@ CDlgMdfRegisterBit::~CDlgMdfRegisterBit() // void -CDlgMdfRegisterBit::initDialogData(CMDF_Bit* pbit, int index) +CDlgMdfRegisterBit::initDialogData(CMDF_Bit* pbit, int index, mdf_record_type type) { QString str; @@ -94,6 +95,8 @@ CDlgMdfRegisterBit::initDialogData(CMDF_Bit* pbit, int index) } m_pbit = pbit; + m_index = index; + m_type = type; setName(pbit->getName().c_str()); setPos(pbit->getPos()); @@ -136,6 +139,27 @@ CDlgMdfRegisterBit::initDialogData(CMDF_Bit* pbit, int index) ui->editName->setFocus(); break; } + + if (mdf_type_register == type) { + setWindowTitle("Register bit definitions"); + } + else if (mdf_type_remotevar == type) { + setWindowTitle("Remote variable bit definitions"); + } + else if (mdf_type_alarm == type) { + setWindowTitle("Alarm bit definitions"); + ui->spinWidth->setValue(1); + ui->spinWidth->setEnabled(false); + ui->spinDefault->setValue(0); + ui->spinDefault->setEnabled(false); + ui->spinMin->setValue(0); + ui->spinMin->setEnabled(false); + ui->spinMax->setValue(1); + ui->spinMax->setEnabled(false); + ui->comboAccess->setCurrentIndex(3); + ui->comboAccess->setEnabled(false); + } + } /////////////////////////////////////////////////////////////////////////////// diff --git a/src/cdlgmdfregisterbit.h b/src/cdlgmdfregisterbit.h index 06d4260c..2842da1d 100644 --- a/src/cdlgmdfregisterbit.h +++ b/src/cdlgmdfregisterbit.h @@ -68,7 +68,7 @@ class CDlgMdfRegisterBit : public QDialog { @param index Selected file item */ - void initDialogData(CMDF_Bit* pbit, int index = 0); + void initDialogData(CMDF_Bit* pbit, int index = 0, mdf_record_type type = mdf_type_register); // ---------------------------------------------------------------------------- // Getters & Setters @@ -117,6 +117,12 @@ public slots: /// Pointer to bit information CMDF_Bit* m_pbit; + + /// Selected field in dialog + int m_index; + + /// MDF record owner type we are editing + mdf_record_type m_type; }; #endif // CDLGMDFREGISTERBIT_H diff --git a/src/cdlgmdfregisterbitlist.cpp b/src/cdlgmdfregisterbitlist.cpp index 87c3e8ad..a89c5156 100644 --- a/src/cdlgmdfregisterbitlist.cpp +++ b/src/cdlgmdfregisterbitlist.cpp @@ -57,7 +57,8 @@ CDlgMdfRegisterBitList::CDlgMdfRegisterBitList(QWidget* parent) : QDialog(parent) , ui(new Ui::CDlgMdfRegisterBitList) { - m_preg = nullptr; + m_pobj = nullptr; + m_type = mdf_type_unknown; ui->setupUi(this); @@ -68,7 +69,7 @@ CDlgMdfRegisterBitList::CDlgMdfRegisterBitList(QWidget* parent) connect(ui->btnDupRegisterBit, &QToolButton::clicked, this, &CDlgMdfRegisterBitList::dupRegisterBit); connect(ui->btnDelRegisterBit, &QToolButton::clicked, this, &CDlgMdfRegisterBitList::deleteRegisterBit); - connect(ui->listRegisterBit, &QListWidget::doubleClicked, this, &CDlgMdfRegisterBitList::editRegisterBit); + connect(ui->listBits, &QListWidget::doubleClicked, this, &CDlgMdfRegisterBitList::editRegisterBit); this->setFixedSize(this->size()); } @@ -87,23 +88,55 @@ CDlgMdfRegisterBitList::~CDlgMdfRegisterBitList() // void -CDlgMdfRegisterBitList::initDialogData(CMDF_Register* preg) +CDlgMdfRegisterBitList::initDialogData(CMDF_Object* pobj, mdf_record_type type) { QString str; - if (nullptr == preg) { - QMessageBox::critical(this, tr("MDF register bit information"), tr("Invalid MDF register object")); - spdlog::error("MDF register information - Invalid MDF register object"); + if (nullptr == pobj) { + QMessageBox::critical(this, tr("MDF bit information"), tr("Invalid MDF object")); + spdlog::error("MDF bit information - Invalid MDF object"); return; } - // Save register pointer and page - m_preg = preg; + // Save object pointer and type + m_pobj = pobj; + m_type = type; + + // Disable items that are not used + if (mdf_type_register == type) { + setWindowTitle("Register bit definitionsöööö"); + } + else if (mdf_type_remotevar == type) { + setWindowTitle("Remote variable bit definitions"); + } + else if (mdf_type_alarm == type) { + setWindowTitle("Alarm bit definitions"); + } // m_pmdf->getRegisterMap(m_page, pages); renderBitItems(); } +/////////////////////////////////////////////////////////////////////////////// +// getBits +// + +std::deque* +CDlgMdfRegisterBitList::getBits(void) +{ + if (mdf_type_register == m_type) { + return ((CMDF_Register*)m_pobj)->getListBits(); + } + else if (mdf_type_remotevar == m_type) { + return ((CMDF_RemoteVariable*)m_pobj)->getListBits(); + } + else if (mdf_type_alarm == m_type) { + return ((CMDF*)m_pobj)->getAlarmListBits(); + } + + return nullptr; +} + /////////////////////////////////////////////////////////////////////////////// // renderBitItems // @@ -111,14 +144,26 @@ CDlgMdfRegisterBitList::initDialogData(CMDF_Register* preg) void CDlgMdfRegisterBitList::renderBitItems(void) { + std::deque* pbits = nullptr; std::map pages; - if (nullptr == m_preg) { + if (nullptr == m_pobj) { return; } - ui->listRegisterBit->clear(); - std::deque* pbits = m_preg->getListBits(); + ui->listBits->clear(); + if (mdf_type_register == m_type) { + pbits = ((CMDF_Register*)m_pobj)->getListBits(); + } + else if (mdf_type_remotevar == m_type) { + pbits = ((CMDF_RemoteVariable*)m_pobj)->getListBits(); + } + else if (mdf_type_alarm == m_type) { + pbits = ((CMDF*)m_pobj)->getAlarmListBits(); + } + else { + return; + } // If no enteries there is nothing to do if (!pbits->size()) { @@ -130,9 +175,9 @@ CDlgMdfRegisterBitList::renderBitItems(void) CMDF_Bit* pbit = *it; if (nullptr != pbit) { QString str = QString("Bit %1(%2)-- %3").arg(pbit->getPos()).arg(pbit->getWidth()).arg(pbit->getName().c_str()); - ui->listRegisterBit->addItem(str); + ui->listBits->addItem(str); // Set data to register index - ui->listRegisterBit->item(ui->listRegisterBit->count() - 1)->setData(Qt::UserRole, idx); + ui->listBits->item(ui->listBits->count() - 1)->setData(Qt::UserRole, idx); } idx++; } @@ -146,6 +191,8 @@ void CDlgMdfRegisterBitList::addRegisterBit(void) { bool ok; + std::deque* pbits; + CMDF_Bit* pbitnew = new CMDF_Bit(); if (nullptr == pbitnew) { QMessageBox::critical(this, tr("MDF register bit information"), tr("Memory problem")); @@ -154,7 +201,7 @@ CDlgMdfRegisterBitList::addRegisterBit(void) } // Save the selected row - int idx = ui->listRegisterBit->currentRow(); + int idx = ui->listBits->currentRow(); CDlgMdfRegisterBit dlg(this); dlg.initDialogData(pbitnew); @@ -169,7 +216,19 @@ CDlgMdfRegisterBitList::addRegisterBit(void) QMessageBox::warning(this, tr("Add new bit definition"), tr("Can not add bit definition. Bits overlap with already defined bits 0b%1").arg(mask, 8, 2, QChar('0'))); goto addbitdlg; } - std::deque* pbits = m_preg->getListBits(); + if (mdf_type_register == m_type) { + pbits = ((CMDF_Register*)m_pobj)->getListBits(); + } + else if (mdf_type_remotevar == m_type) { + pbits = ((CMDF_RemoteVariable*)m_pobj)->getListBits(); + } + else if (mdf_type_alarm == m_type) { + pbits = ((CMDF*)m_pobj)->getAlarmListBits(); + } + else { + return; + } + pbits->push_back(pbitnew); renderBitItems(); } @@ -186,17 +245,31 @@ void CDlgMdfRegisterBitList::editRegisterBit(void) { bool ok; + CMDF_Bit* pbit = nullptr; - if (-1 != ui->listRegisterBit->currentRow()) { + if (-1 != ui->listBits->currentRow()) { // Save the selected row - int idx = ui->listRegisterBit->currentRow(); + int idx = ui->listBits->currentRow(); - QListWidgetItem* pitem = ui->listRegisterBit->currentItem(); - CMDF_Bit* pbit = m_preg->getListBits()->at(pitem->data(Qt::UserRole).toUInt()); + QListWidgetItem* pitem = ui->listBits->currentItem(); + // CMDF_Bit* pbit = m_preg->getListBits()->at(pitem->data(Qt::UserRole).toUInt()); + + if (mdf_type_register == m_type) { + pbit = ((CMDF_Register*)m_pobj)->getListBits()->at(pitem->data(Qt::UserRole).toUInt()); + } + else if (mdf_type_remotevar == m_type) { + pbit = ((CMDF_RemoteVariable*)m_pobj)->getListBits()->at(pitem->data(Qt::UserRole).toUInt()); + } + else if (mdf_type_alarm == m_type) { + pbit = ((CMDF*)m_pobj)->getAlarmListBits()->at(pitem->data(Qt::UserRole).toUInt()); + } + else { + return; + } CDlgMdfRegisterBit dlg(this); - dlg.initDialogData(pbit); + dlg.initDialogData(pbit, 0, m_type); editbitdlg: @@ -206,9 +279,9 @@ CDlgMdfRegisterBitList::editRegisterBit(void) QMessageBox::warning(this, tr("Edit bit definition"), tr("Can not add bit definition. Bits overlap with already defined bits 0b%1").arg(mask, 8, 2, QChar('0'))); goto editbitdlg; } - ui->listRegisterBit->clear(); + ui->listBits->clear(); renderBitItems(); - ui->listRegisterBit->setCurrentRow(idx); + ui->listBits->setCurrentRow(idx); } } else { @@ -223,12 +296,12 @@ CDlgMdfRegisterBitList::editRegisterBit(void) void CDlgMdfRegisterBitList::dupRegisterBit(void) { - if (-1 != ui->listRegisterBit->currentRow()) { + if (-1 != ui->listBits->currentRow()) { // Save the selected row - int idx = ui->listRegisterBit->currentRow(); + int idx = ui->listBits->currentRow(); - // QListWidgetItem* pitem = ui->listRegisterBit->currentItem(); + // QListWidgetItem* pitem = ui->listBits->currentItem(); // CMDF_Bit* preg = m_pmdf->getRegister(pitem->data(Qt::UserRole).toUInt(), m_page); // CMDF_Bit* pregnew = new CMDF_Bit(); @@ -252,10 +325,10 @@ CDlgMdfRegisterBitList::dupRegisterBit(void) // if (m_page == pregnew->getPage()) { // m_registersSet.insert(pregnew->getOffset()); // } - // ui->listRegisterBit->clear(); + // ui->listBits->clear(); // renderRegisterItems(); // if (-1 != idx) { - // ui->listRegisterBit->setCurrentRow(idx); + // ui->listBits->setCurrentRow(idx); // } // // Warn if page is not the same as for dialog // if (pregnew->getPage() != m_page) { @@ -280,30 +353,52 @@ CDlgMdfRegisterBitList::dupRegisterBit(void) void CDlgMdfRegisterBitList::deleteRegisterBit(void) { - if (-1 != ui->listRegisterBit->currentRow()) { + CMDF_Bit* pbit; + std::deque::iterator it; + std::deque* pbits; + + if (mdf_type_register == m_type) { + pbits = ((CMDF_Register*)m_pobj)->getListBits(); + } + else if (mdf_type_remotevar == m_type) { + pbits = ((CMDF_RemoteVariable*)m_pobj)->getListBits(); + } + else if (mdf_type_alarm == m_type) { + pbits = ((CMDF*)m_pobj)->getAlarmListBits(); + } + else { + return; + } + + if (-1 != ui->listBits->currentRow()) { // Save the row - int idx = ui->listRegisterBit->currentRow(); + int idx = ui->listBits->currentRow(); - QListWidgetItem* pitem = ui->listRegisterBit->currentItem(); - CMDF_Bit* pbit = m_preg->getListBits()->at(pitem->data(Qt::UserRole).toUInt()); + QListWidgetItem* pitem = ui->listBits->currentItem(); + // CMDF_Bit* pbit = m_preg->getListBits()->at(pitem->data(Qt::UserRole).toUInt()); + pbit = pbits->at(pitem->data(Qt::UserRole).toUInt()); - std::deque::iterator it = m_preg->getListBits()->begin() + pitem->data(Qt::UserRole).toUInt(); - m_preg->getListBits()->erase(it); + // std::deque::iterator it = m_preg->getListBits()->begin() + pitem->data(Qt::UserRole).toUInt(); - ui->listRegisterBit->clear(); + it = pbits->begin() + pitem->data(Qt::UserRole).toUInt(); + pbits->erase(it); + + // m_preg->getListBits()->erase(it); + + ui->listBits->clear(); renderBitItems(); int sel = idx; if (0 == idx) { sel = 0; } - else if (m_preg->getListBits()->size() == idx) { - sel = m_preg->getListBits()->size() - 1; + else if (pbits->size() == idx) { + sel = pbits->size() - 1; } else { sel = idx + 1; } - ui->listRegisterBit->setCurrentRow(sel); + ui->listBits->setCurrentRow(sel); } } @@ -315,26 +410,8 @@ void CDlgMdfRegisterBitList::accept() { std::string str; - if (nullptr != m_preg) { - - // str = ui->editName->text().toStdString(); - // m_pmdf->setName(str); - - // str = ui->editModel->text().toStdString(); - // m_pmdf->setModuleModel(str); - - // m_pmdf->setModuleLevel(ui->comboModuleLevel->currentIndex()); - - // str = ui->editVersion->text().toStdString(); - // m_pmdf->setModuleVersion(str); - - // str = ui->editDate->text().toStdString(); - // m_pmdf->setModuleChangeDate(str); - - // m_pmdf->setModuleBufferSize(ui->editBufferSize->value()); - - // str = ui->editCopyright->text().toStdString(); - // m_pmdf->setModuleCopyright(str); + if (nullptr != m_pobj) { + ; } else { spdlog::error("MDF module information - Invalid MDF object (accept)"); diff --git a/src/cdlgmdfregisterbitlist.h b/src/cdlgmdfregisterbitlist.h index 78561e93..05bdc170 100644 --- a/src/cdlgmdfregisterbitlist.h +++ b/src/cdlgmdfregisterbitlist.h @@ -57,7 +57,13 @@ class CDlgMdfRegisterBitList : public QDialog { Init dialog data @param pmdf Pointer to MDF object */ - void initDialogData(CMDF_Register* preg); + void initDialogData(CMDF_Object* preg, mdf_record_type type = mdf_type_register); + + /*! + Get bit list for set type + @return pointer to bitlist or nullpointer if invalid type + */ + std::deque* getBits(void); /*! Fill in register bit items @@ -72,8 +78,23 @@ class CDlgMdfRegisterBitList : public QDialog { */ uint8_t checkIfBitsOverlap(CMDF_Bit* pbit2test, bool bEdit = false) { - uint8_t result = 0; - std::deque* pbits = m_preg->getListBits(); + uint8_t result = 0; + std::deque* pbits; + + // pbits = m_preg->getListBits(); + if (mdf_type_register == m_type) { + pbits = ((CMDF_Register*)m_pobj)->getListBits(); + } + else if (mdf_type_remotevar == m_type) { + pbits = ((CMDF_RemoteVariable*)m_pobj)->getListBits(); + } + else if (mdf_type_alarm == m_type) { + pbits = ((CMDF*)m_pobj)->getAlarmListBits(); + } + else { + return 0; + } + for (auto it = pbits->cbegin(); it != pbits->cend(); ++it) { CMDF_Bit* pbit = *it; // Don't test the edited bit of in edit mode @@ -111,7 +132,11 @@ public slots: Ui::CDlgMdfRegisterBitList* ui; // MDF - CMDF_Register* m_preg; + CMDF_Object* m_pobj; + // CMDF_Register* m_preg; + + // Object type + mdf_record_type m_type; }; #endif // CDLGMDFREGISTERBITLIST_H diff --git a/src/cdlgmdfregisterbitlist.ui b/src/cdlgmdfregisterbitlist.ui index cd294b2d..1ef8b87f 100644 --- a/src/cdlgmdfregisterbitlist.ui +++ b/src/cdlgmdfregisterbitlist.ui @@ -47,7 +47,7 @@ - + diff --git a/src/cfrmmdf.cpp b/src/cfrmmdf.cpp index dd1b7015..0552a369 100644 --- a/src/cfrmmdf.cpp +++ b/src/cfrmmdf.cpp @@ -67,8 +67,8 @@ #include "cdlgmdfregisterlist.h" #include "cdlgmdfregistervalue.h" #include "cdlgmdfregistervaluelist.h" -#include "cdlgmdfremotevarlist.h" #include "cdlgmdfremotevar.h" +#include "cdlgmdfremotevarlist.h" #include #include @@ -443,7 +443,7 @@ CFrmMdf::showMdfContextMenu(const QPoint& pos) break; case mdf_type_alarm: - menu->addAction(QString(tr("Alarm")), this, SLOT(loadSelectedMdf())); + //menu->addAction(QString(tr("Edit alarm bits")), this, SLOT(editRegisterBit())); break; case mdf_type_address: @@ -1269,7 +1269,6 @@ CFrmMdf::renderRemoteVariables(QTreeWidgetItem* pParent) } } - /////////////////////////////////////////////////////////////////////////////// // renderManufacturerEmail // @@ -5559,6 +5558,24 @@ CFrmMdf::deleteRegister(void) // m_headFileSetupScript->removeChild(pItemHead); } +mdf_record_type +getTopParentType(QTreeWidgetItem* pcurrentitem) +{ + mdf_record_type type = mdf_type_unknown; + QTreeWidgetItem* pItem = pcurrentitem; + + while (nullptr != pItem) { + type = ((QMdfTreeWidgetItem*)pItem)->getObjectType(); + qDebug() << static_cast(((QMdfTreeWidgetItem*)pItem)->getObjectType()); + pItem = pItem->parent(); + if ((nullptr != pItem) && (1 == ((QMdfTreeWidgetItem*)pItem)->getObjectType())) { + return type; + } + } + + return static_cast(0); +} + /////////////////////////////////////////////////////////////////////////////// // editRegisterBit // @@ -5566,9 +5583,14 @@ CFrmMdf::deleteRegister(void) void CFrmMdf::editRegisterBit(void) { - QMdfTreeWidgetItem* pItem = (QMdfTreeWidgetItem*)ui->treeMDF->currentItem(); - QMdfTreeWidgetItem* pItemHead = (QMdfTreeWidgetItem*)pItem->parent(); - uint16_t selectedIndex = pItem->getElementIndex(); + QMdfTreeWidgetItem* pItem = (QMdfTreeWidgetItem*)ui->treeMDF->currentItem(); + if (nullptr == pItem) return; + QMdfTreeWidgetItem* pItemHead = (nullptr != pItem) ? (QMdfTreeWidgetItem*)pItem->parent() : nullptr; + if (nullptr == pItemHead) return; + QMdfTreeWidgetItem* pItemHeadHead = (nullptr != pItemHead) ? (QMdfTreeWidgetItem*)pItemHead->parent() : nullptr; + if (nullptr == pItemHeadHead) return; + QMdfTreeWidgetItem* pItemHeadHeadHead = (nullptr != pItemHeadHead) ? (QMdfTreeWidgetItem*)pItemHeadHead->parent() : nullptr; + uint16_t selectedIndex = pItem->getElementIndex(); // Item must be selected if (nullptr == pItem) { @@ -5583,22 +5605,64 @@ CFrmMdf::editRegisterBit(void) return; } + bool bAlarm = false; + if ((nullptr != pItemHead) && (mdf_type_alarm == pItemHead->getObjectType())) { + bAlarm = true; + } + + if ((nullptr != pItemHeadHead) && (mdf_type_alarm == pItemHeadHead->getObjectType())) { + bAlarm = true; + } + switch (pItem->getObjectType()) { case mdf_type_bit: { - CMDF_Register* preg = (CMDF_Register*)pItem->getObject(); - CDlgMdfRegisterBitList dlg(this); - dlg.initDialogData(preg); - if (QDialog::Accepted == dlg.exec()) { - // Redraw all rbit items - We do not know changes - QList childrenList = pItem->takeChildren(); - // Remove children - for (qsizetype i = 0; i < childrenList.size(); ++i) { - QMdfTreeWidgetItem* item = (QMdfTreeWidgetItem*)childrenList.at(i); - delete item; + if ((nullptr != pItemHeadHeadHead) && (mdf_type_register == pItemHeadHeadHead->getObjectType())) { + CMDF_Register* preg = (CMDF_Register*)pItem->getObject(); + CDlgMdfRegisterBitList dlg(this); + dlg.initDialogData(preg, mdf_type_register); + if (QDialog::Accepted == dlg.exec()) { + // Redraw all bit items - We do not know changes + QList childrenList = pItem->takeChildren(); + // Remove children + for (qsizetype i = 0; i < childrenList.size(); ++i) { + QMdfTreeWidgetItem* item = (QMdfTreeWidgetItem*)childrenList.at(i); + delete item; + } + childrenList.clear(); + renderBits(pItem, *preg->getListBits(), true); + } + } + else if ((nullptr != pItemHeadHead) && (mdf_type_remotevar == pItemHeadHead->getObjectType())) { + CMDF_RemoteVariable* prvar = (CMDF_RemoteVariable*)pItem->getObject(); + CDlgMdfRegisterBitList dlg(this); + dlg.initDialogData(prvar, mdf_type_remotevar); + if (QDialog::Accepted == dlg.exec()) { + // Redraw all bit items - We do not know changes + QList childrenList = pItem->takeChildren(); + // Remove children + for (qsizetype i = 0; i < childrenList.size(); ++i) { + QMdfTreeWidgetItem* item = (QMdfTreeWidgetItem*)childrenList.at(i); + delete item; + } + childrenList.clear(); + renderBits(pItem, *prvar->getListBits(), true); + } + } + else if ((nullptr != pItemHead) && (mdf_type_alarm == pItemHead->getObjectType())) { + CDlgMdfRegisterBitList dlg(this); + dlg.initDialogData(&m_mdf, mdf_type_alarm); + if (QDialog::Accepted == dlg.exec()) { + // Redraw all bit items - We do not know changes + QList childrenList = pItem->takeChildren(); + // Remove children + for (qsizetype i = 0; i < childrenList.size(); ++i) { + QMdfTreeWidgetItem* item = (QMdfTreeWidgetItem*)childrenList.at(i); + delete item; + } + childrenList.clear(); + renderBits(pItem, *m_mdf.getAlarmList(), true); } - childrenList.clear(); - renderBits(pItem, *preg->getListBits(), true); } } break; @@ -5610,7 +5674,7 @@ CFrmMdf::editRegisterBit(void) return; } CDlgMdfRegisterBit dlg(this); - dlg.initDialogData(pbit, 0); + dlg.initDialogData(pbit, 0, getTopParentType(pItem)); if (QDialog::Accepted == dlg.exec()) { pItemHead->setExpanded(true); QList childrenList = pItem->takeChildren(); @@ -5639,7 +5703,7 @@ CFrmMdf::editRegisterBit(void) return; } CDlgMdfRegisterBit dlg(this); - dlg.initDialogData(pbit, selectedIndex); + dlg.initDialogData(pbit, selectedIndex, getTopParentType(pItem)); if (QDialog::Accepted == dlg.exec()) { pItemHead->setExpanded(true); QList childrenList = pItemHead->takeChildren();