Skip to content

Commit

Permalink
Correctly handle selection of mixed item types
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 14, 2019
1 parent 4b154a4 commit f8af098
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 145 deletions.
13 changes: 11 additions & 2 deletions python/gui/auto_generated/symbology/qgsstylemanagerdialog.sip.in
Expand Up @@ -265,8 +265,17 @@ add a new color ramp to style
bool editSymbol(); bool editSymbol();
bool editColorRamp(); bool editColorRamp();


bool removeSymbol(); bool removeSymbol() /Deprecated/;
bool removeColorRamp(); %Docstring

.. deprecated:: in QGIS 3.6 - has no effect and will be removed in QGIS 4.0
%End

bool removeColorRamp() /Deprecated/;
%Docstring

.. deprecated:: in QGIS 3.6 - has no effect and will be removed in QGIS 4.0
%End


void enableSymbolInputs( bool ); void enableSymbolInputs( bool );
%Docstring %Docstring
Expand Down
242 changes: 101 additions & 141 deletions src/gui/symbology/qgsstylemanagerdialog.cpp
Expand Up @@ -355,20 +355,54 @@ void QgsStyleManagerDialog::tabItemType_currentChanged( int )
actnExportAsSVG->setVisible( isSymbol ); actnExportAsSVG->setVisible( isSymbol );


mModel->setEntityFilter( isSymbol ? QgsStyle::SymbolEntity : QgsStyle::ColorrampEntity ); mModel->setEntityFilter( isSymbol ? QgsStyle::SymbolEntity : QgsStyle::ColorrampEntity );
mModel->setEntityFilterEnabled( true ); mModel->setEntityFilterEnabled( !allTypesSelected() );
mModel->setSymbolTypeFilterEnabled( isSymbol && tabItemType->currentIndex() > 0 ); mModel->setSymbolTypeFilterEnabled( isSymbol && !allTypesSelected() );
mModel->setSymbolType( static_cast< QgsSymbol::SymbolType >( currentItemType() ) ); if ( isSymbol && !allTypesSelected() )
mModel->setSymbolType( static_cast< QgsSymbol::SymbolType >( currentItemType() ) );


populateList(); populateList();
} }


void QgsStyleManagerDialog::populateList() int QgsStyleManagerDialog::selectedItemType()
{
QModelIndex index = listItems->selectionModel()->currentIndex();
if ( !index.isValid() )
return 0;

const QgsStyle::StyleEntity entity = static_cast< QgsStyle::StyleEntity >( mModel->data( index, QgsStyleModel::TypeRole ).toInt() );
if ( entity == QgsStyle::ColorrampEntity )
return 3;

return mModel->data( index, QgsStyleModel::SymbolTypeRole ).toInt();
}

bool QgsStyleManagerDialog::allTypesSelected() const
{
return tabItemType->currentIndex() == 0;
}

QList< QgsStyleManagerDialog::ItemDetails > QgsStyleManagerDialog::selectedItems()
{ {
if ( currentItemType() > 3 ) QList<QgsStyleManagerDialog::ItemDetails > res;
QModelIndexList indices = listItems->selectionModel()->selectedRows();
for ( const QModelIndex &index : indices )
{ {
Q_ASSERT( false && "not implemented" ); if ( !index.isValid() )
return; continue;

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();

res << details;
} }
return res;
}

void QgsStyleManagerDialog::populateList()
{
groupChanged( groupTree->selectionModel()->currentIndex() ); groupChanged( groupTree->selectionModel()->currentIndex() );
} }


Expand Down Expand Up @@ -695,11 +729,11 @@ bool QgsStyleManagerDialog::addColorRamp( QAction *action )


