Changeset - c83e2c5aa6fe
[Not reviewed]
Hasan Yavuz Ă–ZDERYA - 9 years ago 2017-01-02 05:36:00
hy@ozderya.net
update color selector on selection change, fix index checks
3 files changed with 42 insertions and 26 deletions:
0 comments (0 inline, 0 general)
src/channelinfomodel.cpp
Show inline comments
 
@@ -63,13 +63,13 @@ Qt::ItemFlags ChannelInfoModel::flags(co
 
    return Qt::NoItemFlags;
 
}
 

	
 
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();
 
    }
 

	
 
    // get color
 
    if (role == Qt::ForegroundRole)
 
@@ -124,13 +124,13 @@ QVariant ChannelInfoModel::headerData(in
 
    return QVariant();
 
}
 

	
 
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;
 
    }
 

	
 
    // set color
 
    if (role == Qt::ForegroundRole)
 
@@ -242,24 +242,26 @@ void ChannelInfoModel::loadSettings(QSet
 
        ChannelInfo chanInfo;
 
        chanInfo.name       = settings->value(SG_Channels_Name,
 
                                              QString(tr("Channel %1")).arg(ci+1)).toString();
 
        chanInfo.color      = settings->value(SG_Channels_Color, colors[ci % 8]).value<QColor>();
 
        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<int>({
 
                    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<int>({
 
                    Qt::DisplayRole, Qt::EditRole, Qt::ForegroundRole, Qt::CheckStateRole});
 

	
 
            emit dataChanged(index(ci, 0), index(ci, COLUMN_COUNT-1), roles);
 
            infos.append(chanInfo);
 
        }
 
    }
 

	
 
    settings->endArray();
 
    settings->endGroup();
 
}
src/channelmanager.cpp
Show inline comments
 
@@ -137,13 +137,12 @@ void ChannelManager::onChannelInfoChange
 
        {
 
            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)
 
                    {
src/plotcontrolpanel.cpp
Show inline comments
 
@@ -207,28 +207,43 @@ void PlotControlPanel::onRangeSelected()
 

	
 
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)
 
                     {
 
                         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>());
 
                     });
 
    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);
 
            });
 

	
 
    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<int> & roles = QVector<int> ())
 
            {
 
                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<QColor>();
 

	
 
                // 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)
 
{
 
    settings->beginGroup(SettingGroup_Plot);
 
    settings->setValue(SG_Plot_NumOfSamples, numOfSamples());
0 comments (0 inline, 0 general)