24 typedef std::vector< std::pair< QString, std::function< QString( const QString & ) > > >
CustomResolvers;
28 : mBaseFileName( baseFileName )
38 for (
const auto &resolver : customResolvers )
39 filename = resolver.second( filename );
41 if ( filename.isEmpty() )
44 QString src = filename;
45 if ( src.startsWith( QLatin1String(
"inbuilt:" ) ) )
51 if ( mBaseFileName.isNull() )
58 if ( ! vsiPrefix.isEmpty() )
62 if ( src.startsWith( QLatin1String(
"/vsi" ), Qt::CaseInsensitive ) )
63 src.remove( 0, vsiPrefix.size() );
69 if ( !src.startsWith( QLatin1String(
"./" ) ) && !src.startsWith( QLatin1String(
"../" ) ) )
72 if ( src.startsWith(
"\\\\" ) ||
73 src.startsWith(
"//" ) ||
74 ( src[0].isLetter() && src[1] ==
':' ) )
77 return vsiPrefix + src;
83 return vsiPrefix + src;
92 QFileInfo pfi( mBaseFileName );
93 QString home = pfi.absolutePath();
95 return vsiPrefix + src;
97 QFileInfo fi( home +
'/' + src );
101 return vsiPrefix + src;
105 return vsiPrefix + fi.canonicalFilePath();
109 QString srcPath = src;
110 QString projPath = mBaseFileName;
112 if ( projPath.isEmpty() )
114 return vsiPrefix + src;
117 #if defined(Q_OS_WIN) 118 srcPath.replace(
'\\',
'/' );
119 projPath.replace(
'\\',
'/' );
121 bool uncPath = projPath.startsWith(
"//" );
125 projPath = QFileInfo( projPath ).absoluteFilePath();
127 QStringList srcElems = srcPath.split(
'/', QString::SkipEmptyParts );
128 QStringList projElems = projPath.split(
'/', QString::SkipEmptyParts );
130 #if defined(Q_OS_WIN) 133 projElems.insert( 0,
"" );
134 projElems.insert( 0,
"" );
139 projElems.removeLast();
142 projElems << srcElems;
143 projElems.removeAll( QStringLiteral(
"." ) );
147 while ( ( pos = projElems.indexOf( QStringLiteral(
".." ) ) ) > 0 )
150 projElems.removeAt( pos - 1 );
151 projElems.removeAt( pos - 1 );
154 #if !defined(Q_OS_WIN) 156 projElems.prepend( QString() );
159 return vsiPrefix + projElems.join( QStringLiteral(
"/" ) );
164 QString
id = QUuid::createUuid().toString();
165 sCustomResolvers()->emplace_back( std::make_pair(
id, processor ) );
171 const size_t prevCount = sCustomResolvers()->size();
172 sCustomResolvers()->erase( std::remove_if( sCustomResolvers()->begin(), sCustomResolvers()->end(), [
id]( std::pair< QString, std::function< QString(
const QString & ) > > &a )
174 return a.first == id;
175 } ), sCustomResolvers()->end() );
176 return prevCount != sCustomResolvers()->size();
192 if ( mBaseFileName.isEmpty() )
198 QFileInfo pfi( QFileInfo( mBaseFileName ).path() );
199 QString projPath = pfi.canonicalFilePath();
203 if ( projPath.isEmpty() )
204 projPath = pfi.absoluteFilePath();
206 if ( projPath.isEmpty() )
213 QString srcPath { src };
216 if ( url.isLocalFile( ) )
218 srcPath = url.path();
219 urlQuery = url.query();
222 QFileInfo srcFileInfo( srcPath );
223 if ( srcFileInfo.exists() )
224 srcPath = srcFileInfo.canonicalFilePath();
228 if ( ! vsiPrefix.isEmpty() )
230 srcPath.remove( 0, vsiPrefix.size() );
233 #if defined( Q_OS_WIN ) 234 const Qt::CaseSensitivity cs = Qt::CaseInsensitive;
236 srcPath.replace(
'\\',
'/' );
238 if ( srcPath.startsWith(
"//" ) )
241 srcPath =
"\\\\" + srcPath.mid( 2 );
244 projPath.replace(
'\\',
'/' );
245 if ( projPath.startsWith(
"//" ) )
248 projPath =
"\\\\" + projPath.mid( 2 );
251 const Qt::CaseSensitivity cs = Qt::CaseSensitive;
254 QStringList projElems = projPath.split(
'/', QString::SkipEmptyParts );
255 QStringList srcElems = srcPath.split(
'/', QString::SkipEmptyParts );
257 projElems.removeAll( QStringLiteral(
"." ) );
258 srcElems.removeAll( QStringLiteral(
"." ) );
262 while ( !srcElems.isEmpty() &&
263 !projElems.isEmpty() &&
264 srcElems[0].compare( projElems[0], cs ) == 0 )
266 srcElems.removeFirst();
267 projElems.removeFirst();
277 if ( !projElems.isEmpty() )
280 for (
int i = 0; i < projElems.size(); i++ )
282 srcElems.insert( 0, QStringLiteral(
".." ) );
289 srcElems.insert( 0, QStringLiteral(
"." ) );
293 QString returnPath { vsiPrefix + srcElems.join( QStringLiteral(
"/" ) ) };
294 if ( ! urlQuery.isEmpty() )
296 returnPath.append(
'?' );
297 returnPath.append( urlQuery );
static QString setPathPreprocessor(const std::function< QString(const QString &filename)> &processor)
Sets a path pre-processor function, which allows for manipulation of paths and data sources prior to ...
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
static bool removePathPreprocessor(const QString &id)
Removes the custom pre-processor function with matching id.
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
std::vector< std::pair< QString, std::function< QString(const QString &) > > > CustomResolvers
static QString pkgDataPath()
Returns the common root path of all application data directories.
QString qgsVsiPrefix(const QString &path)
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
Resolves relative paths into absolute paths and vice versa.