Skip to content
Permalink
Browse files

Support dragging colors from QgsColorPreviewWidget

  • Loading branch information
nyalldawson committed Sep 11, 2014
1 parent c4c58ab commit e9d7b520db44b43c2bb0a80e81dc1d594407ee62
@@ -200,6 +200,15 @@ class QgsSymbolLayerV2Utils
*/
static QList< QColor > parseColorList( const QString colorStr );

/**
* Creates mime data from a color. Sets both the mime data's color data, and the
* mime data's text with the color's hex code.
* @param color color to encode as mime data
* @see colorFromMimeData
* @note added in 2.5
*/
static QMimeData * colorToMimeData( const QColor color );

/**
* Attempts to parse mime data as a color
* @param data mime data to parse
@@ -56,6 +56,11 @@ class QgsColorWidget : QWidget
* @see component
*/
int componentValue() const;

/**Create an icon for dragging colors
* @param color for icon
*/
static QPixmap createDragIcon( const QColor color );

public slots:

@@ -409,5 +414,13 @@ class QgsColorPreviewWidget : QgsColorWidget
* @see color2
*/
virtual void setColor2( const QColor& color );

protected:

//reimplemented to allow dragging colors
void mousePressEvent( QMouseEvent* e );

//reimplemented to allow dragging colors
void mouseMoveEvent( QMouseEvent *e );

};
@@ -2799,6 +2799,16 @@ QList<QColor> QgsSymbolLayerV2Utils::parseColorList( const QString colorStr )
return colors;
}

QMimeData * QgsSymbolLayerV2Utils::colorToMimeData( const QColor color )
{
//set both the mime color data (which includes alpha channel), and the text (which is the color's hex
//value, and can be used when pasting colors outside of QGIS).
QMimeData *mimeData = new QMimeData;
mimeData->setColorData( QVariant( color ) );
mimeData->setText( color.name() );
return mimeData;
}

