aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Cord-Landwehr <[email protected]>2017-05-26 17:29:31 +0200
committerAndreas Cord-Landwehr <[email protected]>2017-05-26 17:29:31 +0200
commitb8cc5ef420474cadf3a506527bebde09ac36e3f9 (patch)
tree9f308126e0b4bfdc3d271add3fac34655d28d46c
parentd0e4b649b5758e6d1e6153a9d626d8b433f85ff5 (diff)
Fix graph export filter
Includes: - add message boxes if anything goes fatally wrong (i.e. no data is written) - fix generation of export filters - deduce export plugin from selected filter and not from file suffix, since that is not relyable BUG: 378095
-rw-r--r--src/ui/fileformatdialog.cpp36
1 files changed, 21 insertions, 15 deletions
diff --git a/src/ui/fileformatdialog.cpp b/src/ui/fileformatdialog.cpp
index 207c67f..19f5c8d 100644
--- a/src/ui/fileformatdialog.cpp
+++ b/src/ui/fileformatdialog.cpp
@@ -31,6 +31,7 @@
#include <QUrl>
#include <QFile>
#include <QPointer>
+#include <QRegularExpression>
using namespace GraphTheory;
@@ -43,17 +44,16 @@ bool FileFormatDialog::exportFile(GraphDocumentPtr document) const
{
FileFormatManager manager;
- //TODO rewrite this whole class, it is a wonder that the code works :)
- QString ext;
+ QStringList nameFilter;
QList<FileFormatInterface*> exportBackends = manager.backends(FileFormatManager::Export);
foreach(FileFormatInterface * f, exportBackends) { //TODO fragile code
- ext.append(f->extensions().join(";;"));
+ nameFilter << f->extensions();
}
- ext.append(i18n("*|All files"));
QUrl startDirectory = QUrl::fromLocalFile(Settings::lastOpenedDirectory());
- QPointer<QFileDialog> exportDialog = new QFileDialog(qobject_cast< QWidget* >(parent()), QString(), QString(), ext);
+ QPointer<QFileDialog> exportDialog = new QFileDialog(qobject_cast< QWidget* >(parent()));
+ exportDialog->setNameFilters(nameFilter);
exportDialog->setLabelText(QFileDialog::Accept, i18nc("@action:button", "Export"));
if (exportDialog->exec() != QDialog::Accepted) {
return false;
@@ -64,13 +64,7 @@ bool FileFormatDialog::exportFile(GraphDocumentPtr document) const
}
// set file ending
- QUrl file;
- ext = exportDialog->selectedNameFilter().remove('*');
- if (exportDialog->selectedFiles().first().endsWith(ext)) {
- file = QUrl::fromLocalFile(exportDialog->selectedFiles().first());
- } else {
- file = QUrl::fromLocalFile(exportDialog->selectedFiles().first().append(ext));
- }
+ const QUrl file = QUrl::fromLocalFile(exportDialog->selectedFiles().first());
// test if any file is overwritten
if (QFile::exists(exportDialog->selectedFiles().first())) {
@@ -85,16 +79,28 @@ bool FileFormatDialog::exportFile(GraphDocumentPtr document) const
}
// select plugin by extension
+ const QString filter = exportDialog->selectedNameFilter();
+
+ // find match for "(*.foo)"
+ QRegularExpressionMatch match;
+ filter.lastIndexOf(QRegularExpression("\\*\\.[a-zA-Z0-9]+"), -1, &match);
+ const QString ext = match.captured(0).right(match.captured(0).length() - 2);
FileFormatInterface * filePlugin = manager.backendByExtension(ext);
- if (!filePlugin) {
- qDebug() << "Cannot export file: " << file.toLocalFile();
+ if (ext == QString() || !filePlugin) {
+ KMessageBox::error(qobject_cast< QWidget* >(parent()), i18n(
+ "<p>Cannot resolve suffix of file <strong>'%1'</strong> to an available file backend."
+ "Aborting export.</p>",
+ file.toDisplayString()));
+ qCritical() << "Cannot export file, since cannot find plugin for extension: " << file.toLocalFile() << ext;
return false;
}
filePlugin->setFile(file);
filePlugin->writeFile(document);
if (filePlugin->hasError()) {
- qDebug() << "Error occurred when writing file: " << filePlugin->errorString();
+ KMessageBox::error(qobject_cast< QWidget* >(parent()), i18n(
+ "<p>Error occured when writing file: <strong>'%1'</strong></p>", filePlugin->errorString()));
+ qCritical() << "Error occurred when writing file: " << filePlugin->errorString();
return false;
}