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" ) );
121 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0) 126 listItems->setIconSize( QSize( static_cast< int >( iconSize ), static_cast< int >( iconSize * 0.9 ) ) );
129 listItems->setModel( mModel );
131 connect( listItems->selectionModel(), &QItemSelectionModel::selectionChanged,
132 this, &QgsStyleExportImportDialog::selectionChanged );
135 disconnect( buttonBox, &QDialogButtonBox::accepted,
this, &QDialog::accept );
137 buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
false );
139 connect( buttonBox, &QDialogButtonBox::helpRequested,
this, &QgsStyleExportImportDialog::showHelp );
144 QModelIndexList selection = listItems->selectionModel()->selectedIndexes();
145 if ( selection.isEmpty() )
147 QMessageBox::warning(
this, tr(
"Export/import Item(s)" ),
148 tr(
"You should select at least one symbol/color ramp." ) );
152 if ( mDialogMode ==
Export )
154 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save Styles" ), QDir::homePath(),
155 tr(
"XML files (*.xml *.XML)" ) );
156 if ( fileName.isEmpty() )
162 if ( !fileName.endsWith( QLatin1String(
".xml" ), Qt::CaseInsensitive ) )
164 fileName += QLatin1String(
".xml" );
167 mFileName = fileName;
169 mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
170 moveStyles( &selection, mStyle, mTempStyle.get() );
171 if ( !mTempStyle->exportXml( mFileName ) )
173 mCursorOverride.reset();
174 QMessageBox::warning(
this, tr(
"Export Symbols" ),
175 tr(
"Error when saving selected symbols to file:\n%1" )
176 .arg( mTempStyle->errorString() ) );
181 mCursorOverride.reset();
182 QMessageBox::information(
this, tr(
"Export Symbols" ),
183 tr(
"The selected symbols were successfully exported to file:\n%1" )
189 mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
190 moveStyles( &selection, mTempStyle.get(), mStyle );
193 mCursorOverride.reset();
200 bool QgsStyleExportImportDialog::populateStyles()
207 if ( !mTempStyle->importXml( mFileName ) )
210 QMessageBox::warning(
this, tr(
"Import Symbols or Color Ramps" ),
211 tr(
"An error occurred during import:\n%1" ).arg( mTempStyle->errorString() ) );
217 void QgsStyleExportImportDialog::moveStyles( QModelIndexList *selection,
QgsStyle *src,
QgsStyle *dst )
219 QList< QgsStyleManagerDialog::ItemDetails > items;
220 items.reserve( selection->size() );
221 for (
int i = 0; i < selection->size(); ++i )
223 QModelIndex index = selection->at( i );
225 QgsStyleManagerDialog::ItemDetails details;
233 QgsStyleManagerDialog::copyItems( items, src, dst,
this, mCursorOverride, mDialogMode ==
Import,
234 mSymbolTags->text().split(
',' ), mFavorite->isChecked(), mIgnoreXMLTags->isChecked() );
240 delete mGroupSelectionDlg;
245 mImportFileWidget->setFilePath( path );
250 listItems->selectAll();
255 listItems->clearSelection();
260 const auto constSymbolNames = symbolNames;
261 for (
const QString &symbolName : constSymbolNames )
263 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0,
QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
264 const auto constIndexes = indexes;
265 for (
const QModelIndex &index : constIndexes )
267 listItems->selectionModel()->select( index, QItemSelectionModel::Select );
274 const auto constSymbolNames = symbolNames;
275 for (
const QString &symbolName : constSymbolNames )
277 QModelIndexList indexes = listItems->model()->match( listItems->model()->index( 0,
QgsStyleModel::Name ), Qt::DisplayRole, symbolName, 1, Qt::MatchFixedString | Qt::MatchCaseSensitive );
278 const auto constIndexes = indexes;
279 for (
const QModelIndex &index : constIndexes )
281 QItemSelection deselection( index, index );
282 listItems->selectionModel()->select( deselection, QItemSelectionModel::Deselect );
321 if ( ! mGroupSelectionDlg )
324 mGroupSelectionDlg->setWindowTitle( tr(
"Select Item(s) by Group" ) );
332 mGroupSelectionDlg->show();
333 mGroupSelectionDlg->raise();
334 mGroupSelectionDlg->activateWindow();
339 ImportSource source =
static_cast< ImportSource
>( importTypeCombo->itemData( index ).toInt() );
343 case ImportSource::File:
345 mLocationStackedEdit->setCurrentIndex( 0 );
346 mLocationLabel->setText( tr(
"File" ) );
350 case ImportSource::Official:
352 btnBrowse->setText( QStringLiteral(
"Fetch Items" ) );
353 locationLineEdit->setEnabled(
false );
357 case ImportSource::Url:
359 mLocationStackedEdit->setCurrentIndex( 1 );
360 mLocationLabel->setText( tr(
"URL" ) );
366 void QgsStyleExportImportDialog::fetch()
368 downloadStyleXml( QUrl( mUrlLineEdit->text() ) );
371 void QgsStyleExportImportDialog::importFileChanged(
const QString &path )
373 if ( path.isEmpty() )
377 QFileInfo pathInfo( mFileName );
378 QString tag = pathInfo.fileName().remove( QStringLiteral(
".xml" ) );
379 mSymbolTags->setText( tag );
380 if ( QFileInfo::exists( mFileName ) )
384 mImportFileWidget->setDefaultRoot( pathInfo.absolutePath() );
390 void QgsStyleExportImportDialog::downloadStyleXml(
const QUrl &url )
392 mTempFile =
new QTemporaryFile();
393 if ( mTempFile->open() )
395 mFileName = mTempFile->fileName();
397 QProgressDialog *progressDlg =
new QProgressDialog(
this );
398 progressDlg->setLabelText( tr(
"Downloading style…" ) );
399 progressDlg->setAutoClose(
true );
408 QNetworkReply *reply = fetcher->
reply();
409 if ( !reply || reply->error() != QNetworkReply::NoError )
414 QMessageBox::information(
this, tr(
"Import from URL" ),
415 tr(
"HTTP Error! Download failed: %1." ).arg( reply->errorString() ) );
419 mTempFile->write( reply->readAll() );
424 progressDlg->deleteLater();
431 void QgsStyleExportImportDialog::selectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
434 Q_UNUSED( deselected )
435 bool nothingSelected = listItems->selectionModel()->selectedIndexes().empty();
436 buttonBox->button( QDialogButtonBox::Ok )->setDisabled( nothingSelected );
439 void QgsStyleExportImportDialog::showHelp()
441 QgsHelp::openHelp( QStringLiteral(
"style_library/style_manager.html#sharing-style-items" ) );
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)