flagrowobj.cpp
author insilmaril
Thu, 21 Sep 2006 13:48:05 +0000
changeset 389 bb94eec7c8f3
parent 366 e95081c21da2
child 406 1c8ff1928b97
permissions -rw-r--r--
Added universal support for Mac
     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(Q3Canvas* c):MapObj(c) 
    15 {
    16 //    cout << "Const FlagRowObj\n";
    17     init ();
    18 }
    19 
    20 FlagRowObj::~FlagRowObj()
    21 {
    22 //    cout << "Destr FlagRowObj\n";
    23 	flag.clear();
    24 }
    25 
    26 void FlagRowObj::init ()
    27 {
    28     flag.setAutoDelete (true);
    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 	FlagObj *fo;
    39     for (fo=other->flag.first(); fo; fo=other->flag.next() )
    40 		addFlag (fo);
    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 	int dx=0;
    58 	FlagObj *fo;
    59     for (fo=flag.first(); fo; fo=flag.next() )
    60 	{
    61 		fo->move(x+dx,y);
    62 		dx+=QSize(fo->getSize() ).width();
    63 	}
    64 }
    65 
    66 void FlagRowObj::moveBy(double x, double y)
    67 {
    68     move (x+absPos.x(),y+absPos.y() );
    69 }
    70 
    71 void FlagRowObj::setVisibility (bool v)
    72 {
    73 	MapObj::setVisibility(v);
    74 	FlagObj *fo;
    75 	for (fo=flag.first(); fo; fo=flag.next() )
    76 		fo->setVisibility (v);
    77 }
    78 
    79 FlagObj* FlagRowObj::addFlag (FlagObj *fo)
    80 {
    81 	FlagObj *newfo=new FlagObj (canvas);
    82 	newfo->move (absPos.x() + bbox.width(), absPos.y() );
    83 	newfo->copy (fo);	// create a deep copy of fo
    84 	flag.append(newfo);
    85 	calcBBoxSize();
    86 	positionBBox();
    87 	return newfo;
    88 }
    89 
    90 void FlagRowObj::positionBBox()
    91 {
    92     bbox.moveTopLeft(absPos );
    93     clickBox.moveTopLeft(absPos );
    94 }
    95 
    96 void FlagRowObj::calcBBoxSize()
    97 {
    98 	QSize size(0,0);
    99 	QSize boxsize(0,0);
   100 	FlagObj *fo;
   101     for (fo=flag.first(); fo; fo=flag.next() )
   102 	{
   103 		size=fo->getSize();
   104 		// add widths
   105 		boxsize.setWidth(boxsize.width() + size.width() );
   106 		// maximize height
   107 		if (size.height() > boxsize.height() ) 
   108 			boxsize.setHeight(size.height() );
   109 	}
   110 	bbox.setSize (boxsize);
   111 	clickBox.setSize (boxsize);
   112 }
   113 
   114 QString FlagRowObj::getFlagName (const QPoint &p)
   115 {
   116 	if (!inBox (p)) return "";
   117 	FlagObj *fo;
   118 	for (fo=flag.first();fo; fo=flag.next() )
   119 		if (fo->inBox (p)) return fo->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, bool exclusive)
   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 	} else
   144 	{
   145 		// FlagObj is not present in this row.
   146 		// Copy it from parentRow
   147 		fo=parentRow->findFlag (foname);
   148 		if (fo)
   149 		{
   150 			fo=addFlag (fo);
   151 			fo->activate();
   152 			if (exclusive) 
   153 			{
   154 				deactivateGroup (fo);
   155 				updateToolbar();
   156 			}
   157 		} else
   158 			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
   159 	}	
   160 	calcBBoxSize();
   161 	positionBBox();	
   162 }
   163 
   164 void FlagRowObj::activate (const QString &foname)
   165 {
   166 	// Note: "activate" is also called during loading of a map
   167 	// Here we do not check for exclusive flags!
   168 	FlagObj *fo=findFlag (foname);
   169 	if (parentRow)
   170 	{
   171 		if (!fo)
   172 		{
   173 			// FlagObj is not present in this row.
   174 			// Copy it from parentRow and activate there
   175 			fo=parentRow->findFlag (foname);
   176 			if (fo)
   177 			{
   178 				fo=addFlag (fo);
   179 				fo->activate();
   180 				if (showFlags) 
   181 					fo->setVisibility (visible);
   182 				else
   183 					fo->setVisibility (false);
   184 				calcBBoxSize();
   185 			} else
   186 				qWarning ("FlagRowObj ("+name+")::activate ("+foname+")  failed - could not find it in parentRow");
   187 		}	
   188 	} else
   189 	{
   190 		// I am the parentRow, mark flag as used
   191 		if (fo)
   192 		{
   193 			fo->setUsed(true);
   194 			fo->activate();
   195 		}	
   196 		else
   197 			qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
   198 	}
   199 }
   200 
   201 
   202 void FlagRowObj::deactivate (const QString &foname)
   203 {
   204 	FlagObj *fo=findFlag (foname);
   205 	if (fo) flag.remove(fo);
   206 	calcBBoxSize();
   207 	positionBBox();
   208 }
   209 
   210 void FlagRowObj::deactivateAll ()
   211 {
   212 	if (!parentRow)
   213 	{
   214 		FlagObj *fo;
   215 		for (fo=flag.first();fo; fo=flag.next() )
   216 			fo->deactivate();
   217 	} else
   218 		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
   219 }
   220 
   221 void FlagRowObj::deactivateGroup (FlagObj *keepfo)
   222 {
   223 	// deactivate all flags in keepof, but keep keepfo [sic!]
   224 	if (keepfo)
   225 	{
   226 		QString g=keepfo->getGroup();
   227 		if (g!="undefined")
   228 		{
   229 			FlagObj *fo;
   230 			for (fo=flag.first();fo; fo=flag.next() )
   231 				if (g==fo->getGroup() && keepfo!=fo) 
   232 					flag.remove(fo);
   233 		}		
   234 	}	
   235 }
   236 
   237 void FlagRowObj::setToolBar(QToolBar *tb)
   238 {
   239 	toolbar=tb;
   240 }
   241 
   242 void FlagRowObj::setEnabled (bool b)
   243 {
   244 	if (toolbar)
   245 	{
   246 		toolbar->setEnabled (b);
   247 	}
   248 }
   249 
   250 void FlagRowObj::setShowFlags (bool b)
   251 {
   252 	showFlags=b;
   253 }
   254 
   255 void FlagRowObj::resetUsedCounter()
   256 {
   257 	FlagObj *fo;
   258 	for (fo=flag.first();fo; fo=flag.next() )
   259 		fo->setUsed (false);
   260 }
   261 
   262 QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
   263 {
   264 	// Build xml string
   265 	QString s;
   266 	FlagObj *fo;
   267 	if (parentRow)
   268 		for (fo=flag.first();fo; fo=flag.next() )
   269 		{
   270 			// save flag to xml, if flag is set 
   271 			s+=valueElement("standardflag",fo->getName() );
   272 
   273 			// and tell parentRow, that this flag is used
   274 			parentRow->activate(fo->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 (fo=flag.first();fo; fo=flag.next() )
   282 				if (fo->isUsed()) fo->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 	FlagObj *fo;
   295 	if (parentRow)
   296 	{
   297 		// We are just a branch, not the toolbar default
   298 		// but state has to be copied from ourselves to parentrow!
   299 		parentRow->deactivateAll();
   300 		// In parentRow activate all existing (==active) flags
   301 		for (fo=flag.first();fo; fo=flag.next() ) 
   302 			parentRow->activate(fo->getName());
   303 		parentRow->updateToolbar();	
   304 	} else
   305 	{
   306 		// We are the toolbar default
   307 		if (toolbar)
   308 		{
   309 			// Update state of actions in toolbar
   310 			for (fo=flag.first();fo; fo=flag.next() ) 
   311 				fo->updateAction();
   312 		}	
   313 	}
   314 }
   315 
   316 FlagObj* FlagRowObj::findFlag (const QString &name)
   317 {
   318 	FlagObj *fo;
   319 	for (fo=flag.first();fo; fo=flag.next() )
   320 	{
   321 		if (fo->getName()==name) return fo;
   322 	}
   323 	return NULL;
   324 }
   325