Changeset - 3be63f78737a
[Not reviewed]
Hasan Yavuz ÖZDERYA - 9 years ago 2017-01-01 14:43:12
hy@ozderya.net
somewhat integrated new channelinfomodel
10 files changed with 103 insertions and 101 deletions:
0 comments (0 inline, 0 general)
src/channelinfomodel.cpp
Show inline comments
 
@@ -16,19 +16,12 @@
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "channelinfomodel.h"
 

	
 
enum ChannelInfoColumn
 
{
 
    COLUMN_NAME = 0,
 
    COLUMN_VISIBILITY,
 
    COLUMN_COUNT
 
};
 

	
 
const QColor colors[8] =
 
{
 
    QColor(237,97,68),
 
    QColor(92,200,96),
 
    QColor(225,98,207),
 
    QColor(163,195,58),
 
@@ -149,21 +142,23 @@ bool ChannelInfoModel::setData(const QMo
 
    // set name
 
    if (index.column() == COLUMN_NAME)
 
    {
 
        if (role == Qt::DisplayRole || role == Qt::EditRole)
 
        {
 
            infos[index.row()].name = value.toString();
 
            emit dataChanged(index, index, QVector<int>({role}));
 
            return true;
 
        }
 
    } // set visibility
 
    else if (index.column() == COLUMN_VISIBILITY)
 
    {
 
        if (role == Qt::CheckStateRole)
 
        {
 
            bool checked = value.toInt() == Qt::Checked;
 
            infos[index.row()].visibility = checked;
 
            emit dataChanged(index, index, QVector<int>({role}));
 
            return true;
 
        }
 
    }
 

	
 
    // invalid index/role
 
    return false;
src/channelinfomodel.h
Show inline comments
 
@@ -25,12 +25,19 @@
 

	
 
class ChannelInfoModel : public QAbstractTableModel
 
{
 
    Q_OBJECT
 

	
 
public:
 
    enum ChannelInfoColumn
 
    {
 
        COLUMN_NAME = 0,
 
        COLUMN_VISIBILITY,
 
        COLUMN_COUNT
 
    };
 

	
 
    explicit ChannelInfoModel(unsigned numberOfChannels, QObject *parent = 0);
 

	
 
    // implemented from QAbstractItemModel
 
    int           rowCount(const QModelIndex &parent = QModelIndex()) const;
 
    int           columnCount(const QModelIndex &parent = QModelIndex()) const;
 
    QVariant      data(const QModelIndex &index, int role = Qt::DisplayRole) const;
src/channelmanager.cpp
Show inline comments
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 
  Copyright © 2017 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
 
@@ -14,36 +14,33 @@
 
  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 <QStringList>
 
#include <QModelIndex>
 

	
 
#include <QtDebug>
 

	
 
#include "channelmanager.h"
 
#include "setting_defines.h"
 

	
 
ChannelManager::ChannelManager(unsigned numberOfChannels, unsigned numberOfSamples, QObject *parent) :
 
    QObject(parent)
 
    QObject(parent),
 
    _infoModel(numberOfChannels)
 
{
 
    _numOfChannels = numberOfChannels;
 
    _numOfSamples = numberOfSamples;
 

	
 
    QStringList channelNamesList;
 

	
 
    for (unsigned int i = 0; i < numberOfChannels; i++)
 
    {
 
        channelBuffers.append(new FrameBuffer(numberOfSamples));
 
        channelNamesList << QString("Channel %1").arg(i+1);
 
    }
 

	
 
    _channelNames.setStringList(channelNamesList);
 

	
 
    connect(&_channelNames, &QStringListModel::dataChanged,
 
            this, &ChannelManager::onChannelNameDataChange);
 
    connect(&_infoModel, &ChannelInfoModel::dataChanged,
 
            this, &ChannelManager::onChannelInfoChanged);
 
}
 

	
 
ChannelManager::~ChannelManager()
 
{
 
    for (auto buffer : channelBuffers)
 
    {
 
@@ -68,25 +65,25 @@ void ChannelManager::setNumOfChannels(un
 
    if (number > oldNum)
 
    {
 
        // add new channels
 
        for (unsigned int i = 0; i < number - oldNum; i++)
 
        {
 
            channelBuffers.append(new FrameBuffer(_numOfSamples));
 
            addChannelName(QString("Channel %1").arg(oldNum+i+1));
 
        }
 
    }
 
    else if(number < oldNum)
 
    {
 
        // remove channels
 
        for (unsigned int i = oldNum-1; i > number-1; i--)
 
        {
 
            delete channelBuffers.takeLast();
 
            _channelNames.removeRow(i);
 
        }
 
    }
 

	
 
    _infoModel.setNumOfChannels(number);
 

	
 
    emit numOfChannelsChanged(number);
 
}
 

	
 
void ChannelManager::setNumOfSamples(unsigned number)
 
{
 
    _numOfSamples = number;
 
@@ -101,45 +98,70 @@ void ChannelManager::setNumOfSamples(uns
 

	
 
FrameBuffer* ChannelManager::channelBuffer(unsigned channel)
 
{
 
    return channelBuffers[channel];
 
}
 

	
 
QStringListModel* ChannelManager::channelNames()
 
ChannelInfoModel* ChannelManager::infoModel()
 
{
 
    return &_channelNames;
 
    return &_infoModel;
 
}
 

	
 
QString ChannelManager::channelName(unsigned channel)
 
{
 
    return _channelNames.data(_channelNames.index(channel, 0), Qt::DisplayRole).toString();
 
}
 

	
 
void ChannelManager::setChannelName(unsigned channel, QString name)
 
{
 
    _channelNames.setData(_channelNames.index(channel, 0), QVariant(name), Qt::DisplayRole);
 
    return _infoModel.data(_infoModel.index(channel, ChannelInfoModel::COLUMN_NAME),
 
                           Qt::DisplayRole).toString();
 
}
 

	
 
void ChannelManager::addChannelName(QString name)
 
QStringList ChannelManager::channelNames()
 
{
 
    _channelNames.insertRow(_channelNames.rowCount());
 
    setChannelName(_channelNames.rowCount()-1, name);
 
    QStringList list;
 
    for (unsigned ci = 0; ci < _numOfChannels; ci++)
 
    {
 
        list << channelName(ci);
 
    }
 
    return list;
 
}
 

	
 
void ChannelManager::onChannelNameDataChange(const QModelIndex & topLeft,
 
void ChannelManager::onChannelInfoChanged(const QModelIndex & topLeft,
 
                                             const QModelIndex & bottomRight,
 
                                             const QVector<int> & roles)
 
{
 
    Q_UNUSED(roles);
 
    int start = topLeft.row();
 
    int end = bottomRight.row();
 
    int col = topLeft.column();
 

	
 
    // TODO: maybe check `roles` parameter, can't think of a reason for current use case
 
    for (int i = start; i <= end; i++)
 
    for (int ci = start; ci <= end; ci++)
 
    {
 
        for (auto role : roles)
 
        {
 
            switch (role)
 
            {
 
                case Qt::EditRole:
 
                    if (col == ChannelInfoModel::COLUMN_NAME)
 
    {
 
        emit channelNameChanged(i, channelName(i));
 
                        qDebug() << channelName(ci);
 
                        emit channelNameChanged(ci, channelName(ci));
 
                    }
 
                    break;
 
                case Qt::ForegroundRole:
 
                    if (col == ChannelInfoModel::COLUMN_NAME)
 
                    {
 
                        // TODO: emit channel color changed
 
                    }
 
                    break;
 
                case Qt::CheckStateRole:
 
                    if (col == ChannelInfoModel::COLUMN_VISIBILITY)
 
                    {
 
                        // TODO: emit visibility
 
                    }
 
                    break;
 
            }
 
        }
 
        // emit channelNameChanged(i, channelName(i));
 
    }
 
}
 

	
 
void ChannelManager::addChannelData(unsigned channel, double* data, unsigned size)
 
{
 
    channelBuffer(channel)->addSamples(data, size);
 
@@ -162,11 +184,12 @@ void ChannelManager::loadSettings(QSetti
 
{
 
    settings->beginGroup(SettingGroup_Channels);
 
    settings->beginReadArray(SG_Channels_Channel);
 
    for (unsigned i = 0; i < numOfChannels(); i++)
 
    {
 
        settings->setArrayIndex(i);
 
        setChannelName(i, settings->value(SG_Channels_Name, channelName(i)).toString());
 
        // TODO: fix load settings
 
        // setChannelName(i, settings->value(SG_Channels_Name, channelName(i)).toString());
 
    }
 
    settings->endArray();
 
    settings->endGroup();
 
}
src/channelmanager.h
Show inline comments
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 
  Copyright © 2017 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
 
@@ -18,56 +18,61 @@
 
*/
 

	
 
#ifndef CHANNELMANAGER_H
 
#define CHANNELMANAGER_H
 

	
 
#include <QObject>
 
#include <QStringListModel>
 
#include <QStringList>
 
#include <QModelIndex>
 
#include <QVector>
 
#include <QSettings>
 

	
 
#include "framebuffer.h"
 
#include "channelinfomodel.h"
 

	
 
class ChannelManager : public QObject
 
{
 
    Q_OBJECT
 
public:
 
    explicit ChannelManager(unsigned numberOfChannels, unsigned numberOfSamples, QObject *parent = 0);
 
    ~ChannelManager();
 

	
 
    unsigned numOfChannels();
 
    unsigned numOfSamples();
 
    FrameBuffer* channelBuffer(unsigned channel);
 
    QStringListModel* channelNames();
 
    // QStringListModel* channelNames();
 
    QString channelName(unsigned channel);
 
    /// Stores channel names into a `QSettings`
 
    void saveSettings(QSettings* settings);
 
    /// Loads channel names from a `QSettings`.
 
    void loadSettings(QSettings* settings);
 
    /// Returns a model that manages channel information (name, color etc)
 
    ChannelInfoModel* infoModel();
 
    /// Returns a list of channel names
 
    QStringList channelNames();
 

	
 
signals:
 
    void numOfChannelsChanged(unsigned value);
 
    void numOfSamplesChanged(unsigned value);
 
    void channelNameChanged(unsigned channel, QString name);
 

	
 
public slots:
 
    void setNumOfChannels(unsigned number);
 
    void setNumOfSamples(unsigned number);
 
    void setChannelName(unsigned channel, QString name);
 
    void addChannelData(unsigned channel, double* data, unsigned size);
 

	
 
private:
 
    unsigned _numOfChannels;
 
    unsigned _numOfSamples;
 
    QList<FrameBuffer*> channelBuffers;
 
    QStringListModel _channelNames;
 
    // QStringListModel _channelNames;
 
    ChannelInfoModel _infoModel;
 

	
 
    void addChannelName(QString name); ///< appends a new channel name at the end of list
 

	
 
private slots:
 
    void onChannelNameDataChange(const QModelIndex & topLeft,
 
    void onChannelInfoChanged(const QModelIndex & topLeft,
 
                                 const QModelIndex & bottomRight,
 
                                 const QVector<int> & roles = QVector<int> ());
 
};
 

	
 
#endif // CHANNELMANAGER_H
src/main.cpp
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 
  Copyright © 2017 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
 
@@ -21,22 +21,12 @@
 
#include <QtGlobal>
 

	
 
#include "mainwindow.h"
 
#include "tooltipfilter.h"
 
#include "version.h"
 

	
 
// test code
 
#include <QTableView>
 
#include <QSpinBox>
 
#include <QColorDialog>
 
#include "channelinfomodel.h"
 
#include "utils.h"
 

	
 
#include "color_selector.hpp"
 
#include "color_dialog.hpp"
 

	
 
MainWindow* pMainWindow;
 

	
 
void messageHandler(QtMsgType type, const QMessageLogContext &context,
 
                    const QString &msg)
 
{
 
    // TODO: don't call MainWindow::messageHandler if window is destroyed
 
@@ -57,46 +47,8 @@ int main(int argc, char *argv[])
 
    // log application information
 
    qDebug() << "SerialPlot" << VERSION_STRING;
 
    qDebug() << "Revision" << VERSION_REVISION;
 

	
 
    w.show();
 

	
 
    // test code
 
    ChannelInfoModel cim(2, &a);
 
    QTableView tv;
 
    QSpinBox cb;
 

	
 
    QObject::connect(&cb, SELECT<int>::OVERLOAD_OF(&QSpinBox::valueChanged), [&cim](int value)
 
                     {
 
                         cim.setNumOfChannels(value);
 
                     });
 

	
 
    tv.setModel(&cim);
 

	
 
    tv.show();
 
    // cb.show();
 

	
 
    cim.setNumOfChannels(3);
 
    cim.setNumOfChannels(7);
 
    cim.setNumOfChannels(10);
 

	
 

	
 
    color_widgets::ColorSelector cpicker;
 
    cpicker.setColor(QColor("red"));
 
    cpicker.show();
 

	
 
    QObject::connect(tv.selectionModel(), &QItemSelectionModel::currentRowChanged,
 
                     [&cim, &cpicker](const QModelIndex &current, const QModelIndex &previous)
 
                     {
 
                         cpicker.setColor(cim.data(current, Qt::ForegroundRole).value<QColor>());
 
                     });
 

	
 
    QObject::connect(&cpicker, &color_widgets::ColorSelector::colorChanged,
 
                     [&cim, &tv](QColor color)
 
                     {
 
                         auto index = tv.selectionModel()->currentIndex();
 
                         index = index.sibling(index.row(), 0);
 
                         cim.setData(index, color, Qt::ForegroundRole);
 
                     });
 

	
 
    return a.exec();
 
}
src/mainwindow.cpp
Show inline comments
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 
  Copyright © 2017 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
 
@@ -169,13 +169,13 @@ MainWindow::MainWindow(QWidget *parent) 
 
    connect(&channelMan, &ChannelManager::numOfChannelsChanged,
 
            this, &MainWindow::onNumOfChannelsChanged);
 

	
 
    connect(&channelMan, &ChannelManager::channelNameChanged,
 
            this, &MainWindow::onChannelNameChanged);
 

	
 
    plotControlPanel.setChannelNamesModel(channelMan.channelNames());
 
    plotControlPanel.setChannelInfoModel(channelMan.infoModel());
 

	
 
    // init curve list
 
    for (unsigned int i = 0; i < numOfChannels; i++)
 
    {
 
        plotMan->addCurve(channelMan.channelName(i), channelMan.channelBuffer(i));
 
    }
src/plotcontrolpanel.cpp
Show inline comments
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 
  Copyright © 2017 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
 
@@ -20,12 +20,13 @@
 
#include <QVariant>
 
#include <QMessageBox>
 
#include <QCheckBox>
 

	
 
#include <math.h>
 

	
 
#include "color_selector.hpp"
 
#include "plotcontrolpanel.h"
 
#include "ui_plotcontrolpanel.h"
 
#include "setting_defines.h"
 

	
 
/// Confirm if #samples is being set to a value greater than this
 
const int NUMSAMPLES_CONFIRM_AT = 10000;
 
@@ -201,15 +202,33 @@ void PlotControlPanel::onRangeSelected()
 
    Range r = ui->cbRangePresets->currentData().value<Range>();
 
    ui->spYmin->setValue(r.rmin);
 
    ui->spYmax->setValue(r.rmax);
 
    ui->cbAutoScale->setChecked(false);
 
}
 

	
 
void PlotControlPanel::setChannelNamesModel(QAbstractItemModel * model)
 
void PlotControlPanel::setChannelInfoModel(ChannelInfoModel* model)
 
{
 
    ui->tvChannelInfo->setModel(model);
 

	
 
    // channel color selector
 
    QObject::connect(ui->tvChannelInfo->selectionModel(), &QItemSelectionModel::currentRowChanged,
 
                     [this](const QModelIndex &current, const QModelIndex &previous)
 
{
 
    ui->lvChannelNames->setModel(model);
 
                         auto model = ui->tvChannelInfo->model();
 
                         QColor color = model->data(current, Qt::ForegroundRole).value<QColor>();
 
                         ui->colorSelector->setColor(color);
 
                         // cpicker.setColor(cim.data(current, Qt::ForegroundRole).value<QColor>());
 
                     });
 

	
 
    QObject::connect(ui->colorSelector, &color_widgets::ColorSelector::colorChanged,
 
                     [this](QColor color)
 
                     {
 
                         auto index = ui->tvChannelInfo->selectionModel()->currentIndex();
 
                         // index = index.sibling(index.row(), 0);
 
                         ui->tvChannelInfo->model()->setData(index, color, Qt::ForegroundRole);
 
                     });
 
}
 

	
 
void PlotControlPanel::saveSettings(QSettings* settings)
 
{
 
    settings->beginGroup(SettingGroup_Plot);
 
    settings->setValue(SG_Plot_NumOfSamples, numOfSamples());
src/plotcontrolpanel.h
Show inline comments
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 
  Copyright © 2017 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
 
@@ -18,15 +18,16 @@
 
*/
 

	
 
#ifndef PLOTCONTROLPANEL_H
 
#define PLOTCONTROLPANEL_H
 

	
 
#include <QWidget>
 
#include <QAbstractItemModel>
 
#include <QSettings>
 

	
 
#include "channelinfomodel.h"
 

	
 
namespace Ui {
 
class PlotControlPanel;
 
}
 

	
 
class PlotControlPanel : public QWidget
 
{
 
@@ -38,13 +39,13 @@ public:
 

	
 
    unsigned numOfSamples();
 
    bool autoScale();
 
    double yMax();
 
    double yMin();
 

	
 
    void setChannelNamesModel(QAbstractItemModel * model);
 
    void setChannelInfoModel(ChannelInfoModel* model);
 

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

	
src/plotcontrolpanel.ui
Show inline comments
 
@@ -35,13 +35,13 @@
 
         </property>
 
        </widget>
 
       </item>
 
      </layout>
 
     </item>
 
     <item>
 
      <widget class="QListView" name="lvChannelNames">
 
      <widget class="QTableView" name="tvChannelInfo">
 
       <property name="maximumSize">
 
        <size>
 
         <width>16777215</width>
 
         <height>170</height>
 
        </size>
 
       </property>
src/snapshotmanager.cpp
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 
  Copyright © 2017 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
 
@@ -73,13 +73,13 @@ Snapshot* SnapshotManager::makeSnapshot(
 
        snapshot->data.append(QVector<QPointF>(numOfSamples));
 
        for (unsigned i = 0; i < numOfSamples; i++)
 
        {
 
            snapshot->data[ci][i] = QPointF(i, _channelMan->channelBuffer(ci)->sample(i));
 
        }
 
    }
 
    snapshot->setChannelNames(_channelMan->channelNames()->stringList());
 
    snapshot->setChannelNames(_channelMan->channelNames());
 

	
 
    return snapshot;
 
}
 

	
 
void SnapshotManager::takeSnapshot()
 
{
0 comments (0 inline, 0 general)