Skip to content

Commit

Permalink
Ensure export file path always contains a valid file name extension
Browse files Browse the repository at this point in the history
  • Loading branch information
J5lx committed Oct 30, 2022
1 parent 72173bd commit bdb6dfd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
27 changes: 21 additions & 6 deletions app/src/filedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ QString FileDialog::getSaveFileName(QWidget* parent, FileType fileType, const QS

if (filePath.isEmpty()) { return QString(); }

if (!hasValidSuffix(strFilter, filePath))
{
filePath += getDefaultExtensionByFileType(fileType);
}

if (fileType == FileType::ANIMATION)
{
// When we save a new project, change default path for all other filetypes
Expand All @@ -103,12 +108,6 @@ QString FileDialog::getSaveFileName(QWidget* parent, FileType fileType, const QS

setLastSavePath(fileType, filePath);

QFileInfo info(filePath);
if (info.suffix().isEmpty() && strSelectedFilter.isEmpty())
{
filePath += getDefaultExtensionByFileType(fileType);
}

return filePath;
}

Expand Down Expand Up @@ -220,6 +219,22 @@ QString FileDialog::saveFileFilters(FileType fileType)
return "";
}

bool FileDialog::hasValidSuffix(const QString& filters, const QString& filePath)
{
QString fileName = QFileInfo(filePath).fileName();
for (const QString& filter : filters.split(";;"))
{
int start = filter.indexOf("(") + 1;
int end = filter.indexOf(")");
Q_ASSERT(start >= 1 && end >= 0);

if (QDir::match(filter.mid(start, end - start), fileName)) {
return true;
}
}
return false;
}

QString FileDialog::getFilterForFile(const QString& filters, QString filePath)
{
if (!filePath.contains("."))
Expand Down
1 change: 1 addition & 0 deletions app/src/filedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class FileDialog : public QObject
static QString getFilterForFile( const QString& fileType, QString filePath );
static QString defaultFileName(FileType fileType , QString baseName = QString());

static bool hasValidSuffix(const QString& filters, const QString& filePath);
static QString getDefaultExtensionByFileType(FileType fileType);

static QString toSettingKey( FileType fileType );
Expand Down
2 changes: 1 addition & 1 deletion core_lib/src/util/fileformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ GNU General Public License for more details.
QString(".pclx")

#define PFF_DEFAULT_IMAGE_EXT \
QString(".png")
QString(".png")

#define PFF_DEFAULT_IMAGE_SEQ_EXT \
QString(".png")
Expand Down

0 comments on commit bdb6dfd

Please sign in to comment.