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
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the 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 of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  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/>.
 
*/
 

	
 
#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
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the 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 of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  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
 
@@ -58,48 +58,53 @@ void CommandWidget::onSendClicked()
 
    {
 
        qWarning() << "Enter a command to send!";
 
        ui->leCommand->setFocus(Qt::OtherFocusReason);
 
        return;
 
    }
 

	
 
    if (isASCIIMode())
 
    {
 
        qDebug() << "Sending:" << command;
 
        emit sendCommand(ui->leCommand->unEscapedText().toLatin1());
 
    }
 
    else // hex mode
 
    {
 
        command = command.remove(' ');
 
        // check if nibbles are missing
 
        if (command.size() % 2 == 1)
 
        {
 
            qWarning() << "HEX command is missing a nibble at the end!";
 
            ui->leCommand->setFocus(Qt::OtherFocusReason);
 
            // highlight the byte that is missing a nibble (last byte obviously)
 
            int textSize = ui->leCommand->text().size();
 
            ui->leCommand->setSelection(textSize-1, textSize);
 
            return;
 
        }
 
        qDebug() << "Sending HEX:" << command;
 
        emit sendCommand(QByteArray::fromHex(command.toLatin1()));
 
    }
 
}
 

	
 
void CommandWidget::onASCIIToggled(bool checked)
 
{
 
    ui->leCommand->setMode(checked);
 
}
 

	
 
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
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the 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 of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  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 COMMANDWIDGET_H
 
#define COMMANDWIDGET_H
 

	
 
#include <QWidget>
 
#include <QByteArray>
 

	
 
namespace Ui {
 
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;
 

	
 
    bool isASCIIMode(); // true: ascii mode, false hex mode
 

	
 
private slots:
 
    void onDeleteClicked();
 
    void onSendClicked();
 
    void onASCIIToggled(bool checked);
 
};
 

	
 
#endif // COMMANDWIDGET_H
mainwindow.cpp
Show inline comments
 
@@ -22,96 +22,101 @@
 
#include <QByteArray>
 
#include <QApplication>
 
#include <QFileDialog>
 
#include <QFile>
 
#include <QTextStream>
 
#include <QMenu>
 
#include <QtDebug>
 
#include <qwt_plot.h>
 
#include <limits.h>
 
#include <cmath>
 
#include <iostream>
 

	
 
#include <plot.h>
 

	
 
#include "utils.h"
 
#include "version.h"
 

	
 
#if defined(Q_OS_WIN) && defined(QT_STATIC)
 
#include <QtPlugin>
 
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
 
#endif
 

	
 
struct Range
 
{
 
    double rmin;
 
    double rmax;
 
};
 

	
 
Q_DECLARE_METATYPE(Range);
 

	
 
MainWindow::MainWindow(QWidget *parent) :
 
    QMainWindow(parent),
 
    ui(new Ui::MainWindow),
 
    aboutDialog(this),
 
    portControl(&serialPort),
 
    commandPanel(&serialPort),
 
    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");
 
    tbMenu->addAction(ui->plotToolBar->toggleViewAction());
 
    tbMenu->addAction(portControl.toolBar()->toggleViewAction());
 

	
 
    // init UI signals
 

	
 
    // menu signals
 
    QObject::connect(ui->actionHelpAbout, &QAction::triggered,
 
              &aboutDialog, &QWidget::show);
 

	
 
    QObject::connect(ui->actionExportCsv, &QAction::triggered,
 
                     this, &MainWindow::onExportCsv);
 

	
 
    ui->actionQuit->setShortcutContext(Qt::ApplicationShortcut);
 

	
 
    QObject::connect(ui->actionQuit, &QAction::triggered,
 
                     this, &MainWindow::close);
 

	
 
    // port control signals
 
    QObject::connect(&portControl, &PortControl::portToggled,
 
                     this, &MainWindow::onPortToggled);
 

	
 
    QObject::connect(&portControl, &PortControl::skipByteRequested,
 
                     &dataFormatPanel, &DataFormatPanel::requestSkipByte);
 

	
 
    QObject::connect(ui->spNumOfSamples, SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged),
 
                     this, &MainWindow::onNumOfSamplesChanged);
 

	
 
    QObject::connect(ui->cbAutoScale, &QCheckBox::toggled,
 
                     this, &MainWindow::onAutoScaleChecked);
 

	
 
    QObject::connect(ui->spYmin, SIGNAL(valueChanged(double)),
 
                     this, SLOT(onYScaleChanged()));
 

	
 
    QObject::connect(ui->spYmax, SIGNAL(valueChanged(double)),
 
                     this, SLOT(onYScaleChanged()));
 

	
0 comments (0 inline, 0 general)