1 /*
2 * Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 or (at your option)
7 * version 3 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "KeyComponentWidget.h"
19 #include "ui_KeyComponentWidget.h"
20
21 #include <QStackedWidget>
22 #include <QTimer>
23
KeyComponentWidget(QWidget * parent)24 KeyComponentWidget::KeyComponentWidget(QWidget* parent)
25 : KeyComponentWidget({}, parent)
26 {
27 }
28
KeyComponentWidget(const QString & name,QWidget * parent)29 KeyComponentWidget::KeyComponentWidget(const QString& name, QWidget* parent)
30 : QWidget(parent)
31 , m_ui(new Ui::KeyComponentWidget())
32 {
33 m_ui->setupUi(this);
34
35 connect(m_ui->addButton, SIGNAL(clicked(bool)), SIGNAL(componentAddRequested()));
36 connect(m_ui->changeButton, SIGNAL(clicked(bool)), SIGNAL(componentEditRequested()));
37 connect(m_ui->removeButton, SIGNAL(clicked(bool)), SIGNAL(componentRemovalRequested()));
38 connect(m_ui->cancelButton, SIGNAL(clicked(bool)), SLOT(cancelEdit()));
39
40 connect(m_ui->stackedWidget, SIGNAL(currentChanged(int)), SLOT(resetComponentEditWidget()));
41
42 connect(this, SIGNAL(nameChanged(QString)), SLOT(updateComponentName(QString)));
43 connect(this, SIGNAL(descriptionChanged(QString)), SLOT(updateComponentDescription(QString)));
44 connect(this, SIGNAL(componentAddRequested()), SLOT(doAdd()));
45 connect(this, SIGNAL(componentEditRequested()), SLOT(doEdit()));
46 connect(this, SIGNAL(componentRemovalRequested()), SLOT(doRemove()));
47 connect(this, SIGNAL(componentAddChanged(bool)), SLOT(updateAddStatus(bool)));
48
49 bool prev = blockSignals(true);
50 setComponentName(name);
51 blockSignals(prev);
52
53 prev = m_ui->stackedWidget->blockSignals(true);
54 m_ui->stackedWidget->setCurrentIndex(Page::AddNew);
55 m_ui->stackedWidget->blockSignals(prev);
56 }
57
~KeyComponentWidget()58 KeyComponentWidget::~KeyComponentWidget()
59 {
60 }
61
62 /**
63 * @param name display name for the key component
64 */
setComponentName(const QString & name)65 void KeyComponentWidget::setComponentName(const QString& name)
66 {
67 if (name == m_componentName) {
68 return;
69 }
70
71 m_componentName = name;
72 emit nameChanged(name);
73 }
74
75 /**
76 * @return The key component's display name
77 */
componentName() const78 QString KeyComponentWidget::componentName() const
79 {
80 return m_componentName;
81 }
82
setComponentDescription(const QString & description)83 void KeyComponentWidget::setComponentDescription(const QString& description)
84 {
85 if (description == m_componentDescription) {
86 return;
87 }
88
89 m_componentDescription = description;
90 emit descriptionChanged(description);
91 }
92
componentDescription() const93 QString KeyComponentWidget::componentDescription() const
94 {
95 return m_componentDescription;
96 }
97
setComponentAdded(bool added)98 void KeyComponentWidget::setComponentAdded(bool added)
99 {
100 if (m_isComponentAdded == added) {
101 return;
102 }
103
104 m_isComponentAdded = added;
105 emit componentAddChanged(added);
106 }
107
componentAdded() const108 bool KeyComponentWidget::componentAdded() const
109 {
110 return m_isComponentAdded;
111 }
112
changeVisiblePage(KeyComponentWidget::Page page)113 void KeyComponentWidget::changeVisiblePage(KeyComponentWidget::Page page)
114 {
115 m_previousPage = static_cast<Page>(m_ui->stackedWidget->currentIndex());
116 m_ui->stackedWidget->setCurrentIndex(page);
117 }
118
visiblePage() const119 KeyComponentWidget::Page KeyComponentWidget::visiblePage() const
120 {
121 return static_cast<Page>(m_ui->stackedWidget->currentIndex());
122 }
123
updateComponentName(const QString & name)124 void KeyComponentWidget::updateComponentName(const QString& name)
125 {
126 m_ui->groupBox->setTitle(name);
127 m_ui->addButton->setText(tr("Add %1", "Add a key component").arg(name));
128 m_ui->changeButton->setText(tr("Change %1", "Change a key component").arg(name));
129 m_ui->removeButton->setText(tr("Remove %1", "Remove a key component").arg(name));
130 m_ui->changeOrRemoveLabel->setText(
131 tr("%1 set, click to change or remove", "Change or remove a key component").arg(name));
132 }
133
updateComponentDescription(const QString & description)134 void KeyComponentWidget::updateComponentDescription(const QString& description)
135 {
136 m_ui->componentDescription->setText(description);
137 }
138
updateAddStatus(bool added)139 void KeyComponentWidget::updateAddStatus(bool added)
140 {
141 if (added) {
142 m_ui->stackedWidget->setCurrentIndex(Page::LeaveOrRemove);
143 } else {
144 m_ui->stackedWidget->setCurrentIndex(Page::AddNew);
145 }
146 }
147
doAdd()148 void KeyComponentWidget::doAdd()
149 {
150 changeVisiblePage(Page::Edit);
151 }
152
doEdit()153 void KeyComponentWidget::doEdit()
154 {
155 changeVisiblePage(Page::Edit);
156 }
157
doRemove()158 void KeyComponentWidget::doRemove()
159 {
160 changeVisiblePage(Page::AddNew);
161 }
162
cancelEdit()163 void KeyComponentWidget::cancelEdit()
164 {
165 m_ui->stackedWidget->setCurrentIndex(m_previousPage);
166 emit editCanceled();
167 }
168
showEvent(QShowEvent * event)169 void KeyComponentWidget::showEvent(QShowEvent* event)
170 {
171 resetComponentEditWidget();
172 QWidget::showEvent(event);
173 }
174
resetComponentEditWidget()175 void KeyComponentWidget::resetComponentEditWidget()
176 {
177 if (!m_componentWidget || static_cast<Page>(m_ui->stackedWidget->currentIndex()) == Page::Edit) {
178 if (m_componentWidget) {
179 delete m_componentWidget;
180 }
181
182 m_componentWidget = componentEditWidget();
183 m_ui->componentWidgetLayout->addWidget(m_componentWidget);
184 initComponentEditWidget(m_componentWidget);
185 }
186
187 QTimer::singleShot(0, this, SLOT(updateSize()));
188 }
189
updateSize()190 void KeyComponentWidget::updateSize()
191 {
192 for (int i = 0; i < m_ui->stackedWidget->count(); ++i) {
193 if (m_ui->stackedWidget->currentIndex() == i) {
194 m_ui->stackedWidget->widget(i)->setSizePolicy(
195 m_ui->stackedWidget->widget(i)->sizePolicy().horizontalPolicy(), QSizePolicy::Preferred);
196 } else {
197 m_ui->stackedWidget->widget(i)->setSizePolicy(
198 m_ui->stackedWidget->widget(i)->sizePolicy().horizontalPolicy(), QSizePolicy::Ignored);
199 }
200 }
201 }
202