1 /****************************************************************************************
2 * Copyright (c) 2008 Téo Mrnjavac <teo@kde.org> *
3 * Copyright (c) 2009 Seb Ruiz <ruiz@kde.org> *
4 * Copyright (c) 2009 Thomas Luebking <thomas.luebking@web.de> *
5 * Copyright (c) 2009 Daniel Dewald <Daniel.Dewald@time-shift.de> *
6 * *
7 * This program is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License as published by the Free Software *
9 * Foundation; either version 2 of the License, or (at your option) any later *
10 * version. *
11 * *
12 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License along with *
17 * this program. If not, see <http://www.gnu.org/licenses/>. *
18 ****************************************************************************************/
19
20 #include "TokenPool.h"
21
22 #include <QApplication>
23
24 #include <QMouseEvent>
25 #include <QMimeData>
26 #include <QDrag>
27
28 #include "core/support/Debug.h"
29
TokenPool(QWidget * parent)30 TokenPool::TokenPool( QWidget *parent )
31 : QListWidget( parent )
32 {
33 setAcceptDrops( true );
34 setWrapping( true );
35 setResizeMode( QListView::Adjust );
36 }
37
38 void
addToken(Token * token)39 TokenPool::addToken( Token * token )
40 {
41 token->setParent( this );
42 token->setVisible( false );
43
44 QListWidgetItem *item = new QListWidgetItem( token->icon(), token->name() );
45 if( token->hasCustomColor() ) // don't override the default tooltip color if possible. This very easily produces black test on black tooltip background
46 {
47 item->setData( Qt::ForegroundRole, token->textColor() );
48 item->setToolTip( "<font color=\"" + token->textColor().name() + "\">" + token->name() + "</font>" );
49 }
50 else
51 {
52 item->setToolTip( token->name() );
53 }
54 addItem( item );
55
56 token->setParent( this );
57 token->hide();
58 m_itemTokenMap.insert( item, token );
59 }
60
61 QSize
sizeHint() const62 TokenPool::sizeHint() const
63 {
64 int h = iconSize().height();
65 if (h <= 0) {
66 h = style()->pixelMetric(QStyle::PM_SmallIconSize, 0, this);
67 }
68
69 // we are planning the size for three columns of token text
70 // with eight rows (note: we might get less than eight rows if because
71 // of the space the border and the scroll bar uses).
72 return QSize(fontMetrics().width(QLatin1String("Artist's Initial")) * 3,
73 h * 8 );
74 }
75
76 // Executed on doubleclick of the TokenPool, emits signal onDoubleClick( QString )
77 // that connects to TokenLayoutWidget::addToken( QString )
78 void
mouseDoubleClickEvent(QMouseEvent * event)79 TokenPool::mouseDoubleClickEvent( QMouseEvent *event )
80 {
81 QListWidgetItem *tokenItem = itemAt( event->pos() );
82 if( tokenItem )
83 Q_EMIT onDoubleClick( m_itemTokenMap.value( tokenItem ) );
84 }
85
86 //Executed on mouse press, handles start of drag.
87 void
mousePressEvent(QMouseEvent * event)88 TokenPool::mousePressEvent( QMouseEvent *event )
89 {
90 if( event->button() == Qt::LeftButton )
91 m_startPos = event->pos(); //store the start position
92 QListWidget::mousePressEvent( event ); //feed it to parent's event
93 }
94
95 //Executed on mouse move, handles start of drag.
96 void
mouseMoveEvent(QMouseEvent * event)97 TokenPool::mouseMoveEvent( QMouseEvent *event )
98 {
99 if( event->buttons() & Qt::LeftButton )
100 {
101 int distance = ( event->pos() - m_startPos ).manhattanLength();
102 if ( distance >= QApplication::startDragDistance() )
103 performDrag();
104 }
105 QListWidget::mouseMoveEvent( event );
106 }
107
108 void
dragEnterEvent(QDragEnterEvent * event)109 TokenPool::dragEnterEvent( QDragEnterEvent *event )
110 {
111 QObject *source = event->source();
112 if( source != this && event->mimeData()->hasFormat( Token::mimeType() ) )
113 {
114 event->setDropAction( Qt::MoveAction );
115 event->accept();
116 }
117 }
118
119 void
dropEvent(QDropEvent * event)120 TokenPool::dropEvent( QDropEvent *event )
121 {
122 Q_UNUSED( event )
123 //does nothing, I want the item to be deleted and not dragged here
124 }
125
126 //Handles the creation of a QDrag object that carries the (text-only) QDataStream from an item in TokenPool
127 void
performDrag()128 TokenPool::performDrag()
129 {
130 QListWidgetItem *item = currentItem();
131
132 if( item )
133 {
134 Token *token = m_itemTokenMap.value( item );
135
136 QDrag *drag = new QDrag( this );
137 drag->setMimeData( token->mimeData() );
138
139 // -- icon for pointer
140 // since the TokenPools tokens are invisible we need to resize them before drawing
141 // in the pixmap buffer
142 token->resize( token->sizeHint() );
143
144 // now draw in the pixmap buffer
145 QPixmap pixmap( token->size() );
146 token->render( &pixmap );
147 drag->setPixmap( pixmap );
148 drag->setHotSpot ( pixmap.rect().center() );
149
150 drag->exec( Qt::MoveAction | Qt::CopyAction, Qt::CopyAction );
151 }
152 }
153
154