QGIS API Documentation 3.29.0-Master (19d7edcfed)
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
16#include <QDir>
17
19#include "qgssettings.h"
20#include "qgis.h"
21#include "qgsreadwritelocker.h"
22
23
25
27{
28 readFromSettings();
29}
30
31QString QgsLocalizedDataPathRegistry::globalPath( const QString &relativePath ) const
32{
34
35 for ( const QDir &basePath : std::as_const( mPaths ) )
36 if ( basePath.exists( relativePath ) )
37 return basePath.absoluteFilePath( relativePath );
38
39 return QString();
40}
41
42QString QgsLocalizedDataPathRegistry::localizedPath( const QString &fullPath ) const
43{
45
46 for ( const QDir &basePath : std::as_const( mPaths ) )
47 if ( fullPath.startsWith( basePath.absolutePath() ) )
48 return basePath.relativeFilePath( fullPath );
49
50 return QString();
51
52}
53
55{
57
58 QStringList paths;
59 for ( const QDir &dir : mPaths )
60 paths << dir.absolutePath();
61 return paths;
62}
63
64void QgsLocalizedDataPathRegistry::setPaths( const QStringList &paths )
65{
67
68 mPaths.clear();
69 for ( const QString &path : paths )
70 {
71 QDir dir( path );
72 if ( !mPaths.contains( dir ) )
73 mPaths << dir;
74 }
75
76 locker.unlock();
77 writeToSettings();
78}
79
80void QgsLocalizedDataPathRegistry::registerPath( const QString &path, int position )
81{
83
84 QDir dir( path );
85 if ( mPaths.contains( dir ) )
86 return;
87
89
90 if ( position >= 0 && position < mPaths.count() )
91 mPaths.insert( position, dir );
92 else
93 mPaths.append( dir );
94
95 locker.unlock();
96 writeToSettings();
97}
98
100{
102
103 mPaths.removeAll( QDir( path ) );
104 locker.unlock();
105 writeToSettings();
106}
107
108void QgsLocalizedDataPathRegistry::readFromSettings()
109{
111}
112
113void QgsLocalizedDataPathRegistry::writeToSettings() const
114{
116}
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.
The QgsReadWriteLocker class is a convenience class that simplifies locking and unlocking QReadWriteL...
@ Write
Lock for write.
@ Read
Lock for read.
void unlock()
Unlocks the lock.
void changeMode(Mode mode)
Change the mode of the lock to mode.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
A string list settings entry.
static QgsSettingsTreeNode * sTreeQgis
Definition: qgssettings.h:105