blob: 5ae61c06c4ecc790354cf097809538d2062f9520 [file] [log] [blame]
Edric Milaret3e6aefe2015-06-05 16:07:26 -04001/***************************************************************************
Edric Milaret5f316da2015-09-28 11:57:42 -04002 * Copyright (C) 2015 by Savoir-faire Linux *
Edric Milaret3e6aefe2015-06-05 16:07:26 -04003 * 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 Milaret3ea594a2015-06-17 15:47:57 -040024#include <QtWidgets/QGroupBox>
25#include <QtWidgets/QFormLayout>
Edric Milaret3e6aefe2015-06-05 16:07:26 -040026#include <QtWidgets/QAbstractButton>
Edric Milaret3ea594a2015-06-17 15:47:57 -040027#include <QtWidgets/QLabel>
Edric Milaret3e6aefe2015-06-05 16:07:26 -040028
29#include <account.h>
30#include <accountmodel.h>
31
32constexpr static const char LRC_CFG[] = "lrcfg_";
33constexpr static const int LRC_CFG_LEN = 6 ;
34
35struct ConnHolder {
Edric Milarete0855a92015-06-12 11:38:24 -040036 QMetaObject::Connection c;
37 ~ConnHolder();
Edric Milaret3e6aefe2015-06-05 16:07:26 -040038};
39Q_DECLARE_METATYPE(ConnHolder*);
40
41ConnHolder::~ConnHolder()
42{
Edric Milarete0855a92015-06-12 11:38:24 -040043 QObject::disconnect(c);
Edric Milaret3e6aefe2015-06-05 16:07:26 -040044}
45
46static void avoidDuplicate(QWidget* w)
47{
Edric Milarete0855a92015-06-12 11:38:24 -040048 if (qvariant_cast<ConnHolder*>(w->property("lrcfgConn")))
49 delete qvariant_cast<ConnHolder*>(w->property("lrcfgConn"));
Edric Milaret3e6aefe2015-06-05 16:07:26 -040050}
51
52/**
53 * This check for some supported widgets and bind the widgets and property
54 */
Edric Milaret3ea594a2015-06-17 15:47:57 -040055static void setupWidget(QWidget* w, Account* a, const QHash<QByteArray, int>& roles)
Edric Milaret3e6aefe2015-06-05 16:07:26 -040056{
Edric Milarete0855a92015-06-12 11:38:24 -040057 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 Milaret3ea594a2015-06-17 15:47:57 -040061 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) {
116
117 w->setProperty("lrcfgVisible", w->isVisible() ? 2 : 1);
Edric Milarete0855a92015-06-12 11:38:24 -0400118 w->setVisible(false);
Edric Milaret3ea594a2015-06-17 15:47:57 -0400119
120 QFormLayout* fm = qobject_cast<QFormLayout*>(w->parentWidget()->layout());
121
122 //There is many of corner case here, this only handle the one that's
123 //created by Qt Designer
124 if (!fm) {
125 QLayoutItem* il = w->parentWidget()->layout()->itemAt(0);
126 if (il && il->layout())
127 fm = qobject_cast<QFormLayout*>(il->layout());
128 }
129
130 if (fm) {
131 QWidget* buddy = fm->labelForField(w);
132 qDebug() << "BUDDY " << buddy;
133 if (buddy)
134 buddy->setVisible(false);
135
136 }
137 }
138 else {
139 //0 = unset, 1=invisible, 2=visible
140 const int oldVisibleState = w->property("lrcfgVisible").toInt();
141 if (oldVisibleState)
142 w->setVisible(oldVisibleState-1);
Edric Milarete0855a92015-06-12 11:38:24 -0400143 }
144 }
145 else {
146 qWarning() << "Unknown config properties" << w->objectName();
147 }
148 }
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400149}
150
151static void clearConnections(QWidget* w)
152{
Edric Milarete0855a92015-06-12 11:38:24 -0400153 if (w->objectName().left(LRC_CFG_LEN) == LRC_CFG) {
154 avoidDuplicate(w);
155 }
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400156}
157
Edric Milaret3ea594a2015-06-17 15:47:57 -0400158static void drill(QWidget* w, Account* a, const QHash<QByteArray, int>& roles, bool clear = false)
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400159{
Edric Milarete0855a92015-06-12 11:38:24 -0400160 for (QObject *object : w->children()) {
161 if (!object->isWidgetType())
162 continue;
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400163
Edric Milarete0855a92015-06-12 11:38:24 -0400164 QWidget* w2 = static_cast<QWidget*>(object);
165 if (clear)
166 clearConnections(w2);
167 else
168 setupWidget(w2, a, roles);
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400169
Edric Milarete0855a92015-06-12 11:38:24 -0400170 drill(w2, a, roles);
171 }
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400172}
173
Edric Milaret3ea594a2015-06-17 15:47:57 -0400174static void hideLabel(QWidget* w) {
175 for (QObject *object : w->children()) {
176 if (!object->isWidgetType())
177 continue;
178 QWidget* w2 = qobject_cast<QWidget*>(object);
179 if (w2) {
180 QLabel* l = qobject_cast<QLabel*>(w2);
181 if (l && l->buddy()) {
182 l->setVisible(!l->buddy()->isHidden());
183 }
184 }
185 hideLabel(w2);
186 }
187}
188
189AccountSerializationAdapter::AccountSerializationAdapter(Account* a, QWidget* w) : QObject(w)
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400190{
Edric Milarete0855a92015-06-12 11:38:24 -0400191 static QHash<QByteArray, int> reverse;
192 if (reverse.isEmpty()) {
Edric Milareta3e47282015-10-23 15:20:30 -0400193 const QHash<int, QByteArray> a = AccountModel::instance().roleNames();
Edric Milarete0855a92015-06-12 11:38:24 -0400194 for (QHash<int, QByteArray>::const_iterator i = a.begin(); i != a.end(); ++i) {
195 reverse[i.value()] = i.key();
196 }
197 }
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400198
Edric Milarete0855a92015-06-12 11:38:24 -0400199 drill(w, a, reverse);
Edric Milaret3ea594a2015-06-17 15:47:57 -0400200 hideLabel(w);
Edric Milaret3e6aefe2015-06-05 16:07:26 -0400201}
202
203AccountSerializationAdapter::~AccountSerializationAdapter()
Edric Milaret3ea594a2015-06-17 15:47:57 -0400204{
205
206}