2015-06-07 16:07:25 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2015 by Andrius Štikonas <andrius@stikonas.eu> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include <QComboBox>
|
|
|
|
|
|
|
|
#include "dialog.h"
|
|
|
|
#include "ui_dialog.h"
|
|
|
|
|
|
|
|
Dialog::Dialog(std::vector<Socket*> *sockets_vector, QWidget *parent) :
|
|
|
|
QDialog(parent),
|
|
|
|
ui(new Ui::Dialog)
|
|
|
|
{
|
|
|
|
sockets = sockets_vector;
|
|
|
|
ui->setupUi(this);
|
2015-11-15 16:43:37 +00:00
|
|
|
connect(ui->toggleButton, &QPushButton::clicked, this, &Dialog::togglePower);
|
2015-11-16 01:51:27 +00:00
|
|
|
connect(ui->socketsComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &Dialog::updateUi);
|
|
|
|
connect(ui->offTimerEnabled, &QCheckBox::released, this, &Dialog::toggleOffTimer);
|
2015-11-15 16:43:37 +00:00
|
|
|
updateUi();
|
2015-11-16 01:51:27 +00:00
|
|
|
ui->offTimer->setEnabled(false);
|
2015-06-07 16:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Dialog::~Dialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog::updateUi()
|
|
|
|
{
|
2015-08-18 15:29:09 +01:00
|
|
|
for (unsigned int i = 0; i < (*sockets).size(); ++i) {
|
2015-11-16 01:51:27 +00:00
|
|
|
ui->socketsComboBox->setItemText(i, (*sockets)[i]->socketName);
|
2015-06-07 16:07:25 +01:00
|
|
|
}
|
2015-11-16 01:51:27 +00:00
|
|
|
unsigned int index = ui->socketsComboBox->currentIndex();
|
|
|
|
if (index != -1) {
|
2015-11-15 16:43:37 +00:00
|
|
|
ui->toggleButton->setEnabled(true);
|
2015-11-16 01:51:27 +00:00
|
|
|
ui->offTimerEnabled->setEnabled(true);
|
|
|
|
ui->toggleButton->setText((*sockets)[index]->powered ? QStringLiteral("Turn off") : QStringLiteral("Turn on"));
|
|
|
|
ui->offTimerEnabled->setChecked((*sockets)[index]->offTimerEnabled);
|
2015-11-15 16:43:37 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ui->toggleButton->setEnabled(false);
|
2015-11-16 01:51:27 +00:00
|
|
|
ui->offTimerEnabled->setEnabled(false);
|
2015-11-15 16:43:37 +00:00
|
|
|
}
|
2015-06-07 16:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog::discovered()
|
|
|
|
{
|
2015-11-16 01:51:27 +00:00
|
|
|
ui->socketsComboBox->clear();
|
2015-08-18 15:29:09 +01:00
|
|
|
for (std::vector<Socket*>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i) {
|
2015-06-07 16:07:25 +01:00
|
|
|
connect(*i, &Socket::stateChanged, this, &Dialog::updateUi);
|
2015-11-16 01:51:27 +00:00
|
|
|
ui->socketsComboBox->addItem("Socket");
|
2015-06-07 16:07:25 +01:00
|
|
|
}
|
2015-08-18 15:29:09 +01:00
|
|
|
|
2015-06-07 16:07:25 +01:00
|
|
|
updateUi();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog::togglePower()
|
|
|
|
{
|
2015-11-16 01:51:27 +00:00
|
|
|
(*sockets)[ui->socketsComboBox->currentIndex()]->toggle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dialog::toggleOffTimer()
|
|
|
|
{
|
|
|
|
ui->offTimerEnabled->setCheckState(Qt::PartiallyChecked);
|
|
|
|
(*sockets)[ui->socketsComboBox->currentIndex()]->toggleOffTimer();
|
2015-06-07 16:07:25 +01:00
|
|
|
}
|