/
qgsstylev2exportimportdialog.cpp
477 lines (425 loc) · 14 KB
/
qgsstylev2exportimportdialog.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
/***************************************************************************
qgsstylev2exportimportdialog.cpp
---------------------
begin : Jan 2011
copyright : (C) 2011 by Alexander Bruy
email : alexander dot bruy at gmail dot com
***************************************************************************
* *
* 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 "qgsstylev2exportimportdialog.h"
#include "qgsstylev2.h"
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"
#include "qgsvectorcolorrampv2.h"
#include "qgslogger.h"
#include <QDate>
#include <QCloseEvent>
#include <QFileDialog>
#include <QMessageBox>
#include <QPushButton>
#include <QStandardItemModel>
QgsStyleV2ExportImportDialog::QgsStyleV2ExportImportDialog( QgsStyleV2* style, QWidget *parent, Mode mode )
: QDialog( parent )
, mDialogMode( mode )
, mQgisStyle( style )
{
setupUi( this );
// additional buttons
QPushButton *pb;
pb = new QPushButton( tr( "Select all" ) );
buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
connect( pb, SIGNAL( clicked() ), this, SLOT( selectAll() ) );
pb = new QPushButton( tr( "Clear selection" ) );
buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
connect( pb, SIGNAL( clicked() ), this, SLOT( clearSelection() ) );
QStandardItemModel* model = new QStandardItemModel( listItems );
listItems->setModel( model );
mTempStyle = new QgsStyleV2();
// TODO validate
mFileName = "";
mProgressDlg = NULL;
mTempFile = NULL;
mNetManager = new QNetworkAccessManager( this );
mNetReply = NULL;
if ( mDialogMode == Import )
{
// populate the import types
importTypeCombo->addItem( "file specified below", QVariant( "file" ) );
// importTypeCombo->addItem( "official QGIS repo online", QVariant( "official" ) );
importTypeCombo->addItem( "URL specified below", QVariant( "url" ) );
connect( importTypeCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( importTypeChanged( int ) ) );
QStringList groups = mQgisStyle->groupNames();
groupCombo->addItem( "New Default Group", QVariant( "default" ) );
foreach ( QString gName, groups )
{
groupCombo->addItem( gName );
}
btnBrowse->setText( "Browse" );
connect( btnBrowse, SIGNAL( clicked() ), this, SLOT( browse() ) );
label->setText( tr( "Select symbols to import" ) );
buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Import" ) );
}
else
{
// hide import specific controls when exporting
btnBrowse->setHidden( true );
fromLabel->setHidden( true );
importTypeCombo->setHidden( true );
locationLabel->setHidden( true );
locationLineEdit->setHidden( true );
groupLabel->setHidden( true );
groupCombo->setHidden( true );
buttonBox->button( QDialogButtonBox::Ok )->setText( tr( "Export" ) );
if ( !populateStyles( mQgisStyle ) )
{
QApplication::postEvent( this, new QCloseEvent() );
}
}
// use Ok button for starting import and export operations
disconnect( buttonBox, SIGNAL( accepted() ), this, SLOT( accept() ) );
connect( buttonBox, SIGNAL( accepted() ), this, SLOT( doExportImport() ) );
}
void QgsStyleV2ExportImportDialog::doExportImport()
{
QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
if ( selection.isEmpty() )
{
QMessageBox::warning( this, tr( "Export/import error" ),
tr( "You should select at least one symbol/color ramp." ) );
return;
}
if ( mDialogMode == Export )
{
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save styles" ), ".",
tr( "XML files (*.xml *.XML)" ) );
if ( fileName.isEmpty() )
{
return;
}
// ensure the user never ommited the extension from the file name
if ( !fileName.toLower().endsWith( ".xml" ) )
{
fileName += ".xml";
}
mFileName = fileName;
moveStyles( &selection, mQgisStyle, mTempStyle );
if ( !mTempStyle->exportXML( mFileName ) )
{
QMessageBox::warning( this, tr( "Export/import error" ),
tr( "Error when saving selected symbols to file:\n%1" )
.arg( mTempStyle->errorString() ) );
return;
}
}
else // import
{
moveStyles( &selection, mTempStyle, mQgisStyle );
// clear model
QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
model->clear();
accept();
}
mFileName = "";
mTempStyle->clear();
}
bool QgsStyleV2ExportImportDialog::populateStyles( QgsStyleV2* style )
{
// load symbols and color ramps from file
if ( mDialogMode == Import )
{
// NOTE mTempStyle is style here
if ( !style->importXML( mFileName ) )
{
QMessageBox::warning( this, tr( "Import error" ),
tr( "An error occured during import:\n%1" ).arg( style->errorString() ) );
return false;
}
}
QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
model->clear();
// populate symbols
QStringList styleNames = style->symbolNames();
QString name;
for ( int i = 0; i < styleNames.count(); ++i )
{
name = styleNames[i];
QgsSymbolV2* symbol = style->symbol( name );
QStandardItem* item = new QStandardItem( name );
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, listItems->iconSize() );
item->setIcon( icon );
model->appendRow( item );
delete symbol;
}
// and color ramps
styleNames = style->colorRampNames();
for ( int i = 0; i < styleNames.count(); ++i )
{
name = styleNames[i];
QgsVectorColorRampV2* ramp = style->colorRamp( name );
QStandardItem* item = new QStandardItem( name );
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, listItems->iconSize() );
item->setIcon( icon );
model->appendRow( item );
delete ramp;
}
return true;
}
void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection, QgsStyleV2* src, QgsStyleV2* dst )
{
QString symbolName;
QgsSymbolV2* symbol;
QgsVectorColorRampV2 *ramp = 0;
QModelIndex index;
bool isSymbol = true;
bool prompt = true;
bool overwrite = true;
int groupid = 0;
// get the groupid when going for import
if ( mDialogMode == Import )
{
QSettings settings;
int count = settings.value( "/qgis/style/importcount", 0 ).toInt();
int index = groupCombo->currentIndex();
if ( groupCombo->itemData( index ).toString() == "default" )
{
QString groupName = "import-" + QString::number( count );
groupid = dst->addGroup( groupName );
count++;
settings.setValue( "/qgis/style/importcount", count );
}
else
{
groupid = dst->groupId( groupCombo->itemText( index ) );
}
}
for ( int i = 0; i < selection->size(); ++i )
{
index = selection->at( i );
symbolName = index.model()->data( index, 0 ).toString();
symbol = src->symbol( symbolName );
if ( symbol == NULL )
{
isSymbol = false;
ramp = src->colorRamp( symbolName );
}
if ( isSymbol )
{
if ( dst->symbolNames().contains( symbolName ) && prompt )
{
int res = QMessageBox::warning( this, tr( "Duplicate names" ),
tr( "Symbol with name '%1' already exists.\nOverwrite?" )
.arg( symbolName ),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
switch ( res )
{
case QMessageBox::Cancel:
return;
case QMessageBox::No:
continue;
case QMessageBox::Yes:
dst->addSymbol( symbolName, symbol );
if ( mDialogMode == Import )
dst->saveSymbol( symbolName, symbol, groupid, QStringList() );
continue;
case QMessageBox::YesToAll:
prompt = false;
overwrite = true;
break;
case QMessageBox::NoToAll:
prompt = false;
overwrite = false;
break;
}
}
if ( dst->symbolNames().contains( symbolName ) && overwrite )
{
dst->addSymbol( symbolName, symbol );
if ( mDialogMode == Import )
dst->saveSymbol( symbolName, symbol, groupid, QStringList() );
}
else if ( dst->symbolNames().contains( symbolName ) && !overwrite )
{
continue;
}
else
{
dst->addSymbol( symbolName, symbol );
if ( mDialogMode == Import )
dst->saveSymbol( symbolName, symbol, groupid, QStringList() );
}
}
else
{
if ( dst->colorRampNames().contains( symbolName ) && prompt )
{
int res = QMessageBox::warning( this, tr( "Duplicate names" ),
tr( "Color ramp with name '%1' already exists.\nOverwrite?" )
.arg( symbolName ),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
switch ( res )
{
case QMessageBox::Cancel:
return;
case QMessageBox::No:
continue;
case QMessageBox::Yes:
dst->addColorRamp( symbolName, ramp );
if ( mDialogMode == Import )
dst->saveColorRamp( symbolName, ramp, groupid, QStringList() );
continue;
case QMessageBox::YesToAll:
prompt = false;
overwrite = true;
break;
case QMessageBox::NoToAll: prompt = false;
overwrite = false;
break;
}
}
if ( dst->colorRampNames().contains( symbolName ) && overwrite )
{
dst->addColorRamp( symbolName, ramp );
if ( mDialogMode == Import )
dst->saveColorRamp( symbolName, ramp, groupid, QStringList() );
}
else if ( dst->colorRampNames().contains( symbolName ) && !overwrite )
{
continue;
}
else
{
dst->addColorRamp( symbolName, ramp );
if ( mDialogMode == Import )
dst->saveColorRamp( symbolName, ramp, groupid, QStringList() );
}
}
}
}
QgsStyleV2ExportImportDialog::~QgsStyleV2ExportImportDialog()
{
delete mTempStyle;
}
void QgsStyleV2ExportImportDialog::selectAll()
{
listItems->selectAll();
}
void QgsStyleV2ExportImportDialog::clearSelection()
{
listItems->clearSelection();
}
void QgsStyleV2ExportImportDialog::importTypeChanged( int index )
{
QString type = importTypeCombo->itemData( index ).toString();
locationLineEdit->setText( "" );
if ( type == "file" )
{
locationLineEdit->setEnabled( true );
btnBrowse->setText( "Browse" );
}
else if ( type == "official" )
{
btnBrowse->setText( "Fetch Symbols" );
locationLineEdit->setEnabled( false );
}
else
{
btnBrowse->setText( "Fetch Symbols" );
locationLineEdit->setEnabled( true );
}
}
void QgsStyleV2ExportImportDialog::browse()
{
QString type = importTypeCombo->itemData( importTypeCombo->currentIndex() ).toString();
if ( type == "file" )
{
mFileName = QFileDialog::getOpenFileName( this, tr( "Load styles" ), ".",
tr( "XML files (*.xml *XML)" ) );
if ( mFileName.isEmpty() )
{
return;
}
locationLineEdit->setText( mFileName );
populateStyles( mTempStyle );
}
else if ( type == "official" )
{
// TODO set URL
// downloadStyleXML( QUrl( "http://...." ) );
}
else
{
downloadStyleXML( QUrl( locationLineEdit->text() ) );
}
}
void QgsStyleV2ExportImportDialog::downloadStyleXML( QUrl url )
{
// XXX Try to move this code to some core Network interface,
// HTTP downloading is a generic functionality that might be used elsewhere
mTempFile = new QTemporaryFile();
if ( mTempFile->open() )
{
mFileName = mTempFile->fileName();
if ( mProgressDlg )
{
QProgressDialog *dummy = mProgressDlg;
mProgressDlg = NULL;
delete dummy;
}
mProgressDlg = new QProgressDialog();
mProgressDlg->setLabelText( tr( "Downloading style ... " ) );
mProgressDlg->setAutoClose( true );
connect( mProgressDlg, SIGNAL( canceled() ), this, SLOT( downloadCanceled() ) );
// open the network connection and connect the respective slots
if ( mNetReply )
{
QNetworkReply *dummyReply = mNetReply;
mNetReply = NULL;
delete dummyReply;
}
mNetReply = mNetManager->get( QNetworkRequest( url ) );
connect( mNetReply, SIGNAL( finished() ), this, SLOT( httpFinished() ) );
connect( mNetReply, SIGNAL( readyRead() ), this, SLOT( fileReadyRead() ) );
connect( mNetReply, SIGNAL( downloadProgress( qint64, qint64 ) ), this, SLOT( updateProgress( qint64, qint64 ) ) );
}
}
void QgsStyleV2ExportImportDialog::httpFinished()
{
if ( mNetReply->error() )
{
mTempFile->remove();
mFileName = "";
mProgressDlg->hide();
QMessageBox::information( this, tr( "HTTP Error!" ),
tr( "Download failed: %1." ).arg( mNetReply->errorString() ) );
return;
}
else
{
mTempFile->flush();
mTempFile->close();
populateStyles( mTempStyle );
delete mTempFile;
mTempFile = NULL;
}
}
void QgsStyleV2ExportImportDialog::fileReadyRead()
{
mTempFile->write( mNetReply->readAll() );
}
void QgsStyleV2ExportImportDialog::updateProgress( qint64 bytesRead, qint64 bytesTotal )
{
mProgressDlg->setMaximum( bytesTotal );
mProgressDlg->setValue( bytesRead );
}
void QgsStyleV2ExportImportDialog::downloadCanceled()
{
mNetReply->abort();
mTempFile->remove();
mFileName = "";
}