on c488c56 help sign in
unity8/tests/mocks/liblightdm/MockSessionsModel.cpp
122 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";
47 connect(MockController::instance(), &MockController::sessionModeChanged,
48 this, &SessionsModel::resetEntries);
49 connect(MockController::instance(), &MockController::numSessionsChanged,
50 this, &SessionsModel::resetEntries);
51 resetEntries();
54SessionsModel::~SessionsModel()
56 delete d_ptr;
59QVariant SessionsModel::data(const QModelIndex& index, int role) const
61 Q_D(const SessionsModel);
63 if(!index.isValid()) {
64 return QVariant();
65 }
67 int row = index.row();
69 switch (role) {
70 case QLightDM::SessionsModel::KeyRole:
71 return d->sessionItems[row].key;
72 case Qt::DisplayRole:
73 return d->sessionItems[row].name;
74 default:
75 return QVariant();
76 }
79QHash<int, QByteArray> SessionsModel::roleNames() const
81 Q_D(const SessionsModel);
83 return d->roleNames;
86int SessionsModel::rowCount(const QModelIndex& parent) const
88 Q_D(const SessionsModel);
90 if (parent.isValid()) {
91 return 0;
92 } else { // parent is root
93 return d->sessionItems.size();
94 }
97void SessionsModel::resetEntries()
99 Q_D(SessionsModel);
101 beginResetModel();
103 QString sessionMode = MockController::instance()->sessionMode();
105 if (sessionMode == "full") {
106 d->sessionItems = MockController::instance()->fullSessionItems();
107 d->sessionItems = d->sessionItems.mid(0, MockController::instance()->numSessions());
108 } else if (sessionMode == "single") {
109 d->sessionItems = {MockController::instance()->fullSessionItems()[0]};
110 } else {
111 d->sessionItems = {};
112 }
114 endResetModel();
117QObject *SessionsModel::mock()
119 return MockController::instance();
122} // namespace QLightDM