QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsfavoritesitem.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfavoritesitem.cpp
3 -------------------
4 begin : 2011-04-01
5 copyright : (C) 2011 Radim Blazek
6 email : radim dot blazek at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsfavoritesitem.h"
19
20#include "qgsapplication.h"
21#include "qgsdataitemprovider.h"
23#include "qgslogger.h"
24#include "qgssettings.h"
25
26#include "moc_qgsfavoritesitem.cpp"
27
28//
29// QgsFavoritesItem
30//
31
33 : QgsDataCollectionItem( parent, name, QStringLiteral( "favorites:" ), QStringLiteral( "special:Favorites" ) )
34{
35 Q_UNUSED( path )
38 mIconName = QStringLiteral( "/mIconFavorites.svg" );
39 populate();
40}
41
42QVector<QgsDataItem *> QgsFavoritesItem::createChildren()
43{
44 QVector<QgsDataItem *> children;
45
46 const QgsSettings settings;
47
48 const QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ), QVariant() ).toStringList();
49
50 children.reserve( favDirs.size() );
51 for ( const QString &favDir : favDirs )
52 {
53 const QStringList parts = favDir.split( QStringLiteral( "|||" ) );
54 if ( parts.empty() )
55 continue;
56
57 const QString dir = parts.at( 0 );
58 QString name = dir;
59 if ( parts.count() > 1 )
60 name = parts.at( 1 );
61
62 children << createChildren( dir, name );
63 }
64
65 return children;
66}
67
68void QgsFavoritesItem::addDirectory( const QString &favDir, const QString &n )
69{
70 const QString name = n.isEmpty() ? favDir : n;
71
72 QgsSettings settings;
73 QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
74 favDirs.append( QStringLiteral( "%1|||%2" ).arg( favDir, name ) );
75 settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
76
78 {
79 const QVector<QgsDataItem *> items = createChildren( favDir, name );
80 for ( QgsDataItem *item : items )
81 {
82 addChildItem( item, true );
83 }
84 }
85}
86
88{
89 if ( !item )
90 return;
91
92 QgsSettings settings;
93 QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
94 for ( int i = favDirs.count() - 1; i >= 0; --i )
95 {
96 const QStringList parts = favDirs.at( i ).split( QStringLiteral( "|||" ) );
97 if ( parts.empty() )
98 continue;
99
100 const QString dir = parts.at( 0 );
101 if ( dir == item->dirPath() )
102 favDirs.removeAt( i );
103 }
104 settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
105
106 const int idx = findItem( mChildren, item );
107 if ( idx < 0 )
108 {
109 QgsDebugError( QStringLiteral( "favorites item %1 not found" ).arg( item->path() ) );
110 return;
111 }
112
114 deleteChildItem( mChildren.at( idx ) );
115}
116
117void QgsFavoritesItem::renameFavorite( const QString &path, const QString &name )
118{
119 // update stored name
120 QgsSettings settings;
121 QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
122 for ( int i = 0; i < favDirs.count(); ++i )
123 {
124 const QStringList parts = favDirs.at( i ).split( QStringLiteral( "|||" ) );
125 if ( parts.empty() )
126 continue;
127
128 const QString dir = parts.at( 0 );
129 if ( dir == path )
130 {
131 const QStringList newParts { path, name };
132 favDirs[i] = newParts.join( QLatin1String( "|||" ) );
133 break;
134 }
135 }
136 settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
137
138 // also update existing data item
139 const QVector<QgsDataItem *> ch = children();
140 for ( QgsDataItem *child : ch )
141 {
142 if ( QgsFavoriteItem *favorite = qobject_cast< QgsFavoriteItem * >( child ) )
143 {
144 if ( favorite->dirPath() == path )
145 {
146 favorite->setName( name );
147 break;
148 }
149 }
150 }
151}
152
154{
155 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconFavorites.svg" ) );
156}
157
159{
160 return QStringLiteral( " 0" );
161}
162
163QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &directory, const QString &name )
164{
165 const QString pathName = pathComponent( directory );
166 const QList<QgsDataItemProvider *> providers = QgsApplication::dataItemProviderRegistry()->providers();
167
168 QVector<QgsDataItem *> children;
169 children.reserve( providers.size() );
170 for ( QgsDataItemProvider *provider : providers )
171 {
172 if ( provider->capabilities() & Qgis::DataItemProviderCapability::Directories )
173 {
174 if ( QgsDataItem *item = provider->createDataItem( directory, this ) )
175 {
176 item->setName( name );
177 children.append( item );
178 }
179 }
180 }
181 if ( children.isEmpty() )
182 {
183 children.append( new QgsFavoriteItem( this, name, directory, mPath + '/' + pathName ) );
184 }
185 return children;
186}
187
188//
189// QgsFavoriteItem
190//
191
192QgsFavoriteItem::QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path )
193 : QgsDirectoryItem( parent, name, dirPath, path, QStringLiteral( "special:Favorites" ) )
194 , mFavorites( parent )
195{
197}
198
199bool QgsFavoriteItem::rename( const QString &name )
200{
201 mFavorites->renameFavorite( dirPath(), name );
202 return true;
203}
204
205
@ Directories
Can provides items which corresponds to directories.
Definition qgis.h:979
@ Populated
Children created.
Definition qgis.h:941
@ Rename
Item can be renamed.
Definition qgis.h:957
@ Fast
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,...
Definition qgis.h:955
@ Favorites
Represents a favorite item.
Definition qgis.h:924
static QgsDataItemProviderRegistry * dataItemProviderRegistry()
Returns the application's data item provider registry, which keeps a list of data item providers that...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsDataCollectionItem(QgsDataItem *parent, const QString &name, const QString &path=QString(), const QString &providerKey=QString())
Constructor for QgsDataCollectionItem, with the specified parent item.
QList< QgsDataItemProvider * > providers() const
Returns the list of available providers.
Interface for providers that add custom data items to the browser tree.
Base class for all items in the model.
Definition qgsdataitem.h:47
static int findItem(QVector< QgsDataItem * > items, QgsDataItem *item)
Qgis::BrowserItemType mType
virtual void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
QVector< QgsDataItem * > mChildren
QString mPath
QVector< QgsDataItem * > children() const
Qgis::BrowserItemCapabilities mCapabilities
QgsDataItem(Qgis::BrowserItemType type, QgsDataItem *parent, const QString &name, const QString &path, const QString &providerKey=QString())
Constructor for QgsDataItem, with the specified parent item.
QString mIconName
Qgis::BrowserItemState state() const
static QString pathComponent(const QString &component)
Create path component replacing path separators.
QString name() const
Returns the name of the item (the displayed text for the item).
QString path() const
virtual void addChildItem(QgsDataItem *child, bool refresh=false)
Inserts a new child item.
QgsDataItem * parent() const
Gets item parent.
virtual void populate(const QVector< QgsDataItem * > &children)
A browser item for directories: contains subdirectories and layers.
QString dirPath() const
Returns the full path to the directory the item represents.
QgsDirectoryItem(QgsDataItem *parent, const QString &name, const QString &path)
Constructor for QgsDirectoryItem, with the specified parent item.
A directory item showing a single favorite directory.
QgsFavoriteItem(QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path)
Constructor for QgsFavoriteItem.
bool rename(const QString &name) override
Sets a new name for the favorite.
A browser item which contains various Favorites directories.
void addDirectory(const QString &directory, const QString &name=QString())
Adds a new directory to the favorites group.
QVariant sortKey() const override
Returns the sorting key for the item.
void removeDirectory(QgsDirectoryItem *item)
Removes an existing directory from the favorites group.
static QIcon iconFavorites()
Icon for favorites group.
QVector< QgsDataItem * > createChildren() override
Create children.
QgsFavoritesItem(QgsDataItem *parent, const QString &name, const QString &path=QString())
Constructor for QgsFavoritesItem.
void renameFavorite(const QString &path, const QString &name)
Renames the stored favorite with corresponding path a new name.
Stores settings for use within QGIS.
Definition qgssettings.h:65
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define QgsDebugError(str)
Definition qgslogger.h:57