-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightlistmodel.cpp
112 lines (90 loc) · 2.88 KB
/
lightlistmodel.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "lightlistmodel.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QUrl>
#include <qeventloop.h>
#include "api.h"
#include <iostream>
#include <vector>
#include "light.h"
#include <memory>
#include <algorithm>
#include "mainwindow.h"
#include <QCoreApplication>
#include <QVariantMap>
#include <tuple>
#include <QFileIconProvider>
using namespace std;
LightListModel::LightListModel(QObject *parent)
{
connect(MainWindow::HUESTATE.get(), &HueState::setLight,
[this](int row){
emit dataChanged(index(row), index(row));
});
setFetchConnection();
}
void LightListModel::setFetchConnection(){
connect(MainWindow::HUESTATE.get(), &HueState::startFetchingLights, [this](){ beginResetModel(); });
connect(MainWindow::HUESTATE.get(), &HueState::fetchedLights, [this](){ endResetModel(); });
}
int LightListModel::rowCount(const QModelIndex &parent) const
{
return MainWindow::HUESTATE->lights_.size();
}
QVariant LightListModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::SizeHintRole)
return QSize(100, 40); // set row height
if(MainWindow::HUESTATE->lights_.size() > index.row() && index.row() >= 0){
Light l = lightAt(index);
QColor color = QColor::fromHsv(l.hue(), l.sat(), l.bri()).toRgb();
if(role == Qt::DisplayRole){
QString name = l.name();
return name;
}
else if(role == Qt::BackgroundRole){
if(color.lightness() <= 20){
color = QColor::fromRgb(100,100,100);
}
return QBrush(color);
}
else if(role == Qt::ForegroundRole){
if(color.lightness() <= 20){
return QColor(255,255,255);
}
else return QVariant();
}
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> LightListModel::lightAt(const QModelIndex &index){
return MainWindow::HUESTATE->lights_.at(index.row());
}
Light LightListModel::lightAt(const QModelIndex &index) const{
return *MainWindow::HUESTATE->lights_.at(index.row());
}
void LightListModel::setLight(const QModelIndex &index, Light newLight)
{
MainWindow::HUESTATE->updateLightState(index.row(), newLight);
emit dataChanged(index, index);
}
void LightListModel::addAsAmbilight(const QModelIndex& index)
{
std::shared_ptr<Light> l = MainWindow::HUESTATE->lights_.at(index.row());
MainWindow::HUESTATE->addAsAmbilight(l);
l->setIsAmbi(true);
}
void LightListModel::removeFromAmbilight(const QModelIndex& index)
{
MainWindow::HUESTATE->removeFromAmbilight(index.row());
}
void LightListModel::setAmbiState(const Light& lightState)
{
MainWindow::HUESTATE->setAmbiState(lightState);
}