ATLAS Offline Software
Loading...
Searching...
No Matches
VP1GraphicsItemCollection.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6// //
7// Implementation of class VP1GraphicsItemCollection //
8// //
9// Author: Thomas Kittelmann <Thomas.Kittelmann@cern.ch> //
10// //
11// Initial version: April 2007 //
12// //
14
17
18#include <QGraphicsItem>
19#include <QGraphicsScene>
20#include <QHash>
21#include <QSet>
22
23#include <iostream>
24#include <cassert>
25
26//____________________________________________________________________
28public:
29
30 QGraphicsScene *scene;
36
37 void setEnabledMovableItems(const bool& enabled);
38 bool ignoresInteractions() const;
39
40
41 QHash<QGraphicsItem*,bool> items2active;
42 typedef QHash<QGraphicsItem*,bool>::iterator ItemDataMapItr;//fixme: optimise flag.
43
44 //For performance reasons, we keep a separate list of the items that
45 //are movable (anyone making a collection with 100000 movable items
46 //will be the first against the wall when the revolution comes):
47 QSet<QGraphicsItem*> movableitems;
48
50
51};
52
53
54//____________________________________________________________________
56 : QObject(parent), m_d(new Imp)
57{
58 m_d->view=0;
59 m_d->lastview=0;
60 m_d->scene=0;
61 m_d->ignoreall_int=false;
62 m_d->ignoreall_move=false;
63 m_d->interactionmode=INERT;
64 m_d->nactiveitems=0;
65}
66
67//____________________________________________________________________
75
76//____________________________________________________________________
78{
79 assert(!m_d->scene&&"VP1GraphicsItemCollection::setInteractionMode can not be called while a scene is attached.");
80 if (m_d->scene) {
81 std::cout<<"VP1GraphicsItemCollection::setInteractionMode can not be called while a scene is attached."<<std::endl;
82 return;
83 }
84 m_d->interactionmode = im;
85}
86
87//____________________________________________________________________
92
93//____________________________________________________________________
94void VP1GraphicsItemCollection::addItem(QGraphicsItem*item, const bool& active,const bool&movable)
95{
96 assert(!m_d->items2active.contains(item)&&"Please do not add the same item twice");
97 assert(!item->flags()&&"Please do not add any flags to your items!!");
98
99 if (m_d->scene) {
100 //The following must postponed if there is no scene:
101 m_d->scene->addItem(item);
102 item->setFlag(QGraphicsItem::ItemIsMovable,(movable&&(!m_d->ignoreall_move)));
103 }
104 assert(!m_d->items2active.contains(item));
105 m_d->items2active.insert(item,active);
106 if (active)
107 ++m_d->nactiveitems;
108 if (movable)
109 m_d->movableitems<<item;
110}
111
112//Fixme: Make VP1GraphicsView setScene() private so it is the same throughout a GV lifetime.
113
114//____________________________________________________________________
116{
117 Imp::ItemDataMapItr it = m_d->items2active.find( item );
118 if (it==m_d->items2active.end())
119 return false;
120 if (it.value())
121 --m_d->nactiveitems;
122 if (m_d->scene) {
123 m_d->scene->removeItem(item);
124 }
125 m_d->items2active.erase(it);
126 assert(!m_d->items2active.contains(item));
127
128 if (!m_d->movableitems.empty()) {
129 QSet<QGraphicsItem*>::iterator it2 = m_d->movableitems.find(item);
130 if (it2!=m_d->movableitems.end()) {
131 assert(m_d->movableitems.contains(item));
132 m_d->movableitems.erase(it2);
133 }
134 }
135 assert(!m_d->movableitems.contains(item));
136 return true;
137}
138
139//____________________________________________________________________
140bool VP1GraphicsItemCollection::setMovable(QGraphicsItem* item, const bool& movable)
141{
142 Imp::ItemDataMapItr it = m_d->items2active.find( item );
143 if (it==m_d->items2active.end())
144 return false;
145 if (movable==m_d->movableitems.contains(item))//Nothing needs to be done:
146 return true;
147 if (movable) {
148 assert(!m_d->movableitems.contains(item));
149 m_d->movableitems<<item;
150 } else {
151 assert(m_d->movableitems.contains(item));
152 m_d->movableitems.remove(item);
153 assert(!m_d->movableitems.contains(item));
154 }
155 if (!m_d->ignoreall_move)
156 item->setFlag(QGraphicsItem::ItemIsMovable,movable);
157 return true;
158}
159
160//____________________________________________________________________
161bool VP1GraphicsItemCollection::setActive(QGraphicsItem* item, const bool& active)
162{
163 Imp::ItemDataMapItr it = m_d->items2active.find( item );
164 if (it==m_d->items2active.end())
165 return false;
166 it.value()=active;
167 if (active)
168 ++m_d->nactiveitems;
169 else
170 --m_d->nactiveitems;
171 return true;
172}
173
174//____________________________________________________________________
175void VP1GraphicsItemCollection::clear(const bool& deleteitems)
176{
177 //Clear selections before deleting items in order to only get one selectionchanged signal.
178 if (m_d->view)
179 m_d->view->clearSelections();
180
181 Imp::ItemDataMapItr it,itE=m_d->items2active.end();
182 if (m_d->scene) {
183 //Remove item from scene and possible also remove event filter.
184 for (it=m_d->items2active.begin();it!=itE;++it)
185 m_d->scene->removeItem(it.key());
186 }
187 if (deleteitems) {
188 //Delete the items:
189 for (it=m_d->items2active.begin();it!=itE;++it) {
190 delete it.key();
191 }
192 }
193 m_d->items2active.clear();
194 m_d->movableitems.clear();
195 m_d->nactiveitems=0;
196}
197
198//____________________________________________________________________
199bool VP1GraphicsItemCollection::hasItem(QGraphicsItem* item) const
200{
201 return m_d->items2active.contains(item);
202}
203
204//____________________________________________________________________
206 if (movableitems.empty())
207 return;
208 QSet<QGraphicsItem*>::iterator it,itE=movableitems.end();
209 for (it=movableitems.begin();it!=itE;++it) {
210 (*it)->setFlag(QGraphicsItem::ItemIsMovable,enabled);
211 }
212
213}
214
215//____________________________________________________________________
217{
218 if (m_d->ignoreall_int==b)
219 return;
220 m_d->ignoreall_int=b;
221}
222
223//____________________________________________________________________
225{
226 if (m_d->ignoreall_move==b)
227 return;
228 m_d->ignoreall_move=b;
229 if (m_d->scene)
230 m_d->setEnabledMovableItems(!b);
231}
232
233//____________________________________________________________________
238
239//____________________________________________________________________
241{
242 assert(!m_d->scene);
243 assert(!m_d->view);
244 m_d->scene=view->scene();
245 // m_d->lastscene=m_d->scene;
246 m_d->view=view;
247 m_d->lastview=m_d->view;
248 Imp::ItemDataMapItr it,itE=m_d->items2active.end();
249 for (it=m_d->items2active.begin();it!=itE;++it) {
250 m_d->scene->addItem(it.key());
251 }
252 //Update this, since we didnt do any updates while there was no scene:
253 m_d->setEnabledMovableItems(!m_d->ignoreall_move);
254}
255
256//____________________________________________________________________
258{
259 if (!m_d->view)
260 return;
261 m_d->view->removeItemCollection(this);
262 //NB. The call to removeItemCollection ends up by calling
263 //VP1GraphicsItemCollection::real_detachFromView().
264}
265
266//____________________________________________________________________
268{
269 //This method is called after the collection has been removed from the the view.
270 if (!m_d->view)
271 return;
272 //Remove item from scene:
273 Imp::ItemDataMapItr it,itE=m_d->items2active.end();
274 for (it=m_d->items2active.begin();it!=itE;++it) {
275 m_d->scene->removeItem(it.key());
276 }
277 m_d->view=0;
278 m_d->scene=0;
279}
280
281//____________________________________________________________________
283{
284 if (m_d->view)
285 return;
286 assert(m_d->lastview);
287 m_d->lastview->addItemCollection(this);
288 //NB. The call to addItemCollection ends up by calling
289 //VP1GraphicsItemCollection::attachToView().
290}
291
292//____________________________________________________________________
294{
295 return m_d->view;
296}
297
298
299//____________________________________________________________________
301{
302 return m_d->items2active.count();
303}
304
305//____________________________________________________________________
307{
308 return m_d->nactiveitems;
309}
310
311//____________________________________________________________________
313{
314 return m_d->movableitems.count();
315}
316
317//____________________________________________________________________
319{
320 return m_d->ignoresInteractions() ? 0 : m_d->nactiveitems;
321}
322
323//____________________________________________________________________
325{
326 return m_d->ignoresInteractions() ? 0 : m_d->movableitems.count();
327}
328
329//____________________________________________________________________
331{
333 return false;
334 QHash<QGraphicsItem*,bool>::const_iterator it = m_d->items2active.find(item);
335
336
337 if (it==m_d->items2active.constEnd())
338 return false;
339 else
340 return it.value();
341}
342
343//____________________________________________________________________
344void VP1GraphicsItemCollection::itemPickedPrivate(QGraphicsItem* item) const
345{
346 itemPicked(item);
347}
348
349//____________________________________________________________________
350void VP1GraphicsItemCollection::itemGotEventPrivate(QGraphicsItem*item,QEvent*event) const
351{
352 itemGotEvent(item,event);
353}
354
355//____________________________________________________________________
356void VP1GraphicsItemCollection::selectionChangedPrivate(const QList<QGraphicsItem*>& items) const
357{
358 selectionChanged(items);
359}
VP1GraphicsItemCollection::INTERACTIONMODE interactionmode
void setEnabledMovableItems(const bool &enabled)
QHash< QGraphicsItem *, bool >::iterator ItemDataMapItr
QHash< QGraphicsItem *, bool > items2active
void setInteractionMode(const INTERACTIONMODE &)
INTERACTIONMODE interactionMode() const
void clear(const bool &deleteitems=true)
bool itemBelongsAndIsPresentlyActive(QGraphicsItem *) const
void itemPicked(QGraphicsItem *) const
void setTemporaryIgnoreInteractions(const bool &)
void selectionChangedPrivate(const QList< QGraphicsItem * > &) const
bool hasItem(QGraphicsItem *) const
void itemGotEvent(QGraphicsItem *, QEvent *) const
void selectionChanged(QList< QGraphicsItem * >) const
void itemPickedPrivate(QGraphicsItem *) const
bool setActive(QGraphicsItem *, const bool &active=true)
void itemGotEventPrivate(QGraphicsItem *, QEvent *) const
void addItem(QGraphicsItem *, const bool &active=false, const bool &movable=false)
bool setMovable(QGraphicsItem *item, const bool &movable=true)