-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsgrassprovidermodule.cpp
1304 lines (1139 loc) · 40.9 KB
/
qgsgrassprovidermodule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsgrassprovidermodule.cpp - Data provider for GRASS format
-------------------
begin : March, 2004
copyright : (C) 2004 by Gary E.Sherman, Radim Blazek
email : sherman@mrcc.com, blazek@itc.it
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmessageoutput.h"
#include "qgsmimedatautils.h"
#include "qgsproviderregistry.h"
#include "qgsrasterlayer.h"
#include "qgsrasterdataprovider.h"
#include "qgsrasterprojector.h"
#include "qgslogger.h"
#include "qgssettings.h"
#include "qgsgrassprovidermodule.h"
#include "qgsgrassprovider.h"
#include "qgsgrass.h"
#include "qgsgrassvector.h"
#ifdef HAVE_GUI
#include "qgsnewnamedialog.h"
#include "qgsgrassoptions.h"
#endif
#include <QAction>
#include <QFileInfo>
#include <QDir>
#include <QLabel>
#include <QObject>
#include <QProgressBar>
#include <QScrollBar>
#include <QTextEdit>
//----------------------- QgsGrassItemActions ------------------------------
#ifdef HAVE_GUI
QgsGrassItemActions::QgsGrassItemActions( const QgsGrassObject &grassObject, bool valid, QObject *parent )
: QObject( parent )
, mGrassObject( grassObject )
, mValid( valid )
{
}
QList<QAction *> QgsGrassItemActions::actions( QWidget *parent )
{
QList<QAction *> list;
QAction *optionsAction = new QAction( tr( "GRASS Options" ), parent );
connect( optionsAction, &QAction::triggered, QgsGrass::instance(), &QgsGrass::openOptions );
list << optionsAction;
bool isMapsetOwner = QgsGrass::isOwner( mGrassObject.gisdbase(), mGrassObject.location(), mGrassObject.mapset() );
// TODO: add icons to provider
// TODO: check ownership
if ( mGrassObject.type() == QgsGrassObject::Location )
{
QAction *newMapsetAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "grass_new_mapset.png" ) ), tr( "New mapset" ), parent );
connect( newMapsetAction, &QAction::triggered, this, &QgsGrassItemActions::newMapset );
list << newMapsetAction;
}
if ( mGrassObject.type() == QgsGrassObject::Mapset && isMapsetOwner )
{
QAction *openMapsetAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "grass_open_mapset.png" ) ), tr( "Open mapset" ), parent );
connect( openMapsetAction, &QAction::triggered, this, &QgsGrassItemActions::openMapset );
list << openMapsetAction;
}
if ( mGrassObject.type() == QgsGrassObject::Mapset && mGrassObject.locationIdentical( QgsGrass::getDefaultLocationObject() )
&& mGrassObject.mapset() != QgsGrass::getDefaultMapset() )
{
if ( !QgsGrass::instance()->isMapsetInSearchPath( mGrassObject.mapset() ) )
{
QAction *openMapsetAction = new QAction( tr( "Add mapset to search path" ), parent );
connect( openMapsetAction, &QAction::triggered, this, &QgsGrassItemActions::addMapsetToSearchPath );
list << openMapsetAction;
}
else
{
QAction *openMapsetAction = new QAction( tr( "Remove mapset from search path" ), parent );
connect( openMapsetAction, &QAction::triggered, this, &QgsGrassItemActions::removeMapsetFromSearchPath );
list << openMapsetAction;
}
}
if ( ( mGrassObject.type() == QgsGrassObject::Raster || mGrassObject.type() == QgsGrassObject::Vector
|| mGrassObject.type() == QgsGrassObject::Group ) && isMapsetOwner )
{
QAction *renameAction = new QAction( tr( "Rename" ), parent );
connect( renameAction, &QAction::triggered, this, &QgsGrassItemActions::renameGrassObject );
list << renameAction;
QAction *deleteAction = new QAction( tr( "Delete" ), parent );
connect( deleteAction, &QAction::triggered, this, &QgsGrassItemActions::deleteGrassObject );
list << deleteAction;
}
if ( ( mGrassObject.type() == QgsGrassObject::Mapset || mGrassObject.type() == QgsGrassObject::Vector )
&& mValid && isMapsetOwner )
{
// TODO: disable new layer actions on maps currently being edited
QAction *newPointAction = new QAction( tr( "New Point Layer" ), parent );
connect( newPointAction, &QAction::triggered, this, &QgsGrassItemActions::newPointLayer );
list << newPointAction;
QAction *newLineAction = new QAction( tr( "New Line Layer" ), parent );
connect( newLineAction, &QAction::triggered, this, &QgsGrassItemActions::newLineLayer );
list << newLineAction;
QAction *newPolygonAction = new QAction( tr( "New Polygon Layer" ), parent );
connect( newPolygonAction, &QAction::triggered, this, &QgsGrassItemActions::newPolygonLayer );
list << newPolygonAction;
}
return list;
}
void QgsGrassItemActions::newMapset()
{
QStringList existingNames = QgsGrass::mapsets( mGrassObject.gisdbase(), mGrassObject.mapsetPath() );
QgsDebugMsg( "existingNames = " + existingNames.join( "," ) );
QRegExp regExp = QgsGrassObject::newNameRegExp( QgsGrassObject::Mapset );
Qt::CaseSensitivity caseSensitivity = QgsGrass::caseSensitivity();
QgsNewNameDialog dialog( QLatin1String( "" ), QLatin1String( "" ), QStringList(), existingNames, regExp, caseSensitivity );
if ( dialog.exec() != QDialog::Accepted )
{
return;
}
QString name = dialog.name();
QgsDebugMsg( "name = " + name );
QString error;
QgsGrass::createMapset( mGrassObject.gisdbase(), mGrassObject.location(), name, error );
if ( !error.isEmpty() )
{
QgsGrass::warning( tr( "Cannot create new mapset: %1" ).arg( error ) );
}
}
void QgsGrassItemActions::openMapset()
{
QString error = QgsGrass::openMapset( mGrassObject.gisdbase(), mGrassObject.location(), mGrassObject.mapset() );
if ( !error.isEmpty() )
{
QgsGrass::warning( error );
return;
}
QgsGrass::saveMapset();
}
void QgsGrassItemActions::addMapsetToSearchPath()
{
QString error;
QgsGrass::instance()->addMapsetToSearchPath( mGrassObject.mapset(), error );
if ( !error.isEmpty() )
{
QgsGrass::warning( error );
return;
}
}
void QgsGrassItemActions::removeMapsetFromSearchPath()
{
QString error;
QgsGrass::instance()->removeMapsetFromSearchPath( mGrassObject.mapset(), error );
if ( !error.isEmpty() )
{
QgsGrass::warning( error );
return;
}
}
void QgsGrassItemActions::renameGrassObject()
{
QStringList existingNames = QgsGrass::grassObjects( mGrassObject, mGrassObject.type() );
// remove current name to avoid warning that exists
existingNames.removeOne( mGrassObject.name() );
QgsDebugMsg( "existingNames = " + existingNames.join( "," ) );
QRegExp regExp = QgsGrassObject::newNameRegExp( mGrassObject.type() );
Qt::CaseSensitivity caseSensitivity = QgsGrass::caseSensitivity();
QgsNewNameDialog dialog( mGrassObject.name(), mGrassObject.name(), QStringList(), existingNames, regExp, caseSensitivity );
if ( dialog.exec() != QDialog::Accepted || dialog.name() == mGrassObject.name() )
{
return;
}
QgsDebugMsg( "rename " + mGrassObject.name() + " -> " + dialog.name() );
QgsGrassObject obj( mGrassObject );
obj.setName( dialog.name() );
QString errorTitle = QObject::tr( "Rename GRASS %1" ).arg( mGrassObject.elementName() );
if ( QgsGrass::objectExists( obj ) )
{
QgsDebugMsg( obj.name() + " exists -> delete" );
if ( !QgsGrass::deleteObject( obj ) )
{
QgsMessageOutput::showMessage( errorTitle, QObject::tr( "Cannot delete %1" ).arg( obj.name() ), QgsMessageOutput::MessageText );
return;
}
}
try
{
QgsGrass::renameObject( mGrassObject, obj.name() );
}
catch ( QgsGrass::Exception &e )
{
QgsMessageOutput::showMessage( errorTitle,
QObject::tr( "Cannot rename %1 to %2" ).arg( mGrassObject.name(), obj.name() ) + "\n" + e.what(),
QgsMessageOutput::MessageText );
}
}
void QgsGrassItemActions::deleteGrassObject()
{
if ( !QgsGrass::deleteObjectDialog( mGrassObject ) )
return;
// Warning if failed is currently in QgsGrass::deleteMap
if ( QgsGrass::deleteObject( mGrassObject ) )
{
// message on success is redundant, like in normal file browser
}
}
QString QgsGrassItemActions::newVectorMap()
{
QStringList existingNames = QgsGrass::grassObjects( mGrassObject, QgsGrassObject::Vector );
QgsDebugMsg( "existingNames = " + existingNames.join( "," ) );
QRegExp regExp = QgsGrassObject::newNameRegExp( QgsGrassObject::Vector );
Qt::CaseSensitivity caseSensitivity = QgsGrass::caseSensitivity();
QgsNewNameDialog dialog( QLatin1String( "" ), QLatin1String( "" ), QStringList(), existingNames, regExp, caseSensitivity );
if ( dialog.exec() != QDialog::Accepted )
{
return QString();
}
QString name = dialog.name();
QgsDebugMsg( "name = " + name );
QgsGrassObject mapObject = mGrassObject;
mapObject.setName( name );
mapObject.setType( QgsGrassObject::Vector );
QString error;
QgsGrass::createVectorMap( mapObject, error );
if ( !error.isEmpty() )
{
QgsGrass::warning( error );
name = QLatin1String( "" );
}
return name;
}
void QgsGrassItemActions::newLayer( const QString &type )
{
QString name;
if ( mGrassObject.type() == QgsGrassObject::Mapset )
{
name = newVectorMap();
}
else if ( mGrassObject.type() == QgsGrassObject::Vector )
{
name = mGrassObject.name();
}
QgsDebugMsg( "name = " + name );
if ( name.isEmpty() )
{
QgsDebugMsg( "culd not create map" );
return;
}
// TODO: better to get max layer number from item?
QgsGrassObject grassObject = mGrassObject;
grassObject.setName( name );
grassObject.setType( QgsGrassObject::Vector );
QgsGrassVector vector( grassObject );
vector.openHead();
int layerNumber = vector.maxLayerNumber() + 1;
QgsDebugMsg( QString( "layerNumber = %1" ).arg( layerNumber ) );
QString uri = mGrassObject.mapsetPath() + "/" + name + QStringLiteral( "/%1_%2" ).arg( layerNumber ).arg( type );
QgsDebugMsg( "uri = " + uri );
QgsGrass::instance()->emitNewLayer( uri, name );
}
void QgsGrassItemActions::newPointLayer()
{
newLayer( QStringLiteral( "point" ) );
}
void QgsGrassItemActions::newLineLayer()
{
newLayer( QStringLiteral( "line" ) );
}
void QgsGrassItemActions::newPolygonLayer()
{
newLayer( QStringLiteral( "polygon" ) );
}
#endif
//----------------------- QgsGrassObjectItemBase ------------------------------
QgsGrassObjectItemBase::QgsGrassObjectItemBase( const QgsGrassObject &grassObject )
: mGrassObject( grassObject )
{
}
//----------------------- QgsGrassLocationItem ------------------------------
QgsGrassLocationItem::QgsGrassLocationItem( QgsDataItem *parent, QString dirPath, QString path )
: QgsDirectoryItem( parent, QLatin1String( "" ), dirPath, path )
, QgsGrassObjectItemBase( QgsGrassObject() )
{
// modify path to distinguish from directory, so that it can be expanded by path in browser
QDir dir( mDirPath );
mName = dir.dirName();
dir.cdUp();
QString gisdbase = dir.path();
mGrassObject = QgsGrassObject( gisdbase, mName, QLatin1String( "" ), QLatin1String( "" ), QgsGrassObject::Location );
#ifdef HAVE_GUI
mActions = new QgsGrassItemActions( mGrassObject, true, this );
#endif
mIconName = QStringLiteral( "grass_location.png" );
// set Directory type so that when sorted it gets into dirs (after the dir it represents)
mType = QgsDataItem::Directory;
}
QVector<QgsDataItem *>QgsGrassLocationItem::createChildren()
{
QVector<QgsDataItem *> mapsets;
QDir dir( mDirPath );
QStringList entries = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name );
Q_FOREACH ( const QString &name, entries )
{
QString path = dir.absoluteFilePath( name );
if ( QgsGrass::isMapset( path ) )
{
QgsGrassMapsetItem *mapset = new QgsGrassMapsetItem( this, path, mPath + "/" + name );
mapsets.append( mapset );
}
}
return mapsets;
}
//----------------------- QgsGrassMapsetItem ------------------------------
QList<QgsGrassImport *> QgsGrassMapsetItem::sImports;
QgsGrassMapsetItem::QgsGrassMapsetItem( QgsDataItem *parent, QString dirPath, QString path )
: QgsDirectoryItem( parent, QLatin1String( "" ), dirPath, path )
, QgsGrassObjectItemBase( QgsGrassObject() )
, mRefreshLater( false )
{
QDir dir( mDirPath );
mName = dir.dirName();
dir.cdUp();
QString location = dir.dirName();
dir.cdUp();
QString gisdbase = dir.path();
mGrassObject = QgsGrassObject( gisdbase, location, mName, QLatin1String( "" ), QgsGrassObject::Mapset );
#ifdef HAVE_GUI
mActions = new QgsGrassItemActions( mGrassObject, true, this );
#endif
// emit data changed to possibly change icon
connect( QgsGrass::instance(), &QgsGrass::mapsetChanged, this, &QgsGrassMapsetItem::updateIcon );
connect( QgsGrass::instance(), &QgsGrass::mapsetSearchPathChanged, this, &QgsGrassMapsetItem::updateIcon );
mIconName = QStringLiteral( "grass_mapset.png" );
}
QIcon QgsGrassMapsetItem::icon()
{
if ( mGrassObject == QgsGrass::getDefaultMapsetObject() )
{
return QgsApplication::getThemeIcon( "/grass_mapset_open.png" );
}
else if ( mGrassObject.locationIdentical( QgsGrass::getDefaultLocationObject() ) )
{
if ( QgsGrass::instance()->isMapsetInSearchPath( mGrassObject.mapset() ) )
{
return QgsApplication::getThemeIcon( "/grass_mapset_search.png" );
}
}
return QgsDirectoryItem::icon();
}
void QgsGrassMapsetItem::setState( State state )
{
// TODO: it seems to be causing strange icon switching during import, sometimes
if ( state == Populated )
{
if ( !mMapsetFileSystemWatcher )
{
mMapsetFileSystemWatcher = new QFileSystemWatcher( this );
mMapsetFileSystemWatcher->addPath( mDirPath + "/vector" );
mMapsetFileSystemWatcher->addPath( mDirPath + "/cellhd" );
connect( mMapsetFileSystemWatcher, &QFileSystemWatcher::directoryChanged, this, &QgsGrassMapsetItem::onDirectoryChanged );
}
}
else if ( state == NotPopulated )
{
if ( mMapsetFileSystemWatcher )
{
delete mMapsetFileSystemWatcher;
mMapsetFileSystemWatcher = nullptr;
}
}
QgsDirectoryItem::setState( state );
}
bool QgsGrassMapsetItem::objectInImports( const QgsGrassObject &grassObject )
{
Q_FOREACH ( QgsGrassImport *import, sImports )
{
if ( !import )
{
continue;
}
if ( !import->grassObject().mapsetIdentical( grassObject )
|| import->grassObject().type() != grassObject.type() )
{
continue;
}
if ( import->names().contains( grassObject.name() ) )
{
return true;
}
}
return false;
}
QVector<QgsDataItem *> QgsGrassMapsetItem::createChildren()
{
QVector<QgsDataItem *> items;
QStringList vectorNames = QgsGrass::vectors( mDirPath );
Q_FOREACH ( const QString &name, vectorNames )
{
if ( mRefreshLater )
{
deleteLater( items );
return items;
}
QgsGrassObject vectorObject = mGrassObject;
vectorObject.setName( name );
vectorObject.setType( QgsGrassObject::Vector );
// Skip temporary import maps. If Vect_open_old during refresh fails due to missing topo, hist file remains open
// and Windows does not allow deleting the temporary map to qgis.v.in. In any case we don't want to show temporary import maps.
// TODO: add some auto cleaning mechanism to remove temporary maps left after import fail
// keep excluded tmp name in sync with qgis.v.in
QgsDebugMsg( "name = " + name );
if ( name.startsWith( QLatin1String( "qgis_import_tmp_" ) ) )
{
QgsDebugMsg( "skip tmp import vector " + name );
continue;
}
if ( objectInImports( vectorObject ) )
{
QgsDebugMsg( "skip currently being imported vector " + name );
continue;
}
QString mapPath = mPath + "/vector/" + name;
QStringList layerNames;
QgsGrassVectorItem *map = nullptr;
// test topo version before getting layers, because GRASS 7 Vect_open_old is calling G_fatal_error
// if topo version does not match GRASS lib version
int topoMajor = 0;
int topoMinor = 0;
bool gotTopoVersion = QgsGrass::topoVersion( mGrassObject.gisdbase(), mGrassObject.location(), mGrassObject.mapset(), name, topoMajor, topoMinor );
QgsDebugMsg( QString( "name = %1 topoMajor = %2 topoMinor = %3" ).arg( name ).arg( topoMajor ).arg( topoMinor ) );
QString topoError;
if ( !gotTopoVersion )
{
topoError = tr( "topology missing" );
}
// GRASS 5-6: topoMajor = 5 topoMinor = 0
// GRASS 7: topoMajor = 5 topoMinor = 1
else if ( topoMajor != 5 )
{
topoError = tr( "topology version not supported" );
}
else if ( topoMinor == 0 && GRASS_VERSION_MAJOR == 7 )
{
topoError = tr( "topology version 6" );
}
if ( !topoError.isEmpty() )
{
map = new QgsGrassVectorItem( this, vectorObject, mapPath, name + " : " + topoError, false );
items.append( map );
continue;
}
try
{
layerNames = QgsGrass::vectorLayers( mGrassObject.gisdbase(), mGrassObject.location(), mGrassObject.mapset(), name );
}
catch ( QgsGrass::Exception &e )
{
map = new QgsGrassVectorItem( this, vectorObject, mapPath, name + " : " + e.what(), false );
items.append( map );
continue;
}
if ( layerNames.size() == 0 )
{
map = new QgsGrassVectorItem( this, vectorObject, mapPath, name + " : " + tr( "empty" ), true );
items.append( map );
continue;
}
else if ( layerNames.size() > 1 )
{
//map = new QgsDataCollectionItem( this, name, mapPath );
//map->setCapabilities( QgsDataItem::NoCapabilities ); // disable fertility
map = new QgsGrassVectorItem( this, vectorObject, mapPath );
}
Q_FOREACH ( const QString &layerName, layerNames )
{
// don't use QDir::separator(), windows work with '/' and backslash may be lost if
// somewhere not properly escaped (there was bug in QgsMimeDataUtils for example)
QString uri = mDirPath + "/" + name + "/" + layerName;
QgsLayerItem::LayerType layerType = QgsLayerItem::Vector;
QString typeName = layerName.split( '_' ).value( 1 );
QString baseLayerName = layerName.split( '_' ).value( 0 );
if ( typeName == QLatin1String( "point" ) || typeName == QLatin1String( "node" ) )
layerType = QgsLayerItem::Point;
else if ( typeName == QLatin1String( "line" ) )
layerType = QgsLayerItem::Line;
else if ( typeName == QLatin1String( "polygon" ) )
layerType = QgsLayerItem::Polygon;
QString layerPath = mapPath + "/" + layerName;
if ( !map )
{
/* This may happen (one layer only) in GRASS 7 with points (no topo layers) or if topo layers are disabled */
QgsLayerItem *layer = new QgsGrassVectorLayerItem( this, vectorObject, name, layerPath, uri, layerType, true );
//layer->setState( Populated );
items.append( layer );
}
else
{
QgsLayerItem *layer = new QgsGrassVectorLayerItem( map, vectorObject, baseLayerName, layerPath, uri, layerType, false );
map->addChild( layer );
}
}
if ( map )
{
map->setState( Populated );
items.append( map );
}
}
QStringList rasterNames = QgsGrass::rasters( mDirPath );
Q_FOREACH ( const QString &name, rasterNames )
{
if ( mRefreshLater )
{
deleteLater( items );
return items;
}
QString path = mPath + "/" + "raster" + "/" + name;
QString uri = mDirPath + "/" + "cellhd" + "/" + name;
QgsDebugMsg( "uri = " + uri );
QgsGrassObject rasterObject( mGrassObject.gisdbase(), mGrassObject.location(), mGrassObject.mapset(), name, QgsGrassObject::Raster );
if ( objectInImports( rasterObject ) )
{
QgsDebugMsg( "skip currently being imported raster " + name );
continue;
}
QgsGrassRasterItem *layer = new QgsGrassRasterItem( this, rasterObject, path, uri, QgsGrass::isExternal( rasterObject ) );
items.append( layer );
}
QStringList groupNames = QgsGrass::groups( mDirPath );
Q_FOREACH ( const QString &name, groupNames )
{
if ( mRefreshLater )
{
deleteLater( items );
return items;
}
QString path = mPath + "/" + "group" + "/" + name;
QString uri = mDirPath + "/" + "group" + "/" + name;
QgsDebugMsg( "uri = " + uri );
QgsGrassObject rasterObject( mGrassObject.gisdbase(), mGrassObject.location(), mGrassObject.mapset(), name, QgsGrassObject::Group );
QgsGrassGroupItem *layer = new QgsGrassGroupItem( this, rasterObject, path, uri );
items.append( layer );
}
Q_FOREACH ( QgsGrassImport *import, sImports )
{
if ( mRefreshLater )
{
deleteLater( items );
return items;
}
if ( !import )
{
continue;
}
if ( !import->grassObject().mapsetIdentical( mGrassObject ) )
{
continue;
}
Q_FOREACH ( const QString &name, import->names() )
{
QString path = mPath + "/" + import->grassObject().elementName() + "/" + name;
items.append( new QgsGrassImportItem( this, name, path, import ) );
}
}
return items;
}
bool QgsGrassMapsetItem::acceptDrop()
{
return QgsGrass::isOwner( mGrassObject.gisdbase(), mGrassObject.location(), mGrassObject.mapset() );
}
bool QgsGrassMapsetItem::handleDrop( const QMimeData *data, Qt::DropAction )
{
if ( !QgsMimeDataUtils::isUriList( data ) )
return false;
QgsSettings settings;
QgsCoordinateReferenceSystem mapsetCrs = QgsGrass::crsDirect( mGrassObject.gisdbase(), mGrassObject.location() );
QStringList existingRasters = QgsGrass::rasters( mGrassObject.mapsetPath() );
QStringList existingVectors = QgsGrass::vectors( mGrassObject.mapsetPath() );
// add currently being imported
Q_FOREACH ( QgsGrassImport *import, sImports )
{
if ( import && import->grassObject().type() == QgsGrassObject::Raster )
{
existingRasters.append( import->names() );
}
else if ( import && import->grassObject().type() == QgsGrassObject::Vector )
{
existingVectors.append( import->names() );
}
}
QStringList errors;
QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( data );
Q_FOREACH ( const QgsMimeDataUtils::Uri &u, lst )
{
if ( u.layerType != QLatin1String( "raster" ) && u.layerType != QLatin1String( "vector" ) )
{
errors.append( tr( "%1 layer type not supported" ).arg( u.name ) );
continue;
}
QgsRasterDataProvider *rasterProvider = nullptr;
QgsVectorDataProvider *vectorProvider = nullptr;
QgsDataProvider *provider = nullptr;
QStringList extensions;
QStringList existingNames;
QRegExp regExp;
QgsGrassObject srcObject;
QString srcName;
// use g.copy for GRASS maps in the same location
bool useCopy = false;
if ( u.layerType == QLatin1String( "raster" ) )
{
if ( u.providerKey == QLatin1String( "grassraster" ) && srcObject.setFromUri( u.uri )
&& srcObject.locationIdentical( mGrassObject ) )
{
useCopy = true;
}
else
{
rasterProvider = qobject_cast<QgsRasterDataProvider *>( QgsProviderRegistry::instance()->createProvider( u.providerKey, u.uri ) );
provider = rasterProvider;
}
existingNames = existingRasters;
regExp = QgsGrassObject::newNameRegExp( QgsGrassObject::Raster );
}
else if ( u.layerType == QLatin1String( "vector" ) )
{
if ( u.providerKey == QLatin1String( "grass" ) && srcObject.setFromUri( u.uri )
&& srcObject.locationIdentical( mGrassObject ) )
{
useCopy = true;
}
else
{
vectorProvider = qobject_cast<QgsVectorDataProvider *>( QgsProviderRegistry::instance()->createProvider( u.providerKey, u.uri ) );
provider = vectorProvider;
}
existingNames = existingVectors;
regExp = QgsGrassObject::newNameRegExp( QgsGrassObject::Vector );
}
QgsDebugMsg( "existingNames = " + existingNames.join( "," ) );
if ( useCopy )
{
srcName = srcObject.name();
}
else
{
if ( !provider )
{
errors.append( tr( "Cannot create provider %1 : %2" ).arg( u.providerKey, u.uri ) );
continue;
}
if ( !provider->isValid() )
{
errors.append( tr( "Provider is not valid %1 : %2" ).arg( u.providerKey, u.uri ) );
delete provider;
continue;
}
if ( u.layerType == QLatin1String( "raster" ) )
{
extensions = QgsGrassRasterImport::extensions( rasterProvider );
}
srcName = u.name;
}
// TODO: add a method in QgsGrass to convert a name to GRASS valid name
QString destName = srcName.replace( QLatin1String( " " ), QLatin1String( "_" ) );
#ifdef HAVE_GUI
Qt::CaseSensitivity caseSensitivity = QgsGrass::caseSensitivity();
if ( QgsNewNameDialog::exists( destName, extensions, existingNames, caseSensitivity )
|| !regExp.exactMatch( destName ) )
{
QgsNewNameDialog dialog( srcName, destName, extensions, existingNames, regExp, caseSensitivity );
if ( dialog.exec() != QDialog::Accepted )
{
delete provider;
continue;
}
destName = dialog.name();
}
#endif
QgsGrassImport *import = nullptr;
if ( useCopy )
{
QgsDebugMsg( "location is the same -> g.copy" );
QgsGrassObject destObject( mGrassObject );
destObject.setName( destName );
destObject.setType( srcObject.type() );
import = new QgsGrassCopy( srcObject, destObject );
}
else if ( u.layerType == QLatin1String( "raster" ) )
{
QgsRectangle newExtent = rasterProvider->extent();
int newXSize;
int newYSize;
bool useSrcRegion = true;
if ( rasterProvider->capabilities() & QgsRasterInterface::Size )
{
newXSize = rasterProvider->xSize();
newYSize = rasterProvider->ySize();
}
else
{
// TODO: open dialog with size options
// use location default
QgsDebugMsg( "Unknown size -> using default location region" );
struct Cell_head window;
if ( !QgsGrass::defaultRegion( mGrassObject.gisdbase(), mGrassObject.location(), &window ) )
{
errors.append( tr( "Cannot get default location region." ) );
delete rasterProvider;
continue;
}
newXSize = window.cols;
newYSize = window.rows;
newExtent = QgsGrass::extent( &window );
useSrcRegion = false;
}
QgsCoordinateReferenceSystem providerCrs = rasterProvider->crs();
QgsDebugMsg( "providerCrs = " + providerCrs.toWkt() );
QgsDebugMsg( "mapsetCrs = " + mapsetCrs.toWkt() );
bool settingsExternal = settings.value( QStringLiteral( "GRASS/browser/import/external" ), true ).toBool();
QgsGrassObject rasterObject( mGrassObject.gisdbase(), mGrassObject.location(), mGrassObject.mapset(), destName, QgsGrassObject::Raster );
if ( providerCrs.isValid() && mapsetCrs.isValid() && providerCrs == mapsetCrs
&& rasterProvider->name() == QLatin1String( "gdal" ) && settingsExternal )
{
import = new QgsGrassExternal( rasterProvider->dataSourceUri(), rasterObject );
delete rasterProvider;
}
else
{
QgsRasterPipe *pipe = new QgsRasterPipe();
pipe->set( rasterProvider );
if ( providerCrs.isValid() && mapsetCrs.isValid() && providerCrs != mapsetCrs )
{
QgsRasterProjector *projector = new QgsRasterProjector;
projector->setCrs( providerCrs, mapsetCrs );
if ( useSrcRegion )
{
projector->destExtentSize( rasterProvider->extent(), rasterProvider->xSize(), rasterProvider->ySize(),
newExtent, newXSize, newYSize );
}
QgsRasterProjector::Precision precision = ( QgsRasterProjector::Precision ) settings.value( QStringLiteral( "GRASS/browser/import/crsTransform" ), QgsRasterProjector::Approximate ).toInt();
projector->setPrecision( precision );
pipe->set( projector );
}
QgsDebugMsg( "newExtent = " + newExtent.toString() );
QgsDebugMsg( QString( "newXSize = %1 newYSize = %2" ).arg( newXSize ).arg( newYSize ) );
//QString path = mPath + "/" + "raster" + "/" + u.name;
import = new QgsGrassRasterImport( pipe, rasterObject, newExtent, newXSize, newYSize ); // takes pipe ownership
}
}
else if ( u.layerType == QLatin1String( "vector" ) )
{
QgsGrassObject vectorObject( mGrassObject.gisdbase(), mGrassObject.location(), mGrassObject.mapset(), destName, QgsGrassObject::Vector );
import = new QgsGrassVectorImport( vectorProvider, vectorObject ); // takes provider ownership
}
else
{
return false;
}
connect( import, &QgsGrassImport::finished, this, &QgsGrassMapsetItem::onImportFinished );
// delete existing files (confirmed before in dialog)
bool deleteOk = true;
Q_FOREACH ( const QString &name, import->names() )
{
QgsGrassObject obj( import->grassObject() );
obj.setName( name );
if ( QgsGrass::objectExists( obj ) )
{
QgsDebugMsg( name + " exists -> delete" );
if ( !QgsGrass::deleteObject( obj ) )
{
errors.append( tr( "Cannot delete %1" ).arg( name ) );
deleteOk = false;
}
}
}
if ( !deleteOk )
{
delete import;
continue;
}
import->importInThread();
sImports.append( import );
if ( u.layerType == QLatin1String( "raster" ) )
{
existingRasters.append( import->names() );
}
else if ( u.layerType == QLatin1String( "vector" ) )
{
existingVectors.append( import->names() );
}
refresh(); // after each new item so that it is visible if dialog is opened on next item
}
if ( !errors.isEmpty() )
{
QgsMessageOutput::showMessage( tr( "Import to GRASS mapset" ),
tr( "Failed to import some layers!\n\n" ) + errors.join( QStringLiteral( "\n" ) ),
QgsMessageOutput::MessageText );
}
return true;
}
void QgsGrassMapsetItem::onImportFinished( QgsGrassImport *import )
{
if ( !import->error().isEmpty() )
{
QgsMessageOutput *output = QgsMessageOutput::createMessageOutput();
output->setTitle( tr( "Import to GRASS mapset failed" ) );
output->setMessage( tr( "Failed to import %1 to %2: %3" ).arg( import->srcDescription(),
import->grassObject().mapsetPath(),
import->error() ), QgsMessageOutput::MessageText );
output->showMessage();
}
sImports.removeOne( import );
import->deleteLater();
refresh();
}
void QgsGrassMapsetItem::onDirectoryChanged()
{
if ( state() == Populating )
{
// schedule to refresh later, because refres() simply returns if Populating
mRefreshLater = true;
}
else
{
refresh();
}
}
void QgsGrassMapsetItem::childrenCreated()
{
QgsDebugMsg( QString( "mRefreshLater = %1" ).arg( mRefreshLater ) );
if ( mRefreshLater )
{
QgsDebugMsg( "directory changed during createChidren() -> refresh() again" );
mRefreshLater = false;
setState( Populated );
refresh();
}
else
{
QgsDirectoryItem::childrenCreated();
}
}
//------------------------ QgsGrassObjectItem ----------------------------------
QgsGrassObjectItem::QgsGrassObjectItem( QgsDataItem *parent, QgsGrassObject grassObject,
QString name, QString path, QString uri,
LayerType layerType, QString providerKey )
: QgsLayerItem( parent, name, path, uri, layerType, providerKey )
, QgsGrassObjectItemBase( grassObject )
{
setState( Populated ); // no children, to show non expandable in browser
#ifdef HAVE_GUI
mActions = new QgsGrassItemActions( mGrassObject, true, this );
#endif
}
bool QgsGrassObjectItem::equal( const QgsDataItem *other )
{
const QgsGrassObjectItem *item = qobject_cast<const QgsGrassObjectItem *>( other );
return QgsLayerItem::equal( other ) && item && mGrassObject == item->mGrassObject;
}
//----------------------- QgsGrassVectorItem ------------------------------
QgsGrassVectorItem::QgsGrassVectorItem( QgsDataItem *parent, QgsGrassObject grassObject, QString path, QString labelName, bool valid )
: QgsDataCollectionItem( parent, labelName.isEmpty() ? grassObject.name() : labelName, path )
, QgsGrassObjectItemBase( grassObject )
, mValid( valid )
{
QgsDebugMsg( "name = " + grassObject.name() + " path = " + path );
mCapabilities = NoCapabilities; // disable Fertile from QgsDataCollectionItem
setCapabilities( QgsDataItem::NoCapabilities ); // disable fertility
if ( !mValid )
{
setState( Populated );
setIconName( QStringLiteral( "/mIconDelete.png" ) );
}
#ifdef HAVE_GUI