QColor QgsSymbolLayerV2Utils::colorFromMimeData( const QMimeData * mimeData, bool& hasAlpha )
{
//attempt to read color data directly from mime
@@ -238,6 +238,15 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
*/
static QList< QColor > parseColorList( const QString colorStr );

/**
* Creates mime data from a color. Sets both the mime data's color data, and the
* mime data's text with the color's hex code.
* @param color color to encode as mime data
* @see colorFromMimeData
* @note added in 2.5
*/
static QMimeData * colorToMimeData( const QColor color );

/**
* Attempts to parse mime data as a color
* @param data mime data to parse
@@ -21,6 +21,7 @@
#include "qgscursors.h"
#include "qgscolorswatchgrid.h"
#include "qgscolorschemeregistry.h"
#include "qgscolorwidgets.h"

#include <QPainter>
#include <QSettings>
@@ -143,16 +144,6 @@ void QgsColorButtonV2::mousePressEvent( QMouseEvent *e )
QToolButton::mousePressEvent( e );
}

QMimeData * QgsColorButtonV2::createColorMimeData() const
{
//set both the mime color data (which includes alpha channel), and the text (which is the color's hex
//value, and can be used when pasting colors outside of QGIS).
QMimeData *mimeData = new QMimeData;
mimeData->setColorData( QVariant( mColor ) );
mimeData->setText( mColor.name() );
return mimeData;
}

bool QgsColorButtonV2::colorFromMimeData( const QMimeData * mimeData, QColor& resultColor )
{
bool hasAlpha = false;
@@ -214,31 +205,12 @@ void QgsColorButtonV2::mouseMoveEvent( QMouseEvent *e )

//user is dragging color
QDrag *drag = new QDrag( this );
drag->setMimeData( createColorMimeData() );
drag->setPixmap( createDragIcon( mColor ) );
drag->setMimeData( QgsSymbolLayerV2Utils::colorToMimeData( mColor ) );
drag->setPixmap( QgsColorWidget::createDragIcon( mColor ) );
drag->exec( Qt::CopyAction );
setDown( false );
}

QPixmap QgsColorButtonV2::createDragIcon( const QColor color )
{
//craft a pixmap for the drag icon
QPixmap pixmap( 50, 50 );
pixmap.fill( Qt::transparent );
QPainter painter;
painter.begin( &pixmap );
//start with a light gray background
painter.fillRect( QRect( 0, 0, 50, 50 ), QBrush( QColor( 200, 200, 200 ) ) );
//draw rect with white border, filled with current color
QColor pixmapColor = color;
pixmapColor.setAlpha( 255 );
painter.setBrush( QBrush( pixmapColor ) );
painter.setPen( QPen( Qt::white ) );
painter.drawRect( QRect( 1, 1, 47, 47 ) );
painter.end();
return pixmap;
}

void QgsColorButtonV2::mouseReleaseEvent( QMouseEvent *e )
{
if ( mPickingColor )
@@ -557,7 +529,7 @@ void QgsColorButtonV2::setButtonBackground( const QColor color )
void QgsColorButtonV2::copyColor()
{
//copy color
QApplication::clipboard()->setMimeData( createColorMimeData() );
QApplication::clipboard()->setMimeData( QgsSymbolLayerV2Utils::colorToMimeData( mColor ) );
}

void QgsColorButtonV2::pasteColor()
@@ -304,12 +304,6 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton

QSize mIconSize;

/**Creates mime data from the current color. Sets both the mime data's color data, and the
* mime data's text with the color's hex code.
* @see colorFromMimeData
*/
QMimeData* createColorMimeData() const;

/**Attempts to parse mimeData as a color, either via the mime data's color data or by
* parsing a textual representation of a color.
* @returns true if mime data could be intrepreted as a color
@@ -331,11 +325,6 @@ class GUI_EXPORT QgsColorButtonV2: public QToolButton
*/
void addRecentColor( const QColor color );

/**Create an icon for dragging colors
* @param color for icon
*/
QPixmap createDragIcon( const QColor color );

/**Create a color icon for display in the drop down menu
* @param color for icon
* @param showChecks set to true to display a checkboard pattern behind
@@ -51,6 +51,25 @@ int QgsColorWidget::componentValue() const
return componentValue( mComponent );
}

QPixmap QgsColorWidget::createDragIcon( const QColor color )
{
//craft a pixmap for the drag icon
QPixmap pixmap( 50, 50 );
pixmap.fill( Qt::transparent );
QPainter painter;
painter.begin( &pixmap );
//start with a light gray background
painter.fillRect( QRect( 0, 0, 50, 50 ), QBrush( QColor( 200, 200, 200 ) ) );
//draw rect with white border, filled with current color
QColor pixmapColor = color;
pixmapColor.setAlpha( 255 );
painter.setBrush( QBrush( pixmapColor ) );
painter.setPen( QPen( Qt::white ) );
painter.drawRect( QRect( 1, 1, 47, 47 ) );
painter.end();
return pixmap;
}

int QgsColorWidget::componentValue( const QgsColorWidget::ColorComponent component ) const
{
if ( !mCurrentColor.isValid() )
@@ -1554,3 +1573,50 @@ void QgsColorPreviewWidget::setColor2( const QColor &color )
mColor2 = color;
update();
}

void QgsColorPreviewWidget::mousePressEvent( QMouseEvent *e )
{
if ( e->button() == Qt::LeftButton )
{
mDragStartPosition = e->pos();
}
QWidget::mousePressEvent( e );
}

void QgsColorPreviewWidget::mouseMoveEvent( QMouseEvent *e )
{
//handle dragging colors from button

if ( !( e->buttons() & Qt::LeftButton ) )
{
//left button not depressed, so not a drag
QWidget::mouseMoveEvent( e );
return;
}

if (( e->pos() - mDragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
{
//mouse not moved, so not a drag
QWidget::mouseMoveEvent( e );
return;
}

//user is dragging color

//work out which color is being dragged
QColor dragColor = mCurrentColor;
if ( mColor2.isValid() )
{
//two color sections, check if dragged color was the second color
int verticalSplit = qRound( height() / 2.0 );
if ( mDragStartPosition.y() >= verticalSplit )
{
dragColor = mColor2;
}
}

QDrag *drag = new QDrag( this );
drag->setMimeData( QgsSymbolLayerV2Utils::colorToMimeData( dragColor ) );
drag->setPixmap( createDragIcon( dragColor ) );
drag->exec( Qt::CopyAction );
}
@@ -80,6 +80,11 @@ class GUI_EXPORT QgsColorWidget : public QWidget
*/
int componentValue() const;

/**Create an icon for dragging colors
* @param color for icon
*/
static QPixmap createDragIcon( const QColor color );

public slots:

/**Sets the color for the widget
@@ -621,11 +626,21 @@ class GUI_EXPORT QgsColorPreviewWidget : public QgsColorWidget
*/
virtual void setColor2( const QColor& color );

protected:

//reimplemented to allow dragging colors
void mousePressEvent( QMouseEvent* e );

//reimplemented to allow dragging colors
void mouseMoveEvent( QMouseEvent *e );

private:

/*Secondary color for widget*/
QColor mColor2;

QPoint mDragStartPosition;

/*Draws a color preview within the specified rect.
* @param color color to draw
* @param rect rect to draw color in

0 comments on commit e9d7b52

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