insilmaril@0: #include "flagrowobj.h" insilmaril@0: insilmaril@3: #include insilmaril@3: insilmaril@0: ///////////////////////////////////////////////////////////////// insilmaril@0: // FlagRowObj insilmaril@0: ///////////////////////////////////////////////////////////////// insilmaril@0: FlagRowObj::FlagRowObj() insilmaril@0: { insilmaril@2: // cout << "Const FlagRowObj ()\n"; insilmaril@0: init (); insilmaril@0: } insilmaril@0: insilmaril@2: FlagRowObj::FlagRowObj(Q3Canvas* c):MapObj(c) insilmaril@0: { insilmaril@0: // cout << "Const FlagRowObj\n"; insilmaril@0: init (); insilmaril@0: } insilmaril@0: insilmaril@0: FlagRowObj::~FlagRowObj() insilmaril@0: { insilmaril@0: // cout << "Destr FlagRowObj\n"; insilmaril@0: flag.clear(); insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::init () insilmaril@0: { insilmaril@0: flag.setAutoDelete (true); insilmaril@0: parentRow=NULL; insilmaril@2: showFlags=true; insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::copy (FlagRowObj* other) insilmaril@0: { insilmaril@0: MapObj::copy(other); insilmaril@0: parentRow=other->parentRow; insilmaril@0: flag.clear(); insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=other->flag.first(); fo; fo=other->flag.next() ) insilmaril@0: addFlag (fo); insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::clone (FlagRowObj* pr) insilmaril@0: { insilmaril@0: // Difference to copy: insilmaril@0: // We don't copy the flags here, they insilmaril@0: // are created on the fly by toggle and activate insilmaril@0: // This saves lots of canvas objects. insilmaril@0: MapObj::copy(pr); insilmaril@0: flag.clear(); insilmaril@0: parentRow=pr; insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::move(double x, double y) insilmaril@0: { insilmaril@0: MapObj::move(x,y); insilmaril@0: int dx=0; insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=flag.first(); fo; fo=flag.next() ) insilmaril@0: { insilmaril@0: fo->move(x+dx,y); insilmaril@0: dx+=QSize(fo->getSize() ).width(); insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::moveBy(double x, double y) insilmaril@0: { insilmaril@0: move (x+absPos.x(),y+absPos.y() ); insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::setVisibility (bool v) insilmaril@0: { insilmaril@0: MapObj::setVisibility(v); insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=flag.first(); fo; fo=flag.next() ) insilmaril@0: fo->setVisibility (v); insilmaril@0: } insilmaril@0: insilmaril@0: FlagObj* FlagRowObj::addFlag (FlagObj *fo) insilmaril@0: { insilmaril@0: FlagObj *newfo=new FlagObj (canvas); insilmaril@0: newfo->move (absPos.x() + bbox.width(), absPos.y() ); insilmaril@0: newfo->copy (fo); // create a deep copy of fo insilmaril@0: flag.append(newfo); insilmaril@0: calcBBoxSize(); insilmaril@0: positionBBox(); insilmaril@0: return newfo; insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::positionBBox() insilmaril@0: { insilmaril@2: bbox.moveTopLeft(absPos ); insilmaril@2: clickBox.moveTopLeft(absPos ); insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::calcBBoxSize() insilmaril@0: { insilmaril@0: QSize size(0,0); insilmaril@0: QSize boxsize(0,0); insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=flag.first(); fo; fo=flag.next() ) insilmaril@0: { insilmaril@0: size=fo->getSize(); insilmaril@0: // add widths insilmaril@0: boxsize.setWidth(boxsize.width() + size.width() ); insilmaril@0: // maximize height insilmaril@0: if (size.height() > boxsize.height() ) insilmaril@0: boxsize.setHeight(size.height() ); insilmaril@0: } insilmaril@2: bbox.setSize (boxsize); insilmaril@2: clickBox.setSize (boxsize); insilmaril@0: } insilmaril@0: insilmaril@0: QString FlagRowObj::getFlagName (const QPoint &p) insilmaril@0: { insilmaril@2: if (!inBox (p)) return ""; insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@2: if (fo->inBox (p)) return fo->getName(); insilmaril@0: return ""; insilmaril@0: insilmaril@0: insilmaril@0: } insilmaril@0: insilmaril@0: bool FlagRowObj::isActive (const QString &foname) insilmaril@0: { insilmaril@0: FlagObj *fo=findFlag (foname); insilmaril@2: if (parentRow && fo) insilmaril@2: return fo->isActive(); insilmaril@2: else insilmaril@0: if (fo) return true; insilmaril@0: return false; insilmaril@0: } insilmaril@0: insilmaril@2: void FlagRowObj::toggle (const QString &foname, bool exclusive) insilmaril@0: { insilmaril@0: FlagObj *fo=findFlag (foname); insilmaril@0: if (fo) insilmaril@0: { insilmaril@0: // FlagObj is here, it will be active, too. insilmaril@0: // Deactivate it by removing it from this row. insilmaril@0: flag.remove (fo); insilmaril@0: } else insilmaril@0: { insilmaril@0: // FlagObj is not present in this row. insilmaril@0: // Copy it from parentRow insilmaril@0: fo=parentRow->findFlag (foname); insilmaril@0: if (fo) insilmaril@0: { insilmaril@0: fo=addFlag (fo); insilmaril@0: fo->activate(); insilmaril@2: if (exclusive) insilmaril@2: { insilmaril@2: deactivateGroup (fo); insilmaril@2: updateToolbar(); insilmaril@2: } insilmaril@0: } else insilmaril@0: qWarning ("FlagRowObj ("+name+")::toggle ("+foname+") failed - could not find it in parentRow"); insilmaril@0: } insilmaril@0: calcBBoxSize(); insilmaril@0: positionBBox(); insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::activate (const QString &foname) insilmaril@0: { insilmaril@2: // Note: "activate" is also called during loading of a map insilmaril@2: // Here we do not check for exclusive flags! insilmaril@0: FlagObj *fo=findFlag (foname); insilmaril@0: if (parentRow) insilmaril@0: { insilmaril@0: if (!fo) insilmaril@0: { insilmaril@0: // FlagObj is not present in this row. insilmaril@0: // Copy it from parentRow and activate there insilmaril@0: fo=parentRow->findFlag (foname); insilmaril@0: if (fo) insilmaril@0: { insilmaril@0: fo=addFlag (fo); insilmaril@0: fo->activate(); insilmaril@2: if (showFlags) insilmaril@2: fo->setVisibility (visible); insilmaril@2: else insilmaril@2: fo->setVisibility (false); insilmaril@0: calcBBoxSize(); insilmaril@0: } else insilmaril@0: qWarning ("FlagRowObj ("+name+")::activate ("+foname+") failed - could not find it in parentRow"); insilmaril@0: } insilmaril@0: } else insilmaril@0: { insilmaril@0: // I am the parentRow, mark flag as used insilmaril@0: if (fo) insilmaril@0: { insilmaril@0: fo->setUsed(true); insilmaril@0: fo->activate(); insilmaril@0: } insilmaril@0: else insilmaril@0: qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow"); insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@2: insilmaril@0: void FlagRowObj::deactivate (const QString &foname) insilmaril@0: { insilmaril@0: FlagObj *fo=findFlag (foname); insilmaril@0: if (fo) flag.remove(fo); insilmaril@0: calcBBoxSize(); insilmaril@0: positionBBox(); insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::deactivateAll () insilmaril@0: { insilmaril@0: if (!parentRow) insilmaril@0: { insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@0: fo->deactivate(); insilmaril@0: } else insilmaril@0: qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows"); insilmaril@0: } insilmaril@0: insilmaril@2: void FlagRowObj::deactivateGroup (FlagObj *keepfo) insilmaril@2: { insilmaril@2: // deactivate all flags in keepof, but keep keepfo [sic!] insilmaril@2: if (keepfo) insilmaril@2: { insilmaril@2: QString g=keepfo->getGroup(); insilmaril@2: if (g!="undefined") insilmaril@2: { insilmaril@2: FlagObj *fo; insilmaril@2: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@2: if (g==fo->getGroup() && keepfo!=fo) insilmaril@2: flag.remove(fo); insilmaril@2: } insilmaril@2: } insilmaril@2: } insilmaril@2: insilmaril@3: void FlagRowObj::setToolBar(QToolBar *tb) insilmaril@3: { insilmaril@3: toolbar=tb; insilmaril@3: } insilmaril@3: insilmaril@0: void FlagRowObj::setEnabled (bool b) insilmaril@0: { insilmaril@3: if (toolbar) insilmaril@0: { insilmaril@3: toolbar->setEnabled (b); insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@2: void FlagRowObj::setShowFlags (bool b) insilmaril@2: { insilmaril@2: showFlags=b; insilmaril@2: } insilmaril@2: insilmaril@0: void FlagRowObj::resetUsedCounter() insilmaril@0: { insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@0: fo->setUsed (false); insilmaril@0: } insilmaril@0: insilmaril@0: QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags) insilmaril@0: { insilmaril@0: // Build xml string insilmaril@0: QString s; insilmaril@0: FlagObj *fo; insilmaril@0: if (parentRow) insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@0: { insilmaril@0: // save flag to xml, if flag is set insilmaril@0: s+=valueElement("standardflag",fo->getName() ); insilmaril@0: insilmaril@0: // and tell parentRow, that this flag is used insilmaril@0: parentRow->activate(fo->getName() ); insilmaril@0: } insilmaril@0: else insilmaril@0: // Save icons to dir, if verbose is set (xml export) insilmaril@0: // and I am a parentRow insilmaril@0: // and this flag is really used somewhere insilmaril@0: if (writeflags) insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@0: if (fo->isUsed()) fo->saveToDir (tmpdir,prefix); insilmaril@0: return s; insilmaril@0: insilmaril@0: } insilmaril@0: insilmaril@0: void FlagRowObj::setName (const QString &n) insilmaril@0: { insilmaril@0: name=n; insilmaril@0: } insilmaril@0: insilmaril@2: void FlagRowObj::updateToolbar() insilmaril@0: { insilmaril@0: FlagObj *fo; insilmaril@0: if (parentRow) insilmaril@0: { insilmaril@0: // We are just a branch, not the toolbar default insilmaril@3: // but state has to be copied from ourselves to parentrow! insilmaril@0: parentRow->deactivateAll(); insilmaril@0: // In parentRow activate all existing (==active) flags insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@0: parentRow->activate(fo->getName()); insilmaril@2: parentRow->updateToolbar(); insilmaril@0: } else insilmaril@0: { insilmaril@0: // We are the toolbar default insilmaril@3: if (toolbar) insilmaril@3: { insilmaril@3: // Update state of actions in toolbar insilmaril@3: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@3: fo->updateAction(); insilmaril@3: } insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@0: FlagObj* FlagRowObj::findFlag (const QString &name) insilmaril@0: { insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@0: { insilmaril@0: if (fo->getName()==name) return fo; insilmaril@0: } insilmaril@0: return NULL; insilmaril@0: } insilmaril@0: