1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/flagrowobj.cpp Wed May 04 20:35:39 2005 +0000
1.3 @@ -0,0 +1,322 @@
1.4 +#include "flagrowobj.h"
1.5 +
1.6 +/////////////////////////////////////////////////////////////////
1.7 +// FlagRowObj
1.8 +/////////////////////////////////////////////////////////////////
1.9 +FlagRowObj::FlagRowObj()
1.10 +{
1.11 + cout << "Const FlagRowObj ()\n";
1.12 + init ();
1.13 +}
1.14 +
1.15 +FlagRowObj::FlagRowObj(QCanvas* c):MapObj(c)
1.16 +{
1.17 +// cout << "Const FlagRowObj\n";
1.18 + init ();
1.19 +}
1.20 +
1.21 +FlagRowObj::~FlagRowObj()
1.22 +{
1.23 +// cout << "Destr FlagRowObj\n";
1.24 + flag.clear();
1.25 +}
1.26 +
1.27 +void FlagRowObj::init ()
1.28 +{
1.29 + flag.setAutoDelete (true);
1.30 + parentRow=NULL;
1.31 +}
1.32 +
1.33 +void FlagRowObj::copy (FlagRowObj* other)
1.34 +{
1.35 + MapObj::copy(other);
1.36 + parentRow=other->parentRow;
1.37 + flag.clear();
1.38 + FlagObj *fo;
1.39 + for (fo=other->flag.first(); fo; fo=other->flag.next() )
1.40 + addFlag (fo);
1.41 +}
1.42 +
1.43 +void FlagRowObj::clone (FlagRowObj* pr)
1.44 +{
1.45 + // Difference to copy:
1.46 + // We don't copy the flags here, they
1.47 + // are created on the fly by toggle and activate
1.48 + // This saves lots of canvas objects.
1.49 + MapObj::copy(pr);
1.50 + flag.clear();
1.51 + parentRow=pr;
1.52 +}
1.53 +
1.54 +void FlagRowObj::move(double x, double y)
1.55 +{
1.56 + MapObj::move(x,y);
1.57 + int dx=0;
1.58 + FlagObj *fo;
1.59 + for (fo=flag.first(); fo; fo=flag.next() )
1.60 + {
1.61 + fo->move(x+dx,y);
1.62 + dx+=QSize(fo->getSize() ).width();
1.63 + }
1.64 +}
1.65 +
1.66 +void FlagRowObj::moveBy(double x, double y)
1.67 +{
1.68 + move (x+absPos.x(),y+absPos.y() );
1.69 +}
1.70 +
1.71 +void FlagRowObj::setVisibility (bool v)
1.72 +{
1.73 + MapObj::setVisibility(v);
1.74 + FlagObj *fo;
1.75 + for (fo=flag.first(); fo; fo=flag.next() )
1.76 + fo->setVisibility (v);
1.77 +}
1.78 +
1.79 +FlagObj* FlagRowObj::addFlag (FlagObj *fo)
1.80 +{
1.81 + FlagObj *newfo=new FlagObj (canvas);
1.82 + newfo->move (absPos.x() + bbox.width(), absPos.y() );
1.83 + newfo->copy (fo); // create a deep copy of fo
1.84 + flag.append(newfo);
1.85 + calcBBoxSize();
1.86 + positionBBox();
1.87 + return newfo;
1.88 +}
1.89 +
1.90 +void FlagRowObj::positionBBox()
1.91 +{
1.92 + bbox.setX (absPos.x() );
1.93 + bbox.setY (absPos.y() );
1.94 +}
1.95 +
1.96 +void FlagRowObj::calcBBoxSize()
1.97 +{
1.98 + QSize size(0,0);
1.99 + QSize boxsize(0,0);
1.100 + FlagObj *fo;
1.101 + for (fo=flag.first(); fo; fo=flag.next() )
1.102 + {
1.103 + size=fo->getSize();
1.104 + // add widths
1.105 + boxsize.setWidth(boxsize.width() + size.width() );
1.106 + // maximize height
1.107 + if (size.height() > boxsize.height() )
1.108 + boxsize.setHeight(size.height() );
1.109 + }
1.110 + bbox.setSize (QSize(boxsize.width(), boxsize.height() ));
1.111 +}
1.112 +
1.113 +QString FlagRowObj::getFlagName (const QPoint &p)
1.114 +{
1.115 + if (!inBBox (p)) return "";
1.116 + FlagObj *fo;
1.117 + for (fo=flag.first();fo; fo=flag.next() )
1.118 + if (fo->inBBox (p)) return fo->getName();
1.119 + return "";
1.120 +
1.121 +
1.122 +}
1.123 +
1.124 +bool FlagRowObj::isActive (const QString &foname)
1.125 +{
1.126 + FlagObj *fo=findFlag (foname);
1.127 + if (parentRow)
1.128 + {
1.129 + if (fo)
1.130 + return fo->isActive();
1.131 + else
1.132 + qWarning ("FlagRowObj::isActive of "+name+" couldn't find "+foname);
1.133 +
1.134 + } else
1.135 + if (fo) return true;
1.136 + return false;
1.137 +}
1.138 +
1.139 +void FlagRowObj::toggle (const QString &foname)
1.140 +{
1.141 + FlagObj *fo=findFlag (foname);
1.142 + if (fo)
1.143 + {
1.144 + // FlagObj is here, it will be active, too.
1.145 + // Deactivate it by removing it from this row.
1.146 + flag.remove (fo);
1.147 + } else
1.148 + {
1.149 + // FlagObj is not present in this row.
1.150 + // Copy it from parentRow
1.151 + fo=parentRow->findFlag (foname);
1.152 + if (fo)
1.153 + {
1.154 + fo=addFlag (fo);
1.155 + fo->activate();
1.156 + } else
1.157 + qWarning ("FlagRowObj ("+name+")::toggle ("+foname+") failed - could not find it in parentRow");
1.158 + }
1.159 + calcBBoxSize();
1.160 + positionBBox();
1.161 +}
1.162 +
1.163 +void FlagRowObj::activate (const QString &foname)
1.164 +{
1.165 + FlagObj *fo=findFlag (foname);
1.166 + if (parentRow)
1.167 + {
1.168 + if (!fo)
1.169 + {
1.170 + // FlagObj is not present in this row.
1.171 + // Copy it from parentRow and activate there
1.172 + fo=parentRow->findFlag (foname);
1.173 + if (fo)
1.174 + {
1.175 + fo=addFlag (fo);
1.176 + fo->activate();
1.177 + fo->setVisibility (visible);
1.178 + calcBBoxSize();
1.179 + positionBBox();
1.180 + } else
1.181 + qWarning ("FlagRowObj ("+name+")::activate ("+foname+") failed - could not find it in parentRow");
1.182 + }
1.183 + } else
1.184 + {
1.185 + // I am the parentRow, mark flag as used
1.186 + if (fo)
1.187 + {
1.188 + fo->setUsed(true);
1.189 + fo->activate();
1.190 + }
1.191 + else
1.192 + qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
1.193 + }
1.194 +}
1.195 +
1.196 +void FlagRowObj::deactivate (const QString &foname)
1.197 +{
1.198 + FlagObj *fo=findFlag (foname);
1.199 + if (fo) flag.remove(fo);
1.200 + calcBBoxSize();
1.201 + positionBBox();
1.202 +}
1.203 +
1.204 +void FlagRowObj::deactivateAll ()
1.205 +{
1.206 + if (!parentRow)
1.207 + {
1.208 + FlagObj *fo;
1.209 + for (fo=flag.first();fo; fo=flag.next() )
1.210 + {
1.211 + fo->deactivate();
1.212 + }
1.213 + } else
1.214 + qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
1.215 +}
1.216 +
1.217 +void FlagRowObj::setEnabled (bool b)
1.218 +{
1.219 + // If we have no parent, we are the default FlagRowObj
1.220 + // and have QToolbarButtons
1.221 + if (!parentRow)
1.222 + {
1.223 + FlagObj *fo;
1.224 + for (fo=flag.first();fo; fo=flag.next() )
1.225 + fo->setEnabled (b);
1.226 + }
1.227 +}
1.228 +
1.229 +void FlagRowObj::resetUsedCounter()
1.230 +{
1.231 + FlagObj *fo;
1.232 + for (fo=flag.first();fo; fo=flag.next() )
1.233 + fo->setUsed (false);
1.234 +}
1.235 +
1.236 +QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
1.237 +{
1.238 + // Build xml string
1.239 + QString s;
1.240 + FlagObj *fo;
1.241 + if (parentRow)
1.242 + for (fo=flag.first();fo; fo=flag.next() )
1.243 + {
1.244 + // save flag to xml, if flag is set
1.245 + s+=valueElement("standardflag",fo->getName() );
1.246 +
1.247 + // and tell parentRow, that this flag is used
1.248 + parentRow->activate(fo->getName() );
1.249 + }
1.250 + else
1.251 + // Save icons to dir, if verbose is set (xml export)
1.252 + // and I am a parentRow
1.253 + // and this flag is really used somewhere
1.254 + if (writeflags)
1.255 + for (fo=flag.first();fo; fo=flag.next() )
1.256 + if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
1.257 + return s;
1.258 +
1.259 +}
1.260 +
1.261 +void FlagRowObj::setName (const QString &n)
1.262 +{
1.263 + name=n;
1.264 +}
1.265 +
1.266 +void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
1.267 +{
1.268 + if (!parentRow)
1.269 + {
1.270 + // create bar and buttons
1.271 + QToolBar* tb = new QToolBar( w);
1.272 + tb->setLabel (n);
1.273 + QAction *a;
1.274 + FlagObj *fo;
1.275 + for (fo=flag.first();fo; fo=flag.next() )
1.276 + {
1.277 + a=new QAction (
1.278 + fo->getToolTip(),
1.279 + fo->getPixmap(),
1.280 + fo->getName(),
1.281 + 0,
1.282 + w,
1.283 + fo->getName()
1.284 + );
1.285 + a->setToggleAction(true);
1.286 + // FIXME should not be enabled by default, later in updateToolbar
1.287 + a->setEnabled(true);
1.288 + a->addTo (tb);
1.289 + fo->setButton (a);
1.290 + connect(a, SIGNAL( activated() ),
1.291 + w, SLOT( standardFlagChanged() ) );
1.292 + }
1.293 + } else
1.294 + qWarning ("FlagRowObj::makeToolbar mustn't be called for ordinary rows");
1.295 +}
1.296 +
1.297 +void FlagRowObj::updateToolBar()
1.298 +{
1.299 + FlagObj *fo;
1.300 + if (parentRow)
1.301 + {
1.302 + // We are just a branch, not the toolbar default
1.303 + parentRow->deactivateAll();
1.304 + // In parentRow activate all existing (==active) flags
1.305 + for (fo=flag.first();fo; fo=flag.next() )
1.306 + parentRow->activate(fo->getName());
1.307 + parentRow->updateToolBar();
1.308 + } else
1.309 + {
1.310 + // We are the toolbar default
1.311 + for (fo=flag.first();fo; fo=flag.next() )
1.312 + fo->updateButton();
1.313 + }
1.314 +}
1.315 +
1.316 +FlagObj* FlagRowObj::findFlag (const QString &name)
1.317 +{
1.318 + FlagObj *fo;
1.319 + for (fo=flag.first();fo; fo=flag.next() )
1.320 + {
1.321 + if (fo->getName()==name) return fo;
1.322 + }
1.323 + return NULL;
1.324 +}
1.325 +