QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
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 "
qgslocalizeddatapathregistry.h
"
17
18
#include "
qgis.h
"
19
#include "
qgsreadwritelocker.h
"
20
#include "
qgssettingsentryimpl.h
"
21
#include "
qgssettingstree.h
"
22
23
#include <QDir>
24
#include <QString>
25
26
using namespace
Qt::StringLiterals;
27
28
const
QgsSettingsEntryStringList
*
QgsLocalizedDataPathRegistry::settingsLocalizedDataPaths
=
new
QgsSettingsEntryStringList
( u
"localized_data_paths"
_s,
QgsSettingsTree::sTreeQgis
, QStringList() );
29
30
QgsLocalizedDataPathRegistry::QgsLocalizedDataPathRegistry
()
31
{
32
readFromSettings();
33
}
34
35
QString
QgsLocalizedDataPathRegistry::globalPath
(
const
QString &relativePath )
const
36
{
37
QgsReadWriteLocker
locker( mLock,
QgsReadWriteLocker::Read
);
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
46
QString
QgsLocalizedDataPathRegistry::localizedPath
(
const
QString &fullPath )
const
47
{
48
QgsReadWriteLocker
locker( mLock,
QgsReadWriteLocker::Read
);
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
QStringList
QgsLocalizedDataPathRegistry::paths
()
const
58
{
59
QgsReadWriteLocker
locker( mLock,
QgsReadWriteLocker::Read
);
60
61
QStringList
paths
;
62
for
(
const
QDir &dir : mPaths )
63
paths
<< dir.absolutePath();
64
return
paths
;
65
}
66
67
void
QgsLocalizedDataPathRegistry::setPaths
(
const
QStringList &
paths
)
68
{
69
QgsReadWriteLocker
locker( mLock,
QgsReadWriteLocker::Write
);
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
83
void
QgsLocalizedDataPathRegistry::registerPath
(
const
QString &path,
int
position )
84
{
85
QgsReadWriteLocker
locker( mLock,
QgsReadWriteLocker::Read
);
86
87
QDir dir( path );
88
if
( mPaths.contains( dir ) )
89
return
;
90
91
locker.
changeMode
(
QgsReadWriteLocker::Write
);
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
102
void
QgsLocalizedDataPathRegistry::unregisterPath
(
const
QString &path )
103
{
104
QgsReadWriteLocker
locker( mLock,
QgsReadWriteLocker::Write
);
105
106
mPaths.removeAll( QDir( path ) );
107
locker.
unlock
();
108
writeToSettings();
109
}
110
111
void
QgsLocalizedDataPathRegistry::readFromSettings()
112
{
113
setPaths
(
settingsLocalizedDataPaths
->
value
() );
114
}
115
116
void
QgsLocalizedDataPathRegistry::writeToSettings()
const
117
{
118
settingsLocalizedDataPaths
->setValue(
paths
() );
119
}
QgsLocalizedDataPathRegistry::globalPath
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.
Definition
qgslocalizeddatapathregistry.cpp:35
QgsLocalizedDataPathRegistry::setPaths
void setPaths(const QStringList &paths)
Sets the complete list of localized path.
Definition
qgslocalizeddatapathregistry.cpp:67
QgsLocalizedDataPathRegistry::unregisterPath
void unregisterPath(const QString &path)
Unregisters a localized path.
Definition
qgslocalizeddatapathregistry.cpp:102
QgsLocalizedDataPathRegistry::QgsLocalizedDataPathRegistry
QgsLocalizedDataPathRegistry()
Definition
qgslocalizeddatapathregistry.cpp:30
QgsLocalizedDataPathRegistry::localizedPath
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.
Definition
qgslocalizeddatapathregistry.cpp:46
QgsLocalizedDataPathRegistry::settingsLocalizedDataPaths
static const QgsSettingsEntryStringList * settingsLocalizedDataPaths
Settings entry localized data paths.
Definition
qgslocalizeddatapathregistry.h:73
QgsLocalizedDataPathRegistry::registerPath
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...
Definition
qgslocalizeddatapathregistry.cpp:83
QgsLocalizedDataPathRegistry::paths
QStringList paths() const
Returns a list of registered localized paths.
Definition
qgslocalizeddatapathregistry.cpp:57
QgsReadWriteLocker
A convenience class that simplifies locking and unlocking QReadWriteLocks.
Definition
qgsreadwritelocker.h:41
QgsReadWriteLocker::Write
@ Write
Lock for write.
Definition
qgsreadwritelocker.h:49
QgsReadWriteLocker::Read
@ Read
Lock for read.
Definition
qgsreadwritelocker.h:48
QgsReadWriteLocker::unlock
void unlock()
Unlocks the lock.
Definition
qgsreadwritelocker.cpp:45
QgsReadWriteLocker::changeMode
void changeMode(Mode mode)
Change the mode of the lock to mode.
Definition
qgsreadwritelocker.cpp:30
QgsSettingsEntryBaseTemplate::value
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
Definition
qgssettingsentry.h:449
QgsSettingsEntryStringList
A string list settings entry.
Definition
qgssettingsentryimpl.h:229
QgsSettingsTree::sTreeQgis
static QgsSettingsTreeNode * sTreeQgis
Definition
qgssettingstree.h:70
qgis.h
qgslocalizeddatapathregistry.h
qgsreadwritelocker.h
qgssettingsentryimpl.h
qgssettingstree.h
Generated on
for QGIS API Documentation by
1.17.0