Skip to content
Permalink
Browse files

Improvements to color list:

- Cleanup code
- Support copying and pasting multiple colors to list
- Support additional color list mime formats
  • Loading branch information
nyalldawson committed Aug 17, 2014
1 parent 6d10ff3 commit b45735529b4e7e319dde54104df42537b2a8c8f0
@@ -200,6 +200,23 @@ class QgsSymbolLayerV2Utils
*/
static QList< QColor > parseColorList( const QString colorStr );

/**
* Attempts to parse mime data as a list of named colors
* @param data mime data to parse
* @returns list of parsed colors
* @note added in 2.5
*/
static QgsNamedColorList colorListFromMimeData( const QMimeData *data );

/**
* Creates mime data from a list of named colors
* @param colorList list of named colors
* @param allFormats set to true to include additional mime formats, include text/plain and application/x-color
* @returns mime data containing encoded colors
* @note added in 2.5
*/
static QMimeData* colorListToMimeData( const QgsNamedColorList colorList, const bool allFormats = true );

/**
* Attempts to parse a string as a color using a variety of common formats, including hex
* codes, rgb and rgba strings.
@@ -120,6 +120,8 @@ class QgsColorSchemeList: QTreeView
*/
void setScheme( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor() );

public slots:

/**Saves the current colors shown in the list back to a color scheme, if supported
* by the color scheme.
* @note this method is only effective if the color scheme is editable
@@ -136,5 +138,15 @@ class QgsColorSchemeList: QTreeView
*/
void addColor( const QColor color, const QString label = QString() );

/**Pastes colors from clipboard to the list
* @see copyColors
*/
void pasteColors();

/**Copies colors from the list to the clipboard
* @see pasteColors
*/
void copyColors();

};

@@ -688,6 +688,9 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
//
// Color palette
//
connect( mButtonCopyColors, SIGNAL( clicked() ), mTreeCustomColors, SLOT( copyColors() ) );
connect( mButtonRemoveColor, SIGNAL( clicked() ), mTreeCustomColors, SLOT( removeSelection() ) );
connect( mButtonPasteColors, SIGNAL( clicked() ), mTreeCustomColors, SLOT( pasteColors() ) );

//find custom color scheme from registry
QList<QgsCustomColorScheme *> customSchemes;
@@ -2086,8 +2089,3 @@ void QgsOptions::on_mButtonAddColor_clicked()

mTreeCustomColors->addColor( newColor );
}

void QgsOptions::on_mButtonRemoveColor_clicked()
{
mTreeCustomColors->removeSelection();
}
@@ -242,7 +242,6 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
void on_mAddDefaultTransformButton_clicked();

void on_mButtonAddColor_clicked();
void on_mButtonRemoveColor_clicked();

private:
QStringList i18nList();
@@ -479,6 +479,10 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
populateStyles();

// Color palette
connect( mButtonCopyColors, SIGNAL( clicked() ), mTreeProjectColors, SLOT( copyColors() ) );
connect( mButtonRemoveColor, SIGNAL( clicked() ), mTreeProjectColors, SLOT( removeSelection() ) );
connect( mButtonPasteColors, SIGNAL( clicked() ), mTreeProjectColors, SLOT( pasteColors() ) );

QList<QgsProjectColorScheme *> projectSchemes;
QgsColorSchemeRegistry::instance()->schemes( projectSchemes );
if ( projectSchemes.length() > 0 )
@@ -1680,8 +1684,3 @@ void QgsProjectProperties::on_mButtonAddColor_clicked()

mTreeProjectColors->addColor( newColor );
}

void QgsProjectProperties::on_mButtonRemoveColor_clicked()
{
mTreeProjectColors->removeSelection();
}
@@ -168,8 +168,6 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:

void on_mButtonAddColor_clicked();

void on_mButtonRemoveColor_clicked();

