From 47d63c6296e0bfc7f4307bee2fea6ee4993fdd43 Mon Sep 17 00:00:00 2001 From: Andy Huska Date: Thu, 22 Sep 2022 18:04:27 -0500 Subject: [PATCH] duplicate works! multiplexed signals are not copied at this time need to learn more about them first need to do clean up and corner case handling --- SavvyCAN.pro | 4 + dbc/dbcmaineditor.cpp | 105 +++++++++++++++++++- dbc/dbcmaineditor.h | 4 + dbc/dbcnodeduplicateeditor.cpp | 171 +++++++++++++++++++++++++++++++++ dbc/dbcnodeduplicateeditor.h | 45 +++++++++ dbc/dbcnoderebaseeditor.cpp | 9 +- ui/dbcnodeduplicateeditor.ui | 75 +++++++++++++++ ui/dbcnoderebaseeditor.ui | 2 +- 8 files changed, 409 insertions(+), 6 deletions(-) create mode 100644 dbc/dbcnodeduplicateeditor.cpp create mode 100644 dbc/dbcnodeduplicateeditor.h create mode 100644 ui/dbcnodeduplicateeditor.ui diff --git a/SavvyCAN.pro b/SavvyCAN.pro index 0a7094b..b8f445b 100644 --- a/SavvyCAN.pro +++ b/SavvyCAN.pro @@ -21,6 +21,7 @@ ICON = icons/SavvyIcon.icns SOURCES += main.cpp\ connections/mqtt_bus.cpp \ + dbc/dbcnodeduplicateeditor.cpp \ mqtt/qmqtt_client.cpp \ mqtt/qmqtt_client_p.cpp \ mqtt/qmqtt_frame.cpp \ @@ -101,6 +102,7 @@ HEADERS += mainwindow.h \ canframemodel.h \ connections/socketcand.h \ connections/mqtt_bus.h \ + dbc/dbcnodeduplicateeditor.h \ dbc/dbcnoderebaseeditor.h \ mqtt/qmqtt.h \ mqtt/qmqtt_client.h \ @@ -185,11 +187,13 @@ HEADERS += mainwindow.h \ pcaplite.h FORMS += ui/candatagrid.ui \ + ui/dbcnodeduplicateeditor.ui \ ui/dbccomparatorwindow.ui \ ui/dbcmessageeditor.ui \ ui/connectionwindow.ui \ ui/dbcloadsavewindow.ui \ ui/dbcmaineditor.ui \ + ui/dbcnodeduplicateeditor.ui \ ui/dbcnoderebaseeditor.ui \ ui/dbcsignaleditor.ui \ ui/dbcnodeeditor.ui \ diff --git a/dbc/dbcmaineditor.cpp b/dbc/dbcmaineditor.cpp index 88e3753..2415bd5 100644 --- a/dbc/dbcmaineditor.cpp +++ b/dbc/dbcmaineditor.cpp @@ -31,7 +31,7 @@ DBCMainEditor::DBCMainEditor( const QVector *frames, QWidget *parent) connect(ui->treeDBC, &QTreeWidget::customContextMenuRequested, this, &DBCMainEditor::onTreeContextMenu); connect(ui->treeDBC, &QTreeWidget::currentItemChanged, this, &DBCMainEditor::currentItemChanged); connect(ui->btnDelete, &QAbstractButton::clicked, this, &DBCMainEditor::deleteCurrentTreeItem); - connect(ui->btnNewNode, &QAbstractButton::clicked, this, &DBCMainEditor::newNode); + connect(ui->btnNewNode, &QAbstractButton::clicked, this, QOverload<>::of(&DBCMainEditor::newNode)); connect(ui->btnNewMessage, &QAbstractButton::clicked, this, &DBCMainEditor::newMessage); connect(ui->btnNewSignal, &QAbstractButton::clicked, this, &DBCMainEditor::newSignal); @@ -39,12 +39,17 @@ DBCMainEditor::DBCMainEditor( const QVector *frames, QWidget *parent) msgEditor = new DBCMessageEditor(this); nodeEditor = new DBCNodeEditor(this); nodeRebaseEditor = new DBCNodeRebaseEditor(this); + nodeDuplicateEditor = new DBCNodeDuplicateEditor(this); //all three might potentially change the data stored and force the tree to be updated connect(sigEditor, &DBCSignalEditor::updatedTreeInfo, this, &DBCMainEditor::updatedSignal); connect(msgEditor, &DBCMessageEditor::updatedTreeInfo, this, &DBCMainEditor::updatedMessage); connect(nodeEditor, &DBCNodeEditor::updatedTreeInfo, this, &DBCMainEditor::updatedNode); connect(nodeRebaseEditor, &DBCNodeRebaseEditor::updatedTreeInfo, this, &DBCMainEditor::updatedMessage); + connect(nodeDuplicateEditor, &DBCNodeDuplicateEditor::updatedTreeInfo, this, &DBCMainEditor::updatedMessage); + connect(nodeDuplicateEditor, &DBCNodeDuplicateEditor::createNode, this, QOverload::of(&DBCMainEditor::newNode)); + connect(nodeDuplicateEditor, &DBCNodeDuplicateEditor::cloneMessageToNode, this, &DBCMainEditor::copyMessageToNode); + connect(nodeDuplicateEditor, &DBCNodeDuplicateEditor::nodeAdded, this, &DBCMainEditor::refreshTree); nodeIcon = QIcon(":/icons/images/node.png"); messageIcon = QIcon(":/icons/images/message.png"); @@ -360,7 +365,21 @@ void DBCMainEditor::onRebaseMessages() void DBCMainEditor::onDuplicateNode() { - qDebug() << "dupe!!"; + QTreeWidgetItem* firstCol = ui->treeDBC->currentItem(); + bool ret = false; + DBC_MESSAGE *msg; + DBC_SIGNAL *sig; + DBC_NODE *node; + uint32_t msgID; + QString idString; + + idString = firstCol->text(0).split(" ")[0]; + node = dbcFile->findNodeByName(idString); + nodeDuplicateEditor->setFileIdx(fileIdx); + nodeDuplicateEditor->setNodeRef(node); + nodeDuplicateEditor->refreshView(); + nodeDuplicateEditor->setModal(true); + nodeDuplicateEditor->show(); } /* @@ -545,11 +564,18 @@ void DBCMainEditor::updatedSignal(DBC_SIGNAL *sig) else qDebug() << "That signal doesn't exist. That's a bug dude."; } -void DBCMainEditor::newNode() +void DBCMainEditor::newNode(QString nodeName) { DBC_NODE node; DBC_NODE *nodePtr; - node.name = "Unnamed" + QString::number(randGen.bounded(50000)); + if(nodeName.isEmpty()) + { + node.name = "Unnamed" + QString::number(randGen.bounded(50000)); + } + else + { + node.name = nodeName; + } dbcFile->dbc_nodes.append(node); nodePtr = dbcFile->findNodeByName(node.name); QTreeWidgetItem *nodeItem = new QTreeWidgetItem(); @@ -563,6 +589,77 @@ void DBCMainEditor::newNode() dbcFile->setDirtyFlag(); } +void DBCMainEditor::newNode() +{ + newNode(QString()); +} + +void DBCMainEditor::copyMessageToNode(DBC_NODE *parentNode, DBC_MESSAGE *source, uint newMsgId) +{ + DBC_NODE *node = parentNode; + if (!node) node = dbcFile->findNodeByIdx(0); + QTreeWidgetItem *nodeItem = nullptr; + DBC_MESSAGE msg; + DBC_MESSAGE *msgPtr; + + nodeItem = ui->treeDBC->currentItem(); + + msg.name = source->name; + msg.ID = newMsgId; + msg.len = source->len; + msg.bgColor = source->bgColor; + msg.fgColor = source->fgColor; + msg.comment = source->comment; + msg.sender = node; + + DBC_SIGNAL *sigSource; + int sigCount = source->sigHandler->getCount(); + + for(int i=0; isigHandler->findSignalByIdx(i); + + DBC_SIGNAL sig; + + //Does not properly handle multiplexed signals, for now + sig.name = sigSource->name; + sig.bias = sigSource->bias; + sig.isMultiplexed = false; //sigSource->isMultiplexed; + sig.isMultiplexor = false; //sigSource->isMultiplexor; + sig.max = sigSource->max; + sig.min = sigSource->min; + sig.multiplexLowValue = sigSource->multiplexLowValue; + sig.multiplexHighValue = sigSource->multiplexHighValue; + sig.factor = sigSource->factor; + sig.intelByteOrder = sigSource->intelByteOrder; + sig.parentMessage = &msg; + sig.multiplexParent = nullptr; //need to learn about multiplexed signals and track them when copying + sig.receiver = node; + sig.signalSize = sigSource->signalSize; + sig.startBit = sigSource->startBit; + sig.valType = sigSource->valType; + + + sig.parentMessage = &msg; + msg.sigHandler->addSignal(sig); + } + + msg.sigHandler->sort(); + + dbcFile->messageHandler->addMessage(msg); + msgPtr = dbcFile->messageHandler->findMsgByIdx(dbcFile->messageHandler->getCount() - 1); + QTreeWidgetItem *newMsgItem = new QTreeWidgetItem(); + QString msgInfo = Utility::formatCANID(msg.ID) + " " + msg.name; + if (msg.comment.count() > 0) msgInfo.append(" - ").append(msg.comment); + newMsgItem->setText(0, msgInfo); + newMsgItem->setIcon(0, messageIcon); + newMsgItem->setData(0, Qt::UserRole, 2); + messageToItem.insert(msgPtr, newMsgItem); + itemToMessage.insert(newMsgItem, msgPtr); + nodeItem->addChild(newMsgItem); + //ui->treeDBC->setCurrentItem(newMsgItem); + dbcFile->setDirtyFlag(); +} //create a new message with it's parent being the node we're currently within void DBCMainEditor::newMessage() diff --git a/dbc/dbcmaineditor.h b/dbc/dbcmaineditor.h index 62f0876..5977f82 100644 --- a/dbc/dbcmaineditor.h +++ b/dbc/dbcmaineditor.h @@ -11,6 +11,7 @@ #include "dbcmessageeditor.h" #include "dbcnodeeditor.h" #include "dbcnoderebaseeditor.h" +#include "dbcnodeduplicateeditor.h" #include "utility.h" namespace Ui { @@ -43,7 +44,9 @@ private slots: void handleSearch(); void handleSearchForward(); void handleSearchBackward(); + void newNode(QString nodeName); void newNode(); + void copyMessageToNode(DBC_NODE *node, DBC_MESSAGE *source, uint newMsgId); void newMessage(); void newSignal(); void onRebaseMessages(); @@ -57,6 +60,7 @@ private: DBCMessageEditor *msgEditor; DBCNodeEditor *nodeEditor; DBCNodeRebaseEditor *nodeRebaseEditor; + DBCNodeDuplicateEditor *nodeDuplicateEditor; DBCFile *dbcFile; int fileIdx; QIcon nodeIcon; diff --git a/dbc/dbcnodeduplicateeditor.cpp b/dbc/dbcnodeduplicateeditor.cpp new file mode 100644 index 0000000..c2a3164 --- /dev/null +++ b/dbc/dbcnodeduplicateeditor.cpp @@ -0,0 +1,171 @@ +#include "dbcnodeduplicateeditor.h" +#include "ui_dbcnodeduplicateeditor.h" + +#include +#include +#include +#include "helpwindow.h" +#include "utility.h" + +DBCNodeDuplicateEditor::DBCNodeDuplicateEditor(QWidget *parent) : + QDialog(parent), + ui(new Ui::DBCNodeDuplicateEditor) +{ + ui->setupUi(this); + + readSettings(); + + dbcHandler = DBCHandler::getReference(); + dbcNode = nullptr; + + connect(ui->btnDuplicate, &QPushButton::pressed, + [=]() + { + if (dbcNode == nullptr) return; + if (lowestMsgId > 0x1FFFFFFFul) return; + + uint newBase = Utility::ParseStringToNum(ui->lineNewBaseId->text()); + + if (newBase <= 0x1FFFFFFFul && newBase != lowestMsgId) + { + uint rebaseDiff = newBase - lowestMsgId; + + QList messagesForNode = dbcFile->messageHandler->findMsgsByNode(dbcNode); + if(messagesForNode.count() == 0) + { + return; + } + + if(ui->lineNodeName->text().isEmpty()) + { + //tell! + return; + } + + QString newNodeName = ui->lineNodeName->text(); + emit createNode(newNodeName); + + DBC_NODE *nodePtr = dbcFile->findNodeByName(newNodeName); + + if(nodePtr == nullptr) + { + //uhoh + return; + } + + for (int i=0; iID + rebaseDiff; + emit cloneMessageToNode(nodePtr, messagesForNode[i], newMsgId); + } + + dbcFile->setDirtyFlag(); + emit nodeAdded(); + } + }); + + connect(ui->btnCancel, &QPushButton::pressed, + [=]() + { + + + }); + + installEventFilter(this); +} + +DBCNodeDuplicateEditor::~DBCNodeDuplicateEditor() +{ + removeEventFilter(this); + delete ui; +} + +void DBCNodeDuplicateEditor::closeEvent(QCloseEvent *event) +{ + Q_UNUSED(event); + writeSettings(); +} + +bool DBCNodeDuplicateEditor::eventFilter(QObject *obj, QEvent *event) +{ + if (event->type() == QEvent::KeyRelease) { + QKeyEvent *keyEvent = static_cast(event); + switch (keyEvent->key()) + { + case Qt::Key_F1: + HelpWindow::getRef()->showHelp("nodeeditor.md"); + break; + } + return true; + } else { + // standard event processing + return QObject::eventFilter(obj, event); + } + return false; +} + +void DBCNodeDuplicateEditor::setFileIdx(int idx) +{ + if (idx < 0 || idx > dbcHandler->getFileCount() - 1) return; + dbcFile = dbcHandler->getFileByIdx(idx); +} + +void DBCNodeDuplicateEditor::readSettings() +{ + QSettings settings; + if (settings.value("Main/SaveRestorePositions", false).toBool()) + { + resize(settings.value("DBCNodeDuplicateEditor/WindowSize", QSize(312, 128)).toSize()); + move(Utility::constrainedWindowPos(settings.value("DBCNodeDuplicateEditor/WindowPos", QPoint(100, 100)).toPoint())); + } +} + +void DBCNodeDuplicateEditor::writeSettings() +{ + QSettings settings; + + if (settings.value("Main/SaveRestorePositions", false).toBool()) + { + settings.setValue("DBCNodeDuplicateEditor/WindowSize", size()); + settings.setValue("DBCNodeDuplicateEditor/WindowPos", pos()); + } +} + + +void DBCNodeDuplicateEditor::setNodeRef(DBC_NODE *node) +{ + dbcNode = node; +} + +void DBCNodeDuplicateEditor::showEvent(QShowEvent* event) +{ + QDialog::showEvent(event); + + refreshView(); +} + +void DBCNodeDuplicateEditor::refreshView() +{ + if(dbcNode) + { + QList messagesForNode = dbcFile->messageHandler->findMsgsByNode(dbcNode); + if(messagesForNode.count() == 0) + { + //?? + + } + + lowestMsgId = 0xFFFFFFFF; + + for (int i=0; iID < lowestMsgId) + lowestMsgId = messagesForNode[i]->ID; + } + + ui->lineOriginalBaseId->setText(Utility::formatCANID(lowestMsgId & 0x1FFFFFFFul)); + ui->lineNodeName->setText(dbcNode->name + QString("_Copy")); + } + + //generateSampleText(); +} diff --git a/dbc/dbcnodeduplicateeditor.h b/dbc/dbcnodeduplicateeditor.h new file mode 100644 index 0000000..9a29a93 --- /dev/null +++ b/dbc/dbcnodeduplicateeditor.h @@ -0,0 +1,45 @@ +#ifndef DBCNODEDUPLICATEEDITOR_H +#define DBCNODEDUPLICATEEDITOR_H + +#include +#include "dbc_classes.h" +#include "dbchandler.h" + +namespace Ui { +class DBCNodeDuplicateEditor; +} + +class DBCNodeDuplicateEditor : public QDialog +{ + Q_OBJECT + +public: + explicit DBCNodeDuplicateEditor(QWidget *parent = nullptr); + ~DBCNodeDuplicateEditor(); + void showEvent(QShowEvent*); + void setNodeRef(DBC_NODE *node); + void setFileIdx(int idx); + void refreshView(); + +signals: + void updatedTreeInfo(DBC_MESSAGE *msg); + void createNode(QString nodeName); + void cloneMessageToNode(DBC_NODE *parentNode, DBC_MESSAGE *source, uint newMsgId); + void nodeAdded(); + +private: + Ui::DBCNodeDuplicateEditor *ui; + + DBCHandler *dbcHandler; + DBC_NODE *dbcNode; + DBCFile *dbcFile; + + void closeEvent(QCloseEvent *event); + bool eventFilter(QObject *obj, QEvent *event); + void readSettings(); + void writeSettings(); + + uint lowestMsgId; +}; + +#endif // DBCNODEDUPLICATEEDITOR_H diff --git a/dbc/dbcnoderebaseeditor.cpp b/dbc/dbcnoderebaseeditor.cpp index 393b656..5a8f6bb 100644 --- a/dbc/dbcnoderebaseeditor.cpp +++ b/dbc/dbcnoderebaseeditor.cpp @@ -46,6 +46,13 @@ DBCNodeRebaseEditor::DBCNodeRebaseEditor(QWidget *parent) : } + }); + + connect(ui->btnCancel, &QPushButton::pressed, + [=]() + { + + }); // connect(ui->lineOriginalBaseId, &QLineEdit::editingFinished, @@ -159,7 +166,7 @@ void DBCNodeRebaseEditor::refreshView() } ui->lineOriginalBaseId->setText(Utility::formatCANID(lowestMsgId & 0x1FFFFFFFul)); - ui->lineMsgName->setText(dbcNode->name); + ui->lineNodeName->setText(dbcNode->name); } //generateSampleText(); diff --git a/ui/dbcnodeduplicateeditor.ui b/ui/dbcnodeduplicateeditor.ui new file mode 100644 index 0000000..a07c435 --- /dev/null +++ b/ui/dbcnodeduplicateeditor.ui @@ -0,0 +1,75 @@ + + + DBCNodeDuplicateEditor + + + + 0 + 0 + 314 + 193 + + + + Dialog + + + + + 10 + 10 + 290 + 116 + + + + + + + New Node Name: + + + + + + + + + + Current Base ID: + + + + + + + + + + New Base ID: + + + + + + + + + + Duplicate Node + + + + + + + Cancel + + + + + + + + + diff --git a/ui/dbcnoderebaseeditor.ui b/ui/dbcnoderebaseeditor.ui index 58bc46c..ddcfbe1 100644 --- a/ui/dbcnoderebaseeditor.ui +++ b/ui/dbcnoderebaseeditor.ui @@ -31,7 +31,7 @@ - +