Commit e0855a9e authored by Edric Milaret's avatar Edric Milaret Committed by gerrit2

settings: prepare for hiding/disabling not relevant field in settings panel

Refs #75422

Change-Id: I1056e3a041479c18f01519616d2551f4d2923edb
parent 5d61a061
......@@ -30,112 +30,128 @@ constexpr static const char LRC_CFG[] = "lrcfg_";
constexpr static const int LRC_CFG_LEN = 6 ;
struct ConnHolder {
QMetaObject::Connection c;
~ConnHolder();
QMetaObject::Connection c;
~ConnHolder();
};
Q_DECLARE_METATYPE(ConnHolder*);
ConnHolder::~ConnHolder()
{
QObject::disconnect(c);
QObject::disconnect(c);
}
static void avoidDuplicate(QWidget* w)
{
if (qvariant_cast<ConnHolder*>(w->property("lrcfgConn")))
delete qvariant_cast<ConnHolder*>(w->property("lrcfgConn"));
if (qvariant_cast<ConnHolder*>(w->property("lrcfgConn")))
delete qvariant_cast<ConnHolder*>(w->property("lrcfgConn"));
}
/**
* This check for some supported widgets and bind the widgets and property
*/
static void setupWidget(QWidget* w, Account* a, const QHash<QByteArray, int>& roles)
static void setupWidget(QWidget* w,
Account* a,
const QHash<QByteArray, int>& roles)
{
if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) {
const QByteArray prop = w->objectName().mid(LRC_CFG_LEN, 999).toLatin1();
if (roles.contains(prop)) {
const int role = roles[prop];
if (qobject_cast<QLineEdit*>(w)) {
QLineEdit* le = qobject_cast<QLineEdit*>(w);
avoidDuplicate(le);
le->setText(a->roleData(role).toString());
ConnHolder* c = new ConnHolder {
QObject::connect(le, &QLineEdit::textChanged, [a,role](const QString& text) {
if (a->roleData(role) != text)
a->setRoleData(role, text);
})
};
le->setProperty("lrcfgConn",QVariant::fromValue(c));
}
else if (qobject_cast<QSpinBox*>(w)) {
QSpinBox* sb = qobject_cast<QSpinBox*>(w);
avoidDuplicate(sb);
sb->setValue(a->roleData(role).toInt());
ConnHolder* c = new ConnHolder {
QObject::connect(sb, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [a,role](int value) {
if (a->roleData(role).toInt() != value)
a->setRoleData(role, value);
})
};
sb->setProperty("lrcfgConn",QVariant::fromValue(c));
}
else if (qobject_cast<QAbstractButton*>(w)) {
//QCheckBox, QRadioButton, QToolButton, QPushButton
QAbstractButton* b = qobject_cast<QAbstractButton*>(w);
avoidDuplicate(b);
b->setChecked(a->roleData(role).toBool());
ConnHolder* c = new ConnHolder {
QObject::connect(b, &QAbstractButton::toggled,[a,role](bool c) {
if (a->roleData(role).toBool() != c)
a->setRoleData(role, c);
})
};
b->setProperty("lrcfgConn",QVariant::fromValue(c));
}
}
else {
qWarning() << "Unknown config properties" << w->objectName();
}
}
if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) {
const QByteArray prop = w->objectName().mid(LRC_CFG_LEN, 999).toLatin1();
if (roles.contains(prop)) {
const int role = roles[prop];
auto roleState = a->roleState(static_cast<Account::Role>(role));
if (roleState == Account::RoleState::READ_WRITE
|| roleState == Account::RoleState::READ_ONLY) {
if (qobject_cast<QLineEdit*>(w)) {
QLineEdit* le = qobject_cast<QLineEdit*>(w);
avoidDuplicate(le);
le->setText(a->roleData(role).toString());
ConnHolder* c = new ConnHolder {
QObject::connect(le, &QLineEdit::textChanged,
[a,role](const QString& text) {
if (a->roleData(role) != text)
a->setRoleData(role, text);
})
};
le->setProperty("lrcfgConn",QVariant::fromValue(c));
}
else if (qobject_cast<QSpinBox*>(w)) {
QSpinBox* sb = qobject_cast<QSpinBox*>(w);
avoidDuplicate(sb);
sb->setValue(a->roleData(role).toInt());
ConnHolder* c = new ConnHolder {
QObject::connect(sb, static_cast<void (QSpinBox::*)
(int)>(&QSpinBox::valueChanged),
[a,role](int value) {
if (a->roleData(role).toInt() != value)
a->setRoleData(role, value);
})
};
sb->setProperty("lrcfgConn",QVariant::fromValue(c));
}
else if (qobject_cast<QAbstractButton*>(w)) {
//QCheckBox, QRadioButton, QToolButton, QPushButton
QAbstractButton* b = qobject_cast<QAbstractButton*>(w);
avoidDuplicate(b);
b->setChecked(a->roleData(role).toBool());
ConnHolder* c = new ConnHolder {
QObject::connect(b, &QAbstractButton::toggled,
[a,role](bool c) {
if (a->roleData(role).toBool() != c)
a->setRoleData(role, c);
})
};
b->setProperty("lrcfgConn",QVariant::fromValue(c));
}
w->setVisible(true);
w->setEnabled(roleState == Account::RoleState::READ_WRITE);
} else {
w->setVisible(false);
}
}
else {
qWarning() << "Unknown config properties" << w->objectName();
}
}
}
static void clearConnections(QWidget* w)
{
if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) {
avoidDuplicate(w);
}
if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) {
avoidDuplicate(w);
}
}
static void drill(QWidget* w, Account* a, const QHash<QByteArray, int>& roles, bool clear = false)
static void drill(QWidget* w, Account* a,
const QHash<QByteArray, int>& roles,
bool clear = false)
{
for (QObject *object : w->children()) {
if (!object->isWidgetType())
continue;
for (QObject *object : w->children()) {
if (!object->isWidgetType())
continue;
QWidget* w2 = static_cast<QWidget*>(object);
if (clear)
clearConnections(w2);
else
setupWidget(w2, a, roles);
QWidget* w2 = static_cast<QWidget*>(object);
if (clear)
clearConnections(w2);
else
setupWidget(w2, a, roles);
drill(w2, a, roles);
}
drill(w2, a, roles);
}
}
AccountSerializationAdapter::AccountSerializationAdapter(Account* a, QWidget* w) : QObject(w)
AccountSerializationAdapter::AccountSerializationAdapter(Account* a,
QWidget* w)
: QObject(w)
{
static QHash<QByteArray, int> reverse;
if (reverse.isEmpty()) {
const QHash<int, QByteArray> a = AccountModel::instance()->roleNames();
for (QHash<int, QByteArray>::const_iterator i = a.begin(); i != a.end(); ++i) {
reverse[i.value()] = i.key();
}
}
static QHash<QByteArray, int> reverse;
if (reverse.isEmpty()) {
const QHash<int, QByteArray> a = AccountModel::instance()->roleNames();
for (QHash<int, QByteArray>::const_iterator i = a.begin(); i != a.end(); ++i) {
reverse[i.value()] = i.key();
}
}
drill(w, a, reverse);
drill(w, a, reverse);
}
AccountSerializationAdapter::~AccountSerializationAdapter()
{
}
{}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment