2
* Copyright (C) 2013 Canonical, Ltd.
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.
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.
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/>.
16
* Author: Pete Woods <pete.woods@canonical.com>
19
#include "InfographicModelPrivate.h"
20
#include "InfographicModel.h"
22
#include <QtCore/QDir>
23
#include <QtCore/QString>
24
#include <QtGui/QIcon>
27
using namespace QLightDM;
32
class InfographicColorThemePrivate: QObject
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;
51
int calculateLength();
54
Q_DECLARE_PUBLIC(InfographicColorTheme)
57
class InfographicDataPrivate: QObject
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;
72
InfographicColorTheme m_firstColor;
73
QVariantList m_firstMonth;
74
InfographicColorTheme m_secondColor;
75
QVariantList m_secondMonth;
79
int calculateLength();
82
Q_DECLARE_PUBLIC(InfographicData)
87
InfographicColorThemePrivate::InfographicColorThemePrivate(
88
InfographicColorTheme *parent) :
93
InfographicColorThemePrivate::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)
99
InfographicColorThemePrivate::~InfographicColorThemePrivate()
103
InfographicColorTheme::InfographicColorTheme(QObject *parent) :
104
QObject(parent), d_ptr(new InfographicColorThemePrivate(this))
108
InfographicColorTheme::InfographicColorTheme(QColor &first, QColor &main,
109
QColor &end, QObject *parent) :
110
QObject(parent), d_ptr(
111
new InfographicColorThemePrivate(first, main, end, this))
116
InfographicColorTheme & InfographicColorTheme::operator=(
117
const InfographicColorTheme & other)
119
if (d_ptr->m_start != other.d_ptr->m_start)
121
d_ptr->m_start = other.d_ptr->m_start;
122
startChanged(d_ptr->m_start);
124
if (d_ptr->m_main != other.d_ptr->m_main)
126
d_ptr->m_main = other.d_ptr->m_main;
127
mainChanged(d_ptr->m_main);
130
if (d_ptr->m_end != other.d_ptr->m_end)
132
d_ptr->m_end = other.d_ptr->m_end;
133
endChanged(d_ptr->m_end);
139
InfographicColorTheme::~InfographicColorTheme()
144
QColor InfographicColorTheme::start() const
146
return d_ptr->m_start;
149
QColor InfographicColorTheme::main() const
151
return d_ptr->m_main;
154
QColor InfographicColorTheme::end() const
159
InfographicDataPrivate::InfographicDataPrivate(InfographicData *parent) :
160
q_ptr(parent), m_firstColor(this), m_secondColor(this)
162
m_length = calculateLength();
165
InfographicDataPrivate::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;
177
InfographicDataPrivate::~InfographicDataPrivate()
181
int InfographicDataPrivate::calculateLength()
183
int day(m_firstMonth.size());
184
auto it = m_firstMonth.end(), end = m_firstMonth.begin();
198
InfographicData::InfographicData(QObject *parent) :
199
QObject(parent), d_ptr(new InfographicDataPrivate(this))
203
InfographicData::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))
213
InfographicData::~InfographicData()
218
const QString & InfographicData::label() const
220
return d_ptr->m_label;
223
const InfographicColorTheme & InfographicData::firstColor() const
225
return d_ptr->m_firstColor;
228
const QVariantList & InfographicData::firstMonth() const
230
return d_ptr->m_firstMonth;
233
const InfographicColorTheme & InfographicData::secondColor() const
235
return d_ptr->m_secondColor;
238
const QVariantList & InfographicData::secondMonth() const
240
return d_ptr->m_secondMonth;
243
int InfographicData::length() const
245
return d_ptr->m_length;
248
InfographicModelPrivate::InfographicModelPrivate(InfographicModel *parent) :
249
q_ptr(parent), m_currentDay(0)
251
m_fakeData.insert("", InfographicDataPtr(new InfographicData(this)));
254
InfographicModelPrivate::~InfographicModelPrivate()
258
void InfographicModelPrivate::setUsername(const QString &username)
260
if(m_username == username) {
264
m_username = username;
266
m_dataIndex = m_fakeData.constFind(m_username);
267
if (m_dataIndex == m_fakeData.end())
269
m_dataIndex = m_fakeData.constFind("");
274
q_ptr->usernameChanged(m_username);
277
void InfographicModelPrivate::loadFakeData()
279
m_newData = *m_dataIndex;
281
bool oldLabelEmpty = m_label.isEmpty();
282
bool newLabelEmpty = m_newData->label().isEmpty();
284
if (oldLabelEmpty && !newLabelEmpty)
286
q_ptr->dataAboutToAppear();
288
} else if (!oldLabelEmpty && newLabelEmpty)
290
q_ptr->dataAboutToDisappear();
291
} else if (!oldLabelEmpty && !newLabelEmpty)
293
q_ptr->dataAboutToChange();
295
// we emit no signal if the data has stayed empty
298
void 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)
315
q_ptr->currentDayChanged(m_currentDay);
318
if (oldLabelEmpty && !newLabelEmpty)
320
q_ptr->dataAppeared();
321
} else if (!oldLabelEmpty && newLabelEmpty)
323
q_ptr->dataDisappeared();
324
} else if (!oldLabelEmpty && !newLabelEmpty)
326
q_ptr->dataChanged();
328
// we emit no signal if the data has stayed empty
331
void InfographicModelPrivate::nextFakeData()
334
if (m_dataIndex == m_fakeData.end() || m_dataIndex.key() != m_username)
336
m_dataIndex = m_fakeData.constFind(m_username);
342
InfographicModel::InfographicModel(QObject *parent) :
343
QObject(parent), d_ptr(new InfographicModelPrivate(this))
345
d_ptr->generateFakeData();
348
connect(this, SIGNAL(nextDataSource()), this, SLOT(nextDataSourceSlot()),
349
Qt::QueuedConnection);
350
connect(this, SIGNAL(readyForDataChange()), this,
351
SLOT(readyForDataChangeSlot()), Qt::QueuedConnection);
354
InfographicModel::~InfographicModel()
359
QString InfographicModel::label() const
361
return d_ptr->m_label;
364
QString InfographicModel::username() const
366
return d_ptr->m_username;
369
void InfographicModel::setUsername(const QString &username)
371
d_ptr->setUsername(username);
374
InfographicColorTheme * InfographicModel::firstColor() const
376
return &d_ptr->m_firstColor;
379
InfographicColorTheme * InfographicModel::secondColor() const
381
return &d_ptr->m_secondColor;
384
QAbstractItemModel * InfographicModel::firstMonth() const
386
return &d_ptr->m_firstMonth;
389
QAbstractItemModel * InfographicModel::secondMonth() const
391
return &d_ptr->m_secondMonth;
394
int InfographicModel::currentDay() const
396
return d_ptr->m_currentDay;
399
void InfographicModel::nextDataSourceSlot()
401
d_ptr->nextFakeData();
404
void InfographicModel::readyForDataChangeSlot()
406
d_ptr->finishSetFakeData();
413
InfographicModel * InfographicModel::getInstance()
415
return new InfographicModel();