~macslow/unity8/fix-1475678

« back to all changes in this revision

Viewing changes to tests/mocks/LightDM/InfographicModel.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
18
 
 
19
#include "InfographicModelPrivate.h"
 
20
#include "InfographicModel.h"
 
21
 
 
22
#include <QtCore/QDir>
 
23
#include <QtCore/QString>
 
24
#include <QtGui/QIcon>
 
25
#include <QMultiMap>
 
26
 
 
27
using namespace QLightDM;
 
28
 
 
29
namespace QLightDM
 
30
{
 
31
 
 
32
class InfographicColorThemePrivate: QObject
 
33
{
 
34
public:
 
35
    explicit InfographicColorThemePrivate(InfographicColorTheme *parent = 0);
 
36
 
 
37
    InfographicColorThemePrivate(const QColor &start, const QColor &main,
 
38
            const QColor &end, InfographicColorTheme *parent = 0);
 
39
 
 
40
    ~InfographicColorThemePrivate();
 
41
 
 
42
    InfographicColorTheme * const q_ptr;
 
43
 
 
44
    QColor m_start;
 
45
 
 
46
    QColor m_main;
 
47
 
 
48
    QColor m_end;
 
49
 
 
50
protected:
 
51
    int calculateLength();
 
52
 
 
53
private:
 
54
    Q_DECLARE_PUBLIC(InfographicColorTheme)
 
55
};
 
56
 
 
57
class InfographicDataPrivate: QObject
 
58
{
 
59
public:
 
60
    explicit InfographicDataPrivate(InfographicData *parent);
 
61
 
 
62
    InfographicDataPrivate(const QString &label,
 
63
            const InfographicColorTheme &firstColor,
 
64
            const QVariantList &firstMonth,
 
65
            const InfographicColorTheme &secondColor,
 
66
            const QVariantList &secondMonth, InfographicData *parent);
 
67
 
 
68
    ~InfographicDataPrivate();
 
69
 
 
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;
 
77
 
 
78
protected:
 
79
    int calculateLength();
 
80
 
 
81
private:
 
82
    Q_DECLARE_PUBLIC(InfographicData)
 
83
};
 
84
 
 
85
}
 
86
 
 
87
InfographicColorThemePrivate::InfographicColorThemePrivate(
 
88
        InfographicColorTheme *parent) :
 
89
        q_ptr(parent)
 
90
{
 
91
}
 
92
 
 
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)
 
96
{
 
97
}
 
98
 
 
99
InfographicColorThemePrivate::~InfographicColorThemePrivate()
 
100
{
 
101
}
 
102
 
 
103
InfographicColorTheme::InfographicColorTheme(QObject *parent) :
 
104
        QObject(parent), d_ptr(new InfographicColorThemePrivate(this))
 
105
{
 
106
}
 
107
 
 
108
InfographicColorTheme::InfographicColorTheme(QColor &first, QColor &main,
 
109
        QColor &end, QObject *parent) :
 
110
        QObject(parent), d_ptr(
 
111
                new InfographicColorThemePrivate(first, main, end, this))
 
112
{
 
113
 
 
114
}
 
115
 
 
116
InfographicColorTheme & InfographicColorTheme::operator=(
 
117
        const InfographicColorTheme & other)
 
118
{
 
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
    }
 
129
 
 
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
    }
 
135
 
 
136
    return *this;
 
137
}
 
138
 
 
139
InfographicColorTheme::~InfographicColorTheme()
 
140
{
 
141
    delete d_ptr;
 
142
}
 
143
 
 
144
QColor InfographicColorTheme::start() const
 
145
{
 
146
    return d_ptr->m_start;
 
147
}
 
148
 
 
149
QColor InfographicColorTheme::main() const
 
150
{
 
151
    return d_ptr->m_main;
 
152
}
 
153
 
 
154
QColor InfographicColorTheme::end() const
 
155
{
 
156
    return d_ptr->m_end;
 
157
}
 
158
 
 
159
InfographicDataPrivate::InfographicDataPrivate(InfographicData *parent) :
 
160
        q_ptr(parent), m_firstColor(this), m_secondColor(this)
 
161
{
 
162
    m_length = calculateLength();
 
163
}
 
164
 
 
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)
 
171
{
 
172
    m_length = calculateLength();
 
173
    m_firstColor = firstColor;
 
174
    m_secondColor = secondColor;
 
175
}
 
176
 
 
177
InfographicDataPrivate::~InfographicDataPrivate()
 
178
{
 
179
}
 
180
 
 
181
int InfographicDataPrivate::calculateLength()
 
182
{
 
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
    }
 
194
 
 
195
    return -1;
 
196
}
 
197
 
 
198
InfographicData::InfographicData(QObject *parent) :
 
199
        QObject(parent), d_ptr(new InfographicDataPrivate(this))
 
200
{
 
201
}
 
202
 
 
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))
 
210
{
 
211
}
 
212
 
 
213
InfographicData::~InfographicData()
 
214
{
 
215
    delete d_ptr;
 
216
}
 
217
 
 
218
const QString & InfographicData::label() const
 
219
{
 
220
    return d_ptr->m_label;
 
221
}
 
222
 
 
223
const InfographicColorTheme & InfographicData::firstColor() const
 
224
{
 
225
    return d_ptr->m_firstColor;
 
226
}
 
