24 #include <QInputDialog>
25 #include <QCloseEvent>
26 #include <QFileDialog>
27 #include <QMessageBox>
28 #include <QPushButton>
29 #include <QStandardItemModel>
40 pb =
new QPushButton(
tr(
"Select all" ) );
41 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
42 connect( pb, SIGNAL( clicked() ),
this, SLOT(
selectAll() ) );
44 pb =
new QPushButton(
tr(
"Clear selection" ) );
45 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
46 connect( pb, SIGNAL( clicked() ),
this, SLOT(
clearSelection() ) );
48 QStandardItemModel* model =
new QStandardItemModel( listItems );
49 listItems->setModel( model );
56 mNetManager =
new QNetworkAccessManager(
this );
59 if ( mDialogMode ==
Import )
61 setWindowTitle(
tr(
"Import style(s)" ) );
63 importTypeCombo->addItem(
tr(
"file specified below" ), QVariant(
"file" ) );
65 importTypeCombo->addItem(
tr(
"URL specified below" ), QVariant(
"url" ) );
66 connect( importTypeCombo, SIGNAL( currentIndexChanged(
int ) ),
this, SLOT(
importTypeChanged(
int ) ) );
69 groupCombo->addItem(
"imported", QVariant(
"new" ) );
70 foreach ( QString gName, groups )
72 groupCombo->addItem( gName );
75 btnBrowse->setText(
"Browse" );
76 connect( btnBrowse, SIGNAL( clicked() ),
this, SLOT(
browse() ) );
78 label->setText(
tr(
"Select symbols to import" ) );
79 buttonBox->button( QDialogButtonBox::Ok )->setText(
tr(
"Import" ) );
83 setWindowTitle(
tr(
"Export style(s)" ) );
85 btnBrowse->setHidden(
true );
86 fromLabel->setHidden(
true );
87 importTypeCombo->setHidden(
true );
88 locationLabel->setHidden(
true );
89 locationLineEdit->setHidden(
true );
90 groupLabel->setHidden(
true );
91 groupCombo->setHidden(
true );
93 buttonBox->button( QDialogButtonBox::Ok )->setText(
tr(
"Export" ) );
94 if ( !populateStyles( mQgisStyle ) )
96 QApplication::postEvent(
this,
new QCloseEvent() );
101 disconnect( buttonBox, SIGNAL( accepted() ),
this, SLOT( accept() ) );
102 connect( buttonBox, SIGNAL( accepted() ),
this, SLOT(
doExportImport() ) );
107 QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
108 if ( selection.isEmpty() )
110 QMessageBox::warning(
this,
tr(
"Export/import error" ),
111 tr(
"You should select at least one symbol/color ramp." ) );
115 if ( mDialogMode ==
Export )
117 QString fileName = QFileDialog::getSaveFileName(
this,
tr(
"Save styles" ),
".",
118 tr(
"XML files (*.xml *.XML)" ) );
119 if ( fileName.isEmpty() )
125 if ( !fileName.toLower().endsWith(
".xml" ) )
130 mFileName = fileName;
132 moveStyles( &selection, mQgisStyle, mTempStyle );
133 if ( !mTempStyle->
exportXML( mFileName ) )
135 QMessageBox::warning(
this,
tr(
"Export/import error" ),
136 tr(
"Error when saving selected symbols to file:\n%1" )
143 moveStyles( &selection, mTempStyle, mQgisStyle );
146 QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
155 bool QgsStyleV2ExportImportDialog::populateStyles(
QgsStyleV2* style )
158 if ( mDialogMode ==
Import )
163 QMessageBox::warning(
this,
tr(
"Import error" ),
164 tr(
"An error occured during import:\n%1" ).arg( style->
errorString() ) );
169 QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
176 for (
int i = 0; i < styleNames.count(); ++i )
178 name = styleNames[i];
180 QStandardItem* item =
new QStandardItem( name );
182 item->setIcon( icon );
183 model->appendRow( item );
190 for (
int i = 0; i < styleNames.count(); ++i )
192 name = styleNames[i];
195 QStandardItem* item =
new QStandardItem( name );
197 item->setIcon( icon );
198 model->appendRow( item );
204 void QgsStyleV2ExportImportDialog::moveStyles( QModelIndexList* selection,
QgsStyleV2* src,
QgsStyleV2* dst )
210 bool isSymbol =
true;
212 bool overwrite =
true;
216 if ( mDialogMode ==
Import )
218 int index = groupCombo->currentIndex();
219 QString name = groupCombo->itemText( index );
220 if ( name.isEmpty() )
223 bool nameInvalid =
true;
224 while ( nameInvalid )
227 name = QInputDialog::getText(
this,
tr(
"Group Name" ),
228 tr(
"Please enter a name for new group:" ),
234 QMessageBox::warning(
this,
tr(
"New Group" ),
235 tr(
"New group cannot be created without a name. Kindly enter a name." ) );
239 if ( name.isEmpty() )
241 QMessageBox::warning(
this,
tr(
"New group" ),
242 tr(
"Cannot create a group without name. Enter a name." ) );
252 else if ( dst->
groupNames().contains( name ) )
254 groupid = dst->
groupId( name );
262 for (
int i = 0; i < selection->size(); ++i )
264 index = selection->at( i );
265 symbolName = index.model()->data( index, 0 ).toString();
266 symbol = src->
symbol( symbolName );
267 if ( symbol == NULL )
275 if ( dst->
symbolNames().contains( symbolName ) && prompt )
277 int res = QMessageBox::warning(
this,
tr(
"Duplicate names" ),
278 tr(
"Symbol with name '%1' already exists.\nOverwrite?" )
280 QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
283 case QMessageBox::Cancel:
285 case QMessageBox::No:
287 case QMessageBox::Yes:
289 if ( mDialogMode ==
Import )
290 dst->
saveSymbol( symbolName, symbol, groupid, QStringList() );
292 case QMessageBox::YesToAll:
296 case QMessageBox::NoToAll:
303 if ( dst->
symbolNames().contains( symbolName ) && overwrite )
306 if ( mDialogMode ==
Import )
307 dst->
saveSymbol( symbolName, symbol, groupid, QStringList() );
309 else if ( dst->
symbolNames().contains( symbolName ) && !overwrite )
316 if ( mDialogMode ==
Import )
317 dst->
saveSymbol( symbolName, symbol, groupid, QStringList() );
324 int res = QMessageBox::warning(
this,
tr(
"Duplicate names" ),
325 tr(
"Color ramp with name '%1' already exists.\nOverwrite?" )
327 QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
330 case QMessageBox::Cancel:
332 case QMessageBox::No:
334 case QMessageBox::Yes:
336 if ( mDialogMode ==
Import )
337 dst->
saveColorRamp( symbolName, ramp, groupid, QStringList() );
339 case QMessageBox::YesToAll:
343 case QMessageBox::NoToAll: prompt =
false;
352 if ( mDialogMode ==
Import )
353 dst->
saveColorRamp( symbolName, ramp, groupid, QStringList() );
355 else if ( dst->
colorRampNames().contains( symbolName ) && !overwrite )
362 if ( mDialogMode ==
Import )
363 dst->
saveColorRamp( symbolName, ramp, groupid, QStringList() );
377 listItems->selectAll();
382 listItems->clearSelection();
387 QString type = importTypeCombo->itemData( index ).toString();
389 locationLineEdit->setText(
"" );
391 if ( type ==
"file" )
393 locationLineEdit->setEnabled(
true );
394 btnBrowse->setText(
"Browse" );
396 else if ( type ==
"official" )
398 btnBrowse->setText(
"Fetch Symbols" );
399 locationLineEdit->setEnabled(
false );
403 btnBrowse->setText(
"Fetch Symbols" );
404 locationLineEdit->setEnabled(
true );
410 QString type = importTypeCombo->itemData( importTypeCombo->currentIndex() ).toString();
412 if ( type ==
"file" )
414 mFileName = QFileDialog::getOpenFileName(
this,
tr(
"Load styles" ),
".",
415 tr(
"XML files (*.xml *XML)" ) );
416 if ( mFileName.isEmpty() )
420 QFileInfo pathInfo( mFileName );
421 QString groupName = pathInfo.fileName().replace(
".xml",
"" );
422 groupCombo->setItemText( 0, groupName );
423 locationLineEdit->setText( mFileName );
424 populateStyles( mTempStyle );
426 else if ( type ==
"official" )
433 downloadStyleXML( QUrl( locationLineEdit->text() ) );
437 void QgsStyleV2ExportImportDialog::downloadStyleXML( QUrl url )
442 mTempFile =
new QTemporaryFile();
443 if ( mTempFile->open() )
445 mFileName = mTempFile->fileName();
449 QProgressDialog *dummy = mProgressDlg;
453 mProgressDlg =
new QProgressDialog();
454 mProgressDlg->setLabelText(
tr(
"Downloading style ... " ) );
455 mProgressDlg->setAutoClose(
true );
457 connect( mProgressDlg, SIGNAL( canceled() ),
this, SLOT( downloadCanceled() ) );
462 QNetworkReply *dummyReply = mNetReply;
466 mNetReply = mNetManager->get( QNetworkRequest( url ) );
468 connect( mNetReply, SIGNAL( finished() ),
this, SLOT( httpFinished() ) );
469 connect( mNetReply, SIGNAL( readyRead() ),
this, SLOT( fileReadyRead() ) );
470 connect( mNetReply, SIGNAL( downloadProgress( qint64, qint64 ) ),
this, SLOT( updateProgress( qint64, qint64 ) ) );
474 void QgsStyleV2ExportImportDialog::httpFinished()
476 if ( mNetReply->error() )
480 mProgressDlg->hide();
481 QMessageBox::information(
this,
tr(
"HTTP Error!" ),
482 tr(
"Download failed: %1." ).arg( mNetReply->errorString() ) );
489 populateStyles( mTempStyle );
493 void QgsStyleV2ExportImportDialog::fileReadyRead()
495 mTempFile->write( mNetReply->readAll() );
498 void QgsStyleV2ExportImportDialog::updateProgress( qint64 bytesRead, qint64 bytesTotal )
500 mProgressDlg->setMaximum( bytesTotal );
501 mProgressDlg->setValue( bytesRead );
504 void QgsStyleV2ExportImportDialog::downloadCanceled()