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 107 insertions and 105 deletions:
0 comments (0 inline, 0 general)
src/channelinfomodel.cpp
Show inline comments
 
@@ -19,13 +19,6 @@
 

	
 
#include "channelinfomodel.h"
 

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

	
 
const QColor colors[8] =
 
{
 
    QColor(237,97,68),
 
@@ -152,6 +145,7 @@ bool ChannelInfoModel::setData(const QMo
 
        if (role == Qt::DisplayRole || role == Qt::EditRole)
 
        {
 
            infos[index.row()].name = value.toString();
 
            emit dataChanged(index, index, QVector<int>({role}));
 
            return true;
 
        }
 
    } // set visibility
 
@@ -161,6 +155,7 @@ bool ChannelInfoModel::setData(const QMo
 
        {
 
            bool checked = value.toInt() == Qt::Checked;
 
            infos[index.row()].visibility = checked;
 
            emit dataChanged(index, index, QVector<int>({role}));
 
            return true;
 
        }
 
    }
src/channelinfomodel.h
Show inline comments
 
@@ -28,6 +28,13 @@ class ChannelInfoModel : public QAbstrac
 
    Q_OBJECT
 

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

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

	
 
    // implemented from QAbstractItemModel
src/channelmanager.cpp
Show inline comments
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 
  Copyright © 2017 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
@@ -17,30 +17,27 @@
 
  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()
 
@@ -71,7 +68,6 @@ void ChannelManager::setNumOfChannels(un
 
        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)
 
@@ -80,10 +76,11 @@ void ChannelManager::setNumOfChannels(un
 
        for (unsigned int i = oldNum-1; i > number-1; i--)
 
        {
 
            delete channelBuffers.takeLast();
 
            _channelNames.removeRow(i);
 
        }
 
    }
 

	
 
    _infoModel.setNumOfChannels(number);
 

	
 
    emit numOfChannelsChanged(number);
 
}
 

	
 
@@ -104,39 +101,64 @@ FrameBuffer* ChannelManager::channelBuff
 
    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,
 
                                             const QModelIndex & bottomRight,
 
                                             const QVector<int> & roles)
 
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++)
 
    {
 
        emit channelNameChanged(i, channelName(i));
 
        for (auto role : roles)
 
        {
 
            switch (role)
 
            {
 
                case Qt::EditRole:
 
                    if (col == ChannelInfoModel::COLUMN_NAME)
 
                    {
 
                        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));
 
    }
 
}
 

	
 
@@ -165,7 +187,8 @@ void ChannelManager::loadSettings(QSetti
 
    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.
 

	
 
@@ -21,12 +21,13 @@
 
#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
 
{
 
@@ -38,12 +39,16 @@ public:
 
    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);
 
@@ -53,21 +58,21 @@ signals:
 
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,
 
                                 const QModelIndex & bottomRight,
 
                                 const QVector<int> & roles = QVector<int> ());
 
    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.
 

	
 
@@ -24,16 +24,6 @@
 
#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,
 
@@ -60,43 +50,5 @@ int main(int argc, char *argv[])
 

	
 
    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.
 

	
 
@@ -172,7 +172,7 @@ MainWindow::MainWindow(QWidget *parent) 
 
    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++)
src/plotcontrolpanel.cpp
Show inline comments
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 
  Copyright © 2017 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
@@ -23,6 +23,7 @@
 

	
 
#include <math.h>
 

	
 
#include "color_selector.hpp"
 
#include "plotcontrolpanel.h"
 
#include "ui_plotcontrolpanel.h"
 
#include "setting_defines.h"
 
@@ -204,9 +205,27 @@ void PlotControlPanel::onRangeSelected()
 
    ui->cbAutoScale->setChecked(false);
 
}
 

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

	
 
    // channel color selector
 
    QObject::connect(ui->tvChannelInfo->selectionModel(), &QItemSelectionModel::currentRowChanged,
 
                     [this](const QModelIndex &current, const QModelIndex &previous)
 
                     {
 
                         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)
src/plotcontrolpanel.h
Show inline comments
 
/*
 
  Copyright © 2016 Hasan Yavuz Özderya
 
  Copyright © 2017 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
@@ -21,9 +21,10 @@
 
#define PLOTCONTROLPANEL_H
 

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

	
 
#include "channelinfomodel.h"
 

	
 
namespace Ui {
 
class PlotControlPanel;
 
}
 
@@ -41,7 +42,7 @@ public:
 
    double yMax();
 
    double yMin();
 

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

	
 
    /// Stores plot settings into a `QSettings`
 
    void saveSettings(QSettings* settings);
src/plotcontrolpanel.ui
Show inline comments
 
@@ -38,7 +38,7 @@
 
      </layout>
 
     </item>
 
     <item>
 
      <widget class="QListView" name="lvChannelNames">
 
      <widget class="QTableView" name="tvChannelInfo">
 
       <property name="maximumSize">
 
        <size>
 
         <width>16777215</width>
src/snapshotmanager.cpp
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 
  Copyright © 2017 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
@@ -76,7 +76,7 @@ Snapshot* SnapshotManager::makeSnapshot(
 
            snapshot->data[ci][i] = QPointF(i, _channelMan->channelBuffer(ci)->sample(i));
 
        }
 
    }
 
    snapshot->setChannelNames(_channelMan->channelNames()->stringList());
 
    snapshot->setChannelNames(_channelMan->channelNames());
 

	
 
    return snapshot;
 
}
0 comments (0 inline, 0 general)