Skip to content

Commit abaebbe

Browse files
committed
Use copy mode for drag and drop of colors from QgsColorSchemeList,
prevent addition of duplicate colors
1 parent 33b120f commit abaebbe

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/gui/qgscolorschemelist.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ QgsColorSchemeList::QgsColorSchemeList( QWidget *parent , QgsColorScheme *scheme
4545
setAcceptDrops( true );
4646
setDragDropMode( QTreeView::DragDrop );
4747
setDropIndicatorShown( true );
48-
setDefaultDropAction( Qt::MoveAction );
48+
setDefaultDropAction( Qt::CopyAction );
4949
}
5050

5151
QgsColorSchemeList::~QgsColorSchemeList()
@@ -414,7 +414,7 @@ Qt::DropActions QgsColorSchemeModel::supportedDropActions() const
414414
{
415415
if ( mScheme->isEditable() )
416416
{
417-
return Qt::MoveAction | Qt::CopyAction;
417+
return Qt::CopyAction | Qt::MoveAction;
418418
}
419419
else
420420
{
@@ -450,7 +450,7 @@ QMimeData* QgsColorSchemeModel::mimeData( const QModelIndexList &indexes ) const
450450
colorList << qMakePair( mColors[( *indexIt ).row()].first, mColors[( *indexIt ).row()].second );
451451
}
452452

453-
QMimeData* mimeData = QgsSymbolLayerV2Utils::colorListToMimeData( colorList, false );
453+
QMimeData* mimeData = QgsSymbolLayerV2Utils::colorListToMimeData( colorList );
454454
return mimeData;
455455
}
456456

@@ -482,9 +482,31 @@ bool QgsColorSchemeModel::dropMimeData( const QMimeData *data, Qt::DropAction ac
482482
return false;
483483
}
484484

485+
//any existing colors? if so, remove them first
486+
QgsNamedColorList::const_iterator colorIt = droppedColors.constBegin();
487+
for ( ; colorIt != droppedColors.constEnd(); ++colorIt )
488+
{
489+
//dest color
490+
QPair< QColor, QString > color = qMakePair(( *colorIt ).first, !( *colorIt ).second.isEmpty() ? ( *colorIt ).second : QgsSymbolLayerV2Utils::colorToName(( *colorIt ).first ) );
491+
//if color already exists, remove it
492+
int existingIndex = mColors.indexOf( color );
493+
if ( existingIndex >= 0 )
494+
{
495+
if ( existingIndex < beginRow )
496+
{
497+
//color is before destination row, so decrease destination row to account for removal
498+
beginRow--;
499+
}
500+
501+
beginRemoveRows( parent, existingIndex, existingIndex );
502+
mColors.removeAt( existingIndex );
503+
endRemoveRows();
504+
}
505+
}
506+
485507
//insert dropped colors
486508
insertRows( beginRow, droppedColors.length(), QModelIndex() );
487-
QgsNamedColorList::const_iterator colorIt = droppedColors.constBegin();
509+
colorIt = droppedColors.constBegin();
488510
for ( ; colorIt != droppedColors.constEnd(); ++colorIt )
489511
{
490512
QModelIndex colorIdx = index( beginRow, 0, QModelIndex() );

0 commit comments

Comments
 (0)