@@ -51,6 +51,7 @@ QgsColorButton::QgsColorButton( QWidget *parent, QString cdt, QColorDialog::Colo
51
51
, mTempPNG( NULL )
52
52
, mColorSet( false )
53
53
{
54
+ setAcceptDrops ( true );
54
55
connect ( this , SIGNAL ( clicked () ), this , SLOT ( onButtonClicked () ) );
55
56
}
56
57
@@ -96,78 +97,149 @@ void QgsColorButton::mousePressEvent( QMouseEvent *e )
96
97
if ( e->button () == Qt::RightButton )
97
98
{
98
99
showContextMenu ( e );
100
+ return ;
99
101
}
100
- else
102
+ else if ( e-> button () == Qt::LeftButton )
101
103
{
102
- QPushButton::mousePressEvent ( e );
104
+ mDragStartPosition = e-> pos ( );
103
105
}
106
+ QPushButton::mousePressEvent ( e );
104
107
}
105
108
106
- void QgsColorButton::showContextMenu ( QMouseEvent *event )
109
+ QMimeData * QgsColorButton::createColorMimeData () const
107
110
{
108
- QMenu colorContextMenu;
111
+ QMimeData *mimeData = new QMimeData;
112
+ mimeData->setColorData ( QVariant ( mColor ) );
113
+ mimeData->setText ( mColor .name () );
114
+ return mimeData;
115
+ }
109
116
110
- QAction* copyAsHexAction = new QAction ( tr ( " Copy color" ), 0 );
111
- colorContextMenu.addAction ( copyAsHexAction );
112
- QAction* copyAsRgbAction = new QAction ( tr ( " Copy as rgb" ), 0 );
113
- colorContextMenu.addAction ( copyAsRgbAction );
114
- QAction* copyAsRgbaAction = new QAction ( tr ( " Copy as rgba" ), 0 );
115
- if ( mColorDialogOptions & QColorDialog::ShowAlphaChannel )
117
+ bool QgsColorButton::colorFromMimeData ( const QMimeData * mimeData, QColor& resultColor )
118
+ {
119
+ // attempt to read color data directly from mime
120
+ QColor mimeColor = qVariantValue<QColor>( mimeData->colorData () );
121
+ if ( mimeColor.isValid () )
116
122
{
117
- // alpha enabled, so add rgba action
118
- colorContextMenu.addAction ( copyAsRgbaAction );
123
+ if ( !( mColorDialogOptions & QColorDialog::ShowAlphaChannel ) )
124
+ {
125
+ // remove alpha channel
126
+ mimeColor.setAlpha ( 255 );
127
+ }
128
+ resultColor = mimeColor;
129
+ return true ;
119
130
}
120
131
121
- QString clipboardText = QApplication::clipboard ()->text ();
122
- QAction* pasteColorAction = new QAction ( tr ( " Paste color" ), 0 );
123
- pasteColorAction->setEnabled ( false );
124
- colorContextMenu.addSeparator ();
125
- colorContextMenu.addAction ( pasteColorAction );
126
- QColor clipColor;
127
- if ( !( clipboardText.isEmpty () ) )
132
+ // attempt to intrepret a color from mime text data
133
+ bool hasAlpha = false ;
134
+ QColor textColor = QgsSymbolLayerV2Utils::parseColorWithAlpha ( mimeData->text (), hasAlpha );
135
+ if ( textColor.isValid () )
128
136
{
129
- bool hasAlpha = false ;
130
- clipColor = QgsSymbolLayerV2Utils::parseColorWithAlpha ( clipboardText, hasAlpha );
131
-
132
- if ( clipColor.isValid () )
137
+ if ( !( mColorDialogOptions & QColorDialog::ShowAlphaChannel ) )
133
138
{
134
- if ( !hasAlpha )
135
- {
136
- // clipboard color has no explicit alpha component, so keep existing alpha
137
- clipColor.setAlpha ( mColor .alpha () );
138
- }
139
- pasteColorAction->setEnabled ( true );
139
+ // remove alpha channel
140
+ textColor.setAlpha ( 255 );
140
141
}
142
+ else if ( !hasAlpha )
143
+ {
144
+ // mime color has no explicit alpha component, so keep existing alpha
145
+ textColor.setAlpha ( mColor .alpha () );
146
+ }
147
+ resultColor = textColor;
148
+ return true ;
141
149
}
142
150
143
- QAction* selectedAction = colorContextMenu.exec ( event->globalPos ( ) );
144
- if ( selectedAction == copyAsHexAction )
151
+ // could not get color from mime data
152
+ return false ;
153
+ }
154
+
155
+ void QgsColorButton::mouseMoveEvent ( QMouseEvent *e )
156
+ {
157
+ if ( !( e->buttons () & Qt::LeftButton ) )
158
+ {
159
+ QPushButton::mouseMoveEvent ( e );
160
+ return ;
161
+ }
162
+
163
+ if (( e->pos () - mDragStartPosition ).manhattanLength () < QApplication::startDragDistance () )
145
164
{
146
- // copy color as hex code
147
- QString colorString = mColor .name ();
148
- QApplication::clipboard ()->setText ( colorString );
165
+ QPushButton::mouseMoveEvent ( e );
166
+ return ;
167
+ }
168
+
169
+ QDrag *drag = new QDrag ( this );
170
+ drag->setMimeData ( createColorMimeData () );
171
+
172
+ // craft a pixmap for the drag icon
173
+ QImage colorImage ( 50 , 50 , QImage::Format_RGB32 );
174
+ QPainter imagePainter;
175
+ imagePainter.begin ( &colorImage );
176
+ // start with a light gray background
177
+ imagePainter.fillRect ( QRect ( 0 , 0 , 50 , 50 ), QBrush ( QColor ( 200 , 200 , 200 ) ) );
178
+ // draw rect with white border, filled with current color
179
+ QColor pixmapColor = mColor ;
180
+ pixmapColor.setAlpha ( 255 );
181
+ imagePainter.setBrush ( QBrush ( pixmapColor ) );
182
+ imagePainter.setPen ( QPen ( Qt::white ) );
183
+ imagePainter.drawRect ( QRect ( 1 , 1 , 47 , 47 ) );
184
+ imagePainter.end ();
185
+ // set as drag pixmap
186
+ drag->setPixmap ( QPixmap::fromImage ( colorImage ) );
187
+
188
+ Qt::DropAction dropAction = drag->exec ( Qt::CopyAction | Qt::MoveAction );
189
+ setDown ( false );
190
+ }
191
+
192
+ void QgsColorButton::dragEnterEvent ( QDragEnterEvent *e )
193
+ {
194
+ // is dragged data valid color data?
195
+ QColor mimeColor;
196
+ if ( colorFromMimeData ( e->mimeData (), mimeColor ) )
197
+ {
198
+ e->acceptProposedAction ();
149
199
}
150
- else if ( selectedAction == copyAsRgbAction )
200
+ }
201
+
202
+ void QgsColorButton::dropEvent ( QDropEvent *e )
203
+ {
204
+ // is dropped data valid color data?
205
+ QColor mimeColor;
206
+ if ( colorFromMimeData ( e->mimeData (), mimeColor ) )
207
+ {
208
+ e->acceptProposedAction ();
209
+ setColor ( mimeColor );
210
+ }
211
+ }
212
+
213
+ void QgsColorButton::showContextMenu ( QMouseEvent *event )
214
+ {
215
+ QMenu colorContextMenu;
216
+
217
+ QAction* copyColorAction = new QAction ( tr ( " Copy color" ), 0 );
218
+ colorContextMenu.addAction ( copyColorAction );
219
+ QAction* pasteColorAction = new QAction ( tr ( " Paste color" ), 0 );
220
+ pasteColorAction->setEnabled ( false );
221
+ colorContextMenu.addSeparator ();
222
+ colorContextMenu.addAction ( pasteColorAction );
223
+
224
+ QColor clipColor;
225
+ if ( colorFromMimeData ( QApplication::clipboard ()->mimeData (), clipColor ) )
151
226
{
152
- // copy color as rgb
153
- QString colorString = QString ( " rgb(%1,%2,%3)" ).arg ( mColor .red () ). arg ( mColor .green () ).arg ( mColor .blue () );
154
- QApplication::clipboard ()->setText ( colorString );
227
+ pasteColorAction->setEnabled ( true );
155
228
}
156
- else if ( selectedAction == copyAsRgbaAction )
229
+
230
+ QAction* selectedAction = colorContextMenu.exec ( event->globalPos ( ) );
231
+ if ( selectedAction == copyColorAction )
157
232
{
158
- // copy color as rgba
159
- QString colorString = QString ( " rgba(%1,%2,%3,%4)" ).arg ( mColor .red () ).arg ( mColor .green () ).arg ( mColor .blue () ).arg ( QString::number ( mColor .alphaF (), ' f' , 2 ) );
160
- QApplication::clipboard ()->setText ( colorString );
233
+ // copy color
234
+ QApplication::clipboard ()->setMimeData ( createColorMimeData () );
161
235
}
162
236
else if ( selectedAction == pasteColorAction )
163
237
{
164
238
// paste color
165
239
setColor ( clipColor );
166
240
}
167
241
168
- delete copyAsHexAction;
169
- delete copyAsRgbAction;
170
- delete copyAsRgbaAction;
242
+ delete copyColorAction;
171
243
delete pasteColorAction;
172
244
}
173
245
0 commit comments