227
 
 
228
const QVariantList & InfographicData::firstMonth() const
 
229
{
 
230
    return d_ptr->m_firstMonth;
 
231
}
 
232
 
 
233
const InfographicColorTheme & InfographicData::secondColor() const
 
234
{
 
235
    return d_ptr->m_secondColor;
 
236
}
 
237
 
 
238
const QVariantList & InfographicData::secondMonth() const
 
239
{
 
240
    return d_ptr->m_secondMonth;
 
241
}
 
242
 
 
243
int InfographicData::length() const
 
244
{
 
245
    return d_ptr->m_length;
 
246
}
 
247
 
 
248
InfographicModelPrivate::InfographicModelPrivate(InfographicModel *parent) :
 
249
        q_ptr(parent), m_currentDay(0)
 
250
{
 
251
    m_fakeData.insert("", InfographicDataPtr(new InfographicData(this)));
 
252
}
 
253
 
 
254
InfographicModelPrivate::~InfographicModelPrivate()
 
255
{
 
256
}
 
257
 
 
258
void InfographicModelPrivate::setUsername(const QString &username)
 
259
{
 
260
    if(m_username == username) {
 
261
        return;
 
262
    }
 
263
 
 
264
    m_username = username;
 
265
 
 
266
    m_dataIndex = m_fakeData.constFind(m_username);
 
267
    if (m_dataIndex == m_fakeData.end())
 
268
    {
 
269
        m_dataIndex = m_fakeData.constFind("");
 
270
    }
 
271
 
 
272
    loadFakeData();
 
273
 
 
274
    q_ptr->usernameChanged(m_username);
 
275
}
 
276
 
 
277
void InfographicModelPrivate::loadFakeData()
 
278
{
 
279
    m_newData = *m_dataIndex;
 
280
 
 
281
    bool oldLabelEmpty = m_label.isEmpty();
 
282
    bool newLabelEmpty = m_newData->label().isEmpty();
 
283
 
 
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
 
296
}
 
297
 
 
298
void InfographicModelPrivate::finishSetFakeData()
 
299
{
 
300
    bool oldLabelEmpty = m_label.isEmpty();
 
301
    bool newLabelEmpty = m_newData->label().isEmpty();
 
302
 
 
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());
 
308
 
 
309
    bool currentDayChanged = m_currentDay != m_newData->length();
 
310
    m_currentDay = m_newData->length();
 
311
 
 
312
    q_ptr->labelChanged(m_label);
 
313
    if (currentDayChanged)
 
314
    {
 
315
        q_ptr->currentDayChanged(m_currentDay);
 
316
    }
 
317
 
 
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
 
329
}
 
330
 
 
331
void InfographicModelPrivate::nextFakeData()
 
332
{
 
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
    }
 
338
 
 
339
    loadFakeData();
 
340
}
 
341
 
 
342
InfographicModel::InfographicModel(QObject *parent) :
 
343
        QObject(parent), d_ptr(new InfographicModelPrivate(this))
 
344
{
 
345
    d_ptr->generateFakeData();
 
346
    setUsername("");
 
347
 
 
348
    connect(this, SIGNAL(nextDataSource()), this, SLOT(nextDataSourceSlot()),
 
349
            Qt::QueuedConnection);
 
350
    connect(this, SIGNAL(readyForDataChange()), this,
 
351
            SLOT(readyForDataChangeSlot()), Qt::QueuedConnection);
 
352
}
 
353
 
 
354
InfographicModel::~InfographicModel()
 
355
{
 
356
    delete d_ptr;
 
357
}
 
358
 
 
359
QString InfographicModel::label() const
 
360
{
 
361
    return d_ptr->m_label;
 
362
}
 
363
 
 
364
QString InfographicModel::username() const
 
365
{
 
366
    return d_ptr->m_username;
 
367
}
 
368
 
 
369
void InfographicModel::setUsername(const QString &username)
 
370
{
 
371
    d_ptr->setUsername(username);
 
372
}
 
373
 
 
374
InfographicColorTheme * InfographicModel::firstColor() const
 
375
{
 
376
    return &d_ptr->m_firstColor;
 
377
}
 
378
 
 
379
InfographicColorTheme * InfographicModel::secondColor() const
 
380
{
 
381
    return &d_ptr->m_secondColor;
 
382
}
 
383
 
 
384
QAbstractItemModel * InfographicModel::firstMonth() const
 
385
{
 
386
    return &d_ptr->m_firstMonth;
 
387
}
 
388
 
 
389
QAbstractItemModel * InfographicModel::secondMonth() const
 
390
{
 
391
    return &d_ptr->m_secondMonth;
 
392
}
 
393
 
 
394
int InfographicModel::currentDay() const
 
395
{
 
396
    return d_ptr->m_currentDay;
 
397
}
 
398
 
 
399
void InfographicModel::nextDataSourceSlot()
 
400
{
 
401
    d_ptr->nextFakeData();
 
402
}
 
403
 
 
404
void InfographicModel::readyForDataChangeSlot()
 
405
{
 
406
    d_ptr->finishSetFakeData();
 
407
}
 
408
 
 
409
/**
 
410
 * Factory methods
 
411
 */
 
412
 
 
413
InfographicModel * InfographicModel::getInstance()
 
414
{
 
415
    return new InfographicModel();
 
416
}