Skip to content

Commit

Permalink
Merge pull request #5864 from nyalldawson/favorites
Browse files Browse the repository at this point in the history
[browser] Allow renaming favorite items
  • Loading branch information
nyalldawson committed Dec 14, 2017
2 parents 49990b8 + 12e44b2 commit 9984962
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 29 deletions.
8 changes: 6 additions & 2 deletions python/core/qgsbrowsermodel.sip
Expand Up @@ -165,9 +165,13 @@ Reload the whole model
void itemDataChanged( QgsDataItem *item );
void itemStateChanged( QgsDataItem *item, QgsDataItem::State oldState );

void addFavoriteDirectory( const QString &directory );
void addFavoriteDirectory( const QString &directory, const QString &name = QString() );
%Docstring
Adds a directory to the favorites group.
Adds a ``directory`` to the favorites group.

If ``name`` is specified, it will be used as the favorite's name. Otherwise
the name will be set to match ``directory``.

.. versionadded:: 3.0
.. seealso:: :py:func:`removeFavorite()`
%End
Expand Down
24 changes: 22 additions & 2 deletions python/core/qgsdataitem.sip
Expand Up @@ -255,11 +255,22 @@ Create new data item.
%Docstring
:rtype: QIcon
%End

QString name() const;
%Docstring
Returns the name of the item (the displayed text for the item).

.. seealso:: :py:func:`setName()`
:rtype: str
%End

void setName( const QString &name );
%Docstring
Sets the ``name`` of the item (the displayed text for the item).

.. seealso:: :py:func:`name()`
%End

QString path() const;
%Docstring
:rtype: str
Expand Down Expand Up @@ -673,9 +684,13 @@ class QgsFavoritesItem : QgsDataCollectionItem
virtual QVector<QgsDataItem *> createChildren();


void addDirectory( const QString &directory );
void addDirectory( const QString &directory, const QString &name = QString() );
%Docstring
Adds a new directory to the favorites group.
Adds a new ``directory`` to the favorites group.

If ``name`` is specified, it will be used as the favorite's name. Otherwise
the name will be set to match ``directory``.

.. seealso:: :py:func:`removeDirectory()`
%End

Expand All @@ -685,6 +700,11 @@ class QgsFavoritesItem : QgsDataCollectionItem
.. seealso:: :py:func:`addDirectory()`
%End

void renameFavorite( const QString &path, const QString &name );
%Docstring
Renames the stored favorite with corresponding ``path`` a new ``name``.
%End

static QIcon iconFavorites();
%Docstring
Icon for favorites group
Expand Down
2 changes: 1 addition & 1 deletion python/gui/qgsbrowserdockwidget.sip
Expand Up @@ -29,7 +29,7 @@ class QgsBrowserDockWidget : QgsDockWidget
\param parent parent widget
%End
~QgsBrowserDockWidget();
void addFavoriteDirectory( const QString &favDir );
void addFavoriteDirectory( const QString &favDir, const QString &name = QString() );
%Docstring
Add directory to favorites
%End
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsbrowsermodel.cpp
Expand Up @@ -546,10 +546,10 @@ void QgsBrowserModel::refresh( const QModelIndex &index )
item->refresh();
}

void QgsBrowserModel::addFavoriteDirectory( const QString &directory )
void QgsBrowserModel::addFavoriteDirectory( const QString &directory, const QString &name )
{
Q_ASSERT( mFavorites );
mFavorites->addDirectory( directory );
mFavorites->addDirectory( directory, name );
}

void QgsBrowserModel::removeFavorite( const QModelIndex &index )
Expand Down
8 changes: 6 additions & 2 deletions src/core/qgsbrowsermodel.h
Expand Up @@ -164,11 +164,15 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
void itemStateChanged( QgsDataItem *item, QgsDataItem::State oldState );

/**
* Adds a directory to the favorites group.
* Adds a \a directory to the favorites group.
*
* If \a name is specified, it will be used as the favorite's name. Otherwise
* the name will be set to match \a directory.
*
* \since QGIS 3.0
* \see removeFavorite()
*/
void addFavoriteDirectory( const QString &directory );
void addFavoriteDirectory( const QString &directory, const QString &name = QString() );

