1818#include " qgsrasterformatsaveoptionswidget.h"
1919#include " qgslogger.h"
2020#include " qgsdialog.h"
21-
22- #include " gdal.h"
23- #include " cpl_string.h"
24- #include " cpl_conv.h"
25- #include " cpl_minixml.h"
21+ #include " qgsrasterlayer.h"
22+ #include " qgsproviderregistry.h"
2623
2724#include < QSettings>
2825#include < QInputDialog>
3128#include < QMouseEvent>
3229#include < QMenu>
3330
34- // todo put this somewhere else - how can we access gdal provider?
35- char ** papszFromStringList ( const QStringList& list )
36- {
37- char **papszRetList = NULL ;
38- foreach ( QString elem, list )
39- {
40- papszRetList = CSLAddString ( papszRetList, elem.toLocal8Bit ().constData () );
41- }
42- return papszRetList;
43- }
4431
4532QMap< QString, QStringList > QgsRasterFormatSaveOptionsWidget::mBuiltinProfiles ;
4633
4734QgsRasterFormatSaveOptionsWidget::QgsRasterFormatSaveOptionsWidget ( QWidget* parent, QString format,
48- QgsRasterFormatSaveOptionsWidget::Type type,
49- QString provider )
50- : QWidget( parent ), mFormat( format ), mProvider( provider )
35+ QgsRasterFormatSaveOptionsWidget::Type type, QString provider )
36+ : QWidget( parent ), mFormat( format ), mProvider( provider ), mRasterLayer( 0 )
5137
5238{
5339 setupUi ( this );
5440
55-
5641 setType ( type );
5742
5843 if ( mBuiltinProfiles .isEmpty () )
@@ -104,6 +89,7 @@ QgsRasterFormatSaveOptionsWidget::QgsRasterFormatSaveOptionsWidget( QWidget* par
10489 mOptionsLineEdit ->installEventFilter ( this );
10590 mOptionsStackedWidget ->installEventFilter ( this );
10691
92+ updateControls ();
10793 updateProfiles ();
10894}
10995
@@ -114,12 +100,14 @@ QgsRasterFormatSaveOptionsWidget::~QgsRasterFormatSaveOptionsWidget()
114100void QgsRasterFormatSaveOptionsWidget::setFormat ( QString format )
115101{
116102 mFormat = format;
103+ updateControls ();
117104 updateProfiles ();
118105}
119106
120107void QgsRasterFormatSaveOptionsWidget::setProvider ( QString provider )
121108{
122109 mProvider = provider;
110+ updateControls ();
123111}
124112
125113// show/hide widgets - we need this function if widget is used in creator
@@ -236,27 +224,35 @@ void QgsRasterFormatSaveOptionsWidget::apply()
236224 setCreateOptions ();
237225}
238226
227+ // typedefs for gdal provider function pointers
228+ typedef QString validateCreationOptionsFormat_t ( const QStringList& createOptions, QString format );
229+ typedef QString helpCreationOptionsFormat_t ( QString format );
239230
240231void QgsRasterFormatSaveOptionsWidget::helpOptions ()
241232{
242233 QString message;
243234
244235 if ( mProvider == " gdal" && mFormat != " " && mFormat != " _pyramids" )
245236 {
246- GDALDriverH myGdalDriver = GDALGetDriverByName ( mFormat .toLocal8Bit ().constData () );
247- if ( myGdalDriver )
237+ // get helpCreationOptionsFormat() function ptr for provider
238+ QLibrary *library = QgsProviderRegistry::instance ()->providerLibrary ( mProvider );
239+ if ( library )
248240 {
249- // need to serialize xml to get newlines
250- CPLXMLNode *psCOL = CPLParseXMLString ( GDALGetMetadataItem ( myGdalDriver,
251- GDAL_DMD_CREATIONOPTIONLIST , " " ) );
252- char *pszFormattedXML = CPLSerializeXMLTree ( psCOL );
253- if ( pszFormattedXML )
254- message = tr ( " Create Options: \n\n %1 " ). arg ( pszFormattedXML );
255- if ( psCOL )
256- CPLDestroyXMLNode ( psCOL );
257- if ( pszFormattedXML )
258- CPLFree ( pszFormattedXML );
241+ helpCreationOptionsFormat_t * helpCreationOptionsFormat =
242+ ( helpCreationOptionsFormat_t * ) cast_to_fptr ( library-> resolve ( " helpCreationOptionsFormat " ) );
243+ if ( helpCreationOptionsFormat )
244+ {
245+ message = helpCreationOptionsFormat ( mFormat );
246+ }
247+ else
248+ {
249+ message = library-> fileName () + " does not have helpCreationOptionsFormat " ;
250+ }
259251 }
252+ else
253+ message = QString ( " cannot load provider library %1" ).arg ( mProvider );
254+
255+
260256 if ( message.isEmpty () )
261257 message = tr ( " Cannot get create options for driver %1" ).arg ( mFormat );
262258 }
@@ -267,36 +263,65 @@ void QgsRasterFormatSaveOptionsWidget::helpOptions()
267263 QgsDialog *dlg = new QgsDialog ( this );
268264 QTextEdit *textEdit = new QTextEdit ( dlg );
269265 textEdit->setReadOnly ( true );
266+ message = tr ( " Create Options:\n\n %1" ).arg ( message );
270267 textEdit->setText ( message );
271268 dlg->layout ()->addWidget ( textEdit );
272- dlg->resize ( 600 , 600 );
269+ dlg->resize ( 600 , 400 );
273270 dlg->show (); // non modal
274271}
275272
276- bool QgsRasterFormatSaveOptionsWidget::validateOptions ( bool gui )
273+ QString QgsRasterFormatSaveOptionsWidget::validateOptions ( bool gui, bool reportOK )
277274{
278275 QStringList createOptions = options ();
279- bool ok = false ;
276+ QString message ;
280277
281278 if ( !createOptions.isEmpty () && mProvider == " gdal" && mFormat != " " && mFormat != " _pyramids" )
282279 {
283- GDALDriverH myGdalDriver = GDALGetDriverByName ( mFormat .toLocal8Bit ().constData () );
284- if ( myGdalDriver )
280+ if ( mRasterLayer )
281+ {
282+ QgsDebugMsg ( " calling validate on layer's data provider" );
283+ message = mRasterLayer ->dataProvider ()->validateCreationOptions ( createOptions, mFormat );
284+ }
285+ else
285286 {
286- // print error string?
287- char ** papszOptions = papszFromStringList ( createOptions );
288- ok = GDALValidateCreationOptions ( myGdalDriver, papszOptions );
289- CSLDestroy ( papszOptions );
290- if ( gui )
287+ // get validateCreationOptionsFormat() function ptr for provider
288+ QLibrary *library = QgsProviderRegistry::instance ()->providerLibrary ( mProvider );
289+ if ( library )
291290 {
292- if ( ok )
293- QMessageBox::information ( this , " " , tr ( " Valid" ), QMessageBox::Close );
291+ validateCreationOptionsFormat_t * validateCreationOptionsFormat =
292+ ( validateCreationOptionsFormat_t * ) cast_to_fptr ( library->resolve ( " validateCreationOptionsFormat" ) );
293+ if ( validateCreationOptionsFormat )
294+ {
295+ message = validateCreationOptionsFormat ( createOptions, mFormat );
296+ }
294297 else
295- QMessageBox::warning ( this , " " , tr ( " Invalid" ), QMessageBox::Close );
298+ {
299+ message = library->fileName () + " does not have validateCreationOptionsFormat" ;
300+ }
301+ }
302+ else
303+ message = QString ( " cannot load provider library %1" ).arg ( mProvider );
304+ }
305+
306+ if ( gui )
307+ {
308+ if ( message.isNull () )
309+ {
310+ if ( reportOK )
311+ QMessageBox::information ( this , " " , tr ( " Valid" ), QMessageBox::Close );
312+ }
313+ else
314+ {
315+ QMessageBox::warning ( this , " " , tr ( " Invalid creation option :\n\n %1\n\n Click on help button to get valid creation options for this format" ).arg ( message ), QMessageBox::Close );
296316 }
297317 }
298318 }
299- return ok;
319+ else
320+ {
321+ QMessageBox::information ( this , " " , tr ( " Cannot validate" ), QMessageBox::Close );
322+ }
323+
324+ return message;
300325}
301326
302327void QgsRasterFormatSaveOptionsWidget::optionsTableChanged ()
@@ -481,6 +506,13 @@ void QgsRasterFormatSaveOptionsWidget::swapOptionsUI( int newIndex )
481506 updateOptions ();
482507}
483508
509+ void QgsRasterFormatSaveOptionsWidget::updateControls ()
510+ {
511+ bool enabled = ( mProvider == " gdal" && mFormat != " " && mFormat != " _pyramids" );
512+ mOptionsValidateButton ->setEnabled ( enabled );
513+ mOptionsHelpButton ->setEnabled ( enabled );
514+ }
515+
484516// map options label left mouse click to optionsToggle()
485517bool QgsRasterFormatSaveOptionsWidget::eventFilter ( QObject *obj, QEvent *event )
486518{
0 commit comments