Files
UvosSmartHomeInterface/mainwindow.cpp
2017-11-02 20:17:05 +01:00

203 lines
5.9 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QSettings *settings, Microcontroller *micro , bool isRemoteMode , QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
colorChooser(this),
_settings(settings),
_micro(micro)
{
ui->setupUi(this);
if(!_micro->connected()) ui->label_serialRecive->setText("No IO Port! Debug only.");
//Settings
ui->alarmTime->setTime(_settings->value("alarmTime").toTime());
ui->nightTime->setTime(_settings->value("nightTime").toTime());
ui->checkBox_alarm->setChecked(_settings->value("alarmOn").toBool());
//RGB Leds
connect(ui->presettApply, SIGNAL(clicked()), this, SLOT(slotApplyPreset()));
connect(&colorChooser, SIGNAL(currentColorChanged(const QColor)), this, SLOT(slotChangedRgb(const QColor)));
connect(ui->button_lightsOn, SIGNAL(clicked()), _micro, SLOT(rgbOn()));
connect(ui->button_lightsOff, SIGNAL(clicked()), _micro, SLOT(rgbOff()));
connect(ui->button_quit, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->button_color, SIGNAL(clicked()), &colorChooser, SLOT(show()));
new QListWidgetItem(tr("Pattern 0 Solid"), ui->listWidget_patern);
new QListWidgetItem(tr("Pattern 1"), ui->listWidget_patern);
new QListWidgetItem(tr("Pattern 2 Alarm"), ui->listWidget_patern);
new QListWidgetItem(tr("Pattern 3"), ui->listWidget_patern);
new QListWidgetItem(tr("Pattern 4 Sunrise"), ui->listWidget_patern);
//Amp
if(!isRemoteMode)connect(ui->checkBox_ampAuto, SIGNAL(stateChanged(int)), this, SLOT(slotAmpChkbtn(int)));
connect(ui->checkBox_amp, SIGNAL(stateChanged(int)), this, SLOT(slotAmpToggle(int)));
//Bedroom Speakers
connect(ui->checkBox_bspeaker, SIGNAL(stateChanged(int)), this, SLOT(slotBSpeakerToggle(int)));
if(!isRemoteMode)connect(ui->checkBox_bspeakerAuto, SIGNAL(stateChanged(int)), this, SLOT(slotBSpeakerAutoToggle(int)));
//Infinity Mirror
connect(ui->checkBox_inf, SIGNAL(stateChanged(int)), this, SLOT(slotInfMirrorToggle(int)));
if(!isRemoteMode)connect(ui->checkBox_infAuto, SIGNAL(stateChanged(int)), this, SLOT(slotInfMirrorAutoToggle(int)));
//Airconditioner
connect(ui->checkBox_aircon, SIGNAL(stateChanged(int)), this, SLOT(slotAirconToggle(int)));
if(!isRemoteMode)
{
//Alarm
connect(ui->alarmTime, SIGNAL(timeChanged(QTime)), this, SLOT(slotChangedAlarmTime(QTime)));
connect(ui->checkBox_alarm, SIGNAL(stateChanged(int)), this, SIGNAL(signalAlmAlarmStateChanged(int)));
connect(ui->checkBox_alarm, SIGNAL(stateChanged(int)), this, SLOT(saveAlarmState(int)));
//Night Time
connect(ui->nightTime, SIGNAL(timeChanged(QTime)), this, SLOT(slotChangedNightTime(QTime)));
connect(ui->checkBox_nightTime, SIGNAL(stateChanged(int)), this, SIGNAL(signalAlmNightStateChanged(int)));
}
else remoteMode();
//adv relays
connect(ui->button_advRelay, SIGNAL(clicked()), this, SIGNAL(showAdvRelayDialog()));
}
void MainWindow::remoteMode()
{
ui->alarmTime->setEnabled(false);
ui->checkBox_alarm->setEnabled(false);
ui->label_alarm->setEnabled(false);
ui->nightTime->setEnabled(false);
ui->checkBox_nightTime->setEnabled(false);
ui->label_nightTime->setEnabled(false);
ui->checkBox_ampAuto->setEnabled(false);
ui->checkBox_bspeakerAuto->setEnabled(false);
ui->checkBox_amp->setEnabled(true);
ui->checkBox_bspeaker->setEnabled(true);
ui->checkBox_inf->setEnabled(true);
ui->checkBox_infAuto->setEnabled(false);
}
void MainWindow::postActivate()
{
QMetaObject::invokeMethod( this, "signalAlmNightStateChanged", Qt::QueuedConnection, Q_ARG(int, ui->checkBox_nightTime->checkState()) );
signalAlmAlarmStateChanged(ui->checkBox_alarm->checkState());
}
MainWindow::~MainWindow()
{
_settings->sync();
delete ui;
}
void MainWindow::slotChangedRgb(const QColor color)
{
_micro->changeRgbColor(color);
if( ui->checkBox_infAuto->isChecked() )
{
if( color.redF() < 0.2 && color.greenF() < 0.2 && color.blueF() > 0.8 )
{
qDebug()<<"Auto turn on inf mirror\n";
slotInfMirrorToggle(true);
}
else slotInfMirrorToggle(false);
}
}
void MainWindow::slotApplyPreset()
{
if(ui->listWidget_patern->selectedItems().count() == 1) _micro->setPattern(ui->listWidget_patern->currentRow());
}
void MainWindow::slotAmpToggle(int state)
{
_micro->relayToggle(state, 0);
}
void MainWindow::slotBSpeakerToggle(int state)
{
_micro->relayToggle(state, 1);
}
void MainWindow::slotBSpeakerAutoToggle(int state)
{
ui->checkBox_bspeaker->setEnabled(!state);
}
void MainWindow::slotInfMirrorToggle(int state)
{
_micro->relayToggle(state, 2);
}
void MainWindow::slotInfMirrorAutoToggle(int state)
{
ui->checkBox_inf->setEnabled(!state);
if(!state)
{
slotInfMirrorToggle(ui->checkBox_inf->isChecked());
}
}
void MainWindow::slotAirconToggle(int state)
{
_micro->relayToggle(state, 3);
}
void MainWindow::slotChangedAlarmTime(const QTime time)
{
_settings->setValue("alarmTime", time);
signalAlmAlarmChanged(time);
}
void MainWindow::saveAlarmState(int state)
{
_settings->setValue("alarmOn", state);
}
void MainWindow::slotChangedNightTime(const QTime time)
{
_settings->setValue("nightTime", time);
signalAlmNightChanged(time);
}
void MainWindow::slotAmpChkbtn(int state)
{
ui->checkBox_amp->setEnabled(!state);
if(state)
{
signalAmpOn();
}
else
{
signalAmpOff();
slotAmpToggle(ui->checkBox_amp->checkState());
}
}
void MainWindow::changeHeaderLableText(const QString string)
{
ui->label_serialRecive->setText(string);
}
void MainWindow::slotSyncoff()
{
qDebug()<<"Power Off on alarm\n";
_settings->sync();
slotAmpToggle(false);
slotBSpeakerToggle(false);
slotInfMirrorToggle(false);
slotAirconToggle(false);
QProcess::execute ( "syncoff" );
}