Welcome to mirror list, hosted at ThFree Co, Russian Federation.

KeyComponentWidget.cpp « masterkey « gui « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7d795aca1aee629ff6167e2151211ca487848db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 *  Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "KeyComponentWidget.h"
#include "ui_KeyComponentWidget.h"

#include <QStackedWidget>
#include <QTimer>

KeyComponentWidget::KeyComponentWidget(QWidget* parent)
    : KeyComponentWidget({}, parent)
{
}

KeyComponentWidget::KeyComponentWidget(const QString& name, QWidget* parent)
    : QWidget(parent)
    , m_ui(new Ui::KeyComponentWidget())
{
    m_ui->setupUi(this);

    connect(m_ui->addButton, SIGNAL(clicked(bool)), SIGNAL(componentAddRequested()));
    connect(m_ui->changeButton, SIGNAL(clicked(bool)), SIGNAL(componentEditRequested()));
    connect(m_ui->removeButton, SIGNAL(clicked(bool)), SIGNAL(componentRemovalRequested()));
    connect(m_ui->cancelButton, SIGNAL(clicked(bool)), SLOT(cancelEdit()));

    connect(m_ui->stackedWidget, SIGNAL(currentChanged(int)), SLOT(reset()));

    connect(this, SIGNAL(nameChanged(QString)), SLOT(updateComponentName(QString)));
    connect(this, SIGNAL(descriptionChanged(QString)), SLOT(updateComponentDescription(QString)));
    connect(this, SIGNAL(componentAddRequested()), SLOT(doAdd()));
    connect(this, SIGNAL(componentEditRequested()), SLOT(doEdit()));
    connect(this, SIGNAL(componentRemovalRequested()), SLOT(doRemove()));
    connect(this, SIGNAL(componentAddChanged(bool)), SLOT(updateAddStatus(bool)));

    blockSignals(true);
    setComponentName(name);
    m_ui->stackedWidget->setCurrentIndex(Page::AddNew);
    updateSize();
    blockSignals(false);
}

KeyComponentWidget::~KeyComponentWidget()
{
}

/**
 * @param name display name for the key component
 */
void KeyComponentWidget::setComponentName(const QString& name)
{
    if (name == m_componentName) {
        return;
    }

    m_componentName = name;
    emit nameChanged(name);
}

/**
 * @return The key component's display name
 */
QString KeyComponentWidget::componentName() const
{
    return m_componentName;
}

void KeyComponentWidget::setComponentDescription(const QString& description)
{
    if (description == m_componentDescription) {
        return;
    }

    m_componentDescription = description;
    emit descriptionChanged(description);
}

QString KeyComponentWidget::componentDescription() const
{
    return m_componentDescription;
}

void KeyComponentWidget::setComponentAdded(bool added)
{
    if (m_isComponentAdded == added) {
        return;
    }

    m_isComponentAdded = added;
    emit componentAddChanged(added);
}

bool KeyComponentWidget::componentAdded() const
{
    return m_isComponentAdded;
}

void KeyComponentWidget::changeVisiblePage(KeyComponentWidget::Page page)
{
    m_previousPage = static_cast<Page>(m_ui->stackedWidget->currentIndex());
    m_ui->stackedWidget->setCurrentIndex(page);
}

KeyComponentWidget::Page KeyComponentWidget::visiblePage() const
{
    return static_cast<Page>(m_ui->stackedWidget->currentIndex());
}

void KeyComponentWidget::updateComponentName(const QString& name)
{
    m_ui->groupBox->setTitle(name);
    m_ui->addButton->setText(tr("Add %1", "Add a key component").arg(name));
    m_ui->changeButton->setText(tr("Change %1", "Change a key component").arg(name));
    m_ui->removeButton->setText(tr("Remove %1", "Remove a key component").arg(name));
    m_ui->changeOrRemoveLabel->setText(
        tr("%1 set, click to change or remove", "Change or remove a key component").arg(name));
}

void KeyComponentWidget::updateComponentDescription(const QString& description)
{
    m_ui->componentDescription->setText(description);
}

void KeyComponentWidget::updateAddStatus(bool added)
{
    if (added) {
        m_ui->stackedWidget->setCurrentIndex(Page::LeaveOrRemove);
    } else {
        m_ui->stackedWidget->setCurrentIndex(Page::AddNew);
    }
}

void KeyComponentWidget::doAdd()
{
    changeVisiblePage(Page::Edit);
}

void KeyComponentWidget::doEdit()
{
    changeVisiblePage(Page::Edit);
}

void KeyComponentWidget::doRemove()
{
    changeVisiblePage(Page::AddNew);
}

void KeyComponentWidget::cancelEdit()
{
    m_ui->stackedWidget->setCurrentIndex(m_previousPage);
    emit editCanceled();
}

void KeyComponentWidget::reset()
{
    if (static_cast<Page>(m_ui->stackedWidget->currentIndex()) == Page::Edit) {
        if (!m_ui->componentWidgetLayout->isEmpty()) {
            auto* item = m_ui->componentWidgetLayout->takeAt(0);
            if (item->widget()) {
                delete item->widget();
            }
            delete item;
        }

        QWidget* widget = componentEditWidget();
        m_ui->componentWidgetLayout->addWidget(widget);

        initComponentEditWidget(widget);
    }

    QTimer::singleShot(0, this, SLOT(updateSize()));
}

void KeyComponentWidget::updateSize()
{
    for (int i = 0; i < m_ui->stackedWidget->count(); ++i) {
        if (m_ui->stackedWidget->currentIndex() == i) {
            m_ui->stackedWidget->widget(i)->setSizePolicy(
                m_ui->stackedWidget->widget(i)->sizePolicy().horizontalPolicy(), QSizePolicy::Preferred);
        } else {
            m_ui->stackedWidget->widget(i)->setSizePolicy(
                m_ui->stackedWidget->widget(i)->sizePolicy().horizontalPolicy(), QSizePolicy::Ignored);
        }
    }
}