Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qgroundcontrol
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Valentin Platzgummer
qgroundcontrol
Commits
1e8545f7
Commit
1e8545f7
authored
Feb 02, 2015
by
dogmaphobic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding code to check and see if a given file has a proper extension (when the strict flag is set).
parent
6ee1a423
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
5 deletions
+72
-5
QGCFileDialog.cc
src/QGCFileDialog.cc
+69
-4
QGCFileDialog.h
src/QGCFileDialog.h
+3
-1
No files found.
src/QGCFileDialog.cc
View file @
1e8545f7
...
...
@@ -23,6 +23,7 @@
#include "QGCFileDialog.h"
#include "QGCApplication.h"
#include "QRegularExpression.h"
#include "MainWindow.h"
#ifdef QT_DEBUG
#include "UnitTest.h"
...
...
@@ -105,9 +106,6 @@ QString QGCFileDialog::getSaveFileName(QWidget* parent,
{
QFileDialog
dlg
(
parent
,
caption
,
dir
,
filter
);
dlg
.
setAcceptMode
(
QFileDialog
::
AcceptSave
);
if
(
selectedFilter
)
{
dlg
.
selectNameFilter
(
*
selectedFilter
);
}
if
(
options
)
{
dlg
.
setOptions
(
options
);
}
...
...
@@ -121,7 +119,45 @@ QString QGCFileDialog::getSaveFileName(QWidget* parent,
while
(
true
)
{
if
(
dlg
.
exec
())
{
if
(
dlg
.
selectedFiles
().
count
())
{
return
dlg
.
selectedFiles
().
first
();
QString
result
=
dlg
.
selectedFiles
().
first
();
//-- If we don't care about the extension, just return it
if
(
!
strict
)
{
return
result
;
}
else
{
//-- We must enforce the file extension
QFileInfo
fi
(
result
);
QString
userSuffix
(
fi
.
suffix
());
if
(
_validateExtension
(
filter
,
userSuffix
))
{
return
result
;
}
#if 0 //-- If we ever want to propose replacing the extension, we can guess the replacement below
//-- Do we have a default extension?
QString localDefaultSuffix;
if (!defaultSuffix) {
//-- We don't, so get the first one in the filter
localDefaultSuffix = _getFirstExtensionInFilter(filter);
defaultSuffix = &localDefaultSuffix;
}
Q_ASSERT(defaultSuffix->isEmpty() == false);
QString proposed = fi.completeBaseName();
proposed += ".";
proposed += *defaultSuffix;
#endif
//-- Ask user what to do
QMessageBox
msgBox
(
QMessageBox
::
Critical
,
tr
(
"Invalid File Extension."
),
tr
(
"The given extension (.%1) is not a valid extension for this type of file."
).
arg
(
userSuffix
),
QMessageBox
::
Cancel
,
parent
);
msgBox
.
setDefaultButton
(
QMessageBox
::
Cancel
);
msgBox
.
setWindowModality
(
Qt
::
ApplicationModal
);
QPushButton
*
retryButton
=
msgBox
.
addButton
(
tr
(
"Retry"
),
QMessageBox
::
ActionRole
);
msgBox
.
exec
();
if
(
msgBox
.
clickedButton
()
==
retryButton
)
{
continue
;
}
}
}
}
break
;
...
...
@@ -130,6 +166,35 @@ QString QGCFileDialog::getSaveFileName(QWidget* parent,
}
}
/// @brief Make sure filename is using one of the valid extensions defined in the filter
bool
QGCFileDialog
::
_validateExtension
(
const
QString
&
filter
,
const
QString
&
extension
)
{
QRegularExpression
re
(
"(
\\
*
\\
.
\\
w+)"
);
QRegularExpressionMatchIterator
i
=
re
.
globalMatch
(
filter
);
while
(
i
.
hasNext
())
{
QRegularExpressionMatch
match
=
i
.
next
();
if
(
match
.
hasMatch
())
{
//-- Compare "foo" with "*.foo"
if
(
extension
==
match
.
captured
(
0
).
mid
(
2
))
{
return
true
;
}
}
}
return
false
;
}
/// @brief Returns first extension found in filter
QString
QGCFileDialog
::
_getFirstExtensionInFilter
(
const
QString
&
filter
)
{
QRegularExpression
re
(
"(
\\
*
\\
.
\\
w+)"
);
QRegularExpressionMatchIterator
i
=
re
.
globalMatch
(
filter
);
while
(
i
.
hasNext
())
{
QRegularExpressionMatch
match
=
i
.
next
();
if
(
match
.
hasMatch
())
{
return
match
.
captured
(
0
).
mid
(
1
);
}
}
return
QString
(
""
);
}
/// @brief Validates and updates the parameters for the file dialog calls
void
QGCFileDialog
::
_validate
(
QString
*
selectedFilter
,
Options
&
options
)
{
...
...
src/QGCFileDialog.h
View file @
1e8545f7
...
...
@@ -125,7 +125,9 @@ private slots:
int
exec
(
void
)
{
return
QGCFileDialog
::
exec
();
}
private:
static
void
_validate
(
QString
*
selectedFilter
,
Options
&
options
);
static
void
_validate
(
QString
*
selectedFilter
,
Options
&
options
);
static
bool
_validateExtension
(
const
QString
&
filter
,
const
QString
&
extension
);
static
QString
_getFirstExtensionInFilter
(
const
QString
&
filter
);
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment