-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcostumelightlistmodel.cpp
68 lines (60 loc) · 1.77 KB
/
costumelightlistmodel.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
64
65
66
67
68
#include "costumelightlistmodel.h"
#include <QAbstractListModel>
#include <QColor>
#include <QIcon>
#include <QSize>
CostumeLightListModel::CostumeLightListModel(QObject *parent)
: QAbstractListModel(parent),
lights_()
{
}
int CostumeLightListModel::rowCount(const QModelIndex &parent) const
{
return lights_.size();
}
QVariant CostumeLightListModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::SizeHintRole)
return QSize(100, 40); // set row height
if(lights_.size() > index.row() && index.row() >= 0){
Light l = lightAt(index);
if(role == Qt::DisplayRole){
QString name = l.name();
return name;
}
else if(role == Qt::DecorationRole){
if(l.on()) return QIcon(":/e27_on_waca.svg");
else return QIcon(":/e27_off_waca.svg");
}
}
return QVariant();
}
std::weak_ptr<Light> CostumeLightListModel::lightAt(const QModelIndex &index)
{
return lights_.at(index.row());
}
Light CostumeLightListModel::lightAt(const QModelIndex &index) const
{
auto light = lights_.at(index.row()).lock();
if(!light){
Light l;
l.setName("deleted");
return l;
}
return *light;
}
void CostumeLightListModel::addLight(std::shared_ptr<Light> light){
beginInsertRows(index(lights_.size()),0,0);
lights_.push_back(light);
endInsertRows();
}
void CostumeLightListModel::removeLight(const QModelIndex &index){
beginRemoveRows(index,0,0);
lights_.erase(lights_.begin()+index.row());
endRemoveRows();
}
void CostumeLightListModel::resetList(const std::vector<std::weak_ptr<Light>>& lights){
beginResetModel();
lights_ = lights;
endResetModel();
}