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 
106 bool IndicatorsModel::light() const
107 {
108  return m_light;
109 }
110 
111 void IndicatorsModel::setLight(const bool &light)
112 {
113  if (m_light != light) {
114  m_light = light;
115  Q_EMIT lightChanged();
116  }
117 }
118 
124 void IndicatorsModel::load()
125 {
126  m_indicators.clear();
127  m_manager->load();
128 }
129 
135 void IndicatorsModel::unload()
136 {
137  m_manager->unload();
138 }
139 
141 void IndicatorsModel::onIndicatorLoaded(const QString& indicator_name)
142 {
143  Indicator::Ptr indicator = m_manager->indicator(indicator_name);
144  if (!indicator)
145  {
146  return;
147  }
148 
149  if (m_indicators.indexOf(indicator) >= 0)
150  {
151  return;
152  }
153 
154  // find the insert position
155  int pos = 0;
156  while (pos < count())
157  {
158  // keep going while the existing position is greater. (put lower position on end)
159  if (indicator->position() >= data(index(pos), IndicatorsModelRole::Position).toInt())
160  break;
161  pos++;
162  }
163 
164  QObject::connect(indicator.data(), &Indicator::identifierChanged, this, &IndicatorsModel::onIdentifierChanged);
165  QObject::connect(indicator.data(), &Indicator::indicatorPropertiesChanged, this, &IndicatorsModel::onIndicatorPropertiesChanged);
166 
167  beginInsertRows(QModelIndex(), pos, pos);
168 
169  m_indicators.insert(pos, indicator);
170  endInsertRows();
171 }
172 
174 void IndicatorsModel::onIndicatorAboutToBeUnloaded(const QString& indicator_name)
175 {
176  Indicator::Ptr indicator = m_manager->indicator(indicator_name);
177  if (!indicator)
178  {
179  return;
180  }
181 
182  int i = 0;
183  QMutableListIterator<Indicator::Ptr> iter(m_indicators);
184  while(iter.hasNext())
185  {
186  if (indicator == iter.next())
187  {
188  beginRemoveRows(QModelIndex(), i, i);
189  iter.remove();
190  endRemoveRows();
191  break;
192  }
193  i++;
194  }
195 
196 }
197 
199 void IndicatorsModel::onIdentifierChanged()
200 {
201  notifyDataChanged(QObject::sender(), IndicatorsModelRole::Identifier);
202 }
203 
205 void IndicatorsModel::onIndicatorPropertiesChanged()
206 {
207  notifyDataChanged(QObject::sender(), IndicatorsModelRole::IndicatorProperties);
208 }
209 
211 void IndicatorsModel::notifyDataChanged(QObject *sender, int role)
212 {
213  Indicator* indicator = qobject_cast<Indicator*>(sender);
214  if (!indicator)
215  {
216  return;
217  }
218 
219  int index = 0;
220  QMutableListIterator<Indicator::Ptr> iter(m_indicators);
221  while(iter.hasNext())
222  {
223  if (indicator == iter.next())
224  {
225  QModelIndex changedIndex = this->index(index);
226  dataChanged(changedIndex, changedIndex, QVector<int>() << role);
227  break;
228  }
229  index++;
230  }
231 }
232 
234 QHash<int, QByteArray> IndicatorsModel::roleNames() const
235 {
236  static QHash<int, QByteArray> roles;
237  if (roles.isEmpty())
238  {
239  roles[IndicatorsModelRole::Identifier] = "identifier";
240  roles[IndicatorsModelRole::Position] = "position";
241  roles[IndicatorsModelRole::IndicatorProperties] = "indicatorProperties";
242  }
243  return roles;
244 }
245 
247 int IndicatorsModel::columnCount(const QModelIndex &) const
248 {
249  return 1;
250 }
251 
252 QVariant IndicatorsModel::data(int row, int role) const
253 {
254  return data(index(row, 0), role);
255 }
256 
258 QVariant IndicatorsModel::data(const QModelIndex &index, int role) const
259 {
260  if (!index.isValid() || index.row() >= m_indicators.size())
261  return QVariant();
262 
263  Indicator::Ptr indicator = m_indicators.at(index.row());
264 
265  switch (role)
266  {
267  case IndicatorsModelRole::Identifier:
268  if (indicator)
269  {
270  return indicator->identifier();
271  }
272  break;
273  case IndicatorsModelRole::Position:
274  if (indicator)
275  {
276  return indicator->position();
277  }
278  break;
279  case IndicatorsModelRole::IndicatorProperties:
280  if (indicator)
281  {
282  return indicator->indicatorProperties();
283  }
284  break;
285  default:
286  break;
287  }
288  return QVariant();
289 }
290 
292 QModelIndex IndicatorsModel::parent(const QModelIndex&) const
293 {
294  return QModelIndex();
295 }
296 
298 int IndicatorsModel::rowCount(const QModelIndex&) const
299 {
300  return m_indicators.count();
301 }