Debian Codegraph @

src/context/popupdropper/libpud/PopupDropperView.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2008 Jeff Mitchell <[email protected]> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
19
20 #include "PopupDropperView.h"
21 #include "PopupDropper.h"
22 #include "PopupDropper_p.h"
23 #include "PopupDropperItem.h"
24
25 #include <QDragEnterEvent>
26 #include <QDragMoveEvent>
27 #include <QDragLeaveEvent>
28 #include <QDropEvent>
29
30 class PopupDropperViewPrivate
31 {
32 public:
33 PopupDropperViewPrivate( PopupDropper* pd )
34 : pd( pd )
35 , lastItem( nullptr )
36 , entered( false )
37 {}
38
39 PopupDropper *pd;
40 PopupDropperItem *lastItem;
41 bool entered;
42 };
43
44 ////////////////////////////////////////////////////////////////////////////
45
46 PopupDropperView::PopupDropperView( PopupDropper *pd, QGraphicsScene *scene, QWidget *parent )
47 : QGraphicsView( scene, parent )
48 , d( new PopupDropperViewPrivate( pd ) )
49 {
50 setInteractive( true );
51 setAcceptDrops( true );
52 }
53
54 PopupDropperView::~PopupDropperView()
55 {
56 delete d;
57 }
58
59 void PopupDropperView::dragMoveEvent( QDragMoveEvent *event )
60 {
61 //qDebug() << "PopupDropperView::dragMoveEvent";
62 QGraphicsItem* item = itemAt( event->position().toPoint() );
63
64 #define svgitem(x) dynamic_cast<QGraphicsSvgItem*>(x)
65 #define textitem(x) dynamic_cast<QGraphicsTextItem*>(x)
66 #define borderitem(x) dynamic_cast<QGraphicsRectItem*>(x)
67
68 if( !svgitem(item) && !textitem(item) && !borderitem(item) )
69 {
70 if( d->lastItem )
71 d->lastItem->hoverLeft();
72 d->lastItem = nullptr;
73 }
74 else if( svgitem(item) &&
75 d->lastItem != dynamic_cast<PopupDropperItem*>( svgitem(item)->parentItem() ) )
76 {
77 //qDebug() << "svg item";
78 if( d->lastItem )
79 d->lastItem->hoverLeft();
80 static_cast<PopupDropperItem*>( svgitem(item)->parentItem() )->hoverEntered();
81 d->lastItem = static_cast<PopupDropperItem*>( svgitem(item)->parentItem() );
82 }
83 else if( textitem(item) &&
84 d->lastItem != dynamic_cast<PopupDropperItem*>( textitem(item)->parentItem() ) )
85 {
86 //qDebug() << "text item";
87 if( d->lastItem )
88 d->lastItem->hoverLeft();
89 static_cast<PopupDropperItem*>( textitem(item)->parentItem() )->hoverEntered();
90 d->lastItem = static_cast<PopupDropperItem*>( textitem(item)->parentItem() );
91 }
92 else if( borderitem(item) &&
93 d->lastItem != dynamic_cast<PopupDropperItem*>( borderitem(item)->parentItem() ) )
94 {
95 //qDebug() << "border item";
96 if( d->lastItem )
97 d->lastItem->hoverLeft();
98 static_cast<PopupDropperItem*>( borderitem(item)->parentItem() )->hoverEntered();
99 d->lastItem = static_cast<PopupDropperItem*>( borderitem(item)->parentItem() );
100 }
101
102 #undef borderitem
103 #undef textitem
104 #undef pditem
105
106 event->accept();
107 }
108
109 void PopupDropperView::dragEnterEvent( QDragEnterEvent *event )
110 {
111 //qDebug() << "PopupDropperView::dragEnterEvent";
112 event->accept();
113 d->entered = true;
114 d->pd->d->dragEntered();
115 }
116
117 void PopupDropperView::dragLeaveEvent( QDragLeaveEvent *event )
118 {
119 //qDebug() << "PopupDropperView::dragLeaveEvent";
120 event->accept();
121 if( d->lastItem )
122 {
123 d->lastItem->hoverLeft();
124 d->lastItem = nullptr;
125 }
126 d->pd->d->dragLeft();
127 }
128
129 void PopupDropperView::dropEvent( QDropEvent *event )
130 {
131 //qDebug() << "PopupDropperView::dropEvent";
132
133 if( !d->pd->d->amIOnTop( this ) )
134 {
135 event->accept();
136 return;
137 }
138
139 QGraphicsItem* item = itemAt( event->position().toPoint() );
140
141 if( QGraphicsSvgItem *svgItem = dynamic_cast<QGraphicsSvgItem*>(item) )
142 {
143 //qDebug() << "It's a svg item";
144 if( PopupDropperItem *pdi = dynamic_cast<PopupDropperItem*>( svgItem->parentItem() ) )
145 pdi->dropped( event );
146 }
147 else if( QGraphicsTextItem *textItem = dynamic_cast<QGraphicsTextItem*>(item) )
148 {
149 //qDebug() << "It's a text item";
150 if( PopupDropperItem *pdi = dynamic_cast<PopupDropperItem*>( textItem->parentItem() ) )
151 pdi->dropped( event );
152 }
153 else if( QGraphicsRectItem *borderItem = dynamic_cast<QGraphicsRectItem*>(item) )
154 {
155 //qDebug() << "It's a border item";
156 if( PopupDropperItem *pdi = dynamic_cast<PopupDropperItem*>( borderItem->parentItem() ) )
157 pdi->dropped( event );
158 }
159 event->accept();
160 //qDebug() << "Leaving dropEvent";
161 }
162
163 void PopupDropperView::resetView()
164 {
165 d->lastItem = nullptr;
166 d->entered = false;
167 setAcceptDrops( true );
168 }
169
170 void PopupDropperView::deactivateHover()
171 {
172 if( d->lastItem )
173 d->lastItem->hoverLeft();
174 d->lastItem = nullptr;
175 }
176
177 bool PopupDropperView::entered() const
178 {
179 return d->entered;
180 }
181
182 void PopupDropperView::setEntered( bool entered )
183 {
184 d->entered = entered;
185 }
186
187