Loading...
Searching...
No Matches
indicatorsmodel.cpp
/*
* Copyright 2012 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Renato Araujo Oliveira Filho <renato@canonical.com>
*/
#include "indicatorsmodel.h"
#include "indicatorsmanager.h"
#include "indicator.h"
#include "indicators.h"
#include <paths.h>
#include <QQmlContext>
#include <QQmlEngine>
#include <QDebug>
IndicatorsModel::IndicatorsModel(QObject *parent)
: QAbstractListModel(parent)
{
m_manager = new IndicatorsManager(this);
QObject::connect(m_manager, &IndicatorsManager::indicatorLoaded, this, &IndicatorsModel::onIndicatorLoaded);
QObject::connect(m_manager, &IndicatorsManager::indicatorAboutToBeUnloaded, this, &IndicatorsModel::onIndicatorAboutToBeUnloaded);
QObject::connect(m_manager, &IndicatorsManager::profileChanged, this, &IndicatorsModel::profileChanged);
QObject::connect(this, &IndicatorsModel::rowsInserted, this, &IndicatorsModel::countChanged);
QObject::connect(this, &IndicatorsModel::rowsRemoved, this, &IndicatorsModel::countChanged);
QObject::connect(this, &IndicatorsModel::modelReset, this, &IndicatorsModel::countChanged);
}
IndicatorsModel::~IndicatorsModel()
{
disconnect(m_manager, 0, 0, 0);
m_manager->deleteLater();
}
int IndicatorsModel::count() const
{
return rowCount();
}
QString IndicatorsModel::profile() const
{
return m_manager->profile();
}
void IndicatorsModel::setProfile(const QString &profile)
{
m_manager->setProfile(profile);
}
bool IndicatorsModel::light() const
{
return m_light;
}
void IndicatorsModel::setLight(const bool &light)
{
if (m_light != light) {
m_light = light;
Q_EMIT lightChanged();
}
}
void IndicatorsModel::load()
{
m_indicators.clear();
m_manager->load();
}
void IndicatorsModel::unload()
{
m_manager->unload();
}
void IndicatorsModel::onIndicatorLoaded(const QString& indicator_name)
{
Indicator::Ptr indicator = m_manager->indicator(indicator_name);
if (!indicator)
{
return;
}
if (m_indicators.indexOf(indicator) >= 0)
{
return;
}
// find the insert position
int pos = 0;
while (pos < count())
{
// keep going while the existing position is greater. (put lower position on end)
if (indicator->position() >= data(index(pos), IndicatorsModelRole::Position).toInt())
break;
pos++;
}
QObject::connect(indicator.data(), &Indicator::identifierChanged, this, &IndicatorsModel::onIdentifierChanged);
QObject::connect(indicator.data(), &Indicator::indicatorPropertiesChanged, this, &IndicatorsModel::onIndicatorPropertiesChanged);
beginInsertRows(QModelIndex(), pos, pos);
m_indicators.insert(pos, indicator);
endInsertRows();
}
void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator_name)
{
Indicator::Ptr indicator = m_manager->indicator(indicator_name);
if (!indicator)
{
return;
}
int i = 0;
QMutableListIterator<Indicator::Ptr> iter(m_indicators);
while(iter.hasNext())
{
if (indicator == iter.next())
{
beginRemoveRows(QModelIndex(), i, i);
iter.remove();
endRemoveRows();
break;
}
i++;
}
}
void IndicatorsModel::onIdentifierChanged()
{
notifyDataChanged(QObject::sender(), IndicatorsModelRole::Identifier);
}
void IndicatorsModel::onIndicatorPropertiesChanged()
{
notifyDataChanged(QObject::sender(), IndicatorsModelRole::IndicatorProperties);
}
void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
{
Indicator* indicator = qobject_cast<Indicator*>(sender);
if (!indicator)
{
return;
}
int index = 0;
QMutableListIterator<Indicator::Ptr> iter(m_indicators);
while(iter.hasNext())
{
if (indicator == iter.next())
{
QModelIndex changedIndex = this->index(index);
dataChanged(changedIndex, changedIndex, QVector<int>() << role);
break;
}
index++;
}
}
QHash<int, QByteArray> IndicatorsModel::roleNames() const
{
static QHash<int, QByteArray> roles;
if (roles.isEmpty())
{
roles[IndicatorsModelRole::Identifier] = "identifier";
roles[IndicatorsModelRole::Position] = "position";
roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
}
return roles;
}
int IndicatorsModel::columnCount(const QModelIndex &) const
{
return 1;
}
QVariant IndicatorsModel::data(int row, int role) const
{
return data(index(row, 0), role);
}
QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= m_indicators.size())
return QVariant();
Indicator::Ptr indicator = m_indicators.at(index.row());
switch (role)
{
case IndicatorsModelRole::Identifier:
if (indicator)
{
return indicator->identifier();
}
break;
case IndicatorsModelRole::Position:
if (indicator)
{
return indicator->position();
}
break;
case IndicatorsModelRole::IndicatorProperties:
if (indicator)
{
return indicator->indicatorProperties();
}
break;
default:
break;
}
return QVariant();
}
QModelIndex IndicatorsModel::parent(const QModelIndex&) const
{
return QModelIndex();
}
int IndicatorsModel::rowCount(const QModelIndex&) const
{
return m_indicators.count();
}