Changeset - 3da4b5482573
[Not reviewed]
settings
0 11 2
Hasan Yavuz ÖZDERYA - 9 years ago 2016-09-04 17:56:19
hy@ozderya.net
implemented settings for simple binary reader
13 files changed with 189 insertions and 14 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
@@ -112,12 +112,13 @@ add_executable(${PROGRAM_NAME} WIN32
 
  src/asciireader.cpp
 
  src/asciireadersettings.cpp
 
  src/demoreader.cpp
 
  src/framedreader.cpp
 
  src/framedreadersettings.cpp
 
  src/plotmanager.cpp
 
  src/numberformat.cpp
 
  misc/windows_icon.rc
 
  ${UI_FILES}
 
  ${RES_FILES}
 
  )
 

	
 
# Use the Widgets module from Qt 5.
src/binarystreamreader.cpp
Show inline comments
 
@@ -176,13 +176,12 @@ void BinaryStreamReader::onDataReady()
 
    }
 
    emit dataAdded();
 

	
 
    delete channelSamples;
 
}
 

	
 

	
 
template<typename T> double BinaryStreamReader::readSampleAs()
 
{
 
    T data;
 

	
 
    _device->read((char*) &data, sizeof(data));
 

	
 
@@ -201,6 +200,16 @@ template<typename T> double BinaryStream
 
void BinaryStreamReader::addChannelData(unsigned int channel,
 
                                        double* data, unsigned size)
 
{
 
    _channelMan->addChannelData(channel, data, size);
 
    sampleCount += size;
 
}
 

	
 
void BinaryStreamReader::saveSettings(QSettings* settings)
 
{
 
    _settingsWidget.saveSettings(settings);
 
}
 

	
 
void BinaryStreamReader::loadSettings(QSettings* settings)
 
{
 
    _settingsWidget.loadSettings(settings);
 
}
src/binarystreamreader.h
Show inline comments
 
@@ -17,12 +17,14 @@
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef BINARYSTREAMREADER_H
 
#define BINARYSTREAMREADER_H
 

	
 
#include <QSettings>
 

	
 
#include "abstractreader.h"
 
#include "binarystreamreadersettings.h"
 

	
 
/**
 
 * Reads a simple stream of samples in binary form from the
 
 * device. There is no means of synchronization other than a button
 
@@ -33,12 +35,16 @@ class BinaryStreamReader : public Abstra
 
    Q_OBJECT
 
public:
 
    explicit BinaryStreamReader(QIODevice* device, ChannelManager* channelMan, QObject *parent = 0);
 
    QWidget* settingsWidget();
 
    unsigned numOfChannels();
 
    void enable(bool enabled = true);
 
    /// Stores settings into a `QSettings`
 
    void saveSettings(QSettings* settings);
 
    /// Loads settings from a `QSettings`.
 
    void loadSettings(QSettings* settings);
 

	
 
public slots:
 
    void pause(bool);
 

	
 
private:
 
    BinaryStreamReaderSettings _settingsWidget;
src/binarystreamreadersettings.cpp
Show inline comments
 
@@ -18,12 +18,13 @@
 
*/
 

	
 
#include "binarystreamreadersettings.h"
 
#include "ui_binarystreamreadersettings.h"
 

	
 
#include "utils.h"
 
#include "setting_defines.h"
 

	
 
BinaryStreamReaderSettings::BinaryStreamReaderSettings(QWidget *parent) :
 
    QWidget(parent),
 
    ui(new Ui::BinaryStreamReaderSettings)
 
{
 
    ui->setupUi(this);
 
@@ -58,6 +59,46 @@ NumberFormat BinaryStreamReaderSettings:
 
}
 

	
 
Endianness BinaryStreamReaderSettings::endianness()
 
{
 
    return ui->endiBox->currentSelection();
 
}
 

	
 
void BinaryStreamReaderSettings::saveSettings(QSettings* settings)
 
{
 
    settings->beginGroup(SettingGroup_Binary);
 
    settings->setValue(SG_Binary_NumOfChannels, numOfChannels());
 
    settings->setValue(SG_Binary_NumberFormat, numberFormatToStr(numberFormat()));
 
    settings->setValue(SG_Binary_Endianness,
 
                       endianness() == LittleEndian ? "little" : "big");
 
    settings->endGroup();
 
}
 

	
 
void BinaryStreamReaderSettings::loadSettings(QSettings* settings)
 
{
 
    settings->beginGroup(SettingGroup_Binary);
 

	
 
    // load number of channels
 
    ui->spNumOfChannels->setValue(
 
        settings->value(SG_Binary_NumOfChannels, numOfChannels()).toInt());
 

	
 
    // load number format
 
    NumberFormat nfSetting =
 
        strToNumberFormat(settings->value(SG_Binary_NumberFormat,
 
                                          QString()).toString());
 
    if (nfSetting == NumberFormat_INVALID) nfSetting = numberFormat();
 
    ui->nfBox->setSelection(nfSetting);
 

	
 
    // load endianness
 
    QString endiannessSetting =
 
        settings->value(SG_Binary_Endianness, QString()).toString();
 
    if (endiannessSetting == "little")
 
    {
 
        ui->endiBox->setSelection(LittleEndian);
 
    }
 
    else if (endiannessSetting == "big")
 
    {
 
        ui->endiBox->setSelection(BigEndian);
 
    } // else don't change
 

	
 
    settings->endGroup();
 
}
src/binarystreamreadersettings.h
Show inline comments
 
@@ -18,12 +18,14 @@
 
*/
 

	
 
#ifndef BINARYSTREAMREADERSETTINGS_H
 
#define BINARYSTREAMREADERSETTINGS_H
 

	
 
#include <QWidget>
 
#include <QSettings>
 

	
 
#include "numberformatbox.h"
 
#include "endiannessbox.h"
 

	
 
namespace Ui {
 
class BinaryStreamReaderSettings;
 
}
 
@@ -37,12 +39,17 @@ public:
 
    ~BinaryStreamReaderSettings();
 

	
 
    unsigned numOfChannels();
 
    NumberFormat numberFormat();
 
    Endianness endianness();
 

	
 
    /// Stores settings into a `QSettings`
 
    void saveSettings(QSettings* settings);
 
    /// Loads settings from a `QSettings`.
 
    void loadSettings(QSettings* settings);
 

	
 
signals:
 
    void numOfChannelsChanged(unsigned);
 
    void numberFormatChanged(NumberFormat);
 
    void skipByteRequested();
 
    void skipSampleRequested();
 

	
src/dataformatpanel.cpp
Show inline comments
 
@@ -166,12 +166,15 @@ void DataFormatPanel::saveSettings(QSett
 
    {
 
        format = "custom";
 
    }
 
    settings->setValue(SG_DataFormat_Format, format);
 

	
 
    settings->endGroup();
 

	
 
    // save reader settings
 
    bsReader.saveSettings(settings);
 
}
 

	
 
void DataFormatPanel::loadSettings(QSettings* settings)
 
{
 
    settings->beginGroup(SettingGroup_DataFormat);
 

	
 
@@ -193,7 +196,10 @@ void DataFormatPanel::loadSettings(QSett
 
    {
 
        selectReader(&framedReader);
 
        ui->rbFramed->setChecked(true);
 
    } // else current selection stays
 

	
 
    settings->endGroup();
 

	
 
    // load reader settings
 
    bsReader.loadSettings(settings);
 
}
src/endiannessbox.cpp
Show inline comments
 
@@ -45,6 +45,18 @@ Endianness EndiannessBox::currentSelecti
 
    }
 
    else
 
    {
 
        return BigEndian;
 
    }
 
}
 

	
 
