QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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#include "moc_qgsfavoritesitem.cpp"
20#include "qgssettings.h"
21#include "qgslogger.h"
22#include "qgsapplication.h"
24#include "qgsdataitemprovider.h"
25
26//
27// QgsFavoritesItem
28//
29
30QgsFavoritesItem::QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path )
31 : QgsDataCollectionItem( parent, name, QStringLiteral( "favorites:" ), QStringLiteral( "special:Favorites" ) )
32{
33 Q_UNUSED( path )
36 mIconName = QStringLiteral( "/mIconFavorites.svg" );
37 populate();
38}
39
40QVector<QgsDataItem *> QgsFavoritesItem::createChildren()
41{
42 QVector<QgsDataItem *> children;
43
44 const QgsSettings settings;
45
46 const QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ), QVariant() ).toStringList();
47
48 children.reserve( favDirs.size() );
49 for ( const QString &favDir : favDirs )
50 {
51 const QStringList parts = favDir.split( QStringLiteral( "|||" ) );
52 if ( parts.empty() )
53 continue;
54
55 const QString dir = parts.at( 0 );
56 QString name = dir;
57 if ( parts.count() > 1 )
58 name = parts.at( 1 );
59
60 children << createChildren( dir, name );
61 }
62
63 return children;
64}
65
66void QgsFavoritesItem::addDirectory( const QString &favDir, const QString &n )
67{
68 const QString name = n.isEmpty() ? favDir : n;
69
70 QgsSettings settings;
71 QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
72 favDirs.append( QStringLiteral( "%1|||%2" ).arg( favDir, name ) );
73 settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
74
76 {
77 const QVector<QgsDataItem *> items = createChildren( favDir, name );
78 for ( QgsDataItem *item : items )
79 {
80 addChildItem( item, true );
81 }
82 }
83}
84
86{
87 if ( !item )
88 return;
89
90 QgsSettings settings;
91 QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
92 for ( int i = favDirs.count() - 1; i >= 0; --i )
93 {
94 const QStringList parts = favDirs.at( i ).split( QStringLiteral( "|||" ) );
95 if ( parts.empty() )
96 continue;
97
98 const QString dir = parts.at( 0 );
99 if ( dir == item->dirPath() )
100 favDirs.removeAt( i );
101 }
102 settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
103
104 const int idx = findItem( mChildren, item );
105 if ( idx < 0 )
106 {
107 QgsDebugError( QStringLiteral( "favorites item %1 not found" ).arg( item->path() ) );
108 return;
109 }
110
112 deleteChildItem( mChildren.at( idx ) );
113}
114
115void QgsFavoritesItem::renameFavorite( const QString &path, const QString &name )
116{
117 // update stored name
118 QgsSettings settings;
119 QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
120 for ( int i = 0; i < favDirs.count(); ++i )
121 {
122 const QStringList parts = favDirs.at( i ).split( QStringLiteral( "|||" ) );
123 if ( parts.empty() )
124 continue;
125
126 const QString dir = parts.at( 0 );
127 if ( dir == path )
128 {
129 const QStringList newParts { path, name };
130 favDirs[i] = newParts.join( QLatin1String( "|||" ) );
131 break;
132 }
133 }
134 settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
135
136 // also update existing data item
137 const QVector<QgsDataItem *> ch = children();
138 for ( QgsDataItem *child : ch )
139 {
140 if ( QgsFavoriteItem *favorite = qobject_cast< QgsFavoriteItem * >( child ) )
141 {
142 if ( favorite->dirPath() == path )
143 {
144 favorite->setName( name );
145 break;
146 }
147 }
148 }
149}
150
152{
153 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconFavorites.svg" ) );
154}
155
157{
158 return QStringLiteral( " 0" );
159}
160
161QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &directory, const QString &name )
162{
163 const QString pathName = pathComponent( directory );
164 const QList<QgsDataItemProvider *> providers = QgsApplication::dataItemProviderRegistry()->providers();
165
166 QVector<QgsDataItem *> children;
167 children.reserve( providers.size() );
168 for ( QgsDataItemProvider *provider : providers )
169 {
170 if ( provider->capabilities() & Qgis::DataItemProviderCapability::Directories )
171 {
172 if ( QgsDataItem *item = provider->createDataItem( directory, this ) )
173 {
174 item->setName( name );
175 children.append( item );
176 }
177 }
178 }
179 if ( children.isEmpty() )
180 {
181 children.append( new QgsFavoriteItem( this, name, directory, mPath + '/' + pathName ) );
182 }
183 return children;
184}
185
186//
187// QgsFavoriteItem
188//
189
190QgsFavoriteItem::QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path )
191 : QgsDirectoryItem( parent, name, dirPath, path, QStringLiteral( "special:Favorites" ) )
192 , mFavorites( parent )
193{
195}
196
197bool QgsFavoriteItem::rename( const QString &name )
198{
199 mFavorites->renameFavorite( dirPath(), name );
200 return true;
201}
202
203
@ Directories
Can provides items which corresponds to directories.
@ Populated
Children created.
@ Rename
Item can be renamed.
@ Fast
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,...
@ Favorites
Represents a favorite item.
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.
A Collection: logical collection of layers or subcollections, e.g.
QList< QgsDataItemProvider * > providers() const
Returns the list of available providers.
This is the interface for those who want to add custom data items to the browser tree.
Base class for all items in the model.
Definition qgsdataitem.h:46
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
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.
virtual void populate(const QVector< QgsDataItem * > &children)
A directory: contains subdirectories and layers.
QString dirPath() const
Returns the full path to the directory the item represents.
A directory item showing the 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.
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.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
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:38