Changeset - 81a003dc29e2
[Not reviewed]
default
0 4 0
Mehmet ASLAN - 7 years ago 2018-06-24 07:14:21
aaslan.mehmet@hotmail.com
AbstractReader should only implement generic enable function.
4 files changed with 20 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/abstractreader.cpp
Show inline comments
 
@@ -32,23 +32,21 @@ void AbstractReader::pause(bool enabled)
 

	
 
unsigned AbstractReader::numChannels() const
 
{
 
    // TODO: an alternative is to never set _numChannels to '0'
 
    // do not allow '0'
 
    return _numChannels == 0 ? 1 : _numChannels;
 
}
 

	
 
void AbstractReader::enable(bool enabled)
 
{
 
    if (enabled)
 
    {
 
        firstReadAfterEnable = true;
 
        QObject::connect(_device, &QIODevice::readyRead,
 
                         this, &AbstractReader::onDataReady);
 
    }
 
    else
 
    {
 
        firstReadAfterEnable = false;
 
        QObject::disconnect(_device, 0, this, 0);
 
        disconnectSinks();
 
    }
 
}
src/abstractreader.h
Show inline comments
 
@@ -35,44 +35,43 @@ class AbstractReader : public QObject, p
 
    Q_OBJECT
 
public:
 
    explicit AbstractReader(QIODevice* device, QObject* parent = 0);
 

	
 
    /**
 
     * Returns a widget to be shown in data format panel when reader
 
     * is selected.
 
     */
 
    virtual QWidget* settingsWidget() = 0;
 

	
 
    /// Reader should only read when enabled. Default state should be
 
    /// 'disabled'.
 
    void enable(bool enabled = true);
 
    virtual void enable(bool enabled = true);
 

	
 
    /// None of the current readers support X channel at the moment
 
    bool hasX() const final { return false; };
 

	
 
    unsigned numChannels() const;
 

	
 
signals:
 
    // TODO: should we keep this?
 
    void numOfChannelsChanged(unsigned);
 

	
 
public slots:
 
    /**
 
     * Pauses the reading.
 
     *
 
     * Reader should actually continue reading to keep the
 
     * synchronization but shouldn't commit data.
 
     */
 
    void pause(bool enabled);
 

	
 
protected:
 
    QIODevice* _device;
 
    bool paused;
 
    unsigned _numChannels;
 
    bool firstReadAfterEnable = false;
 

	
 
protected slots:
 
    /// all derived readers has to override this function
 
    virtual void onDataReady() = 0;
 
};
 

	
 
#endif // ABSTRACTREADER_H
src/asciireader.cpp
Show inline comments
 
@@ -49,24 +49,40 @@ AsciiReader::AsciiReader(QIODevice* devi
 
    connect(&_settingsWidget, &AsciiReaderSettings::delimiterChanged,
 
            [this](QChar d)
 
            {
 
                delimiter = d;
 
            });
 
}
 

	
 
QWidget* AsciiReader::settingsWidget()
 
{
 
    return &_settingsWidget;
 
}
 

	
 
void AsciiReader::enable(bool enabled)
 
{
 
    if (enabled)
 
    {
 
        firstReadAfterEnable = true;
 
        QObject::connect(_device, &QIODevice::readyRead,
 
                         this, &AsciiReader::onDataReady);
 
    }
 
    else
 
    {
 
        firstReadAfterEnable = false;
 
        QObject::disconnect(_device, 0, this, 0);
 
        disconnectSinks();
 
    }
 
}
 

	
 
void AsciiReader::onDataReady()
 
{
 
    while(_device->canReadLine())
 
    {
 
        QString line = QString(_device->readLine());
 

	
 
        // discard only once when we just started reading
 
        if (firstReadAfterEnable)
 
        {
 
            firstReadAfterEnable = false;
 
            continue;
 
        }
src/asciireader.h
Show inline comments
 
@@ -23,28 +23,31 @@
 
#include <QSettings>
 

	
 
#include "abstractreader.h"
 
#include "asciireadersettings.h"
 

	
 
class AsciiReader : public AbstractReader
 
{
 
    Q_OBJECT
 

	
 
public:
 
    explicit AsciiReader(QIODevice* device, QObject *parent = 0);
 
    QWidget* settingsWidget();
 
    void enable(bool enabled);
 
    /// Stores settings into a `QSettings`
 
    void saveSettings(QSettings* settings);
 
    /// Loads settings from a `QSettings`.
 
    void loadSettings(QSettings* settings);
 

	
 
private:
 
    AsciiReaderSettings _settingsWidget;
 
    /// number of channels will be determined from incoming data
 
    unsigned autoNumOfChannels;
 
    QChar delimiter; ///< selected column delimiter
 

	
 
    bool firstReadAfterEnable = false;
 

	
 
private slots:
 
    void onDataReady() override;
 
};
 

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