1 /*
2 SPDX-FileCopyrightText: 2013-2021 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "configureimmutablewidgetutils.h"
8 #include "simplestringlisteditor.h"
9
10 #include <KLocalizedString>
11 #include <KUrlRequester>
12
13 #include <QButtonGroup>
14 #include <QCheckBox>
15 #include <QComboBox>
16 #include <QGroupBox>
17 #include <QHBoxLayout>
18 #include <QLineEdit>
19 #include <QRadioButton>
20 #include <QSpinBox>
21 #include <QVBoxLayout>
22 #include <QWidget>
23
24 using namespace PimCommon;
25
26 static const char lockedDownWarning[] = I18N_NOOP(
27 "<qt><p>This setting has been fixed by your administrator.</p>"
28 "<p>If you think this is an error, please contact him.</p></qt>");
29
checkLockDown(QWidget * w,const KConfigSkeletonItem * item)30 void ConfigureImmutableWidgetUtils::checkLockDown(QWidget *w, const KConfigSkeletonItem *item)
31 {
32 if (item->isImmutable()) {
33 w->setEnabled(false);
34 w->setToolTip(i18n(lockedDownWarning));
35 }
36 }
37
populateButtonGroup(QGroupBox * box,QButtonGroup * group,int orientation,const KCoreConfigSkeleton::ItemEnum * e)38 void ConfigureImmutableWidgetUtils::populateButtonGroup(QGroupBox *box, QButtonGroup *group, int orientation, const KCoreConfigSkeleton::ItemEnum *e)
39 {
40 box->setTitle(e->label());
41 if (orientation == Qt::Horizontal) {
42 box->setLayout(new QHBoxLayout());
43 } else {
44 box->setLayout(new QVBoxLayout());
45 }
46 const int numberChoices(e->choices().size());
47 for (int i = 0; i < numberChoices; ++i) {
48 auto button = new QRadioButton(e->choices().at(i).label, box);
49 group->addButton(button, i);
50 box->layout()->addWidget(button);
51 }
52 }
53
populateCheckBox(QCheckBox * b,const KCoreConfigSkeleton::ItemBool * e)54 void ConfigureImmutableWidgetUtils::populateCheckBox(QCheckBox *b, const KCoreConfigSkeleton::ItemBool *e)
55 {
56 b->setText(e->label());
57 }
58
loadWidget(QLineEdit * b,const KCoreConfigSkeleton::ItemString * e)59 void ConfigureImmutableWidgetUtils::loadWidget(QLineEdit *b, const KCoreConfigSkeleton::ItemString *e)
60 {
61 checkLockDown(b, e);
62 b->setText(e->value());
63 }
64
loadWidget(QCheckBox * b,const KCoreConfigSkeleton::ItemBool * e)65 void ConfigureImmutableWidgetUtils::loadWidget(QCheckBox *b, const KCoreConfigSkeleton::ItemBool *e)
66 {
67 checkLockDown(b, e);
68 b->setChecked(e->value());
69 }
70
loadWidget(PimCommon::SimpleStringListEditor * b,const KCoreConfigSkeleton::ItemStringList * e)71 void ConfigureImmutableWidgetUtils::loadWidget(PimCommon::SimpleStringListEditor *b, const KCoreConfigSkeleton::ItemStringList *e)
72 {
73 checkLockDown(b, e);
74 b->setStringList(e->value());
75 }
76
loadWidget(QGroupBox * box,QButtonGroup * group,const KCoreConfigSkeleton::ItemEnum * e)77 void ConfigureImmutableWidgetUtils::loadWidget(QGroupBox *box, QButtonGroup *group, const KCoreConfigSkeleton::ItemEnum *e)
78 {
79 Q_ASSERT(group->buttons().size() == e->choices().size());
80 checkLockDown(box, e);
81 group->buttons().at(e->value())->setChecked(true);
82 }
83
loadWidget(QSpinBox * b,const KCoreConfigSkeleton::ItemInt * e)84 void ConfigureImmutableWidgetUtils::loadWidget(QSpinBox *b, const KCoreConfigSkeleton::ItemInt *e)
85 {
86 checkLockDown(b, e);
87 b->setValue(e->value());
88 }
89
saveSpinBox(QSpinBox * b,KCoreConfigSkeleton::ItemInt * e)90 void ConfigureImmutableWidgetUtils::saveSpinBox(QSpinBox *b, KCoreConfigSkeleton::ItemInt *e)
91 {
92 e->setValue(b->value());
93 }
94
saveSpinBox(QSpinBox * b,KCoreConfigSkeleton::ItemUInt * e)95 void ConfigureImmutableWidgetUtils::saveSpinBox(QSpinBox *b, KCoreConfigSkeleton::ItemUInt *e)
96 {
97 e->setValue(b->value());
98 }
99
loadWidget(QSpinBox * b,const KCoreConfigSkeleton::ItemUInt * e)100 void ConfigureImmutableWidgetUtils::loadWidget(QSpinBox *b, const KCoreConfigSkeleton::ItemUInt *e)
101 {
102 checkLockDown(b, e);
103 b->setValue(e->value());
104 }
105
saveCheckBox(QCheckBox * b,KCoreConfigSkeleton::ItemBool * e)106 void ConfigureImmutableWidgetUtils::saveCheckBox(QCheckBox *b, KCoreConfigSkeleton::ItemBool *e)
107 {
108 e->setValue(b->isChecked());
109 }
110
saveLineEdit(QLineEdit * b,KCoreConfigSkeleton::ItemString * e)111 void ConfigureImmutableWidgetUtils::saveLineEdit(QLineEdit *b, KCoreConfigSkeleton::ItemString *e)
112 {
113 e->setValue(b->text());
114 }
115
saveUrlRequester(KUrlRequester * b,KCoreConfigSkeleton::ItemString * e)116 void ConfigureImmutableWidgetUtils::saveUrlRequester(KUrlRequester *b, KCoreConfigSkeleton::ItemString *e)
117 {
118 e->setValue(b->text());
119 }
120
saveGroupBox(QGroupBox * b,KCoreConfigSkeleton::ItemBool * e)121 void ConfigureImmutableWidgetUtils::saveGroupBox(QGroupBox *b, KCoreConfigSkeleton::ItemBool *e)
122 {
123 if (b->isCheckable()) {
124 e->setValue(b->isChecked());
125 }
126 }
127
loadWidget(KUrlRequester * b,const KCoreConfigSkeleton::ItemString * e)128 void ConfigureImmutableWidgetUtils::loadWidget(KUrlRequester *b, const KCoreConfigSkeleton::ItemString *e)
129 {
130 checkLockDown(b, e);
131 b->setText(e->value());
132 }
133
saveButtonGroup(QButtonGroup * group,KCoreConfigSkeleton::ItemEnum * e)134 void ConfigureImmutableWidgetUtils::saveButtonGroup(QButtonGroup *group, KCoreConfigSkeleton::ItemEnum *e)
135 {
136 Q_ASSERT(group->buttons().size() == e->choices().size());
137 if (group->checkedId() != -1) {
138 e->setValue(group->checkedId());
139 }
140 }
141
saveSimpleStringListEditor(PimCommon::SimpleStringListEditor * b,KCoreConfigSkeleton::ItemStringList * e)142 void ConfigureImmutableWidgetUtils::saveSimpleStringListEditor(PimCommon::SimpleStringListEditor *b, KCoreConfigSkeleton::ItemStringList *e)
143 {
144 e->setValue(b->stringList());
145 }
146
loadWidget(QGroupBox * b,const KCoreConfigSkeleton::ItemBool * e)147 void ConfigureImmutableWidgetUtils::loadWidget(QGroupBox *b, const KCoreConfigSkeleton::ItemBool *e)
148 {
149 checkLockDown(b, e);
150 if (b->isCheckable()) {
151 b->setChecked(e->value());
152 }
153 }
154
loadWidget(QComboBox * b,const KCoreConfigSkeleton::ItemEnum * e)155 void ConfigureImmutableWidgetUtils::loadWidget(QComboBox *b, const KCoreConfigSkeleton::ItemEnum *e)
156 {
157 checkLockDown(b, e);
158 b->setCurrentIndex(e->value());
159 }
160
saveComboBox(QComboBox * b,KCoreConfigSkeleton::ItemEnum * e)161 void ConfigureImmutableWidgetUtils::saveComboBox(QComboBox *b, KCoreConfigSkeleton::ItemEnum *e)
162 {
163 Q_ASSERT(b->count() == e->choices().size());
164 if (b->currentIndex() != -1) {
165 e->setValue(b->currentIndex());
166 }
167 }
168