From e4210ac3b6ba2a25bfd217d529a4ecd929b7ed6e Mon Sep 17 00:00:00 2001 From: Richard Genoud Date: Sat, 26 Sep 2020 17:50:34 +0200 Subject: [PATCH] thermostat: edit_dlg: suppress a line --- soft/thermostat/inc/edit_dlg.h | 4 +-- soft/thermostat/src/edit_dlg.cpp | 76 +++++++++++++++++++++++++++++++--------- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/soft/thermostat/inc/edit_dlg.h b/soft/thermostat/inc/edit_dlg.h index e701f8f..d20ad4c 100644 --- a/soft/thermostat/inc/edit_dlg.h +++ b/soft/thermostat/inc/edit_dlg.h @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include class EditDlg : public QWidget @@ -30,7 +30,7 @@ private: void set_font(QWidget *widget); QDoubleSpinBox m_default_temperature; - QGridLayout *m_progLayout; + QVBoxLayout *m_progsLayout; QVector m_progs; int m_idx; // room index diff --git a/soft/thermostat/src/edit_dlg.cpp b/soft/thermostat/src/edit_dlg.cpp index 73adf9e..e02ac61 100644 --- a/soft/thermostat/src/edit_dlg.cpp +++ b/soft/thermostat/src/edit_dlg.cpp @@ -6,6 +6,7 @@ * */ +#include #include #include #include @@ -89,11 +90,13 @@ EditDlg::EditDlg(int idx, QWidget *parent, Qt::WindowFlags f) : QWidget(parent, topLayout->addLayout(upperLayout); - m_progLayout = new QGridLayout; + m_progsLayout = new QVBoxLayout; for (int i = 0; i < m_progs.count(); i++) { p = &(m_progs.at(i)); + QHBoxLayout *progLayout = new QHBoxLayout; + text = QString(""); if (p->temperature == FORCE_OFF) { text += QString("-"); @@ -108,7 +111,7 @@ EditDlg::EditDlg(int idx, QWidget *parent, Qt::WindowFlags f) : QWidget(parent, QLabel *prog_temperature = new QLabel(QString(text)); set_font(prog_temperature); - m_progLayout->addWidget(prog_temperature, i, 0, Qt::AlignHCenter); + progLayout->addWidget(prog_temperature, 0, Qt::AlignHCenter); text = QString(""); if (p->DoW == 0) { @@ -137,7 +140,7 @@ EditDlg::EditDlg(int idx, QWidget *parent, Qt::WindowFlags f) : QWidget(parent, } QLabel *prog_DoW = new QLabel(QString(text)); set_font(prog_DoW); - m_progLayout->addWidget(prog_DoW, i, 1, Qt::AlignHCenter); + progLayout->addWidget(prog_DoW, 0, Qt::AlignHCenter); text = QString(""); text += p->start_time.toString(QString(" HH:mm")); @@ -145,7 +148,7 @@ EditDlg::EditDlg(int idx, QWidget *parent, Qt::WindowFlags f) : QWidget(parent, QLabel *prog_time= new QLabel(QString(text)); set_font(prog_time); - m_progLayout->addWidget(prog_time, i, 2, Qt::AlignHCenter); + progLayout->addWidget(prog_time, 0, Qt::AlignHCenter); QPushButton *edit_btn = new QPushButton(tr("Edit"), this); QPushButton *delete_btn = new QPushButton(tr("Delete"), this); @@ -161,11 +164,12 @@ EditDlg::EditDlg(int idx, QWidget *parent, Qt::WindowFlags f) : QWidget(parent, connect(delete_btn, SIGNAL(clicked(void)), this, SLOT(delete_prog_clicked(void))); - m_progLayout->addWidget(edit_btn, i, 3, Qt::AlignHCenter); - m_progLayout->addWidget(delete_btn, i, 4, Qt::AlignHCenter); + progLayout->addWidget(edit_btn, 0, Qt::AlignHCenter); + progLayout->addWidget(delete_btn, 0, Qt::AlignHCenter); - topLayout->addLayout(m_progLayout); + m_progsLayout->addLayout(progLayout); } + topLayout->addLayout(m_progsLayout); topLayout->addLayout(btnLayout); @@ -202,10 +206,11 @@ void EditDlg::reject(void) void EditDlg::delete_prog_clicked(void) { - QLayoutItem *item; + QLayoutItem *lineItem, *item; + QLayout *layout; QObject* obj = sender(); - QVariant v; QWidget *w; + QVariant v; int idx; v = obj->property("idx"); @@ -213,27 +218,64 @@ void EditDlg::delete_prog_clicked(void) return; idx = v.toInt(); - if (idx >= m_progLayout->rowCount()) { + if (idx >= m_progsLayout->count()) { // TODO ERROR: index mismatch return; } + // remove the corresponding program m_progs.remove(idx); - for (int i = 0; i < m_progLayout->columnCount(); i++) { - item = m_progLayout->itemAtPosition(idx, i); + // retrieve the corresponding line + lineItem = m_progsLayout->itemAt(idx); + if (lineItem == NULL) { + // TODO: hum, this should not happend + return; + } + + // and remove it + m_progsLayout->removeItem(lineItem); + + // We also need to remove the Hlayout and all widgets attached + layout = lineItem->layout(); + + if (layout == NULL) { + // TODO: this shouldn't happend neither + return; + } + + while (layout->count()) { + item = layout->itemAt(0); if (item && item->widget()) { w = item->widget(); - m_progLayout->removeWidget(w); + layout->removeWidget(w); w->hide(); delete w; + } else { + layout->removeItem(item); + delete item; } } - - for (int j = idx; j < (m_progLayout->rowCount() - 1); j++) { - + delete lineItem; + + // renumber the buttons' properties + for (int i = idx; i < m_progsLayout->count(); i++) { + lineItem = m_progsLayout->itemAt(i); + if (lineItem && lineItem->layout()) { + layout = lineItem->layout(); + for (int j = 0; j < layout->count(); j++) { + item = layout->itemAt(j); + if (item && item->widget()) { + w = item->widget(); + v = w->property("idx"); + if (v.isValid()) { + qDebug() << "new:" << v.toInt()-1; + w->setProperty("idx", v.toInt() - 1); + } + } + } + } } - } EditDlg::~EditDlg()