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;
69 if ( mDialogMode ==
Import )
71 setWindowTitle( tr(
"Import Item(s)" ) );
73 importTypeCombo->addItem( tr(
"File" ), ImportSource::File );
75 importTypeCombo->addItem( tr(
"URL" ), ImportSource::Url );
79 mSymbolTags->setText( QStringLiteral(
"imported" ) );
81 connect( mButtonFetch, &QAbstractButton::clicked,
this, &QgsStyleExportImportDialog::fetch );
84 mImportFileWidget->setDialogTitle( tr(
"Load Styles" ) );
85 mImportFileWidget->setFilter( tr(
"XML files (*.xml *.XML)" ) );
88 mImportFileWidget->setDefaultRoot( settings.
value( QStringLiteral(
"StyleManager/lastImportDir" ), QDir::homePath(),
QgsSettings::Gui ).toString() );
91 label->setText( tr(
"Select items to import" ) );
92 buttonBox->button( QDialogButtonBox::Ok )->setText( tr(
"Import" ) );
95 listItems->setModel( mModel );
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" ) );
122 listItems->setIconSize( QSize( static_cast< int >( iconSize ), static_cast< int >( iconSize * 0.9 ) ) );
125 listItems->setModel( mModel );
127 connect( listItems->selectionModel(), &QItemSelectionModel::selectionChanged,
128 this, &QgsStyleExportImportDialog::selectionChanged );
131 disconnect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
133 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
135 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsStyleExportImportDialog::showHelp );
140 QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
141 if ( selection.isEmpty() )
143 QMessageBox::warning(
this, tr(
"Export/import Item(s)" ),
144 tr(
"You should select at least one symbol/color ramp." ) );
148 if ( mDialogMode ==
Export )
150 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save Styles" ), QDir::homePath(),
151 tr(
"XML files (*.xml *.XML)" ) );
152 if ( fileName.isEmpty() )
158 if ( !fileName.endsWith( QLatin1String(
".xml" ), Qt::CaseInsensitive ) )
160 fileName += QLatin1String(
".xml" );
163 mFileName = fileName;
165 mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
166 moveStyles( &selection, mStyle, mTempStyle.get() );
167 if ( !mTempStyle->exportXml( mFileName ) )
169 mCursorOverride.reset();
170 QMessageBox::warning(
this, tr(
"Export Symbols" ),
171 tr(
"Error when saving selected symbols to file:\n%1" )
172 .arg( mTempStyle->errorString() ) );
177 mCursorOverride.reset();
178 QMessageBox::information(
this, tr(
"Export Symbols" ),
179 tr(
"The selected symbols were successfully exported to file:\n%1" )
185 mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
186 moveStyles( &selection, mTempStyle.get(), mStyle );
189 mCursorOverride.reset();
196 bool QgsStyleExportImportDialog::populateStyles()
203 if ( !mTempStyle->importXml( mFileName ) )
206 QMessageBox::warning(
this, tr(
"Import Symbols or Color Ramps" ),
207 tr(
"An error occurred during import:\n%1" ).arg( mTempStyle->errorString() ) );
213 void QgsStyleExportImportDialog::moveStyles( QModelIndexList *selection,
QgsStyle *src,
QgsStyle *dst )
215 QList< QgsStyleManagerDialog::ItemDetails > items;
216 items.reserve( selection->size() );
217 for (
int i = 0; i < selection->size(); ++i )
219 QModelIndex index = selection->at( i );
221 QgsStyleManagerDialog::ItemDetails details;
229 QgsStyleManagerDialog::copyItems( items, src, dst,
this, mCursorOverride, mDialogMode ==
Import,
230 mSymbolTags->text().split(
',' ), mFavorite->isChecked(), mIgnoreXMLTags->isChecked() );
236 delete mGroupSelectionDlg;
241 mImportFileWidget->setFilePath( path );
246 listItems->selectAll();
251 listItems->clearSelection();
256 const auto constSymbolNames = symbolNames;
257 for (
const QString &symbolName : constSymbolNames )
259 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0,
QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
260 const auto constIndexes = indexes;
261 for (
const QModelIndex &index : constIndexes )
263 listItems->selectionModel()->select( index, QItemSelectionModel::Select );
270 const auto constSymbolNames = symbolNames;
271 for (
const QString &symbolName : constSymbolNames )
273 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0,
QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
274 const auto constIndexes = indexes;
275 for (
const QModelIndex &index : constIndexes )
277 QItemSelection deselection( index, index );
278 listItems->selectionModel()->select( deselection, QItemSelectionModel::Deselect );
317 if ( ! mGroupSelectionDlg )
320 mGroupSelectionDlg->setWindowTitle( tr(
"Select Item(s) by Group" ) );
328 mGroupSelectionDlg->show();
329 mGroupSelectionDlg->raise();
330 mGroupSelectionDlg->activateWindow();
335 ImportSource source =
static_cast< ImportSource
>( importTypeCombo->itemData( index ).toInt() );
339 case ImportSource::File:
341 mLocationStackedEdit->setCurrentIndex( 0 );
342 mLocationLabel->setText( tr(
"File" ) );
346 case ImportSource::Official:
348 btnBrowse->setText( QStringLiteral(
"Fetch Items" ) );
349 locationLineEdit->setEnabled(
false );
353 case ImportSource::Url:
355 mLocationStackedEdit->setCurrentIndex( 1 );
356 mLocationLabel->setText( tr(
"URL" ) );
362 void QgsStyleExportImportDialog::fetch()
364 downloadStyleXml( QUrl( mUrlLineEdit->text() ) );
367 void QgsStyleExportImportDialog::importFileChanged(
const QString &path )
369 if ( path.isEmpty() )
373 QFileInfo pathInfo( mFileName );
374 QString tag = pathInfo.fileName().remove( QStringLiteral(
".xml" ) );
375 mSymbolTags->setText( tag );
376 if ( QFileInfo::exists( mFileName ) )
380 mImportFileWidget->setDefaultRoot( pathInfo.absolutePath() );
386 void QgsStyleExportImportDialog::downloadStyleXml(
const QUrl &url )
388 mTempFile =
new QTemporaryFile();
389 if ( mTempFile->open() )
391 mFileName = mTempFile->fileName();
393 QProgressDialog *progressDlg =
new QProgressDialog(
this );
394 progressDlg->setLabelText( tr(
"Downloading style…" ) );
395 progressDlg->setAutoClose(
true );
404 QNetworkReply *reply = fetcher->
reply();
405 if ( !reply || reply->error() != QNetworkReply::NoError )
410 QMessageBox::information(
this, tr(
"Import from URL" ),
411 tr(
"HTTP Error! Download failed: %1." ).arg( reply->errorString() ) );
415 mTempFile->write( reply->readAll() );
420 progressDlg->deleteLater();
427 void QgsStyleExportImportDialog::selectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
430 Q_UNUSED( deselected )
431 bool nothingSelected = listItems->selectionModel()->selectedIndexes().empty();
432 buttonBox->button( QDialogButtonBox::Ok )->setDisabled( nothingSelected );
435 void QgsStyleExportImportDialog::showHelp()
437 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 setDescription(const QString &description)
Sets the task's description.
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:
void fetched()
Emitted when the network content has been fetched, regardless of whether the fetch was successful or ...
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
QNetworkReply * reply()
Returns the network reply.
void selectSmartgroup(const QString &groupName)
selectSmartgroup selects all symbols from a smart group
Handles HTTP network content fetching in a background task.
void progressChanged(double progress)
Will be emitted by task when its progress changes.
void allDeselected()
all deselected
StyleEntity
Enum for Entities involved in a style.
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling...
SymbolType
Type of the symbol.
void deselectSymbols(const QStringList &symbolNames)
deselectSymbols deselect symbols by name
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
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 cancel() override
Notifies the task that it should terminate.
void tagDeselected(const QString &tagName)
tag with tagName has been deselected
Symbol type (for symbol entities)
void importTypeChanged(int)