File indexing completed on 2024-05-05 04:49:29

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Téo Mrnjavac <teo@kde.org>                                        *
0003  * Copyright (c) 2009 Seb Ruiz <ruiz@kde.org>                                           *
0004  * Copyright (c) 2009 Thomas Luebking <thomas.luebking@web.de>                          *
0005  * Copyright (c) 2009 Daniel Dewald <Daniel.Dewald@time-shift.de>                       *
0006  *                                                                                      *
0007  * This program is free software; you can redistribute it and/or modify it under        *
0008  * the terms of the GNU General Public License as published by the Free Software        *
0009  * Foundation; either version 2 of the License, or (at your option) any later           *
0010  * version.                                                                             *
0011  *                                                                                      *
0012  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0014  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0015  *                                                                                      *
0016  * You should have received a copy of the GNU General Public License along with         *
0017  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0018  ****************************************************************************************/
0019 
0020 #include "TokenPool.h"
0021 
0022 #include <QApplication>
0023 
0024 #include <QMouseEvent>
0025 #include <QMimeData>
0026 #include <QDrag>
0027 
0028 #include "core/support/Debug.h"
0029 
0030 TokenPool::TokenPool( QWidget *parent )
0031     : QListWidget( parent )
0032 {
0033     setAcceptDrops( true );
0034     setWrapping( true );
0035     setResizeMode( QListView::Adjust );
0036 }
0037 
0038 void
0039 TokenPool::addToken( Token * token )
0040 {
0041     token->setParent( this );
0042     token->setVisible( false );
0043 
0044     QListWidgetItem *item = new QListWidgetItem( token->icon(), token->name() );
0045     if( token->hasCustomColor() ) // don't override the default tooltip color if possible. This very easily produces black test on black tooltip background
0046     {
0047         item->setData( Qt::ForegroundRole, token->textColor() );
0048         item->setToolTip( "<font color=\"" + token->textColor().name() + "\">" + token->name() + "</font>" );
0049     }
0050     else
0051     {
0052         item->setToolTip( token->name() );
0053     }
0054     addItem( item );
0055 
0056     token->setParent( this );
0057     token->hide();
0058     m_itemTokenMap.insert( item, token );
0059 }
0060 
0061 QSize
0062 TokenPool::sizeHint() const
0063 {
0064     int h = iconSize().height();
0065     if (h <= 0) {
0066         h = style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this);
0067     }
0068 
0069     // we are planning the size for three columns of token text
0070     // with eight rows (note: we might get less than eight rows if because
0071     // of the space the border and the scroll bar uses).
0072     return QSize(fontMetrics().horizontalAdvance(QLatin1String("Artist's Initial")) * 3,
0073                  h * 8 );
0074 }
0075 
0076 // Executed on doubleclick of the TokenPool, emits signal onDoubleClick( QString )
0077 // that connects to TokenLayoutWidget::addToken( QString )
0078 void
0079 TokenPool::mouseDoubleClickEvent( QMouseEvent *event )
0080 {
0081     QListWidgetItem *tokenItem = itemAt( event->pos() );
0082     if( tokenItem )
0083         Q_EMIT onDoubleClick( m_itemTokenMap.value( tokenItem ) );
0084 }
0085 
0086 //Executed on mouse press, handles start of drag.
0087 void
0088 TokenPool::mousePressEvent( QMouseEvent *event )
0089 {
0090     if( event->button() == Qt::LeftButton )
0091         m_startPos = event->pos();            //store the start position
0092     QListWidget::mousePressEvent( event );    //feed it to parent's event
0093 }
0094 
0095 //Executed on mouse move, handles start of drag.
0096 void
0097 TokenPool::mouseMoveEvent( QMouseEvent *event )
0098 {
0099     if( event->buttons() & Qt::LeftButton )
0100     {
0101         int distance = ( event->pos() - m_startPos ).manhattanLength();
0102         if ( distance >= QApplication::startDragDistance() )
0103             performDrag();
0104     }
0105     QListWidget::mouseMoveEvent( event );
0106 }
0107 
0108 void
0109 TokenPool::dragEnterEvent( QDragEnterEvent *event )
0110 {
0111     QObject *source = event->source();
0112     if( source != this && event->mimeData()->hasFormat( Token::mimeType() ) )
0113     {
0114         event->setDropAction( Qt::MoveAction );
0115         event->accept();
0116     }
0117 }
0118 
0119 void
0120 TokenPool::dropEvent( QDropEvent *event )
0121 {
0122     Q_UNUSED( event )
0123     //does nothing, I want the item to be deleted and not dragged here
0124 }
0125 
0126 //Handles the creation of a QDrag object that carries the (text-only) QDataStream from an item in TokenPool
0127 void
0128 TokenPool::performDrag()
0129 {
0130     QListWidgetItem *item = currentItem();
0131 
0132     if( item )
0133     {
0134         Token *token = m_itemTokenMap.value( item );
0135 
0136         QDrag *drag = new QDrag( this );
0137         drag->setMimeData( token->mimeData() );
0138 
0139         // -- icon for pointer
0140         // since the TokenPools tokens are invisible we need to resize them before drawing
0141         // in the pixmap buffer
0142         token->resize( token->sizeHint() );
0143 
0144         // now draw in the pixmap buffer
0145         QPixmap pixmap( token->size() );
0146         token->render( &pixmap );
0147         drag->setPixmap( pixmap );
0148         drag->setHotSpot ( pixmap.rect().center() );
0149 
0150         drag->exec( Qt::MoveAction | Qt::CopyAction, Qt::CopyAction );
0151     }
0152 }
0153