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