Skip to content

Commit

Permalink
Accept dropped colors on color widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 11, 2014
1 parent 6e3c363 commit c4c58ab
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 19 deletions.
11 changes: 11 additions & 0 deletions python/core/symbology-ng/qgssymbollayerv2utils.sip
Expand Up @@ -200,6 +200,17 @@ class QgsSymbolLayerV2Utils
*/
static QList< QColor > parseColorList( const QString colorStr );

/**
* Attempts to parse mime data as a color
* @param data mime data to parse
* @param hasAlpha will be set to true if mime data was interpreted as a color containing
* an explicit alpha value
* @returns valid color if mimedata could be interpreted as a color, otherwise an
* invalid color
* @note added in 2.5
*/
static QColor colorFromMimeData( const QMimeData * mimeData, bool& hasAlpha );

/**
* Attempts to parse mime data as a list of named colors
* @param data mime data to parse
Expand Down
6 changes: 6 additions & 0 deletions python/gui/qgscolorwidgets.sip
Expand Up @@ -127,6 +127,12 @@ class QgsColorWidget : QWidget
*/
static const QPixmap& transparentBackground();

//Reimplemented to accept dragged colors
void dragEnterEvent( QDragEnterEvent * e ) ;

//Reimplemented to accept dropped colors
void dropEvent( QDropEvent *e );

};


Expand Down
22 changes: 22 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2utils.cpp
Expand Up @@ -2799,6 +2799,28 @@ QList<QColor> QgsSymbolLayerV2Utils::parseColorList( const QString colorStr )
return colors;
}

QColor QgsSymbolLayerV2Utils::colorFromMimeData( const QMimeData * mimeData, bool& hasAlpha )
{
//attempt to read color data directly from mime
QColor mimeColor = mimeData->colorData().value<QColor>();
if ( mimeColor.isValid() )
{
hasAlpha = true;
return mimeColor;
}

//attempt to intrepret a color from mime text data
hasAlpha = false;
QColor textColor = QgsSymbolLayerV2Utils::parseColorWithAlpha( mimeData->text(), hasAlpha );
if ( textColor.isValid() )
{
return textColor;
}

//could not get color from mime data
return QColor();
}

QgsNamedColorList QgsSymbolLayerV2Utils::colorListFromMimeData( const QMimeData *data )
{
QgsNamedColorList mimeColors;
Expand Down
11 changes: 11 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2utils.h
Expand Up @@ -238,6 +238,17 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
*/
static QList< QColor > parseColorList( const QString colorStr );

/**
* Attempts to parse mime data as a color
* @param data mime data to parse
* @param hasAlpha will be set to true if mime data was interpreted as a color containing
* an explicit alpha value
* @returns valid color if mimedata could be interpreted as a color, otherwise an
* invalid color
* @note added in 2.5
*/
static QColor colorFromMimeData( const QMimeData * mimeData, bool& hasAlpha );

/**
* Attempts to parse mime data as a list of named colors
* @param data mime data to parse
Expand Down
23 changes: 5 additions & 18 deletions src/gui/qgscolorbuttonv2.cpp
Expand Up @@ -155,35 +155,22 @@ QMimeData * QgsColorButtonV2::createColorMimeData() const

bool QgsColorButtonV2::colorFromMimeData( const QMimeData * mimeData, QColor& resultColor )
{
//attempt to read color data directly from mime
QColor mimeColor = mimeData->colorData().value<QColor>();
bool hasAlpha = false;
QColor mimeColor = QgsSymbolLayerV2Utils::colorFromMimeData( mimeData, hasAlpha );

if ( mimeColor.isValid() )
{
if ( !( mColorDialogOptions & QColorDialog::ShowAlphaChannel ) )
{
//remove alpha channel
mimeColor.setAlpha( 255 );
}
resultColor = mimeColor;
return true;
}

//attempt to intrepret a color from mime text data
bool hasAlpha = false;
QColor textColor = QgsSymbolLayerV2Utils::parseColorWithAlpha( mimeData->text(), hasAlpha );
if ( textColor.isValid() )
{
if ( !( mColorDialogOptions & QColorDialog::ShowAlphaChannel ) )
{
//remove alpha channel
textColor.setAlpha( 255 );
}
else if ( !hasAlpha )
{
//mime color has no explicit alpha component, so keep existing alpha
textColor.setAlpha( mColor.alpha() );
mimeColor.setAlpha( mColor.alpha() );
}
resultColor = textColor;
resultColor = mimeColor;
return true;
}

Expand Down
39 changes: 38 additions & 1 deletion src/gui/qgscolorwidgets.cpp
Expand Up @@ -38,7 +38,7 @@ QgsColorWidget::QgsColorWidget( QWidget* parent, const ColorComponent component
, mCurrentColor( Qt::red )
, mComponent( component )
{

setAcceptDrops( true );
}

QgsColorWidget::~QgsColorWidget()
Expand Down Expand Up @@ -163,6 +163,43 @@ const QPixmap &QgsColorWidget::transparentBackground()
return transpBkgrd;
}

void QgsColorWidget::dragEnterEvent( QDragEnterEvent *e )
{
//is dragged data valid color data?
bool hasAlpha;
QColor mimeColor = QgsSymbolLayerV2Utils::colorFromMimeData( e->mimeData(), hasAlpha );

if ( mimeColor.isValid() )
{
//if so, we accept the drag
e->acceptProposedAction();
}
}

void QgsColorWidget::dropEvent( QDropEvent *e )
{
//is dropped data valid color data?
bool hasAlpha = false;
QColor mimeColor = QgsSymbolLayerV2Utils::colorFromMimeData( e->mimeData(), hasAlpha );

if ( mimeColor.isValid() )
{
//accept drop and set new color
e->acceptProposedAction();

if ( !hasAlpha )
{
//mime color has no explicit alpha component, so keep existing alpha
mimeColor.setAlpha( mCurrentColor.alpha() );
}

setColor( mimeColor );
emit colorChanged( mCurrentColor );
}

//could not get color from mime data
}

QColor QgsColorWidget::color() const
{
return mCurrentColor;
Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgscolorwidgets.h
Expand Up @@ -158,6 +158,12 @@ class GUI_EXPORT QgsColorWidget : public QWidget
* @returns checkerboard pixmap
*/
static const QPixmap& transparentBackground();

//Reimplemented to accept dragged colors
void dragEnterEvent( QDragEnterEvent * e ) ;

//Reimplemented to accept dropped colors
void dropEvent( QDropEvent *e );
};


Expand Down

0 comments on commit c4c58ab

Please sign in to comment.