Merge remote-tracking branch elecpower/cpn-auto-widget into elecpower/cpn-refactor-gen-settings
This commit is contained in:
@@ -20,72 +20,30 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QCheckBox>
|
||||
#include "genericpanel.h"
|
||||
#include "autowidget.h"
|
||||
|
||||
class AutoCheckBox: public QCheckBox
|
||||
#include <QCheckBox>
|
||||
|
||||
class AutoCheckBox : public QCheckBox, AutoWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AutoCheckBox(QWidget * parent = nullptr):
|
||||
QCheckBox(parent),
|
||||
field(nullptr),
|
||||
panel(nullptr),
|
||||
lock(false),
|
||||
invert(false)
|
||||
{
|
||||
connect(this, &AutoCheckBox::toggled, this, &AutoCheckBox::onToggled);
|
||||
}
|
||||
explicit AutoCheckBox(QWidget * parent = nullptr);
|
||||
~AutoCheckBox();
|
||||
|
||||
void setField(bool & field, GenericPanel * panel = nullptr, bool invert = false)
|
||||
{
|
||||
this->field = &field;
|
||||
this->panel = panel;
|
||||
this->invert = invert;
|
||||
updateValue();
|
||||
}
|
||||
virtual void updateValue() override;
|
||||
|
||||
void setEnabled(bool enabled)
|
||||
{
|
||||
QCheckBox::setEnabled(enabled);
|
||||
}
|
||||
|
||||
void setInvert(bool invert)
|
||||
{
|
||||
this->invert = invert;
|
||||
updateValue();
|
||||
}
|
||||
|
||||
void updateValue()
|
||||
{
|
||||
if (field) {
|
||||
lock = true;
|
||||
setChecked(invert ? !(*field) : *field);
|
||||
lock = false;
|
||||
}
|
||||
}
|
||||
void setField(bool & field, GenericPanel * panel = nullptr, bool invert = false);
|
||||
void setInvert(bool invert);
|
||||
|
||||
signals:
|
||||
void currentDataChanged(bool value);
|
||||
|
||||
protected slots:
|
||||
void onToggled(bool checked)
|
||||
{
|
||||
if (panel && panel->lock)
|
||||
return;
|
||||
if (field && !lock) {
|
||||
const bool val = invert ? !checked : checked;
|
||||
*field = val;
|
||||
emit currentDataChanged(val);
|
||||
if (panel)
|
||||
emit panel->modified();
|
||||
}
|
||||
}
|
||||
void onToggled(bool checked);
|
||||
|
||||
protected:
|
||||
bool *field = nullptr;
|
||||
GenericPanel *panel = nullptr;
|
||||
bool lock = false;
|
||||
bool invert = false;
|
||||
private:
|
||||
bool *m_field;
|
||||
bool m_invert;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user