Skip to content

Commit 8df6711

Browse files
committed
[needs-docs][browser] Allow renaming favorite items
Otherwise favorites can be useless if you're trying to favorite a long path Fixes #6780
1 parent 457abe7 commit 8df6711

File tree

9 files changed

+171
-28
lines changed

9 files changed

+171
-28
lines changed

python/core/qgsbrowsermodel.sip

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,13 @@ Reload the whole model
165165
void itemDataChanged( QgsDataItem *item );
166166
void itemStateChanged( QgsDataItem *item, QgsDataItem::State oldState );
167167

168-
void addFavoriteDirectory( const QString &directory );
168+
void addFavoriteDirectory( const QString &directory, const QString &name = QString() );
169169
%Docstring
170-
Adds a directory to the favorites group.
170+
Adds a ``directory`` to the favorites group.
171+
172+
If ``name`` is specified, it will be used as the favorite's name. Otherwise
173+
the name will be set to match ``directory``.
174+
171175
.. versionadded:: 3.0
172176
.. seealso:: :py:func:`removeFavorite()`
173177
%End

python/core/qgsdataitem.sip

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,13 @@ class QgsFavoritesItem : QgsDataCollectionItem
673673
virtual QVector<QgsDataItem *> createChildren();
674674

675675

676-
void addDirectory( const QString &directory );
676+
void addDirectory( const QString &directory, const QString &name = QString() );
677677
%Docstring
678-
Adds a new directory to the favorites group.
678+
Adds a new ``directory`` to the favorites group.
679+
680+
If ``name`` is specified, it will be used as the favorite's name. Otherwise
681+
the name will be set to match ``directory``.
682+
679683
.. seealso:: :py:func:`removeDirectory()`
680684
%End
681685

@@ -685,6 +689,11 @@ class QgsFavoritesItem : QgsDataCollectionItem
685689
.. seealso:: :py:func:`addDirectory()`
686690
%End
687691

692+
void renameFavorite( const QString &path, const QString &name );
693+
%Docstring
694+
Renames the stored favorite with corresponding ``path`` a new ``name``.
695+
%End
696+
688697
static QIcon iconFavorites();
689698
%Docstring
690699
Icon for favorites group

python/gui/qgsbrowserdockwidget.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class QgsBrowserDockWidget : QgsDockWidget
2929
\param parent parent widget
3030
%End
3131
~QgsBrowserDockWidget();
32-
void addFavoriteDirectory( const QString &favDir );
32+
void addFavoriteDirectory( const QString &favDir, const QString &name = QString() );
3333
%Docstring
3434
Add directory to favorites
3535
%End

src/core/qgsbrowsermodel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,10 @@ void QgsBrowserModel::refresh( const QModelIndex &index )
546546
item->refresh();
547547
}
548548

549-
void QgsBrowserModel::addFavoriteDirectory( const QString &directory )
549+
void QgsBrowserModel::addFavoriteDirectory( const QString &directory, const QString &name )
550550
{
551551
Q_ASSERT( mFavorites );
552-
mFavorites->addDirectory( directory );
552+
mFavorites->addDirectory( directory, name );
553553
}
554554

555555
void QgsBrowserModel::removeFavorite( const QModelIndex &index )

src/core/qgsbrowsermodel.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,15 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
164164
void itemStateChanged( QgsDataItem *item, QgsDataItem::State oldState );
165165

166166
/**
167-
* Adds a directory to the favorites group.
167+
* Adds a \a directory to the favorites group.
168+
*
169+
* If \a name is specified, it will be used as the favorite's name. Otherwise
170+
* the name will be set to match \a directory.
171+
*
168172
* \since QGIS 3.0
169173
* \see removeFavorite()
170174
*/
171-
void addFavoriteDirectory( const QString &directory );
175+
void addFavoriteDirectory( const QString &directory, const QString &name = QString() );
172176

