@@ -57,55 +57,207 @@ extern "C"
57
57
/* *************************** QgsGrassModuleInputModel ****************************/
58
58
QgsGrassModuleInputModel::QgsGrassModuleInputModel ( QObject *parent )
59
59
: QStandardItemModel( parent )
60
+ , mWatcher( 0 )
60
61
{
61
62
setColumnCount ( 1 );
62
63
reload ();
64
+
65
+ QString locationPath = QgsGrass::getDefaultLocationPath ();
66
+ mWatcher = new QFileSystemWatcher ( this );
67
+ mWatcher ->addPath ( locationPath );
68
+
69
+ // Watching all dirs in loacation because a dir may become a mapset later, when WIND is created
70
+
71
+ // QStringList mapsets = QgsGrass::mapsets( QgsGrass::getDefaultGisdbase(), QgsGrass::getDefaultLocation() );
72
+ QStringList dirNames = locationDirNames ();
73
+ foreach ( QString dirName, dirNames )
74
+ {
75
+ QString dirPath = locationPath + " /" + dirName;
76
+ // Watch the dir in any case, WIND mabe created later
77
+ mWatcher ->addPath ( dirPath );
78
+
79
+ foreach ( QString watchedDir, watchedDirs () )
80
+ {
81
+ watch ( dirPath + " /" + watchedDir );
82
+ }
83
+ }
84
+ connect ( mWatcher , SIGNAL ( directoryChanged ( const QString & ) ), SLOT ( onDirectoryChanged ( const QString & ) ) );
63
85
}
64
86
65
- void QgsGrassModuleInputModel::reload ( )
87
+ void QgsGrassModuleInputModel::onDirectoryChanged ( const QString & path )
66
88
{
67
- clear ();
68
- QStringList mapsets = QgsGrass::mapsets ( QgsGrass::getDefaultGisdbase (), QgsGrass::getDefaultLocation () );
69
- // Put current mapset on top
70
- mapsets.removeOne ( QgsGrass::getDefaultMapset () );
71
- mapsets.prepend ( QgsGrass::getDefaultMapset () );
89
+ QgsDebugMsg ( " path = " + path );
72
90
73
- foreach ( QString mapset, mapsets )
91
+ QString locationPath = QgsGrass::getDefaultLocationPath ();
92
+ QDir parentDir ( path );
93
+ parentDir.cdUp ();
94
+ QString mapset;
95
+
96
+ if ( path == locationPath )
74
97
{
75
- bool currentMapset = mapset == QgsGrass::getDefaultMapset ();
76
- QStandardItem *mapsetItem = new QStandardItem ( mapset );
77
- mapsetItem->setData ( mapset, MapsetRole );
78
- mapsetItem->setData ( mapset, Qt::EditRole );
79
- mapsetItem->setData ( QgsGrassObject::None, TypeRole );
80
- mapsetItem->setSelectable ( false );
98
+ QgsDebugMsg ( " location = " + path );
99
+ QStringList dirNames = locationDirNames ();
100
+ // QStringList mapsets = QgsGrass::mapsets( QgsGrass::getDefaultGisdbase(), QgsGrass::getDefaultLocation() );
81
101
82
- QList<QgsGrassObject::Type> types;
83
- types << QgsGrassObject::Raster << QgsGrassObject::Vector;
84
- foreach ( QgsGrassObject::Type type, types )
102
+ for ( int i = rowCount () - 1 ; i >= 0 ; i-- )
85
103
{
86
- QStringList maps = QgsGrass::grassObjects ( QgsGrass::getDefaultGisdbase () + " / " + QgsGrass::getDefaultLocation () + " / " + mapset, type );
87
- foreach ( QString map, maps )
104
+ QString mapset = item ( i )-> text ( );
105
+ if ( ! QgsGrass::isMapset ( locationPath + " / " + mapset ) )
88
106
{
89
- if ( map.startsWith ( " qgis_import_tmp_" ) )
90
- {
91
- continue ;
92
- }
93
- QString mapName = map;
94
- // For now, for completer popup simplicity
95
- // TODO: implement tree view in popup
96
- if ( !currentMapset )
107
+ QgsDebugMsg ( " removed mapset " + mapset );
108
+ removeRows ( i, 1 );
109
+ }
110
+ }
111
+
112
+ foreach ( QString dirName, dirNames )
113
+ {
114
+ // Add to watcher in any case, either for WIND, cellhd or vector
115
+ QString dirPath = locationPath + " /" + dirName;
116
+ watch ( dirPath );
117
+ if ( QgsGrass::isMapset ( dirPath ) && findItems ( dirName ).isEmpty () )
118
+ {
119
+ addMapset ( dirName );
120
+ }
121
+ }
122
+ }
123
+ else if ( parentDir.canonicalPath () == QDir ( locationPath ).canonicalPath () ) // mapset
124
+ {
125
+ QgsDebugMsg ( " mapset = " + path );
126
+ QDir dir ( path );
127
+ mapset = dir.dirName ();
128
+ foreach ( QString watchedDir, watchedDirs () )
129
+ {
130
+ watch ( path + " /" + watchedDir );
131
+ }
132
+ }
133
+ else // cellhd or vector dir
134
+ {
135
+ QgsDebugMsg ( " cellhd/vector = " + path );
136
+ mapset = parentDir.dirName ();
137
+ }
138
+ if ( !mapset.isEmpty () )
139
+ {
140
+ QList<QStandardItem *> items = findItems ( mapset );
141
+ if ( items.size () == 1 )
142
+ {
143
+ refreshMapset ( items[0 ], mapset );
144
+ }
145
+ }
146
+ }
147
+
148
+ void QgsGrassModuleInputModel::watch ( const QString & path )
149
+ {
150
+ if ( !mWatcher ->directories ().contains ( path ) && QFileInfo ( path ).exists () )
151
+ {
152
+ mWatcher ->addPath ( path );
153
+ }
154
+ }
155
+
156
+ QStringList QgsGrassModuleInputModel::locationDirNames ()
157
+ {
158
+ QString locationPath = QgsGrass::getDefaultLocationPath ();
159
+ QDir locationDir ( locationPath );
160
+ return locationDir.entryList ( QDir::Dirs | QDir::NoDotAndDotDot );
161
+ }
162
+
163
+ void QgsGrassModuleInputModel::addMapset ( const QString & mapset )
164
+ {
165
+ QgsDebugMsg ( " mapset = " + mapset );
166
+
167
+
168
+ QStandardItem *mapsetItem = new QStandardItem ( mapset );
169
+ mapsetItem->setData ( mapset, MapsetRole );
170
+ mapsetItem->setData ( mapset, Qt::EditRole );
171
+ mapsetItem->setData ( QgsGrassObject::None, TypeRole );
172
+ mapsetItem->setSelectable ( false );
173
+
174
+ refreshMapset ( mapsetItem, mapset );
175
+
176
+ appendRow ( mapsetItem );
177
+ }
178
+
179
+ void QgsGrassModuleInputModel::refreshMapset ( QStandardItem *mapsetItem, const QString & mapset )
180
+ {
181
+ QgsDebugMsg ( " mapset = " + mapset );
182
+ if ( !mapsetItem )
183
+ {
184
+ return ;
185
+ }
186
+ bool currentMapset = mapset == QgsGrass::getDefaultMapset ();
187
+ QList<QgsGrassObject::Type> types;
188
+ types << QgsGrassObject::Raster << QgsGrassObject::Vector;
189
+ foreach ( QgsGrassObject::Type type, types )
190
+ {
191
+ QStringList maps = QgsGrass::grassObjects ( QgsGrass::getDefaultGisdbase () + " /" + QgsGrass::getDefaultLocation () + " /" + mapset, type );
192
+ QStringList mapNames;
193
+ foreach ( QString map, maps )
194
+ {
195
+ if ( map.startsWith ( " qgis_import_tmp_" ) )
196
+ {
197
+ continue ;
198
+ }
199
+ 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
+ }
206
+
207
+ bool found = false ;
208
+ for ( int i = 0 ; i < mapsetItem->rowCount (); i++ )
209
+ {
210
+ QStandardItem * item = mapsetItem->child ( i );
211
+ if ( item->text () == mapName && item->data ( TypeRole ).toInt () == type )
97
212
{
98
- mapName += " @" + mapset;
213
+ found = true ;
214
+ break ;
99
215
}
216
+ }
217
+ if ( !found )
218
+ {
219
+ QgsDebugMsg ( " add map : " + mapName );
100
220
QStandardItem *mapItem = new QStandardItem ( mapName );
101
221
mapItem->setData ( mapName, Qt::EditRole );
102
222
mapItem->setData ( map, MapRole );
103
223
mapItem->setData ( mapset, MapsetRole );
104
224
mapItem->setData ( type, TypeRole );
105
225
mapsetItem->appendRow ( mapItem );
106
226
}
227
+ else
228
+ {
229
+ QgsDebugMsg ( " map exists : " + mapName );
230
+ }
231
+ mapNames << mapName;
232
+ }
233
+
234
+ for ( int i = mapsetItem->rowCount () - 1 ; i >= 0 ; i-- )
235
+ {
236
+ if ( mapsetItem->child ( i )->data ( TypeRole ).toInt () != type )
237
+ {
238
+ continue ;
239
+ }
240
+ QString mapName = mapsetItem->child ( i )->text ();
241
+ if ( !mapNames.contains ( mapName ) )
242
+ {
243
+ QgsDebugMsg ( " remove map : " + mapName );
244
+ mapsetItem->removeRows ( i, 1 );
245
+ }
107
246
}
108
- appendRow ( mapsetItem );
247
+ }
248
+ }
249
+
250
+ void QgsGrassModuleInputModel::reload ()
251
+ {
252
+ clear ();
253
+ 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
+
258
+ foreach ( QString mapset, mapsets )
259
+ {
260
+ addMapset ( mapset );
109
261
}
110
262
}
111
263
@@ -612,7 +764,7 @@ QgsGrassModuleInput::QgsGrassModuleInput( QgsGrassModule *module,
612
764
613
765
QVBoxLayout *layout = new QVBoxLayout ( this );
614
766
// Map + region
615
- QHBoxLayout *mapLayout = new QHBoxLayout ( this );
767
+ QHBoxLayout *mapLayout = new QHBoxLayout ();
616
768
layout->addLayout ( mapLayout );
617
769
618
770
// Map input
@@ -651,7 +803,7 @@ QgsGrassModuleInput::QgsGrassModuleInput( QgsGrassModule *module,
651
803
// Vector layer + type
652
804
if ( mType == QgsGrassObject::Vector && !multiple () )
653
805
{
654
- QHBoxLayout *layerLayout = new QHBoxLayout ( this );
806
+ QHBoxLayout *layerLayout = new QHBoxLayout ();
655
807
layout->addLayout ( layerLayout );
656
808
657
809
mLayerLabel = new QLabel ( tr ( " Sublayer" ), this );
@@ -661,7 +813,7 @@ QgsGrassModuleInput::QgsGrassModuleInput( QgsGrassModule *module,
661
813
connect ( mLayerComboBox , SIGNAL ( currentIndexChanged ( int ) ), this , SLOT ( onLayerChanged () ) );
662
814
layerLayout->addWidget ( mLayerComboBox );
663
815
664
- QHBoxLayout *typeLayout = new QHBoxLayout ( this );
816
+ QHBoxLayout *typeLayout = new QHBoxLayout ();
665
817
layerLayout->addLayout ( typeLayout );
666
818
667
819
// Vector types
@@ -686,13 +838,6 @@ QgsGrassModuleInput::QgsGrassModuleInput( QgsGrassModule *module,
686
838
layerLayout->addItem ( new QSpacerItem ( 10 , 10 , QSizePolicy::Expanding, QSizePolicy::Minimum ) );
687
839
}
688
840
689
-
690
- connect ( QgsMapLayerRegistry::instance (), SIGNAL ( layersAdded ( QList<QgsMapLayer *> ) ),
691
- this , SLOT ( updateQgisLayers () ) );
692
- connect ( QgsMapLayerRegistry::instance (), SIGNAL ( layersRemoved ( QStringList ) ),
693
- this , SLOT ( updateQgisLayers () ) );
694
-
695
-
696
841
if ( !mMapId .isEmpty () )
697
842
{
698
843
QgsGrassModuleParam *item = mModuleStandardOptions ->item ( mMapId );
0 commit comments