Skip to content

Commit

Permalink
Avoid new colors in ramp overwriting old colors
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 14, 2016
1 parent 392a986 commit 0390760
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
10 changes: 6 additions & 4 deletions python/gui/qgscolorschemelist.sip
Expand Up @@ -85,8 +85,9 @@ class QgsColorSchemeModel: QAbstractItemModel
/** Add a color to the list
* @param color color to add
* @param label label for color
* @param allowDuplicate set to true to allow duplicate colors to be added (colors which are already present in the list)
*/
void addColor( const QColor color, const QString &label = QString() );
void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );

/** Returns whether the color scheme model has been modified
* @returns true if colors have been modified
Expand Down Expand Up @@ -162,11 +163,12 @@ class QgsColorSchemeList: QTreeView
*/
void removeSelection();

/** Adds a color to the list
/** Add a color to the list
* @param color color to add
* @param label optional label for color
* @param label label for color
* @param allowDuplicate set to true to allow duplicate colors to be added (colors which are already present in the list)
*/
void addColor( const QColor &color, const QString &label = QString() );
void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );

/** Pastes colors from clipboard to the list
* @see copyColors
Expand Down
25 changes: 14 additions & 11 deletions src/gui/qgscolorschemelist.cpp
Expand Up @@ -93,9 +93,9 @@ void QgsColorSchemeList::removeSelection()
}
}

void QgsColorSchemeList::addColor( const QColor &color, const QString &label )
void QgsColorSchemeList::addColor( const QColor &color, const QString &label, bool allowDuplicate )
{
mModel->addColor( color, label );
mModel->addColor( color, label, allowDuplicate );
}

void QgsColorSchemeList::pasteColors()
Expand Down Expand Up @@ -647,22 +647,25 @@ bool QgsColorSchemeModel::insertRows( int row, int count, const QModelIndex& par
return true;
}

void QgsColorSchemeModel::addColor( const QColor &color, const QString &label )
void QgsColorSchemeModel::addColor( const QColor &color, const QString &label, bool allowDuplicate )
{
if ( !mScheme || !mScheme->isEditable() )
{
return;
}

//matches existing color? if so, remove it first
QPair< QColor, QString > newColor = qMakePair( color, !label.isEmpty() ? label : QgsSymbolLayerUtils::colorToName( color ) );
//if color already exists, remove it
int existingIndex = mColors.indexOf( newColor );
if ( existingIndex >= 0 )
if ( !allowDuplicate )
{
beginRemoveRows( QModelIndex(), existingIndex, existingIndex );
mColors.removeAt( existingIndex );
endRemoveRows();
//matches existing color? if so, remove it first
QPair< QColor, QString > newColor = qMakePair( color, !label.isEmpty() ? label : QgsSymbolLayerUtils::colorToName( color ) );
//if color already exists, remove it
int existingIndex = mColors.indexOf( newColor );
if ( existingIndex >= 0 )
{
beginRemoveRows( QModelIndex(), existingIndex, existingIndex );
mColors.removeAt( existingIndex );
endRemoveRows();
}
}

int row = rowCount();
Expand Down
6 changes: 4 additions & 2 deletions src/gui/qgscolorschemelist.h
Expand Up @@ -119,8 +119,9 @@ class GUI_EXPORT QgsColorSchemeModel: public QAbstractItemModel
/** Add a color to the list
* @param color color to add
* @param label label for color
* @param allowDuplicate set to true to allow duplicate colors to be added (colors which are already present in the list)
*/
void addColor( const QColor &color, const QString &label = QString() );
void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );

/** Returns whether the color scheme model has been modified
* @returns true if colors have been modified
Expand Down Expand Up @@ -210,8 +211,9 @@ class GUI_EXPORT QgsColorSchemeList: public QTreeView
/** Adds a color to the list
* @param color color to add
* @param label optional label for color
* @param allowDuplicate set to true to allow duplicate colors to be added, ie colors which already exist in the list
*/
void addColor( const QColor &color, const QString &label = QString() );
void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );

/** Pastes colors from clipboard to the list
* @see copyColors
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgspresetcolorrampdialog.cpp
Expand Up @@ -74,7 +74,7 @@ void QgsPresetColorRampWidget::on_mButtonAddColor_clicked()
{
if ( dockMode() )
{
mTreeColors->addColor( QgsRecentColorScheme::lastUsedColor(), QgsSymbolLayerUtils::colorToName( QgsRecentColorScheme::lastUsedColor() ) );
mTreeColors->addColor( QgsRecentColorScheme::lastUsedColor(), QgsSymbolLayerUtils::colorToName( QgsRecentColorScheme::lastUsedColor() ), true );

QgsCompoundColorWidget* colorWidget = new QgsCompoundColorWidget( this, QgsRecentColorScheme::lastUsedColor(), QgsCompoundColorWidget::LayoutVertical );
colorWidget->setPanelTitle( tr( "Select Color" ) );
Expand Down

0 comments on commit 0390760

Please sign in to comment.