Skip to content

Commit

Permalink
Support dragging colors from QgsColorPreviewWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 11, 2014
1 parent c4c58ab commit e9d7b52
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 43 deletions.
9 changes: 9 additions & 0 deletions python/core/symbology-ng/qgssymbollayerv2utils.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions python/gui/qgscolorwidgets.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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 );

};
10 changes: 10 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 4 additions & 32 deletions src/gui/qgscolorbuttonv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "qgscursors.h"
#include "qgscolorswatchgrid.h"
#include "qgscolorschemeregistry.h"
#include "qgscolorwidgets.h"

#include <QPainter>
#include <QSettings>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 0 additions & 11 deletions src/gui/qgscolorbuttonv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
66 changes: 66 additions & 0 deletions src/gui/qgscolorwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
Expand Down Expand Up @@ -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 );
}
15 changes: 15 additions & 0 deletions src/gui/qgscolorwidgets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e9d7b52

Please sign in to comment.