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