Skip to content

Commit

Permalink
Correct issues with apply button
Browse files Browse the repository at this point in the history
* Don't show apply button when creating new entries or groups (Fix #2191)
* Don't mark entry/group as dirty when first creating a new one (prevents unnecessary discard dialog on cancel)
* Properly enable/disable apply button when changes are made to entries and groups
* Don't show discard change warning when locking database unless their are actual changes made

NOTE: Extra pages in the group edit widget are not watched for changes yet. Requires a major refactor.
  • Loading branch information
droidmonkey committed Apr 7, 2019
1 parent 84c037e commit 3839449
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 89 deletions.
11 changes: 5 additions & 6 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,11 @@ bool DatabaseWidget::isSearchActive() const
bool DatabaseWidget::isEditWidgetModified() const
{
if (currentWidget() == m_editEntryWidget) {
return m_editEntryWidget->hasBeenModified();
} else {
// other edit widget don't have a hasBeenModified() method yet
// assume that they already have been modified
return true;
return m_editEntryWidget->isModified();
} else if (currentWidget() == m_editGroupWidget) {
return m_editGroupWidget->isModified();
}
return false;
}

QList<int> DatabaseWidget::mainSplitterSizes() const
Expand Down Expand Up @@ -1247,7 +1246,7 @@ bool DatabaseWidget::lock()

clipboard()->clearCopiedText();

if (currentMode() == DatabaseWidget::Mode::EditMode) {
if (isEditWidgetModified()) {
auto result = MessageBox::question(this,
tr("Lock Database?"),
tr("You are editing an entry. Discard changes and lock anyway?"),
Expand Down
37 changes: 34 additions & 3 deletions src/gui/EditWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ EditWidget::EditWidget(QWidget* parent)
{
m_ui->setupUi(this);
setReadOnly(false);
setModified(false);

m_ui->messageWidget->setHidden(true);

Expand All @@ -43,6 +44,7 @@ EditWidget::EditWidget(QWidget* parent)

connect(m_ui->buttonBox, SIGNAL(accepted()), SIGNAL(accepted()));
connect(m_ui->buttonBox, SIGNAL(rejected()), SIGNAL(rejected()));
connect(m_ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), SLOT(buttonClicked(QAbstractButton*)));
}

EditWidget::~EditWidget()
Expand Down Expand Up @@ -106,9 +108,6 @@ void EditWidget::setReadOnly(bool readOnly)
m_ui->buttonBox->setStandardButtons(QDialogButtonBox::Close);
} else {
m_ui->buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply);
// Find and connect the apply button
QPushButton* applyButton = m_ui->buttonBox->button(QDialogButtonBox::Apply);
connect(applyButton, SIGNAL(clicked()), SIGNAL(apply()));
}
}

Expand All @@ -117,6 +116,17 @@ bool EditWidget::readOnly() const
return m_readOnly;
}

void EditWidget::setModified(bool state)
{
m_modified = state;
enableApplyButton(state);
}

bool EditWidget::isModified() const
{
return m_modified;
}

void EditWidget::enableApplyButton(bool enabled)
{
QPushButton* applyButton = m_ui->buttonBox->button(QDialogButtonBox::Apply);
Expand All @@ -125,6 +135,27 @@ void EditWidget::enableApplyButton(bool enabled)
}
}

void EditWidget::showApplyButton(bool state)
{
if (!m_readOnly) {
auto buttons = m_ui->buttonBox->standardButtons();
if (state) {
buttons |= QDialogButtonBox::Apply;
} else {
buttons &= ~QDialogButtonBox::Apply;
}
m_ui->buttonBox->setStandardButtons(buttons);
}
}

void EditWidget::buttonClicked(QAbstractButton* button)
{
auto stdButton = m_ui->buttonBox->standardButton(button);
if (stdButton == QDialogButtonBox::Apply) {
emit apply();
}
}