173177
/**
174178
* Removes a favorite directory from its corresponding model index.

src/core/qgsdataitem.cpp

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,26 +1106,37 @@ QVector<QgsDataItem *> QgsFavoritesItem::createChildren()
11061106
QVector<QgsDataItem *> children;
11071107

11081108
QgsSettings settings;
1109-
QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ), QVariant() ).toStringList();
1109+
const QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ), QVariant() ).toStringList();
11101110

1111-
Q_FOREACH ( const QString &favDir, favDirs )
1111+
for ( const QString &favDir : favDirs )
11121112
{
1113-
children << createChildren( favDir );
1113+
QStringList parts = favDir.split( QStringLiteral( "|||" ) );
1114+
if ( parts.empty() )
1115+
continue;
1116+
1117+
QString dir = parts.at( 0 );
1118+
QString name = dir;
1119+
if ( parts.count() > 1 )
1120+
name = parts.at( 1 );
1121+
1122+
children << createChildren( dir, name );
11141123
}
11151124

11161125
return children;
11171126
}
11181127

1119-
void QgsFavoritesItem::addDirectory( const QString &favDir )
1128+
void QgsFavoritesItem::addDirectory( const QString &favDir, const QString &n )
11201129
{
1130+
QString name = n.isEmpty() ? favDir : n;
1131+
11211132
QgsSettings settings;
11221133
QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
1123-
favDirs.append( favDir );
1134+
favDirs.append( QStringLiteral( "%1|||%2" ).arg( favDir, name ) );
11241135
settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
11251136

11261137
if ( state() == Populated )
11271138
{
1128-
QVector<QgsDataItem *> items = createChildren( favDir );
1139+
QVector<QgsDataItem *> items = createChildren( favDir, name );
11291140
Q_FOREACH ( QgsDataItem *item, items )
11301141
{
11311142
addChildItem( item, true );
@@ -1140,7 +1151,16 @@ void QgsFavoritesItem::removeDirectory( QgsDirectoryItem *item )
11401151

11411152
QgsSettings settings;
11421153
QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
1143-
favDirs.removeAll( item->dirPath() );
1154+
for ( int i = favDirs.count() - 1; i >= 0; --i )
1155+
{
1156+
QStringList parts = favDirs.at( i ).split( QStringLiteral( "|||" ) );
1157+
if ( parts.empty() )
1158+
continue;
1159+
1160+
QString dir = parts.at( 0 );
1161+
if ( dir == item->dirPath() )
1162+
favDirs.removeAt( i );
1163+
}
11441164
settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
11451165

11461166
int idx = findItem( mChildren, item );
@@ -1154,7 +1174,42 @@ void QgsFavoritesItem::removeDirectory( QgsDirectoryItem *item )
11541174
deleteChildItem( mChildren.at( idx ) );
11551175
}
11561176

1157-
QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &favDir )
1177+
void QgsFavoritesItem::renameFavorite( const QString &path, const QString &name )
1178+
{
1179+
// update stored name
1180+
QgsSettings settings;
1181+
QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
1182+
for ( int i = 0; i < favDirs.count(); ++i )
1183+
{
1184+
QStringList parts = favDirs.at( i ).split( QStringLiteral( "|||" ) );
1185+
if ( parts.empty() )
1186+
continue;
1187+
1188+
QString dir = parts.at( 0 );
1189+
if ( dir == path )
1190+
{
1191+
favDirs[i] = QStringLiteral( "%1|||%2" ).arg( path, name );
1192+
break;
1193+
}
1194+
}
1195+
settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );
1196+
1197+
// also update existing data item
1198+
const QVector<QgsDataItem *> ch = children();
1199+
for ( QgsDataItem *child : ch )
1200+
{
1201+
if ( QgsFavoriteItem *favorite = qobject_cast< QgsFavoriteItem * >( child ) )
1202+
{
1203+
if ( favorite->dirPath() == path )
1204+
{
1205+
favorite->setName( name );
1206+
break;
1207+
}
1208+
}
1209+
}
1210+
}
1211+
1212+
QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &favDir, const QString &name )
11581213
{
11591214
QVector<QgsDataItem *> children;
11601215
QString pathName = pathComponent( favDir );
@@ -1167,14 +1222,14 @@ QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &favDir )
11671222
QgsDataItem *item = provider->createDataItem( favDir, this );
11681223
if ( item )
11691224
{
1170-
item->setName( favDir );
1225+
item->setName( name );
11711226
children.append( item );
11721227
}
11731228
}
11741229
}
11751230
if ( children.isEmpty() )
11761231
{
1177-
QgsDataItem *item = new QgsDirectoryItem( this, favDir, favDir, mPath + '/' + pathName );
1232+
QgsFavoriteItem *item = new QgsFavoriteItem( this, name, favDir, mPath + '/' + pathName );
11781233
if ( item )
11791234
{
11801235
children.append( item );
@@ -1558,4 +1613,18 @@ QVariant QgsProjectHomeItem::sortKey() const
15581613
return QStringLiteral( " 1" );
15591614
}
15601615

1616+
QgsFavoriteItem::QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path )
1617+
: QgsDirectoryItem( parent, name, dirPath, path )
1618+
, mFavorites( parent )
1619+
{
1620+
1621+
}
1622+
1623+
void QgsFavoriteItem::rename( const QString &name )
1624+
{
1625+
mFavorites->renameFavorite( dirPath(), name );
1626+
}
1627+
1628+
15611629
///@endcond
1630+

src/core/qgsdataitem.h

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,24 +640,33 @@ class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
640640
QVector<QgsDataItem *> createChildren() override;
641641

642642
/**
643-
* Adds a new directory to the favorites group.
643+
* Adds a new \a directory to the favorites group.
644+
*
645+
* If \a name is specified, it will be used as the favorite's name. Otherwise
646+
* the name will be set to match \a directory.
647+
*
644648
* \see removeDirectory()
645649
*/
646-
void addDirectory( const QString &directory );
650+
void addDirectory( const QString &directory, const QString &name = QString() );
647651

