Thermostat pour piloter jusqu'à 4 radiateurs avec fil pilote
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.

211 lines
5.1 KiB

  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /*
  3. * Qt mutizone MQTT thermostat
  4. *
  5. * Copyright (C) 2020 Richard Genoud
  6. *
  7. */
  8. #include <QDialogButtonBox>
  9. #include <QSpinBox>
  10. #include <QPushButton>
  11. #include <QVBoxLayout>
  12. #include <QHBoxLayout>
  13. #include <QGridLayout>
  14. #include <QLocale>
  15. #include <QLabel>
  16. #include <QtMath>
  17. #include <QDateTime>
  18. #include <QTime>
  19. #include "settings.h"
  20. #include "edit_dlg.h"
  21. #define SPIN_ARROW_W 75
  22. #define SPIN_ARROW_H 50
  23. #define SPIN_FONT_SZ 20
  24. EditDlg::EditDlg(int idx, QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f)
  25. {
  26. Settings *s = Settings::getInstance();
  27. const struct Room r = s->getRoom(idx);
  28. const struct Program *p = NULL;
  29. QString sheet;
  30. QString text;
  31. QLabel *room_name = new QLabel(QString(tr("%1 default target:").arg(r.name)));
  32. set_font(room_name);
  33. m_idx = idx;
  34. m_progs = r.progs;
  35. m_default_temperature.setDecimals(1);
  36. m_default_temperature.setValue(r.default_temperature);
  37. m_default_temperature.setSingleStep(0.1);
  38. m_default_temperature.setSuffix(" °C");
  39. set_font(&m_default_temperature);
  40. /*
  41. * TODO: setting size in pixels sucks
  42. * We should set size in mm or ratio of the window size
  43. */
  44. sheet = QString("QDoubleSpinBox { height: %1px; font-size: %4px }"
  45. "QDoubleSpinBox::up-button { width: %2px; height: %3px }"
  46. "QDoubleSpinBox::down-button { width: %2px; height: %3px }")
  47. .arg(2*SPIN_ARROW_H)
  48. .arg(SPIN_ARROW_W)
  49. .arg(SPIN_ARROW_H)
  50. .arg(SPIN_FONT_SZ);
  51. m_default_temperature.setStyleSheet(sheet);
  52. QHBoxLayout *upperLayout = new QHBoxLayout;
  53. upperLayout->addWidget(room_name);
  54. upperLayout->addWidget(&m_default_temperature);
  55. QPushButton *ok_btn = new QPushButton(tr("Ok"), this);
  56. QPushButton *cancel_btn = new QPushButton(tr("Cancel"), this);
  57. QSizePolicy *szPolicy = new QSizePolicy(QSizePolicy::Minimum,
  58. QSizePolicy::MinimumExpanding,
  59. QSizePolicy::PushButton);
  60. set_font(ok_btn);
  61. set_font(cancel_btn);
  62. ok_btn->setSizePolicy(*szPolicy);
  63. cancel_btn->setSizePolicy(*szPolicy);
  64. connect(ok_btn, SIGNAL(clicked(void)), this, SLOT(save(void)));
  65. connect(cancel_btn, SIGNAL(clicked(void)), this, SLOT(reject(void)));
  66. QHBoxLayout *btnLayout = new QHBoxLayout;
  67. btnLayout->addWidget(cancel_btn);
  68. btnLayout->addWidget(ok_btn);
  69. QVBoxLayout *topLayout = new QVBoxLayout;
  70. topLayout->addLayout(upperLayout);
  71. QGridLayout *progLayout = new QGridLayout;
  72. for (int i = 0; i < m_progs.count(); i++) {
  73. p = &(m_progs.at(i));
  74. text = QString("");
  75. if (p->temperature == FORCE_OFF) {
  76. text += QString("-");
  77. } else {
  78. if (p->temperature == FORCE_ON) {
  79. text += QString("+");
  80. } else {
  81. text += QString::number(p->temperature);
  82. }
  83. }
  84. text += QString("°C");
  85. QLabel *prog_temperature = new QLabel(QString(text));
  86. set_font(prog_temperature);
  87. progLayout->addWidget(prog_temperature, i, 1, Qt::AlignHCenter);
  88. text = QString("");
  89. if (p->DoW == 0) {
  90. text += QString("-");
  91. } else {
  92. bool isFirst = true;
  93. for (int j = 0; j < 7; j++) {
  94. if (p->DoW & (1 << j)) {
  95. if (isFirst) {
  96. isFirst = false;
  97. } else {
  98. text += QString("/");
  99. }
  100. switch (j) {
  101. case 0: text += QString(tr("Mo")); break;
  102. case 1: text += QString(tr("Tu")); break;
  103. case 2: text += QString(tr("We")); break;
  104. case 3: text += QString(tr("Th")); break;
  105. case 4: text += QString(tr("Fr")); break;
  106. case 5: text += QString(tr("Sa")); break;
  107. case 6: text += QString(tr("Su")); break;
  108. }
  109. }
  110. }
  111. }
  112. QLabel *prog_DoW = new QLabel(QString(text));
  113. set_font(prog_DoW);
  114. progLayout->addWidget(prog_DoW, i, 2, Qt::AlignHCenter);
  115. text = QString("");
  116. text += p->start_time.toString(QString(" HH:mm"));
  117. text += p->end_time.toString(QString("-HH:mm"));
  118. QLabel *prog_time= new QLabel(QString(text));
  119. set_font(prog_time);
  120. progLayout->addWidget(prog_time, i, 3, Qt::AlignHCenter);
  121. QPushButton *edit_btn = new QPushButton(tr("Edit"), this);
  122. QPushButton *delete_btn = new QPushButton(tr("Delete"), this);
  123. set_font(edit_btn);
  124. set_font(delete_btn);
  125. edit_btn->setSizePolicy(*szPolicy);
  126. delete_btn->setSizePolicy(*szPolicy);
  127. edit_btn->setProperty("idx", i);
  128. delete_btn->setProperty("idx", i);
  129. connect(delete_btn, SIGNAL(clicked(void)), this, SLOT(delete_prog_clicked(void)));
  130. progLayout->addWidget(edit_btn, i, 4, Qt::AlignHCenter);
  131. progLayout->addWidget(delete_btn, i, 5, Qt::AlignHCenter);
  132. topLayout->addLayout(progLayout);
  133. }
  134. topLayout->addLayout(btnLayout);
  135. this->setLayout(topLayout);
  136. }
  137. void EditDlg::set_font(QWidget *widget)
  138. {
  139. QFont font = widget->font();
  140. font.setPointSize(12);
  141. font.setBold(true);
  142. widget->setFont(font);
  143. QSizePolicy *szQPolicy = new QSizePolicy(QSizePolicy::Minimum,
  144. QSizePolicy::MinimumExpanding,
  145. QSizePolicy::PushButton);
  146. widget->setSizePolicy(*szQPolicy);
  147. }
  148. void EditDlg::save(void)
  149. {
  150. Settings *s = Settings::getInstance();
  151. s->setRoomDefaultTemperature(m_idx, m_default_temperature.value());
  152. s->setRoomPrograms(m_idx, m_progs);
  153. emit close_edit_dlg();
  154. }
  155. void EditDlg::reject(void)
  156. {
  157. emit close_edit_dlg();
  158. }
  159. void EditDlg::delete_prog_clicked(void)
  160. {
  161. }
  162. EditDlg::~EditDlg()
  163. {
  164. }
  165. /* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */