27
27
#include < QPushButton>
28
28
#include < QStandardItemModel>
29
29
30
- QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog ( QgsStyleV2* style, QWidget *parent, Mode mode, QString fileName )
30
+ QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog ( QgsStyleV2* style, QWidget *parent, Mode mode )
31
31
: QDialog( parent )
32
- , mFileName( fileName )
33
32
, mDialogMode( mode )
34
33
, mQgisStyle( style )
35
34
{
@@ -49,18 +48,37 @@ QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog( QgsStyleV2* style, Q
49
48
listItems->setModel ( model );
50
49
51
50
mTempStyle = new QgsStyleV2 ();
51
+ // TODO validate
52
+ mFileName = " " ;
53
+ mProgressDlg = NULL ;
54
+ mTempFile = NULL ;
55
+ mNetManager = new QNetworkAccessManager ( this );
56
+ mNetReply = NULL ;
52
57
53
58
if ( mDialogMode == Import )
54
59
{
60
+ // populate the import types
61
+ importTypeCombo->addItem ( " file specified below" , QVariant ( " file" ) );
62
+ importTypeCombo->addItem ( " official QGIS repo online" , QVariant ( " official" ) );
63
+ importTypeCombo->addItem ( " URL specified below" , QVariant ( " url" ) );
64
+ connect ( importTypeCombo, SIGNAL ( currentIndexChanged ( int ) ), this , SLOT ( importTypeChanged ( int ) ) );
65
+
66
+ btnBrowse->setText ( " Browse" );
67
+ connect ( btnBrowse, SIGNAL ( clicked () ), this , SLOT ( browse () ) );
68
+
55
69
label->setText ( tr ( " Select symbols to import" ) );
56
70
buttonBox->button ( QDialogButtonBox::Ok )->setText ( tr ( " Import" ) );
57
- if ( !populateStyles ( mTempStyle ) )
58
- {
59
- QApplication::postEvent ( this , new QCloseEvent () );
60
- }
71
+
61
72
}
62
73
else
63
74
{
75
+ // hide import specific controls when exporting
76
+ btnBrowse->setHidden ( true );
77
+ fromLabel->setHidden ( true );
78
+ importTypeCombo->setHidden ( true );
79
+ locationLabel->setHidden ( true );
80
+ locationLineEdit->setHidden ( true );
81
+
64
82
buttonBox->button ( QDialogButtonBox::Ok )->setText ( tr ( " Export" ) );
65
83
if ( !populateStyles ( mQgisStyle ) )
66
84
{
@@ -112,8 +130,6 @@ void QgsStyleV2ExportImportDialog::doExportImport()
112
130
else // import
113
131
{
114
132
moveStyles ( &selection, mTempStyle , mQgisStyle );
115
- // TODO save in the move function itself using saveSymbol and saveColorRamp
116
- mQgisStyle ->save ();
117
133
118
134
// clear model
119
135
QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model () );
@@ -305,3 +321,128 @@ void QgsStyleV2ExportImportDialog::clearSelection()
305
321
{
306
322
listItems->clearSelection ();
307
323
}
324
+
325
+ void QgsStyleV2ExportImportDialog::importTypeChanged ( int index )
326
+ {
327
+ QString type = importTypeCombo->itemData ( index ).toString ();
328
+
329
+ locationLineEdit->setText ( " " );
330
+
331
+ if ( type == " file" )
332
+ {
333
+ locationLineEdit->setEnabled ( true );
334
+ btnBrowse->setText ( " Browse" );
335
+ }
336
+ else if ( type == " official" )
337
+ {
338
+ btnBrowse->setText ( " Fetch Symbols" );
339
+ locationLineEdit->setEnabled ( false );
340
+ }
341
+ else
342
+ {
343
+ btnBrowse->setText ( " Fetch Symbols" );
344
+ locationLineEdit->setEnabled ( true );
345
+ }
346
+ }
347
+
348
+ void QgsStyleV2ExportImportDialog::browse ()
349
+ {
350
+ QString type = importTypeCombo->itemData ( importTypeCombo->currentIndex () ).toString ();
351
+
352
+ if ( type == " file" )
353
+ {
354
+ mFileName = QFileDialog::getOpenFileName ( this , tr ( " Load styles" ), " ." ,
355
+ tr ( " XML files (*.xml *XML)" ) );
356
+ if ( mFileName .isEmpty () )
357
+ {
358
+ return ;
359
+ }
360
+ locationLineEdit->setText ( mFileName );
361
+ }
362
+ else if ( type == " official" )
363
+ {
364
+ // TODO set URL
365
+ downloadStyleXML ( QUrl ( " http://...." ) );
366
+ }
367
+ else
368
+ {
369
+ downloadStyleXML ( QUrl ( locationLineEdit->text () ) );
370
+ }
371
+ populateStyles ( mTempStyle );
372
+ }
373
+
374
+ void QgsStyleV2ExportImportDialog::downloadStyleXML ( QUrl url )
375
+ {
376
+ // TODO Try to move this code to some core Network interface,
377
+ // HTTP downloading is a generic functionality that might be used elsewhere
378
+
379
+ mTempFile = new QTemporaryFile ();
380
+ if ( mTempFile ->open () )
381
+ {
382
+ mFileName = mTempFile ->fileName ();
383
+
384
+ if ( mProgressDlg )
385
+ {
386
+ QProgressDialog *dummy = mProgressDlg ;
387
+ mProgressDlg = NULL ;
388
+ delete dummy;
389
+ }
390
+ mProgressDlg = new QProgressDialog ();
391
+ mProgressDlg ->setLabelText ( tr ( " Downloading style ... " ) );
392
+ mProgressDlg ->setAutoClose ( true );
393
+
394
+ connect ( mProgressDlg , SIGNAL ( canceled () ), this , SLOT ( downloadCanceled () ) );
395
+
396
+ // open the network connection and connect the respective slots
397
+ if ( mNetReply )
398
+ {
399
+ QNetworkReply *dummyReply = mNetReply ;
400
+ mNetReply = NULL ;
401
+ delete dummyReply;
402
+ }
403
+ mNetReply = mNetManager ->get ( QNetworkRequest ( url ) );
404
+
405
+ connect ( mNetReply , SIGNAL ( finished () ), this , SLOT ( httpFinished () ) );
406
+ connect ( mNetReply , SIGNAL ( readyRead () ), this , SLOT ( fileReadyRead () ) );
407
+ connect ( mNetReply , SIGNAL ( downloadProgress ( qint64, qint64 ) ), this , SLOT ( updateProgress ( qint64, qint64 ) ) );
408
+ }
409
+ }
410
+
411
+ void QgsStyleV2ExportImportDialog::httpFinished ()
412
+ {
413
+ if ( mNetReply ->error () )
414
+ {
415
+ mTempFile ->remove ();
416
+ mFileName = " " ;
417
+ mProgressDlg ->hide ();
418
+ QMessageBox::information ( this , tr ( " HTTP Error!" ),
419
+ tr ( " Download failed: %1." ).arg ( mNetReply ->errorString () ) );
420
+ return ;
421
+ }
422
+ else
423
+ {
424
+ mTempFile ->flush ();
425
+ mTempFile ->close ();
426
+ populateStyles ( mTempStyle );
427
+ delete mTempFile ;
428
+ mTempFile = NULL ;
429
+ }
430
+ }
431
+
432
+ void QgsStyleV2ExportImportDialog::fileReadyRead ()
433
+ {
434
+ mTempFile ->write ( mNetReply ->readAll () );
435
+ }
436
+
437
+ void QgsStyleV2ExportImportDialog::updateProgress ( qint64 bytesRead, qint64 bytesTotal )
438
+ {
439
+ mProgressDlg ->setMaximum ( bytesTotal );
440
+ mProgressDlg ->setValue ( bytesRead );
441
+ }
442
+
443
+ void QgsStyleV2ExportImportDialog::downloadCanceled ()
444
+ {
445
+ mNetReply ->abort ();
446
+ mTempFile ->remove ();
447
+ mFileName = " " ;
448
+ }
0 commit comments