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