Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Anthony LĂ©onard | 2fde81d | 2017-04-17 10:06:55 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015-2017 by Savoir-faire Linux * |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 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> |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 24 | #include <QtWidgets/QGroupBox> |
| 25 | #include <QtWidgets/QFormLayout> |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 26 | #include <QtWidgets/QAbstractButton> |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 27 | #include <QtWidgets/QLabel> |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 28 | |
| 29 | #include <account.h> |
| 30 | #include <accountmodel.h> |
| 31 | |
| 32 | constexpr static const char LRC_CFG[] = "lrcfg_"; |
| 33 | constexpr static const int LRC_CFG_LEN = 6 ; |
| 34 | |
| 35 | struct ConnHolder { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 36 | QMetaObject::Connection c; |
| 37 | ~ConnHolder(); |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 38 | }; |
| 39 | Q_DECLARE_METATYPE(ConnHolder*); |
| 40 | |
| 41 | ConnHolder::~ConnHolder() |
| 42 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 43 | QObject::disconnect(c); |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | static void avoidDuplicate(QWidget* w) |
| 47 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 48 | if (qvariant_cast<ConnHolder*>(w->property("lrcfgConn"))) |
| 49 | delete qvariant_cast<ConnHolder*>(w->property("lrcfgConn")); |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
| 53 | * This check for some supported widgets and bind the widgets and property |
| 54 | */ |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 55 | static void setupWidget(QWidget* w, Account* a, const QHash<QByteArray, int>& roles) |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 56 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 57 | if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) { |
| 58 | const QByteArray prop = w->objectName().mid(LRC_CFG_LEN, 999).toLatin1(); |
| 59 | if (roles.contains(prop)) { |
| 60 | const int role = roles[prop]; |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 61 | if (qobject_cast<QLineEdit*>(w)) { |
| 62 | QLineEdit* le = qobject_cast<QLineEdit*>(w); |
| 63 | avoidDuplicate(le); |
| 64 | le->setText(a->roleData(role).toString()); |
| 65 | ConnHolder* c = new ConnHolder { |
| 66 | QObject::connect(le, &QLineEdit::textChanged, [a,role](const QString& text) { |
| 67 | if (a->roleData(role) != text) |
| 68 | a->setRoleData(role, text); |
| 69 | }) |
| 70 | }; |
| 71 | le->setProperty("lrcfgConn",QVariant::fromValue(c)); |
| 72 | } |
| 73 | else if (qobject_cast<QSpinBox*>(w)) { |
| 74 | QSpinBox* sb = qobject_cast<QSpinBox*>(w); |
| 75 | avoidDuplicate(sb); |
| 76 | sb->setValue(a->roleData(role).toInt()); |
| 77 | ConnHolder* c = new ConnHolder { |
| 78 | QObject::connect(sb, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [a,role](int value) { |
| 79 | if (a->roleData(role).toInt() != value) |
| 80 | a->setRoleData(role, value); |
| 81 | }) |
| 82 | }; |
| 83 | sb->setProperty("lrcfgConn",QVariant::fromValue(c)); |
| 84 | } |
| 85 | else if (qobject_cast<QAbstractButton*>(w)) { |
| 86 | //QCheckBox, QRadioButton, QToolButton, QPushButton |
| 87 | QAbstractButton* b = qobject_cast<QAbstractButton*>(w); |
| 88 | avoidDuplicate(b); |
| 89 | b->setChecked(a->roleData(role).toBool()); |
| 90 | ConnHolder* c = new ConnHolder { |
| 91 | QObject::connect(b, &QAbstractButton::toggled,[a,role](bool c) { |
| 92 | if (a->roleData(role).toBool() != c) |
| 93 | a->setRoleData(role, c); |
| 94 | }) |
| 95 | }; |
| 96 | b->setProperty("lrcfgConn",QVariant::fromValue(c)); |
| 97 | } |
| 98 | else if (qobject_cast<QGroupBox*>(w)) { |
| 99 | QGroupBox* b = qobject_cast<QGroupBox*>(w); |
| 100 | avoidDuplicate(b); |
| 101 | b->setChecked(a->roleData(role).toBool()); |
| 102 | ConnHolder* c = new ConnHolder { |
| 103 | QObject::connect(b, &QGroupBox::toggled,[a,role](bool c) { |
| 104 | if (a->roleData(role).toBool() != c) |
| 105 | a->setRoleData(role, c); |
| 106 | }) |
| 107 | }; |
| 108 | b->setProperty("lrcfgConn",QVariant::fromValue(c)); |
| 109 | } |
| 110 | else { |
| 111 | qDebug() << "Unsupported widget type" << w; |
| 112 | } |
| 113 | |
| 114 | //Check if the field is required for this account type |
| 115 | if (a->roleState((Account::Role)role) == Account::RoleState::UNAVAILABLE) { |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 116 | w->setProperty("lrcfgVisible", w->isVisible() ? 2 : 1); |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 117 | w->setVisible(false); |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 118 | |
| 119 | QFormLayout* fm = qobject_cast<QFormLayout*>(w->parentWidget()->layout()); |
| 120 | |
| 121 | //There is many of corner case here, this only handle the one that's |
| 122 | //created by Qt Designer |
| 123 | if (!fm) { |
| 124 | QLayoutItem* il = w->parentWidget()->layout()->itemAt(0); |
| 125 | if (il && il->layout()) |
| 126 | fm = qobject_cast<QFormLayout*>(il->layout()); |
| 127 | } |
| 128 | |
| 129 | if (fm) { |
| 130 | QWidget* buddy = fm->labelForField(w); |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 131 | if (buddy) |
| 132 | buddy->setVisible(false); |
| 133 | |
| 134 | } |
| 135 | } |
| 136 | else { |
| 137 | //0 = unset, 1=invisible, 2=visible |
Edric Milaret | 9d877ab | 2016-04-11 11:15:56 -0400 | [diff] [blame] | 138 | w->setProperty("lrcfgVisible", 2); |
| 139 | w->setVisible(true); |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | else { |
| 143 | qWarning() << "Unknown config properties" << w->objectName(); |
| 144 | } |
| 145 | } |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static void clearConnections(QWidget* w) |
| 149 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 150 | if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) { |
| 151 | avoidDuplicate(w); |
| 152 | } |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 155 | static void drill(QWidget* w, Account* a, const QHash<QByteArray, int>& roles, bool clear = false) |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 156 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 157 | for (QObject *object : w->children()) { |
| 158 | if (!object->isWidgetType()) |
| 159 | continue; |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 160 | |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 161 | QWidget* w2 = static_cast<QWidget*>(object); |
| 162 | if (clear) |
| 163 | clearConnections(w2); |
| 164 | else |
| 165 | setupWidget(w2, a, roles); |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 166 | |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 167 | drill(w2, a, roles); |
| 168 | } |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 171 | static void hideLabel(QWidget* w) { |
| 172 | for (QObject *object : w->children()) { |
| 173 | if (!object->isWidgetType()) |
| 174 | continue; |
| 175 | QWidget* w2 = qobject_cast<QWidget*>(object); |
| 176 | if (w2) { |
| 177 | QLabel* l = qobject_cast<QLabel*>(w2); |
| 178 | if (l && l->buddy()) { |
| 179 | l->setVisible(!l->buddy()->isHidden()); |
| 180 | } |
| 181 | } |
| 182 | hideLabel(w2); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | AccountSerializationAdapter::AccountSerializationAdapter(Account* a, QWidget* w) : QObject(w) |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 187 | { |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 188 | static QHash<QByteArray, int> reverse; |
| 189 | if (reverse.isEmpty()) { |
Edric Milaret | a3e4728 | 2015-10-23 15:20:30 -0400 | [diff] [blame] | 190 | const QHash<int, QByteArray> a = AccountModel::instance().roleNames(); |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 191 | for (QHash<int, QByteArray>::const_iterator i = a.begin(); i != a.end(); ++i) { |
| 192 | reverse[i.value()] = i.key(); |
| 193 | } |
| 194 | } |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 195 | |
Edric Milaret | e0855a9 | 2015-06-12 11:38:24 -0400 | [diff] [blame] | 196 | drill(w, a, reverse); |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 197 | hideLabel(w); |
Edric Milaret | 3e6aefe | 2015-06-05 16:07:26 -0400 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | AccountSerializationAdapter::~AccountSerializationAdapter() |
Edric Milaret | 3ea594a | 2015-06-17 15:47:57 -0400 | [diff] [blame] | 201 | { |
| 202 | |
| 203 | } |