diff --git a/src/shared/Modules/CommandPad/MessageFormElement.cpp b/src/shared/Modules/CommandPad/MessageFormElement.cpp index d34e6c1b10d4811f00c1a3afdaeaa66e47b5650f..91b9a4fe16ccd12556ab547534270c2a854161f0 100644 --- a/src/shared/Modules/CommandPad/MessageFormElement.cpp +++ b/src/shared/Modules/CommandPad/MessageFormElement.cpp @@ -22,26 +22,7 @@ MessageFormElement::MessageFormElement() {} -MessageFormElement::~MessageFormElement() -{ - for (auto key : comboBoxMap.keys()) - { - delete comboBoxMap[key].first; - delete comboBoxMap[key].second; - } - - for (auto key : floatMap.keys()) - { - delete std::get<0>(floatMap[key]); - delete std::get<1>(floatMap[key]); - } - - for (auto key : integerMap.keys()) - { - delete std::get<0>(integerMap[key]); - delete std::get<1>(integerMap[key]); - } -} +MessageFormElement::~MessageFormElement() {} bool MessageFormElement::addComboBox(QString id, QString label, const QMap<QString, int>& options) @@ -49,14 +30,15 @@ bool MessageFormElement::addComboBox(QString id, QString label, if (!comboBoxMap.contains(id) && !floatMap.contains(id) && !integerMap.contains(id)) { - auto* comboBox = new QComboBox; + auto comboBox = std::make_unique<QComboBox>(); comboBox->setSizePolicy( QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum)); for (auto key : options.keys()) comboBox->addItem(key, options[key]); - comboBoxMap[id] = {new QLabel(label), comboBox}; + comboBoxMap[id] = {std::make_unique<QLabel>(label), + std::move(comboBox)}; return true; } else @@ -71,11 +53,12 @@ bool MessageFormElement::addFloat(QString id, QString label, float min, if (!comboBoxMap.contains(id) && !floatMap.contains(id) && !integerMap.contains(id)) { - auto* lineEdit = new QLineEdit; + auto lineEdit = std::make_unique<QLineEdit>(); lineEdit->setSizePolicy( QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum)); lineEdit->setValidator(new QDoubleValidator(min, max, decimals)); - floatMap[id] = {new QLabel(label), lineEdit, min, max, decimals}; + floatMap[id] = {std::make_unique<QLabel>(label), std::move(lineEdit), + min, max, decimals}; return true; } else @@ -89,11 +72,12 @@ bool MessageFormElement::addInteger(QString id, QString label, int min, int max) if (!comboBoxMap.contains(id) && !floatMap.contains(id) && !integerMap.contains(id)) { - auto* lineEdit = new QLineEdit; + auto lineEdit = std::make_unique<QLineEdit>(); lineEdit->setSizePolicy( QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum)); lineEdit->setValidator(new QIntValidator(min, max)); - integerMap[id] = {new QLabel(label), lineEdit, min, max}; + integerMap[id] = {std::make_unique<QLabel>(label), std::move(lineEdit), + min, max}; return true; } else @@ -106,7 +90,7 @@ XmlObject MessageFormElement::toXmlObject(XmlObject& obj) { for (auto key : comboBoxMap.keys()) { - auto comboBox = comboBoxMap[key]; + auto& comboBox = comboBoxMap[key]; XmlObject comboBoxXml("combo_box"); comboBoxXml.addAttribute("id", key); comboBoxXml.addAttribute("text", comboBox.second->currentText()); @@ -115,7 +99,7 @@ XmlObject MessageFormElement::toXmlObject(XmlObject& obj) for (auto key : floatMap.keys()) { - auto floatField = floatMap[key]; + auto& floatField = floatMap[key]; XmlObject floatFieldXml("float_field"); floatFieldXml.addAttribute("id", key); floatFieldXml.addAttribute("text", std::get<1>(floatField)->text()); @@ -127,7 +111,7 @@ XmlObject MessageFormElement::toXmlObject(XmlObject& obj) for (auto key : floatMap.keys()) { - auto integerField = floatMap[key]; + auto& integerField = floatMap[key]; XmlObject integerFieldXml("integer_field"); integerFieldXml.addAttribute("id", key); integerFieldXml.addAttribute("text", std::get<1>(integerField)->text()); @@ -148,13 +132,13 @@ void MessageFormElement::fromXmlObject(const XmlObject& obj) if (child.getObjectName() == "combo_box" && comboBoxMap.contains(key)) { - auto comboBox = comboBoxMap[key]; + auto& comboBox = comboBoxMap[key]; comboBox.second->setCurrentText(child.getAttribute("text")); } else if (child.getObjectName() == "float_field" && floatMap.contains(key)) { - auto floatField = floatMap[key]; + auto& floatField = floatMap[key]; std::get<1>(floatField)->setText(child.getAttribute("text")); child.getAttribute("min", std::get<2>(floatField)); child.getAttribute("max", std::get<3>(floatField)); @@ -167,7 +151,7 @@ void MessageFormElement::fromXmlObject(const XmlObject& obj) else if (child.getObjectName() == "integer_field" && integerMap.contains(key)) { - auto integerField = integerMap[key]; + auto& integerField = integerMap[key]; std::get<1>(integerField)->setText(child.getAttribute("text")); child.getAttribute("min", std::get<2>(integerField)); child.getAttribute("max", std::get<3>(integerField)); @@ -182,12 +166,12 @@ void MessageFormElement::applyToGridLayout(QGridLayout* layout) { for (auto key : comboBoxMap.keys()) { - auto label = comboBoxMap[key].first; - auto comboBox = comboBoxMap[key].second; + auto& label = comboBoxMap[key].first; + auto& comboBox = comboBoxMap[key].second; int rows = layout->rowCount(); - layout->addWidget(label, rows, 0, Qt::AlignRight); - layout->addWidget(comboBox, rows, 1); + layout->addWidget(label.get(), rows, 0, Qt::AlignRight); + layout->addWidget(comboBox.get(), rows, 1); label->show(); comboBox->show(); @@ -195,12 +179,12 @@ void MessageFormElement::applyToGridLayout(QGridLayout* layout) for (auto key : floatMap.keys()) { - auto label = std::get<0>(floatMap[key]); - auto floatField = std::get<1>(floatMap[key]); + auto& label = std::get<0>(floatMap[key]); + auto& floatField = std::get<1>(floatMap[key]); int rows = layout->rowCount(); - layout->addWidget(label, rows, 0, Qt::AlignRight); - layout->addWidget(floatField, rows, 1); + layout->addWidget(label.get(), rows, 0, Qt::AlignRight); + layout->addWidget(floatField.get(), rows, 1); label->show(); floatField->show(); @@ -208,12 +192,12 @@ void MessageFormElement::applyToGridLayout(QGridLayout* layout) for (auto key : integerMap.keys()) { - auto label = std::get<0>(integerMap[key]); - auto integerField = std::get<1>(integerMap[key]); + auto& label = std::get<0>(integerMap[key]); + auto& integerField = std::get<1>(integerMap[key]); int rows = layout->rowCount(); - layout->addWidget(label, rows, 0, Qt::AlignRight); - layout->addWidget(integerField, rows, 1); + layout->addWidget(label.get(), rows, 0, Qt::AlignRight); + layout->addWidget(integerField.get(), rows, 1); label->show(); integerField->show(); @@ -224,11 +208,11 @@ void MessageFormElement::removeFromGridLayout(QGridLayout* layout) { for (auto key : comboBoxMap.keys()) { - auto label = comboBoxMap[key].first; - auto comboBox = comboBoxMap[key].second; + auto& label = comboBoxMap[key].first; + auto& comboBox = comboBoxMap[key].second; - layout->removeWidget(label); - layout->removeWidget(comboBox); + layout->removeWidget(label.get()); + layout->removeWidget(comboBox.get()); label->hide(); comboBox->hide(); @@ -236,11 +220,11 @@ void MessageFormElement::removeFromGridLayout(QGridLayout* layout) for (auto key : floatMap.keys()) { - auto label = std::get<0>(floatMap[key]); - auto floatField = std::get<1>(floatMap[key]); + auto& label = std::get<0>(floatMap[key]); + auto& floatField = std::get<1>(floatMap[key]); - layout->removeWidget(label); - layout->removeWidget(floatField); + layout->removeWidget(label.get()); + layout->removeWidget(floatField.get()); label->hide(); floatField->hide(); @@ -248,11 +232,11 @@ void MessageFormElement::removeFromGridLayout(QGridLayout* layout) for (auto key : integerMap.keys()) { - auto label = std::get<0>(integerMap[key]); - auto integerField = std::get<1>(integerMap[key]); + auto& label = std::get<0>(integerMap[key]); + auto& integerField = std::get<1>(integerMap[key]); - layout->removeWidget(label); - layout->removeWidget(integerField); + layout->removeWidget(label.get()); + layout->removeWidget(integerField.get()); label->hide(); integerField->hide(); @@ -265,22 +249,22 @@ Message MessageFormElement::prepareMessage(const QString& messageId) for (auto key : comboBoxMap.keys()) { - auto comboBox = comboBoxMap[key].second; - auto msg = Field{(uint64_t)comboBox->currentData().toInt()}; + auto& comboBox = comboBoxMap[key].second; + auto msg = Field{(uint64_t)comboBox->currentData().toInt()}; message.setField(key, msg); } for (auto key : floatMap.keys()) { - auto floatField = std::get<1>(floatMap[key]); - auto msg = Field{floatField->text().toDouble()}; + auto& floatField = std::get<1>(floatMap[key]); + auto msg = Field{floatField->text().toDouble()}; message.setField(key, msg); } for (auto key : integerMap.keys()) { - auto integerField = std::get<1>(integerMap[key]); - auto msg = Field{(uint64_t)integerField->text().toInt()}; + auto& integerField = std::get<1>(integerMap[key]); + auto msg = Field{(uint64_t)integerField->text().toInt()}; message.setField(key, msg); } diff --git a/src/shared/Modules/CommandPad/MessageFormElement.h b/src/shared/Modules/CommandPad/MessageFormElement.h index 7c85e7471840e672fd159ea479cf1ba22bfce508..e1a756db480a3a6a863ab0c6ee589da2d0cae47b 100644 --- a/src/shared/Modules/CommandPad/MessageFormElement.h +++ b/src/shared/Modules/CommandPad/MessageFormElement.h @@ -73,7 +73,10 @@ public: Message prepareMessage(const QString& messageId); private: - QMap<QString, QPair<QLabel*, QComboBox*>> comboBoxMap; - QMap<QString, std::tuple<QLabel*, QLineEdit*, float, float, int>> floatMap; - QMap<QString, std::tuple<QLabel*, QLineEdit*, int, int>> integerMap; + QMap<QString, QPair<std::unique_ptr<QLabel>, std::unique_ptr<QComboBox>>> + comboBoxMap; + QMap<QString, std::tuple<std::unique_ptr<QLabel>, + std::unique_ptr<QLineEdit>, float, float, int>> + floatMap; + QMap<QString, std::tuple<std::unique_ptr<QLabel>, std::unique_ptr<QLineEdit>, int, int>> integerMap; }; diff --git a/src/shared/Modules/CommandPad/MessagesList.h b/src/shared/Modules/CommandPad/MessagesList.h index c8f583a4c3e26c0db2fa64729d98eecc9600e9f7..fee244ecab5cd9da4a9caa7b0b3dc8143c35e697 100644 --- a/src/shared/Modules/CommandPad/MessagesList.h +++ b/src/shared/Modules/CommandPad/MessagesList.h @@ -123,94 +123,95 @@ const QMap<QString, int> servosList{ {"FILLING_VALVE", FILLING_VALVE}, {"DISCONNECT_SERVO", DISCONNECT_SERVO}}; -inline void fillMessagesMap(QMap<QString, MessageFormElement *> &formElements) +inline void fillMessagesMap( + QMap<QString, std::unique_ptr<MessageFormElement>> &formElements) { - MessageFormElement *element; + std::unique_ptr<MessageFormElement> element; - element = new MessageFormElement(); - formElements["PING_TC"] = element; + element = std::make_unique<MessageFormElement>(); + formElements["PING_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addComboBox("command_id", "Command:", commandsList); - formElements["COMMAND_TC"] = element; + formElements["COMMAND_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addComboBox("tm_id", "Telemetry:", systemTmList); - formElements["SYSTEM_TM_REQUEST_TC"] = element; + formElements["SYSTEM_TM_REQUEST_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addComboBox("sensor_id", "Sensor:", sensorsList); - formElements["SENSOR_TM_REQUEST_TC"] = element; + formElements["SENSOR_TM_REQUEST_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addComboBox("servo_id", "Servo:", servosList); - formElements["SERVO_TM_REQUEST_TC"] = element; + formElements["SERVO_TM_REQUEST_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addComboBox("servo_id", "Servo:", servosList); element->addFloat("angle", "Angle:", 0, 180, 2); - formElements["SET_SERVO_ANGLE_TC"] = element; + formElements["SET_SERVO_ANGLE_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addComboBox("servo_id", "Servo:", servosList); - formElements["WIGGLE_SERVO_TC"] = element; + formElements["WIGGLE_SERVO_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addComboBox("servo_id", "Servo:", servosList); - formElements["RESET_SERVO_TC"] = element; + formElements["RESET_SERVO_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addFloat("ref_altitude", "Altitude:", 0, 9999, 2); - formElements["SET_REFERENCE_ALTITUDE_TC"] = element; + formElements["SET_REFERENCE_ALTITUDE_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addFloat("ref_temp", "Temperature:", 0, 999, 2); - formElements["SET_REFERENCE_TEMPERATURE_TC"] = element; + formElements["SET_REFERENCE_TEMPERATURE_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addFloat("yaw", "Yaw:", -180, 180, 2); element->addFloat("pitch", "Pitch:", -180, 180, 2); element->addFloat("roll", "Roll:", -180, 180, 2); - formElements["SET_ORIENTATION_TC"] = element; + formElements["SET_ORIENTATION_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addFloat("latitude", "Latitude:", -90, 90, 6); element->addFloat("longitude", "Longitude:", -90, 90, 6); - formElements["SET_COORDINATES_TC"] = element; + formElements["SET_COORDINATES_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addInteger("topic_id", "Topic:", 0, 999); element->addInteger("event_id", "Event:", 0, 999); - formElements["RAW_EVENT_TC"] = element; + formElements["RAW_EVENT_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addFloat("dpl_altitude", "Altitude:", 0, 9999, 2); - formElements["SET_DEPLOYMENT_ALTITUDE_TC"] = element; + formElements["SET_DEPLOYMENT_ALTITUDE_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addFloat("latitude", "Latitude:", -90, 90, 6); element->addFloat("longitude", "Longitude:", -90, 90, 6); - formElements["SET_TARGET_COORDINATES_TC"] = element; + formElements["SET_TARGET_COORDINATES_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addInteger("algorithm_number", "Algorithm:", 0, 999); - formElements["SET_ALGORITHM_TC"] = element; + formElements["SET_ALGORITHM_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addComboBox("servo_id", "Servo:", servosList); element->addInteger("maximum_timing", "Timing [ms]:", 0, 3600000); - formElements["SET_ATOMIC_VALVE_TIMING_TC"] = element; + formElements["SET_ATOMIC_VALVE_TIMING_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addComboBox("servo_id", "Servo:", servosList); element->addFloat("maximum_aperture", "Aperture [0-1]:", 0, 1); - formElements["SET_VALVE_MAXIMUM_APERTURE_TC"] = element; + formElements["SET_VALVE_MAXIMUM_APERTURE_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addInteger("timing", "Timing [ms]:", 0, 3600000); - formElements["SET_IGNITION_TIME_TC"] = element; + formElements["SET_IGNITION_TIME_TC"] = std::move(element); - element = new MessageFormElement(); + element = std::make_unique<MessageFormElement>(); element->addInteger("ignition_btn", "Ignition:", 0, 1); element->addInteger("filling_valve_btn", "Filling:", 0, 1); element->addInteger("venting_valve_btn", "Venting:", 0, 1); @@ -218,7 +219,7 @@ inline void fillMessagesMap(QMap<QString, MessageFormElement *> &formElements) element->addInteger("quick_connector_btn", "Quick connector:", 0, 1); element->addInteger("start_tars_btn", "Tars:", 0, 1); element->addInteger("arm_switch", "Arm:", 0, 1); - formElements["CONRIG_STATE_TC"] = element; + formElements["CONRIG_STATE_TC"] = std::move(element); } } // namespace MessagesList