ATLAS Offline Software
VP1GraphicsItemCollection.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 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 //____________________________________________________________________
28 public:
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;
64  m_d->nactiveitems=0;
65 }
66 
67 //____________________________________________________________________
69 {
70  if (m_d->view)
72  clear();
73  delete m_d;
74 }
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  }
85 }
86 
87 //____________________________________________________________________
89 {
90  return m_d->interactionmode;
91 }
92 
93 //____________________________________________________________________
94 void 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)
110 }
111 
112 //Fixme: Make VP1GraphicsView setScene() private so it is the same throughout a GV lifetime.
113 
114 //____________________________________________________________________
116 {
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()) {
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 //____________________________________________________________________
140 bool VP1GraphicsItemCollection::setMovable(QGraphicsItem* item, const bool& movable)
141 {
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));
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 //____________________________________________________________________
161 bool VP1GraphicsItemCollection::setActive(QGraphicsItem* item, const bool& active)
162 {
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 //____________________________________________________________________
175 void 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)
180 
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 //____________________________________________________________________
199 bool VP1GraphicsItemCollection::hasItem(QGraphicsItem* item) const
200 {
201  return m_d->items2active.contains(item);
202 }
203 
204 //____________________________________________________________________
206  if (movableitems.empty())
207  return;
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;
221 }
222 
223 //____________________________________________________________________
225 {
226  if (m_d->ignoreall_move==b)
227  return;
229  if (m_d->scene)
231 }
232 
233 //____________________________________________________________________
235 {
236  return ( interactionmode==INERT || ignoreall_int );
237 }
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;
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:
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:
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);
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 {
332  if (!nPresentlyActiveItems())
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 //____________________________________________________________________
345 {
346  itemPicked(item);
347 }
348 
349 //____________________________________________________________________
351 {
353 }
354 
355 //____________________________________________________________________
357 {
359 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
VP1GraphicsItemCollection::Imp::items2active
QHash< QGraphicsItem *, bool > items2active
Definition: VP1GraphicsItemCollection.cxx:41
VP1GraphicsItemCollection::itemGotEvent
void itemGotEvent(QGraphicsItem *, QEvent *) const
VP1GraphicsView::clearSelections
void clearSelections()
Definition: VP1GraphicsView.cxx:673
VP1GraphicsItemCollection::isAttachedToView
bool isAttachedToView()
Definition: VP1GraphicsItemCollection.cxx:293
VP1GraphicsItemCollection::m_d
Imp * m_d
Definition: VP1GraphicsItemCollection.h:101
VP1GraphicsItemCollection::setTemporaryIgnoreInteractions
void setTemporaryIgnoreInteractions(const bool &)
Definition: VP1GraphicsItemCollection.cxx:216
VP1GraphicsItemCollection::Imp
Definition: VP1GraphicsItemCollection.cxx:27
VP1GraphicsItemCollection::Imp::lastview
VP1GraphicsView * lastview
Definition: VP1GraphicsItemCollection.cxx:31
VP1GraphicsItemCollection::nMovableItems
int nMovableItems() const
Definition: VP1GraphicsItemCollection.cxx:312
VP1GraphicsItemCollection::Imp::interactionmode
VP1GraphicsItemCollection::INTERACTIONMODE interactionmode
Definition: VP1GraphicsItemCollection.cxx:49
VP1GraphicsItemCollection::setActive
bool setActive(QGraphicsItem *, const bool &active=true)
Definition: VP1GraphicsItemCollection.cxx:161
VP1GraphicsItemCollection::nPresentlyMovableItems
int nPresentlyMovableItems() const
Definition: VP1GraphicsItemCollection.cxx:324
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
skel.it
it
Definition: skel.GENtoEVGEN.py:396
VP1GraphicsItemCollection.h
VP1GraphicsItemCollection::nPresentlyActiveItems
int nPresentlyActiveItems() const
Definition: VP1GraphicsItemCollection.cxx:318
python.atlas_oh.im
im
Definition: atlas_oh.py:167
VP1GraphicsItemCollection::Imp::ignoreall_move
bool ignoreall_move
Definition: VP1GraphicsItemCollection.cxx:34
VP1GraphicsItemCollection::INERT
@ INERT
Definition: VP1GraphicsItemCollection.h:62
VP1GraphicsItemCollection::hasItem
bool hasItem(QGraphicsItem *) const
Definition: VP1GraphicsItemCollection.cxx:199
VP1GraphicsItemCollection::setTemporaryIgnoreMovable
void setTemporaryIgnoreMovable(const bool &)
Definition: VP1GraphicsItemCollection.cxx:224
VP1GraphicsItemCollection::selectionChangedPrivate
void selectionChangedPrivate(QList< QGraphicsItem * >) const
Definition: VP1GraphicsItemCollection.cxx:356
VP1GraphicsItemCollection::setMovable
bool setMovable(QGraphicsItem *item, const bool &movable=true)
Definition: VP1GraphicsItemCollection.cxx:140
TruthTest.itE
itE
Definition: TruthTest.py:25
VP1GraphicsItemCollection::itemBelongsAndIsPresentlyActive
bool itemBelongsAndIsPresentlyActive(QGraphicsItem *) const
Definition: VP1GraphicsItemCollection.cxx:330
PyPoolBrowser.item
item
Definition: PyPoolBrowser.py:129
VP1GraphicsItemCollection::nActiveItems
int nActiveItems() const
Definition: VP1GraphicsItemCollection.cxx:306
VP1GraphicsItemCollection::Imp::setEnabledMovableItems
void setEnabledMovableItems(const bool &enabled)
Definition: VP1GraphicsItemCollection.cxx:205
VP1GraphicsItemCollection::attachToView
void attachToView(VP1GraphicsView *)
Definition: VP1GraphicsItemCollection.cxx:240
Trk::active
@ active
Definition: Layer.h:48
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
VP1GraphicsItemCollection::Imp::ignoresInteractions
bool ignoresInteractions() const
Definition: VP1GraphicsItemCollection.cxx:234
VP1GraphicsView::addItemCollection
void addItemCollection(VP1GraphicsItemCollection *)
Definition: VP1GraphicsView.cxx:380
VP1GraphicsItemCollection::detachFromView
void detachFromView()
Definition: VP1GraphicsItemCollection.cxx:257
VP1GraphicsItemCollection::interactionMode
INTERACTIONMODE interactionMode() const
Definition: VP1GraphicsItemCollection.cxx:88
test_pyathena.parent
parent
Definition: test_pyathena.py:15
VP1GraphicsItemCollection::clear
void clear(const bool &deleteitems=true)
Definition: VP1GraphicsItemCollection.cxx:175
VP1GraphicsItemCollection::Imp::movableitems
QSet< QGraphicsItem * > movableitems
Definition: VP1GraphicsItemCollection.cxx:47
VP1GraphicsItemCollection::reattachToView
void reattachToView()
Definition: VP1GraphicsItemCollection.cxx:282
VP1GraphicsItemCollection::addItem
void addItem(QGraphicsItem *, const bool &active=false, const bool &movable=false)
Definition: VP1GraphicsItemCollection.cxx:94
VP1GraphicsItemCollection::itemGotEventPrivate
void itemGotEventPrivate(QGraphicsItem *, QEvent *) const
Definition: VP1GraphicsItemCollection.cxx:350
VP1GraphicsView::removeItemCollection
void removeItemCollection(VP1GraphicsItemCollection *)
Definition: VP1GraphicsView.cxx:393
VP1GraphicsItemCollection::Imp::view
VP1GraphicsView * view
Definition: VP1GraphicsItemCollection.cxx:32
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
VP1GraphicsItemCollection::removeItem
bool removeItem(QGraphicsItem *)
Definition: VP1GraphicsItemCollection.cxx:115
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
VP1GraphicsItemCollection::VP1GraphicsItemCollection
VP1GraphicsItemCollection(QObject *parent=0)
Definition: VP1GraphicsItemCollection.cxx:55
item
Definition: ItemListSvc.h:43
VP1GraphicsItemCollection::selectionChanged
void selectionChanged(QList< QGraphicsItem * >) const
VP1GraphicsItemCollection::setInteractionMode
void setInteractionMode(const INTERACTIONMODE &)
Definition: VP1GraphicsItemCollection.cxx:77
VP1GraphicsItemCollection::itemPickedPrivate
void itemPickedPrivate(QGraphicsItem *) const
Definition: VP1GraphicsItemCollection.cxx:344
VP1GraphicsItemCollection::Imp::scene
QGraphicsScene * scene
Definition: VP1GraphicsItemCollection.cxx:30
VP1GraphicsItemCollection::Imp::nactiveitems
int nactiveitems
Definition: VP1GraphicsItemCollection.cxx:35
VP1GraphicsItemCollection::itemPicked
void itemPicked(QGraphicsItem *) const
VP1GraphicsItemCollection::Imp::ItemDataMapItr
QHash< QGraphicsItem *, bool >::iterator ItemDataMapItr
Definition: VP1GraphicsItemCollection.cxx:42
VP1GraphicsItemCollection::nItems
int nItems() const
Definition: VP1GraphicsItemCollection.cxx:300
VP1GraphicsItemCollection::real_detachFromView
void real_detachFromView()
Definition: VP1GraphicsItemCollection.cxx:267
VP1GraphicsItemCollection::~VP1GraphicsItemCollection
virtual ~VP1GraphicsItemCollection()
Definition: VP1GraphicsItemCollection.cxx:68
VP1GraphicsView.h
VP1GraphicsItemCollection::Imp::ignoreall_int
bool ignoreall_int
Definition: VP1GraphicsItemCollection.cxx:33
VP1GraphicsView
Definition: VP1GraphicsView.h:22
drawFromPickle.view
view
Definition: drawFromPickle.py:294
VP1GraphicsItemCollection::INTERACTIONMODE
INTERACTIONMODE
Definition: VP1GraphicsItemCollection.h:62