diff options
author | w0xlt <94266259+w0xlt@users.noreply.github.com> | 2022-06-21 18:53:03 -0300 |
---|---|---|
committer | w0xlt <94266259+w0xlt@users.noreply.github.com> | 2022-06-21 19:16:39 -0300 |
commit | c378535e28e0c54353fde9c4a66cb3134a3ee742 (patch) | |
tree | da8798d66a8c29bb2bd33bf7169ae11380000730 /src/qt | |
parent | a09033e22c4f072c86a1885dd476f3059e5416d1 (diff) |
qt: Add a function that extracts the suffix from a filter
Extract the 'Extract first suffix from filter pattern...'
functionality into a testable utility function
Diffstat (limited to 'src/qt')
-rw-r--r-- | src/qt/guiutil.cpp | 28 | ||||
-rw-r--r-- | src/qt/guiutil.h | 8 |
2 files changed, 21 insertions, 15 deletions
diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index e3c6d8a624..2059ef8583 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -292,6 +292,16 @@ QString getDefaultDataDirectory() return PathToQString(GetDefaultDataDir()); } +QString ExtractFirstSuffixFromFilter(const QString& filter) +{ + QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]"); + QString suffix; + if (filter_re.exactMatch(filter)) { + suffix = filter_re.cap(1); + } + return suffix; +} + QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut) @@ -309,13 +319,7 @@ QString getSaveFileName(QWidget *parent, const QString &caption, const QString & /* Directly convert path to native OS path separators */ QString result = QDir::toNativeSeparators(QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter)); - /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */ - QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]"); - QString selectedSuffix; - if(filter_re.exactMatch(selectedFilter)) - { - selectedSuffix = filter_re.cap(1); - } + QString selectedSuffix = ExtractFirstSuffixFromFilter(selectedFilter); /* Add suffix if needed */ QFileInfo info(result); @@ -357,14 +361,8 @@ QString getOpenFileName(QWidget *parent, const QString &caption, const QString & if(selectedSuffixOut) { - /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */ - QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]"); - QString selectedSuffix; - if(filter_re.exactMatch(selectedFilter)) - { - selectedSuffix = filter_re.cap(1); - } - *selectedSuffixOut = selectedSuffix; + *selectedSuffixOut = ExtractFirstSuffixFromFilter(selectedFilter); + ; } return result; } diff --git a/src/qt/guiutil.h b/src/qt/guiutil.h index e38ac6026a..acbe415a91 100644 --- a/src/qt/guiutil.h +++ b/src/qt/guiutil.h @@ -123,6 +123,14 @@ namespace GUIUtil */ QString getDefaultDataDirectory(); + /** + * Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...). + * + * @param[in] filter Filter specification such as "Comma Separated Files (*.csv)" + * @return QString + */ + QString ExtractFirstSuffixFromFilter(const QString& filter); + /** Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix when no suffix is provided by the user. |