-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmygrouplistmodel.cpp
63 lines (55 loc) · 1.95 KB
/
mygrouplistmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "mygrouplistmodel.h"
#include <QDebug>
#include "mainwindow.h"
MyGroupListModel::MyGroupListModel(QObject *parent)
: QAbstractListModel(parent),
groups_()
{
connect(MainWindow::HUESTATE.get(), &HueState::startFetchingLights, [this](){ beginResetModel(); });
connect(MainWindow::HUESTATE.get(), &HueState::fetchedLights, [this](){ endResetModel(); });
connect(MainWindow::HUESTATE.get(), &HueState::setLight,
[this](int row){
emit dataChanged(index(row), index(row));
});
}
int MyGroupListModel::rowCount(const QModelIndex &parent) const
{
return MainWindow::HUESTATE->myGroups_.size();
}
QVariant MyGroupListModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::SizeHintRole)
return QSize(100, 40);
if(MainWindow::HUESTATE->myGroups_.size() > index.row() && index.row() >= 0) {
Group group = getGroup(index);
if(role == Qt::DisplayRole){
QString name = group.name();
return name;
}
}
return QVariant();
}
void MyGroupListModel::addGroup(Group g){
beginInsertRows(index(MainWindow::HUESTATE->myGroups_.size()), 0, 0);
MainWindow::HUESTATE->myGroups_.push_back(std::make_shared<Group>(g));
endInsertRows();
}
void MyGroupListModel::removeGroup(QModelIndex index){
if(index.row() > MainWindow::HUESTATE->myGroups_.size() && index.row() > 0) return;
beginRemoveRows(index,0,0);
MainWindow::HUESTATE->myGroups_.erase(MainWindow::HUESTATE->myGroups_.begin()+index.row());
endRemoveRows();
}
std::weak_ptr<Group> MyGroupListModel::getGroup(QModelIndex index)
{
return MainWindow::HUESTATE->myGroups_.at(index.row());
}
Group MyGroupListModel::getGroup(QModelIndex index) const
{
return *MainWindow::HUESTATE->myGroups_.at(index.row());
}
void MyGroupListModel::setGroup(const QModelIndex &index, Light newLight)
{
MainWindow::HUESTATE->updateMyGroupState(index.row(), newLight);
emit dataChanged(index, index);
}