From de83dc6a0c019da30bb214ba6d43be8fd3c478d3 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Wed, 30 Oct 2024 20:33:33 -0400 Subject: [PATCH] Fix for DBC editor bugs that crept in during conversion to qt6 --- dbc/dbchandler.cpp | 12 ++++++++ dbc/dbchandler.h | 1 + dbc/dbcmaineditor.cpp | 60 ++++++++++++++++++++++++++++-------- dbc/dbcmaineditor.h | 2 +- dbc/dbcmessageeditor.cpp | 15 ++++++--- dbc/dbcmessageeditor.h | 3 +- dbc/dbcnodeduplicateeditor.h | 2 +- dbc/dbcnoderebaseeditor.cpp | 2 +- dbc/dbcnoderebaseeditor.h | 2 +- 9 files changed, 77 insertions(+), 22 deletions(-) diff --git a/dbc/dbchandler.cpp b/dbc/dbchandler.cpp index 9f2cc0d4..27c359c4 100644 --- a/dbc/dbchandler.cpp +++ b/dbc/dbchandler.cpp @@ -173,6 +173,18 @@ bool DBCMessageHandler::addMessage(DBC_MESSAGE *msg) return true; } +bool DBCMessageHandler::changeMessageID(DBC_MESSAGE *msg, quint32 oldID) +{ + //remove existing entry, add new entry with proper ID mapping + if (messages.contains(oldID)) + { + messages.insert(msg->ID, msg); + messages.remove(oldID); + return true; + } + return false; +} + bool DBCMessageHandler::removeMessage(uint32_t ID) { if (messages.count() == 0) return false; diff --git a/dbc/dbchandler.h b/dbc/dbchandler.h index 32c6f517..5295df80 100644 --- a/dbc/dbchandler.h +++ b/dbc/dbchandler.h @@ -42,6 +42,7 @@ class DBCMessageHandler: public QObject QList findMsgsByNode(DBC_NODE *node); QList getMsgsAsList(); bool addMessage(DBC_MESSAGE *msg); + bool changeMessageID(DBC_MESSAGE *msg, quint32 oldID); bool removeMessage(uint32_t ID); bool removeMessage(QString name); void removeAllMessages(); diff --git a/dbc/dbcmaineditor.cpp b/dbc/dbcmaineditor.cpp index 6e01ec05..d33dff29 100644 --- a/dbc/dbcmaineditor.cpp +++ b/dbc/dbcmaineditor.cpp @@ -283,6 +283,7 @@ void DBCMainEditor::onTreeDoubleClicked(const QModelIndex &index) idString = firstCol->text(0).split(" ")[0]; msgID = static_cast(Utility::ParseStringToNum(idString)); msg = dbcFile->messageHandler->findMsgByID(msgID); + //msg = itemToMessage[firstCol]; msgEditor->setMessageRef(msg); msgEditor->setFileIdx(fileIdx); //msgEditor->setWindowModality(Qt::WindowModal); @@ -296,6 +297,7 @@ void DBCMainEditor::onTreeDoubleClicked(const QModelIndex &index) if (nameString.contains("(")) nameString = nameString.split(" ")[1]; else nameString = nameString.split(" ")[0]; sig = msg->sigHandler->findSignalByName(nameString); + //sig = itemToSignal[firstCol]; if (sig) { sigEditor->setSignalRef(sig); @@ -494,8 +496,14 @@ void DBCMainEditor::updatedNode(DBC_NODE *node) else qDebug() << "That node doesn't exist. That's a bug dude."; } -void DBCMainEditor::updatedMessage(DBC_MESSAGE *msg) +void DBCMainEditor::updatedMessage(DBC_MESSAGE *msg, quint32 orig_ID) { + //if ID was changed then the messagehandler must be told to update its QMap as well. + if (msg->ID != orig_ID) + { + dbcFile->messageHandler->changeMessageID(msg, orig_ID); + } + if (messageToItem.contains(msg)) { QTreeWidgetItem *item = messageToItem.value(msg); @@ -675,7 +683,7 @@ void DBCMainEditor::newMessage() nodeItem = msgItem->parent(); } if (typ == DBCItemTypes::NODE){ - msgItem = nodeItem; + msgItem = nullptr; //we have no message, just a node selected } //if there was a comment this will find the location of the comment and snip it out. @@ -709,7 +717,7 @@ void DBCMainEditor::newMessage() else { msgPtr->name = nodeName + "Msg" + QString::number(randGen.bounded(500)); - msgPtr->ID = 0; + msgPtr->ID = 0x003; msgPtr->len = 8; msgPtr->bgColor = QApplication::palette().color(QPalette::Base); } @@ -717,7 +725,7 @@ void DBCMainEditor::newMessage() else { msgPtr->name = nodeName + "Msg" + QString::number(randGen.bounded(500)); - msgPtr->ID = 0; + msgPtr->ID = 0x004; msgPtr->len = 8; } msgPtr->sender = node; @@ -743,9 +751,17 @@ void DBCMainEditor::newSignal() QTreeWidgetItem *parentItem = nullptr; msgItem = ui->treeDBC->currentItem(); parentItem = msgItem; - if (!msgItem) return; //nothing selected! + if (!msgItem) + { + qDebug() << "Nothing selected!"; + return; //nothing selected! + } int typ = msgItem->data(0, Qt::UserRole).toInt(); - if (typ == DBCItemTypes::NODE) return; //can't add signals to a node! + if (typ == DBCItemTypes::NODE) + { + qDebug() << "Can't add signals to a node!"; + return; //can't add signals to a node! + } if (typ == DBCItemTypes::SIG) { sigItem = msgItem; @@ -753,13 +769,23 @@ void DBCMainEditor::newSignal() parentItem = msgItem; //walk up the tree to find the parent msg while (msgItem && msgItem->data(0, Qt::UserRole).toInt() != DBCItemTypes::MESG) msgItem = msgItem->parent(); - if (!msgItem) return; //something bad happened. abort. + if (!msgItem) + { + qDebug() << "Could not find parent message to attach signal to!"; + return; //something bad happened. abort. + } } QString idString = msgItem->text(0).split(" ")[0]; int msgID = static_cast(Utility::ParseStringToNum(idString)); - DBC_MESSAGE *msg = dbcFile->messageHandler->findMsgByID(msgID); - if (!msg) return; //null pointers are a bummer. Do not follow them. + DBCMessageHandler *msgHandler = dbcFile->messageHandler; + + DBC_MESSAGE *msg = msgHandler->findMsgByID(msgID); + if (!msg) + { + qDebug() << "Could not find data structure for message 0x" << QString::number(msgID, 16) << ". Aborting!"; + return; //null pointers are a bummer. Do not follow them. + } DBC_SIGNAL *sigPtr = new DBC_SIGNAL; if (sigItem) { @@ -841,8 +867,8 @@ void DBCMainEditor::deleteCurrentTreeItem() qDebug() << "Could not find the node in the map. That should not happen."; } } - break; + case DBCItemTypes::MESG: //cascades to removing all signals too. idString = currItem->text(0).split(" ")[0]; msgID = static_cast(Utility::ParseStringToNum(idString)); @@ -864,6 +890,7 @@ void DBCMainEditor::deleteCurrentTreeItem() } } break; + case DBCItemTypes::SIG: //no cascade, just this one signal. confirmDialog = QMessageBox::question(this, "Really?", "Are you sure you want to delete this signal?", QMessageBox::Yes|QMessageBox::No); @@ -957,13 +984,22 @@ void DBCMainEditor::deleteMessage(DBC_MESSAGE *msg) void DBCMainEditor::deleteSignal(DBC_SIGNAL *sig) { qDebug() << "Signal about to vanish."; - if (!signalToItem.contains(sig)) return; + if (!signalToItem.contains(sig)) + { + qDebug() << "Could not find signal in signalToItem collection! Aborting!"; + return; + } QTreeWidgetItem *currItem = signalToItem[sig]; + if (!currItem) + { + qDebug() << "currItem was null in deleteSignal. Aborting!"; + return; + } sig->parentMessage->sigHandler->removeSignal(sig->name); itemToSignal.remove(currItem); signalToItem.remove(sig); ui->treeDBC->removeItemWidget(currItem, 0); - //delete currItem; //already removed by above remove call + delete currItem; //should have been removed above but do we need to call this anyway?! dbcFile->setDirtyFlag(); } diff --git a/dbc/dbcmaineditor.h b/dbc/dbcmaineditor.h index 75780ea7..08ff354d 100644 --- a/dbc/dbcmaineditor.h +++ b/dbc/dbcmaineditor.h @@ -36,7 +36,7 @@ class DBCMainEditor : public QDialog public slots: void updatedNode(DBC_NODE *node); - void updatedMessage(DBC_MESSAGE *msg); + void updatedMessage(DBC_MESSAGE *msg, quint32 orig_id); void updatedSignal(DBC_SIGNAL *sig); private slots: diff --git a/dbc/dbcmessageeditor.cpp b/dbc/dbcmessageeditor.cpp index 4fbd3a09..e42ac04b 100644 --- a/dbc/dbcmessageeditor.cpp +++ b/dbc/dbcmessageeditor.cpp @@ -26,7 +26,7 @@ DBCMessageEditor::DBCMessageEditor(QWidget *parent) : if (suppressEditCallbacks) return; if (dbcMessage->comment != ui->lineComment->text()) dbcFile->setDirtyFlag(); dbcMessage->comment = ui->lineComment->text(); - emit updatedTreeInfo(dbcMessage); + emit updatedTreeInfo(dbcMessage, origID); }); connect(ui->lineFrameID, &QLineEdit::editingFinished, @@ -36,7 +36,11 @@ DBCMessageEditor::DBCMessageEditor(QWidget *parent) : if (suppressEditCallbacks) return; if ((dbcMessage->ID & 0x1FFFFFFFul) != Utility::ParseStringToNum(ui->lineFrameID->text())) dbcFile->setDirtyFlag(); dbcMessage->ID = Utility::ParseStringToNum(ui->lineFrameID->text()); - emit updatedTreeInfo(dbcMessage); + + if (dbcMessage->ID > 0x7FF) dbcMessage->extendedID = true; + else dbcMessage->extendedID = false; + + emit updatedTreeInfo(dbcMessage, origID); }); connect(ui->lineMsgName, &QLineEdit::editingFinished, @@ -46,7 +50,7 @@ DBCMessageEditor::DBCMessageEditor(QWidget *parent) : if (suppressEditCallbacks) return; if (dbcMessage->name != ui->lineMsgName->text().simplified().replace(' ', '_')) dbcFile->setDirtyFlag(); dbcMessage->name = ui->lineMsgName->text().simplified().replace(' ', '_'); - emit updatedTreeInfo(dbcMessage); + emit updatedTreeInfo(dbcMessage, origID); }); connect(ui->lineFrameLen, &QLineEdit::editingFinished, @@ -67,7 +71,7 @@ DBCMessageEditor::DBCMessageEditor(QWidget *parent) : if (!node) return; if (node != dbcMessage->sender) dbcFile->setDirtyFlag(); dbcMessage->sender = node; - emit updatedTreeInfo(dbcMessage); + emit updatedTreeInfo(dbcMessage, origID); }); connect(ui->comboSender->lineEdit(), &QLineEdit::editingFinished, @@ -87,7 +91,7 @@ DBCMessageEditor::DBCMessageEditor(QWidget *parent) : } if (node != dbcMessage->sender) dbcFile->setDirtyFlag(); dbcMessage->sender = node; - emit updatedTreeInfo(dbcMessage); + emit updatedTreeInfo(dbcMessage, origID); }); connect(ui->btnTextColor, &QAbstractButton::clicked, @@ -206,6 +210,7 @@ void DBCMessageEditor::writeSettings() void DBCMessageEditor::setMessageRef(DBC_MESSAGE *msg) { dbcMessage = msg; + origID = msg->ID; } void DBCMessageEditor::showEvent(QShowEvent* event) diff --git a/dbc/dbcmessageeditor.h b/dbc/dbcmessageeditor.h index 473b54d0..f4e8291b 100644 --- a/dbc/dbcmessageeditor.h +++ b/dbc/dbcmessageeditor.h @@ -22,7 +22,7 @@ class DBCMessageEditor : public QDialog void refreshView(); signals: - void updatedTreeInfo(DBC_MESSAGE *msg); + void updatedTreeInfo(DBC_MESSAGE *msg, quint32 orig_id); private: Ui::DBCMessageEditor *ui; @@ -31,6 +31,7 @@ class DBCMessageEditor : public QDialog DBC_MESSAGE *dbcMessage; DBCFile *dbcFile; bool suppressEditCallbacks; + quint32 origID; void closeEvent(QCloseEvent *event); bool eventFilter(QObject *obj, QEvent *event); diff --git a/dbc/dbcnodeduplicateeditor.h b/dbc/dbcnodeduplicateeditor.h index 57610bde..43202c92 100644 --- a/dbc/dbcnodeduplicateeditor.h +++ b/dbc/dbcnodeduplicateeditor.h @@ -22,7 +22,7 @@ class DBCNodeDuplicateEditor : public QDialog bool refreshView(); signals: - void updatedTreeInfo(DBC_MESSAGE *msg); + void updatedTreeInfo(DBC_MESSAGE *msg, quint32 origID); void createNode(QString nodeName); void cloneMessageToNode(DBC_NODE *parentNode, DBC_MESSAGE *source, uint newMsgId); void nodeAdded(); diff --git a/dbc/dbcnoderebaseeditor.cpp b/dbc/dbcnoderebaseeditor.cpp index 8d037f0f..29456aae 100644 --- a/dbc/dbcnoderebaseeditor.cpp +++ b/dbc/dbcnoderebaseeditor.cpp @@ -77,7 +77,7 @@ DBCNodeRebaseEditor::DBCNodeRebaseEditor(QWidget *parent) : for (int i = 0; i < messagesForNode.count(); i++) { messagesForNode[i]->ID += rebaseDiff; - emit updatedTreeInfo(messagesForNode[i]); + emit updatedTreeInfo(messagesForNode[i], messagesForNode[i]->ID); } dbcFile->setDirtyFlag(); diff --git a/dbc/dbcnoderebaseeditor.h b/dbc/dbcnoderebaseeditor.h index 26f16452..68d297bc 100644 --- a/dbc/dbcnoderebaseeditor.h +++ b/dbc/dbcnoderebaseeditor.h @@ -22,7 +22,7 @@ class DBCNodeRebaseEditor : public QDialog bool refreshView(); signals: - void updatedTreeInfo(DBC_MESSAGE *msg); + void updatedTreeInfo(DBC_MESSAGE *msg, quint32 origID); private: Ui::DBCNodeRebaseEditor *ui;