1
0
mirror of https://github.com/qgis/QGIS.git synced 2025-03-28 00:04:04 -04:00

Merge pull request from nyalldawson/sort_browser

Correctly sort browser items
This commit is contained in:
Nyall Dawson 2017-12-13 20:57:27 +11:00 committed by GitHub
commit b6ddc702c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 84 additions and 1 deletions

@ -23,6 +23,7 @@ class QgsBrowserModel : QAbstractItemModel
{
PathRole,
CommentRole,
SortRole,
};
virtual Qt::ItemFlags flags( const QModelIndex &index ) const;

@ -271,6 +271,27 @@ Create path component replacing path separators
:rtype: str
%End
virtual QVariant sortKey() const;
%Docstring
Returns the sorting key for the item. By default name() is returned,
but setSortKey() can be used to set a custom sort key for the item.
Alternatively subclasses can override this method to return a custom
sort key.
.. seealso:: :py:func:`setSortKey()`
.. versionadded:: 3.0
:rtype: QVariant
%End
void setSortKey( const QVariant &key );
%Docstring
Sets a custom sorting ``key`` for the item.
.. seealso:: :py:func:`sortKey()`
.. versionadded:: 3.0
%End
void setIcon( const QIcon &icon );
void setIconName( const QString &iconName );
@ -307,6 +328,7 @@ Move object and all its descendants to thread
%End
public slots:
virtual void deleteLater();
@ -669,6 +691,9 @@ Icon for favorites group
:rtype: QIcon
%End
virtual QVariant sortKey() const;
};
class QgsZipItem : QgsDataCollectionItem

@ -72,6 +72,7 @@ void QgsBrowserModel::updateProjectHome()
mProjectHome = home.isNull() ? nullptr : new QgsDirectoryItem( nullptr, tr( "Project home" ), home, "project:" + home );
if ( mProjectHome )
{
mProjectHome->setSortKey( QStringLiteral( " 1" ) );
connectItem( mProjectHome );
beginInsertRows( QModelIndex(), 0, 0 );
@ -84,8 +85,9 @@ void QgsBrowserModel::addRootItems()
{
updateProjectHome();
// give the home directory a prominent second place
// give the home directory a prominent third place
QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, tr( "Home" ), QDir::homePath(), "home:" + QDir::homePath() );
item->setSortKey( QStringLiteral( " 2" ) );
QStyle *style = QApplication::style();
QIcon homeIcon( style->standardPixmap( QStyle::SP_DirHomeIcon ) );
item->setIcon( homeIcon );
@ -109,6 +111,7 @@ void QgsBrowserModel::addRootItems()
continue;
QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path );
item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
connectItem( item );
mRootItems << item;
@ -212,6 +215,10 @@ QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
{
return item->name();
}
else if ( role == QgsBrowserModel::SortRole )
{
return item->sortKey();
}
else if ( role == Qt::ToolTipRole )
{
return item->toolTip();

@ -64,6 +64,7 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
{
PathRole = Qt::UserRole, //!< Item path used to access path in the tree, see QgsDataItem::mPath
CommentRole = Qt::UserRole + 1, //!< Item comment
SortRole, //!< Custom sort role, see QgsDataItem::sortKey()
};
// implemented methods from QAbstractItemModel for read-only access

@ -102,6 +102,11 @@ QIcon QgsFavoritesItem::iconFavorites()
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconFavourites.png" ) );
}
QVariant QgsFavoritesItem::sortKey() const
{
return QStringLiteral( " 0" );
}
QIcon QgsZipItem::iconZip()
{
return QgsApplication::getThemeIcon( QStringLiteral( "/mIconZip.png" ) );
@ -150,6 +155,16 @@ QString QgsDataItem::pathComponent( const QString &string )
return QString( string ).replace( QRegExp( "[\\\\/]" ), QStringLiteral( "|" ) );
}
QVariant QgsDataItem::sortKey() const
{
return mSortKey.isValid() ? mSortKey : name();
}
void QgsDataItem::setSortKey( const QVariant &key )
{
mSortKey = key;
}
void QgsDataItem::deleteLater()
{
QgsDebugMsgLevel( "path = " + path(), 3 );
@ -753,6 +768,10 @@ QVector<QgsDataItem *> QgsDirectoryItem::createChildren()
continue;
QgsDirectoryItem *item = new QgsDirectoryItem( this, subdir, subdirPath, path );
// we want directories shown before files
item->setSortKey( QStringLiteral( " %1" ).arg( subdir ) );
// propagate signals up to top
children.append( item );

@ -255,6 +255,26 @@ class CORE_EXPORT QgsDataItem : public QObject
//! Create path component replacing path separators
static QString pathComponent( const QString &component );
/**
* Returns the sorting key for the item. By default name() is returned,
* but setSortKey() can be used to set a custom sort key for the item.
*
* Alternatively subclasses can override this method to return a custom
* sort key.
*
* \see setSortKey()
* \since QGIS 3.0
*/
virtual QVariant sortKey() const;
/**
* Sets a custom sorting \a key for the item.
* \see sortKey()
* \since QGIS 3.0
*/
void setSortKey( const QVariant &key );
// Because QIcon (QPixmap) must not be used in outside the GUI thread, it is
// not possible to set mIcon in constructor. Either use mIconName/setIconName()
// or implement icon().
@ -303,6 +323,9 @@ class CORE_EXPORT QgsDataItem : public QObject
QIcon mIcon;
QMap<QString, QIcon> mIconMap;
//! Custom sort key. If invalid, name() will be used for sorting instead.
QVariant mSortKey;
public slots:
/**
@ -631,6 +654,8 @@ class CORE_EXPORT QgsFavoritesItem : public QgsDataCollectionItem
//! Icon for favorites group
static QIcon iconFavorites();
QVariant sortKey() const override;
private:
QVector<QgsDataItem *> createChildren( const QString &favDir );
};

@ -122,6 +122,8 @@ void QgsBrowserDockWidget::showEvent( QShowEvent *e )
mBrowserView->setSettingsSection( objectName().toLower() ); // to distinguish 2 or more instances of the browser
mBrowserView->setBrowserModel( mModel );
mBrowserView->setModel( mProxyModel );
mBrowserView->setSortingEnabled( true );
mBrowserView->sortByColumn( 0, Qt::AscendingOrder );
// provide a horizontal scroll bar instead of using ellipse (...) for longer items
mBrowserView->setTextElideMode( Qt::ElideNone );
mBrowserView->header()->setSectionResizeMode( 0, QHeaderView::ResizeToContents );

@ -339,6 +339,9 @@ QgsBrowserTreeFilterProxyModel::QgsBrowserTreeFilterProxyModel( QObject *parent
, mCaseSensitivity( Qt::CaseInsensitive )
{
setDynamicSortFilter( true );
setSortRole( QgsBrowserModel::SortRole );
setSortCaseSensitivity( Qt::CaseInsensitive );
sort( 0 );
}
void QgsBrowserTreeFilterProxyModel::setBrowserModel( QgsBrowserModel *model )