QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgslocalizeddatapathregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslocalizeddatapathregistry.cpp
3 --------------------------------------
4 Date : May 2020
5 Copyright : (C) 2020 by Denis Rouzaud
6 Email : denis.rouzaud
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgis.h"
19#include "qgsreadwritelocker.h"
21#include "qgssettingstree.h"
22
23#include <QDir>
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29
34
35QString QgsLocalizedDataPathRegistry::globalPath( const QString &relativePath ) const
36{
38
39 for ( const QDir &basePath : std::as_const( mPaths ) )
40 if ( basePath.exists( relativePath ) )
41 return basePath.absoluteFilePath( relativePath );
42
43 return QString();
44}
45
46QString QgsLocalizedDataPathRegistry::localizedPath( const QString &fullPath ) const
47{
49
50 for ( const QDir &basePath : std::as_const( mPaths ) )
51 if ( fullPath.startsWith( basePath.absolutePath() ) )
52 return basePath.relativeFilePath( fullPath );
53
54 return QString();
55
56}
57
59{
61
62 QStringList paths;
63 for ( const QDir &dir : mPaths )
64 paths << dir.absolutePath();
65 return paths;
66}
67
69{
71
72 mPaths.clear();
73 for ( const QString &path : paths )
74 {
75 QDir dir( path );
76 if ( !mPaths.contains( dir ) )
77 mPaths << dir;
78 }
79
80 locker.unlock();
81 writeToSettings();
82}
83
84void QgsLocalizedDataPathRegistry::registerPath( const QString &path, int position )
85{
87
88 QDir dir( path );
89 if ( mPaths.contains( dir ) )
90 return;
91
93
94 if ( position >= 0 && position < mPaths.count() )
95 mPaths.insert( position, dir );
96 else
97 mPaths.append( dir );
98
99 locker.unlock();
100 writeToSettings();
101}
102
104{
106
107 mPaths.removeAll( QDir( path ) );
108 locker.unlock();
109 writeToSettings();
110}
111
112void QgsLocalizedDataPathRegistry::readFromSettings()
113{
115}
116
117void QgsLocalizedDataPathRegistry::writeToSettings() const
118{
119 settingsLocalizedDataPaths->setValue( paths() );
120}
QString globalPath(const QString &localizedPath) const
Returns the global path if the file has been found in one of the paths, an empty string otherwise.
void setPaths(const QStringList &paths)
Sets the complete list of localized path.
void unregisterPath(const QString &path)
Unregisters a localized path.
QString localizedPath(const QString &globalPath) const
Returns the localized path if the file has been found in one of the path, an empty string otherwise.
static const QgsSettingsEntryStringList * settingsLocalizedDataPaths
Settings entry localized data paths.
void registerPath(const QString &path, int position=-1)
Registers a localized path If position is given, the path is inserted at the given position in the li...
QStringList paths() const
Returns a list of registered localized paths.
A convenience class that simplifies locking and unlocking QReadWriteLocks.
@ Write
Lock for write.
void unlock()
Unlocks the lock.
void changeMode(Mode mode)
Change the mode of the lock to mode.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
A string list settings entry.
static QgsSettingsTreeNode * sTreeQgis