Skip to content

Commit c4c58ab

Browse files
committed
Accept dropped colors on color widgets
1 parent 6e3c363 commit c4c58ab

File tree

7 files changed

+99
-19
lines changed

7 files changed

+99
-19
lines changed

python/core/symbology-ng/qgssymbollayerv2utils.sip

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ class QgsSymbolLayerV2Utils
200200
*/
201201
static QList< QColor > parseColorList( const QString colorStr );
202202

203+
/**
204+
* Attempts to parse mime data as a color
205+
* @param data mime data to parse
206+
* @param hasAlpha will be set to true if mime data was interpreted as a color containing
207+
* an explicit alpha value
208+
* @returns valid color if mimedata could be interpreted as a color, otherwise an
209+
* invalid color
210+
* @note added in 2.5
211+
*/
212+
static QColor colorFromMimeData( const QMimeData * mimeData, bool& hasAlpha );
213+
203214
/**
204215
* Attempts to parse mime data as a list of named colors
205216
* @param data mime data to parse

python/gui/qgscolorwidgets.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ class QgsColorWidget : QWidget
127127
*/
128128
static const QPixmap& transparentBackground();
129129

130+
//Reimplemented to accept dragged colors
131+
void dragEnterEvent( QDragEnterEvent * e ) ;
132+
133+
//Reimplemented to accept dropped colors
134+
void dropEvent( QDropEvent *e );
135+
130136
};
131137

132138

src/core/symbology-ng/qgssymbollayerv2utils.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2799,6 +2799,28 @@ QList<QColor> QgsSymbolLayerV2Utils::parseColorList( const QString colorStr )
27992799
return colors;
28002800
}
28012801

2802+
QColor QgsSymbolLayerV2Utils::colorFromMimeData( const QMimeData * mimeData, bool& hasAlpha )
2803+
{
2804+
//attempt to read color data directly from mime
2805+
QColor mimeColor = mimeData->colorData().value<QColor>();
2806+
if ( mimeColor.isValid() )
2807+
{
2808+
hasAlpha = true;
2809+
return mimeColor;
2810+
}
2811+
2812+
//attempt to intrepret a color from mime text data
2813+
hasAlpha = false;
2814+
QColor textColor = QgsSymbolLayerV2Utils::parseColorWithAlpha( mimeData->text(), hasAlpha );
2815+
if ( textColor.isValid() )
2816+
{
2817+
return textColor;
2818+
}
2819+
2820+
//could not get color from mime data
2821+
return QColor();
2822+
}
2823+
28022824
QgsNamedColorList QgsSymbolLayerV2Utils::colorListFromMimeData( const QMimeData *data )
28032825
{
28042826
QgsNamedColorList mimeColors;

src/core/symbology-ng/qgssymbollayerv2utils.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,17 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
238238
*/
239239
static QList< QColor > parseColorList( const QString colorStr );
240240

241+
/**
242+
* Attempts to parse mime data as a color
243+
* @param data mime data to parse
244+
* @param hasAlpha will be set to true if mime data was interpreted as a color containing
245+
* an explicit alpha value
246+
* @returns valid color if mimedata could be interpreted as a color, otherwise an
247+
* invalid color
248+
* @note added in 2.5
249+
*/
250+
static QColor colorFromMimeData( const QMimeData * mimeData, bool& hasAlpha );
251+
241252
/**
242253
* Attempts to parse mime data as a list of named colors
243254
* @param data mime data to parse

src/gui/qgscolorbuttonv2.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,35 +155,22 @@ QMimeData * QgsColorButtonV2::createColorMimeData() const
155155

156156
bool QgsColorButtonV2::colorFromMimeData( const QMimeData * mimeData, QColor& resultColor )
157157
{
158-
//attempt to read color data directly from mime
159-
QColor mimeColor = mimeData->colorData().value<QColor>();
158+
bool hasAlpha = false;
159+
QColor mimeColor = QgsSymbolLayerV2Utils::colorFromMimeData( mimeData, hasAlpha );
160+
160161
if ( mimeColor.isValid() )
161162
{
162163
if ( !( mColorDialogOptions & QColorDialog::ShowAlphaChannel ) )
163164
{
164165
//remove alpha channel
165166
mimeColor.setAlpha( 255 );
166167
}
167-
resultColor = mimeColor;
168-
return true;
169-
}
170-
171-
//attempt to intrepret a color from mime text data
172-
bool hasAlpha = false;
173-
QColor textColor = QgsSymbolLayerV2Utils::parseColorWithAlpha( mimeData->text(), hasAlpha );
174-
if ( textColor.isValid() )
175-
{
176-
if ( !( mColorDialogOptions & QColorDialog::ShowAlphaChannel ) )
177-
{
178-
//remove alpha channel
179-
textColor.setAlpha( 255 );
180-
}
181168
else if ( !hasAlpha )
182169
{
183170
//mime color has no explicit alpha component, so keep existing alpha
184-
textColor.setAlpha( mColor.alpha() );
171+
mimeColor.setAlpha( mColor.alpha() );
185172
}
186-
resultColor = textColor;
173+
resultColor = mimeColor;
187174
return true;
188175
}
189176

src/gui/qgscolorwidgets.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ QgsColorWidget::QgsColorWidget( QWidget* parent, const ColorComponent component
3838
, mCurrentColor( Qt::red )
3939
, mComponent( component )
4040
{
41-
41+
setAcceptDrops( true );
4242
}
4343

4444
QgsColorWidget::~QgsColorWidget()
@@ -163,6 +163,43 @@ const QPixmap &QgsColorWidget::transparentBackground()
163163
return transpBkgrd;
164164
}
165165

166+
void QgsColorWidget::dragEnterEvent( QDragEnterEvent *e )
167+
{
168+
//is dragged data valid color data?
169+
bool hasAlpha;
170+
QColor mimeColor = QgsSymbolLayerV2Utils::colorFromMimeData( e->mimeData(), hasAlpha );
171+
172+
if ( mimeColor.isValid() )
173+
{
174+
//if so, we accept the drag
175+
e->acceptProposedAction();
176+
}
177+
}
178+
179+
void QgsColorWidget::dropEvent( QDropEvent *e )
180+
{
181+
//is dropped data valid color data?
182+
bool hasAlpha = false;
183+
QColor mimeColor = QgsSymbolLayerV2Utils::colorFromMimeData( e->mimeData(), hasAlpha );
184+
185+
if ( mimeColor.isValid() )
186+
{
187+
//accept drop and set new color
188+
e->acceptProposedAction();
189+
190+
if ( !hasAlpha )
191+
{
192+
//mime color has no explicit alpha component, so keep existing alpha
193+
mimeColor.setAlpha( mCurrentColor.alpha() );
194+
}
195+
196+
setColor( mimeColor );
197+
emit colorChanged( mCurrentColor );
198+
}
199+
200+
//could not get color from mime data
201+
}
202+
166203
QColor QgsColorWidget::color() const
167204
{
168205
return mCurrentColor;

src/gui/qgscolorwidgets.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ class GUI_EXPORT QgsColorWidget : public QWidget
158158
* @returns checkerboard pixmap
159159
*/
160160
static const QPixmap& transparentBackground();
161+
162+
//Reimplemented to accept dragged colors
163+
void dragEnterEvent( QDragEnterEvent * e ) ;
164+
165+
//Reimplemented to accept dropped colors
166+
void dropEvent( QDropEvent *e );
161167
};
162168

163169

0 commit comments

Comments
 (0)