18 #include "ui_qgsstyleexportimportdialogbase.h" 32 #include <QInputDialog> 33 #include <QCloseEvent> 34 #include <QFileDialog> 35 #include <QMessageBox> 36 #include <QPushButton> 37 #include <QStandardItemModel> 49 QPushButton *pb =
nullptr;
50 pb =
new QPushButton( tr(
"Select All" ) );
51 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
54 pb =
new QPushButton( tr(
"Clear Selection" ) );
55 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
58 mTempStyle = qgis::make_unique< QgsStyle >();
59 mTempStyle->createMemoryDatabase();
62 mProgressDlg =
nullptr;
63 mGroupSelectionDlg =
nullptr;
65 mNetManager =
new QNetworkAccessManager(
this );
68 if ( mDialogMode ==
Import )
70 setWindowTitle( tr(
"Import Item(s)" ) );
72 importTypeCombo->addItem( tr(
"File" ), ImportSource::File );
74 importTypeCombo->addItem( tr(
"URL" ), ImportSource::Url );
78 mSymbolTags->setText( QStringLiteral(
"imported" ) );
80 connect( mButtonFetch, &QAbstractButton::clicked,
this, &QgsStyleExportImportDialog::fetch );
83 mImportFileWidget->setDialogTitle( tr(
"Load Styles" ) );
84 mImportFileWidget->setFilter( tr(
"XML files (*.xml *.XML)" ) );
87 mImportFileWidget->setDefaultRoot( settings.
value( QStringLiteral(
"StyleManager/lastImportDir" ), QDir::homePath(),
QgsSettings::Gui ).toString() );
90 label->setText( tr(
"Select items to import" ) );
91 buttonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Import" ) );
94 listItems->setModel( mModel );
98 setWindowTitle( tr(
"Export Item(s)" ) );
100 mLocationStackedEdit->setHidden(
true );
101 fromLabel->setHidden(
true );
102 importTypeCombo->setHidden(
true );
103 mLocationLabel->setHidden(
true );
105 mFavorite->setHidden(
true );
106 mIgnoreXMLTags->setHidden(
true );
108 pb =
new QPushButton( tr(
"Select by Group…" ) );
109 buttonBox->addButton( pb, QDialogButtonBox::ActionRole );
111 tagLabel->setHidden(
true );
112 mSymbolTags->setHidden(
true );
113 tagHintLabel->setHidden(
true );
115 buttonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Export" ) );
121 listItems->setIconSize( QSize( static_cast< int >( iconSize ), static_cast< int >( iconSize * 0.9 ) ) );
124 listItems->setModel( mModel );
126 connect( listItems->selectionModel(), &QItemSelectionModel::selectionChanged,
127 this, &QgsStyleExportImportDialog::selectionChanged );
130 disconnect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
132 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
134 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsStyleExportImportDialog::showHelp );
139 QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
140 if ( selection.isEmpty() )
142 QMessageBox::warning(
this, tr(
"Export/import Item(s)" ),
143 tr(
"You should select at least one symbol/color ramp." ) );
147 if ( mDialogMode ==
Export )
149 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save Styles" ), QDir::homePath(),
150 tr(
"XML files (*.xml *.XML)" ) );
151 if ( fileName.isEmpty() )
157 if ( !fileName.endsWith( QLatin1String(
".xml" ), Qt::CaseInsensitive ) )
159 fileName += QLatin1String(
".xml" );
162 mFileName = fileName;
164 mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
165 moveStyles( &selection, mStyle, mTempStyle.get() );
166 if ( !mTempStyle->exportXml( mFileName ) )
168 mCursorOverride.reset();
169 QMessageBox::warning(
this, tr(
"Export Symbols" ),
170 tr(
"Error when saving selected symbols to file:\n%1" )
171 .arg( mTempStyle->errorString() ) );
176 mCursorOverride.reset();
177 QMessageBox::information(
this, tr(
"Export Symbols" ),
178 tr(
"The selected symbols were successfully exported to file:\n%1" )
184 mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
185 moveStyles( &selection, mTempStyle.get(), mStyle );
188 mCursorOverride.reset();
195 bool QgsStyleExportImportDialog::populateStyles()
202 if ( !mTempStyle->importXml( mFileName ) )
205 QMessageBox::warning(
this, tr(
"Import Symbols or Color Ramps" ),
206 tr(
"An error occurred during import:\n%1" ).arg( mTempStyle->errorString() ) );
212 void QgsStyleExportImportDialog::moveStyles( QModelIndexList *selection,
QgsStyle *src,
QgsStyle *dst )
214 QList< QgsStyleManagerDialog::ItemDetails > items;
215 items.reserve( selection->size() );
216 for (
int i = 0; i < selection->size(); ++i )
218 QModelIndex index = selection->at( i );
220 QgsStyleManagerDialog::ItemDetails details;
228 QgsStyleManagerDialog::copyItems( items, src, dst,
this, mCursorOverride, mDialogMode ==
Import,
229 mSymbolTags->text().split(
',' ), mFavorite->isChecked(), mIgnoreXMLTags->isChecked() );
235 delete mGroupSelectionDlg;
240 mImportFileWidget->setFilePath( path );
245 listItems->selectAll();
250 listItems->clearSelection();
255 Q_FOREACH (
const QString &symbolName, symbolNames )
257 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0,
QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
258 Q_FOREACH (
const QModelIndex &index, indexes )
260 listItems->selectionModel()->select( index, QItemSelectionModel::Select );
267 Q_FOREACH (
const QString &symbolName, symbolNames )
269 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0,
QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
270 Q_FOREACH (
const QModelIndex &index, indexes )
272 QItemSelection deselection( index, index );
273 listItems->selectionModel()->select( deselection, QItemSelectionModel::Deselect );
308 if ( ! mGroupSelectionDlg )
311 mGroupSelectionDlg->setWindowTitle( tr(
"Select Item(s) by Group" ) );
319 mGroupSelectionDlg->show();
320 mGroupSelectionDlg->raise();
321 mGroupSelectionDlg->activateWindow();
326 ImportSource source =
static_cast< ImportSource
>( importTypeCombo->itemData( index ).toInt() );
330 case ImportSource::File:
332 mLocationStackedEdit->setCurrentIndex( 0 );
333 mLocationLabel->setText( tr(
"File" ) );
337 case ImportSource::Official:
339 btnBrowse->setText( QStringLiteral(
"Fetch Items" ) );
340 locationLineEdit->setEnabled(
false );
344 case ImportSource::Url:
346 mLocationStackedEdit->setCurrentIndex( 1 );
347 mLocationLabel->setText( tr(
"URL" ) );
353 void QgsStyleExportImportDialog::fetch()
355 downloadStyleXml( QUrl( mUrlLineEdit->text() ) );
358 void QgsStyleExportImportDialog::importFileChanged(
const QString &path )
360 if ( path.isEmpty() )
364 QFileInfo pathInfo( mFileName );
365 QString tag = pathInfo.fileName().remove( QStringLiteral(
".xml" ) );
366 mSymbolTags->setText( tag );
367 if ( QFileInfo::exists( mFileName ) )
371 mImportFileWidget->setDefaultRoot( pathInfo.absolutePath() );
377 void QgsStyleExportImportDialog::downloadStyleXml(
const QUrl &url )
382 mTempFile =
new QTemporaryFile();
383 if ( mTempFile->open() )
385 mFileName = mTempFile->fileName();
389 QProgressDialog *dummy = mProgressDlg;
390 mProgressDlg =
nullptr;
393 mProgressDlg =
new QProgressDialog();
394 mProgressDlg->setLabelText( tr(
"Downloading style…" ) );
395 mProgressDlg->setAutoClose(
true );
397 connect( mProgressDlg, &QProgressDialog::canceled,
this, &QgsStyleExportImportDialog::downloadCanceled );
402 QNetworkReply *dummyReply = mNetReply;
406 mNetReply = mNetManager->get( QNetworkRequest( url ) );
408 connect( mNetReply, &QNetworkReply::finished,
this, &QgsStyleExportImportDialog::httpFinished );
409 connect( mNetReply, &QIODevice::readyRead,
this, &QgsStyleExportImportDialog::fileReadyRead );
410 connect( mNetReply, &QNetworkReply::downloadProgress,
this, &QgsStyleExportImportDialog::updateProgress );
414 void QgsStyleExportImportDialog::httpFinished()
416 if ( mNetReply->error() )
420 mProgressDlg->hide();
421 QMessageBox::information(
this, tr(
"Import from URL" ),
422 tr(
"HTTP Error! Download failed: %1." ).arg( mNetReply->errorString() ) );
434 void QgsStyleExportImportDialog::fileReadyRead()
436 mTempFile->write( mNetReply->readAll() );
439 void QgsStyleExportImportDialog::updateProgress( qint64 bytesRead, qint64 bytesTotal )
441 mProgressDlg->setMaximum( bytesTotal );
442 mProgressDlg->setValue( bytesRead );
445 void QgsStyleExportImportDialog::downloadCanceled()
452 void QgsStyleExportImportDialog::selectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
454 Q_UNUSED( selected );
455 Q_UNUSED( deselected );
456 bool nothingSelected = listItems->selectionModel()->selectedIndexes().empty();
457 buttonBox->button( QDialogButtonBox::Ok )->setDisabled( nothingSelected );
460 void QgsStyleExportImportDialog::showHelp()
462 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.
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)