Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2015 by Savoir-Faire Linux * |
| 3 | * Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>* |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | **************************************************************************/ |
| 18 | #include "accountserializationadapter.h" |
| 19 | |
| 20 | #include <QtWidgets/QWidget> |
| 21 | #include <QtWidgets/QLayout> |
| 22 | #include <QtWidgets/QLineEdit> |
| 23 | #include <QtWidgets/QSpinBox> |
| 24 | #include <QtWidgets/QAbstractButton> |
| 25 | |
| 26 | #include <account.h> |
| 27 | #include <accountmodel.h> |
| 28 | |
| 29 | constexpr static const char LRC_CFG[] = "lrcfg_"; |
| 30 | constexpr static const int LRC_CFG_LEN = 6 ; |
| 31 | |
| 32 | struct ConnHolder { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 33 | QMetaObject::Connection c; |
| 34 | ~ConnHolder(); |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 35 | }; |
| 36 | Q_DECLARE_METATYPE(ConnHolder*); |
| 37 | |
| 38 | ConnHolder::~ConnHolder() |
| 39 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 40 | QObject::disconnect(c); |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static void avoidDuplicate(QWidget* w) |
| 44 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 45 | if (qvariant_cast<ConnHolder*>(w->property("lrcfgConn"))) |
| 46 | delete qvariant_cast<ConnHolder*>(w->property("lrcfgConn")); |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
| 50 | * This check for some supported widgets and bind the widgets and property |
| 51 | */ |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 52 | static void setupWidget(QWidget* w, |
| 53 | Account* a, |
| 54 | const QHash<QByteArray, int>& roles) |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 55 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 56 | if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) { |
| 57 | const QByteArray prop = w->objectName().mid(LRC_CFG_LEN, 999).toLatin1(); |
| 58 | if (roles.contains(prop)) { |
| 59 | const int role = roles[prop]; |
| 60 | auto roleState = a->roleState(static_cast<Account::Role>(role)); |
| 61 | if (roleState == Account::RoleState::READ_WRITE |
| 62 | || roleState == Account::RoleState::READ_ONLY) { |
| 63 | if (qobject_cast<QLineEdit*>(w)) { |
| 64 | QLineEdit* le = qobject_cast<QLineEdit*>(w); |
| 65 | avoidDuplicate(le); |
| 66 | le->setText(a->roleData(role).toString()); |
| 67 | ConnHolder* c = new ConnHolder { |
| 68 | QObject::connect(le, &QLineEdit::textChanged, |
| 69 | [a,role](const QString& text) { |
| 70 | if (a->roleData(role) != text) |
| 71 | a->setRoleData(role, text); |
| 72 | }) |
| 73 | }; |
| 74 | le->setProperty("lrcfgConn",QVariant::fromValue(c)); |
| 75 | } |
| 76 | else if (qobject_cast<QSpinBox*>(w)) { |
| 77 | QSpinBox* sb = qobject_cast<QSpinBox*>(w); |
| 78 | avoidDuplicate(sb); |
| 79 | sb->setValue(a->roleData(role).toInt()); |
| 80 | ConnHolder* c = new ConnHolder { |
| 81 | QObject::connect(sb, static_cast<void (QSpinBox::*) |
| 82 | (int)>(&QSpinBox::valueChanged), |
| 83 | [a,role](int value) { |
| 84 | if (a->roleData(role).toInt() != value) |
| 85 | a->setRoleData(role, value); |
| 86 | }) |
| 87 | }; |
| 88 | sb->setProperty("lrcfgConn",QVariant::fromValue(c)); |
| 89 | } |
| 90 | else if (qobject_cast<QAbstractButton*>(w)) { |
| 91 | //QCheckBox, QRadioButton, QToolButton, QPushButton |
| 92 | QAbstractButton* b = qobject_cast<QAbstractButton*>(w); |
| 93 | avoidDuplicate(b); |
| 94 | b->setChecked(a->roleData(role).toBool()); |
| 95 | ConnHolder* c = new ConnHolder { |
| 96 | QObject::connect(b, &QAbstractButton::toggled, |
| 97 | [a,role](bool c) { |
| 98 | if (a->roleData(role).toBool() != c) |
| 99 | a->setRoleData(role, c); |
| 100 | }) |
| 101 | }; |
| 102 | b->setProperty("lrcfgConn",QVariant::fromValue(c)); |
| 103 | } |
| 104 | w->setVisible(true); |
| 105 | w->setEnabled(roleState == Account::RoleState::READ_WRITE); |
| 106 | } else { |
| 107 | w->setVisible(false); |
| 108 | } |
| 109 | } |
| 110 | else { |
| 111 | qWarning() << "Unknown config properties" << w->objectName(); |
| 112 | } |
| 113 | } |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void clearConnections(QWidget* w) |
| 117 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 118 | if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) { |
| 119 | avoidDuplicate(w); |
| 120 | } |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 123 | static void drill(QWidget* w, Account* a, |
| 124 | const QHash<QByteArray, int>& roles, |
| 125 | bool clear = false) |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 126 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 127 | for (QObject *object : w->children()) { |
| 128 | if (!object->isWidgetType()) |
| 129 | continue; |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 130 | |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 131 | QWidget* w2 = static_cast<QWidget*>(object); |
| 132 | if (clear) |
| 133 | clearConnections(w2); |
| 134 | else |
| 135 | setupWidget(w2, a, roles); |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 136 | |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 137 | drill(w2, a, roles); |
| 138 | } |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 141 | AccountSerializationAdapter::AccountSerializationAdapter(Account* a, |
| 142 | QWidget* w) |
| 143 | : QObject(w) |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 144 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 145 | static QHash<QByteArray, int> reverse; |
| 146 | if (reverse.isEmpty()) { |
| 147 | const QHash<int, QByteArray> a = AccountModel::instance()->roleNames(); |
| 148 | for (QHash<int, QByteArray>::const_iterator i = a.begin(); i != a.end(); ++i) { |
| 149 | reverse[i.value()] = i.key(); |
| 150 | } |
| 151 | } |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 152 | |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 153 | drill(w, a, reverse); |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | AccountSerializationAdapter::~AccountSerializationAdapter() |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 157 | {} |