/*Copyright © 2016 Hasan Yavuz ÖzderyaThis file is part of serialplot.serialplot is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.serialplot is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with serialplot. If not, see <http://www.gnu.org/licenses/>.*/#include <QByteArray>#include <QtDebug>#include "commandpanel.h"#include "ui_commandpanel.h"#include "setting_defines.h"CommandPanel::CommandPanel(QSerialPort* port, QWidget *parent) :QWidget(parent),ui(new Ui::CommandPanel),_menu(trUtf8("&Commands")), _newCommandAction(trUtf8("&New Command"), this){serialPort = port;ui->setupUi(this);auto layout = new QVBoxLayout();layout->setSpacing(0);ui->scrollAreaWidgetContents->setLayout(layout);#ifdef Q_OS_WINui->pbNew->setIcon(QIcon(":/icons/list-add"));#endif // Q_OS_WINconnect(ui->pbNew, &QPushButton::clicked, this, &CommandPanel::newCommand);connect(&_newCommandAction, &QAction::triggered, this, &CommandPanel::newCommand);_menu.addAction(&_newCommandAction);_menu.addSeparator();command_name_counter = 0;}CommandPanel::~CommandPanel(){commands.clear(); // UI will 'delete' actual objectsdelete ui;}CommandWidget* CommandPanel::newCommand(){auto command = new CommandWidget();command_name_counter++;command->setName(trUtf8("Command ") + QString::number(command_name_counter));ui->scrollAreaWidgetContents->layout()->addWidget(command);command->setFocusToEdit();connect(command, &CommandWidget::sendCommand, this, &CommandPanel::sendCommand);connect(command, &CommandWidget::focusRequested, this, &CommandPanel::focusRequested);_menu.addAction(command->sendAction());// add to command list and remove on destroycommands << command;connect(command, &QObject::destroyed, [this](QObject* obj){commands.removeOne(static_cast<CommandWidget*>(obj));});return command;}void CommandPanel::sendCommand(QByteArray command){if (!serialPort->isOpen()){qCritical() << "Port is not open!";return;}if (serialPort->write(command) < 0){qCritical() << "Send command failed!";}}QMenu* CommandPanel::menu(){return &_menu;}QAction* CommandPanel::newCommandAction(){return &_newCommandAction;}unsigned CommandPanel::numOfCommands(){return commands.size();}void CommandPanel::saveSettings(QSettings* settings){settings->beginGroup(SettingGroup_Commands);settings->beginWriteArray(SG_Commands_Command);for (int i = 0; i < commands.size(); i ++){settings->setArrayIndex(i);auto command = commands[i];settings->setValue(SG_Commands_Name, command->name());settings->setValue(SG_Commands_Type, command->isASCIIMode() ? "ascii" : "hex");settings->setValue(SG_Commands_Data, command->commandText());}settings->endArray();settings->endGroup();}void CommandPanel::loadSettings(QSettings* settings){// clear all commandswhile (commands.size()){auto command = commands.takeLast();command->disconnect();delete command;}// load commandssettings->beginGroup(SettingGroup_Commands);unsigned size = settings->beginReadArray(SG_Commands_Command);for (unsigned i = 0; i < size; i ++){settings->setArrayIndex(i);auto command = newCommand();// load command nameQString name = settings->value(SG_Commands_Name, "").toString();if (!name.isEmpty()) command->setName(name);// Important: type should be set before command data for correct validationQString type = settings->value(SG_Commands_Type, "").toString();if (type == "ascii"){command->setASCIIMode(true);}else if (type == "hex"){command->setASCIIMode(false);} // else unchanged// load command datacommand->setCommandText(settings->value(SG_Commands_Data, "").toString());}settings->endArray();settings->endGroup();}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。