18#include "filedialog.h"
24#include "fileformat.h"
31 QString strFilter = openFileFilters(fileType);
32 QString strSelectedFilter = getFilterForFile(strFilter, strInitialFilePath);
35 caption.
isEmpty() ? openDialogCaption(fileType) : caption,
38 strSelectedFilter.
isNull() ?
nullptr : &strSelectedFilter);
43 if (fileType == FileType::ANIMATION)
46 setLastSavePaths(filePath);
56 QString strFilter = openFileFilters(fileType);
57 QString strSelectedFilter = getFilterForFile(strFilter, strInitialFilePath);
60 caption.
isEmpty() ? openDialogCaption(fileType) : caption,
63 strSelectedFilter.
isNull() ?
nullptr : &strSelectedFilter);
64 if (!filePaths.
isEmpty() && !filePaths.
first().isEmpty())
72void FileDialog::setLastSavePaths(
const QString& filePath)
75 QDir projectPath = filePathInfo.absoluteDir();
76 QString baseName = filePathInfo.baseName();
77 QList<FileType> fileTypes = { FileType::IMAGE, FileType::IMAGE_SEQUENCE, FileType::GIF, FileType::MOVIE, FileType::SOUND, FileType::PALETTE };
78 for (FileType& fileType : fileTypes)
87 QString strFilter = saveFileFilters(fileType);
88 QString strSelectedFilter = getFilterForFile(strFilter, strInitialFilePath);
91 caption.
isEmpty() ? saveDialogCaption(fileType) : caption,
94 strSelectedFilter.
isNull() ?
nullptr : &strSelectedFilter);
98 if (!hasValidSuffix(strFilter, filePath))
100 filePath += getDefaultExtensionByFileType(fileType);
103 if (fileType == FileType::ANIMATION)
106 setLastSavePaths(filePath);
114QString FileDialog::getDefaultExtensionByFileType(
const FileType fileType)
118 case FileType::ANIMATION:
return PFF_DEFAULT_PROJECT_EXT;
119 case FileType::IMAGE:
return PFF_DEFAULT_IMAGE_EXT;
120 case FileType::IMAGE_SEQUENCE:
return PFF_DEFAULT_IMAGE_SEQ_EXT;
121 case FileType::GIF:
return PFF_DEFAULT_ANIMATED_EXT;
122 case FileType::ANIMATED_IMAGE:
return PFF_DEFAULT_ANIMATED_EXT;
123 case FileType::PALETTE:
return PFF_DEFAULT_PALETTE_EXT;
124 case FileType::MOVIE:
return PFF_DEFAULT_MOVIE_EXT;
125 case FileType::SOUND:
return PFF_DEFAULT_SOUND_EXT;
151 return setting.
value(toSettingKey(fileType),
160 setting.
setValue(toSettingKey(fileType), savePath);
163QString FileDialog::openDialogCaption(FileType fileType)
167 case FileType::ANIMATION:
return tr(
"Open animation");
168 case FileType::IMAGE:
return tr(
"Import image");
169 case FileType::IMAGE_SEQUENCE:
return tr(
"Import image sequence");
170 case FileType::GIF:
return tr(
"Import Animated GIF");
171 case FileType::ANIMATED_IMAGE:
return tr(
"Import animated image");
172 case FileType::MOVIE:
return tr(
"Import movie");
173 case FileType::SOUND:
return tr(
"Import sound");
174 case FileType::PALETTE:
return tr(
"Open palette");
179QString FileDialog::saveDialogCaption(FileType fileType)
183 case FileType::ANIMATION:
return tr(
"Save animation");
184 case FileType::IMAGE:
return tr(
"Export image");
185 case FileType::IMAGE_SEQUENCE:
return tr(
"Export image sequence");
186 case FileType::GIF:
return tr(
"Export Animated GIF");
187 case FileType::ANIMATED_IMAGE:
return tr(
"Export animated image");
188 case FileType::MOVIE:
return tr(
"Export movie");
189 case FileType::SOUND:
return "";
190 case FileType::PALETTE:
return tr(
"Export palette");
195QString FileDialog::openFileFilters(FileType fileType)
199 case FileType::ANIMATION:
return PFF_OPEN_PROJECT_EXT_FILTER;
200 case FileType::IMAGE:
return PFF_IMAGE_FILTER;
201 case FileType::IMAGE_SEQUENCE:
return PFF_IMAGE_SEQ_FILTER;
202 case FileType::GIF:
return PFF_GIF_EXT_FILTER;
203 case FileType::ANIMATED_IMAGE:
return PFF_ANIMATED_IMAGE_EXT_FILTER;
204 case FileType::MOVIE:
return PFF_MOVIE_EXT;
205 case FileType::SOUND:
return PFF_SOUND_EXT_FILTER;
206 case FileType::PALETTE:
return PFF_PALETTE_EXT_FILTER;
211QString FileDialog::saveFileFilters(FileType fileType)
215 case FileType::ANIMATION:
return PFF_SAVE_PROJECT_EXT_FILTER;
216 case FileType::IMAGE:
return "";
217 case FileType::IMAGE_SEQUENCE:
return "";
218 case FileType::GIF:
return QString(
"%1 (*.gif)").
arg(
tr(
"Animated GIF"));
219 case FileType::ANIMATED_IMAGE:
return "";
220 case FileType::MOVIE:
return "MP4 (*.mp4);; AVI (*.avi);; WebM (*.webm);; APNG (*.apng)";
221 case FileType::SOUND:
return "";
222 case FileType::PALETTE:
return PFF_PALETTE_EXT_FILTER;
227bool FileDialog::hasValidSuffix(
const QString& filters,
const QString& filePath)
232 int start = filter.indexOf(
"(") + 1;
233 int end = filter.indexOf(
")");
234 Q_ASSERT(start >= 1 && end >= 0);
236 if (
QDir::match(filter.mid(start, end - start), fileName)) {
252 for (
const QString& filter : filtersSplit)
254 int start = filter.indexOf(
"(");
255 int end = filter.indexOf(
")");
256 if (start < 0 || end < 0)
261 QStringList filterExts = filter.mid(start, end - start).split(
" ");
264 return filter.trimmed();
271QString FileDialog::defaultFileName(FileType fileType,
QString baseName)
276 defaultName = baseName;
278 else if (fileType == FileType::ANIMATION)
280 defaultName =
tr(
"MyAnimation");
283 return defaultName.
append(getDefaultExtensionByFileType(fileType));
286QString FileDialog::toSettingKey(FileType fileType)
290 case FileType::ANIMATION:
return "Animation";
291 case FileType::IMAGE:
return "Image";
292 case FileType::IMAGE_SEQUENCE:
return "ImageSequence";
293 case FileType::GIF:
return "Animated GIF";
294 case FileType::ANIMATED_IMAGE:
return "Animated Image";
295 case FileType::MOVIE:
return "Movie";
296 case FileType::SOUND:
return "Sound";
297 case FileType::PALETTE:
return "Palette";
static QString getLastSavePath(FileType fileType)
Retrieves the last used save path for a given file type.
static QStringList getOpenFileNames(QWidget *parent, FileType fileType, const QString &caption=QString())
Shows a file dialog which allows the user to select multiple files to open.
static void setLastSavePath(FileType fileType, const QString &savePath)
Saves the last used save path for a given file type.
static QString getSaveFileName(QWidget *parent, FileType fileType, const QString &caption=QString())
Shows a file dialog which allows the user to select a file save path.
static QString getOpenFileName(QWidget *parent, FileType fileType, const QString &caption=QString())
Shows a file dialog which allows the user to select a file to open.
static void setLastOpenPath(FileType fileType, const QString &openPath)
Saves the last opened file of a given type.
static QString getLastOpenPath(FileType fileType)
Retrieves the last opened file of a given type.
QString absoluteFilePath(const QString &fileName) const const
bool match(const QStringList &filters, const QString &fileName)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QStringList getOpenFileNames(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QString fileName() const const
bool isEmpty() const const
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
void beginGroup(const QString &prefix)
void setValue(const QString &key, const QVariant &value)
QVariant value(const QString &key, const QVariant &defaultValue) const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString & append(QChar ch)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
bool isNull() const const
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString & prepend(QChar ch)
QString & remove(int position, int n)
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QString toString() const const