1//
2// KBlackbox
3//
4// A simple game inspired by an emacs module
5//
6/***************************************************************************
7 * Copyright (c) 1999-2000, Robert Cimrman *
8 * cimrman3@students.zcu.cz *
9 * *
10 * Copyright (c) 2007, Nicolas Roffet *
11 * nicolas-kde@roffet.com *
12 * *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA *
28 ***************************************************************************/
29
30#include "kbbgraphicsitemset.h"
31
32
33
34#include <QGraphicsScene>
35
36
37#include "kbbgraphicsitem.h"
38#include "kbbitemwithposition.h"
39
40
41
42//
43// Constructor / Destructor
44//
45
46KBBGraphicsItemSet::KBBGraphicsItemSet(QGraphicsScene* scene)
47{
48 m_scene = scene;
49}
50
51
52KBBGraphicsItemSet::~KBBGraphicsItemSet()
53{
54 clear();
55}
56
57
58
59//
60// Public
61//
62
63int KBBGraphicsItemSet::anyItemPosition()
64{
65 if (m_items.count()>0)
66 return m_items.last()->position();
67 else
68 return NO_INDEX;
69}
70
71
72int KBBGraphicsItemSet::count() const
73{
74 return m_items.count();
75}
76
77
78void KBBGraphicsItemSet::clear()
79{
80 while (m_items.count()>0) {
81 remove(m_items.last()->position());
82 }
83}
84
85
86bool KBBGraphicsItemSet::contains(int position)
87{
88 return (indexOf(position)!=NO_INDEX);
89}
90
91
92bool KBBGraphicsItemSet::containsVisible(int position)
93{
94 int i = indexOf(position);
95
96 if (i!=NO_INDEX) {
97 if(dynamic_cast<KBBGraphicsItem*>(m_items[i]))
98 return ((dynamic_cast<KBBGraphicsItem*>(m_items[i]))->isVisible());
99 else
100 return true;
101 } else
102 return false;
103}
104
105
106void KBBGraphicsItemSet::insert(KBBItemWithPosition* item)
107{
108 if (contains(item->position()))
109 // We want to avoid duplicated item on a given position.
110 item->cleanDelete();
111 else
112 m_items.append(item);
113}
114
115
116KBBItemWithPosition* KBBGraphicsItemSet::item(int position)
117{
118 KBBItemWithPosition* r = NULL;
119
120 for (int i=0;i<m_items.count();i++)
121 if (m_items[i]->position()==position)
122 r = m_items[i];
123 return r;
124}
125
126
127void KBBGraphicsItemSet::remove(int position)
128{
129 int i = indexOf(position);
130
131 if (i!=NO_INDEX) {
132 m_items[i]->cleanDelete();
133 m_scene->update();
134 m_items.removeAt(i);
135 }
136}
137
138
139void KBBGraphicsItemSet::setVisible(const int position, const bool visible)
140{
141 int i = indexOf(position);
142
143 if (i!=NO_INDEX) {
144 if(dynamic_cast<KBBGraphicsItem*>(m_items[i]))
145 (dynamic_cast<KBBGraphicsItem*>(m_items[i]))->setVisible(visible);
146 }
147}
148
149
150
151//
152// Private
153//
154
155int KBBGraphicsItemSet::indexOf(int position)
156{
157 for(int i=0;i<m_items.count();i++)
158 if (m_items[i]->position()==position)
159 return i;
160
161 return NO_INDEX;
162}
163