void EditWidget::showMessage(const QString& text, MessageWidget::MessageType type)
{
// Show error messages for a longer time to make sure the user can read them
Expand Down
5 changes: 5 additions & 0 deletions src/gui/EditWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class EditWidget : public DialogyWidget
void setReadOnly(bool readOnly);
bool readOnly() const;
void enableApplyButton(bool enabled);
void showApplyButton(bool state);
virtual bool isModified() const;

signals:
void apply();
Expand All @@ -58,10 +60,13 @@ class EditWidget : public DialogyWidget
protected slots:
void showMessage(const QString& text, MessageWidget::MessageType type);
void hideMessage();
void setModified(bool state = true);
void buttonClicked(QAbstractButton* button);

private:
const QScopedPointer<Ui::EditWidget> m_ui;
bool m_readOnly;
bool m_modified;

Q_DISABLE_COPY(EditWidget)
};
Expand Down
121 changes: 50 additions & 71 deletions src/gui/entry/EditEntryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,55 +276,54 @@ void EditEntryWidget::setupHistory()
void EditEntryWidget::setupEntryUpdate()
{
// Entry tab
connect(m_mainUi->titleEdit, SIGNAL(textChanged(QString)), this, SLOT(setUnsavedChanges()));
connect(m_mainUi->usernameEdit, SIGNAL(textChanged(QString)), this, SLOT(setUnsavedChanges()));
connect(m_mainUi->passwordEdit, SIGNAL(textChanged(QString)), this, SLOT(setUnsavedChanges()));
connect(m_mainUi->passwordRepeatEdit, SIGNAL(textChanged(QString)), this, SLOT(setUnsavedChanges()));
connect(m_mainUi->urlEdit, SIGNAL(textChanged(QString)), this, SLOT(setUnsavedChanges()));
connect(m_mainUi->titleEdit, SIGNAL(textChanged(QString)), this, SLOT(setModified()));
connect(m_mainUi->usernameEdit, SIGNAL(textChanged(QString)), this, SLOT(setModified()));
connect(m_mainUi->passwordEdit, SIGNAL(textChanged(QString)), this, SLOT(setModified()));
connect(m_mainUi->passwordRepeatEdit, SIGNAL(textChanged(QString)), this, SLOT(setModified()));
connect(m_mainUi->urlEdit, SIGNAL(textChanged(QString)), this, SLOT(setModified()));
#ifdef WITH_XC_NETWORKING
connect(m_mainUi->urlEdit, SIGNAL(textChanged(QString)), this, SLOT(updateFaviconButtonEnable(QString)));
#endif
connect(m_mainUi->expireCheck, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_mainUi->notesEnabled, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_mainUi->expireDatePicker, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(setUnsavedChanges()));
connect(m_mainUi->notesEdit, SIGNAL(textChanged()), this, SLOT(setUnsavedChanges()));
connect(m_mainUi->expireCheck, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_mainUi->notesEnabled, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_mainUi->expireDatePicker, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(setModified()));
connect(m_mainUi->notesEdit, SIGNAL(textChanged()), this, SLOT(setModified()));

// Advanced tab
connect(m_advancedUi->attributesEdit, SIGNAL(textChanged()), this, SLOT(setUnsavedChanges()));
connect(m_advancedUi->protectAttributeButton, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_advancedUi->fgColorCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_advancedUi->bgColorCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_advancedUi->attachmentsWidget, SIGNAL(widgetUpdated()), this, SLOT(setUnsavedChanges()));
connect(m_advancedUi->attributesEdit, SIGNAL(textChanged()), this, SLOT(setModified()));
connect(m_advancedUi->protectAttributeButton, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_advancedUi->fgColorCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_advancedUi->bgColorCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_advancedUi->attachmentsWidget, SIGNAL(widgetUpdated()), this, SLOT(setModified()));

// Icon tab
connect(m_iconsWidget, SIGNAL(widgetUpdated()), this, SLOT(setUnsavedChanges()));
connect(m_iconsWidget, SIGNAL(widgetUpdated()), this, SLOT(setModified()));

// Auto-Type tab
connect(m_autoTypeUi->enableButton, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_autoTypeUi->customWindowSequenceButton, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_autoTypeUi->inheritSequenceButton, SIGNAL(toggled(bool)), this, SLOT(setUnsavedChanges()));
connect(m_autoTypeUi->customSequenceButton, SIGNAL(toggled(bool)), this, SLOT(setUnsavedChanges()));
connect(m_autoTypeUi->windowSequenceEdit, SIGNAL(textChanged(QString)), this, SLOT(setUnsavedChanges()));
connect(m_autoTypeUi->sequenceEdit, SIGNAL(textChanged(QString)), this, SLOT(setUnsavedChanges()));
connect(m_autoTypeUi->windowTitleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_autoTypeUi->windowTitleCombo, SIGNAL(editTextChanged(QString)), this, SLOT(setUnsavedChanges()));
connect(m_autoTypeUi->enableButton, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_autoTypeUi->customWindowSequenceButton, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_autoTypeUi->inheritSequenceButton, SIGNAL(toggled(bool)), this, SLOT(setModified()));
connect(m_autoTypeUi->customSequenceButton, SIGNAL(toggled(bool)), this, SLOT(setModified()));
connect(m_autoTypeUi->windowSequenceEdit, SIGNAL(textChanged(QString)), this, SLOT(setModified()));
connect(m_autoTypeUi->sequenceEdit, SIGNAL(textChanged(QString)), this, SLOT(setModified()));
connect(m_autoTypeUi->windowTitleCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(setModified()));
connect(m_autoTypeUi->windowTitleCombo, SIGNAL(editTextChanged(QString)), this, SLOT(setModified()));

// Properties and History tabs don't need extra connections

#ifdef WITH_XC_SSHAGENT
// SSH Agent tab
if (config()->get("SSHAgent", false).toBool()) {
connect(m_sshAgentUi->attachmentRadioButton, SIGNAL(toggled(bool)), this, SLOT(setUnsavedChanges()));
connect(m_sshAgentUi->externalFileRadioButton, SIGNAL(toggled(bool)), this, SLOT(setUnsavedChanges()));
connect(m_sshAgentUi->attachmentComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_sshAgentUi->attachmentComboBox, SIGNAL(editTextChanged(QString)), this, SLOT(setUnsavedChanges()));
connect(m_sshAgentUi->externalFileEdit, SIGNAL(textChanged(QString)), this, SLOT(setUnsavedChanges()));
connect(m_sshAgentUi->addKeyToAgentCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_sshAgentUi->removeKeyFromAgentCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(
m_sshAgentUi->requireUserConfirmationCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_sshAgentUi->lifetimeCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_sshAgentUi->lifetimeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setUnsavedChanges()));
connect(m_sshAgentUi->attachmentRadioButton, SIGNAL(toggled(bool)), this, SLOT(setModified()));
connect(m_sshAgentUi->externalFileRadioButton, SIGNAL(toggled(bool)), this, SLOT(setModified()));
connect(m_sshAgentUi->attachmentComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setModified()));
connect(m_sshAgentUi->attachmentComboBox, SIGNAL(editTextChanged(QString)), this, SLOT(setModified()));
connect(m_sshAgentUi->externalFileEdit, SIGNAL(textChanged(QString)), this, SLOT(setModified()));
connect(m_sshAgentUi->addKeyToAgentCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_sshAgentUi->removeKeyFromAgentCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_sshAgentUi->requireUserConfirmationCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_sshAgentUi->lifetimeCheckBox, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(m_sshAgentUi->lifetimeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setModified()));
}
#endif
}
Expand Down Expand Up @@ -703,8 +702,10 @@ void EditEntryWidget::loadEntry(Entry* entry,
setCurrentPage(0);
setPageHidden(m_historyWidget, m_history || m_entry->historyItems().count() < 1);

// Force the user to Save/Apply/Discard new entries
setUnsavedChanges(m_create);
// Force the user to Save/Discard new entries
showApplyButton(!m_create);

setModified(false);
}

