Skip to content

Commit dd00777

Browse files
committedJan 16, 2019
Refactor
1 parent 701a29f commit dd00777

File tree

3 files changed

+171
-147
lines changed

3 files changed

+171
-147
lines changed
 

‎src/gui/symbology/qgsstyleexportimportdialog.cpp

Lines changed: 12 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "qgssettings.h"
2828
#include "qgsgui.h"
2929
#include "qgsstylemodel.h"
30+
#include "qgsstylemanagerdialog.h"
3031

3132
#include <QInputDialog>
3233
#include <QCloseEvent>
@@ -210,158 +211,22 @@ bool QgsStyleExportImportDialog::populateStyles()
210211

211212
void QgsStyleExportImportDialog::moveStyles( QModelIndexList *selection, QgsStyle *src, QgsStyle *dst )
212213
{
213-
QString symbolName;
214-
QStringList symbolTags;
215-
bool symbolFavorite;
216-
QModelIndex index;
217-
bool isSymbol = true;
218-
bool prompt = true;
219-
bool overwrite = true;
220-
221-
QStringList importTags = mSymbolTags->text().split( ',' );
222-
223-
QStringList favoriteSymbols = src->symbolsOfFavorite( QgsStyle::SymbolEntity );
224-
QStringList favoriteColorramps = src->symbolsOfFavorite( QgsStyle::ColorrampEntity );
225-
214+
QList< QgsStyleManagerDialog::ItemDetails > items;
215+
items.reserve( selection->size() );
226216
for ( int i = 0; i < selection->size(); ++i )
227217
{
228-
index = selection->at( i );
229-
symbolName = mModel->data( mModel->index( index.row(), QgsStyleModel::Name ), Qt::DisplayRole ).toString();
230-
std::unique_ptr< QgsSymbol > symbol( src->symbol( symbolName ) );
231-
std::unique_ptr< QgsColorRamp > ramp;
232-
233-
if ( !mIgnoreXMLTags->isChecked() )
234-
{
235-
symbolTags = src->tagsOfSymbol( !symbol ? QgsStyle::ColorrampEntity : QgsStyle::SymbolEntity, symbolName );
236-
}
237-
else
238-
{
239-
symbolTags.clear();
240-
}
241-
242-
if ( mDialogMode == Import )
243-
{
244-
symbolTags << importTags;
245-
symbolFavorite = mFavorite->isChecked();
246-
}
247-
else
248-
{
249-
symbolFavorite = !symbol ? favoriteColorramps.contains( symbolName ) : favoriteSymbols.contains( symbolName );
250-
}
218+
QModelIndex index = selection->at( i );
251219

252-
if ( !symbol )
253-
{
254-
isSymbol = false;
255-
ramp.reset( src->colorRamp( symbolName ) );
256-
}
220+
QgsStyleManagerDialog::ItemDetails details;
221+
details.entityType = static_cast< QgsStyle::StyleEntity >( mModel->data( index, QgsStyleModel::TypeRole ).toInt() );
222+
if ( details.entityType == QgsStyle::SymbolEntity )
223+
details.symbolType = static_cast< QgsSymbol::SymbolType >( mModel->data( index, QgsStyleModel::SymbolTypeRole ).toInt() );
224+
details.name = mModel->data( mModel->index( index.row(), QgsStyleModel::Name, index.parent() ), Qt::DisplayRole ).toString();
257225

258-
if ( isSymbol )
259-
{
260-
if ( dst->symbolNames().contains( symbolName ) && prompt )
261-
{
262-
mCursorOverride.reset();
263-
int res = QMessageBox::warning( this, tr( "Export/import Symbols" ),
264-
tr( "Symbol with name '%1' already exists.\nOverwrite?" )
265-
.arg( symbolName ),
266-
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
267-
mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
268-
switch ( res )
269-
{
270-
case QMessageBox::Cancel:
271-
return;
272-
case QMessageBox::No:
273-
continue;
274-
case QMessageBox::Yes:
275-
{
276-
QgsSymbol *newSymbol = symbol.get();
277-
dst->addSymbol( symbolName, symbol.release() );
278-
dst->saveSymbol( symbolName, newSymbol, symbolFavorite, symbolTags );
279-
continue;
280-
}
281-
case QMessageBox::YesToAll:
282-
prompt = false;
283-
overwrite = true;
284-
break;
285-
case QMessageBox::NoToAll:
286-
prompt = false;
287-
overwrite = false;
288-
break;
289-
}
290-
}
291-
292-
if ( dst->symbolNames().contains( symbolName ) && overwrite )
293-
{
294-
QgsSymbol *newSymbol = symbol.get();
295-
dst->addSymbol( symbolName, symbol.release() );
296-
dst->saveSymbol( symbolName, newSymbol, symbolFavorite, symbolTags );
297-
continue;
298-
}
299-
else if ( dst->symbolNames().contains( symbolName ) && !overwrite )
300-
{
301-
continue;
302-
}
303-
else
304-
{
305-
QgsSymbol *newSymbol = symbol.get();
306-
dst->addSymbol( symbolName, symbol.release() );
307-
dst->saveSymbol( symbolName, newSymbol, symbolFavorite, symbolTags );
308-
continue;
309-
}
310-
}
311-
else
312-
{
313-
if ( dst->colorRampNames().contains( symbolName ) && prompt )
314-
{
315-
mCursorOverride.reset();
316-
int res = QMessageBox::warning( this, tr( "Export/import Color Ramps" ),
317-
tr( "Color ramp with name '%1' already exists.\nOverwrite?" )
318-
.arg( symbolName ),
319-
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
320-
mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
321-
switch ( res )
322-
{
323-
case QMessageBox::Cancel:
324-
return;
325-
case QMessageBox::No:
326-
continue;
327-
case QMessageBox::Yes:
328-
{
329-
QgsColorRamp *newRamp = ramp.get();
330-
dst->addColorRamp( symbolName, ramp.release() );
331-
dst->saveColorRamp( symbolName, newRamp, symbolFavorite, symbolTags );
332-
continue;
333-
}
334-
case QMessageBox::YesToAll:
335-
prompt = false;
336-
overwrite = true;
337-
break;
338-
case QMessageBox::NoToAll:
339-
prompt = false;
340-
overwrite = false;
341-
break;
342-
}
343-
}
344-
345-
if ( dst->colorRampNames().contains( symbolName ) && overwrite )
346-
{
347-
QgsColorRamp *newRamp = ramp.get();
348-
dst->addColorRamp( symbolName, ramp.release() );
349-
dst->saveColorRamp( symbolName, newRamp, symbolFavorite, symbolTags );
350-
continue;
351-
}
352-
else if ( dst->colorRampNames().contains( symbolName ) && !overwrite )
353-
{
354-
continue;
355-
}
356-
else
357-
{
358-
QgsColorRamp *newRamp = ramp.get();
359-
dst->addColorRamp( symbolName, ramp.release() );
360-
dst->saveColorRamp( symbolName, newRamp, symbolFavorite, symbolTags );
361-
continue;
362-
}
363-
}
226+
items << details;
364227
}
228+
QgsStyleManagerDialog::copyItems( items, src, dst, this, mCursorOverride, mDialogMode == Import,
229+
mSymbolTags->text().split( ',' ), mFavorite->isChecked(), mIgnoreXMLTags->isChecked() );
365230
}
366231

367232
QgsStyleExportImportDialog::~QgsStyleExportImportDialog()

‎src/gui/symbology/qgsstylemanagerdialog.cpp

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,157 @@ QList< QgsStyleManagerDialog::ItemDetails > QgsStyleManagerDialog::selectedItems
448448
return res;
449449
}
450450

451+
void QgsStyleManagerDialog::copyItems( const QList<QgsStyleManagerDialog::ItemDetails> &items, QgsStyle *src, QgsStyle *dst, QWidget *parentWidget,
452+
std::unique_ptr< QgsTemporaryCursorOverride > &cursorOverride, bool isImport, const QStringList &importTags, bool addToFavorites, bool ignoreSourceTags )
453+
{
454+
QStringList symbolTags;
455+
bool symbolFavorite;
456+
bool isSymbol = true;
457+
bool prompt = true;
458+
bool overwrite = true;
459+
460+
QStringList favoriteSymbols = src->symbolsOfFavorite( QgsStyle::SymbolEntity );
461+
QStringList favoriteColorramps = src->symbolsOfFavorite( QgsStyle::ColorrampEntity );
462+
463+
for ( auto &details : items )
464+
{
465+
std::unique_ptr< QgsSymbol > symbol( src->symbol( details.name ) );
466+
std::unique_ptr< QgsColorRamp > ramp;
467+
468+
if ( !ignoreSourceTags )
469+
{
470+
symbolTags = src->tagsOfSymbol( details.entityType, details.name );
471+
}
472+
else
473+
{
474+
symbolTags.clear();
475+
}
476+
477+
if ( isImport )
478+
{
479+
symbolTags << importTags;
480+
symbolFavorite = addToFavorites;
481+
}
482+
else
483+
{
484+
symbolFavorite = details.entityType == QgsStyle::ColorrampEntity ? favoriteColorramps.contains( details.name ) : favoriteSymbols.contains( details.name );
485+
}
486+
487+
if ( details.entityType == QgsStyle::ColorrampEntity )
488+
{
489+
isSymbol = false;
490+
ramp.reset( src->colorRamp( details.name ) );
491+
}
492+
493+
if ( isSymbol )
494+
{
495+
if ( dst->symbolNames().contains( details.name ) && prompt )
496+
{
497+
cursorOverride.reset();
498+
int res = QMessageBox::warning( parentWidget, tr( "Export/import Symbols" ),
499+
tr( "Symbol with name '%1' already exists.\nOverwrite?" )
500+
.arg( details.name ),
501+
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
502+
cursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
503+
switch ( res )
504+
{
505+
case QMessageBox::Cancel:
506+
return;
507+
case QMessageBox::No:
508+
continue;
509+
case QMessageBox::Yes:
510+
{
511+
QgsSymbol *newSymbol = symbol.get();
512+
dst->addSymbol( details.name, symbol.release() );
513+
dst->saveSymbol( details.name, newSymbol, symbolFavorite, symbolTags );
514+
continue;
515+
}
516+
case QMessageBox::YesToAll:
517+
prompt = false;
518+
overwrite = true;
519+
break;
520+
case QMessageBox::NoToAll:
521+
prompt = false;
522+
overwrite = false;
523+
break;
524+
}
525+
}
526+
527+
if ( dst->symbolNames().contains( details.name ) && overwrite )
528+
{
529+
QgsSymbol *newSymbol = symbol.get();
530+
dst->addSymbol( details.name, symbol.release() );
531+
dst->saveSymbol( details.name, newSymbol, symbolFavorite, symbolTags );
532+
continue;
533+
}
534+
else if ( dst->symbolNames().contains( details.name ) && !overwrite )
535+
{
536+
continue;
537+
}
538+
else
539+
{
540+
QgsSymbol *newSymbol = symbol.get();
541+
dst->addSymbol( details.name, symbol.release() );
542+
dst->saveSymbol( details.name, newSymbol, symbolFavorite, symbolTags );
543+
continue;
544+
}
545+
}
546+
else
547+
{
548+
if ( dst->colorRampNames().contains( details.name ) && prompt )
549+
{
550+
cursorOverride.reset();
551+
int res = QMessageBox::warning( parentWidget, tr( "Export/import Color Ramps" ),
552+
tr( "Color ramp with name '%1' already exists.\nOverwrite?" )
553+
.arg( details.name ),
554+
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
555+
cursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
556+
switch ( res )
557+
{
558+
case QMessageBox::Cancel:
559+
return;
560+
case QMessageBox::No:
561+
continue;
562+
case QMessageBox::Yes:
563+
{
564+
QgsColorRamp *newRamp = ramp.get();
565+
dst->addColorRamp( details.name, ramp.release() );
566+
dst->saveColorRamp( details.name, newRamp, symbolFavorite, symbolTags );
567+
continue;
568+
}
569+
case QMessageBox::YesToAll:
570+
prompt = false;
571+
overwrite = true;
572+
break;
573+
case QMessageBox::NoToAll:
574+
prompt = false;
575+
overwrite = false;
576+
break;
577+
}
578+
}
579+
580+
if ( dst->colorRampNames().contains( details.name ) && overwrite )
581+
{
582+
QgsColorRamp *newRamp = ramp.get();
583+
dst->addColorRamp( details.name, ramp.release() );
584+
dst->saveColorRamp( details.name, newRamp, symbolFavorite, symbolTags );
585+
continue;
586+
}
587+
else if ( dst->colorRampNames().contains( details.name ) && !overwrite )
588+
{
589+
continue;
590+
}
591+
else
592+
{
593+
QgsColorRamp *newRamp = ramp.get();
594+
dst->addColorRamp( details.name, ramp.release() );
595+
dst->saveColorRamp( details.name, newRamp, symbolFavorite, symbolTags );
596+
continue;
597+
}
598+
}
599+
}
600+
}
601+
451602
void QgsStyleManagerDialog::populateList()
452603
{
453604
groupChanged( groupTree->selectionModel()->currentIndex() );

‎src/gui/symbology/qgsstylemanagerdialog.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "qgis_gui.h"
2828

2929
class QgsStyle;
30+
class QgsTemporaryCursorOverride;
3031

3132
#ifndef SIP_RUN
3233
///@cond PRIVATE
@@ -333,6 +334,11 @@ class GUI_EXPORT QgsStyleManagerDialog : public QDialog, private Ui::QgsStyleMan
333334

334335
QList< ItemDetails > selectedItems();
335336

337+
static void copyItems( const QList< ItemDetails > &items, QgsStyle *src, QgsStyle *dst,
338+
QWidget *parentWidget, std::unique_ptr<QgsTemporaryCursorOverride> &cursorOverride,
339+
bool isImport, const QStringList &importTags, bool addToFavorites, bool ignoreSourceTags );
340+
341+
336342
QgsStyle *mStyle = nullptr;
337343

338344
QgsCheckableStyleModel *mModel = nullptr;
@@ -364,6 +370,8 @@ class GUI_EXPORT QgsStyleManagerDialog : public QDialog, private Ui::QgsStyleMan
364370
bool mReadOnly = false;
365371
bool mFavoritesGroupVisible = true;
366372
bool mSmartGroupVisible = true;
373+
374+
friend class QgsStyleExportImportDialog;
367375
};
368376

369377
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.