Changeset - 335ac381f2eb
[Not reviewed]
reader-stat
0 4 2
Hasan Yavuz ÖZDERYA - 6 years ago 2019-04-17 14:45:18
hy@ozderya.net
move bps label functionality to a separate class
6 files changed with 138 insertions and 42 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -136,6 +136,7 @@ add_executable(${PROGRAM_NAME} WIN32
 
  src/samplecounter.cpp
 
  src/ledwidget.cpp
 
  src/datatextview.cpp
 
  src/bpslabel.cpp
 
  misc/windows_icon.rc
 
  ${UI_FILES}
 
  ${RES_FILES}
serialplot.pro
Show inline comments
 
@@ -72,7 +72,8 @@ SOURCES += \
 
    src/updatechecker.cpp \
 
    src/updatecheckdialog.cpp \
 
    src/demoreadersettings.cpp \
 
    src/datatextview.cpp
 
    src/datatextview.cpp \
 
    src/bpslabel.cpp
 

	
 
HEADERS += \
 
    src/mainwindow.h \
 
@@ -116,7 +117,8 @@ HEADERS += \
 
    src/updatechecker.h \
 
    src/updatecheckdialog.h \
 
    src/demoreadersettings.h \
 
    src/datatextview.h
 
    src/datatextview.h \
 
    src/bpslabel.h
 

	
 
FORMS += \
 
    src/mainwindow.ui \
src/bpslabel.cpp
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2019 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 "bpslabel.h"
 

	
 
const char* BPS_TOOLTIP = "bits per second";
 
const char* BPS_TOOLTIP_ERR = "Maximum baud rate may be reached!";
 

	
 
BPSLabel::BPSLabel(PortControl* portControl,
 
                   DataFormatPanel* dataFormatPanel,
 
                   QWidget *parent) :
 
    QLabel(parent)
 
{
 
    _portControl = portControl;
 
    _dataFormatPanel = dataFormatPanel;
 

	
 
    setText("0bps");
 
    setToolTip(tr(BPS_TOOLTIP));
 

	
 
    connect(&bpsTimer, &QTimer::timeout,
 
            this, &BPSLabel::onBpsTimeout);
 

	
 
    connect(portControl, &PortControl::portToggled,
 
            this, &BPSLabel::onPortToggled);
 
}
 

	
 
void BPSLabel::onBpsTimeout()
 
{
 
    unsigned bits = _dataFormatPanel->getBytesRead() * 8;
 
    unsigned maxBps = _portControl->maxBitRate();
 
    QString str;
 
    if (bits >= maxBps)
 
    {
 
        // TODO: an icon for bps warning
 
        str = QString(tr("!%1/%2bps")).arg(bits).arg(maxBps);
 
        setToolTip(tr(BPS_TOOLTIP_ERR));
 
    }
 
    else
 
    {
 
        str = QString(tr("%1bps")).arg(bits);
 
        setToolTip(tr(BPS_TOOLTIP));
 
    }
 
    setText(str);
 
}
 

	
 
void BPSLabel::onPortToggled(bool open)
 
{
 
    if (open)
 
    {
 
        bpsTimer.start(1000);
 
    }
 
    else
 
    {
 
        bpsTimer.stop();
 
        // if not cleared last displayed value is stuck
 
        setText("0bps");
 
        setToolTip(tr(BPS_TOOLTIP));
 
    }
 
}
src/bpslabel.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2019 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 BPSLABEL_H
 
#define BPSLABEL_H
 

	
 
#include <QLabel>
 
#include <QTimer>
 

	
 
#include "portcontrol.h"
 
#include "dataformatpanel.h"
 

	
 
/**
 
 * Displays bits per second read from device.
 
 *
 
 * Displays a warning if maximum bit rate is reached.
 
 */
 
class BPSLabel : public QLabel
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit BPSLabel(PortControl* portControl,
 
                      DataFormatPanel* dataFormatPanel,
 
                      QWidget *parent = 0);
 

	
 
private:
 
    PortControl* _portControl;
 
    DataFormatPanel* _dataFormatPanel;
 
    QTimer bpsTimer;
 

	
 
private slots:
 
    void onBpsTimeout();
 
    void onPortToggled(bool open);
 
};
 

	
 
#endif // BPSLABEL_H
src/mainwindow.cpp
Show inline comments
 
@@ -59,9 +59,6 @@ const QMap<int, QString> panelSettingMap
 
        {6, "Log"}
 
    });
 

	
 
