44
44
static QString PROVIDER_KEY = " grassraster" ;
45
45
static QString PROVIDER_DESCRIPTION = QString( " GRASS %1 raster provider" ).arg( GRASS_VERSION_MAJOR );
46
46
47
+ // Do not use warning dialogs, providers are also created on threads (rendering) where dialogs connot be used (constructing QPixmap icon)
48
+
47
49
QgsGrassRasterProvider::QgsGrassRasterProvider ( QString const & uri )
48
50
: QgsRasterDataProvider( uri )
49
51
, mValid( false )
@@ -94,14 +96,21 @@ QgsGrassRasterProvider::QgsGrassRasterProvider( QString const & uri )
94
96
mRasterValue .start ( mGisdbase , mLocation , mMapset , mMapName );
95
97
// mValidNoDataValue = true;
96
98
97
- mCrs = QgsGrass::crs ( mGisdbase , mLocation );
99
+ QString error;
100
+ mCrs = QgsGrass::crs ( mGisdbase , mLocation , error );
101
+ appendIfError ( error );
98
102
QgsDebugMsg ( " mCrs: " + mCrs .toWkt () );
99
103
100
104
// the block size can change of course when the raster is overridden
101
105
// ibut it is only called once when statistics are calculated
102
- QgsGrass::size ( mGisdbase , mLocation , mMapset , mMapName , &mCols , &mRows );
106
+ error.clear ();
107
+ QgsGrass::size ( mGisdbase , mLocation , mMapset , mMapName , &mCols , &mRows , error );
108
+ appendIfError ( error );
103
109
104
- mInfo = QgsGrass::info ( mGisdbase , mLocation , mMapset , mMapName , QgsGrassObject::Raster );
110
+ error.clear ();
111
+ mInfo = QgsGrass::info ( mGisdbase , mLocation , mMapset , mMapName , QgsGrassObject::Raster,
112
+ " info" , QgsRectangle (), 0 , 0 , 3000 , error );
113
+ appendIfError ( error );
105
114
106
115
mGrassDataType = mInfo [" TYPE" ].toInt ();
107
116
QgsDebugMsg ( " mGrassDataType = " + QString::number ( mGrassDataType ) );
@@ -177,6 +186,7 @@ QImage* QgsGrassRasterProvider::draw( QgsRectangle const & viewExtent, int pixe
177
186
QgsDebugMsg ( " pixelWidth = " + QString::number ( pixelWidth ) );
178
187
QgsDebugMsg ( " pixelHeight = " + QString::number ( pixelHeight ) );
179
188
QgsDebugMsg ( " viewExtent: " + viewExtent.toString () );
189
+ clearLastError ();
180
190
181
191
QImage *image = new QImage ( pixelWidth, pixelHeight, QImage::Format_ARGB32 );
182
192
image->fill ( QColor ( Qt::gray ).rgb () );
@@ -198,9 +208,9 @@ QImage* QgsGrassRasterProvider::draw( QgsRectangle const & viewExtent, int pixe
198
208
}
199
209
catch ( QgsGrass::Exception &e )
200
210
{
201
- QMessageBox::warning ( 0 , QObject::tr ( " Warning " ), QObject:: tr ( " Cannot draw raster" ) + " \n "
202
- + e. what () );
203
-
211
+ QString error = tr ( " Cannot draw raster" ) + " : " + e. what ();
212
+ QgsDebugMsg ( error );
213
+ appendError ( error );
204
214
// We don't set mValid to false, because the raster can be recreated and work next time
205
215
return image;
206
216
}
@@ -219,6 +229,7 @@ void QgsGrassRasterProvider::readBlock( int bandNo, int xBlock, int yBlock, void
219
229
{
220
230
Q_UNUSED ( xBlock );
221
231
QgsDebugMsg ( " Entered" );
232
+ clearLastError ();
222
233
// TODO: optimize, see extent()
223
234
224
235
QgsDebugMsg ( " yBlock = " + QString::number ( yBlock ) );
@@ -251,9 +262,9 @@ void QgsGrassRasterProvider::readBlock( int bandNo, int xBlock, int yBlock, void
251
262
}
252
263
catch ( QgsGrass::Exception &e )
253
264
{
254
- QMessageBox::warning ( 0 , QObject::tr ( " Warning " ), QObject:: tr ( " Cannot draw raster" ) + " \n "
255
- + e. what () );
256
-
265
+ QString error = tr ( " Cannot read raster" ) + " : " + e. what ();
266
+ QgsDebugMsg ( error );
267
+ appendError ( error );
257
268
// We don't set mValid to false, because the raster can be recreated and work next time
258
269
}
259
270
QgsDebugMsg ( QString ( " %1 bytes read from modules stdout" ).arg ( data.size () ) );
@@ -263,8 +274,9 @@ void QgsGrassRasterProvider::readBlock( int bandNo, int xBlock, int yBlock, void
263
274
QgsDebugMsg ( QString ( " mCols = %1 mYBlockSize = %2 dataTypeSize = %3" ).arg ( mCols ).arg ( mYBlockSize ).arg ( dataTypeSize ( bandNo ) ) );
264
275
if ( size != data.size () )
265
276
{
266
- QMessageBox::warning ( 0 , QObject::tr ( " Warning" ),
267
- QString ( " %1 bytes expected but %2 byte were read from qgis.d.rast" ).arg ( size ).arg ( data.size () ) );
277
+ QString error = tr ( " %1 bytes expected but %2 byte were read from qgis.d.rast" ).arg ( size ).arg ( data.size () );
278
+ QgsDebugMsg ( error );
279
+ appendError ( error );
268
280
size = size < data.size () ? size : data.size ();
269
281
}
270
282
memcpy ( block, data.data (), size );
@@ -276,6 +288,7 @@ void QgsGrassRasterProvider::readBlock( int bandNo, QgsRectangle const & viewEx
276
288
QgsDebugMsg ( " pixelWidth = " + QString::number ( pixelWidth ) );
277
289
QgsDebugMsg ( " pixelHeight = " + QString::number ( pixelHeight ) );
278
290
QgsDebugMsg ( " viewExtent: " + viewExtent.toString () );
291
+ clearLastError ();
279
292
280
293
if ( pixelWidth <= 0 || pixelHeight <= 0 )
281
294
return ;
@@ -298,8 +311,9 @@ void QgsGrassRasterProvider::readBlock( int bandNo, QgsRectangle const & viewEx
298
311
}
299
312
catch ( QgsGrass::Exception &e )
300
313
{
301
- QMessageBox::warning ( 0 , QObject::tr ( " Warning" ), QObject::tr ( " Cannot draw raster" ) + " \n "
302
- + e.what () );
314
+ QString error = tr ( " Cannot read raster" ) + " : " + e.what ();
315
+ QgsDebugMsg ( error );
316
+ appendError ( error );
303
317
304
318
// We don't set mValid to false, because the raster can be recreated and work next time
305
319
return ;
@@ -310,8 +324,9 @@ void QgsGrassRasterProvider::readBlock( int bandNo, QgsRectangle const & viewEx
310
324
int size = pixelWidth * pixelHeight * dataTypeSize ( bandNo );
311
325
if ( size != data.size () )
312
326
{
313
- QMessageBox::warning ( 0 , QObject::tr ( " Warning" ),
314
- QString ( " %1 bytes expected but %2 byte were read from qgis.d.rast" ).arg ( size ).arg ( data.size () ) );
327
+ QString error = tr ( " %1 bytes expected but %2 byte were read from qgis.d.rast" ).arg ( size ).arg ( data.size () );
328
+ QgsDebugMsg ( error );
329
+ appendError ( error );
315
330
size = size < data.size () ? size : data.size ();
316
331
}
317
332
memcpy ( block, data.data (), size );
@@ -341,9 +356,11 @@ QgsRasterBandStats QgsGrassRasterProvider::bandStatistics( int theBandNo, int th
341
356
// 0.001 / cell should be sufficient using 0.005 to be sure + constant (ms)
342
357
int timeout = 30000 + 0.005 * xSize () * ySize ();
343
358
344
- QHash<QString, QString> info = QgsGrass::info ( mGisdbase , mLocation , mMapset , mMapName , QgsGrassObject::Raster, " stats" , extent, sampleRows, sampleCols, timeout );
359
+ QString error;
360
+ QHash<QString, QString> info = QgsGrass::info ( mGisdbase , mLocation , mMapset , mMapName , QgsGrassObject::Raster,
361
+ " stats" , extent, sampleRows, sampleCols, timeout, error );
345
362
346
- if ( info.isEmpty () )
363
+ if ( info.isEmpty () || !error. isEmpty () )
347
364
{
348
365
return myRasterBandStats;
349
366
}
@@ -381,7 +398,12 @@ QList<QgsColorRampShader::ColorRampItem> QgsGrassRasterProvider::colorTable( int
381
398
// for now we just believe that they are continuous, i.e. end and beginning
382
399
// of the ramp with the same value has the same color
383
400
// we are also expecting ordered CT records in the list
384
- QList<QgsGrass::Color> colors = QgsGrass::colors ( mGisdbase , mLocation , mMapset , mMapName );
401
+ QString error;
402
+ QList<QgsGrass::Color> colors = QgsGrass::colors ( mGisdbase , mLocation , mMapset , mMapName , error );
403
+ if ( !error.isEmpty () )
404
+ {
405
+ return ct;
406
+ }
385
407
QList<QgsGrass::Color>::iterator i;
386
408
387
409
double v = 0.0 , r = 0.0 , g = 0.0 , b = 0.0 ;
@@ -419,7 +441,8 @@ QgsRectangle QgsGrassRasterProvider::extent()
419
441
// The extend can change of course so we get always fresh, to avoid running always the module
420
442
// we should save mExtent and mLastModified and check if the map was modified
421
443
422
- mExtent = QgsGrass::extent ( mGisdbase , mLocation , mMapset , mMapName , QgsGrassObject::Raster );
444
+ QString error;
445
+ mExtent = QgsGrass::extent ( mGisdbase , mLocation , mMapset , mMapName , QgsGrassObject::Raster, error );
423
446
424
447
QgsDebugMsg ( " Extent got" );
425
448
return mExtent ;
@@ -561,14 +584,34 @@ bool QgsGrassRasterProvider::isValid()
561
584
return mValid ;
562
585
}
563
586
587
+ void QgsGrassRasterProvider::setLastError ( QString error )
588
+ {
589
+ mLastErrorTitle = tr ( " GRASS raster provider" );
590
+ mLastError = error;
591
+ }
592
+
593
+ void QgsGrassRasterProvider::clearLastError ()
594
+ {
595
+ mLastErrorTitle .clear ();
596
+ mLastError .clear ();
597
+ }
598
+
599
+ void QgsGrassRasterProvider::appendIfError ( QString error )
600
+ {
601
+ if ( !error.isEmpty () )
602
+ {
603
+ appendError ( ERR ( error ) );
604
+ }
605
+ }
606
+
564
607
QString QgsGrassRasterProvider::lastErrorTitle ()
565
608
{
566
- return QString ( " Not implemented " ) ;
609
+ return mLastErrorTitle ;
567
610
}
568
611
569
612
QString QgsGrassRasterProvider::lastError ()
570
613
{
571
- return QString ( " Not implemented " ) ;
614
+ return mLastError ;
572
615
}
573
616
574
617
QString QgsGrassRasterProvider::name () const
@@ -647,7 +690,16 @@ void QgsGrassRasterValue::start( QString gisdbase, QString location,
647
690
648
691
arguments.append ( " info=query" );
649
692
arguments.append ( " rast=" + mMapName + " @" + mMapset );
650
- mProcess = QgsGrass::startModule ( mGisdbase , mLocation , mMapset , module , arguments, mGisrcFile );
693
+ try
694
+ {
695
+ mProcess = QgsGrass::startModule ( mGisdbase , mLocation , mMapset , module , arguments, mGisrcFile );
696
+ }
697
+ catch ( QgsGrass::Exception &e )
698
+ {
699
+ QString error = e.what ();
700
+ Q_UNUSED ( error )
701
+ QgsDebugMsg ( error );
702
+ }
651
703
}
652
704
QgsGrassRasterValue::~QgsGrassRasterValue ()
653
705
{
0 commit comments