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