void EndiannessBox::setSelection(Endianness endianness)
 
{
 
    if (endianness == LittleEndian)
 
    {
 
        ui->rbLittleE->setChecked(true);
 
    }
 
    else // big endian
 
    {
 
        ui->rbBigE->setChecked(true);
 
    }
 
}
src/endiannessbox.h
Show inline comments
 
@@ -37,13 +37,16 @@ class EndiannessBox : public QWidget
 
    Q_OBJECT
 

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

	
 
    Endianness currentSelection(); ///< currently selected endianness
 
    /// currently selected endianness
 
    Endianness currentSelection();
 
    /// change the currently selected endianness
 
    void setSelection(Endianness endianness);
 

	
 
signals:
 
    /// Signaled when endianness selection is changed
 
    void selectionChanged(Endianness endianness);
 

	
 
private:
src/numberformat.cpp
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2016 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 <QMap>
 

	
 
#include "numberformat.h"
 

	
 
QMap<NumberFormat, QString> mapping({
 
        {NumberFormat_uint8, "uint8"},
 
        {NumberFormat_uint16, "uint16"},
 
        {NumberFormat_uint32, "uint32"},
 
        {NumberFormat_int8, "int8"},
 
        {NumberFormat_int16, "int16"},
 
        {NumberFormat_int32, "int32"},
 
        {NumberFormat_float, "float"}
 
    });
 

	
 