void EditEntryWidget::setForms(Entry* entry, bool restore)
Expand Down Expand Up @@ -881,7 +882,7 @@ bool EditEntryWidget::commitEntry()
}

updateEntryData(m_entry);
setUnsavedChanges(false);
setModified(false);

if (!m_create) {
m_entry->endUpdate();
Expand Down Expand Up @@ -968,7 +969,7 @@ void EditEntryWidget::cancel()
m_entry->setIcon(Entry::DefaultIconNumber);
}

if (!m_saved) {
if (isModified()) {
auto result = MessageBox::question(this,
QString(),
tr("Entry has unsaved changes"),
Expand All @@ -980,13 +981,13 @@ void EditEntryWidget::cancel()
}
if (result == MessageBox::Save) {
commitEntry();
m_saved = true;
setModified(false);
}
}

clear();

emit editFinished(m_saved);
emit editFinished(!isModified());
}

void EditEntryWidget::clear()
Expand All @@ -1008,22 +1009,6 @@ void EditEntryWidget::clear()
hideMessage();
}

bool EditEntryWidget::hasBeenModified() const
{
// entry has been modified if a history item is to be deleted
if (!m_historyModel->deletedEntries().isEmpty()) {
return true;
}

// check if updating the entry would modify it
auto* entry = new Entry();
entry->copyDataFrom(m_entry.data());

entry->beginUpdate();
updateEntryData(entry);
return entry->endUpdate();
}

