-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightsform.cpp
93 lines (86 loc) · 2.45 KB
/
lightsform.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
#include "lightlistmodel.h"
#include "lightsform.h"
#include "ui_lightsform.h"
#include "mainwindow.h"
#include "grouplistmodel.h"
#include <QDialog>
#include <iostream>
#include <QListView>
#include <QWindow>
#include <QColorDialog>
#include <QScreen>
#include <QRgb>
#include <memory>
#include <stdlib.h>
LightsForm::LightsForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::LightsForm)
{
ui->setupUi(this);
formSingleLight_ = static_cast<FormSingleLight*> (ui->form);
ui->form->hide();
}
void LightsForm::setModel(LightListModel* model){
auto *lightsListModel = model;
lightListModel_ = lightsListModel;
connect(
formSingleLight_,
&FormSingleLight::changed,
[=](Light l, QModelIndex index){
lightListModel_->setLight(index, l);
}
);
connect(MainWindow::HUESTATE.get(), &HueState::setLight, [=](){
ui->form->refreshUI();
});
connect(formSingleLight_, &FormSingleLight::addAsAmbi, [=](bool arg, QModelIndex index){
if(arg) lightListModel_->addAsAmbilight(index);
else lightListModel_->removeFromAmbilight(index);
});
ui->listView->setModel(lightsListModel);
}
LightsForm::~LightsForm()
{
delete ui;
}
QColor LightsForm::avgScreenColor(){
QScreen *screen = QGuiApplication::primaryScreen();
QImage img = screen->grabWindow(0).toImage();
double r = 0;
double g = 0;
double b = 0;
int ratio = 0;
for(auto i = 0; i < img.width(); i+=4){
for(auto j = 0; j < img.height(); j+=4){
QColor color(img.pixel(i, j));
ratio++;
r += color.red();
g += color.green();
b += color.blue();
}
}
r /= ratio;
g /= ratio;
b /= ratio;
return QColor::fromRgb(r, g, b);
}
void LightsForm::ambilight() {
QColor color = avgScreenColor();
Light l;
l.setHue(std::max(color.hsvHue(), 0));
l.setSat(std::min(color.toHsv().saturation()*MainWindow::setting->ambiSatFactor()/100.0, 255.0));
l.setBri(MainWindow::setting->ambiBrightness()/100.0*255);
l.setOn(true);
lightListModel_->setAmbiState(l);
}
void LightsForm::showForm(Light curLight_, QModelIndex index) {
formSingleLight_->setLight(curLight_, index);
ui->form->show();
}
void LightsForm::on_listView_clicked(const QModelIndex &index)
{
std::weak_ptr<Light> l = lightListModel_->lightAt(index);
QModelIndex curIndex = index;
auto light = l.lock();
showForm(*light, curIndex);
}