Skip to content
Permalink
Browse files

Merge pull request #5864 from nyalldawson/favorites

[browser] Allow renaming favorite items
  • Loading branch information
nyalldawson committed Dec 14, 2017
2 parents 49990b8 + 12e44b2 commit 9984962257d35cb4a88e03ec29af358b82cc6c59
@@ -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
@@ -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
@@ -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

@@ -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
@@ -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
@@ -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 )
@@ -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.
@@ -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 *>();
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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

@@ -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
@@ -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 );
};

/**
@@ -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 );
@@ -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

0 comments on commit 9984962

Please sign in to comment.
You can’t perform that action at this time.