diff --git a/src/channelinfomodel.cpp b/src/channelinfomodel.cpp --- a/src/channelinfomodel.cpp +++ b/src/channelinfomodel.cpp @@ -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({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({role})); return true; } } diff --git a/src/channelinfomodel.h b/src/channelinfomodel.h --- a/src/channelinfomodel.h +++ b/src/channelinfomodel.h @@ -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 diff --git a/src/channelmanager.cpp b/src/channelmanager.cpp --- a/src/channelmanager.cpp +++ b/src/channelmanager.cpp @@ -1,5 +1,5 @@ /* - 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 . */ -#include #include +#include + #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 & roles) +void ChannelManager::onChannelInfoChanged(const QModelIndex & topLeft, + const QModelIndex & bottomRight, + const QVector & 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(); diff --git a/src/channelmanager.h b/src/channelmanager.h --- a/src/channelmanager.h +++ b/src/channelmanager.h @@ -1,5 +1,5 @@ /* - Copyright © 2016 Hasan Yavuz Özderya + Copyright © 2017 Hasan Yavuz Özderya This file is part of serialplot. @@ -21,12 +21,13 @@ #define CHANNELMANAGER_H #include -#include +#include #include #include #include #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 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 & roles = QVector ()); + void onChannelInfoChanged(const QModelIndex & topLeft, + const QModelIndex & bottomRight, + const QVector & roles = QVector ()); }; #endif // CHANNELMANAGER_H diff --git a/src/main.cpp b/src/main.cpp --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ /* - 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 -#include -#include -#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::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 ¤t, const QModelIndex &previous) - { - cpicker.setColor(cim.data(current, Qt::ForegroundRole).value()); - }); - - 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(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1,5 +1,5 @@ /* - 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++) diff --git a/src/plotcontrolpanel.cpp b/src/plotcontrolpanel.cpp --- a/src/plotcontrolpanel.cpp +++ b/src/plotcontrolpanel.cpp @@ -1,5 +1,5 @@ /* - Copyright © 2016 Hasan Yavuz Özderya + Copyright © 2017 Hasan Yavuz Özderya This file is part of serialplot. @@ -23,6 +23,7 @@ #include +#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 ¤t, const QModelIndex &previous) + { + auto model = ui->tvChannelInfo->model(); + QColor color = model->data(current, Qt::ForegroundRole).value(); + ui->colorSelector->setColor(color); + // cpicker.setColor(cim.data(current, Qt::ForegroundRole).value()); + }); + + 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) diff --git a/src/plotcontrolpanel.h b/src/plotcontrolpanel.h --- a/src/plotcontrolpanel.h +++ b/src/plotcontrolpanel.h @@ -1,5 +1,5 @@ /* - Copyright © 2016 Hasan Yavuz Özderya + Copyright © 2017 Hasan Yavuz Özderya This file is part of serialplot. @@ -21,9 +21,10 @@ #define PLOTCONTROLPANEL_H #include -#include #include +#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); diff --git a/src/plotcontrolpanel.ui b/src/plotcontrolpanel.ui --- a/src/plotcontrolpanel.ui +++ b/src/plotcontrolpanel.ui @@ -38,7 +38,7 @@ - + 16777215 diff --git a/src/snapshotmanager.cpp b/src/snapshotmanager.cpp --- a/src/snapshotmanager.cpp +++ b/src/snapshotmanager.cpp @@ -1,5 +1,5 @@ /* - 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; }