@@ -46,12 +46,6 @@ QgsGrassLocationItem::QgsGrassLocationItem( QgsDataItem* parent, QString dirPath
46
46
mType = QgsDataItem::Directory;
47
47
}
48
48
49
- bool QgsGrassLocationItem::isLocation ( QString path )
50
- {
51
- // QgsDebugMsg( "path = " + path );
52
- return QFile::exists ( path + " /" + " PERMANENT" + " /" + " DEFAULT_WIND" );
53
- }
54
-
55
49
QVector<QgsDataItem*>QgsGrassLocationItem::createChildren ()
56
50
{
57
51
QVector<QgsDataItem*> mapsets;
@@ -63,7 +57,7 @@ QVector<QgsDataItem*>QgsGrassLocationItem::createChildren()
63
57
{
64
58
QString path = dir.absoluteFilePath ( name );
65
59
66
- if ( QgsGrassMapsetItem ::isMapset ( path ) )
60
+ if ( QgsGrass ::isMapset ( path ) )
67
61
{
68
62
QgsGrassMapsetItem * mapset = new QgsGrassMapsetItem ( this , path, mPath + " /" + name );
69
63
mapsets.append ( mapset );
@@ -89,11 +83,6 @@ QgsGrassMapsetItem::QgsGrassMapsetItem( QgsDataItem* parent, QString dirPath, QS
89
83
mIconName = " grass_mapset.png" ;
90
84
}
91
85
92
- bool QgsGrassMapsetItem::isMapset ( QString path )
93
- {
94
- return QFile::exists ( path + " /WIND" );
95
- }
96
-
97
86
QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren ()
98
87
{
99
88
QgsDebugMsg ( " Entered" );
@@ -122,8 +111,8 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
122
111
// somewhere not properly escaped (there was bug in QgsMimeDataUtils for example)
123
112
QString uri = mDirPath + " /" + name + " /" + layerName;
124
113
QgsLayerItem::LayerType layerType = QgsLayerItem::Vector;
125
- QString typeName = layerName.split ( " _" )[ 1 ] ;
126
- QString baseLayerName = layerName.split ( " _" )[ 0 ] ;
114
+ QString typeName = layerName.split ( " _" ). value ( 1 ) ;
115
+ QString baseLayerName = layerName.split ( " _" ). value ( 0 ) ;
127
116
128
117
if ( typeName == " point" )
129
118
layerType = QgsLayerItem::Point;
@@ -135,8 +124,8 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
135
124
QString layerPath = mapPath + " /" + layerName;
136
125
if ( !map )
137
126
{
138
- /* This may happen (one layer only) in GRASS 7 with points (no topo layers) */
139
- QgsLayerItem *layer = new QgsGrassVectorLayerItem ( this , vectorObject, name + " " + baseLayerName , layerPath, uri, layerType, true );
127
+ /* This may happen (one layer only) in GRASS 7 with points (no topo layers) or if topo layers are disabled */
128
+ QgsLayerItem *layer = new QgsGrassVectorLayerItem ( this , vectorObject, name, layerPath, uri, layerType, true );
140
129
// layer->setState( Populated );
141
130
items.append ( layer );
142
131
}
@@ -228,53 +217,93 @@ bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
228
217
QStringList extensions;
229
218
QStringList existingNames;
230
219
QRegExp regExp;
220
+ QgsGrassObject srcObject;
221
+ QString srcName;
222
+
223
+ // use g.copy for GRASS maps in the same location
224
+ bool useCopy = false ;
225
+
231
226
if ( u.layerType == " raster" )
232
227
{
233
- rasterProvider = qobject_cast<QgsRasterDataProvider*>( QgsProviderRegistry::instance ()->provider ( u.providerKey , u.uri ) );
234
- provider = rasterProvider;
228
+ if ( u.providerKey == " grassraster" && srcObject.setFromUri ( u.uri )
229
+ && srcObject.locationIdentical ( mapsetObject ) )
230
+ {
231
+ useCopy = true ;
232
+ }
233
+ else
234
+ {
235
+ rasterProvider = qobject_cast<QgsRasterDataProvider*>( QgsProviderRegistry::instance ()->provider ( u.providerKey , u.uri ) );
236
+ provider = rasterProvider;
237
+ }
235
238
existingNames = existingRasters;
236
239
regExp = QgsGrassObject::newNameRegExp ( QgsGrassObject::Raster );
237
240
}
238
241
else if ( u.layerType == " vector" )
239
242
{
240
- vectorProvider = qobject_cast<QgsVectorDataProvider*>( QgsProviderRegistry::instance ()->provider ( u.providerKey , u.uri ) );
241
- provider = vectorProvider;
243
+ if ( u.providerKey == " grass" && srcObject.setFromUri ( u.uri )
244
+ && srcObject.locationIdentical ( mapsetObject ) )
245
+ {
246
+ useCopy = true ;
247
+ }
248
+ else
249
+ {
250
+ vectorProvider = qobject_cast<QgsVectorDataProvider*>( QgsProviderRegistry::instance ()->provider ( u.providerKey , u.uri ) );
251
+ provider = vectorProvider;
252
+ }
242
253
existingNames = existingVectors;
243
254
regExp = QgsGrassObject::newNameRegExp ( QgsGrassObject::Vector );
244
255
}
245
256
QgsDebugMsg ( " existingNames = " + existingNames.join ( " ," ) );
246
257
247
- if ( !provider )
258
+ if ( useCopy )
248
259
{
249
- errors.append ( tr ( " Cannot create provider %1 : %2" ).arg ( u.providerKey ).arg ( u.uri ) );
250
- continue ;
251
- }
252
- if ( !provider->isValid () )
253
- {
254
- errors.append ( tr ( " Provider is not valid %1 : %2" ).arg ( u.providerKey ).arg ( u.uri ) );
255
- delete provider;
256
- continue ;
260
+ srcName = srcObject.name ();
257
261
}
258
-
259
- if ( u.layerType == " raster" )
262
+ else
260
263
{
261
- extensions = QgsGrassRasterImport::extensions ( rasterProvider );
264
+ if ( !provider )
265
+ {
266
+ errors.append ( tr ( " Cannot create provider %1 : %2" ).arg ( u.providerKey ).arg ( u.uri ) );
267
+ continue ;
268
+ }
269
+ if ( !provider->isValid () )
270
+ {
271
+ errors.append ( tr ( " Provider is not valid %1 : %2" ).arg ( u.providerKey ).arg ( u.uri ) );
272
+ delete provider;
273
+ continue ;
274
+ }
275
+ if ( u.layerType == " raster" )
276
+ {
277
+ extensions = QgsGrassRasterImport::extensions ( rasterProvider );
278
+ }
279
+ srcName = u.name ;
262
280
}
263
281
264
- QString newName = u.name ;
265
- if ( QgsNewNameDialog::exists ( u.name , extensions, existingNames, caseSensitivity ) )
282
+ // TODO: add a method in QgsGrass to convert a name to GRASS valid name
283
+ QString destName = srcName.replace ( " " , " _" );
284
+ if ( QgsNewNameDialog::exists ( destName, extensions, existingNames, caseSensitivity ) )
266
285
{
267
- QgsNewNameDialog dialog ( u. name , u. name , extensions, existingNames, regExp, caseSensitivity );
286
+ QgsNewNameDialog dialog ( srcName, destName , extensions, existingNames, regExp, caseSensitivity );
268
287
if ( dialog.exec () != QDialog::Accepted )
269
288
{
270
289
delete provider;
271
290
continue ;
272
291
}
273
- newName = dialog.name ();
292
+ destName = dialog.name ();
274
293
}
275
294
276
295
QgsGrassImport *import = 0 ;
277
- if ( u.layerType == " raster" )
296
+ QStringList newNames;
297
+ if ( useCopy )
298
+ {
299
+ QgsDebugMsg ( " location is the same -> g.copy" );
300
+ QgsGrassObject destObject ( mapsetObject );
301
+ destObject.setName ( destName );
302
+ destObject.setType ( srcObject.type () );
303
+ import = new QgsGrassCopy ( srcObject, destObject );
304
+ }
305
+
306
+ else if ( u.layerType == " raster" )
278
307
{
279
308
QgsRectangle newExtent = rasterProvider->extent ();
280
309
int newXSize;
@@ -298,6 +327,7 @@ bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
298
327
continue ;
299
328
}
300
329
newXSize = window.cols ;
330
+
301
331
newYSize = window.rows ;
302
332
303
333
newExtent = QgsGrass::extent ( &window );
@@ -326,13 +356,13 @@ bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
326
356
QgsDebugMsg ( QString ( " newXSize = %1 newYSize = %2" ).arg ( newXSize ).arg ( newYSize ) );
327
357
328
358
QString path = mPath + " /" + " raster" + " /" + u.name ;
329
- QgsGrassObject rasterObject ( mGisdbase , mLocation , mName , newName , QgsGrassObject::Raster );
359
+ QgsGrassObject rasterObject ( mGisdbase , mLocation , mName , destName , QgsGrassObject::Raster );
330
360
import = new QgsGrassRasterImport ( pipe, rasterObject, newExtent, newXSize, newYSize ); // takes pipe ownership
331
361
}
332
362
else if ( u.layerType == " vector" )
333
363
{
334
364
QString path = mPath + " /" + " raster" + " /" + u.name ;
335
- QgsGrassObject vectorObject ( mGisdbase , mLocation , mName , newName , QgsGrassObject::Vector );
365
+ QgsGrassObject vectorObject ( mGisdbase , mLocation , mName , destName , QgsGrassObject::Vector );
336
366
import = new QgsGrassVectorImport ( vectorProvider, vectorObject ); // takes provider ownership
337
367
}
338
368
@@ -390,7 +420,7 @@ void QgsGrassMapsetItem::onImportFinished( QgsGrassImport* import )
390
420
{
391
421
QgsMessageOutput *output = QgsMessageOutput::createMessageOutput ();
392
422
output->setTitle ( tr ( " Import to GRASS mapset failed" ) );
393
- output->setMessage ( tr ( " Failed to import %1 to %2: %3" ).arg ( import ->uri () ).arg ( import ->grassObject ()
423
+ output->setMessage ( tr ( " Failed to import %1 to %2: %3" ).arg ( import ->srcDescription () ).arg ( import ->grassObject ()
394
424
.mapsetPath () ).arg ( import ->error () ), QgsMessageOutput::MessageText );
395
425
output->showMessage ();
396
426
}
@@ -553,12 +583,20 @@ QgsGrassVectorLayerItem::QgsGrassVectorLayerItem( QgsDataItem* parent, QgsGrassO
553
583
: QgsGrassObjectItem( parent, grassObject, layerName, path, uri, layerType, " grass" , mSingleLayer )
554
584
, mSingleLayer( singleLayer )
555
585
{
586
+
556
587
}
557
588
558
589
QString QgsGrassVectorLayerItem::layerName () const
559
590
{
560
- // to get map + layer when added from browser
561
- return mGrassObject .name () + " " + name ();
591
+ if ( mSingleLayer )
592
+ {
593
+ return name ();
594
+ }
595
+ else
596
+ {
597
+ // to get map + layer when added from browser
598
+ return mGrassObject .name () + " " + name ();
599
+ }
562
600
}
563
601
564
602
// ----------------------- QgsGrassRasterItem ------------------------------
@@ -588,7 +626,7 @@ QGISEXTERN int dataCapabilities()
588
626
589
627
QGISEXTERN QgsDataItem * dataItem ( QString theDirPath, QgsDataItem* parentItem )
590
628
{
591
- if ( QgsGrassLocationItem ::isLocation ( theDirPath ) )
629
+ if ( QgsGrass ::isLocation ( theDirPath ) )
592
630
{
593
631
QString path;
594
632
QDir dir ( theDirPath );
0 commit comments