Changeset - 3216cc699a63
[Not reviewed]
default
0 5 0
Hasan Yavuz Ă–ZDERYA - 10 years ago 2016-02-28 09:56:16
hy@ozderya.net
added "Commands" menu
5 files changed with 34 insertions and 1 deletions:
0 comments (0 inline, 0 general)
commandpanel.cpp
Show inline comments
 
@@ -16,56 +16,71 @@
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "commandpanel.h"
 
#include "ui_commandpanel.h"
 

	
 
#include <QByteArray>
 
#include <QtDebug>
 

	
 
CommandPanel::CommandPanel(QSerialPort* port, QWidget *parent) :
 
    QWidget(parent),
 
    ui(new Ui::CommandPanel)
 
    ui(new Ui::CommandPanel),
 
    _menu(trUtf8("Commands")), _newCommandAction(trUtf8("New Command"), this)
 
{
 
    serialPort = port;
 

	
 
    ui->setupUi(this);
 
    ui->scrollAreaWidgetContents->setLayout(new QVBoxLayout);
 

	
 
#ifdef Q_OS_WIN
 
    ui->pbNew->setIcon(QIcon(":/icons/list-add"));
 
#endif // Q_OS_WIN
 

	
 
    connect(ui->pbNew, &QPushButton::clicked, this, &CommandPanel::newCommand);
 
    connect(&_newCommandAction, &QAction::triggered, this, &CommandPanel::newCommand);
 

	
 
    _menu.addAction(&_newCommandAction);
 

	
 
    command_name_counter = 0;
 
    newCommand(); // add an empty slot by default
 
}
 

	
 
CommandPanel::~CommandPanel()
 
{
 
    delete ui;
 
}
 

	
 
void 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);
 
}
 

	
 
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;
 
}
commandpanel.h
Show inline comments
 
@@ -14,39 +14,46 @@
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef COMMANDPANEL_H
 
#define COMMANDPANEL_H
 

	
 
#include <QWidget>
 
#include <QSerialPort>
 
#include <QByteArray>
 
#include <QMenu>
 
#include <QAction>
 

	
 
#include "commandwidget.h"
 

	
 
namespace Ui {
 
class CommandPanel;
 
}
 

	
 
class CommandPanel : public QWidget
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit CommandPanel(QSerialPort* port, QWidget *parent = 0);
 
    ~CommandPanel();
 

	
 
    QMenu* menu();
 
    QAction* newCommandAction();
 

	
 
private:
 
    Ui::CommandPanel *ui;
 
    QSerialPort* serialPort;
 
    QMenu _menu;
 
    QAction _newCommandAction;
 

	
 
    unsigned command_name_counter;
 

	
 
private slots:
 
    void newCommand();
 
    void sendCommand(QByteArray command);
 
};
 

	
 
#endif // COMMANDPANEL_H
commandwidget.cpp
Show inline comments
 
@@ -94,12 +94,17 @@ bool CommandWidget::isASCIIMode()
 
    return ui->pbASCII->isChecked();
 
}
 

	
 
void CommandWidget::setName(QString name)
 
{
 
    ui->leName->setText(name);
 
}
 

	
 
QString CommandWidget::name()
 
{
 
    return ui->leName->text();
 
}
 

	
 
void CommandWidget::setFocusToEdit()
 
{
 
    ui->leCommand->setFocus(Qt::OtherFocusReason);
 
}
commandwidget.h
Show inline comments
 
@@ -28,24 +28,25 @@ class CommandWidget;
 
}
 

	
 
class CommandWidget : public QWidget
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit CommandWidget(QWidget *parent = 0);
 
    ~CommandWidget();
 

	
 
    void setName(QString name);
 
    QString name();
 
    void setFocusToEdit();
 

	
 
signals:
 
    void deleteRequested(CommandWidget* thisWidget); // emitted when delete button is clicked
 

	
 
    // emitted when send button is clicked
 
    //
 
    // in case of hex mode, command text should be a hexadecimal
 
    // string containing hexadecimal characters only (not even spaces)
 
    void sendCommand(QByteArray command);
 

	
 
private:
 
    Ui::CommandWidget *ui;
mainwindow.cpp
Show inline comments
 
@@ -58,24 +58,29 @@ MainWindow::MainWindow(QWidget *parent) 
 
    dataFormatPanel(&serialPort, &channelBuffers),
 
    snapshotMan(this, &channelBuffers)
 
{
 
    ui->setupUi(this);
 
    ui->tabWidget->insertTab(0, &portControl, "Port");
 
    ui->tabWidget->insertTab(1, &dataFormatPanel, "Data Format");
 
    ui->tabWidget->insertTab(3, &commandPanel, "Commands");
 
    ui->tabWidget->setCurrentIndex(0);
 
    addToolBar(portControl.toolBar());
 

	
 
    ui->plotToolBar->addAction(snapshotMan.takeSnapshotAction());
 
    ui->menuBar->insertMenu(ui->menuHelp->menuAction(), snapshotMan.menu());
 
    ui->menuBar->insertMenu(ui->menuHelp->menuAction(), commandPanel.menu());
 
    connect(commandPanel.newCommandAction(), &QAction::triggered, [this]()
 
            {
 
                this->ui->tabWidget->setCurrentWidget(&commandPanel);
 
            });
 

	
 
    setupAboutDialog();
 

	
 
    // init view menu
 
    for (auto a : ui->plot->menuActions())
 
    {
 
        ui->menuView->addAction(a);
 
    }
 

	
 
    ui->menuView->addSeparator();
 

	
 
    QMenu* tbMenu = ui->menuView->addMenu("Toolbars");
0 comments (0 inline, 0 general)