From 344f1756a30ce304adcf43e749b2541d1ff91508 Mon Sep 17 00:00:00 2001 From: leo Date: Fri, 27 Mar 2015 20:39:43 +0000 Subject: [PATCH] Show a default file name in the export dialogs. --- common/project.cpp | 61 +++++++++++++++++++++++++++++----------------- common/project.h | 1 + 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/common/project.cpp b/common/project.cpp index 4bc36f5a..6d4f34ff 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -350,6 +350,31 @@ void Project::GetModelParts(lcArray& ModelParts) SetActiveModel(mModels.FindIndex(mActiveModel)); } +QString Project::GetExportFileName(const QString& FileName, const QString& DefaultExtension, const QString& DialogTitle, const QString& DialogFilter) const +{ + if (!FileName.isEmpty()) + return FileName; + + QString SaveFileName; + + if (!mFileName.isEmpty()) + SaveFileName = mFileName; + else + SaveFileName = GetTitle(); + + QString Extension = QFileInfo(SaveFileName).suffix().toLower(); + + if (Extension.isEmpty()) + SaveFileName += "." + DefaultExtension; + else if (Extension != DefaultExtension && SaveFileName.length() > 4) + { + SaveFileName = SaveFileName.left(SaveFileName.length() - Extension.length() - 1); + SaveFileName += "." + DefaultExtension; + } + + return QFileDialog::getSaveFileName(gMainWindow, DialogTitle, SaveFileName, DialogFilter); +} + void Project::Export3DStudio(const QString& FileName) { lcArray ModelParts; @@ -362,14 +387,10 @@ void Project::Export3DStudio(const QString& FileName) return; } - QString SaveFileName = FileName; - if (SaveFileName.isEmpty()) - { - SaveFileName = QFileDialog::getSaveFileName(gMainWindow, tr("Export 3D Studio"), QString(), tr("3DS Files (*.3ds);;All Files (*.*)")); + QString SaveFileName = GetExportFileName(FileName, "3ds", tr("Export 3D Studio"), tr("3DS Files (*.3ds);;All Files (*.*)")); - if (SaveFileName.isEmpty()) - return; - } + if (SaveFileName.isEmpty()) + return; lcDiskFile File; @@ -854,17 +875,17 @@ void Project::ExportBrickLink() return; } - QString FileName = QFileDialog::getSaveFileName(gMainWindow, tr("Export BrickLink"), QString(), tr("XML Files (*.xml);;All Files (*.*)")); + QString SaveFileName = GetExportFileName(QString(), "xml", tr("Export BrickLink"), tr("XML Files (*.xml);;All Files (*.*)")); - if (FileName.isEmpty()) + if (SaveFileName.isEmpty()) return; lcDiskFile BrickLinkFile; char Line[1024]; - if (!BrickLinkFile.Open(FileName, "wt")) + if (!BrickLinkFile.Open(SaveFileName, "wt")) { - QMessageBox::warning(gMainWindow, tr("LeoCAD"), tr("Could not open file '%1' for writing.").arg(FileName)); + QMessageBox::warning(gMainWindow, tr("LeoCAD"), tr("Could not open file '%1' for writing.").arg(SaveFileName)); return; } @@ -914,17 +935,17 @@ void Project::ExportCSV() return; } - QString FileName = QFileDialog::getSaveFileName(gMainWindow, tr("Export CSV"), QString(), tr("CSV Files (*.csv);;All Files (*.*)")); + QString SaveFileName = GetExportFileName(QString(), "csv", tr("Export CSV"), tr("CSV Files (*.csv);;All Files (*.*)")); - if (FileName.isEmpty()) + if (SaveFileName.isEmpty()) return; lcDiskFile CSVFile; char Line[1024]; - if (!CSVFile.Open(FileName, "wt")) + if (!CSVFile.Open(SaveFileName, "wt")) { - QMessageBox::warning(gMainWindow, tr("LeoCAD"), tr("Could not open file '%1' for writing.").arg(FileName)); + QMessageBox::warning(gMainWindow, tr("LeoCAD"), tr("Could not open file '%1' for writing.").arg(SaveFileName)); return; } @@ -1577,14 +1598,10 @@ void Project::ExportWavefront(const QString& FileName) return; } - QString SaveFileName = FileName; - if (SaveFileName.isEmpty()) - { - SaveFileName = QFileDialog::getSaveFileName(gMainWindow, tr("Export Wavefront"), QString(), tr("Wavefront Files (*.obj);;All Files (*.*)")); + QString SaveFileName = GetExportFileName(FileName, "obj", tr("Export Wavefront"), tr("Wavefront Files (*.obj);;All Files (*.*)")); - if (SaveFileName.isEmpty()) - return; - } + if (SaveFileName.isEmpty()) + return; lcDiskFile OBJFile; char Line[1024]; diff --git a/common/project.h b/common/project.h index cd1e6a44..96c25874 100644 --- a/common/project.h +++ b/common/project.h @@ -72,6 +72,7 @@ public: void ExportWavefront(const QString& FileName); protected: + QString GetExportFileName(const QString& FileName, const QString& DefaultExtension, const QString& DialogTitle, const QString& DialogFilter) const; void GetModelParts(lcArray& ModelParts); void CreateHTMLPieceList(QTextStream& Stream, lcModel* Model, lcStep Step, bool Images, const QString& ImageExtension);