Improve dialog for editing ignore patterns

This commit is contained in:
Martchus 2024-06-07 00:45:26 +02:00
parent 1794bc5c81
commit ef111fb0aa
6 changed files with 59 additions and 37 deletions

View File

@ -191,8 +191,8 @@ ColumnLayout {
}
PlasmaExtras.MenuItem {
id: showIgnorePatternsItem
text: qsTr("Show ignore patterns")
icon: "selection-symbolic"
text: qsTr("Show/edit ignore patterns")
icon: "document-edit"
onClicked: directoryView.triggerNativeActionWithCurrentItemData(
"showIgnorePatterns", "dirId_")
visible: plasmoid.wipFeaturesEnabled

View File

@ -167,47 +167,63 @@ TextViewDialog *ignorePatternsDialog(Data::SyncthingConnection &connection, cons
auto *const dlg
= new TextViewDialog(QCoreApplication::translate("QtGui::OtherDialogs", "Ignore patterns of folder \"%1\"").arg(dir.displayName()), parent);
dlg->browser()->setText(QStringLiteral("Loading…"));
auto res = connection.ignores(dir.id, [dlg](Data::SyncthingIgnores &&ignores, QString &&errorMessage) {
auto *const browser = dlg->browser();
browser->clear();
if (!errorMessage.isEmpty()) {
browser->setText(errorMessage);
return;
}
for (const auto &ignore : ignores.ignore) {
browser->append(ignore);
}
browser->setUndoRedoEnabled(true);
browser->setReadOnly(false);
QObject::connect(dlg, &TextViewDialog::reload, dlg, [&connection, dirId = dir.id, dlg] {
dlg->browser()->setReadOnly(true);
auto res = connection.ignores(dirId, [dlg](Data::SyncthingIgnores &&ignores, QString &&errorMessage) {
auto *const browser = dlg->browser();
browser->setUndoRedoEnabled(false);
browser->clear();
browser->document()->clearUndoRedoStacks();
if (!errorMessage.isEmpty()) {
browser->setText(errorMessage);
return;
}
for (const auto &ignore : ignores.ignore) {
browser->append(ignore);
}
browser->setUndoRedoEnabled(true);
browser->setReadOnly(false);
dlg->setProperty("savedRevision", browser->document()->revision());
});
QObject::connect(dlg, &QObject::destroyed, res.reply, &QNetworkReply::deleteLater);
});
dlg->setCloseHandler([&connection, dirId = dir.id, pending = false](TextViewDialog *textViewDlg) mutable {
if (pending) {
return true;
}
auto *const browser = textViewDlg->browser();
if (!browser->document()->isUndoAvailable()
|| QMessageBox::question(
textViewDlg, textViewDlg->windowTitle(), QCoreApplication::translate("QtGui::OtherDialogs", "Do you want to save the changes?"))
!= QMessageBox::Yes) {
return false;
}
emit dlg->reload();
QObject::connect(dlg, &TextViewDialog::save, dlg, [&connection, dirId = dir.id, dlg] {
dlg->setProperty("isSaving", true);
auto *const browser = dlg->browser();
auto newIgnores = SyncthingIgnores{ .ignore = browser->toPlainText().split(QChar('\n')), .expanded = QStringList() };
auto setRes = connection.setIgnores(dirId, newIgnores, [textViewDlg, &pending](const QString &error) {
auto setRes = connection.setIgnores(dirId, newIgnores, [dlg](const QString &error) {
if (error.isEmpty()) {
QMessageBox::information(
nullptr, textViewDlg->windowTitle(), QCoreApplication::translate("QtGui::OtherDialogs", "Ignore patterns have been changed."));
textViewDlg->setCloseHandler(std::function<bool(TextViewDialog *)>());
textViewDlg->close();
nullptr, dlg->windowTitle(), QCoreApplication::translate("QtGui::OtherDialogs", "Ignore patterns have been changed."));
if (dlg->property("isClosing").toBool()) {
dlg->setCloseHandler(std::function<bool(TextViewDialog *)>());
dlg->close();
}
} else {
QMessageBox::critical(nullptr, textViewDlg->windowTitle(),
QCoreApplication::translate("QtGui::OtherDialogs", "Unable to save ignore patterns: %1").arg(error));
pending = false;
QMessageBox::critical(
nullptr, dlg->windowTitle(), QCoreApplication::translate("QtGui::OtherDialogs", "Unable to save ignore patterns: %1").arg(error));
}
dlg->setProperty("isSaving", false);
dlg->setProperty("savedRevision", dlg->browser()->document()->revision());
});
QObject::connect(textViewDlg, &QObject::destroyed, setRes.reply, &QNetworkReply::deleteLater);
return pending = true;
QObject::connect(dlg, &QObject::destroyed, setRes.reply, &QNetworkReply::deleteLater);
});
dlg->setCloseHandler([](TextViewDialog *textViewDlg) {
textViewDlg->setProperty("isClosing", true);
if (textViewDlg->property("isSaving").toBool()) {
return true;
}
if (textViewDlg->browser()->document()->revision() <= textViewDlg->property("savedRevision").toInt()) {
return false;
}
const auto question = QMessageBox::question(
textViewDlg, textViewDlg->windowTitle(), QCoreApplication::translate("QtGui::OtherDialogs", "Do you want to save the changes?"));
if (question == QMessageBox::Yes) {
emit textViewDlg->save();
}
return question != QMessageBox::No;
});
QObject::connect(dlg, &QObject::destroyed, res.reply, &QNetworkReply::deleteLater);
return dlg;
}

View File

@ -82,6 +82,11 @@ void TextViewDialog::keyPressEvent(QKeyEvent *event)
case Qt::Key_F5:
emit reload();
break;
case Qt::Key_S:
if (event->modifiers() == Qt::ControlModifier) {
emit save();
}
break;
default:;
}
}

View File

@ -31,6 +31,7 @@ public:
Q_SIGNALS:
void reload();
void save();
protected:
void keyPressEvent(QKeyEvent *event) override;

View File

@ -50,6 +50,7 @@ set(REQUIRED_ICONS
dialog-cancel
dialog-ok
dialog-ok-apply
document-edit
document-open
document-open-remote
download
@ -89,7 +90,6 @@ set(REQUIRED_ICONS
preferences-other
process-stop
qtcreator
selection-symbolic
system-run
system-search
system-file-manager

View File

@ -109,7 +109,7 @@ void DirView::showContextMenu(const QPoint &position)
QIcon(QStringLiteral(":/icons/hicolor/scalable/places/document-open-remote.svg"))),
tr("Browse remote files")),
&QAction::triggered, triggerActionForSelectedRow(this, &DirView::browseRemoteFiles));
connect(menu.addAction(QIcon::fromTheme(QStringLiteral("selection-symbolic")), tr("Show ignore patterns")), &QAction::triggered,
connect(menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), tr("Show/edit ignore patterns")), &QAction::triggered,
triggerActionForSelectedRow(this, &DirView::showIgnorePatterns));
}
}