Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure export file path always contains a valid file name extension #1736

Merged
merged 1 commit into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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