flagrowobj.cpp
author insilmaril
Mon, 08 May 2006 13:25:48 +0000
changeset 318 c499e1ca1de0
parent 311 6a7db028088e
child 366 e95081c21da2
permissions -rw-r--r--
1.7.16 Small bugfixes
     1 #include "flagrowobj.h"
     2 
     3 /////////////////////////////////////////////////////////////////
     4 // FlagRowObj
     5 /////////////////////////////////////////////////////////////////
     6 FlagRowObj::FlagRowObj()
     7 {
     8 //    cout << "Const FlagRowObj ()\n";
     9     init ();
    10 }
    11 
    12 FlagRowObj::FlagRowObj(QCanvas* c):MapObj(c) 
    13 {
    14 //    cout << "Const FlagRowObj\n";
    15     init ();
    16 }
    17 
    18 FlagRowObj::~FlagRowObj()
    19 {
    20 //    cout << "Destr FlagRowObj\n";
    21 	flag.clear();
    22 }
    23 
    24 void FlagRowObj::init ()
    25 {
    26     flag.setAutoDelete (true);
    27 	parentRow=NULL;
    28 	showFlags=true;
    29 }
    30 
    31 void FlagRowObj::copy (FlagRowObj* other)
    32 {
    33     MapObj::copy(other);
    34 	parentRow=other->parentRow;
    35 	flag.clear();
    36 	FlagObj *fo;
    37     for (fo=other->flag.first(); fo; fo=other->flag.next() )
    38 		addFlag (fo);
    39 }
    40 
    41 void FlagRowObj::clone (FlagRowObj* pr)
    42 {
    43 	// Difference to copy:
    44 	// We don't copy the flags here, they
    45 	// are created on the fly by toggle and activate
    46 	// This saves lots of canvas objects.
    47 	MapObj::copy(pr);
    48 	flag.clear();
    49 	parentRow=pr;
    50 }
    51 
    52 void FlagRowObj::move(double x, double y)
    53 {
    54     MapObj::move(x,y);
    55 	int dx=0;
    56 	FlagObj *fo;
    57     for (fo=flag.first(); fo; fo=flag.next() )
    58 	{
    59 		fo->move(x+dx,y);
    60 		dx+=QSize(fo->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 	FlagObj *fo;
    73 	for (fo=flag.first(); fo; fo=flag.next() )
    74 		fo->setVisibility (v);
    75 }
    76 
    77 FlagObj* FlagRowObj::addFlag (FlagObj *fo)
    78 {
    79 	FlagObj *newfo=new FlagObj (canvas);
    80 	newfo->move (absPos.x() + bbox.width(), absPos.y() );
    81 	newfo->copy (fo);	// create a deep copy of fo
    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 	QSize size(0,0);
    97 	QSize boxsize(0,0);
    98 	FlagObj *fo;
    99     for (fo=flag.first(); fo; fo=flag.next() )
   100 	{
   101 		size=fo->getSize();
   102 		// add widths
   103 		boxsize.setWidth(boxsize.width() + size.width() );
   104 		// maximize height
   105 		if (size.height() > boxsize.height() ) 
   106 			boxsize.setHeight(size.height() );
   107 	}
   108 	bbox.setSize (boxsize);
   109 	clickBox.setSize (boxsize);
   110 }
   111 
   112 QString FlagRowObj::getFlagName (const QPoint &p)
   113 {
   114 	if (!inBox (p)) return "";
   115 	FlagObj *fo;
   116 	for (fo=flag.first();fo; fo=flag.next() )
   117 		if (fo->inBox (p)) return fo->getName();
   118 	return "";	
   119 
   120 	
   121 }
   122 
   123 bool FlagRowObj::isActive (const QString &foname)
   124 {
   125 	FlagObj *fo=findFlag (foname);
   126 	if (parentRow && fo)
   127 		return fo->isActive();
   128 	else
   129 		if (fo) return true;
   130 	return false;
   131 }
   132 
   133 void FlagRowObj::toggle (const QString &foname, bool exclusive)
   134 {
   135 	FlagObj *fo=findFlag (foname);
   136 	if (fo)
   137 	{
   138 		// FlagObj is here, it will be active, too.
   139 		// Deactivate it by removing it from this row.
   140 		flag.remove (fo);
   141 	} else
   142 	{
   143 		// FlagObj is not present in this row.
   144 		// Copy it from parentRow
   145 		fo=parentRow->findFlag (foname);
   146 		if (fo)
   147 		{
   148 			fo=addFlag (fo);
   149 			fo->activate();
   150 			if (exclusive) 
   151 			{
   152 				deactivateGroup (fo);
   153 				updateToolbar();
   154 			}
   155 		} else
   156 			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
   157 	}	
   158 	calcBBoxSize();
   159 	positionBBox();	
   160 }
   161 
   162 void FlagRowObj::activate (const QString &foname)
   163 {
   164 	// Note: "activate" is also called during loading of a map
   165 	// Here we do not check for exclusive flags!
   166 	FlagObj *fo=findFlag (foname);
   167 	if (parentRow)
   168 	{
   169 		if (!fo)
   170 		{
   171 			// FlagObj is not present in this row.
   172 			// Copy it from parentRow and activate there
   173 			fo=parentRow->findFlag (foname);
   174 			if (fo)
   175 			{
   176 				fo=addFlag (fo);
   177 				fo->activate();
   178 				if (showFlags) 
   179 					fo->setVisibility (visible);
   180 				else
   181 					fo->setVisibility (false);
   182 				calcBBoxSize();
   183 			} else
   184 				qWarning ("FlagRowObj ("+name+")::activate ("+foname+")  failed - could not find it in parentRow");
   185 		}	
   186 	} else
   187 	{
   188 		// I am the parentRow, mark flag as used
   189 		if (fo)
   190 		{
   191 			fo->setUsed(true);
   192 			fo->activate();
   193 		}	
   194 		else
   195 			qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
   196 	}
   197 }
   198 
   199 
   200 void FlagRowObj::deactivate (const QString &foname)
   201 {
   202 	FlagObj *fo=findFlag (foname);
   203 	if (fo) flag.remove(fo);
   204 	calcBBoxSize();
   205 	positionBBox();
   206 }
   207 
   208 void FlagRowObj::deactivateAll ()
   209 {
   210 	if (!parentRow)
   211 	{
   212 		FlagObj *fo;
   213 		for (fo=flag.first();fo; fo=flag.next() )
   214 			fo->deactivate();
   215 	} else
   216 		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
   217 }
   218 
   219 void FlagRowObj::deactivateGroup (FlagObj *keepfo)
   220 {
   221 	// deactivate all flags in keepof, but keep keepfo [sic!]
   222 	if (keepfo)
   223 	{
   224 		QString g=keepfo->getGroup();
   225 		if (g!="undefined")
   226 		{
   227 			FlagObj *fo;
   228 			for (fo=flag.first();fo; fo=flag.next() )
   229 				if (g==fo->getGroup() && keepfo!=fo) 
   230 					flag.remove(fo);
   231 		}		
   232 	}	
   233 }
   234 
   235 void FlagRowObj::setEnabled (bool b)
   236 {
   237 	// If we have no parent, we are the default FlagRowObj
   238 	// and have QToolbarButtons
   239 	if (!parentRow)
   240 	{
   241 		FlagObj *fo;
   242 		for (fo=flag.first();fo; fo=flag.next() )
   243 			fo->setEnabled (b);
   244 	}
   245 }
   246 
   247 void FlagRowObj::setShowFlags (bool b)
   248 {
   249 	showFlags=b;
   250 }
   251 
   252 void FlagRowObj::resetUsedCounter()
   253 {
   254 	FlagObj *fo;
   255 	for (fo=flag.first();fo; fo=flag.next() )
   256 		fo->setUsed (false);
   257 }
   258 
   259 QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
   260 {
   261 	// Build xml string
   262 	QString s;
   263 	FlagObj *fo;
   264 	if (parentRow)
   265 		for (fo=flag.first();fo; fo=flag.next() )
   266 		{
   267 			// save flag to xml, if flag is set 
   268 			s+=valueElement("standardflag",fo->getName() );
   269 
   270 			// and tell parentRow, that this flag is used
   271 			parentRow->activate(fo->getName() );
   272 		}	
   273 	else
   274 		// Save icons to dir, if verbose is set (xml export)
   275 		// and I am a parentRow 
   276 		// and this flag is really used somewhere
   277 		if (writeflags)
   278 			for (fo=flag.first();fo; fo=flag.next() )
   279 				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
   280 	return s;		
   281 
   282 }
   283 
   284 void FlagRowObj::setName (const QString &n)
   285 {
   286 	name=n;
   287 }
   288 
   289 void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
   290 {
   291 	//Only make toolbar for the parentrow, not each row in branches
   292 	if (!parentRow)
   293 	{
   294 		// create bar and buttons
   295 		QToolBar* tb = new QToolBar( w);
   296 		tb->setLabel (n);
   297 		QAction *a;
   298 		FlagObj *fo;
   299 		for (fo=flag.first();fo; fo=flag.next() )
   300 		{
   301 			a=new QAction (
   302 				fo->getToolTip(),
   303 				fo->getPixmap(),
   304 				fo->getName(),
   305 				0,
   306 				w,
   307 				fo->getName()
   308 			);
   309 			a->setToggleAction(true);
   310 			// FIXME should not be enabled by default, later in updateToolbar
   311 			a->setEnabled(true);
   312 			a->addTo (tb);
   313 			fo->setButton (a);
   314 			connect(a, SIGNAL( activated() ), 
   315 					w, SLOT( standardFlagChanged() ) );
   316 		}
   317 	} else
   318 		qWarning ("FlagRowObj::makeToolbar must not be called for ordinary rows");
   319 }
   320 
   321 void  FlagRowObj::updateToolbar()
   322 {
   323 	FlagObj *fo;
   324 	if (parentRow)
   325 	{
   326 		// We are just a branch, not the toolbar default
   327 		parentRow->deactivateAll();
   328 		// In parentRow activate all existing (==active) flags
   329 		for (fo=flag.first();fo; fo=flag.next() ) 
   330 			parentRow->activate(fo->getName());
   331 		parentRow->updateToolbar();	
   332 	} else
   333 	{
   334 		// We are the toolbar default
   335 		for (fo=flag.first();fo; fo=flag.next() ) 
   336 			fo->updateButton();
   337 	}
   338 }
   339 
   340 FlagObj* FlagRowObj::findFlag (const QString &name)
   341 {
   342 	FlagObj *fo;
   343 	for (fo=flag.first();fo; fo=flag.next() )
   344 	{
   345 		if (fo->getName()==name) return fo;
   346 	}
   347 	return NULL;
   348 }
   349