QString numberFormatToStr(NumberFormat nf)
 
{
 
    return mapping.value(nf);
 
}
 

	
 
NumberFormat strToNumberFormat(QString str)
 
{
 
    return mapping.key(str, NumberFormat_INVALID);
 
}
src/numberformat.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2016 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 NUMBERFORMAT_H
 
#define NUMBERFORMAT_H
 

	
 
#include <QString>
 

	
 
enum NumberFormat
 
{
 
    NumberFormat_uint8,
 
    NumberFormat_uint16,
 
    NumberFormat_uint32,
 
    NumberFormat_int8,
 
    NumberFormat_int16,
 
    NumberFormat_int32,
 
    NumberFormat_float,
 
    NumberFormat_INVALID ///< used for error cases
 
};
 

	
 
/// Convert `NumberFormat` to string for representation
 
QString numberFormatToStr(NumberFormat nf);
 

	
 
/// Convert string to `NumberFormat`
 
NumberFormat strToNumberFormat(QString str);
 

	
 
#endif // NUMBERFORMAT_H
src/numberformatbox.cpp
Show inline comments
 
@@ -51,6 +51,11 @@ void NumberFormatBox::onButtonToggled(in
 
}
 

	
 
NumberFormat NumberFormatBox::currentSelection()
 
{
 
    return (NumberFormat) buttonGroup.checkedId();
 
}
 

	
 
void NumberFormatBox::setSelection(NumberFormat nf)
 
{
 
    buttonGroup.button(nf)->setChecked(true);
 
}
src/numberformatbox.h
Show inline comments
 
@@ -20,36 +20,30 @@
 
#ifndef NUMBERFORMATBOX_H
 
#define NUMBERFORMATBOX_H
 

	
 
#include <QWidget>
 
#include <QButtonGroup>
 

	
 
#include "numberformat.h"
 

	
 
namespace Ui {
 
class NumberFormatBox;
 
}
 

	
 
enum NumberFormat
 
{
 
    NumberFormat_uint8,
 
    NumberFormat_uint16,
 
    NumberFormat_uint32,
 
    NumberFormat_int8,
 
    NumberFormat_int16,
 
    NumberFormat_int32,
 
    NumberFormat_float,
 
};
 

	
 
class NumberFormatBox : public QWidget
 
{
 
    Q_OBJECT
 

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

	
 
    NumberFormat currentSelection(); ///< returns the currently selected number format
 
    /// returns the currently selected number format
 
    NumberFormat currentSelection();
 
    /// change the currently selected number format
 
    void setSelection(NumberFormat nf);
 

	
 
signals:
 
    /// Signaled when number format selection is changed
 
    void selectionChanged(NumberFormat numberFormat);
 

	
 
private:
src/setting_defines.h
Show inline comments
 
@@ -20,12 +20,13 @@
 
#ifndef SETTING_DEFINES_H
 
#define SETTING_DEFINES_H
 

	
 
const char SettingGroup_MainWindow[] = "MainWindow";
 
const char SettingGroup_Port[] = "Port";
 
const char SettingGroup_DataFormat[] = "DataFormat";
 
const char SettingGroup_Binary[] = "DataFormat_Binary";
 
const char SettingGroup_Channels[] = "Channels";
 
const char SettingGroup_Plot[] = "Plot";
 
const char SettingGroup_Commands[] = "Commands";
 

	
 
// mainwindow setting keys
 
const char SG_MainWindow_Size[] = "size";
 
@@ -41,7 +42,12 @@ const char SG_Port_DataBits[] = "dataBit
 
const char SG_Port_StopBits[] = "stopBits";
 
const char SG_Port_FlowControl[] = "flowControl";
 

	
 
// data format panel keys
 
const char SG_DataFormat_Format[] = "format";
 

	
 
// binary stream reader keys
 
const char SG_Binary_NumOfChannels[] = "numOfChannels";
 
const char SG_Binary_NumberFormat[] = "numberFormat";
 
const char SG_Binary_Endianness[] = "endianness";
 

	
 
#endif // SETTING_DEFINES_H
0 comments (0 inline, 0 general)