648652
/**
649653
* Removes an existing directory from the favorites group.
650654
* \see addDirectory()
651655
*/
652656
void removeDirectory( QgsDirectoryItem *item );
653657

658+
/**
659+
* Renames the stored favorite with corresponding \a path a new \a name.
660+
*/
661+
void renameFavorite( const QString &path, const QString &name );
662+
654663
//! Icon for favorites group
655664
static QIcon iconFavorites();
656665

657666
QVariant sortKey() const override;
658667

659668
private:
660-
QVector<QgsDataItem *> createChildren( const QString &favDir );
669+
QVector<QgsDataItem *> createChildren( const QString &favDir, const QString &name );
661670
};
662671

663672
/**
@@ -714,6 +723,8 @@ class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
714723
*/
715724
class CORE_EXPORT QgsProjectHomeItem : public QgsDirectoryItem
716725
{
726+
Q_OBJECT
727+
717728
public:
718729

719730
QgsProjectHomeItem( QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path );
@@ -722,6 +733,30 @@ class CORE_EXPORT QgsProjectHomeItem : public QgsDirectoryItem
722733
QVariant sortKey() const override;
723734

724735
};
736+
737+
/**
738+
* \ingroup core
739+
* A directory item showing the a single favorite directory.
740+
* \since QGIS 3.0
741+
*/
742+
class CORE_EXPORT QgsFavoriteItem : public QgsDirectoryItem
743+
{
744+
Q_OBJECT
745+
746+
public:
747+
748+
QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path );
749+
750+
/**
751+
* Sets a new \a name for the favorite, storing the new name permanently for the favorite.
752+
*/
753+
void rename( const QString &name );
754+
755+
private:
756+
757+
QgsFavoritesItem *mFavorites = nullptr;
758+
};
759+
725760
#endif
726761
///@endcond
727762

src/gui/qgsbrowserdockwidget.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "qgsvectorlayer.h"
3232
#include "qgsproject.h"
3333
#include "qgssettings.h"
34+
#include "qgsnewnamedialog.h"
3435

