diff --git a/Core/xmlobject.h b/Core/xmlobject.h index f075cf9493935734e219236c80733138b378b787..0e48cac2f16adc0ccfc85e29bdd222473371591a 100644 --- a/Core/xmlobject.h +++ b/Core/xmlobject.h @@ -23,7 +23,6 @@ public: QString getAttribute(const QString &name) const; bool hasAttribute(const QString &name) const; int attributeCount() const; - QMapIterator<QString, QString> attributesIterator() const; /* diff --git a/Modules/Mavlink/mavlinkrocketmsgtestingmodule.cpp b/Modules/Mavlink/mavlinkrocketmsgtestingmodule.cpp index 2724ad6b9ffb39fa3fed6101eba069ac8d72ea34..5b2ce66a5063f140993c099b1cea71588337488a 100644 --- a/Modules/Mavlink/mavlinkrocketmsgtestingmodule.cpp +++ b/Modules/Mavlink/mavlinkrocketmsgtestingmodule.cpp @@ -1,15 +1,29 @@ #include "mavlinkrocketmsgtestingmodule.h" #include "ui_mavlinkrocketmsgtestingmodule.h" +#include <Core/modulemessagesbroker.h> +#include "Core/xmlobject.h" +#include "mavlinkversionheader.h" +#include <QMap> +#include <QDebug> + MavlinkRocketMsgTestingModule::MavlinkRocketMsgTestingModule(QWidget *parent) : DefaultModule(parent), ui(new Ui::MavlinkRocketMsgTestingModule) { ui->setupUi(this); defaultContextMenuSetup(); + init(); } MavlinkRocketMsgTestingModule::~MavlinkRocketMsgTestingModule() { - delete ui; + delete ui; // This delete all the radiobutton +} + +void MavlinkRocketMsgTestingModule::init() +{ + ui->lineEdit_mavlinkInputFilePath->setText(defaultMavlinkFileName); + connect(ui->pushButton_sendMsg, &QPushButton::clicked, this, &MavlinkRocketMsgTestingModule::onSendMsgClicked); + connect(ui->pushButton_loadInputFile, &QPushButton::clicked, this, &MavlinkRocketMsgTestingModule::onLoadFileClicked); } QWidget *MavlinkRocketMsgTestingModule::toWidget() @@ -26,3 +40,206 @@ void MavlinkRocketMsgTestingModule::fromXmlObject(const XmlObject &xmlObject) { Q_UNUSED(xmlObject); } + +void MavlinkRocketMsgTestingModule::createNewButton(const QString &txt, QLayout *container) +{ + QRadioButton *radiobutton = new QRadioButton(txt); + connectRadioButton(radiobutton); + container->addWidget(radiobutton); +} + +void MavlinkRocketMsgTestingModule::connectRadioButton(QRadioButton *radiobutton) +{ + connect(radiobutton, &QRadioButton::clicked, this, [this, radiobutton](){ + this->onRadioButtonClicked(radiobutton); + }); +} + +void MavlinkRocketMsgTestingModule::setSelectedMsg(const QString &msgName) +{ + if(currentMsgView != nullptr){ + delete currentMsgView; + currentMsgView = nullptr; + } + + QList<XmlObject*> clickedMsg = mavlinkMsgDefinitionFile.deepSearchObjects([this, msgName](const XmlObject *obj){ + QString name = obj->getAttribute(xmlFieldName); + if(name == msgName){ + return true; + } + return false; + }); + + if(clickedMsg.count()==1){ + currentMsgView = new MsgView(clickedMsg[0], msgName); + ui->details_layout->insertWidget(0, currentMsgView->getView()); + } + +} + + +void MavlinkRocketMsgTestingModule::onSendMsgClicked() +{ + QString currentTopic = getCurrentTopic().trimmed(); + if(currentMsgView != nullptr && currentTopic != ""){ + //this->getCore()->getModuleMessagesBroker()->publish(currentTopic, currentMsgView->text()); + } +} + +void MavlinkRocketMsgTestingModule::onLoadFileClicked() +{ + QString filePath = SkywardHubStrings::defaultConfigurationFolder + "/" + ui->lineEdit_mavlinkInputFilePath->text().trimmed(); + if (mavlinkMsgDefinitionFile.loadFromFile(filePath)){ + QList<XmlObject*> xmlMessages = mavlinkMsgDefinitionFile.deepSearchObjects([this](const XmlObject *msg){ + if(msg->getObjectName() == xmlChildName){ + return true; + } + return false; + }); + + createViewFromXmlMessages(xmlMessages); + } +} + +void MavlinkRocketMsgTestingModule::createViewFromXmlMessages(QList<XmlObject *> messagesList) +{ + for (int i = 0; i < messagesList.count(); i++ ) { + XmlObject *xmlObj = messagesList[i]; + QString msgType = xmlObj->getAttribute(xmlFieldType).trimmed(); + QGroupBox* gbox = getOrCreateGBox(msgType); + QString name = xmlObj->getAttribute(xmlFieldName).trimmed(); + if( name != ""){ + createNewButton(xmlObj->getAttribute(xmlFieldName), gbox->layout()); + } + } +} + +QGroupBox* MavlinkRocketMsgTestingModule::getOrCreateGBox(const QString &title) +{ + QGroupBox *gBox = nullptr; + if(title != "" && !viewGroupList.contains(title)){ + gBox = new QGroupBox(title); + viewGroupList[title] = gBox; + ui->msg_layout->insertWidget(0, gBox); + gBox->setLayout(new QVBoxLayout()); + } + else if(viewGroupList.contains(title)){ + gBox = viewGroupList[title]; + } + return gBox; +} + +QString MavlinkRocketMsgTestingModule::getCurrentTopic() const +{ + return ui->lineEdit_outputTopic->text(); +} + +void MavlinkRocketMsgTestingModule::onRadioButtonClicked(QRadioButton *radiobutton) +{ + setSelectedMsg(radiobutton->text()); +} + +//mavlink_message_t MavlinkRocketMsgTestingModule::genericPack(string funcName, char* params[]) +//{ +// mavlink_message_t result_msg; + +// if(!funcName.compare("mavlink_msg_sm_tm_pack")){ +// uint8_t system_id = (uint8_t)atoi(params[0]); +// ecc.. +// mavlink_msg_sm_tm_pack(system_id, ecc...); +// } +// else if(!funcName.compare("mavlink_msg_gps_tm_pack")){ +// param... +// mavlink_msg_gps_tm_pack(param...); +// } +// . +// . +// . + +// return result_msg; +//} + +// _______________________________________________ MsgView ___________________________________________________ + +MsgView::MsgView(const XmlObject *obj, QString name) : QObject() +{ + this->name = name; + bool ok; + id = obj->getAttribute(xmlFieldId).trimmed().toInt(&ok); + if(!ok){ + id = 0; + } + + for(int i = 0; i < obj->childCount(); i++){ + XmlObject fieldXml = obj->childAt(i); + if(fieldXml.getObjectName() == xmlFieldElementName){ +// auto i = fieldXml.attributesIterator(); +// while(i.hasNext()){ +// auto pair = i.next(); +// } + QString fieldName = fieldXml.getAttribute(xmlFieldName); + QLineEdit* lineEdit = new QLineEdit(); + fields[fieldName] = lineEdit; + } + else if(fieldXml.getObjectName() == xmlFieldDescr) { + description = fieldXml.getTextValue(); + } + } +} + +MsgView::~MsgView() +{ + delete view; +} + +QWidget *MsgView::getView() +{ + if(view == nullptr){ + view = new QWidget(); + QVBoxLayout *layout = new QVBoxLayout(); + view->setLayout(layout); + + QLabel* nameLabel = new QLabel(name); + nameLabel->setAlignment(Qt::AlignmentFlag::AlignCenter); + layout->addWidget(nameLabel); + + QHBoxLayout *rowId = new QHBoxLayout(); + rowId->addWidget(new QLabel(xmlFieldId)); + QLabel *idLabel = new QLabel(QString::number(id)); + rowId->addWidget(idLabel); + rowId->setStretchFactor(idLabel, 1); + layout->addLayout(rowId); + layout->addWidget(new QLabel(description)); + + QMapIterator<QString, QLineEdit*> i(fields); + while (i.hasNext()) { + i.next(); + QLabel* fieldNameLabel = new QLabel(name); + QHBoxLayout *rowLayout = new QHBoxLayout(); + + rowLayout->addWidget(fieldNameLabel); + rowLayout->addWidget(i.value()); + layout->addLayout(rowLayout); + } + } + + return view; +} + + + + + + + + + + + + + + + + + + diff --git a/Modules/Mavlink/mavlinkrocketmsgtestingmodule.h b/Modules/Mavlink/mavlinkrocketmsgtestingmodule.h index dce1b88725e7b327997801fedc7cda6b0e5090d2..d9bde6577a6d697cdc6fdcb9d2f03e67216b80b3 100644 --- a/Modules/Mavlink/mavlinkrocketmsgtestingmodule.h +++ b/Modules/Mavlink/mavlinkrocketmsgtestingmodule.h @@ -2,11 +2,39 @@ #define MAVLINKROCKETMSGTESTINGMODULE_H #include "Modules/DefaultModule/defaultmodule.h" +#include <QRadioButton> +#include "Modules/skywardhubstrings.h" +#include <QGroupBox> +#include <QLineEdit> namespace Ui { class MavlinkRocketMsgTestingModule; } + +class MsgView : public QObject +{ + Q_OBJECT + +public: + MsgView(const XmlObject *obj, QString name); + ~MsgView(); + QWidget *getView(); + +private: + QString description; + QString name; + int id; + QMap<QString, QLineEdit*> fields; + QWidget *view = nullptr; + + QString xmlFieldId = "id"; + QString xmlFieldDescr = "description"; + QString xmlFieldElementName = "field"; + QString xmlFieldName = "name"; +}; + + class MavlinkRocketMsgTestingModule : public DefaultModule { Q_OBJECT @@ -20,8 +48,33 @@ public: XmlObject toXmlObject() override; void fromXmlObject(const XmlObject &xmlObject) override; +protected: + void init(); + void createNewButton(const QString &txt, QLayout *container); + void connectRadioButton(QRadioButton *radiobutton); + void setSelectedMsg(const QString &msgName); + void onSendMsgClicked(); + void onLoadFileClicked(); +// mavlink_message_t genericPack(string funcName, char *params[]); + QString getCurrentTopic() const; + void createViewFromXmlMessages(QList<XmlObject*> messagesList); + QGroupBox* getOrCreateGBox(const QString &title); + +protected slots: + void onRadioButtonClicked(QRadioButton *radiobutton); + private: Ui::MavlinkRocketMsgTestingModule *ui; + + MsgView *currentMsgView = nullptr; + + QString defaultMavlinkFileName = "mavlink.xml"; + QString xmlChildName = "message"; + QString xmlFieldType = "msg_type"; + QString xmlFieldName = "name"; + XmlObject mavlinkMsgDefinitionFile; + + QMap<QString, QGroupBox*> viewGroupList; }; #endif // MAVLINKROCKETMSGTESTINGMODULE_H diff --git a/Modules/Mavlink/mavlinkrocketmsgtestingmodule.ui b/Modules/Mavlink/mavlinkrocketmsgtestingmodule.ui index 509b4d79b9d8e1ccd00848621335154418e63d51..019874545dcd23169f14bcd9a7f94ed0b355aaf0 100644 --- a/Modules/Mavlink/mavlinkrocketmsgtestingmodule.ui +++ b/Modules/Mavlink/mavlinkrocketmsgtestingmodule.ui @@ -6,14 +6,14 @@ <rect> <x>0</x> <y>0</y> - <width>400</width> - <height>300</height> + <width>676</width> + <height>431</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> - <layout class="QVBoxLayout" name="verticalLayout" stretch="0,1"> + <layout class="QVBoxLayout" name="verticalLayout" stretch="0,0,0,0"> <item> <widget class="QLabel" name="label"> <property name="text"> @@ -25,7 +25,32 @@ </widget> </item> <item> - <layout class="QHBoxLayout" name="horizontalLayout"> + <layout class="QHBoxLayout" name="horizontalLayout_2"> + <item> + <widget class="QLineEdit" name="lineEdit_mavlinkInputFilePath"> + <property name="placeholderText"> + <string>Mavlink .xml file</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="pushButton_loadInputFile"> + <property name="text"> + <string>Load File</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <widget class="QLineEdit" name="lineEdit_outputTopic"> + <property name="placeholderText"> + <string>Output topic</string> + </property> + </widget> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_4" stretch="1,2"> <item> <widget class="QScrollArea" name="scrollArea"> <property name="widgetResizable"> @@ -36,27 +61,65 @@ <rect> <x>0</x> <y>0</y> - <width>290</width> - <height>255</height> + <width>214</width> + <height>327</height> </rect> </property> - <layout class="QVBoxLayout" name="verticalLayout_2"> + <layout class="QVBoxLayout" name="msg_layout"> <item> - <widget class="QRadioButton" name="radioButton"> - <property name="text"> - <string>Messaggio generico</string> + <spacer name="verticalSpacer_2"> + <property name="orientation"> + <enum>Qt::Vertical</enum> </property> - </widget> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>40</height> + </size> + </property> + </spacer> </item> </layout> </widget> </widget> </item> <item> - <widget class="QPushButton" name="pushButton"> - <property name="text"> - <string>Send</string> + <widget class="QScrollArea" name="scrollArea_2"> + <property name="widgetResizable"> + <bool>true</bool> </property> + <widget class="QWidget" name="scrollAreaWidgetContents_2"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>430</width> + <height>327</height> + </rect> + </property> + <layout class="QVBoxLayout" name="details_layout"> + <item> + <spacer name="verticalSpacer"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>20</width> + <height>254</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="pushButton_sendMsg"> + <property name="text"> + <string>Send</string> + </property> + </widget> + </item> + </layout> + </widget> </widget> </item> </layout> diff --git a/Modules/moduleslist.cpp b/Modules/moduleslist.cpp index 2e339edfc7f3c6abd364dceffc744ce3a1f01418..e3e7c578a321d0bbc50061b66e9dc6d55709d51e 100644 --- a/Modules/moduleslist.cpp +++ b/Modules/moduleslist.cpp @@ -30,7 +30,7 @@ void ModulesList::createModuleList() * WARNING! * ⚠ Each ID must be unique * ⚠ Each ModuleName must be unique and WITHOUT spaces - * ⚠ A Module must NOT include another Module + * ⚠ A Module must NOT include a file outside its folder * ⚠ The ModuleSourceFiles allow you to specify which files will be included * (all the files that match the strings you privide will be included) */ diff --git a/SkywardHub.pro.user b/SkywardHub.pro.user index 616878ec14f40673fab9a92a477b0a4dbc678d81..2748032cfea6f90d870f7f136527a1a97b4ea7e0 100644 --- a/SkywardHub.pro.user +++ b/SkywardHub.pro.user @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE QtCreatorProject> -<!-- Written by QtCreator 4.14.0, 2021-02-20T23:33:22. --> +<!-- Written by QtCreator 4.14.2, 2021-04-09T17:57:14. --> <qtcreator> <data> <variable>EnvironmentId</variable>