void EditEntryWidget::togglePasswordGeneratorButton(bool checked)
{
if (checked) {
Expand Down Expand Up @@ -1070,7 +1055,7 @@ void EditEntryWidget::insertAttribute()
m_advancedUi->attributesView->setCurrentIndex(index);
m_advancedUi->attributesView->edit(index);

setUnsavedChanges(true);
setModified(true);
}

void EditEntryWidget::editCurrentAttribute()
Expand All @@ -1081,7 +1066,7 @@ void EditEntryWidget::editCurrentAttribute()

if (index.isValid()) {
m_advancedUi->attributesView->edit(index);
setUnsavedChanges(true);
setModified(true);
}
}

Expand All @@ -1101,7 +1086,7 @@ void EditEntryWidget::removeCurrentAttribute()

if (result == MessageBox::Remove) {
m_entryAttributes->remove(m_attributesModel->keyByIndex(index));
setUnsavedChanges(true);
setModified(true);
}
}
}
Expand Down Expand Up @@ -1223,7 +1208,7 @@ void EditEntryWidget::insertAutoTypeAssoc()
m_autoTypeUi->assocView->setCurrentIndex(newIndex);
loadCurrentAssoc(newIndex);
m_autoTypeUi->windowTitleCombo->setFocus();
setUnsavedChanges(true);
setModified(true);
}

void EditEntryWidget::removeAutoTypeAssoc()
Expand All @@ -1232,7 +1217,7 @@ void EditEntryWidget::removeAutoTypeAssoc()

if (currentIndex.isValid()) {
m_autoTypeAssoc->remove(currentIndex.row());
setUnsavedChanges(true);
setModified(true);
}
}

Expand Down Expand Up @@ -1295,7 +1280,7 @@ void EditEntryWidget::restoreHistoryEntry()
QModelIndex index = m_sortModel->mapToSource(m_historyUi->historyView->currentIndex());
if (index.isValid()) {
setForms(m_historyModel->entryFromIndex(index), true);
setUnsavedChanges(true);
setModified(true);
}
}

Expand All @@ -1309,15 +1294,15 @@ void EditEntryWidget::deleteHistoryEntry()
} else {
m_historyUi->deleteAllButton->setEnabled(false);
}
setUnsavedChanges(true);
setModified(true);
}
}

void EditEntryWidget::deleteAllHistoryEntries()
{
m_historyModel->deleteAll();
m_historyUi->deleteAllButton->setEnabled(m_historyModel->rowCount() > 0);
setUnsavedChanges(true);
setModified(true);
}

QMenu* EditEntryWidget::createPresetsMenu()
Expand Down Expand Up @@ -1370,12 +1355,6 @@ void EditEntryWidget::pickColor()
QColor newColor = QColorDialog::getColor(oldColor);
if (newColor.isValid()) {
setupColorButton(isForeground, newColor);
setUnsavedChanges(true);
setModified(true);
}
}

void EditEntryWidget::setUnsavedChanges(bool hasUnsaved)
{
m_saved = !hasUnsaved;
enableApplyButton(hasUnsaved);
}
3 changes: 0 additions & 3 deletions src/gui/entry/EditEntryWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class EditEntryWidget : public EditWidget

QString entryTitle() const;
void clear();
bool hasBeenModified() const;

signals:
void editFinished(bool accepted);
Expand Down Expand Up @@ -106,7 +105,6 @@ private slots:
void useExpiryPreset(QAction* action);
void toggleHideNotes(bool visible);
void pickColor();
void setUnsavedChanges(bool hasUnsaved = true);
#ifdef WITH_XC_SSHAGENT
void updateSSHAgent();
void updateSSHAgentAttachment();
Expand Down Expand Up @@ -148,7 +146,6 @@ private slots:

bool m_create;
bool m_history;
bool m_saved;
#ifdef WITH_XC_SSHAGENT
bool m_sshAgentEnabled;
KeeAgentSettings m_sshAgentSettings;
Expand Down
Loading

0 comments on commit 3839449

Please sign in to comment.