QGIS API Documentation 4.1.0-Master (8da4c00a38b)
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"
25
26#include <QString>
27
28#include "moc_qgsfavoritesitem.cpp"
29
30using namespace Qt::StringLiterals;
31
33 = new QgsSettingsEntryStringList( u"favourites"_s, QgsFavoritesItem::sTreeBrowser, QStringList(), u"List of favorite directories in the browser"_s );
34
35//
36// QgsFavoritesItem
37//
38
40 : QgsDataCollectionItem( parent, name, u"favorites:"_s, u"special:Favorites"_s )
41{
42 Q_UNUSED( path )
45 mIconName = u"/mIconFavorites.svg"_s;
46 populate();
47}
48
49QVector<QgsDataItem *> QgsFavoritesItem::createChildren()
50{
51 QVector<QgsDataItem *> children;
52
53 const QStringList favDirs = settingsFavoriteDirs->value();
54
55 children.reserve( favDirs.size() );
56 for ( const QString &favDir : favDirs )
57 {
58 const QStringList parts = favDir.split( u"|||"_s );
59 if ( parts.empty() )
60 continue;
61
62 const QString dir = parts.at( 0 );
63 QString name = dir;
64 if ( parts.count() > 1 )
65 name = parts.at( 1 );
66
67 children << createChildren( dir, name );
68 }
69
70 return children;
71}
72
73void QgsFavoritesItem::addDirectory( const QString &favDir, const QString &n )
74{
75 const QString name = n.isEmpty() ? favDir : n;
76
77 QStringList favDirs = settingsFavoriteDirs->value();
78 favDirs.append( u"%1|||%2"_s.arg( favDir, name ) );
79 settingsFavoriteDirs->setValue( favDirs );
80
82 {
83 const QVector<QgsDataItem *> items = createChildren( favDir, name );
84 for ( QgsDataItem *item : items )
85 {
86 addChildItem( item, true );
87 }
88 }
89}
90
92{
93 if ( !item )
94 return;
95
96 QStringList favDirs = settingsFavoriteDirs->value();
97 for ( int i = favDirs.count() - 1; i >= 0; --i )
98 {
99 const QStringList parts = favDirs.at( i ).split( u"|||"_s );
100 if ( parts.empty() )
101 continue;
102
103 const QString dir = parts.at( 0 );
104 if ( dir == item->dirPath() )
105 favDirs.removeAt( i );
106 }
107 settingsFavoriteDirs->setValue( favDirs );
108
109 const int idx = findItem( mChildren, item );
110 if ( idx < 0 )
111 {
112 QgsDebugError( u"favorites item %1 not found"_s.arg( item->path() ) );
113 return;
114 }
115
117 deleteChildItem( mChildren.at( idx ) );
118}
119
120void QgsFavoritesItem::renameFavorite( const QString &path, const QString &name )
121{
122 // update stored name
123 QStringList favDirs = settingsFavoriteDirs->value();
124 for ( int i = 0; i < favDirs.count(); ++i )
125 {
126 const QStringList parts = favDirs.at( i ).split( u"|||"_s );
127 if ( parts.empty() )
128 continue;
129
130 const QString dir = parts.at( 0 );
131 if ( dir == path )
132 {
133 const QStringList newParts { path, name };
134 favDirs[i] = newParts.join( "|||"_L1 );
135 break;
136 }
137 }
138 settingsFavoriteDirs->setValue( favDirs );
139
140 // also update existing data item
141 const QVector<QgsDataItem *> ch = children();
142 for ( QgsDataItem *child : ch )
143 {
144 if ( QgsFavoriteItem *favorite = qobject_cast< QgsFavoriteItem * >( child ) )
145 {
146 if ( favorite->dirPath() == path )
147 {
148 favorite->setName( name );
149 break;
150 }
151 }
152 }
153}
154
156{
157 return QgsApplication::getThemeIcon( u"/mIconFavorites.svg"_s );
158}
159
161{
162 return u" 0"_s;
163}
164
165QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &directory, const QString &name )
166{
167 const QString pathName = pathComponent( directory );
168 const QList<QgsDataItemProvider *> providers = QgsApplication::dataItemProviderRegistry()->providers();
169
170 QVector<QgsDataItem *> children;
171 children.reserve( providers.size() );
172 for ( QgsDataItemProvider *provider : providers )
173 {
174 if ( provider->capabilities() & Qgis::DataItemProviderCapability::Directories )
175 {
176 if ( QgsDataItem *item = provider->createDataItem( directory, this ) )
177 {
178 item->setName( name );
179 children.append( item );
180 }
181 }
182 }
183 if ( children.isEmpty() )
184 {
185 children.append( new QgsFavoriteItem( this, name, directory, mPath + '/' + pathName ) );
186 }
187 return children;
188}
189
190//
191// QgsFavoriteItem
192//
193
194QgsFavoriteItem::QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path )
195 : QgsDirectoryItem( parent, name, dirPath, path, u"special:Favorites"_s )
196 , mFavorites( parent )
197{
199}
200
201bool QgsFavoriteItem::rename( const QString &name )
202{
203 mFavorites->renameFavorite( dirPath(), name );
204 return true;
205}
@ Directories
Can provides items which corresponds to directories.
Definition qgis.h:1046
@ Populated
Children created.
Definition qgis.h:989
@ Rename
Item can be renamed.
Definition qgis.h:1005
@ Fast
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,...
Definition qgis.h:1003
@ Favorites
Represents a favorite item.
Definition qgis.h:972
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:50
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.
static QgsSettingsTreeNode * sTreeBrowser
Settings tree node for browser settings.
static const QgsSettingsEntryStringList * settingsFavoriteDirs
Settings entry for favorite directories.
A string list settings entry.
#define QgsDebugError(str)
Definition qgslogger.h:59