keepassxc

Форк
0
/
EditWidget.cpp 
202 строки · 6.0 Кб
1
/*
2
 *  Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
3
 *  Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
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 2 or (at your option)
8
 *  version 3 of the License.
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

19
#include "EditWidget.h"
20
#include "ui_EditWidget.h"
21

22
#include <QPushButton>
23
#include <QScrollArea>
24

25
EditWidget::EditWidget(QWidget* parent)
26
    : DialogyWidget(parent)
27
    , m_ui(new Ui::EditWidget())
28
{
29
    m_ui->setupUi(this);
30
    setReadOnly(false);
31
    setModified(false);
32

33
    m_ui->messageWidget->setHidden(true);
34

35
    QFont headerLabelFont = m_ui->headerLabel->font();
36
    headerLabelFont.setBold(true);
37
    headerLabelFont.setPointSize(headerLabelFont.pointSize() + 2);
38
    headlineLabel()->setFont(headerLabelFont);
39
    headlineLabel()->setTextFormat(Qt::PlainText);
40

41
    connect(m_ui->categoryList, SIGNAL(categoryChanged(int)), m_ui->stackedWidget, SLOT(setCurrentIndex(int)));
42

43
    connect(m_ui->buttonBox, SIGNAL(accepted()), SIGNAL(accepted()));
44
    connect(m_ui->buttonBox, SIGNAL(rejected()), SIGNAL(rejected()));
45
    connect(m_ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(buttonClicked(QAbstractButton*)));
46
}
47

48
EditWidget::~EditWidget() = default;
49

50
void EditWidget::addPage(const QString& labelText, const QIcon& icon, QWidget* widget)
51
{
52
    /*
53
     * Instead of just adding a widget we're going to wrap it into a scroll area. It will automatically show
54
     * scrollbars when the widget cannot fit into the page. This approach prevents the main window of the application
55
     * from automatic resizing and it now should be able to fit into a user's monitor even if the monitor is only 768
56
     * pixels high.
57
     */
58
    if (widget->inherits("QScrollArea")) {
59
        m_ui->stackedWidget->addWidget(widget);
60
    } else {
61
        auto* scrollArea = new QScrollArea(m_ui->stackedWidget);
62
        scrollArea->setFrameShape(QFrame::NoFrame);
63
        scrollArea->setFrameShadow(QFrame::Plain);
64
        scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
65
        scrollArea->setSizeAdjustPolicy(QScrollArea::AdjustToContents);
66
        scrollArea->setWidgetResizable(true);
67
        scrollArea->setWidget(widget);
68
        m_ui->stackedWidget->addWidget(scrollArea);
69
    }
70
    m_ui->categoryList->addCategory(labelText, icon);
71
}
72

73
bool EditWidget::hasPage(QWidget* widget)
74
{
75
    for (int i = 0; i < m_ui->stackedWidget->count(); i++) {
76
        auto* scrollArea = qobject_cast<QScrollArea*>(m_ui->stackedWidget->widget(i));
77
        if (scrollArea && scrollArea->widget() == widget) {
78
            return true;
79
        }
80
    }
81

82
    return false;
83
}
84

85
void EditWidget::setPageHidden(QWidget* widget, bool hidden)
86
{
87
    int index = -1;
88

89
    for (int i = 0; i < m_ui->stackedWidget->count(); i++) {
90
        auto* scrollArea = qobject_cast<QScrollArea*>(m_ui->stackedWidget->widget(i));
91
        if (scrollArea && scrollArea->widget() == widget) {
92
            index = i;
93
            break;
94
        }
95
    }
96

97
    if (index == -1) {
98
        return;
99
    }
100

101
    bool changed = m_ui->categoryList->isCategoryHidden(index) != hidden;
102
    m_ui->categoryList->setCategoryHidden(index, hidden);
103

104
    if (changed && index == m_ui->stackedWidget->currentIndex()) {
105
        int newIndex = m_ui->stackedWidget->currentIndex() - 1;
106
        if (newIndex < 0) {
107
            newIndex = m_ui->stackedWidget->count() - 1;
108
        }
109
        m_ui->categoryList->setCurrentCategory(newIndex);
110
    }
111
}
112

113
void EditWidget::setCurrentPage(int index)
114
{
115
    m_ui->categoryList->setCurrentCategory(index);
116
    m_ui->stackedWidget->setCurrentIndex(index);
117
}
118

119
void EditWidget::setHeadline(const QString& text)
120
{
121
    m_ui->headerLabel->setText(text);
122
}
123

124
QLabel* EditWidget::headlineLabel()
125
{
126
    return m_ui->headerLabel;
127
}
128

129
void EditWidget::setReadOnly(bool readOnly)
130
{
131
    m_readOnly = readOnly;
132

133
    if (readOnly) {
134
        m_ui->buttonBox->setStandardButtons(QDialogButtonBox::Close);
135
    } else {
136
        m_ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply);
137
    }
138
}
139

140
bool EditWidget::readOnly() const
141
{
142
    return m_readOnly;
143
}
144

145
void EditWidget::setModified(bool state)
146
{
147
    m_modified = state;
148
    enableApplyButton(state);
149
}
150

151
bool EditWidget::isModified() const
152
{
153
    return m_modified;
154
}
155

156
void EditWidget::enableApplyButton(bool enabled)
157
{
158
    QPushButton* applyButton = m_ui->buttonBox->button(QDialogButtonBox::Apply);
159
    if (applyButton) {
160
        applyButton->setEnabled(enabled);
161
    }
162
}
163

164
void EditWidget::showApplyButton(bool state)
165
{
166
    if (!m_readOnly) {
167
        auto buttons = m_ui->buttonBox->standardButtons();
168
        if (state) {
169
            buttons |= QDialogButtonBox::Apply;
170
        } else {
171
            buttons &= ~QDialogButtonBox::Apply;
172
        }
173
        m_ui->buttonBox->setStandardButtons(buttons);
174
    }
175
}
176

177
void EditWidget::buttonClicked(QAbstractButton* button)
178
{
179
    auto stdButton = m_ui->buttonBox->standardButton(button);
180
    if (stdButton == QDialogButtonBox::Apply) {
181
        emit apply();
182
    }
183
}
184

185
void EditWidget::showMessage(const QString& text, MessageWidget::MessageType type)
186
{
187
    // Show error messages for a longer time to make sure the user can read them
188
    if (type == MessageWidget::Error) {
189
        m_ui->messageWidget->setCloseButtonVisible(true);
190
        m_ui->messageWidget->showMessage(text, type, 15000);
191
    } else {
192
        m_ui->messageWidget->setCloseButtonVisible(false);
193
        m_ui->messageWidget->showMessage(text, type, 2000);
194
    }
195
}
196

197
void EditWidget::hideMessage()
198
{
199
    if (m_ui->messageWidget->isVisible()) {
200
        m_ui->messageWidget->animatedHide();
201
    }
202
}
203

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.