ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
graphics
VP1
VP1Base
src
VP1GraphicsItemCollection.cxx
Go to the documentation of this file.
1
/*
2
Copyright (C) 2002-2026 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
15
#include "
VP1Base/VP1GraphicsItemCollection.h
"
16
#include "
VP1Base/VP1GraphicsView.h
"
17
18
#include <QGraphicsItem>
19
#include <QGraphicsScene>
20
#include <QHash>
21
#include <QSet>
22
23
#include <iostream>
24
#include <cassert>
25
26
//____________________________________________________________________
27
class
VP1GraphicsItemCollection::Imp
{
28
public
:
29
30
QGraphicsScene *
scene
{};
31
VP1GraphicsView
*
lastview
{};
32
VP1GraphicsView
*
view
{};
33
bool
ignoreall_int
{};
34
bool
ignoreall_move
{};
35
int
nactiveitems
{};
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
49
VP1GraphicsItemCollection::INTERACTIONMODE
interactionmode
{};
50
51
};
52
53
54
//____________________________________________________________________
55
VP1GraphicsItemCollection::VP1GraphicsItemCollection
( QObject * parent)
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
//____________________________________________________________________
68
VP1GraphicsItemCollection::~VP1GraphicsItemCollection
()
69
{
70
if
(
m_d
->view)
71
detachFromView
();
72
clear
();
73
delete
m_d
;
74
}
75
76
//____________________________________________________________________
77
void
VP1GraphicsItemCollection::setInteractionMode
(
const
VP1GraphicsItemCollection::INTERACTIONMODE
& im )
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
//____________________________________________________________________
88
VP1GraphicsItemCollection::INTERACTIONMODE
VP1GraphicsItemCollection::interactionMode
()
const
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)
109
m_d
->movableitems<<item;
110
}
111
112
//Fixme: Make VP1GraphicsView setScene() private so it is the same throughout a GV lifetime.
113
114
//____________________________________________________________________
115
bool
VP1GraphicsItemCollection::removeItem
(QGraphicsItem*item)
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
//____________________________________________________________________
140
bool
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
//____________________________________________________________________
161
bool
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
//____________________________________________________________________
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)
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
//____________________________________________________________________
199
bool
VP1GraphicsItemCollection::hasItem
(QGraphicsItem* item)
const
200
{
201
return
m_d
->items2active.contains(item);
202
}
203
204
//____________________________________________________________________
205
void
VP1GraphicsItemCollection::Imp::setEnabledMovableItems
(
const
bool
& enabled) {
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
//____________________________________________________________________
216
void
VP1GraphicsItemCollection::setTemporaryIgnoreInteractions
(
const
bool
& b )
217
{
218
if
(
m_d
->ignoreall_int==b)
219
return
;
220
m_d
->ignoreall_int=b;
221
}
222
223
//____________________________________________________________________
224
void
VP1GraphicsItemCollection::setTemporaryIgnoreMovable
(
const
bool
& b )
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
//____________________________________________________________________
234
bool
VP1GraphicsItemCollection::Imp::ignoresInteractions
()
const
235
{
236
return
(
interactionmode
==
INERT
||
ignoreall_int
);
237
}
238
239
//____________________________________________________________________
240
void
VP1GraphicsItemCollection::attachToView
(
VP1GraphicsView
*view)
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
//____________________________________________________________________
257
void
VP1GraphicsItemCollection::detachFromView
()
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
//____________________________________________________________________
267
void
VP1GraphicsItemCollection::real_detachFromView
()
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
//____________________________________________________________________
282
void
VP1GraphicsItemCollection::reattachToView
()
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
//____________________________________________________________________
293
bool
VP1GraphicsItemCollection::isAttachedToView
()
294
{
295
return
m_d
->view;
296
}
297
298
299
//____________________________________________________________________
300
int
VP1GraphicsItemCollection::nItems
()
const
301
{
302
return
m_d
->items2active.count();
303
}
304
305
//____________________________________________________________________
306
int
VP1GraphicsItemCollection::nActiveItems
()
const
307
{
308
return
m_d
->nactiveitems;
309
}
310
311
//____________________________________________________________________
312
int
VP1GraphicsItemCollection::nMovableItems
()
const
313
{
314
return
m_d
->movableitems.count();
315
}
316
317
//____________________________________________________________________
318
int
VP1GraphicsItemCollection::nPresentlyActiveItems
()
const
319
{
320
return
m_d
->ignoresInteractions() ? 0 :
m_d
->nactiveitems;
321
}
322
323
//____________________________________________________________________
324
int
VP1GraphicsItemCollection::nPresentlyMovableItems
()
const
325
{
326
return
m_d
->ignoresInteractions() ? 0 :
m_d
->movableitems.count();
327
}
328
329
//____________________________________________________________________
330
bool
VP1GraphicsItemCollection::itemBelongsAndIsPresentlyActive
(QGraphicsItem*item)
const
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
//____________________________________________________________________
344
void
VP1GraphicsItemCollection::itemPickedPrivate
(QGraphicsItem* item)
const
345
{
346
itemPicked
(item);
347
}
348
349
//____________________________________________________________________
350
void
VP1GraphicsItemCollection::itemGotEventPrivate
(QGraphicsItem*item,QEvent*event)
const
351
{
352
itemGotEvent
(item,event);
353
}
354
355
//____________________________________________________________________
356
void
VP1GraphicsItemCollection::selectionChangedPrivate
(
const
QList<QGraphicsItem*>& items)
const
357
{
358
selectionChanged
(items);
359
}
clear
void clear()
Empty the pool.
VP1GraphicsItemCollection.h
VP1GraphicsView.h
VP1GraphicsItemCollection::Imp
Definition
VP1GraphicsItemCollection.cxx:27
VP1GraphicsItemCollection::Imp::ignoreall_int
bool ignoreall_int
Definition
VP1GraphicsItemCollection.cxx:33
VP1GraphicsItemCollection::Imp::nactiveitems
int nactiveitems
Definition
VP1GraphicsItemCollection.cxx:35
VP1GraphicsItemCollection::Imp::scene
QGraphicsScene * scene
Definition
VP1GraphicsItemCollection.cxx:30
VP1GraphicsItemCollection::Imp::interactionmode
VP1GraphicsItemCollection::INTERACTIONMODE interactionmode
Definition
VP1GraphicsItemCollection.cxx:49
VP1GraphicsItemCollection::Imp::setEnabledMovableItems
void setEnabledMovableItems(const bool &enabled)
Definition
VP1GraphicsItemCollection.cxx:205
VP1GraphicsItemCollection::Imp::ignoreall_move
bool ignoreall_move
Definition
VP1GraphicsItemCollection.cxx:34
VP1GraphicsItemCollection::Imp::view
VP1GraphicsView * view
Definition
VP1GraphicsItemCollection.cxx:32
VP1GraphicsItemCollection::Imp::ItemDataMapItr
QHash< QGraphicsItem *, bool >::iterator ItemDataMapItr
Definition
VP1GraphicsItemCollection.cxx:42
VP1GraphicsItemCollection::Imp::items2active
QHash< QGraphicsItem *, bool > items2active
Definition
VP1GraphicsItemCollection.cxx:41
VP1GraphicsItemCollection::Imp::lastview
VP1GraphicsView * lastview
Definition
VP1GraphicsItemCollection.cxx:31
VP1GraphicsItemCollection::Imp::ignoresInteractions
bool ignoresInteractions() const
Definition
VP1GraphicsItemCollection.cxx:234
VP1GraphicsItemCollection::Imp::movableitems
QSet< QGraphicsItem * > movableitems
Definition
VP1GraphicsItemCollection.cxx:47
VP1GraphicsItemCollection::setInteractionMode
void setInteractionMode(const INTERACTIONMODE &)
Definition
VP1GraphicsItemCollection.cxx:77
VP1GraphicsItemCollection::interactionMode
INTERACTIONMODE interactionMode() const
Definition
VP1GraphicsItemCollection.cxx:88
VP1GraphicsItemCollection::clear
void clear(const bool &deleteitems=true)
Definition
VP1GraphicsItemCollection.cxx:175
VP1GraphicsItemCollection::nActiveItems
int nActiveItems() const
Definition
VP1GraphicsItemCollection.cxx:306
VP1GraphicsItemCollection::itemBelongsAndIsPresentlyActive
bool itemBelongsAndIsPresentlyActive(QGraphicsItem *) const
Definition
VP1GraphicsItemCollection.cxx:330
VP1GraphicsItemCollection::real_detachFromView
void real_detachFromView()
Definition
VP1GraphicsItemCollection.cxx:267
VP1GraphicsItemCollection::nItems
int nItems() const
Definition
VP1GraphicsItemCollection.cxx:300
VP1GraphicsItemCollection::isAttachedToView
bool isAttachedToView()
Definition
VP1GraphicsItemCollection.cxx:293
VP1GraphicsItemCollection::itemPicked
void itemPicked(QGraphicsItem *) const
VP1GraphicsItemCollection::setTemporaryIgnoreInteractions
void setTemporaryIgnoreInteractions(const bool &)
Definition
VP1GraphicsItemCollection.cxx:216
VP1GraphicsItemCollection::selectionChangedPrivate
void selectionChangedPrivate(const QList< QGraphicsItem * > &) const
Definition
VP1GraphicsItemCollection.cxx:356
VP1GraphicsItemCollection::hasItem
bool hasItem(QGraphicsItem *) const
Definition
VP1GraphicsItemCollection.cxx:199
VP1GraphicsItemCollection::itemGotEvent
void itemGotEvent(QGraphicsItem *, QEvent *) const
VP1GraphicsItemCollection::nMovableItems
int nMovableItems() const
Definition
VP1GraphicsItemCollection.cxx:312
VP1GraphicsItemCollection::setTemporaryIgnoreMovable
void setTemporaryIgnoreMovable(const bool &)
Definition
VP1GraphicsItemCollection.cxx:224
VP1GraphicsItemCollection::VP1GraphicsView
friend class VP1GraphicsView
Definition
VP1GraphicsItemCollection.h:104
VP1GraphicsItemCollection::selectionChanged
void selectionChanged(QList< QGraphicsItem * >) const
VP1GraphicsItemCollection::INTERACTIONMODE
INTERACTIONMODE
Definition
VP1GraphicsItemCollection.h:62
VP1GraphicsItemCollection::INERT
@ INERT
Definition
VP1GraphicsItemCollection.h:62
VP1GraphicsItemCollection::detachFromView
void detachFromView()
Definition
VP1GraphicsItemCollection.cxx:257
VP1GraphicsItemCollection::removeItem
bool removeItem(QGraphicsItem *)
Definition
VP1GraphicsItemCollection.cxx:115
VP1GraphicsItemCollection::nPresentlyActiveItems
int nPresentlyActiveItems() const
Definition
VP1GraphicsItemCollection.cxx:318
VP1GraphicsItemCollection::itemPickedPrivate
void itemPickedPrivate(QGraphicsItem *) const
Definition
VP1GraphicsItemCollection.cxx:344
VP1GraphicsItemCollection::setActive
bool setActive(QGraphicsItem *, const bool &active=true)
Definition
VP1GraphicsItemCollection.cxx:161
VP1GraphicsItemCollection::nPresentlyMovableItems
int nPresentlyMovableItems() const
Definition
VP1GraphicsItemCollection.cxx:324
VP1GraphicsItemCollection::itemGotEventPrivate
void itemGotEventPrivate(QGraphicsItem *, QEvent *) const
Definition
VP1GraphicsItemCollection.cxx:350
VP1GraphicsItemCollection::VP1GraphicsItemCollection
VP1GraphicsItemCollection(QObject *parent=0)
Definition
VP1GraphicsItemCollection.cxx:55
VP1GraphicsItemCollection::addItem
void addItem(QGraphicsItem *, const bool &active=false, const bool &movable=false)
Definition
VP1GraphicsItemCollection.cxx:94
VP1GraphicsItemCollection::setMovable
bool setMovable(QGraphicsItem *item, const bool &movable=true)
Definition
VP1GraphicsItemCollection.cxx:140
VP1GraphicsItemCollection::reattachToView
void reattachToView()
Definition
VP1GraphicsItemCollection.cxx:282
VP1GraphicsItemCollection::~VP1GraphicsItemCollection
virtual ~VP1GraphicsItemCollection()
Definition
VP1GraphicsItemCollection.cxx:68
VP1GraphicsItemCollection::m_d
Imp * m_d
Definition
VP1GraphicsItemCollection.h:102
VP1GraphicsItemCollection::attachToView
void attachToView(VP1GraphicsView *)
Definition
VP1GraphicsItemCollection.cxx:240
Generated on
for ATLAS Offline Software by
1.17.0