void QgsStyleManagerDialog::editItem() void QgsStyleManagerDialog::editItem()
{ {
if ( currentItemType() < 3 ) if ( selectedItemType() < 3 )
{ {
editSymbol(); editSymbol();
} }
else if ( currentItemType() == 3 ) else if ( selectedItemType() == 3 )
{ {
editColorRamp(); editColorRamp();
} }
Expand Down Expand Up @@ -808,76 +842,60 @@ bool QgsStyleManagerDialog::editColorRamp()


void QgsStyleManagerDialog::removeItem() void QgsStyleManagerDialog::removeItem()
{ {
if ( currentItemType() < 3 ) const QList< ItemDetails > items = selectedItems();
{
removeSymbol(); if ( allTypesSelected() )
}
else if ( currentItemType() == 3 )
{ {
removeColorRamp(); if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Remove Items" ),
QString( tr( "Do you really want to remove %n item(s)?", nullptr, items.count() ) ),
QMessageBox::Yes,
QMessageBox::No ) )
return;
} }
else else
{ {
Q_ASSERT( false && "not implemented" ); if ( currentItemType() < 3 )
{
if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Remove Symbol" ),
QString( tr( "Do you really want to remove %n symbol(s)?", nullptr, items.count() ) ),
QMessageBox::Yes,
QMessageBox::No ) )
return;
}
else
{
if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Remove Color Ramp" ),
QString( tr( "Do you really want to remove %n ramp(s)?", nullptr, items.count() ) ),
QMessageBox::Yes,
QMessageBox::No ) )
return;
}
} }
}

bool QgsStyleManagerDialog::removeSymbol()
{
const QModelIndexList indexes = listItems->selectionModel()->selectedIndexes();
if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Remove Symbol" ),
QString( tr( "Do you really want to remove %n symbol(s)?", nullptr, indexes.count() ) ),
QMessageBox::Yes,
QMessageBox::No ) )
return false;


QgsTemporaryCursorOverride override( Qt::WaitCursor ); QgsTemporaryCursorOverride override( Qt::WaitCursor );


QStringList names; for ( const ItemDetails &details : items )
names.reserve( indexes.count() );
for ( const QModelIndex &index : indexes )
{ {
names << mModel->data( mModel->index( index.row(), QgsStyleModel::Name, index.parent() ), Qt::DisplayRole ).toString(); if ( details.name.isEmpty() )
} continue;


for ( const QString &symbolName : qgis::as_const( names ) ) if ( details.entityType == QgsStyle::SymbolEntity )
{ mStyle->removeSymbol( details.name );
// delete from style and update list else if ( details.entityType == QgsStyle::ColorrampEntity )
if ( !symbolName.isEmpty() ) mStyle->removeColorRamp( details.name );
mStyle->removeSymbol( symbolName );
} }


mModified = true; mModified = true;
return true;
} }


bool QgsStyleManagerDialog::removeColorRamp() bool QgsStyleManagerDialog::removeSymbol()
{ {
const QModelIndexList indexes = listItems->selectionModel()->selectedIndexes(); return false;
if ( QMessageBox::Yes != QMessageBox::question( this, tr( "Remove Color Ramp" ), }
QString( tr( "Do you really want to remove %n ramp(s)?", nullptr, indexes.count() ) ),
QMessageBox::Yes,
QMessageBox::No ) )
return false;

QgsTemporaryCursorOverride override( Qt::WaitCursor );

QStringList names;
names.reserve( indexes.count() );
for ( const QModelIndex &index : indexes )
{
names << mModel->data( mModel->index( index.row(), QgsStyleModel::Name, index.parent() ), Qt::DisplayRole ).toString();
}

for ( const QString &rampName : qgis::as_const( names ) )
{
// delete from style and update list
if ( !rampName.isEmpty() )
mStyle->removeColorRamp( rampName );
}


mModified = true; bool QgsStyleManagerDialog::removeColorRamp()
return true; {
return false;
} }