const char* BPS_TOOLTIP = "bits per second";
 
const char* BPS_TOOLTIP_ERR = "Maximum baud rate may be reached!";
 

	
 
MainWindow::MainWindow(QWidget *parent) :
 
    QMainWindow(parent),
 
    ui(new Ui::MainWindow),
 
@@ -73,7 +70,8 @@ MainWindow::MainWindow(QWidget *parent) 
 
    dataFormatPanel(&serialPort),
 
    recordPanel(&stream),
 
    textView(&stream),
 
    updateCheckDialog(this)
 
    updateCheckDialog(this),
 
    bpsLabel(&portControl, &dataFormatPanel, this)
 
{
 
    ui->setupUi(this);
 

	
 
@@ -235,11 +233,7 @@ MainWindow::MainWindow(QWidget *parent) 
 
    plotMan->setPlotWidth(plotControlPanel.plotWidth());
 

	
 
    // init bps (bits per second) counter
 
    bpsLabel.setText("0bps");
 
    bpsLabel.setToolTip(tr(BPS_TOOLTIP));
 
    ui->statusBar->addPermanentWidget(&bpsLabel);
 
    connect(&bpsTimer, &QTimer::timeout,
 
            this, &MainWindow::onBpsTimeout);
 

	
 
    // Init sps (sample per second) counter
 
    spsLabel.setText("0sps");
 
@@ -366,16 +360,8 @@ void MainWindow::onPortToggled(bool open
 
    if (open && isDemoRunning()) enableDemo(false);
 
    ui->actionDemoMode->setEnabled(!open);
 

	
 
    if (open)
 
    if (!open)
 
    {
 
        bpsTimer.start(1000);
 
    }
 
    else
 
    {
 
        bpsTimer.stop();
 
        // if not cleared last displayed value is stuck
 
        bpsLabel.setText("0bps");
 
        bpsLabel.setToolTip(tr(BPS_TOOLTIP));
 
        spsLabel.setText("0sps");
 
    }
 
}
 
@@ -405,25 +391,6 @@ void MainWindow::onSpsChanged(float sps)
 
    spsLabel.setText(QString::number(sps, 'f', precision) + "sps");
 
}
 

	
 
void MainWindow::onBpsTimeout()
 
{
 
    unsigned bits = dataFormatPanel.getBytesRead() * 8;
 
    unsigned maxBps = portControl.maxBitRate();
 
    QString str;
 
    if (bits >= maxBps)
 
    {
 
        // TODO: an icon for bps warning
 
        str = QString(tr("!%1/%2bps")).arg(bits).arg(maxBps);
 
        bpsLabel.setToolTip(tr(BPS_TOOLTIP_ERR));
 
    }
 
    else
 
    {
 
        str = QString(tr("%1bps")).arg(bits);
 
        bpsLabel.setToolTip(tr(BPS_TOOLTIP));
 
    }
 
    bpsLabel.setText(str);
 
}
 

	
 
bool MainWindow::isDemoRunning()
 
{
 
    return ui->actionDemoMode->isChecked();
src/mainwindow.h
Show inline comments
 
@@ -47,6 +47,7 @@
 
#include "updatecheckdialog.h"
 
#include "samplecounter.h"
 
#include "datatextview.h"
 
#include "bpslabel.h"
 

	
 
namespace Ui {
 
class MainWindow;
 
@@ -83,11 +84,8 @@ private:
 
    QWidget* secondaryPlot;
 
    SnapshotManager snapshotMan;
 
    SampleCounter sampleCounter;
 
    /// bit counter timer
 
    QTimer bpsTimer;
 

	
 
    QLabel spsLabel;
 
    QLabel bpsLabel;
 
    CommandPanel commandPanel;
 
    DataFormatPanel dataFormatPanel;
 
    RecordPanel recordPanel;
 
@@ -95,6 +93,7 @@ private:
 
    PlotMenu plotMenu;
 
    DataTextView textView;
 
    UpdateCheckDialog updateCheckDialog;
 
    BPSLabel bpsLabel;
 

	
 
    /// Returns true if demo is running
 
    bool isDemoRunning();
 
@@ -122,7 +121,6 @@ private slots:
 

	
 
    void clearPlot();
 
    void onSpsChanged(float sps);
 
    void onBpsTimeout();
 
    void enableDemo(bool enabled);
 
    void showBarPlot(bool show);
 

	
0 comments (0 inline, 0 general)