18 #include "ui_qgsstyleexportimportdialogbase.h"
34 #include <QInputDialog>
35 #include <QCloseEvent>
36 #include <QFileDialog>
37 #include <QMessageBox>
38 #include <QNetworkReply>
39 #include <QProgressDialog>
40 #include <QPushButton>
41 #include <QStandardItemModel>
53 QPushButton *pb =
nullptr;
54 pb =
new QPushButton( tr(
"Select All" ) );
55 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
58 pb =
new QPushButton( tr(
"Clear Selection" ) );
59 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
62 mTempStyle = qgis::make_unique< QgsStyle >();
63 mTempStyle->createMemoryDatabase();
66 mGroupSelectionDlg =
nullptr;
70 if ( mDialogMode ==
Import )
72 setWindowTitle( tr(
"Import Item(s)" ) );
74 importTypeCombo->addItem( tr(
"File" ), ImportSource::File );
76 importTypeCombo->addItem( tr(
"URL" ), ImportSource::Url );
80 mSymbolTags->setText( QStringLiteral(
"imported" ) );
82 connect( mButtonFetch, &QAbstractButton::clicked,
this, &QgsStyleExportImportDialog::fetch );
85 mImportFileWidget->setDialogTitle( tr(
"Load Styles" ) );
86 mImportFileWidget->setFilter( tr(
"XML files (*.xml *.XML)" ) );
89 mImportFileWidget->setDefaultRoot( settings.
value( QStringLiteral(
"StyleManager/lastImportDir" ), QDir::homePath(),
QgsSettings::Gui ).toString() );
92 label->setText( tr(
"Select items to import" ) );
93 buttonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Import" ) );
95 dialogStyle = mTempStyle.get();
99 setWindowTitle( tr(
"Export Item(s)" ) );
101 mLocationStackedEdit->setHidden(
true );
102 fromLabel->setHidden(
true );
103 importTypeCombo->setHidden(
true );
104 mLocationLabel->setHidden(
true );
106 mFavorite->setHidden(
true );
107 mIgnoreXMLTags->setHidden(
true );
109 pb =
new QPushButton( tr(
"Select by Group…" ) );
110 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
112 tagLabel->setHidden(
true );
113 mSymbolTags->setHidden(
true );
114 tagHintLabel->setHidden(
true );
116 buttonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Export" ) );
118 dialogStyle = mStyle;
121 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
126 listItems->setIconSize( QSize(
static_cast< int >(
iconSize ),
static_cast< int >(
iconSize * 0.9 ) ) );
132 listItems->setModel( mModel );
134 connect( listItems->selectionModel(), &QItemSelectionModel::selectionChanged,
135 this, &QgsStyleExportImportDialog::selectionChanged );
138 disconnect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
140 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
142 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsStyleExportImportDialog::showHelp );
147 QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
148 if ( selection.isEmpty() )
150 QMessageBox::warning(
this, tr(
"Export/import Item(s)" ),
151 tr(
"You should select at least one symbol/color ramp." ) );
155 if ( mDialogMode ==
Export )
157 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save Styles" ), QDir::homePath(),
158 tr(
"XML files (*.xml *.XML)" ) );
159 if ( fileName.isEmpty() )
165 if ( !fileName.endsWith( QLatin1String(
".xml" ), Qt::CaseInsensitive ) )
167 fileName += QLatin1String(
".xml" );
170 mFileName = fileName;
172 mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
173 moveStyles( &selection, mStyle, mTempStyle.get() );
174 if ( !mTempStyle->exportXml( mFileName ) )
176 mCursorOverride.reset();
177 QMessageBox::warning(
this, tr(
"Export Symbols" ),
178 tr(
"Error when saving selected symbols to file:\n%1" )
179 .arg( mTempStyle->errorString() ) );
184 mCursorOverride.reset();
185 QMessageBox::information(
this, tr(
"Export Symbols" ),
186 tr(
"The selected symbols were successfully exported to file:\n%1" )
192 mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
193 moveStyles( &selection, mTempStyle.get(), mStyle );
196 mCursorOverride.reset();
203 bool QgsStyleExportImportDialog::populateStyles()
210 if ( !mTempStyle->importXml( mFileName ) )
213 QMessageBox::warning(
this, tr(
"Import Symbols or Color Ramps" ),
214 tr(
"An error occurred during import:\n%1" ).arg( mTempStyle->errorString() ) );
220 void QgsStyleExportImportDialog::moveStyles( QModelIndexList *selection,
QgsStyle *src,
QgsStyle *dst )
222 QList< QgsStyleManagerDialog::ItemDetails > items;
223 items.reserve( selection->size() );
224 for (
int i = 0; i < selection->size(); ++i )
226 QModelIndex index = selection->at( i );
228 QgsStyleManagerDialog::ItemDetails details;
232 details.name = mModel->data( mModel->index( index.row(),
QgsStyleModel::Name, index.parent() ), Qt::DisplayRole ).toString();
236 QgsStyleManagerDialog::copyItems( items, src, dst,
this, mCursorOverride, mDialogMode ==
Import,
237 mSymbolTags->text().split(
',' ), mFavorite->isChecked(), mIgnoreXMLTags->isChecked() );
243 delete mGroupSelectionDlg;
248 mImportFileWidget->setFilePath( path );
253 listItems->selectAll();
258 listItems->clearSelection();
275 const auto constSymbolNames = symbolNames;
276 for (
const QString &symbolName : constSymbolNames )
278 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0,
QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
279 const auto constIndexes = indexes;
280 for (
const QModelIndex &index : constIndexes )
282 listItems->selectionModel()->select( index, QItemSelectionModel::Select );
289 const auto constSymbolNames = symbolNames;
290 for (
const QString &symbolName : constSymbolNames )
292 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0,
QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
293 const auto constIndexes = indexes;
294 for (
const QModelIndex &index : constIndexes )
296 QItemSelection deselection( index, index );
297 listItems->selectionModel()->select( deselection, QItemSelectionModel::Deselect );
336 if ( ! mGroupSelectionDlg )
339 mGroupSelectionDlg->setWindowTitle( tr(
"Select Item(s) by Group" ) );
349 mGroupSelectionDlg->show();
350 mGroupSelectionDlg->raise();
351 mGroupSelectionDlg->activateWindow();
356 ImportSource source =
static_cast< ImportSource
>( importTypeCombo->itemData( index ).toInt() );
360 case ImportSource::File:
362 mLocationStackedEdit->setCurrentIndex( 0 );
363 mLocationLabel->setText( tr(
"File" ) );
367 case ImportSource::Official:
369 btnBrowse->setText( QStringLiteral(
"Fetch Items" ) );
370 locationLineEdit->setEnabled(
false );
374 case ImportSource::Url:
376 mLocationStackedEdit->setCurrentIndex( 1 );
377 mLocationLabel->setText( tr(
"URL" ) );
383 void QgsStyleExportImportDialog::fetch()
385 downloadStyleXml( QUrl( mUrlLineEdit->text() ) );
388 void QgsStyleExportImportDialog::importFileChanged(
const QString &path )
390 if ( path.isEmpty() )
394 QFileInfo pathInfo( mFileName );
395 QString tag = pathInfo.fileName().remove( QStringLiteral(
".xml" ) );
396 mSymbolTags->setText( tag );
397 if ( QFileInfo::exists( mFileName ) )
401 mImportFileWidget->setDefaultRoot( pathInfo.absolutePath() );
407 void QgsStyleExportImportDialog::downloadStyleXml(
const QUrl &url )
409 mTempFile =
new QTemporaryFile();
410 if ( mTempFile->open() )
412 mFileName = mTempFile->fileName();
414 QProgressDialog *progressDlg =
new QProgressDialog(
this );
415 progressDlg->setLabelText( tr(
"Downloading style…" ) );
416 progressDlg->setAutoClose(
true );
425 QNetworkReply *reply = fetcher->
reply();
426 if ( !reply || reply->error() != QNetworkReply::NoError )
431 QMessageBox::information( this, tr(
"Import from URL" ),
432 tr(
"HTTP Error! Download failed: %1." ).arg( reply->errorString() ) );
436 mTempFile->write( reply->readAll() );
441 progressDlg->deleteLater();
448 void QgsStyleExportImportDialog::selectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
451 Q_UNUSED( deselected )
452 bool nothingSelected = listItems->selectionModel()->selectedIndexes().empty();
453 buttonBox->button( QDialogButtonBox::Ok )->setDisabled( nothingSelected );
456 void QgsStyleExportImportDialog::showHelp()
458 QgsHelp::openHelp( QStringLiteral(
"style_library/style_manager.html#sharing-style-items" ) );