Skip to content

Commit 91e1554

Browse files
committedSep 27, 2015
[GRASS] module's input mapsets sorting
1 parent 3c7d74f commit 91e1554

File tree

3 files changed

+59
-29
lines changed

3 files changed

+59
-29
lines changed
 

‎src/plugins/grass/qgsgrassmoduleinput.cpp

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,10 @@ void QgsGrassModuleInputModel::addMapset( const QString & mapset )
164164
{
165165
QgsDebugMsg( "mapset = " + mapset );
166166

167-
168167
QStandardItem *mapsetItem = new QStandardItem( mapset );
169168
mapsetItem->setData( mapset, MapsetRole );
170169
mapsetItem->setData( mapset, Qt::EditRole );
171-
mapsetItem->setData( QgsGrassObject::None, TypeRole );
170+
mapsetItem->setData( QgsGrassObject::Mapset, TypeRole );
172171
mapsetItem->setSelectable( false );
173172

174173
refreshMapset( mapsetItem, mapset );
@@ -183,7 +182,7 @@ void QgsGrassModuleInputModel::refreshMapset( QStandardItem *mapsetItem, const Q
183182
{
184183
return;
185184
}
186-
bool currentMapset = mapset == QgsGrass::getDefaultMapset();
185+
187186
QList<QgsGrassObject::Type> types;
188187
types << QgsGrassObject::Raster << QgsGrassObject::Vector;
189188
foreach ( QgsGrassObject::Type type, types )
@@ -197,12 +196,6 @@ void QgsGrassModuleInputModel::refreshMapset( QStandardItem *mapsetItem, const Q
197196
continue;
198197
}
199198
QString mapName = map;
200-
// For now, for completer popup simplicity
201-
// TODO: implement tree view in popup
202-
if ( !currentMapset )
203-
{
204-
mapName += "@" + mapset;
205-
}
206199

207200
bool found = false;
208201
for ( int i = 0; i < mapsetItem->rowCount(); i++ )
@@ -218,7 +211,7 @@ void QgsGrassModuleInputModel::refreshMapset( QStandardItem *mapsetItem, const Q
218211
{
219212
QgsDebugMsg( "add map : " + mapName );
220213
QStandardItem *mapItem = new QStandardItem( mapName );
221-
mapItem->setData( mapName, Qt::EditRole );
214+
mapItem->setData( mapName , Qt::EditRole );
222215
mapItem->setData( map, MapRole );
223216
mapItem->setData( mapset, MapsetRole );
224217
mapItem->setData( type, TypeRole );
@@ -251,10 +244,6 @@ void QgsGrassModuleInputModel::reload()
251244
{
252245
clear();
253246
QStringList mapsets = QgsGrass::mapsets( QgsGrass::getDefaultGisdbase(), QgsGrass::getDefaultLocation() );
254-
// Put current mapset on top
255-
mapsets.removeOne( QgsGrass::getDefaultMapset() );
256-
mapsets.prepend( QgsGrass::getDefaultMapset() );
257-
258247
foreach ( QString mapset, mapsets )
259248
{
260249
addMapset( mapset );
@@ -272,11 +261,32 @@ QgsGrassModuleInputModel *QgsGrassModuleInputModel::instance()
272261
return &sInstance;
273262
}
274263

264+
QVariant QgsGrassModuleInputModel::data( const QModelIndex & index, int role ) const
265+
{
266+
QgsDebugMsg( "entered" );
267+
QVariant data = QStandardItemModel::data( index, role );
268+
if ( role == Qt::DisplayRole || role == Qt::EditRole ) // EditRole for combo
269+
{
270+
int type = QStandardItemModel::data( index, QgsGrassModuleInputModel::TypeRole ).toInt();
271+
if ( type == QgsGrassObject::Raster || type == QgsGrassObject::Vector )
272+
{
273+
QString mapset = QStandardItemModel::data( index, QgsGrassModuleInputModel::MapsetRole ).toString();
274+
if ( mapset != QgsGrass::getDefaultMapset() )
275+
{
276+
data = data.toString() + "@" + mapset;
277+
}
278+
}
279+
}
280+
return data;
281+
}
282+
275283
/**************************** QgsGrassModuleInputProxy ****************************/
276-
QgsGrassModuleInputProxy::QgsGrassModuleInputProxy( QgsGrassObject::Type type, QObject *parent )
284+
QgsGrassModuleInputProxy::QgsGrassModuleInputProxy( QgsGrassModuleInputModel *sourceModel, QgsGrassObject::Type type, QObject *parent )
277285
: QSortFilterProxyModel( parent )
286+
, mSourceModel( sourceModel )
278287
, mType( type )
279288
{
289+
setSourceModel( mSourceModel );
280290
setDynamicSortFilter( true );
281291
}
282292

@@ -289,10 +299,30 @@ bool QgsGrassModuleInputProxy::filterAcceptsRow( int sourceRow, const QModelInde
289299
QModelIndex sourceIndex = sourceModel()->index( sourceRow, 0, sourceParent );
290300

291301
QgsDebugMsg( QString( "mType = %1 item type = %2" ).arg( mType ).arg( sourceModel()->data( sourceIndex, QgsGrassModuleInputModel::TypeRole ).toInt() ) );
292-
//return true;
293302
QgsGrassObject::Type itemType = ( QgsGrassObject::Type )( sourceModel()->data( sourceIndex, QgsGrassModuleInputModel::TypeRole ).toInt() );
294303
// TODO: filter out mapsets without given type? May be confusing.
295-
return itemType == QgsGrassObject::None || mType == itemType; // None for mapsets
304+
return itemType == QgsGrassObject::Mapset || mType == itemType;
305+
}
306+
307+
bool QgsGrassModuleInputProxy::lessThan( const QModelIndex & left, const QModelIndex & right ) const
308+
{
309+
Q_UNUSED( right )
310+
if ( mSourceModel )
311+
{
312+
// keep current mapset on top
313+
if ( mSourceModel->data( left, QgsGrassModuleInputModel::TypeRole ).toInt() == QgsGrassObject::Mapset )
314+
{
315+
if ( mSourceModel->data( left ).toString() == QgsGrass::getDefaultMapset() )
316+
{
317+
return true;
318+
}
319+
else if ( mSourceModel->data( right ).toString() == QgsGrass::getDefaultMapset() )
320+
{
321+
return false;
322+
}
323+
}
324+
}
325+
return QSortFilterProxyModel::lessThan( left, right );
296326
}
297327

298328
/**************************** QgsGrassModuleInputTreeView ****************************/
@@ -404,6 +434,8 @@ void QgsGrassModuleInputCompleterProxy::map( const QModelIndex & parent, int lev
404434
}
405435

406436
/**************************** QgsGrassModuleInputCompleter ****************************/
437+
// TODO: implement tree view in popup
438+
407439
QgsGrassModuleInputCompleter::QgsGrassModuleInputCompleter( QAbstractItemModel * model, QWidget * parent )
408440
: QCompleter( model, parent )
409441
{
@@ -439,11 +471,12 @@ QgsGrassModuleInputComboBox::QgsGrassModuleInputComboBox( QgsGrassObject::Type t
439471
setInsertPolicy( QComboBox::NoInsert );
440472

441473
mModel = QgsGrassModuleInputModel::instance();
442-
mProxy = new QgsGrassModuleInputProxy( mType, this );
443-
mProxy->setSourceModel( mModel );
474+
mProxy = new QgsGrassModuleInputProxy( mModel, mType, this );
444475
setModel( mProxy );
445476

446477
mTreeView = new QgsGrassModuleInputTreeView( this );
478+
mTreeView->setSortingEnabled( true );
479+
mTreeView->sortByColumn( 0, Qt::AscendingOrder );
447480
mTreeView->setSelectionMode( QAbstractItemView::SingleSelection );
448481
//mTreeView->setSelectionMode(QAbstractItemView::MultiSelection); // does not work
449482
mTreeView->viewport()->installEventFilter( this );

‎src/plugins/grass/qgsgrassmoduleinput.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ class QgsGrassModuleInputModel : public QStandardItemModel
6767
/** Get singleton instance of this class. */
6868
static QgsGrassModuleInputModel* instance();
6969

70+
QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const override;
71+
72+
7073
public slots:
7174
/** Reload current mapset */
7275
void reload();
7376

7477
void onDirectoryChanged( const QString & path );
7578

76-
signals:
77-
7879
private:
7980
void addMapset( const QString & mapset );
8081
void refreshMapset( QStandardItem *mapsetItem, const QString & mapset );
@@ -85,7 +86,6 @@ class QgsGrassModuleInputModel : public QStandardItemModel
8586
// names of
8687
QStringList locationDirNames();
8788
QFileSystemWatcher *mWatcher;
88-
8989
};
9090

9191
// Filter maps by type
@@ -94,22 +94,18 @@ class QgsGrassModuleInputProxy : public QSortFilterProxyModel
9494
Q_OBJECT
9595

9696
public:
97-
explicit QgsGrassModuleInputProxy( QgsGrassObject::Type type, QObject *parent = 0 );
97+
explicit QgsGrassModuleInputProxy( QgsGrassModuleInputModel *sourceModel, QgsGrassObject::Type type, QObject *parent = 0 );
9898
~QgsGrassModuleInputProxy() {}
9999

100-
public slots:
101-
102-
signals:
103-
104100
protected:
105101
bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const override;
102+
bool lessThan( const QModelIndex & left, const QModelIndex & right ) const override;
106103

107104
private:
105+
QgsGrassModuleInputModel *mSourceModel;
108106
QgsGrassObject::Type mType;
109-
110107
};
111108

112-
113109
class QgsGrassModuleInputTreeView : public QTreeView
114110
{
115111
Q_OBJECT

‎src/plugins/grass/qgsgrasstools.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ void QgsGrassTools::mapsetChanged()
549549
{
550550
QgsDebugMsg( "entered." );
551551

552+
mTabWidget->setCurrentIndex( 0 );
552553
closeTools();
553554
mRegion->mapsetChanged();
554555
showTabs();

0 commit comments

Comments
 (0)
Please sign in to comment.