2
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
3
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
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.
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.
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/>.
19
#include "EditWidget.h"
20
#include "ui_EditWidget.h"
25
EditWidget::EditWidget(QWidget* parent)
26
: DialogyWidget(parent)
27
, m_ui(new Ui::EditWidget())
33
m_ui->messageWidget->setHidden(true);
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);
41
connect(m_ui->categoryList, SIGNAL(categoryChanged(int)), m_ui->stackedWidget, SLOT(setCurrentIndex(int)));
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*)));
48
EditWidget::~EditWidget() = default;
50
void EditWidget::addPage(const QString& labelText, const QIcon& icon, QWidget* widget)
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
58
if (widget->inherits("QScrollArea")) {
59
m_ui->stackedWidget->addWidget(widget);
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);
70
m_ui->categoryList->addCategory(labelText, icon);
73
bool EditWidget::hasPage(QWidget* widget)
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) {
85
void EditWidget::setPageHidden(QWidget* widget, bool hidden)
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) {
101
bool changed = m_ui->categoryList->isCategoryHidden(index) != hidden;
102
m_ui->categoryList->setCategoryHidden(index, hidden);
104
if (changed && index == m_ui->stackedWidget->currentIndex()) {
105
int newIndex = m_ui->stackedWidget->currentIndex() - 1;
107
newIndex = m_ui->stackedWidget->count() - 1;
109
m_ui->categoryList->setCurrentCategory(newIndex);
113
void EditWidget::setCurrentPage(int index)
115
m_ui->categoryList->setCurrentCategory(index);
116
m_ui->stackedWidget->setCurrentIndex(index);
119
void EditWidget::setHeadline(const QString& text)
121
m_ui->headerLabel->setText(text);
124
QLabel* EditWidget::headlineLabel()
126
return m_ui->headerLabel;
129
void EditWidget::setReadOnly(bool readOnly)
131
m_readOnly = readOnly;
134
m_ui->buttonBox->setStandardButtons(QDialogButtonBox::Close);
136
m_ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply);
140
bool EditWidget::readOnly() const
145
void EditWidget::setModified(bool state)
148
enableApplyButton(state);
151
bool EditWidget::isModified() const
156
void EditWidget::enableApplyButton(bool enabled)
158
QPushButton* applyButton = m_ui->buttonBox->button(QDialogButtonBox::Apply);
160
applyButton->setEnabled(enabled);
164
void EditWidget::showApplyButton(bool state)
167
auto buttons = m_ui->buttonBox->standardButtons();
169
buttons |= QDialogButtonBox::Apply;
171
buttons &= ~QDialogButtonBox::Apply;
173
m_ui->buttonBox->setStandardButtons(buttons);
177
void EditWidget::buttonClicked(QAbstractButton* button)
179
auto stdButton = m_ui->buttonBox->standardButton(button);
180
if (stdButton == QDialogButtonBox::Apply) {
185
void EditWidget::showMessage(const QString& text, MessageWidget::MessageType type)
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);
192
m_ui->messageWidget->setCloseButtonVisible(false);
193
m_ui->messageWidget->showMessage(text, type, 2000);
197
void EditWidget::hideMessage()
199
if (m_ui->messageWidget->isVisible()) {
200
m_ui->messageWidget->animatedHide();