QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
58{
60
61 QStringList paths;
62 for ( const QDir &dir : mPaths )
63 paths << dir.absolutePath();
64 return paths;
65}
66
68{
70
71 mPaths.clear();
72 for ( const QString &path : paths )
73 {
74 QDir dir( path );
75 if ( !mPaths.contains( dir ) )
76 mPaths << dir;
77 }
78
79 locker.unlock();
80 writeToSettings();
81}
82
83void QgsLocalizedDataPathRegistry::registerPath( const QString &path, int position )
84{
86
87 QDir dir( path );
88 if ( mPaths.contains( dir ) )
89 return;
90
92
93 if ( position >= 0 && position < mPaths.count() )
94 mPaths.insert( position, dir );
95 else
96 mPaths.append( dir );
97
98 locker.unlock();
99 writeToSettings();
100}
101
103{
105
106 mPaths.removeAll( QDir( path ) );
107 locker.unlock();
108 writeToSettings();
109}
110
111void QgsLocalizedDataPathRegistry::readFromSettings()
112{
114}
115
116void QgsLocalizedDataPathRegistry::writeToSettings() const
117{
118 settingsLocalizedDataPaths->setValue( paths() );
119}
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