signals:
//! Signal used to inform listeners that the mouse display precision may have changed
void displayPrecisionChanged();
@@ -2736,8 +2736,8 @@ QList<QColor> QgsSymbolLayerV2Utils::parseColorList( const QString colorStr )
{
QList<QColor> colors;

//try splitting string at newlines
QStringList components = colorStr.split( QRegExp( "\n" ) );
//try splitting string at commas, spaces or newlines
QStringList components = colorStr.simplified().split( QRegExp( "(,|\\s)" ) );
QStringList::iterator it = components.begin();
for ( ; it != components.end(); ++it )
{
@@ -2752,7 +2752,23 @@ QList<QColor> QgsSymbolLayerV2Utils::parseColorList( const QString colorStr )
return colors;
}

//try splitting string at whitespace
//try splitting string at commas or newlines
components = colorStr.split( QRegExp( "(,|\n)" ) );
it = components.begin();
for ( ; it != components.end(); ++it )
{
QColor result = parseColor( *it, true );
if ( result.isValid() )
{
colors << result;
}
}
if ( colors.length() > 0 )
{
return colors;
}

//try splitting string at whitespace or newlines
components = colorStr.simplified().split( QString( " " ) );
it = components.begin();
for ( ; it != components.end(); ++it )
@@ -2768,8 +2784,8 @@ QList<QColor> QgsSymbolLayerV2Utils::parseColorList( const QString colorStr )
return colors;
}

//try splitting string at commas
components = colorStr.split( QString( "," ) );
//try splitting string just at newlines
components = colorStr.split( QString( "\n" ) );
it = components.begin();
for ( ; it != components.end(); ++it )
{
@@ -2783,6 +2799,160 @@ QList<QColor> QgsSymbolLayerV2Utils::parseColorList( const QString colorStr )
return colors;
}

QgsNamedColorList QgsSymbolLayerV2Utils::colorListFromMimeData( const QMimeData *data )
{
QgsNamedColorList mimeColors;

//prefer xml format
if ( data->hasFormat( "text/xml" ) )
{
//get XML doc
QByteArray encodedData = data->data( "text/xml" );
QDomDocument xmlDoc;
xmlDoc.setContent( encodedData );

QDomElement dragDataElem = xmlDoc.documentElement();
if ( dragDataElem.tagName() == "ColorSchemeModelDragData" )
{
QDomNodeList nodeList = dragDataElem.childNodes();
int nChildNodes = nodeList.size();
QDomElement currentElem;

for ( int i = 0; i < nChildNodes; ++i )
{
currentElem = nodeList.at( i ).toElement();
if ( currentElem.isNull() )
{
continue;
}

QPair< QColor, QString> namedColor;
namedColor.first = QgsSymbolLayerV2Utils::decodeColor( currentElem.attribute( "color", "255,255,255,255" ) );
namedColor.second = currentElem.attribute( "label", "" );

mimeColors << namedColor;
}
}
}

if ( mimeColors.length() == 0 && data->hasFormat( "application/x-colorobject-list" ) )
{
//get XML doc
QByteArray encodedData = data->data( "application/x-colorobject-list" );
QDomDocument xmlDoc;
xmlDoc.setContent( encodedData );

QDomNodeList colorsNodes = xmlDoc.elementsByTagName( QString( "colors" ) );
if ( colorsNodes.length() > 0 )
{
QDomElement colorsElem = colorsNodes.at( 0 ).toElement();
QDomNodeList colorNodeList = colorsElem.childNodes();
int nChildNodes = colorNodeList.size();
QDomElement currentElem;

for ( int i = 0; i < nChildNodes; ++i )
{
//li element
currentElem = colorNodeList.at( i ).toElement();
if ( currentElem.isNull() )
{
continue;
}

QDomNodeList colorNodes = currentElem.elementsByTagName( QString( "color" ) );
QDomNodeList nameNodes = currentElem.elementsByTagName( QString( "name" ) );

if ( colorNodes.length() > 0 )
{
QDomElement colorElem = colorNodes.at( 0 ).toElement();

QStringList colorParts = colorElem.text().simplified().split( " " );
if ( colorParts.length() < 3 )
{
continue;
}

int red = colorParts.at( 0 ).toDouble() * 255;
int green = colorParts.at( 1 ).toDouble() * 255;
int blue = colorParts.at( 2 ).toDouble() * 255;
QPair< QColor, QString> namedColor;
namedColor.first = QColor( red, green, blue );
if ( nameNodes.length() > 0 )
{
QDomElement nameElem = nameNodes.at( 0 ).toElement();
namedColor.second = nameElem.text();
}
mimeColors << namedColor;
}
}
}
}

if ( mimeColors.length() == 0 && data->hasText() )
{
//attempt to read color data from mime text
QList< QColor > parsedColors = QgsSymbolLayerV2Utils::parseColorList( data->text() );
QList< QColor >::iterator it = parsedColors.begin();
for ( ; it != parsedColors.end(); ++it )
{
mimeColors << qMakePair( *it, QString() );
}
}

if ( mimeColors.length() == 0 && data->hasColor() )
{
//attempt to read color data directly from mime
QColor mimeColor = data->colorData().value<QColor>();
if ( mimeColor.isValid() )
{
mimeColors << qMakePair( mimeColor, QString() );
}
}

return mimeColors;
}

