18 #include "ui_qgsstyleexportimportdialogbase.h" 32 #include <QInputDialog> 33 #include <QCloseEvent> 34 #include <QFileDialog> 35 #include <QMessageBox> 36 #include <QProgressDialog> 37 #include <QPushButton> 38 #include <QStandardItemModel> 39 #include <QNetworkAccessManager> 40 #include <QNetworkReply> 52 QPushButton *pb =
nullptr;
53 pb =
new QPushButton( tr(
"Select All" ) );
54 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
57 pb =
new QPushButton( tr(
"Clear Selection" ) );
58 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
61 mTempStyle = qgis::make_unique< QgsStyle >();
62 mTempStyle->createMemoryDatabase();
65 mProgressDlg =
nullptr;
66 mGroupSelectionDlg =
nullptr;
68 mNetManager =
new QNetworkAccessManager(
this );
71 if ( mDialogMode ==
Import )
73 setWindowTitle( tr(
"Import Item(s)" ) );
75 importTypeCombo->addItem( tr(
"File" ), ImportSource::File );
77 importTypeCombo->addItem( tr(
"URL" ), ImportSource::Url );
81 mSymbolTags->setText( QStringLiteral(
"imported" ) );
83 connect( mButtonFetch, &QAbstractButton::clicked,
this, &QgsStyleExportImportDialog::fetch );
86 mImportFileWidget->setDialogTitle( tr(
"Load Styles" ) );
87 mImportFileWidget->setFilter( tr(
"XML files (*.xml *.XML)" ) );
90 mImportFileWidget->setDefaultRoot( settings.
value( QStringLiteral(
"StyleManager/lastImportDir" ), QDir::homePath(),
QgsSettings::Gui ).toString() );
93 label->setText( tr(
"Select items to import" ) );
94 buttonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Import" ) );
97 listItems->setModel( mModel );
101 setWindowTitle( tr(
"Export Item(s)" ) );
103 mLocationStackedEdit->setHidden(
true );
104 fromLabel->setHidden(
true );
105 importTypeCombo->setHidden(
true );
106 mLocationLabel->setHidden(
true );
108 mFavorite->setHidden(
true );
109 mIgnoreXMLTags->setHidden(
true );
111 pb =
new QPushButton( tr(
"Select by Group…" ) );
112 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
114 tagLabel->setHidden(
true );
115 mSymbolTags->setHidden(
true );
116 tagHintLabel->setHidden(
true );
118 buttonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Export" ) );
124 listItems->setIconSize( QSize( static_cast< int >( iconSize ), static_cast< int >( iconSize * 0.9 ) ) );
127 listItems->setModel( mModel );
129 connect( listItems->selectionModel(), &QItemSelectionModel::selectionChanged,
130 this, &QgsStyleExportImportDialog::selectionChanged );
133 disconnect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
135 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
137 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsStyleExportImportDialog::showHelp );
142 QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
143 if ( selection.isEmpty() )
145 QMessageBox::warning(
this, tr(
"Export/import Item(s)" ),
146 tr(
"You should select at least one symbol/color ramp." ) );
150 if ( mDialogMode ==
Export )
152 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save Styles" ), QDir::homePath(),
153 tr(
"XML files (*.xml *.XML)" ) );
154 if ( fileName.isEmpty() )
160 if ( !fileName.endsWith( QLatin1String(
".xml" ), Qt::CaseInsensitive ) )
162 fileName += QLatin1String(
".xml" );
165 mFileName = fileName;
167 mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
168 moveStyles( &selection, mStyle, mTempStyle.get() );
169 if ( !mTempStyle->exportXml( mFileName ) )
171 mCursorOverride.reset();
172 QMessageBox::warning(
this, tr(
"Export Symbols" ),
173 tr(
"Error when saving selected symbols to file:\n%1" )
174 .arg( mTempStyle->errorString() ) );
179 mCursorOverride.reset();
180 QMessageBox::information(
this, tr(
"Export Symbols" ),
181 tr(
"The selected symbols were successfully exported to file:\n%1" )
187 mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
188 moveStyles( &selection, mTempStyle.get(), mStyle );
191 mCursorOverride.reset();
198 bool QgsStyleExportImportDialog::populateStyles()
205 if ( !mTempStyle->importXml( mFileName ) )
208 QMessageBox::warning(
this, tr(
"Import Symbols or Color Ramps" ),
209 tr(
"An error occurred during import:\n%1" ).arg( mTempStyle->errorString() ) );
215 void QgsStyleExportImportDialog::moveStyles( QModelIndexList *selection,
QgsStyle *src,
QgsStyle *dst )
217 QList< QgsStyleManagerDialog::ItemDetails > items;
218 items.reserve( selection->size() );
219 for (
int i = 0; i < selection->size(); ++i )
221 QModelIndex index = selection->at( i );
223 QgsStyleManagerDialog::ItemDetails details;
231 QgsStyleManagerDialog::copyItems( items, src, dst,
this, mCursorOverride, mDialogMode ==
Import,
232 mSymbolTags->text().split(
',' ), mFavorite->isChecked(), mIgnoreXMLTags->isChecked() );
238 delete mGroupSelectionDlg;
243 mImportFileWidget->setFilePath( path );
248 listItems->selectAll();
253 listItems->clearSelection();
258 const auto constSymbolNames = symbolNames;
259 for (
const QString &symbolName : constSymbolNames )
261 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0,
QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
262 const auto constIndexes = indexes;
263 for (
const QModelIndex &index : constIndexes )
265 listItems->selectionModel()->select( index, QItemSelectionModel::Select );
272 const auto constSymbolNames = symbolNames;
273 for (
const QString &symbolName : constSymbolNames )
275 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0,
QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
276 const auto constIndexes = indexes;
277 for (
const QModelIndex &index : constIndexes )
279 QItemSelection deselection( index, index );
280 listItems->selectionModel()->select( deselection, QItemSelectionModel::Deselect );
315 if ( ! mGroupSelectionDlg )
318 mGroupSelectionDlg->setWindowTitle( tr(
"Select Item(s) by Group" ) );
326 mGroupSelectionDlg->show();
327 mGroupSelectionDlg->raise();
328 mGroupSelectionDlg->activateWindow();
333 ImportSource source =
static_cast< ImportSource
>( importTypeCombo->itemData( index ).toInt() );
337 case ImportSource::File:
339 mLocationStackedEdit->setCurrentIndex( 0 );
340 mLocationLabel->setText( tr(
"File" ) );
344 case ImportSource::Official:
346 btnBrowse->setText( QStringLiteral(
"Fetch Items" ) );
347 locationLineEdit->setEnabled(
false );
351 case ImportSource::Url:
353 mLocationStackedEdit->setCurrentIndex( 1 );
354 mLocationLabel->setText( tr(
"URL" ) );
360 void QgsStyleExportImportDialog::fetch()
362 downloadStyleXml( QUrl( mUrlLineEdit->text() ) );
365 void QgsStyleExportImportDialog::importFileChanged(
const QString &path )
367 if ( path.isEmpty() )
371 QFileInfo pathInfo( mFileName );
372 QString tag = pathInfo.fileName().remove( QStringLiteral(
".xml" ) );
373 mSymbolTags->setText( tag );
374 if ( QFileInfo::exists( mFileName ) )
378 mImportFileWidget->setDefaultRoot( pathInfo.absolutePath() );
384 void QgsStyleExportImportDialog::downloadStyleXml(
const QUrl &url )
389 mTempFile =
new QTemporaryFile();
390 if ( mTempFile->open() )
392 mFileName = mTempFile->fileName();
396 QProgressDialog *dummy = mProgressDlg;
397 mProgressDlg =
nullptr;
400 mProgressDlg =
new QProgressDialog();
401 mProgressDlg->setLabelText( tr(
"Downloading style…" ) );
402 mProgressDlg->setAutoClose(
true );
404 connect( mProgressDlg, &QProgressDialog::canceled,
this, &QgsStyleExportImportDialog::downloadCanceled );
409 QNetworkReply *dummyReply = mNetReply;
413 mNetReply = mNetManager->get( QNetworkRequest( url ) );
415 connect( mNetReply, &QNetworkReply::finished,
this, &QgsStyleExportImportDialog::httpFinished );
416 connect( mNetReply, &QIODevice::readyRead,
this, &QgsStyleExportImportDialog::fileReadyRead );
417 connect( mNetReply, &QNetworkReply::downloadProgress,
this, &QgsStyleExportImportDialog::updateProgress );
421 void QgsStyleExportImportDialog::httpFinished()
423 if ( mNetReply->error() )
427 mProgressDlg->hide();
428 QMessageBox::information(
this, tr(
"Import from URL" ),
429 tr(
"HTTP Error! Download failed: %1." ).arg( mNetReply->errorString() ) );
441 void QgsStyleExportImportDialog::fileReadyRead()
443 mTempFile->write( mNetReply->readAll() );
446 void QgsStyleExportImportDialog::updateProgress( qint64 bytesRead, qint64 bytesTotal )
448 mProgressDlg->setMaximum( bytesTotal );
449 mProgressDlg->setValue( bytesRead );
452 void QgsStyleExportImportDialog::downloadCanceled()
459 void QgsStyleExportImportDialog::selectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
462 Q_UNUSED( deselected )
463 bool nothingSelected = listItems->selectionModel()->selectedIndexes().empty();
464 buttonBox->button( QDialogButtonBox::Ok )->setDisabled( nothingSelected );
467 void QgsStyleExportImportDialog::showHelp()
469 QgsHelp::openHelp( QStringLiteral(
"working_with_vector/style_library.html#share-symbols" ) );
A QAbstractItemModel subclass for showing symbol and color ramp entities contained within a QgsStyle ...
void selectByGroup()
selectByGroup open select by group dialog
void deselectSmartgroup(const QString &groupName)
deselectSmartgroup deselects all symbols from a smart group
Temporarily sets a cursor override for the QApplication for the lifetime of the object.
QgsStyleExportImportDialog(QgsStyle *style, QWidget *parent=nullptr, Mode mode=Export)
Constructor for QgsStyleExportImportDialog, with the specified parent widget.
static const double UI_SCALE_FACTOR
UI scaling factor.
This class is a composition of two QSettings instances:
QStringList symbolsWithTag(StyleEntity type, int tagid) const
Returns the symbol names with which have the given tag.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
~QgsStyleExportImportDialog() override
void tagSelected(const QString &tagName)
tag with tagName has been selected
void clearSelection()
clearSelection deselects all symbols
void smartgroupSelected(const QString &groupName)
smartgroup with groupName has been selected
void selectSmartgroup(const QString &groupName)
selectSmartgroup selects all symbols from a smart group
void allDeselected()
all deselected
StyleEntity
Enum for Entities involved in a style.
SymbolType
Type of the symbol.
void deselectSymbols(const QStringList &symbolNames)
deselectSymbols deselect symbols by name
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Export existing symbols mode.
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
void allSelected()
all selected
QStringList symbolsOfSmartgroup(StyleEntity type, int id)
Returns the symbols for the smartgroup.
int smartgroupId(const QString &smartgroup)
Returns the DB id for the given smartgroup name.
int tagId(const QString &tag)
Returns the DB id for the given tag name.
void addDesiredIconSize(QSize size)
Adds an additional icon size to generate for Qt::DecorationRole data.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
void selectTag(const QString &tagName)
Select the symbols belonging to the given tag.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
void selectSymbols(const QStringList &symbolNames)
selectSymbols select symbols by name
void deselectTag(const QString &tagName)
Deselect the symbols belonging to the given tag.
void selectAll()
selectAll selects all symbols
QVariant data(const QModelIndex &index, int role) const override
void smartgroupDeselected(const QString &groupName)
smart group with groupName has been deselected
Style entity type, see QgsStyle::StyleEntity.
void setImportFilePath(const QString &path)
Sets the initial path to use for importing files, when the dialog is in a Import mode.
void tagDeselected(const QString &tagName)
tag with tagName has been deselected
Symbol type (for symbol entities)
void importTypeChanged(int)