Unity 8
 All Classes Functions
indicatorsmodel.cpp
1 /*
2  * Copyright 2012 Canonical Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authors:
17  * Renato Araujo Oliveira Filho <renato@canonical.com>
18  */
19 
20 #include "indicatorsmodel.h"
21 #include "indicatorsmanager.h"
22 #include "indicator.h"
23 #include "indicators.h"
24 
25 #include <paths.h>
26 
27 #include <QQmlContext>
28 #include <QQmlEngine>
29 #include <QDebug>
30 
53 IndicatorsModel::IndicatorsModel(QObject *parent)
54  : QAbstractListModel(parent)
55 {
56  m_manager = new IndicatorsManager(this);
57  QObject::connect(m_manager, SIGNAL(indicatorLoaded(const QString&)), this, SLOT(onIndicatorLoaded(const QString&)));
58  QObject::connect(m_manager, SIGNAL(indicatorAboutToBeUnloaded(const QString&)), this, SLOT(onIndicatorAboutToBeUnloaded(const QString&)));
59 
60  QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
61  QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
62  QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
63 }
64 
66 IndicatorsModel::~IndicatorsModel()
67 {
68  disconnect(m_manager, 0, 0, 0);
69  m_manager->deleteLater();
70 }
71 
78 int IndicatorsModel::count() const
79 {
80  return rowCount();
81 }
82 
88 void IndicatorsModel::load(const QString& profile)
89 {
90  m_indicators.clear();
91  m_manager->load(profile);
92 }
93 
99 void IndicatorsModel::unload()
100 {
101  m_manager->unload();
102 }
103 
105 void IndicatorsModel::onIndicatorLoaded(const QString& indicator_name)
106 {
107  Indicator::Ptr indicator = m_manager->indicator(indicator_name);
108  if (!indicator)
109  {
110  return;
111  }
112 
113  if (m_indicators.indexOf(indicator) >= 0)
114  {
115  return;
116  }
117 
118  // find the insert position
119  int pos = 0;
120  while (pos < count())
121  {
122  // keep going while the existing position is greater. (put lower position on end)
123  if (indicator->position() >= data(index(pos), IndicatorsModelRole::Position).toInt())
124  break;
125  pos++;
126  }
127 
128  QObject::connect(indicator.data(), SIGNAL(identifierChanged(const QString&)), this, SLOT(onIdentifierChanged()));
129  QObject::connect(indicator.data(), SIGNAL(indicatorPropertiesChanged(const QVariant&)), this, SLOT(onIndicatorPropertiesChanged()));
130 
131  beginInsertRows(QModelIndex(), pos, pos);
132 
133  m_indicators.insert(pos, indicator);
134  endInsertRows();
135 }
136 
138 void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator_name)
139 {
140  Indicator::Ptr indicator = m_manager->indicator(indicator_name);
141  if (!indicator)
142  {
143  return;
144  }
145 
146  int i = 0;
147  QMutableListIterator<Indicator::Ptr> iter(m_indicators);
148  while(iter.hasNext())
149  {
150  if (indicator == iter.next())
151  {
152  beginRemoveRows(QModelIndex(), i, i);
153  iter.remove();
154  endRemoveRows();
155  break;
156  }
157  i++;
158  }
159 
160 }
161 
163 void IndicatorsModel::onIdentifierChanged()
164 {
165  notifyDataChanged(QObject::sender(), IndicatorsModelRole::Identifier);
166 }
167 
169 void IndicatorsModel::onIndicatorPropertiesChanged()
170 {
171  notifyDataChanged(QObject::sender(), IndicatorsModelRole::IndicatorProperties);
172 }
173 
175 void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
176 {
177  Indicator* indicator = qobject_cast<Indicator*>(sender);
178  if (!indicator)
179  {
180  return;
181  }
182 
183  int index = 0;
184  QMutableListIterator<Indicator::Ptr> iter(m_indicators);
185  while(iter.hasNext())
186  {
187  if (indicator == iter.next())
188  {
189  QModelIndex changedIndex = this->index(index);
190  dataChanged(changedIndex, changedIndex, QVector<int>() << role);
191  break;
192  }
193  index++;
194  }
195 }
196 
198 QHash<int, QByteArray> IndicatorsModel::roleNames() const
199 {
200  static QHash<int, QByteArray> roles;
201  if (roles.isEmpty())
202  {
203  roles[IndicatorsModelRole::Identifier] = "identifier";
204  roles[IndicatorsModelRole::Position] = "position";
205  roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
206  }
207  return roles;
208 }
209 
211 int IndicatorsModel::columnCount(const QModelIndex &) const
212 {
213  return 1;
214 }
215 
216 Q_INVOKABLE QVariant IndicatorsModel::data(int row, int role) const
217 {
218  return data(index(row, 0), role);
219 }
220 
222 QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
223 {
224  if (!index.isValid() || index.row() >= m_indicators.size())
225  return QVariant();
226 
227  Indicator::Ptr indicator = m_indicators[index.row()];
228 
229  switch (role)
230  {
231  case IndicatorsModelRole::Identifier:
232  if (indicator)
233  {
234  return QVariant(indicator->identifier());
235  }
236  break;
237  case IndicatorsModelRole::Position:
238  if (indicator)
239  {
240  return QVariant(indicator->position());
241  }
242  break;
243  case IndicatorsModelRole::IndicatorProperties:
244  if (indicator)
245  {
246  return QVariant(indicator->indicatorProperties());
247  }
248  break;
249  default:
250  break;
251  }
252  return QVariant();
253 }
254 
256 QModelIndex IndicatorsModel::parent(const QModelIndex&) const
257 {
258  return QModelIndex();
259 }
260 
262 int IndicatorsModel::rowCount(const QModelIndex&) const
263 {
264  return m_indicators.count();
265 }