QMimeData* QgsSymbolLayerV2Utils::colorListToMimeData( const QgsNamedColorList colorList, const bool allFormats )
{
//native format
QMimeData* mimeData = new QMimeData();
QDomDocument xmlDoc;
QDomElement xmlRootElement = xmlDoc.createElement( "ColorSchemeModelDragData" );
xmlDoc.appendChild( xmlRootElement );

QgsNamedColorList::const_iterator colorIt = colorList.constBegin();
for ( ; colorIt != colorList.constEnd(); ++colorIt )
{
QDomElement namedColor = xmlDoc.createElement( "NamedColor" );
namedColor.setAttribute( "color", QgsSymbolLayerV2Utils::encodeColor(( *colorIt ).first ) );
namedColor.setAttribute( "label", ( *colorIt ).second );
xmlRootElement.appendChild( namedColor );
}
mimeData->setData( "text/xml", xmlDoc.toByteArray() );

if ( !allFormats )
{
return mimeData;
}

//set mime text to list of hex values
colorIt = colorList.constBegin();
QStringList colorListString;
for ( ; colorIt != colorList.constEnd(); ++colorIt )
{
colorListString << ( *colorIt ).first.name();
}
mimeData->setText( colorListString.join( "\n" ) );

//set mime color data to first color
if ( colorList.length() > 0 )
{
mimeData->setColorData( QVariant( colorList.at( 0 ).first ) );
}

return mimeData;
}

QColor QgsSymbolLayerV2Utils::parseColor( QString colorStr , bool strictEval )
{
bool hasAlpha;
@@ -33,6 +33,7 @@ class QgsVectorColorRampV2;

typedef QMap<QString, QString> QgsStringMap;
typedef QMap<QString, QgsSymbolV2* > QgsSymbolV2Map;
typedef QList< QPair< QColor, QString > > QgsNamedColorList;

class QDomDocument;
class QDomElement;
@@ -237,6 +238,23 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
*/
static QList< QColor > parseColorList( const QString colorStr );

/**
* Attempts to parse mime data as a list of named colors
* @param data mime data to parse
* @returns list of parsed colors
* @note added in 2.5
*/
static QgsNamedColorList colorListFromMimeData( const QMimeData *data );

/**
* Creates mime data from a list of named colors
* @param colorList list of named colors
* @param allFormats set to true to include additional mime formats, include text/plain and application/x-color
* @returns mime data containing encoded colors
* @note added in 2.5
*/
static QMimeData* colorListToMimeData( const QgsNamedColorList colorList, const bool allFormats = true );

/**
* Attempts to parse a string as a color using a variety of common formats, including hex
* codes, rgb and rgba strings.
@@ -322,6 +340,7 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
* @note added in 2.2
*/
static QString fieldOrExpressionFromExpression( QgsExpression* expression );

};

class QPolygonF;

0 comments on commit b457355

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