Unity 8
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, &IndicatorsManager::indicatorLoaded, this, &IndicatorsModel::onIndicatorLoaded);
58  QObject::connect(m_manager, &IndicatorsManager::indicatorAboutToBeUnloaded, this, &IndicatorsModel::onIndicatorAboutToBeUnloaded);
59  QObject::connect(m_manager, &IndicatorsManager::profileChanged, this, &IndicatorsModel::profileChanged);
60 
61  QObject::connect(this, &IndicatorsModel::rowsInserted, this, &IndicatorsModel::countChanged);
62  QObject::connect(this, &IndicatorsModel::rowsRemoved, this, &IndicatorsModel::countChanged);
63  QObject::connect(this, &IndicatorsModel::modelReset, this, &IndicatorsModel::countChanged);
64 }
65 
67 IndicatorsModel::~IndicatorsModel()
68 {
69  disconnect(m_manager, 0, 0, 0);
70  m_manager->deleteLater();
71 }
72 
79 int IndicatorsModel::count() const
80 {
81  return rowCount();
82 }
83 
90 QString IndicatorsModel::profile() const
91 {
92  return m_manager->profile();
93 }
94 
101 void IndicatorsModel::setProfile(const QString &profile)
102 {
103  m_manager->setProfile(profile);
104 }
105 
111 void IndicatorsModel::load()
112 {
113  m_indicators.clear();
114  m_manager->load();
115 }
116 
122 void IndicatorsModel::unload()
123 {
124  m_manager->unload();
125 }
126 
128 void IndicatorsModel::onIndicatorLoaded(const QString& indicator_name)
129 {
130  Indicator::Ptr indicator = m_manager->indicator(indicator_name);
131  if (!indicator)
132  {
133  return;
134  }
135 
136  if (m_indicators.indexOf(indicator) >= 0)
137  {
138  return;
139  }
140 
141  // find the insert position
142  int pos = 0;
143  while (pos < count())
144  {
145  // keep going while the existing position is greater. (put lower position on end)
146  if (indicator->position() >= data(index(pos), IndicatorsModelRole::Position).toInt())
147  break;
148  pos++;
149  }
150 
151  QObject::connect(indicator.data(), &Indicator::identifierChanged, this, &IndicatorsModel::onIdentifierChanged);
152  QObject::connect(indicator.data(), &Indicator::indicatorPropertiesChanged, this, &IndicatorsModel::onIndicatorPropertiesChanged);
153 
154  beginInsertRows(QModelIndex(), pos, pos);
155 
156  m_indicators.insert(pos, indicator);
157  endInsertRows();
158 }
159 
161 void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator_name)
162 {
163  Indicator::Ptr indicator = m_manager->indicator(indicator_name);
164  if (!indicator)
165  {
166  return;
167  }
168 
169  int i = 0;
170  QMutableListIterator<Indicator::Ptr> iter(m_indicators);
171  while(iter.hasNext())
172  {
173  if (indicator == iter.next())
174  {
175  beginRemoveRows(QModelIndex(), i, i);
176  iter.remove();
177  endRemoveRows();
178  break;
179  }
180  i++;
181  }
182 
183 }
184 
186 void IndicatorsModel::onIdentifierChanged()
187 {
188  notifyDataChanged(QObject::sender(), IndicatorsModelRole::Identifier);
189 }
190 
192 void IndicatorsModel::onIndicatorPropertiesChanged()
193 {
194  notifyDataChanged(QObject::sender(), IndicatorsModelRole::IndicatorProperties);
195 }
196 
198 void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
199 {
200  Indicator* indicator = qobject_cast<Indicator*>(sender);
201  if (!indicator)
202  {
203  return;
204  }
205 
206  int index = 0;
207  QMutableListIterator<Indicator::Ptr> iter(m_indicators);
208  while(iter.hasNext())
209  {
210  if (indicator == iter.next())
211  {
212  QModelIndex changedIndex = this->index(index);
213  dataChanged(changedIndex, changedIndex, QVector<int>() << role);
214  break;
215  }
216  index++;
217  }
218 }
219 
221 QHash<int, QByteArray> IndicatorsModel::roleNames() const
222 {
223  static QHash<int, QByteArray> roles;
224  if (roles.isEmpty())
225  {
226  roles[IndicatorsModelRole::Identifier] = "identifier";
227  roles[IndicatorsModelRole::Position] = "position";
228  roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
229  }
230  return roles;
231 }
232 
234 int IndicatorsModel::columnCount(const QModelIndex &) const
235 {
236  return 1;
237 }
238 
239 Q_INVOKABLE QVariant IndicatorsModel::data(int row, int role) const
240 {
241  return data(index(row, 0), role);
242 }
243 
245 QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
246 {
247  if (!index.isValid() || index.row() >= m_indicators.size())
248  return QVariant();
249 
250  Indicator::Ptr indicator = m_indicators[index.row()];
251 
252  switch (role)
253  {
254  case IndicatorsModelRole::Identifier:
255  if (indicator)
256  {
257  return QVariant(indicator->identifier());
258  }
259  break;
260  case IndicatorsModelRole::Position:
261  if (indicator)
262  {
263  return QVariant(indicator->position());
264  }
265  break;
266  case IndicatorsModelRole::IndicatorProperties:
267  if (indicator)
268  {
269  return QVariant(indicator->indicatorProperties());
270  }
271  break;
272  default:
273  break;
274  }
275  return QVariant();
276 }
277 
279 QModelIndex IndicatorsModel::parent(const QModelIndex&) const
280 {
281  return QModelIndex();
282 }
283 
285 int IndicatorsModel::rowCount(const QModelIndex&) const
286 {
287  return m_indicators.count();
288 }