/**
* Removes a favorite directory from its corresponding model index.
Expand Down
95 changes: 85 additions & 10 deletions src/core/qgsdataitem.cpp
Expand Up @@ -229,6 +229,12 @@ QIcon QgsDataItem::icon()
return mIconMap.value( mIconName );
}

void QgsDataItem::setName( const QString &name )
{
mName = name;
emit dataChanged( this );
}

QVector<QgsDataItem *> QgsDataItem::createChildren()
{
return QVector<QgsDataItem *>();
Expand Down Expand Up @@ -1106,26 +1112,37 @@ QVector<QgsDataItem *> QgsFavoritesItem::createChildren()
QVector<QgsDataItem *> children;

QgsSettings settings;
QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ), QVariant() ).toStringList();
const QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ), QVariant() ).toStringList();

Q_FOREACH ( const QString &favDir, favDirs )
for ( const QString &favDir : favDirs )
{
children << createChildren( favDir );
QStringList parts = favDir.split( QStringLiteral( "|||" ) );
if ( parts.empty() )
continue;

QString dir = parts.at( 0 );
QString name = dir;
if ( parts.count() > 1 )
name = parts.at( 1 );

children << createChildren( dir, name );
}

return children;
}

void QgsFavoritesItem::addDirectory( const QString &favDir )
void QgsFavoritesItem::addDirectory( const QString &favDir, const QString &n )
{
QString name = n.isEmpty() ? favDir : n;

QgsSettings settings;
QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
favDirs.append( favDir );
favDirs.append( QStringLiteral( "%1|||%2" ).arg( favDir, name ) );
settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );

if ( state() == Populated )
{
QVector<QgsDataItem *> items = createChildren( favDir );
QVector<QgsDataItem *> items = createChildren( favDir, name );
Q_FOREACH ( QgsDataItem *item, items )
{
addChildItem( item, true );
Expand All @@ -1140,7 +1157,16 @@ void QgsFavoritesItem::removeDirectory( QgsDirectoryItem *item )

QgsSettings settings;
QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
favDirs.removeAll( item->dirPath() );
for ( int i = favDirs.count() - 1; i >= 0; --i )
{
QStringList parts = favDirs.at( i ).split( QStringLiteral( "|||" ) );
if ( parts.empty() )
continue;

QString dir = parts.at( 0 );
if ( dir == item->dirPath() )
favDirs.removeAt( i );
}
settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );

int idx = findItem( mChildren, item );
Expand All @@ -1154,7 +1180,42 @@ void QgsFavoritesItem::removeDirectory( QgsDirectoryItem *item )
deleteChildItem( mChildren.at( idx ) );
}

QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &favDir )
void QgsFavoritesItem::renameFavorite( const QString &path, const QString &name )
{
// update stored name
QgsSettings settings;
QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
for ( int i = 0; i < favDirs.count(); ++i )
{
QStringList parts = favDirs.at( i ).split( QStringLiteral( "|||" ) );
if ( parts.empty() )
continue;

QString dir = parts.at( 0 );
if ( dir == path )
{
favDirs[i] = QStringLiteral( "%1|||%2" ).arg( path, name );
break;
}
}
settings.setValue( QStringLiteral( "browser/favourites" ), favDirs );

// also update existing data item
const QVector<QgsDataItem *> ch = children();
for ( QgsDataItem *child : ch )
{
if ( QgsFavoriteItem *favorite = qobject_cast< QgsFavoriteItem * >( child ) )
{
if ( favorite->dirPath() == path )
{
favorite->setName( name );
break;
}
}
}
}

QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &favDir, const QString &name )
{
QVector<QgsDataItem *> children;
QString pathName = pathComponent( favDir );
Expand All @@ -1167,14 +1228,14 @@ QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &favDir )
QgsDataItem *item = provider->createDataItem( favDir, this );
if ( item )
{
item->setName( favDir );
item->setName( name );
children.append( item );
}
}
}
if ( children.isEmpty() )
{
QgsDataItem *item = new QgsDirectoryItem( this, favDir, favDir, mPath + '/' + pathName );
QgsFavoriteItem *item = new QgsFavoriteItem( this, name, favDir, mPath + '/' + pathName );
if ( item )
{
children.append( item );
Expand Down Expand Up @@ -1558,4 +1619,18 @@ QVariant QgsProjectHomeItem::sortKey() const
return QStringLiteral( " 1" );
}

QgsFavoriteItem::QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path )
: QgsDirectoryItem( parent, name, dirPath, path )
, mFavorites( parent )
{

}

void QgsFavoriteItem::rename( const QString &name )
{
mFavorites->renameFavorite( dirPath(), name );
}


///@endcond

56 changes: 52 additions & 4 deletions src/core/qgsdataitem.h
Expand Up @@ -248,8 +248,21 @@ class CORE_EXPORT QgsDataItem : public QObject
void setParent( QgsDataItem *parent );
QVector<QgsDataItem *> children() const { return mChildren; }
virtual QIcon icon();

/**
* Returns the name of the item (the displayed text for the item).
*
* \see setName()
*/
QString name() const { return mName; }
void setName( const QString &name ) { mName = name; }

/**
* Sets the \a name of the item (the displayed text for the item).
*
* \see name()
*/
void setName( const QString &name );

QString path() const { return mPath; }
void setPath( const QString &path ) { mPath = path; }
//! Create path component replacing path separators
Expand Down Expand Up @@ -640,24 +653,33 @@ class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
QVector<QgsDataItem *> createChildren() override;

/**
* Adds a new directory to the favorites group.
* Adds a new \a directory to the favorites group.
*
* If \a name is specified, it will be used as the favorite's name. Otherwise
* the name will be set to match \a directory.
*
* \see removeDirectory()
*/
void addDirectory( const QString &directory );
void addDirectory( const QString &directory, const QString &name = QString() );

/**
* Removes an existing directory from the favorites group.
* \see addDirectory()
*/
void removeDirectory( QgsDirectoryItem *item );

/**
* Renames the stored favorite with corresponding \a path a new \a name.
*/
void renameFavorite( const QString &path, const QString &name );

//! Icon for favorites group
static QIcon iconFavorites();

QVariant sortKey() const override;

private:
QVector<QgsDataItem *> createChildren( const QString &favDir );
QVector<QgsDataItem *> createChildren( const QString &favDir, const QString &name );
};

/**
Expand Down Expand Up @@ -714,6 +736,8 @@ class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
*/
class CORE_EXPORT QgsProjectHomeItem : public QgsDirectoryItem
{
Q_OBJECT

public:

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

};

/**
* \ingroup core
* A directory item showing the a single favorite directory.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsFavoriteItem : public QgsDirectoryItem
{
Q_OBJECT

public:

QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path );

/**
* Sets a new \a name for the favorite, storing the new name permanently for the favorite.
*/
void rename( const QString &name );

private:

QgsFavoritesItem *mFavorites = nullptr;
};

#endif
///@endcond

Expand Down

0 comments on commit 9984962

Please sign in to comment.