17 #include <QRegularExpression> 25 list << QObject::tr(
"KB" ) << QObject::tr(
"MB" ) << QObject::tr(
"GB" ) << QObject::tr(
"TB" );
27 QStringListIterator i( list );
28 QString unit = QObject::tr(
"bytes" );
30 while ( bytes >= 1024.0 && i.hasNext() )
35 return QStringLiteral(
"%1 %2" ).arg( QString::number( bytes ), unit );
40 const QRegularExpression rx( QStringLiteral(
"\\*\\.([a-zA-Z0-9]+)" ) );
41 QStringList extensions;
42 QRegularExpressionMatchIterator matches = rx.globalMatch( filter );
44 while ( matches.hasNext() )
46 const QRegularExpressionMatch match = matches.next();
47 if ( match.hasMatch() )
49 QStringList newExtensions = match.capturedTexts();
50 newExtensions.pop_front();
51 extensions.append( newExtensions );
59 if ( extensions.empty() || f.isEmpty() )
64 for (
const QString &extension : qgis::as_const( extensions ) )
66 const QString extWithDot = extension.startsWith(
'.' ) ? extension :
'.' + extension;
67 if ( fileName.endsWith( extWithDot, Qt::CaseInsensitive ) )
76 const QString extension = extensions.at( 0 );
77 const QString extWithDot = extension.startsWith(
'.' ) ? extension :
'.' + extension;
78 fileName += extWithDot;
92 QRegularExpression rx( QStringLiteral(
"[/\\\\\\?%\\*\\:\\|\"<>]" ) );
94 s.replace( rx, QStringLiteral(
"_" ) );
100 if ( path.isEmpty() )
104 QFileInfo fi( path );
106 currentPath = fi.dir();
108 currentPath = QDir( path );
110 QSet< QString > visited;
111 while ( !currentPath.exists() )
113 const QString parentPath = QDir::cleanPath( currentPath.absolutePath() + QStringLiteral(
"/.." ) );
114 if ( visited.contains( parentPath ) )
117 if ( parentPath.isEmpty() || parentPath == QStringLiteral(
"." ) )
119 currentPath = QDir( parentPath );
120 visited << parentPath;
123 const QString res = QDir::cleanPath( currentPath.absolutePath() );
125 if ( res == QDir::currentPath() )
128 return res == QStringLiteral(
"." ) ? QString() : res;
static QString findClosestExistingPath(const QString &path)
Returns the top-most existing folder from path.
static QString ensureFileNameHasExtension(const QString &fileName, const QStringList &extensions)
Ensures that a fileName ends with an extension from the provided list of extensions.
static QString representFileSize(qint64 bytes)
Returns the human size from bytes.
static QString stringToSafeFilename(const QString &string)
Converts a string to a safe filename, replacing characters which are not safe for filenames with an '...
static QStringList extensionsFromFilter(const QString &filter)
Returns a list of the extensions contained within a file filter string.
static QString addExtensionFromFilter(const QString &fileName, const QString &filter)
Ensures that a fileName ends with an extension from the specified filter string.