3536
// browser layer properties dialog
3637
#include "qgsapplication.h"
@@ -168,6 +169,24 @@ void QgsBrowserDockWidget::itemDoubleClicked( const QModelIndex &index )
168169
addLayerAtIndex( index ); // default double-click handler
169170
}
170171

172+
void QgsBrowserDockWidget::renameFavorite()
173+
{
174+
QgsDataItem *dataItem = mModel->dataItem( mProxyModel->mapToSource( mBrowserView->currentIndex() ) );
175+
if ( !dataItem )
176+
return;
177+
178+
QgsFavoriteItem *favorite = qobject_cast< QgsFavoriteItem * >( dataItem );
179+
if ( !favorite )
180+
return;
181+
182+
QgsNewNameDialog dlg( tr( "favorite “%1”" ).arg( favorite->name() ), favorite->name() );
183+
dlg.setWindowTitle( tr( "Rename Favorite" ) );
184+
if ( dlg.exec() != QDialog::Accepted || dlg.name() == favorite->name() )
185+
return;
186+
187+
favorite->rename( dlg.name() );
188+
}
189+
171190
void QgsBrowserDockWidget::showContextMenu( QPoint pt )
172191
{
173192
QModelIndex index = mProxyModel->mapToSource( mBrowserView->indexAt( pt ) );
@@ -180,18 +199,20 @@ void QgsBrowserDockWidget::showContextMenu( QPoint pt )
180199
if ( item->type() == QgsDataItem::Directory )
181200
{
182201
QgsSettings settings;
183-
QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
184-
bool inFavDirs = item->parent() && item->parent()->type() == QgsDataItem::Favorites;
185202

203+
bool inFavDirs = item->parent() && item->parent()->type() == QgsDataItem::Favorites;
186204
if ( item->parent() && !inFavDirs )
187205
{
188206
// only non-root directories can be added as favorites
189207
menu->addAction( tr( "Add as a Favorite" ), this, SLOT( addFavorite() ) );
190208
}
191209
else if ( inFavDirs )
192210
{
193-
// only favorites can be removed
211+
QAction *actionRename = new QAction( tr( "Rename Favorite..." ), this );
212+
connect( actionRename, &QAction::triggered, this, &QgsBrowserDockWidget::renameFavorite );
213+
menu->addAction( actionRename );
194214
menu->addAction( tr( "Remove Favorite" ), this, SLOT( removeFavorite() ) );
215+
menu->addSeparator();
195216
}
196217
menu->addAction( tr( "Properties..." ), this, SLOT( showProperties() ) );
197218
menu->addAction( tr( "Hide from Browser" ), this, SLOT( hideItem() ) );
@@ -261,9 +282,9 @@ void QgsBrowserDockWidget::addFavoriteDirectory()
261282
}
262283
}
263284

264-
void QgsBrowserDockWidget::addFavoriteDirectory( const QString &favDir )
285+
void QgsBrowserDockWidget::addFavoriteDirectory( const QString &favDir, const QString &name )
265286
{
266-
mModel->addFavoriteDirectory( favDir );
287+
mModel->addFavoriteDirectory( favDir, name );
267288
}
268289

269290
void QgsBrowserDockWidget::removeFavorite()

src/gui/qgsbrowserdockwidget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro
5353
explicit QgsBrowserDockWidget( const QString &name, QgsBrowserModel *browserModel, QWidget *parent SIP_TRANSFERTHIS = nullptr );
5454
~QgsBrowserDockWidget();
5555
//! Add directory to favorites
56-
void addFavoriteDirectory( const QString &favDir );
56+
void addFavoriteDirectory( const QString &favDir, const QString &name = QString() );
5757

5858
public slots:
5959
//! Add layer at index
@@ -112,6 +112,7 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro
112112

113113
private slots:
114114
void itemDoubleClicked( const QModelIndex &index );
115+
void renameFavorite();
115116

116117
private:
117118
//! Refresh the model

0 commit comments

Comments
 (0)