insilmaril@0
|
1 |
#include "flagrowobj.h"
|
insilmaril@0
|
2 |
|
insilmaril@366
|
3 |
#include <QToolBar>
|
insilmaril@366
|
4 |
|
insilmaril@0
|
5 |
/////////////////////////////////////////////////////////////////
|
insilmaril@0
|
6 |
// FlagRowObj
|
insilmaril@0
|
7 |
/////////////////////////////////////////////////////////////////
|
insilmaril@0
|
8 |
FlagRowObj::FlagRowObj()
|
insilmaril@0
|
9 |
{
|
insilmaril@166
|
10 |
// cout << "Const FlagRowObj ()\n";
|
insilmaril@0
|
11 |
init ();
|
insilmaril@0
|
12 |
}
|
insilmaril@0
|
13 |
|
insilmaril@408
|
14 |
FlagRowObj::FlagRowObj(QGraphicsScene* s):MapObj(s)
|
insilmaril@0
|
15 |
{
|
insilmaril@408
|
16 |
// cout << "Const FlagRowObj (s)\n";
|
insilmaril@0
|
17 |
init ();
|
insilmaril@0
|
18 |
}
|
insilmaril@0
|
19 |
|
insilmaril@0
|
20 |
FlagRowObj::~FlagRowObj()
|
insilmaril@0
|
21 |
{
|
insilmaril@425
|
22 |
//cout << "Destr FlagRowObj\n";
|
insilmaril@406
|
23 |
while (!flag.isEmpty())
|
insilmaril@425
|
24 |
delete (flag.takeFirst() );
|
insilmaril@0
|
25 |
}
|
insilmaril@0
|
26 |
|
insilmaril@0
|
27 |
void FlagRowObj::init ()
|
insilmaril@0
|
28 |
{
|
insilmaril@0
|
29 |
parentRow=NULL;
|
insilmaril@256
|
30 |
showFlags=true;
|
insilmaril@0
|
31 |
}
|
insilmaril@0
|
32 |
|
insilmaril@0
|
33 |
void FlagRowObj::copy (FlagRowObj* other)
|
insilmaril@0
|
34 |
{
|
insilmaril@0
|
35 |
MapObj::copy(other);
|
insilmaril@0
|
36 |
parentRow=other->parentRow;
|
insilmaril@0
|
37 |
flag.clear();
|
insilmaril@406
|
38 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@406
|
39 |
addFlag (flag.at(i));
|
insilmaril@0
|
40 |
}
|
insilmaril@0
|
41 |
|
insilmaril@0
|
42 |
void FlagRowObj::clone (FlagRowObj* pr)
|
insilmaril@0
|
43 |
{
|
insilmaril@0
|
44 |
// Difference to copy:
|
insilmaril@0
|
45 |
// We don't copy the flags here, they
|
insilmaril@0
|
46 |
// are created on the fly by toggle and activate
|
insilmaril@0
|
47 |
// This saves lots of canvas objects.
|
insilmaril@0
|
48 |
MapObj::copy(pr);
|
insilmaril@0
|
49 |
flag.clear();
|
insilmaril@0
|
50 |
parentRow=pr;
|
insilmaril@0
|
51 |
}
|
insilmaril@0
|
52 |
|
insilmaril@0
|
53 |
void FlagRowObj::move(double x, double y)
|
insilmaril@0
|
54 |
{
|
insilmaril@0
|
55 |
MapObj::move(x,y);
|
insilmaril@408
|
56 |
qreal dx=0;
|
insilmaril@406
|
57 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@0
|
58 |
{
|
insilmaril@406
|
59 |
flag.at(i)->move(x+dx,y);
|
insilmaril@408
|
60 |
dx+=QSizeF(flag.at(i)->getSize() ).width();
|
insilmaril@0
|
61 |
}
|
insilmaril@0
|
62 |
}
|
insilmaril@0
|
63 |
|
insilmaril@0
|
64 |
void FlagRowObj::moveBy(double x, double y)
|
insilmaril@0
|
65 |
{
|
insilmaril@0
|
66 |
move (x+absPos.x(),y+absPos.y() );
|
insilmaril@0
|
67 |
}
|
insilmaril@0
|
68 |
|
insilmaril@0
|
69 |
void FlagRowObj::setVisibility (bool v)
|
insilmaril@0
|
70 |
{
|
insilmaril@0
|
71 |
MapObj::setVisibility(v);
|
insilmaril@406
|
72 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@406
|
73 |
flag.at(i)->setVisibility (v);
|
insilmaril@0
|
74 |
}
|
insilmaril@0
|
75 |
|
insilmaril@0
|
76 |
FlagObj* FlagRowObj::addFlag (FlagObj *fo)
|
insilmaril@0
|
77 |
{
|
insilmaril@408
|
78 |
FlagObj *newfo=new FlagObj (scene);
|
insilmaril@408
|
79 |
newfo->copy (fo); // create a deep copy of fo
|
insilmaril@0
|
80 |
newfo->move (absPos.x() + bbox.width(), absPos.y() );
|
insilmaril@0
|
81 |
flag.append(newfo);
|
insilmaril@0
|
82 |
calcBBoxSize();
|
insilmaril@0
|
83 |
positionBBox();
|
insilmaril@0
|
84 |
return newfo;
|
insilmaril@0
|
85 |
}
|
insilmaril@0
|
86 |
|
insilmaril@0
|
87 |
void FlagRowObj::positionBBox()
|
insilmaril@0
|
88 |
{
|
insilmaril@173
|
89 |
bbox.moveTopLeft(absPos );
|
insilmaril@173
|
90 |
clickBox.moveTopLeft(absPos );
|
insilmaril@0
|
91 |
}
|
insilmaril@0
|
92 |
|
insilmaril@0
|
93 |
void FlagRowObj::calcBBoxSize()
|
insilmaril@0
|
94 |
{
|
insilmaril@408
|
95 |
QSizeF size(0,0);
|
insilmaril@408
|
96 |
QSizeF boxsize(0,0);
|
insilmaril@406
|
97 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@0
|
98 |
{
|
insilmaril@406
|
99 |
size=flag.at(i)->getSize();
|
insilmaril@0
|
100 |
// add widths
|
insilmaril@0
|
101 |
boxsize.setWidth(boxsize.width() + size.width() );
|
insilmaril@0
|
102 |
// maximize height
|
insilmaril@0
|
103 |
if (size.height() > boxsize.height() )
|
insilmaril@0
|
104 |
boxsize.setHeight(size.height() );
|
insilmaril@0
|
105 |
}
|
insilmaril@173
|
106 |
bbox.setSize (boxsize);
|
insilmaril@173
|
107 |
clickBox.setSize (boxsize);
|
insilmaril@0
|
108 |
}
|
insilmaril@0
|
109 |
|
insilmaril@408
|
110 |
QString FlagRowObj::getFlagName (const QPointF &p)
|
insilmaril@0
|
111 |
{
|
insilmaril@173
|
112 |
if (!inBox (p)) return "";
|
insilmaril@406
|
113 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@406
|
114 |
if (flag.at(i)->inBox (p)) return flag.at(i)->getName();
|
insilmaril@0
|
115 |
return "";
|
insilmaril@0
|
116 |
|
insilmaril@0
|
117 |
|
insilmaril@0
|
118 |
}
|
insilmaril@0
|
119 |
|
insilmaril@0
|
120 |
bool FlagRowObj::isActive (const QString &foname)
|
insilmaril@0
|
121 |
{
|
insilmaril@0
|
122 |
FlagObj *fo=findFlag (foname);
|
insilmaril@236
|
123 |
if (parentRow && fo)
|
insilmaril@236
|
124 |
return fo->isActive();
|
insilmaril@236
|
125 |
else
|
insilmaril@0
|
126 |
if (fo) return true;
|
insilmaril@0
|
127 |
return false;
|
insilmaril@0
|
128 |
}
|
insilmaril@0
|
129 |
|
insilmaril@103
|
130 |
void FlagRowObj::toggle (const QString &foname, bool exclusive)
|
insilmaril@0
|
131 |
{
|
insilmaril@0
|
132 |
FlagObj *fo=findFlag (foname);
|
insilmaril@0
|
133 |
if (fo)
|
insilmaril@0
|
134 |
{
|
insilmaril@0
|
135 |
// FlagObj is here, it will be active, too.
|
insilmaril@0
|
136 |
// Deactivate it by removing it from this row.
|
insilmaril@0
|
137 |
flag.remove (fo);
|
insilmaril@408
|
138 |
delete (fo);
|
insilmaril@0
|
139 |
} else
|
insilmaril@0
|
140 |
{
|
insilmaril@0
|
141 |
// FlagObj is not present in this row.
|
insilmaril@0
|
142 |
// Copy it from parentRow
|
insilmaril@0
|
143 |
fo=parentRow->findFlag (foname);
|
insilmaril@0
|
144 |
if (fo)
|
insilmaril@0
|
145 |
{
|
insilmaril@0
|
146 |
fo=addFlag (fo);
|
insilmaril@0
|
147 |
fo->activate();
|
insilmaril@103
|
148 |
if (exclusive)
|
insilmaril@103
|
149 |
{
|
insilmaril@103
|
150 |
deactivateGroup (fo);
|
insilmaril@103
|
151 |
updateToolbar();
|
insilmaril@103
|
152 |
}
|
insilmaril@0
|
153 |
} else
|
insilmaril@0
|
154 |
qWarning ("FlagRowObj ("+name+")::toggle ("+foname+") failed - could not find it in parentRow");
|
insilmaril@0
|
155 |
}
|
insilmaril@0
|
156 |
calcBBoxSize();
|
insilmaril@0
|
157 |
positionBBox();
|
insilmaril@0
|
158 |
}
|
insilmaril@0
|
159 |
|
insilmaril@0
|
160 |
void FlagRowObj::activate (const QString &foname)
|
insilmaril@0
|
161 |
{
|
insilmaril@311
|
162 |
// Note: "activate" is also called during loading of a map
|
insilmaril@311
|
163 |
// Here we do not check for exclusive flags!
|
insilmaril@0
|
164 |
FlagObj *fo=findFlag (foname);
|
insilmaril@0
|
165 |
if (parentRow)
|
insilmaril@0
|
166 |
{
|
insilmaril@0
|
167 |
if (!fo)
|
insilmaril@0
|
168 |
{
|
insilmaril@0
|
169 |
// FlagObj is not present in this row.
|
insilmaril@0
|
170 |
// Copy it from parentRow and activate there
|
insilmaril@0
|
171 |
fo=parentRow->findFlag (foname);
|
insilmaril@0
|
172 |
if (fo)
|
insilmaril@0
|
173 |
{
|
insilmaril@0
|
174 |
fo=addFlag (fo);
|
insilmaril@0
|
175 |
fo->activate();
|
insilmaril@256
|
176 |
if (showFlags)
|
insilmaril@256
|
177 |
fo->setVisibility (visible);
|
insilmaril@256
|
178 |
else
|
insilmaril@256
|
179 |
fo->setVisibility (false);
|
insilmaril@0
|
180 |
calcBBoxSize();
|
insilmaril@0
|
181 |
} else
|
insilmaril@0
|
182 |
qWarning ("FlagRowObj ("+name+")::activate ("+foname+") failed - could not find it in parentRow");
|
insilmaril@0
|
183 |
}
|
insilmaril@0
|
184 |
} else
|
insilmaril@0
|
185 |
{
|
insilmaril@0
|
186 |
// I am the parentRow, mark flag as used
|
insilmaril@0
|
187 |
if (fo)
|
insilmaril@0
|
188 |
{
|
insilmaril@0
|
189 |
fo->setUsed(true);
|
insilmaril@0
|
190 |
fo->activate();
|
insilmaril@0
|
191 |
}
|
insilmaril@0
|
192 |
else
|
insilmaril@0
|
193 |
qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
|
insilmaril@0
|
194 |
}
|
insilmaril@0
|
195 |
}
|
insilmaril@0
|
196 |
|
insilmaril@103
|
197 |
|
insilmaril@0
|
198 |
void FlagRowObj::deactivate (const QString &foname)
|
insilmaril@0
|
199 |
{
|
insilmaril@0
|
200 |
FlagObj *fo=findFlag (foname);
|
insilmaril@408
|
201 |
if (fo)
|
insilmaril@408
|
202 |
{
|
insilmaril@408
|
203 |
flag.remove(fo);
|
insilmaril@408
|
204 |
delete (fo);
|
insilmaril@408
|
205 |
}
|
insilmaril@0
|
206 |
calcBBoxSize();
|
insilmaril@0
|
207 |
positionBBox();
|
insilmaril@0
|
208 |
}
|
insilmaril@0
|
209 |
|
insilmaril@0
|
210 |
void FlagRowObj::deactivateAll ()
|
insilmaril@0
|
211 |
{
|
insilmaril@0
|
212 |
if (!parentRow)
|
insilmaril@0
|
213 |
{
|
insilmaril@406
|
214 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@408
|
215 |
if (flag.at(i)->isActive()) flag.at(i)->deactivate();
|
insilmaril@0
|
216 |
} else
|
insilmaril@0
|
217 |
qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
|
insilmaril@0
|
218 |
}
|
insilmaril@0
|
219 |
|
insilmaril@103
|
220 |
void FlagRowObj::deactivateGroup (FlagObj *keepfo)
|
insilmaril@103
|
221 |
{
|
insilmaril@103
|
222 |
// deactivate all flags in keepof, but keep keepfo [sic!]
|
insilmaril@103
|
223 |
if (keepfo)
|
insilmaril@103
|
224 |
{
|
insilmaril@311
|
225 |
QString g=keepfo->getGroup();
|
insilmaril@311
|
226 |
if (g!="undefined")
|
insilmaril@311
|
227 |
{
|
insilmaril@406
|
228 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@406
|
229 |
if (g==flag.at(i)->getGroup() && keepfo!=flag.at(i))
|
insilmaril@408
|
230 |
{
|
insilmaril@411
|
231 |
FlagObj *fo=flag.at(i);
|
insilmaril@411
|
232 |
flag.remove (fo);
|
insilmaril@411
|
233 |
delete (fo);
|
insilmaril@408
|
234 |
}
|
insilmaril@311
|
235 |
}
|
insilmaril@103
|
236 |
}
|
insilmaril@103
|
237 |
}
|
insilmaril@103
|
238 |
|
insilmaril@366
|
239 |
void FlagRowObj::setToolBar(QToolBar *tb)
|
insilmaril@366
|
240 |
{
|
insilmaril@366
|
241 |
toolbar=tb;
|
insilmaril@366
|
242 |
}
|
insilmaril@366
|
243 |
|
insilmaril@0
|
244 |
void FlagRowObj::setEnabled (bool b)
|
insilmaril@0
|
245 |
{
|
insilmaril@366
|
246 |
if (toolbar)
|
insilmaril@0
|
247 |
{
|
insilmaril@366
|
248 |
toolbar->setEnabled (b);
|
insilmaril@0
|
249 |
}
|
insilmaril@0
|
250 |
}
|
insilmaril@0
|
251 |
|
insilmaril@256
|
252 |
void FlagRowObj::setShowFlags (bool b)
|
insilmaril@256
|
253 |
{
|
insilmaril@256
|
254 |
showFlags=b;
|
insilmaril@256
|
255 |
}
|
insilmaril@256
|
256 |
|
insilmaril@0
|
257 |
void FlagRowObj::resetUsedCounter()
|
insilmaril@0
|
258 |
{
|
insilmaril@406
|
259 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@406
|
260 |
flag.at(i)->setUsed (false);
|
insilmaril@0
|
261 |
}
|
insilmaril@0
|
262 |
|
insilmaril@0
|
263 |
QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
|
insilmaril@0
|
264 |
{
|
insilmaril@0
|
265 |
// Build xml string
|
insilmaril@0
|
266 |
QString s;
|
insilmaril@0
|
267 |
if (parentRow)
|
insilmaril@406
|
268 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@0
|
269 |
{
|
insilmaril@0
|
270 |
// save flag to xml, if flag is set
|
insilmaril@406
|
271 |
s+=valueElement("standardflag",flag.at(i)->getName() );
|
insilmaril@0
|
272 |
|
insilmaril@0
|
273 |
// and tell parentRow, that this flag is used
|
insilmaril@406
|
274 |
parentRow->activate(flag.at(i)->getName() );
|
insilmaril@0
|
275 |
}
|
insilmaril@0
|
276 |
else
|
insilmaril@0
|
277 |
// Save icons to dir, if verbose is set (xml export)
|
insilmaril@0
|
278 |
// and I am a parentRow
|
insilmaril@0
|
279 |
// and this flag is really used somewhere
|
insilmaril@0
|
280 |
if (writeflags)
|
insilmaril@406
|
281 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@406
|
282 |
if (flag.at(i)->isUsed()) flag.at(i)->saveToDir (tmpdir,prefix);
|
insilmaril@0
|
283 |
return s;
|
insilmaril@0
|
284 |
|
insilmaril@0
|
285 |
}
|
insilmaril@0
|
286 |
|
insilmaril@0
|
287 |
void FlagRowObj::setName (const QString &n)
|
insilmaril@0
|
288 |
{
|
insilmaril@0
|
289 |
name=n;
|
insilmaril@0
|
290 |
}
|
insilmaril@0
|
291 |
|
insilmaril@103
|
292 |
void FlagRowObj::updateToolbar()
|
insilmaril@0
|
293 |
{
|
insilmaril@0
|
294 |
if (parentRow)
|
insilmaril@0
|
295 |
{
|
insilmaril@0
|
296 |
// We are just a branch, not the toolbar default
|
insilmaril@366
|
297 |
// but state has to be copied from ourselves to parentrow!
|
insilmaril@0
|
298 |
parentRow->deactivateAll();
|
insilmaril@0
|
299 |
// In parentRow activate all existing (==active) flags
|
insilmaril@406
|
300 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@406
|
301 |
parentRow->activate(flag.at(i)->getName());
|
insilmaril@103
|
302 |
parentRow->updateToolbar();
|
insilmaril@0
|
303 |
} else
|
insilmaril@0
|
304 |
{
|
insilmaril@0
|
305 |
// We are the toolbar default
|
insilmaril@366
|
306 |
if (toolbar)
|
insilmaril@366
|
307 |
{
|
insilmaril@366
|
308 |
// Update state of actions in toolbar
|
insilmaril@406
|
309 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@406
|
310 |
flag.at(i)->updateAction();
|
insilmaril@366
|
311 |
}
|
insilmaril@0
|
312 |
}
|
insilmaril@0
|
313 |
}
|
insilmaril@0
|
314 |
|
insilmaril@0
|
315 |
FlagObj* FlagRowObj::findFlag (const QString &name)
|
insilmaril@0
|
316 |
{
|
insilmaril@406
|
317 |
for (int i=0; i<flag.size(); ++i)
|
insilmaril@406
|
318 |
if (flag.at(i)->getName()==name) return flag.at(i);
|
insilmaril@0
|
319 |
return NULL;
|
insilmaril@0
|
320 |
}
|
insilmaril@0
|
321 |
|