QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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 "qgssettings.h"
20#include "qgslogger.h"
21#include "qgsapplication.h"
23#include "qgsdataitemprovider.h"
24
25//
26// QgsFavoritesItem
27//
28
29QgsFavoritesItem::QgsFavoritesItem( QgsDataItem *parent, const QString &name, const QString &path )
30 : QgsDataCollectionItem( parent, name, QStringLiteral( "favorites:" ), QStringLiteral( "special:Favorites" ) )
31{
32 Q_UNUSED( path )
35 mIconName = QStringLiteral( "/mIconFavorites.svg" );
36 populate();
37}
38
39QVector<QgsDataItem *> QgsFavoritesItem::createChildren()
40{
41 QVector<QgsDataItem *> children;
42
43 const QgsSettings settings;
44
45 const QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ), QVariant() ).toStringList();
46
47 children.reserve( favDirs.size() );
48 for ( const QString &favDir : favDirs )
49 {
50 const QStringList parts = favDir.split( QStringLiteral( "|||" ) );
51 if ( parts.empty() )
52 continue;
53
54 const QString dir = parts.at( 0 );
55 QString name = dir;
56 if ( parts.count() > 1 )
57 name = parts.at( 1 );
58
59 children << createChildren( dir, name );
60 }
61
62 return children;
63}
64
65void QgsFavoritesItem::addDirectory( const QString &favDir, const QString &n )
66{
67 const QString name = n.isEmpty() ? favDir : n;
68
69 QgsSettings settings;
70 QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
71 favDirs.append( QStringLiteral( "%1|||%2" ).arg( favDir, name ) );
72 settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
73
75 {
76 const QVector<QgsDataItem *> items = createChildren( favDir, name );
77 for ( QgsDataItem *item : items )
78 {
79 addChildItem( item, true );
80 }
81 }
82}
83
85{
86 if ( !item )
87 return;
88
89 QgsSettings settings;
90 QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
91 for ( int i = favDirs.count() - 1; i >= 0; --i )
92 {
93 const QStringList parts = favDirs.at( i ).split( QStringLiteral( "|||" ) );
94 if ( parts.empty() )
95 continue;
96
97 const QString dir = parts.at( 0 );
98 if ( dir == item->dirPath() )
99 favDirs.removeAt( i );
100 }
101 settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
102
103 const int idx = findItem( mChildren, item );
104 if ( idx < 0 )
105 {
106 QgsDebugError( QStringLiteral( "favorites item %1 not found" ).arg( item->path() ) );
107 return;
108 }
109
111 deleteChildItem( mChildren.at( idx ) );
112}
113
114void QgsFavoritesItem::renameFavorite( const QString &path, const QString &name )
115{
116 // update stored name
117 QgsSettings settings;
118 QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
119 for ( int i = 0; i < favDirs.count(); ++i )
120 {
121 const QStringList parts = favDirs.at( i ).split( QStringLiteral( "|||" ) );
122 if ( parts.empty() )
123 continue;
124
125 const QString dir = parts.at( 0 );
126 if ( dir == path )
127 {
128 const QStringList newParts { path, name };
129 favDirs[i] = newParts.join( QLatin1String( "|||" ) );
130 break;
131 }
132 }
133 settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
134
135 // also update existing data item
136 const QVector<QgsDataItem *> ch = children();
137 for ( QgsDataItem *child : ch )
138 {
139 if ( QgsFavoriteItem *favorite = qobject_cast< QgsFavoriteItem * >( child ) )
140 {
141 if ( favorite->dirPath() == path )
142 {
143 favorite->setName( name );
144 break;
145 }
146 }
147 }
148}
149
151{
152 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconFavorites.svg" ) );
153}
154
156{
157 return QStringLiteral( " 0" );
158}
159
160QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &directory, const QString &name )
161{
162 const QString pathName = pathComponent( directory );
163 const QList<QgsDataItemProvider *> providers = QgsApplication::dataItemProviderRegistry()->providers();
164
165 QVector<QgsDataItem *> children;
166 children.reserve( providers.size() );
167 for ( QgsDataItemProvider *provider : providers )
168 {
169 if ( provider->capabilities() & Qgis::DataItemProviderCapability::Directories )
170 {
171 if ( QgsDataItem *item = provider->createDataItem( directory, this ) )
172 {
173 item->setName( name );
174 children.append( item );
175 }
176 }
177 }
178 if ( children.isEmpty() )
179 {
180 children.append( new QgsFavoriteItem( this, name, directory, mPath + '/' + pathName ) );
181 }
182 return children;
183}
184
185//
186// QgsFavoriteItem
187//
188
189QgsFavoriteItem::QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path )
190 : QgsDirectoryItem( parent, name, dirPath, path, QStringLiteral( "special:Favorites" ) )
191 , mFavorites( parent )
192{
194}
195
196bool QgsFavoriteItem::rename( const QString &name )
197{
198 mFavorites->renameFavorite( dirPath(), name );
199 return true;
200}
201
202
@ 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