Files @ 3b329010cce9
Branch filter:

Location: tempo-plotter/src/channelmanager.cpp - annotation

Hasan Yavuz ÖZDERYA
Merge with settings
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
797666010f26
797666010f26
797666010f26
797666010f26
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
3ec20782037b
1a05d0ad46b0
797666010f26
3ec20782037b
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
797666010f26
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
1a05d0ad46b0
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
c331aa68f3c2
/*
  Copyright © 2016 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
  (at your option) any later version.

  serialplot is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  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 "channelmanager.h"
#include "setting_defines.h"

ChannelManager::ChannelManager(unsigned numberOfChannels, unsigned numberOfSamples, QObject *parent) :
    QObject(parent)
{
    _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);
}

ChannelManager::~ChannelManager()
{
    for (auto buffer : channelBuffers)
    {
        delete buffer;
    }
}

unsigned ChannelManager::numOfChannels()
{
    return channelBuffers.size();
}

unsigned ChannelManager::numOfSamples()
{
    return _numOfSamples;
}

void ChannelManager::setNumOfChannels(unsigned number)
{
    unsigned int oldNum = channelBuffers.size();

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

    emit numOfChannelsChanged(number);
}

void ChannelManager::setNumOfSamples(unsigned number)
{
    _numOfSamples = number;

    for (int ci = 0; ci < channelBuffers.size(); ci++)
    {
        channelBuffers[ci]->resize(_numOfSamples);
    }

    emit numOfSamplesChanged(number);
}

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

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

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

void ChannelManager::addChannelName(QString name)
{
    _channelNames.insertRow(_channelNames.rowCount());
    setChannelName(_channelNames.rowCount()-1, name);
}

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

    // TODO: maybe check `roles` parameter, can't think of a reason for current use case
    for (int i = start; i <= end; i++)
    {
        emit channelNameChanged(i, channelName(i));
    }
}

void ChannelManager::addChannelData(unsigned channel, double* data, unsigned size)
{
    channelBuffer(channel)->addSamples(data, size);
}

void ChannelManager::saveSettings(QSettings* settings)
{
    settings->beginGroup(SettingGroup_Channels);
    settings->beginWriteArray(SG_Channels_Channel);
    for (unsigned i = 0; i < numOfChannels(); i++)
    {
        settings->setArrayIndex(i);
        settings->setValue(SG_Channels_Name, channelName(i));
    }
    settings->endArray();
    settings->endGroup();
}

void ChannelManager::loadSettings(QSettings* settings)
{
    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());
    }
    settings->endArray();
    settings->endGroup();
}