flagrowobj.cpp
author insilmaril
Tue, 23 Jan 2007 11:50:56 +0000
changeset 423 7019762f8701
parent 411 910ba9fab728
child 425 7014be3ac7d0
permissions -rw-r--r--
1.8.65 Various 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 		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
   218 }
   219 
   220 void FlagRowObj::deactivateGroup (FlagObj *keepfo)
   221 {
   222 	// deactivate all flags in keepof, but keep keepfo [sic!]
   223 	if (keepfo)
   224 	{
   225 		QString g=keepfo->getGroup();
   226 		if (g!="undefined")
   227 		{
   228 			for (int i=0; i<flag.size(); ++i)
   229 				if (g==flag.at(i)->getGroup() && keepfo!=flag.at(i)) 
   230 				{
   231 					FlagObj *fo=flag.at(i);
   232 					flag.remove (fo);
   233 					delete (fo);
   234 				}	
   235 		}		
   236 	}	
   237 }
   238 
   239 void FlagRowObj::setToolBar(QToolBar *tb)
   240 {
   241 	toolbar=tb;
   242 }
   243 
   244 void FlagRowObj::setEnabled (bool b)
   245 {
   246 	if (toolbar)
   247 	{
   248 		toolbar->setEnabled (b);
   249 	}
   250 }
   251 
   252 void FlagRowObj::setShowFlags (bool b)
   253 {
   254 	showFlags=b;
   255 }
   256 
   257 void FlagRowObj::resetUsedCounter()
   258 {
   259 	for (int i=0; i<flag.size(); ++i)
   260 		flag.at(i)->setUsed (false);
   261 }
   262 
   263 QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
   264 {
   265 	// Build xml string
   266 	QString s;
   267 	if (parentRow)
   268 		for (int i=0; i<flag.size(); ++i)
   269 		{
   270 			// save flag to xml, if flag is set 
   271 			s+=valueElement("standardflag",flag.at(i)->getName() );
   272 
   273 			// and tell parentRow, that this flag is used
   274 			parentRow->activate(flag.at(i)->getName() );
   275 		}	
   276 	else
   277 		// Save icons to dir, if verbose is set (xml export)
   278 		// and I am a parentRow 
   279 		// and this flag is really used somewhere
   280 		if (writeflags)
   281 			for (int i=0; i<flag.size(); ++i)
   282 				if (flag.at(i)->isUsed()) flag.at(i)->saveToDir (tmpdir,prefix);
   283 	return s;		
   284 
   285 }
   286 
   287 void FlagRowObj::setName (const QString &n)
   288 {
   289 	name=n;
   290 }
   291 
   292 void  FlagRowObj::updateToolbar()
   293 {
   294 	if (parentRow)
   295 	{
   296 		// We are just a branch, not the toolbar default
   297 		// but state has to be copied from ourselves to parentrow!
   298 		parentRow->deactivateAll();
   299 		// In parentRow activate all existing (==active) flags
   300 		for (int i=0; i<flag.size(); ++i)
   301 			parentRow->activate(flag.at(i)->getName());
   302 		parentRow->updateToolbar();	
   303 	} else
   304 	{
   305 		// We are the toolbar default
   306 		if (toolbar)
   307 		{
   308 			// Update state of actions in toolbar
   309 			for (int i=0; i<flag.size(); ++i)
   310 				flag.at(i)->updateAction();
   311 		}	
   312 	}
   313 }
   314 
   315 FlagObj* FlagRowObj::findFlag (const QString &name)
   316 {
   317 	for (int i=0; i<flag.size(); ++i)
   318 		if (flag.at(i)->getName()==name) return flag.at(i);
   319 	return NULL;
   320 }
   321