Skip to content
Permalink
Browse files

Accept dropped colors on color widgets

  • Loading branch information
nyalldawson committed Sep 11, 2014
1 parent 6e3c363 commit c4c58ab3423ae8ad3f108c266a8da883e14ef5c2
@@ -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
@@ -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 );

};


@@ -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;
@@ -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
@@ -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;
}

@@ -38,7 +38,7 @@ QgsColorWidget::QgsColorWidget( QWidget* parent, const ColorComponent component
, mCurrentColor( Qt::red )
, mComponent( component )
{

setAcceptDrops( true );
}

QgsColorWidget::~QgsColorWidget()
@@ -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;
@@ -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 );
};


0 comments on commit c4c58ab

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