File indexing completed on 2024-05-19 04:48:57

0001 /***************************************************************************
0002  *   Copyright (c) 2008  Jeff Mitchell <mitchell@kde.org>                  *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0018  ***************************************************************************/
0019 
0020 #include "PopupDropperView.h"
0021 #include "PopupDropper.h"
0022 #include "PopupDropper_p.h"
0023 #include "PopupDropperItem.h"
0024 
0025 #include <QDragEnterEvent>
0026 #include <QDragMoveEvent>
0027 #include <QDragLeaveEvent>
0028 #include <QDropEvent>
0029 
0030 #include <QtDebug>
0031 
0032 class PopupDropperViewPrivate
0033 {
0034 public:
0035     PopupDropperViewPrivate( PopupDropper* pd )
0036         : pd( pd )
0037         , lastItem( nullptr )
0038         , entered( false )
0039         {}
0040 
0041     PopupDropper *pd;
0042     PopupDropperItem *lastItem;
0043     bool entered;
0044 };
0045 
0046 ////////////////////////////////////////////////////////////////////////////
0047 
0048 PopupDropperView::PopupDropperView( PopupDropper *pd, QGraphicsScene *scene, QWidget *parent )
0049     : QGraphicsView( scene, parent ) 
0050     , d( new PopupDropperViewPrivate( pd ) )
0051 {
0052     setInteractive( true );
0053     setAcceptDrops( true );
0054 }
0055 
0056 PopupDropperView::~PopupDropperView()
0057 {
0058     delete d;
0059 }
0060 
0061 void PopupDropperView::dragMoveEvent( QDragMoveEvent *event )
0062 {
0063     //qDebug() << "PopupDropperView::dragMoveEvent";
0064     QGraphicsItem* item = itemAt( event->pos() );
0065 
0066     #define svgitem(x) dynamic_cast<QGraphicsSvgItem*>(x)
0067     #define textitem(x) dynamic_cast<QGraphicsTextItem*>(x)
0068     #define borderitem(x) dynamic_cast<QGraphicsRectItem*>(x)
0069 
0070     if( !svgitem(item) && !textitem(item) && !borderitem(item) )
0071     {
0072         if( d->lastItem )
0073             d->lastItem->hoverLeft();
0074         d->lastItem = nullptr;
0075     }
0076     else if( svgitem(item) &&
0077             d->lastItem != dynamic_cast<PopupDropperItem*>( svgitem(item)->parentItem() ) )
0078     {
0079         //qDebug() << "svg item";
0080         if( d->lastItem )
0081             d->lastItem->hoverLeft();
0082         static_cast<PopupDropperItem*>( svgitem(item)->parentItem() )->hoverEntered();
0083         d->lastItem = static_cast<PopupDropperItem*>( svgitem(item)->parentItem() );
0084     }
0085     else if( textitem(item) && 
0086              d->lastItem != dynamic_cast<PopupDropperItem*>( textitem(item)->parentItem() ) )
0087     {
0088         //qDebug() << "text item";
0089         if( d->lastItem )
0090             d->lastItem->hoverLeft();
0091         static_cast<PopupDropperItem*>( textitem(item)->parentItem() )->hoverEntered();
0092         d->lastItem = static_cast<PopupDropperItem*>( textitem(item)->parentItem() );
0093     }
0094     else if( borderitem(item) && 
0095              d->lastItem != dynamic_cast<PopupDropperItem*>( borderitem(item)->parentItem() ) )
0096     {
0097         //qDebug() << "border item";
0098         if( d->lastItem )
0099             d->lastItem->hoverLeft();
0100         static_cast<PopupDropperItem*>( borderitem(item)->parentItem() )->hoverEntered();
0101         d->lastItem = static_cast<PopupDropperItem*>( borderitem(item)->parentItem() );
0102     }
0103     
0104     #undef borderitem
0105     #undef textitem
0106     #undef pditem
0107 
0108     event->accept();
0109 }
0110 
0111 void PopupDropperView::dragEnterEvent( QDragEnterEvent *event )
0112 {
0113     //qDebug() << "PopupDropperView::dragEnterEvent";
0114     event->accept();
0115     d->entered = true;
0116     d->pd->d->dragEntered();
0117 }
0118 
0119 void PopupDropperView::dragLeaveEvent( QDragLeaveEvent *event )
0120 {
0121     //qDebug() << "PopupDropperView::dragLeaveEvent";
0122     event->accept();
0123     if( d->lastItem )
0124     {
0125         d->lastItem->hoverLeft();
0126         d->lastItem = nullptr;
0127     }
0128     d->pd->d->dragLeft();
0129 }
0130 
0131 void PopupDropperView::dropEvent( QDropEvent *event )
0132 {
0133     //qDebug() << "PopupDropperView::dropEvent";
0134 
0135     if( !d->pd->d->amIOnTop( this ) )
0136     {
0137         event->accept();
0138         return;
0139     }
0140 
0141     QGraphicsItem* item = itemAt( event->pos() );
0142 
0143     if( QGraphicsSvgItem *svgItem = dynamic_cast<QGraphicsSvgItem*>(item) )
0144     {
0145         //qDebug() << "It's a svg item";
0146         if( PopupDropperItem *pdi = dynamic_cast<PopupDropperItem*>( svgItem->parentItem() ) )
0147             pdi->dropped( event );
0148     }
0149     else if( QGraphicsTextItem *textItem = dynamic_cast<QGraphicsTextItem*>(item) )
0150     {
0151         //qDebug() << "It's a text item";
0152         if( PopupDropperItem *pdi = dynamic_cast<PopupDropperItem*>( textItem->parentItem() ) )
0153             pdi->dropped( event );
0154     }
0155     else if( QGraphicsRectItem *borderItem = dynamic_cast<QGraphicsRectItem*>(item) )
0156     {
0157         //qDebug() << "It's a border item";
0158         if( PopupDropperItem *pdi = dynamic_cast<PopupDropperItem*>( borderItem->parentItem() ) )
0159             pdi->dropped( event );
0160     }
0161     event->accept();
0162     //qDebug() << "Leaving dropEvent";
0163 }
0164 
0165 void PopupDropperView::resetView()
0166 {
0167     d->lastItem = nullptr;
0168     d->entered = false;
0169     setAcceptDrops( true );
0170 }
0171 
0172 void PopupDropperView::deactivateHover()
0173 {
0174     if( d->lastItem )
0175         d->lastItem->hoverLeft();
0176     d->lastItem = nullptr;
0177 }
0178 
0179 bool PopupDropperView::entered() const
0180 {
0181     return d->entered;
0182 }
0183 
0184 void PopupDropperView::setEntered( bool entered )
0185 {
0186     d->entered = entered;
0187 }
0188 
0189