Orvibo S20 socket manager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dialog.cpp 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*************************************************************************
  2. * Copyright (C) 2015 by Andrius Štikonas <andrius@stikonas.eu> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 3 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.*
  16. *************************************************************************/
  17. #include <QComboBox>
  18. #include "dialog.h"
  19. #include "ui_dialog.h"
  20. Dialog::Dialog(std::vector<Socket*> *sockets_vector, QWidget *parent) :
  21. QDialog(parent),
  22. ui(new Ui::Dialog)
  23. {
  24. sockets = sockets_vector;
  25. ui->setupUi(this);
  26. connect(ui->toggleButton, &QPushButton::clicked, this, &Dialog::togglePower);
  27. connect(ui->socketsComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &Dialog::updateUi);
  28. connect(ui->offTimerEnabled, &QCheckBox::released, this, &Dialog::toggleOffTimer);
  29. updateUi();
  30. ui->offTimer->setEnabled(false);
  31. }
  32. void Dialog::updateUi()
  33. {
  34. for (unsigned int i = 0; i < (*sockets).size(); ++i) {
  35. ui->socketsComboBox->setItemText(i, (*sockets)[i]->socketName);
  36. }
  37. int index = ui->socketsComboBox->currentIndex();
  38. if (index != -1) {
  39. ui->toggleButton->setEnabled(true);
  40. ui->offTimerEnabled->setEnabled(true);
  41. ui->toggleButton->setText((*sockets)[index]->powered ? QStringLiteral("Turn off") : QStringLiteral("Turn on"));
  42. ui->offTimerEnabled->setChecked((*sockets)[index]->offTimerEnabled);
  43. }
  44. else {
  45. ui->toggleButton->setEnabled(false);
  46. ui->offTimerEnabled->setEnabled(false);
  47. }
  48. }
  49. void Dialog::discovered()
  50. {
  51. ui->socketsComboBox->clear();
  52. for (const auto &socket : *sockets) {
  53. connect(socket, &Socket::stateChanged, this, &Dialog::updateUi);
  54. ui->socketsComboBox->addItem("Socket");
  55. }
  56. updateUi();
  57. }
  58. void Dialog::togglePower()
  59. {
  60. (*sockets)[ui->socketsComboBox->currentIndex()]->toggle();
  61. }
  62. void Dialog::toggleOffTimer()
  63. {
  64. ui->offTimerEnabled->setCheckState(Qt::PartiallyChecked);
  65. (*sockets)[ui->socketsComboBox->currentIndex()]->toggleOffTimer();
  66. }