Skip to content
Permalink
Browse files

Refactor

  • Loading branch information
nyalldawson committed Jan 16, 2019
1 parent 701a29f commit dd00777b433bcdcac21bdbda08429f016d0ff367
@@ -27,6 +27,7 @@
#include "qgssettings.h"
#include "qgsgui.h"
#include "qgsstylemodel.h"
#include "qgsstylemanagerdialog.h"

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

void QgsStyleExportImportDialog::moveStyles( QModelIndexList *selection, QgsStyle *src, QgsStyle *dst )
{
QString symbolName;
QStringList symbolTags;
bool symbolFavorite;
QModelIndex index;
bool isSymbol = true;
bool prompt = true;
bool overwrite = true;

QStringList importTags = mSymbolTags->text().split( ',' );

QStringList favoriteSymbols = src->symbolsOfFavorite( QgsStyle::SymbolEntity );
QStringList favoriteColorramps = src->symbolsOfFavorite( QgsStyle::ColorrampEntity );

QList< QgsStyleManagerDialog::ItemDetails > items;
items.reserve( selection->size() );
for ( int i = 0; i < selection->size(); ++i )
{
index = selection->at( i );
symbolName = mModel->data( mModel->index( index.row(), QgsStyleModel::Name ), Qt::DisplayRole ).toString();
std::unique_ptr< QgsSymbol > symbol( src->symbol( symbolName ) );
std::unique_ptr< QgsColorRamp > ramp;

if ( !mIgnoreXMLTags->isChecked() )
{
symbolTags = src->tagsOfSymbol( !symbol ? QgsStyle::ColorrampEntity : QgsStyle::SymbolEntity, symbolName );
}
else
{
symbolTags.clear();
}

if ( mDialogMode == Import )
{
symbolTags << importTags;
symbolFavorite = mFavorite->isChecked();
}
else
{
symbolFavorite = !symbol ? favoriteColorramps.contains( symbolName ) : favoriteSymbols.contains( symbolName );
}
QModelIndex index = selection->at( i );

if ( !symbol )
{
isSymbol = false;
ramp.reset( src->colorRamp( symbolName ) );
}
QgsStyleManagerDialog::ItemDetails details;
details.entityType = static_cast< QgsStyle::StyleEntity >( mModel->data( index, QgsStyleModel::TypeRole ).toInt() );
if ( details.entityType == QgsStyle::SymbolEntity )
details.symbolType = static_cast< QgsSymbol::SymbolType >( mModel->data( index, QgsStyleModel::SymbolTypeRole ).toInt() );
details.name = mModel->data( mModel->index( index.row(), QgsStyleModel::Name, index.parent() ), Qt::DisplayRole ).toString();

if ( isSymbol )
{
if ( dst->symbolNames().contains( symbolName ) && prompt )
{
mCursorOverride.reset();
int res = QMessageBox::warning( this, tr( "Export/import Symbols" ),
tr( "Symbol with name '%1' already exists.\nOverwrite?" )
.arg( symbolName ),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
switch ( res )
{
case QMessageBox::Cancel:
return;
case QMessageBox::No:
continue;
case QMessageBox::Yes:
{
QgsSymbol *newSymbol = symbol.get();
dst->addSymbol( symbolName, symbol.release() );
dst->saveSymbol( symbolName, newSymbol, symbolFavorite, symbolTags );
continue;
}
case QMessageBox::YesToAll:
prompt = false;
overwrite = true;
break;
case QMessageBox::NoToAll:
prompt = false;
overwrite = false;
break;
}
}

if ( dst->symbolNames().contains( symbolName ) && overwrite )
{
QgsSymbol *newSymbol = symbol.get();
dst->addSymbol( symbolName, symbol.release() );
dst->saveSymbol( symbolName, newSymbol, symbolFavorite, symbolTags );
continue;
}
else if ( dst->symbolNames().contains( symbolName ) && !overwrite )
{
continue;
}
else
{
QgsSymbol *newSymbol = symbol.get();
dst->addSymbol( symbolName, symbol.release() );
dst->saveSymbol( symbolName, newSymbol, symbolFavorite, symbolTags );
continue;
}
}
else
{
if ( dst->colorRampNames().contains( symbolName ) && prompt )
{
mCursorOverride.reset();
int res = QMessageBox::warning( this, tr( "Export/import Color Ramps" ),
tr( "Color ramp with name '%1' already exists.\nOverwrite?" )
.arg( symbolName ),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
mCursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
switch ( res )
{
case QMessageBox::Cancel:
return;
case QMessageBox::No:
continue;
case QMessageBox::Yes:
{
QgsColorRamp *newRamp = ramp.get();
dst->addColorRamp( symbolName, ramp.release() );
dst->saveColorRamp( symbolName, newRamp, symbolFavorite, symbolTags );
continue;
}
case QMessageBox::YesToAll:
prompt = false;
overwrite = true;
break;
case QMessageBox::NoToAll:
prompt = false;
overwrite = false;
break;
}
}

if ( dst->colorRampNames().contains( symbolName ) && overwrite )
{
QgsColorRamp *newRamp = ramp.get();
dst->addColorRamp( symbolName, ramp.release() );
dst->saveColorRamp( symbolName, newRamp, symbolFavorite, symbolTags );
continue;
}
else if ( dst->colorRampNames().contains( symbolName ) && !overwrite )
{
continue;
}
else
{
QgsColorRamp *newRamp = ramp.get();
dst->addColorRamp( symbolName, ramp.release() );
dst->saveColorRamp( symbolName, newRamp, symbolFavorite, symbolTags );
continue;
}
}
items << details;
}
QgsStyleManagerDialog::copyItems( items, src, dst, this, mCursorOverride, mDialogMode == Import,
mSymbolTags->text().split( ',' ), mFavorite->isChecked(), mIgnoreXMLTags->isChecked() );
}

QgsStyleExportImportDialog::~QgsStyleExportImportDialog()
@@ -448,6 +448,157 @@ QList< QgsStyleManagerDialog::ItemDetails > QgsStyleManagerDialog::selectedItems
return res;
}

void QgsStyleManagerDialog::copyItems( const QList<QgsStyleManagerDialog::ItemDetails> &items, QgsStyle *src, QgsStyle *dst, QWidget *parentWidget,
std::unique_ptr< QgsTemporaryCursorOverride > &cursorOverride, bool isImport, const QStringList &importTags, bool addToFavorites, bool ignoreSourceTags )
{
QStringList symbolTags;
bool symbolFavorite;
bool isSymbol = true;
bool prompt = true;
bool overwrite = true;

QStringList favoriteSymbols = src->symbolsOfFavorite( QgsStyle::SymbolEntity );
QStringList favoriteColorramps = src->symbolsOfFavorite( QgsStyle::ColorrampEntity );

for ( auto &details : items )
{
std::unique_ptr< QgsSymbol > symbol( src->symbol( details.name ) );
std::unique_ptr< QgsColorRamp > ramp;

if ( !ignoreSourceTags )
{
symbolTags = src->tagsOfSymbol( details.entityType, details.name );
}
else
{
symbolTags.clear();
}

if ( isImport )
{
symbolTags << importTags;
symbolFavorite = addToFavorites;
}
else
{
symbolFavorite = details.entityType == QgsStyle::ColorrampEntity ? favoriteColorramps.contains( details.name ) : favoriteSymbols.contains( details.name );
}

if ( details.entityType == QgsStyle::ColorrampEntity )
{
isSymbol = false;
ramp.reset( src->colorRamp( details.name ) );
}

if ( isSymbol )
{
if ( dst->symbolNames().contains( details.name ) && prompt )
{
cursorOverride.reset();
int res = QMessageBox::warning( parentWidget, tr( "Export/import Symbols" ),
tr( "Symbol with name '%1' already exists.\nOverwrite?" )
.arg( details.name ),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
cursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
switch ( res )
{
case QMessageBox::Cancel:
return;
case QMessageBox::No:
continue;
case QMessageBox::Yes:
{
QgsSymbol *newSymbol = symbol.get();
dst->addSymbol( details.name, symbol.release() );
dst->saveSymbol( details.name, newSymbol, symbolFavorite, symbolTags );
continue;
}
case QMessageBox::YesToAll:
prompt = false;
overwrite = true;
break;
case QMessageBox::NoToAll:
prompt = false;
overwrite = false;
break;
}
}

if ( dst->symbolNames().contains( details.name ) && overwrite )
{
QgsSymbol *newSymbol = symbol.get();
dst->addSymbol( details.name, symbol.release() );
dst->saveSymbol( details.name, newSymbol, symbolFavorite, symbolTags );
continue;
}
else if ( dst->symbolNames().contains( details.name ) && !overwrite )
{
continue;
}
else
{
QgsSymbol *newSymbol = symbol.get();
dst->addSymbol( details.name, symbol.release() );
dst->saveSymbol( details.name, newSymbol, symbolFavorite, symbolTags );
continue;
}
}
else
{
if ( dst->colorRampNames().contains( details.name ) && prompt )
{
cursorOverride.reset();
int res = QMessageBox::warning( parentWidget, tr( "Export/import Color Ramps" ),
tr( "Color ramp with name '%1' already exists.\nOverwrite?" )
.arg( details.name ),
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel );
cursorOverride = qgis::make_unique< QgsTemporaryCursorOverride >( Qt::WaitCursor );
switch ( res )
{
case QMessageBox::Cancel:
return;
case QMessageBox::No:
continue;
case QMessageBox::Yes:
{
QgsColorRamp *newRamp = ramp.get();
dst->addColorRamp( details.name, ramp.release() );
dst->saveColorRamp( details.name, newRamp, symbolFavorite, symbolTags );
continue;
}
case QMessageBox::YesToAll:
prompt = false;
overwrite = true;
break;
case QMessageBox::NoToAll:
prompt = false;
overwrite = false;
break;
}
}

if ( dst->colorRampNames().contains( details.name ) && overwrite )
{
QgsColorRamp *newRamp = ramp.get();
dst->addColorRamp( details.name, ramp.release() );
dst->saveColorRamp( details.name, newRamp, symbolFavorite, symbolTags );
continue;
}
else if ( dst->colorRampNames().contains( details.name ) && !overwrite )
{
continue;
}
else
{
QgsColorRamp *newRamp = ramp.get();
dst->addColorRamp( details.name, ramp.release() );
dst->saveColorRamp( details.name, newRamp, symbolFavorite, symbolTags );
continue;
}
}
}
}

void QgsStyleManagerDialog::populateList()
{
groupChanged( groupTree->selectionModel()->currentIndex() );
@@ -27,6 +27,7 @@
#include "qgis_gui.h"

class QgsStyle;
class QgsTemporaryCursorOverride;

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

QList< ItemDetails > selectedItems();

static void copyItems( const QList< ItemDetails > &items, QgsStyle *src, QgsStyle *dst,
QWidget *parentWidget, std::unique_ptr<QgsTemporaryCursorOverride> &cursorOverride,
bool isImport, const QStringList &importTags, bool addToFavorites, bool ignoreSourceTags );


QgsStyle *mStyle = nullptr;

QgsCheckableStyleModel *mModel = nullptr;
@@ -364,6 +370,8 @@ class GUI_EXPORT QgsStyleManagerDialog : public QDialog, private Ui::QgsStyleMan
bool mReadOnly = false;
bool mFavoritesGroupVisible = true;
bool mSmartGroupVisible = true;

friend class QgsStyleExportImportDialog;
};

#endif

0 comments on commit dd00777

Please sign in to comment.
You can’t perform that action at this time.