-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
97 lines (82 loc) · 2.64 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QUrl>
#include <QtNetwork/QNetworkRequest>
#include <QPushButton>
#include <iostream>
#include <QTableView>
#include <QTimer>
#include <QScreen>
#include <QDesktopWidget>
#include <QBitmap>
#include "api.h"
#include "grouplistmodel.h"
#include "setting.h"
#include "settings.h"
#include <QDialog>
#include <memory>
std::shared_ptr<Setting> MainWindow::setting = std::make_shared<Setting>();
std::unique_ptr<HueState> MainWindow::HUESTATE = std::make_unique<HueState>();
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
settingsDialog_(nullptr)
{
ui->setupUi(this);
auto connectButton = ui->connectButton;
auto lightTab = static_cast<LightsForm*>(ui->lightsTab);
lightTab->setModel(new LightListModel());
auto groupTab = static_cast<LightsForm*>(ui->groupTab);
groupTab->setModel(new GroupListModel());
connect(connectButton, &QPushButton::pressed, this, &MainWindow::setConnection);
timer_ = new QTimer(this);
timerAmbilight_ = new QTimer(this);
connect(timerAmbilight_, &QTimer::timeout, lightTab, &LightsForm::ambilight);
connect(timer_, &QTimer::timeout, [=](){
MainWindow::HUESTATE->fetchLights();
MainWindow::HUESTATE->fetchGroups();
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setConnection(){
auto tmp = API::get().lock();
if(tmp.get() != nullptr && (tmp->adress_ == ui->adressLineEdit->text() && tmp->api_key_ == ui->apiLineEdit->text())) {
return;
}
std::weak_ptr<API> apiPtr = API::api(ui->adressLineEdit->text(), ui->apiLineEdit->text());
auto api = apiPtr.lock();
if (api && api->isAvailable()){
ui->connectButton->setText("Connected");
HUESTATE->fetchGroups();
HUESTATE->fetchLights();
connected();
}
}
void MainWindow::on_actionRefresh_triggered()
{
connected();
}
void MainWindow::on_actionAutofetch_toggled(bool checked)
{
if(checked) timer_->start(800); //time specified in ms
else timer_->stop();
}
void MainWindow::on_actionAmbilight_toggled(bool checked)
{
if(checked) timerAmbilight_->start(300); //time specified in ms
else timerAmbilight_->stop();
}
void MainWindow::on_actionSettings_triggered()
{
settingsDialog_ = new QDialog(this);
Settings *s = new Settings(settingsDialog_);
s->setSetting(setting);
connect(s, &Settings::opacityChanged, [=]{ this->setWindowOpacity(setting->opacity()/100.0); });
settingsDialog_->setLayout(s->layout());
settingsDialog_->setFixedSize(650, 300);
settingsDialog_->show();
}