void QgsStyleManagerDialog::itemChanged( QStandardItem * ) void QgsStyleManagerDialog::itemChanged( QStandardItem * )
Expand Down Expand Up @@ -908,12 +926,14 @@ void QgsStyleManagerDialog::exportSelectedItemsImages( const QString &dir, const
if ( dir.isEmpty() ) if ( dir.isEmpty() )
return; return;


const QModelIndexList indexes = listItems->selectionModel()->selection().indexes(); const QList< ItemDetails > items = selectedItems();
for ( const QModelIndex &index : indexes ) for ( const ItemDetails &details : items )
{ {
QString name = mModel->data( mModel->index( index.row(), QgsStyleModel::Name, index.parent() ), Qt::DisplayRole ).toString(); if ( details.entityType != QgsStyle::SymbolEntity )
QString path = dir + '/' + name + '.' + format; continue;
std::unique_ptr< QgsSymbol > sym( mStyle->symbol( name ) );
QString path = dir + '/' + details.name + '.' + format;
std::unique_ptr< QgsSymbol > sym( mStyle->symbol( details.name ) );
if ( sym ) if ( sym )
sym->exportImage( path, format, size ); sym->exportImage( path, format, size );
} }
Expand Down Expand Up @@ -1031,6 +1051,8 @@ void QgsStyleManagerDialog::groupChanged( const QModelIndex &index )
else if ( category == QLatin1String( "favorite" ) ) else if ( category == QLatin1String( "favorite" ) )
{ {
enableGroupInputs( false ); enableGroupInputs( false );
mModel->setTagId( -1 );
mModel->setSmartGroupId( -1 );
mModel->setFavoritesOnly( true ); mModel->setFavoritesOnly( true );
} }
else if ( index.parent().data( Qt::UserRole + 1 ) == "smartgroups" ) else if ( index.parent().data( Qt::UserRole + 1 ) == "smartgroups" )
Expand Down Expand Up @@ -1280,10 +1302,6 @@ void QgsStyleManagerDialog::tagSymbolsAction()


mModel->setCheckable( true ); mModel->setCheckable( true );


// Connect to slot which handles regrouping
//connect( model, &QStandardItemModel::itemChanged,
// this, &QgsStyleManagerDialog::regrouped );

// No selection should be possible // No selection should be possible
listItems->setSelectionMode( QAbstractItemView::NoSelection ); listItems->setSelectionMode( QAbstractItemView::NoSelection );
mSymbolTreeView->setSelectionMode( QAbstractItemView::NoSelection ); mSymbolTreeView->setSelectionMode( QAbstractItemView::NoSelection );
Expand All @@ -1292,7 +1310,6 @@ void QgsStyleManagerDialog::tagSymbolsAction()


void QgsStyleManagerDialog::regrouped( QStandardItem * ) void QgsStyleManagerDialog::regrouped( QStandardItem * )
{ {

} }


void QgsStyleManagerDialog::setSymbolsChecked( const QStringList & ) void QgsStyleManagerDialog::setSymbolsChecked( const QStringList & )
Expand Down Expand Up @@ -1416,49 +1433,19 @@ void QgsStyleManagerDialog::listitemsContextMenu( QPoint point )


void QgsStyleManagerDialog::addFavoriteSelectedSymbols() void QgsStyleManagerDialog::addFavoriteSelectedSymbols()
{ {
QgsStyle::StyleEntity type = ( currentItemType() < 3 ) ? QgsStyle::SymbolEntity : QgsStyle::ColorrampEntity; const QList< ItemDetails > items = selectedItems();
if ( currentItemType() > 3 ) for ( const ItemDetails &details : items )
{
QgsDebugMsg( QStringLiteral( "unknown entity type" ) );
return;
}

const QModelIndexList indexes = listItems->selectionModel()->selectedIndexes();

QStringList names;
names.reserve( indexes.count() );
for ( const QModelIndex &index : indexes )
{ {
names << mModel->data( mModel->index( index.row(), QgsStyleModel::Name, index.parent() ), Qt::DisplayRole ).toString(); mStyle->addFavorite( details.entityType, details.name );
}

for ( const QString &symbolName : qgis::as_const( names ) )
{
mStyle->addFavorite( type, symbolName );
} }
} }


void QgsStyleManagerDialog::removeFavoriteSelectedSymbols() void QgsStyleManagerDialog::removeFavoriteSelectedSymbols()
{ {
QgsStyle::StyleEntity type = ( currentItemType() < 3 ) ? QgsStyle::SymbolEntity : QgsStyle::ColorrampEntity; const QList< ItemDetails > items = selectedItems();
if ( currentItemType() > 3 ) for ( const ItemDetails &details : items )
{
QgsDebugMsg( QStringLiteral( "unknown entity type" ) );
return;
}

const QModelIndexList indexes = listItems->selectionModel()->selectedIndexes();

QStringList names;
names.reserve( indexes.count() );
for ( const QModelIndex &index : indexes )
{
names << mModel->data( mModel->index( index.row(), QgsStyleModel::Name, index.parent() ), Qt::DisplayRole ).toString();
}

for ( const QString &name : qgis::as_const( names ) )
{ {
mStyle->removeFavorite( type, name ); mStyle->removeFavorite( details.entityType, details.name );
} }
} }


Expand All @@ -1467,21 +1454,7 @@ void QgsStyleManagerDialog::tagSelectedSymbols( bool newTag )
QAction *selectedItem = qobject_cast<QAction *>( sender() ); QAction *selectedItem = qobject_cast<QAction *>( sender() );
if ( selectedItem ) if ( selectedItem )
{ {
QgsStyle::StyleEntity type = ( currentItemType() < 3 ) ? QgsStyle::SymbolEntity : QgsStyle::ColorrampEntity; const QList< ItemDetails > items = selectedItems();
if ( currentItemType() > 3 )
{
QgsDebugMsg( QStringLiteral( "unknown entity type" ) );
return;
}

const QModelIndexList indexes = listItems->selectionModel()->selectedIndexes();
QStringList names;
names.reserve( indexes.count() );
for ( const QModelIndex &index : indexes )
{
names << mModel->data( mModel->index( index.row(), QgsStyleModel::Name, index.parent() ), Qt::DisplayRole ).toString();
}

QString tag; QString tag;
if ( newTag ) if ( newTag )
{ {
Expand All @@ -1498,9 +1471,9 @@ void QgsStyleManagerDialog::tagSelectedSymbols( bool newTag )
tag = selectedItem->data().toString(); tag = selectedItem->data().toString();
} }


for ( const QString &name : qgis::as_const( names ) ) for ( const ItemDetails &details : items )
{ {
mStyle->tagSymbol( type, name, QStringList( tag ) ); mStyle->tagSymbol( details.entityType, details.name, QStringList( tag ) );
} }
} }
} }
Expand All @@ -1511,23 +1484,10 @@ void QgsStyleManagerDialog::detagSelectedSymbols()


if ( selectedItem ) if ( selectedItem )
{ {
QgsStyle::StyleEntity type = ( currentItemType() < 3 ) ? QgsStyle::SymbolEntity : QgsStyle::ColorrampEntity; const QList< ItemDetails > items = selectedItems();
if ( currentItemType() > 3 ) for ( const ItemDetails &details : items )
{
QgsDebugMsg( QStringLiteral( "unknown entity type" ) );
return;
}
const QModelIndexList indexes = listItems->selectionModel()->selectedIndexes();
QStringList names;
names.reserve( indexes.count() );
for ( const QModelIndex &index : indexes )
{
names << mModel->data( mModel->index( index.row(), QgsStyleModel::Name, index.parent() ), Qt::DisplayRole ).toString();
}

for ( const QString &name : qgis::as_const( names ) )
{ {
mStyle->detagSymbol( type, name ); mStyle->detagSymbol( details.entityType, details.name );
} }
} }
} }
Expand Down

0 comments on commit f8af098

Please sign in to comment.