on a45f16d help sign in
unity8/tests/mocks/liblightdm/MockSessionsModel.cpp
118 lines 3.1 kB history raw
1/*
2 * Copyright (C) 2015-2016 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 */
19// LightDM currently is Qt4 compatible, and so doesn't define setRoleNames.
20// To use the same method of setting role name that it does, we
21// set our compatibility to Qt4 here too.
22#define QT_DISABLE_DEPRECATED_BEFORE QT_VERSION_CHECK(4, 0, 0)
24#include "MockController.h"
25#include "MockSessionsModel.h"
27namespace QLightDM
30class SessionsModelPrivate
32public:
33 QHash<int, QByteArray> roleNames;
34 QList<MockController::SessionItem> sessionItems;
35};
37SessionsModel::SessionsModel(QObject* parent)
38 : QAbstractListModel(parent)
39 , d_ptr(new SessionsModelPrivate)
41 Q_D(SessionsModel);
43 d->roleNames = QAbstractListModel::roleNames();
44 d->roleNames[KeyRole] = "key";
45 d->roleNames[TypeRole] = "type";
46 setRoleNames(d->roleNames);
48 connect(MockController::instance(), &MockController::sessionModeChanged,
49 this, &SessionsModel::resetEntries);
50 connect(MockController::instance(), &MockController::numSessionsChanged,
51 this, &SessionsModel::resetEntries);
52 resetEntries();
55SessionsModel::~SessionsModel()
57 delete d_ptr;
60QVariant SessionsModel::data(const QModelIndex& index, int role) const
62 Q_D(const SessionsModel);
64 if(!index.isValid()) {
65 return QVariant();
66 }
68 int row = index.row();
70 switch (role) {
71 case QLightDM::SessionsModel::KeyRole:
72 return d->sessionItems[row].key;
73 case Qt::DisplayRole:
74 return d->sessionItems[row].name;
75 default:
76 return QVariant();
77 }
80QHash<int, QByteArray> SessionsModel::roleNames() const
82 Q_D(const SessionsModel);
84 return d->roleNames;
87int SessionsModel::rowCount(const QModelIndex& parent) const
89 Q_D(const SessionsModel);
91 if (parent.isValid()) {
92 return 0;
93 } else { // parent is root
94 return d->sessionItems.size();
95 }
98void SessionsModel::resetEntries()
100 Q_D(SessionsModel);
102 beginResetModel();
104 QString sessionMode = MockController::instance()->sessionMode();
106 if (sessionMode == "full") {
107 d->sessionItems = MockController::instance()->fullSessionItems();
108 d->sessionItems = d->sessionItems.mid(0, MockController::instance()->numSessions());
109 } else if (sessionMode == "single") {
110 d->sessionItems = {MockController::instance()->fullSessionItems()[0]};
111 } else {
112 d->sessionItems = {};
113 }
115 endResetModel();
118} // namespace QLightDM