QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsdataprovider.h"
25 
26 //
27 // QgsFavoritesItem
28 //
29 
30 QgsFavoritesItem::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 
40 QVector<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 
66 void 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  QgsDebugMsg( QStringLiteral( "favorites item %1 not found" ).arg( item->path() ) );
108  return;
109  }
110 
112  deleteChildItem( mChildren.at( idx ) );
113 }
114 
115 void 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 
161 QVector<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() & QgsDataProvider::Dir )
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 
190 QgsFavoriteItem::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 
197 bool QgsFavoriteItem::rename( const QString &name )
198 {
199  mFavorites->renameFavorite( dirPath(), name );
200  return true;
201 }
202 
203 
QgsDataCollectionItem
A Collection: logical collection of layers or subcollections, e.g. GRASS location/mapset,...
Definition: qgsdatacollectionitem.h:28
QgsDataItem::path
QString path() const
Definition: qgsdataitem.h:354
qgsdataitemproviderregistry.h
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:161
QgsFavoritesItem::renameFavorite
void renameFavorite(const QString &path, const QString &name)
Renames the stored favorite with corresponding path a new name.
Definition: qgsfavoritesitem.cpp:115
QgsFavoritesItem::iconFavorites
static QIcon iconFavorites()
Icon for favorites group.
Definition: qgsfavoritesitem.cpp:151
QgsDataItem::state
Qgis::BrowserItemState state() const
Definition: qgsdataitem.cpp:552
QgsFavoritesItem::removeDirectory
void removeDirectory(QgsDirectoryItem *item)
Removes an existing directory from the favorites group.
Definition: qgsfavoritesitem.cpp:85
QgsDataItem::name
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:345
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsDataItem::mIconName
QString mIconName
Definition: qgsdataitem.h:457
QgsDataItem::mCapabilities
Qgis::BrowserItemCapabilities mCapabilities
Definition: qgsdataitem.h:445
QgsFavoritesItem::addDirectory
void addDirectory(const QString &directory, const QString &name=QString())
Adds a new directory to the favorites group.
Definition: qgsfavoritesitem.cpp:66
QgsFavoritesItem::createChildren
QVector< QgsDataItem * > createChildren() override
Create children.
Definition: qgsfavoritesitem.cpp:40
QgsDataItem::pathComponent
static QString pathComponent(const QString &component)
Create path component replacing path separators.
Definition: qgsdataitem.cpp:90
QgsDirectoryItem::dirPath
QString dirPath() const
Returns the full path to the directory the item represents.
Definition: qgsdirectoryitem.h:86
qgsapplication.h
QgsDataItem::mType
Qgis::BrowserItemType mType
Definition: qgsdataitem.h:444
QgsFavoriteItem::rename
bool rename(const QString &name) override
Sets a new name for the favorite.
Definition: qgsfavoritesitem.cpp:197
QgsDirectoryItem
A directory: contains subdirectories and layers.
Definition: qgsdirectoryitem.h:33
QgsDataItemProviderRegistry::providers
QList< QgsDataItemProvider * > providers() const
Returns the list of available providers.
Definition: qgsdataitemproviderregistry.cpp:51
QgsDataItemProvider
This is the interface for those who want to add custom data items to the browser tree.
Definition: qgsdataitemprovider.h:44
Qgis::BrowserItemState::Populated
@ Populated
Children created.
QgsApplication::dataItemProviderRegistry
static QgsDataItemProviderRegistry * dataItemProviderRegistry()
Returns the application's data item provider registry, which keeps a list of data item providers that...
Definition: qgsapplication.cpp:2335
qgsfavoritesitem.h
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:279
Qgis::BrowserItemType::Favorites
@ Favorites
Represents a favorite item.
QgsFavoriteItem
A directory item showing the a single favorite directory.
Definition: qgsfavoritesitem.h:92
qgsdataitemprovider.h
QgsDataItem::addChildItem
virtual void addChildItem(QgsDataItem *child, bool refresh=false)
Inserts a new child item.
Definition: qgsdataitem.cpp:425
QgsFavoritesItem::QgsFavoritesItem
QgsFavoritesItem(QgsDataItem *parent, const QString &name, const QString &path=QString())
Constructor for QgsFavoritesItem.
Definition: qgsfavoritesitem.cpp:30
QgsDataProvider::Dir
@ Dir
Definition: qgsdataprovider.h:80
QgsDataItem::mPath
QString mPath
Definition: qgsdataitem.h:455
QgsDataItem::populate
virtual void populate(const QVector< QgsDataItem * > &children)
Definition: qgsdataitem.cpp:264
qgssettings.h
QgsDataItem::mChildren
QVector< QgsDataItem * > mChildren
Definition: qgsdataitem.h:447
QgsDataItem::children
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:337
qgsdataprovider.h
Qgis::BrowserItemCapability::Rename
@ Rename
Item can be renamed.
QgsDataItem::findItem
static int findItem(QVector< QgsDataItem * > items, QgsDataItem *item)
Definition: qgsdataitem.cpp:489
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Definition: qgsapplication.cpp:693
qgslogger.h
Qgis::BrowserItemCapability::Fast
@ Fast
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,...
QgsFavoritesItem
Contains various Favorites directories.
Definition: qgsfavoritesitem.h:32
QgsFavoritesItem::sortKey
QVariant sortKey() const override
Returns the sorting key for the item.
Definition: qgsfavoritesitem.cpp:156
QgsDataItem
Base class for all items in the model.
Definition: qgsdataitem.h:45
QgsDataItem::deleteChildItem
virtual void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
Definition: qgsdataitem.cpp:461
QgsFavoriteItem::QgsFavoriteItem
QgsFavoriteItem(QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path)
Constructor for QgsFavoriteItem.
Definition: qgsfavoritesitem.cpp:190