insilmaril@0: #include "flagrowobj.h" insilmaril@0: insilmaril@0: ///////////////////////////////////////////////////////////////// insilmaril@0: // FlagRowObj insilmaril@0: ///////////////////////////////////////////////////////////////// insilmaril@0: FlagRowObj::FlagRowObj() insilmaril@0: { insilmaril@166: // cout << "Const FlagRowObj ()\n"; insilmaril@0: init (); insilmaril@0: } insilmaril@0: insilmaril@0: FlagRowObj::FlagRowObj(QCanvas* 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@256: 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@173: bbox.moveTopLeft(absPos ); insilmaril@173: 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@173: bbox.setSize (boxsize); insilmaril@173: clickBox.setSize (boxsize); insilmaril@0: } insilmaril@0: insilmaril@0: QString FlagRowObj::getFlagName (const QPoint &p) insilmaril@0: { insilmaril@173: if (!inBox (p)) return ""; insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@173: 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@236: if (parentRow && fo) insilmaril@236: return fo->isActive(); insilmaril@236: else insilmaril@0: if (fo) return true; insilmaril@0: return false; insilmaril@0: } insilmaril@0: insilmaril@103: 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@103: if (exclusive) insilmaril@103: { insilmaril@103: deactivateGroup (fo); insilmaril@103: updateToolbar(); insilmaril@103: } 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@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@256: if (showFlags) insilmaril@256: fo->setVisibility (visible); insilmaril@256: else insilmaril@256: 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@103: 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@103: void FlagRowObj::deactivateGroup (FlagObj *keepfo) insilmaril@103: { insilmaril@103: // deactivate all flags in keepof, but keep keepfo [sic!] insilmaril@103: if (keepfo) insilmaril@103: { insilmaril@103: FlagObj *fo; insilmaril@103: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@103: if (keepfo->getGroup()==fo->getGroup() && keepfo!=fo) insilmaril@103: flag.remove(fo); insilmaril@103: } insilmaril@103: } insilmaril@103: insilmaril@0: void FlagRowObj::setEnabled (bool b) insilmaril@0: { insilmaril@0: // If we have no parent, we are the default FlagRowObj insilmaril@0: // and have QToolbarButtons insilmaril@0: if (!parentRow) insilmaril@0: { insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@0: fo->setEnabled (b); insilmaril@0: } insilmaril@0: } insilmaril@0: insilmaril@256: void FlagRowObj::setShowFlags (bool b) insilmaril@256: { insilmaril@256: showFlags=b; insilmaril@256: } insilmaril@256: 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@0: void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n) insilmaril@0: { insilmaril@103: //Only make toolbar for the parentrow, not each row in branches insilmaril@0: if (!parentRow) insilmaril@0: { insilmaril@0: // create bar and buttons insilmaril@0: QToolBar* tb = new QToolBar( w); insilmaril@0: tb->setLabel (n); insilmaril@0: QAction *a; insilmaril@0: FlagObj *fo; insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@0: { insilmaril@0: a=new QAction ( insilmaril@0: fo->getToolTip(), insilmaril@0: fo->getPixmap(), insilmaril@0: fo->getName(), insilmaril@0: 0, insilmaril@0: w, insilmaril@0: fo->getName() insilmaril@0: ); insilmaril@0: a->setToggleAction(true); insilmaril@0: // FIXME should not be enabled by default, later in updateToolbar insilmaril@0: a->setEnabled(true); insilmaril@0: a->addTo (tb); insilmaril@0: fo->setButton (a); insilmaril@0: connect(a, SIGNAL( activated() ), insilmaril@0: w, SLOT( standardFlagChanged() ) ); insilmaril@0: } insilmaril@0: } else insilmaril@166: qWarning ("FlagRowObj::makeToolbar must not be called for ordinary rows"); insilmaril@0: } insilmaril@0: insilmaril@103: 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@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@103: parentRow->updateToolbar(); insilmaril@0: } else insilmaril@0: { insilmaril@0: // We are the toolbar default insilmaril@0: for (fo=flag.first();fo; fo=flag.next() ) insilmaril@0: fo->updateButton(); 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: