18 #include <QRegularExpression> 26 list << QObject::tr(
"KB" ) << QObject::tr(
"MB" ) << QObject::tr(
"GB" ) << QObject::tr(
"TB" );
28 QStringListIterator i( list );
29 QString unit = QObject::tr(
"bytes" );
31 while ( bytes >= 1024.0 && i.hasNext() )
36 return QStringLiteral(
"%1 %2" ).arg( QString::number( bytes ), unit );
41 const QRegularExpression rx( QStringLiteral(
"\\*\\.([a-zA-Z0-9]+)" ) );
42 QStringList extensions;
43 QRegularExpressionMatchIterator matches = rx.globalMatch( filter );
45 while ( matches.hasNext() )
47 const QRegularExpressionMatch match = matches.next();
48 if ( match.hasMatch() )
50 QStringList newExtensions = match.capturedTexts();
51 newExtensions.pop_front();
52 extensions.append( newExtensions );
60 if ( extensions.empty() || f.isEmpty() )
65 for (
const QString &extension : qgis::as_const( extensions ) )
67 const QString extWithDot = extension.startsWith(
'.' ) ? extension :
'.' + extension;
68 if ( fileName.endsWith( extWithDot, Qt::CaseInsensitive ) )
77 const QString extension = extensions.at( 0 );
78 const QString extWithDot = extension.startsWith(
'.' ) ? extension :
'.' + extension;
79 fileName += extWithDot;
93 QRegularExpression rx( QStringLiteral(
"[/\\\\\\?%\\*\\:\\|\"<>]" ) );
95 s.replace( rx, QStringLiteral(
"_" ) );
101 if ( path.isEmpty() )
105 QFileInfo fi( path );
107 currentPath = fi.dir();
109 currentPath = QDir( path );
111 QSet< QString > visited;
112 while ( !currentPath.exists() )
114 const QString parentPath = QDir::cleanPath( currentPath.absolutePath() + QStringLiteral(
"/.." ) );
115 if ( visited.contains( parentPath ) )
118 if ( parentPath.isEmpty() || parentPath == QStringLiteral(
"." ) )
120 currentPath = QDir( parentPath );
121 visited << parentPath;
124 const QString res = QDir::cleanPath( currentPath.absolutePath() );
126 if ( res == QDir::currentPath() )
129 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.