Skip to content

Commit

Permalink
Follow up c6c8277, fix bad behaviour of QgsBrowserModel introduced
Browse files Browse the repository at this point in the history
by 44d9b35 (fix #14296)
  • Loading branch information
nyalldawson committed Feb 16, 2016
1 parent 13c5848 commit 14f9a88
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 36 deletions.
14 changes: 7 additions & 7 deletions python/core/qgsbrowsermodel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class QgsBrowserModel : QAbstractItemModel

enum ItemDataRole
{
// item path used to access path in the tree, see QgsDataItem::mPath
PathRole
PathRole, /*!< Item path used to access path in the tree, see QgsDataItem::mPath */
CommentRole, /*!< Item comment */
};
// implemented methods from QAbstractItemModel for read-only access

Expand Down Expand Up @@ -59,10 +59,10 @@ class QgsBrowserModel : QAbstractItemModel

bool hasChildren( const QModelIndex &parent = QModelIndex() ) const;

// Refresh item specified by path
//! Refresh item specified by path
void refresh( const QString& path );

// Refresh item childs
//! Refresh item children
void refresh( const QModelIndex &index = QModelIndex() );

/** Return index of item with given path. It only searches in currently fetched
Expand All @@ -73,7 +73,7 @@ class QgsBrowserModel : QAbstractItemModel
* @return model index, invalid if item not found */
QModelIndex findPath( const QString& path, Qt::MatchFlag matchFlag = Qt::MatchExactly );

// @note not available in python bindings
//! @note not available in python bindings
// static QModelIndex findPath( QAbstractItemModel *model, const QString& path, Qt::MatchFlag matchFlag = Qt::MatchExactly );

void connectItem( QgsDataItem *item );
Expand All @@ -86,7 +86,7 @@ class QgsBrowserModel : QAbstractItemModel
void stateChanged( const QModelIndex & index, QgsDataItem::State oldState );

public slots:
// Reload the whole model
//! Reload the whole model
void reload();
void beginInsertItems( QgsDataItem *parent, int first, int last );
void endInsertItems();
Expand All @@ -103,7 +103,7 @@ class QgsBrowserModel : QAbstractItemModel
void hidePath( QgsDataItem *item );

protected:
// populates the model
//! Populates the model
void addRootItems();
void removeRootItems();
};
15 changes: 5 additions & 10 deletions src/app/qgsbrowserdockwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,10 +906,7 @@ bool QgsBrowserTreeFilterProxyModel::filterAcceptsRow( int sourceRow, const QMod
if ( mFilter == "" || !mModel ) return true;

QModelIndex sourceIndex = mModel->index( sourceRow, 0, sourceParent );
// also look into the comment column
QModelIndex commentIndex = mModel->index( sourceRow, 1, sourceParent );
return filterAcceptsItem( sourceIndex ) || filterAcceptsAncestor( sourceIndex ) || filterAcceptsDescendant( sourceIndex ) ||
filterAcceptsItem( commentIndex ) || filterAcceptsAncestor( commentIndex ) || filterAcceptsDescendant( commentIndex );
return filterAcceptsItem( sourceIndex ) || filterAcceptsAncestor( sourceIndex ) || filterAcceptsDescendant( sourceIndex );
}

bool QgsBrowserTreeFilterProxyModel::filterAcceptsAncestor( const QModelIndex& sourceIndex ) const
Expand Down Expand Up @@ -939,11 +936,6 @@ bool QgsBrowserTreeFilterProxyModel::filterAcceptsDescendant( const QModelIndex&
return true;
if ( filterAcceptsDescendant( sourceChildIndex ) )
return true;
sourceChildIndex = mModel->index( i, 1, sourceIndex );
if ( filterAcceptsItem( sourceChildIndex ) )
return true;
if ( filterAcceptsDescendant( sourceChildIndex ) )
return true;
}
return false;
}
Expand All @@ -952,5 +944,8 @@ bool QgsBrowserTreeFilterProxyModel::filterAcceptsItem( const QModelIndex& sourc
{
if ( !mModel )
return true;
return filterAcceptsString( mModel->data( sourceIndex, Qt::DisplayRole ).toString() );
//accept item if either displayed text or comment role matches string
QString comment = mModel->data( sourceIndex, QgsBrowserModel::CommentRole ).toString();
return ( filterAcceptsString( mModel->data( sourceIndex, Qt::DisplayRole ).toString() )
|| ( !comment.isEmpty() && filterAcceptsString( comment ) ) );
}
23 changes: 11 additions & 12 deletions src/core/qgsbrowsermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,7 @@ QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
}
else if ( role == Qt::DisplayRole )
{
if ( index.column() == 0 )
{
return item->name();
}
if ( item->type() == QgsDataItem::Layer )
{
QgsLayerItem* lyrItem = qobject_cast<QgsLayerItem*>( item );
return lyrItem->comments();
}
return "";
return item->name();
}
else if ( role == Qt::ToolTipRole )
{
Expand All @@ -230,6 +221,15 @@ QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
{
return item->path();
}
else if ( role == QgsBrowserModel::CommentRole )
{
if ( item->type() == QgsDataItem::Layer )
{
QgsLayerItem* lyrItem = qobject_cast<QgsLayerItem*>( item );
return lyrItem->comments();
}
return QVariant();
}
else
{
// unsupported role
Expand Down Expand Up @@ -337,8 +337,7 @@ void QgsBrowserModel::reload()

QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex &parent ) const
{
if ( column < 0 || column >= columnCount( parent ) ||
row < 0 || row >= rowCount( parent ) )
if ( column < 0 || column >= columnCount() || row < 0 )
return QModelIndex();

QgsDataItem *p = dataItem( parent );
Expand Down
14 changes: 7 additions & 7 deletions src/core/qgsbrowsermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel

enum ItemDataRole
{
// item path used to access path in the tree, see QgsDataItem::mPath
PathRole = Qt::UserRole
PathRole = Qt::UserRole, /*!< Item path used to access path in the tree, see QgsDataItem::mPath */
CommentRole = Qt::UserRole + 1, /*!< Item comment */
};
// implemented methods from QAbstractItemModel for read-only access

Expand Down Expand Up @@ -103,10 +103,10 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel

bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;

// Refresh item specified by path
//! Refresh item specified by path
void refresh( const QString& path );

// Refresh item childs
//! Refresh item children
void refresh( const QModelIndex &index = QModelIndex() );

/** Return index of item with given path. It only searches in currently fetched
Expand All @@ -117,7 +117,7 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
* @return model index, invalid if item not found */
QModelIndex findPath( const QString& path, Qt::MatchFlag matchFlag = Qt::MatchExactly );

// @note not available in python bindings
//! @note not available in python bindings
static QModelIndex findPath( QAbstractItemModel *model, const QString& path, Qt::MatchFlag matchFlag = Qt::MatchExactly );

void connectItem( QgsDataItem *item );
Expand All @@ -130,7 +130,7 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
void stateChanged( const QModelIndex & index, QgsDataItem::State oldState );

public slots:
// Reload the whole model
//! Reload the whole model
void reload();
void beginInsertItems( QgsDataItem *parent, int first, int last );
void endInsertItems();
Expand All @@ -147,7 +147,7 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
void hidePath( QgsDataItem *item );

protected:
// populates the model
//! Populates the model
void addRootItems();
void removeRootItems();

Expand Down

0 comments on commit 14f9a88

Please sign in to comment.