on ada7d2c help sign in
unity8/tests/mocks/LightDM/InfographicModel.cpp
416 lines 9.8 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU 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 General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Pete Woods <pete.woods@canonical.com>
17 */
19#include "InfographicModelPrivate.h"
20#include "InfographicModel.h"
22#include <QtCore/QDir>
23#include <QtCore/QString>
24#include <QtGui/QIcon>
25#include <QMultiMap>
27using namespace QLightDM;
29namespace QLightDM
32class InfographicColorThemePrivate: QObject
34public:
35 explicit InfographicColorThemePrivate(InfographicColorTheme *parent = 0);
37 InfographicColorThemePrivate(const QColor &start, const QColor &main,
38 const QColor &end, InfographicColorTheme *parent = 0);
40 ~InfographicColorThemePrivate();
42 InfographicColorTheme * const q_ptr;
44 QColor m_start;
46 QColor m_main;
48 QColor m_end;
50protected:
51 int calculateLength();
53private:
54 Q_DECLARE_PUBLIC(InfographicColorTheme)
55};
57class InfographicDataPrivate: QObject
59public:
60 explicit InfographicDataPrivate(InfographicData *parent);
62 InfographicDataPrivate(const QString &label,
63 const InfographicColorTheme &firstColor,
64 const QVariantList &firstMonth,
65 const InfographicColorTheme &secondColor,
66 const QVariantList &secondMonth, InfographicData *parent);
68 ~InfographicDataPrivate();
70 InfographicData * const q_ptr;
71 QString m_label;
72 InfographicColorTheme m_firstColor;
73 QVariantList m_firstMonth;
74 InfographicColorTheme m_secondColor;
75 QVariantList m_secondMonth;
76 int m_length;
78protected:
79 int calculateLength();
81private:
82 Q_DECLARE_PUBLIC(InfographicData)
83};
87InfographicColorThemePrivate::InfographicColorThemePrivate(
88 InfographicColorTheme *parent) :
89 q_ptr(parent)
93InfographicColorThemePrivate::InfographicColorThemePrivate(const QColor &start,
94 const QColor &main, const QColor &end, InfographicColorTheme *parent) :
95 q_ptr(parent), m_start(start), m_main(main), m_end(end)
99InfographicColorThemePrivate::~InfographicColorThemePrivate()
103InfographicColorTheme::InfographicColorTheme(QObject *parent) :
104 QObject(parent), d_ptr(new InfographicColorThemePrivate(this))
108InfographicColorTheme::InfographicColorTheme(QColor &first, QColor &main,
109 QColor &end, QObject *parent) :
110 QObject(parent), d_ptr(
111 new InfographicColorThemePrivate(first, main, end, this))
116InfographicColorTheme & InfographicColorTheme::operator=(
117 const InfographicColorTheme & other)
119 if (d_ptr->m_start != other.d_ptr->m_start)
120 {
121 d_ptr->m_start = other.d_ptr->m_start;
122 startChanged(d_ptr->m_start);
123 }
124 if (d_ptr->m_main != other.d_ptr->m_main)
125 {
126 d_ptr->m_main = other.d_ptr->m_main;
127 mainChanged(d_ptr->m_main);
128 }
130 if (d_ptr->m_end != other.d_ptr->m_end)
131 {
132 d_ptr->m_end = other.d_ptr->m_end;
133 endChanged(d_ptr->m_end);
134 }
136 return *this;
139InfographicColorTheme::~InfographicColorTheme()
141 delete d_ptr;
144QColor InfographicColorTheme::start() const
146 return d_ptr->m_start;
149QColor InfographicColorTheme::main() const
151 return d_ptr->m_main;
154QColor InfographicColorTheme::end() const
156 return d_ptr->m_end;
159InfographicDataPrivate::InfographicDataPrivate(InfographicData *parent) :
160 q_ptr(parent), m_firstColor(this), m_secondColor(this)
162 m_length = calculateLength();
165InfographicDataPrivate::InfographicDataPrivate(const QString &label,
166 const InfographicColorTheme &firstColor, const QVariantList &firstMonth,
167 const InfographicColorTheme &secondColor,
168 const QVariantList &secondMonth, InfographicData *parent) :
169 q_ptr(parent), m_label(label), m_firstColor(this), m_firstMonth(
170 firstMonth), m_secondColor(this), m_secondMonth(secondMonth)
172 m_length = calculateLength();
173 m_firstColor = firstColor;
174 m_secondColor = secondColor;
177InfographicDataPrivate::~InfographicDataPrivate()
181int InfographicDataPrivate::calculateLength()
183 int day(m_firstMonth.size());
184 auto it = m_firstMonth.end(), end = m_firstMonth.begin();
185 while (it != end)
186 {
187 --it;
188 --day;
189 if (!it->isNull())
190 {
191 return day;
192 }
193 }
195 return -1;
198InfographicData::InfographicData(QObject *parent) :
199 QObject(parent), d_ptr(new InfographicDataPrivate(this))
203InfographicData::InfographicData(const QString &label,
204 const InfographicColorTheme &firstColor, const QVariantList &firstMonth,
205 const InfographicColorTheme &secondColor,
206 const QVariantList &secondMonth, QObject* parent) :
207 QObject(parent), d_ptr(
208 new InfographicDataPrivate(label, firstColor, firstMonth,
209 secondColor, secondMonth, this))
213InfographicData::~InfographicData()
215 delete d_ptr;
218const QString & InfographicData::label() const
220 return d_ptr->m_label;
223const InfographicColorTheme & InfographicData::firstColor() const
225 return d_ptr->m_firstColor;
228const QVariantList & InfographicData::firstMonth() const
230 return d_ptr->m_firstMonth;
233const InfographicColorTheme & InfographicData::secondColor() const
235 return d_ptr->m_secondColor;
238const QVariantList & InfographicData::secondMonth() const
240 return d_ptr->m_secondMonth;
243int InfographicData::length() const
245 return d_ptr->m_length;
248InfographicModelPrivate::InfographicModelPrivate(InfographicModel *parent) :
249 q_ptr(parent), m_currentDay(0)
251 m_fakeData.insert("", InfographicDataPtr(new InfographicData(this)));
254InfographicModelPrivate::~InfographicModelPrivate()
258void InfographicModelPrivate::setUsername(const QString &username)
260 if(m_username == username) {
261 return;
262 }
264 m_username = username;
266 m_dataIndex = m_fakeData.constFind(m_username);
267 if (m_dataIndex == m_fakeData.end())
268 {
269 m_dataIndex = m_fakeData.constFind("");
270 }
272 loadFakeData();
274 q_ptr->usernameChanged(m_username);
277void InfographicModelPrivate::loadFakeData()
279 m_newData = *m_dataIndex;
281 bool oldLabelEmpty = m_label.isEmpty();
282 bool newLabelEmpty = m_newData->label().isEmpty();
284 if (oldLabelEmpty && !newLabelEmpty)
285 {
286 q_ptr->dataAboutToAppear();
287 finishSetFakeData();
288 } else if (!oldLabelEmpty && newLabelEmpty)
289 {
290 q_ptr->dataAboutToDisappear();
291 } else if (!oldLabelEmpty && !newLabelEmpty)
292 {
293 q_ptr->dataAboutToChange();
294 }
295 // we emit no signal if the data has stayed empty
298void InfographicModelPrivate::finishSetFakeData()
300 bool oldLabelEmpty = m_label.isEmpty();
301 bool newLabelEmpty = m_newData->label().isEmpty();
303 m_label = m_newData->label();
304 m_firstColor = m_newData->firstColor();
305 m_firstMonth.setVariantList(m_newData->firstMonth());
306 m_secondColor = m_newData->secondColor();
307 m_secondMonth.setVariantList(m_newData->secondMonth());
309 bool currentDayChanged = m_currentDay != m_newData->length();
310 m_currentDay = m_newData->length();
312 q_ptr->labelChanged(m_label);
313 if (currentDayChanged)
314 {
315 q_ptr->currentDayChanged(m_currentDay);
316 }
318 if (oldLabelEmpty && !newLabelEmpty)
319 {
320 q_ptr->dataAppeared();
321 } else if (!oldLabelEmpty && newLabelEmpty)
322 {
323 q_ptr->dataDisappeared();
324 } else if (!oldLabelEmpty && !newLabelEmpty)
325 {
326 q_ptr->dataChanged();
327 }
328 // we emit no signal if the data has stayed empty
331void InfographicModelPrivate::nextFakeData()
333 ++m_dataIndex;
334 if (m_dataIndex == m_fakeData.end() || m_dataIndex.key() != m_username)
335 {
336 m_dataIndex = m_fakeData.constFind(m_username);
337 }
339 loadFakeData();
342InfographicModel::InfographicModel(QObject *parent) :
343 QObject(parent), d_ptr(new InfographicModelPrivate(this))
345 d_ptr->generateFakeData();
346 setUsername("");
348 connect(this, SIGNAL(nextDataSource()), this, SLOT(nextDataSourceSlot()),
349 Qt::QueuedConnection);
350 connect(this, SIGNAL(readyForDataChange()), this,
351 SLOT(readyForDataChangeSlot()), Qt::QueuedConnection);
354InfographicModel::~InfographicModel()
356 delete d_ptr;
359QString InfographicModel::label() const
361 return d_ptr->m_label;
364QString InfographicModel::username() const
366 return d_ptr->m_username;
369void InfographicModel::setUsername(const QString &username)
371 d_ptr->setUsername(username);
374InfographicColorTheme * InfographicModel::firstColor() const
376 return &d_ptr->m_firstColor;
379InfographicColorTheme * InfographicModel::secondColor() const
381 return &d_ptr->m_secondColor;
384QAbstractItemModel * InfographicModel::firstMonth() const
386 return &d_ptr->m_firstMonth;
389QAbstractItemModel * InfographicModel::secondMonth() const
391 return &d_ptr->m_secondMonth;
394int InfographicModel::currentDay() const
396 return d_ptr->m_currentDay;
399void InfographicModel::nextDataSourceSlot()
401 d_ptr->nextFakeData();
404void InfographicModel::readyForDataChangeSlot()
406 d_ptr->finishSetFakeData();
409/**
410 * Factory methods
411 */
413InfographicModel * InfographicModel::getInstance()
415 return new InfographicModel();