# HG changeset patch # User Hasan Yavuz Ă–ZDERYA # Date 2017-01-02 05:36:00 # Node ID c83e2c5aa6feb5d0b943a2b711f825ae29767fac # Parent 6254c449bb19074e7cbe36fa82671b8c27705bed update color selector on selection change, fix index checks diff --git a/src/channelinfomodel.cpp b/src/channelinfomodel.cpp --- a/src/channelinfomodel.cpp +++ b/src/channelinfomodel.cpp @@ -66,7 +66,7 @@ Qt::ItemFlags ChannelInfoModel::flags(co QVariant ChannelInfoModel::data(const QModelIndex &index, int role) const { // check index - if (index.row() >= (int) _numOfChannels) + if (index.row() >= (int) _numOfChannels || index.row() < 0) { return QVariant(); } @@ -127,7 +127,7 @@ QVariant ChannelInfoModel::headerData(in bool ChannelInfoModel::setData(const QModelIndex &index, const QVariant &value, int role) { // check index - if (index.row() >= (int) _numOfChannels) + if (index.row() >= (int) _numOfChannels || index.row() < 0) { return false; } @@ -245,18 +245,20 @@ void ChannelInfoModel::loadSettings(QSet chanInfo.color = settings->value(SG_Channels_Color, colors[ci % 8]).value(); chanInfo.visibility = settings->value(SG_Channels_Visible, true).toBool(); - if ((int) ci >= infos.size()) + if ((int) ci < infos.size()) { - infos.append(chanInfo); + infos[ci] = chanInfo; + + if (ci < _numOfChannels) + { + auto roles = QVector({ + Qt::DisplayRole, Qt::EditRole, Qt::ForegroundRole, Qt::CheckStateRole}); + emit dataChanged(index(ci, 0), index(ci, COLUMN_COUNT-1), roles); + } } else { - infos[ci] = chanInfo; - - auto roles = QVector({ - Qt::DisplayRole, Qt::EditRole, Qt::ForegroundRole, Qt::CheckStateRole}); - - emit dataChanged(index(ci, 0), index(ci, COLUMN_COUNT-1), roles); + infos.append(chanInfo); } } diff --git a/src/channelmanager.cpp b/src/channelmanager.cpp --- a/src/channelmanager.cpp +++ b/src/channelmanager.cpp @@ -140,7 +140,6 @@ void ChannelManager::onChannelInfoChange case Qt::EditRole: if (col == ChannelInfoModel::COLUMN_NAME) { - qDebug() << channelName(ci); emit channelNameChanged(ci, channelName(ci)); } break; diff --git a/src/plotcontrolpanel.cpp b/src/plotcontrolpanel.cpp --- a/src/plotcontrolpanel.cpp +++ b/src/plotcontrolpanel.cpp @@ -210,22 +210,37 @@ void PlotControlPanel::setChannelInfoMod 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()); - }); + 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); + }); + + connect(ui->colorSelector, &color_widgets::ColorSelector::colorChanged, + [this](QColor color) + { + auto index = ui->tvChannelInfo->selectionModel()->currentIndex(); + ui->tvChannelInfo->model()->setData(index, color, Qt::ForegroundRole); + }); - 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); - }); + connect(model, &QAbstractItemModel::dataChanged, + [this](const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector & roles = QVector ()) + { + auto current = ui->tvChannelInfo->selectionModel()->currentIndex(); + + // no current selection + if (!current.isValid()) return; + + auto mod = ui->tvChannelInfo->model(); + QColor color = mod->data(current, Qt::ForegroundRole).value(); + + // temporarily block signals because `setColor` emits `colorChanged` + bool wasBlocked = ui->colorSelector->blockSignals(true); + ui->colorSelector->setColor(color); + ui->colorSelector->blockSignals(wasBlocked); + }); } void PlotControlPanel::saveSettings(QSettings* settings)