1 /*
2  * StringPairDrag.cpp - class StringPairDrag which provides general support
3  *                        for drag'n'drop of string-pairs and which is the base
4  *                        for all drag'n'drop-actions within LMMS
5  *
6  * Copyright (c) 2005-2008 Tobias Doerffel <tobydox/at/users.sourceforge.net>
7  *
8  * This file is part of LMMS - https://lmms.io
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program (see COPYING); if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301 USA.
24  *
25  */
26 
27 
28 #include <QMimeData>
29 #include <QDragEnterEvent>
30 
31 
32 #include "StringPairDrag.h"
33 #include "GuiApplication.h"
34 #include "MainWindow.h"
35 
36 
StringPairDrag(const QString & _key,const QString & _value,const QPixmap & _icon,QWidget * _w)37 StringPairDrag::StringPairDrag( const QString & _key, const QString & _value,
38 					const QPixmap & _icon, QWidget * _w ) :
39 	QDrag( _w )
40 {
41 	if( _icon.isNull() && _w )
42 	{
43 		setPixmap( QPixmap::grabWidget( _w ).scaled(
44 						64, 64,
45 						Qt::KeepAspectRatio,
46 						Qt::SmoothTransformation ) );
47 	}
48 	else
49 	{
50 		setPixmap( _icon );
51 	}
52 	QString txt = _key + ":" + _value;
53 	QMimeData * m = new QMimeData();
54 	m->setData( mimeType(), txt.toUtf8() );
55 	setMimeData( m );
56 	start( Qt::IgnoreAction );
57 }
58 
59 
60 
61 
~StringPairDrag()62 StringPairDrag::~StringPairDrag()
63 {
64 	// during a drag, we might have lost key-press-events, so reset
65 	// modifiers of main-win
66 	if( gui->mainWindow() )
67 	{
68 		gui->mainWindow()->clearKeyModifiers();
69 	}
70 }
71 
72 
73 
74 
processDragEnterEvent(QDragEnterEvent * _dee,const QString & _allowed_keys)75 bool StringPairDrag::processDragEnterEvent( QDragEnterEvent * _dee,
76 						const QString & _allowed_keys )
77 {
78 	if( !_dee->mimeData()->hasFormat( mimeType() ) )
79 	{
80 		return( false );
81 	}
82 	QString txt = _dee->mimeData()->data( mimeType() );
83 	if( _allowed_keys.split( ',' ).contains( txt.section( ':', 0, 0 ) ) )
84 	{
85 		_dee->acceptProposedAction();
86 		return( true );
87 	}
88 	_dee->ignore();
89 	return( false );
90 }
91 
92 
93 
94 
decodeMimeKey(const QMimeData * mimeData)95 QString StringPairDrag::decodeMimeKey( const QMimeData * mimeData )
96 {
97 	return( QString::fromUtf8( mimeData->data( mimeType() ) ).section( ':', 0, 0 ) );
98 }
99 
100 
101 
102 
decodeMimeValue(const QMimeData * mimeData)103 QString StringPairDrag::decodeMimeValue( const QMimeData * mimeData )
104 {
105 	return( QString::fromUtf8( mimeData->data( mimeType() ) ).section( ':', 1, -1 ) );
106 }
107 
108 
109 
110 
decodeKey(QDropEvent * _de)111 QString StringPairDrag::decodeKey( QDropEvent * _de )
112 {
113 	return decodeMimeKey( _de->mimeData() );
114 }
115 
116 
117 
118 
decodeValue(QDropEvent * _de)119 QString StringPairDrag::decodeValue( QDropEvent * _de )
120 {
121 	return decodeMimeValue( _de->mimeData() );
122 }
123