flagrowobj.cpp
author insilmaril
Tue, 14 Mar 2006 14:27:08 +0000
changeset 243 fad762341d2d
parent 241 f5243654fe86
child 256 ba81afcb2b51
permissions -rw-r--r--
hideLinkInExport for Branches (Floats still missing). Floats are now OrnamentedObj.
     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 }
    29 
    30 void FlagRowObj::copy (FlagRowObj* other)
    31 {
    32     MapObj::copy(other);
    33 	parentRow=other->parentRow;
    34 	flag.clear();
    35 	FlagObj *fo;
    36     for (fo=other->flag.first(); fo; fo=other->flag.next() )
    37 		addFlag (fo);
    38 }
    39 
    40 void FlagRowObj::clone (FlagRowObj* pr)
    41 {
    42 	// Difference to copy:
    43 	// We don't copy the flags here, they
    44 	// are created on the fly by toggle and activate
    45 	// This saves lots of canvas objects.
    46 	MapObj::copy(pr);
    47 	flag.clear();
    48 	parentRow=pr;
    49 }
    50 
    51 void FlagRowObj::move(double x, double y)
    52 {
    53     MapObj::move(x,y);
    54 	int dx=0;
    55 	FlagObj *fo;
    56     for (fo=flag.first(); fo; fo=flag.next() )
    57 	{
    58 		fo->move(x+dx,y);
    59 		dx+=QSize(fo->getSize() ).width();
    60 	}
    61 }
    62 
    63 void FlagRowObj::moveBy(double x, double y)
    64 {
    65     move (x+absPos.x(),y+absPos.y() );
    66 }
    67 
    68 void FlagRowObj::setVisibility (bool v)
    69 {
    70 	MapObj::setVisibility(v);
    71 	FlagObj *fo;
    72 	for (fo=flag.first(); fo; fo=flag.next() )
    73 		fo->setVisibility (v);
    74 }
    75 
    76 FlagObj* FlagRowObj::addFlag (FlagObj *fo)
    77 {
    78 	FlagObj *newfo=new FlagObj (canvas);
    79 	newfo->move (absPos.x() + bbox.width(), absPos.y() );
    80 	newfo->copy (fo);	// create a deep copy of fo
    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 	QSize size(0,0);
    96 	QSize boxsize(0,0);
    97 	FlagObj *fo;
    98     for (fo=flag.first(); fo; fo=flag.next() )
    99 	{
   100 		size=fo->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 QPoint &p)
   112 {
   113 	if (!inBox (p)) return "";
   114 	FlagObj *fo;
   115 	for (fo=flag.first();fo; fo=flag.next() )
   116 		if (fo->inBox (p)) return fo->getName();
   117 	return "";	
   118 
   119 	
   120 }
   121 
   122 bool FlagRowObj::isActive (const QString &foname)
   123 {
   124 	FlagObj *fo=findFlag (foname);
   125 	if (parentRow && fo)
   126 		return fo->isActive();
   127 	else
   128 		if (fo) return true;
   129 	return false;
   130 }
   131 
   132 void FlagRowObj::toggle (const QString &foname, bool exclusive)
   133 {
   134 	FlagObj *fo=findFlag (foname);
   135 	if (fo)
   136 	{
   137 		// FlagObj is here, it will be active, too.
   138 		// Deactivate it by removing it from this row.
   139 		flag.remove (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 	FlagObj *fo=findFlag (foname);
   164 	if (parentRow)
   165 	{
   166 		if (!fo)
   167 		{
   168 			// FlagObj is not present in this row.
   169 			// Copy it from parentRow and activate there
   170 			fo=parentRow->findFlag (foname);
   171 			if (fo)
   172 			{
   173 				fo=addFlag (fo);
   174 				fo->activate();
   175 				fo->setVisibility (visible);
   176 				calcBBoxSize();
   177 			} else
   178 				qWarning ("FlagRowObj ("+name+")::activate ("+foname+")  failed - could not find it in parentRow");
   179 		}	
   180 	} else
   181 	{
   182 		// I am the parentRow, mark flag as used
   183 		if (fo)
   184 		{
   185 			fo->setUsed(true);
   186 			fo->activate();
   187 		}	
   188 		else
   189 			qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
   190 	}
   191 }
   192 
   193 
   194 void FlagRowObj::deactivate (const QString &foname)
   195 {
   196 	FlagObj *fo=findFlag (foname);
   197 	if (fo) flag.remove(fo);
   198 	calcBBoxSize();
   199 	positionBBox();
   200 }
   201 
   202 void FlagRowObj::deactivateAll ()
   203 {
   204 	if (!parentRow)
   205 	{
   206 		FlagObj *fo;
   207 		for (fo=flag.first();fo; fo=flag.next() )
   208 			fo->deactivate();
   209 	} else
   210 		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
   211 }
   212 
   213 void FlagRowObj::deactivateGroup (FlagObj *keepfo)
   214 {
   215 	// deactivate all flags in keepof, but keep keepfo [sic!]
   216 	if (keepfo)
   217 	{
   218 		FlagObj *fo;
   219 		for (fo=flag.first();fo; fo=flag.next() )
   220 			if (keepfo->getGroup()==fo->getGroup() && keepfo!=fo) 
   221 				flag.remove(fo);
   222 	}	
   223 }
   224 
   225 void FlagRowObj::setEnabled (bool b)
   226 {
   227 	// If we have no parent, we are the default FlagRowObj
   228 	// and have QToolbarButtons
   229 	if (!parentRow)
   230 	{
   231 		FlagObj *fo;
   232 		for (fo=flag.first();fo; fo=flag.next() )
   233 			fo->setEnabled (b);
   234 	}
   235 }
   236 
   237 void FlagRowObj::resetUsedCounter()
   238 {
   239 	FlagObj *fo;
   240 	for (fo=flag.first();fo; fo=flag.next() )
   241 		fo->setUsed (false);
   242 }
   243 
   244 QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
   245 {
   246 	// Build xml string
   247 	QString s;
   248 	FlagObj *fo;
   249 	if (parentRow)
   250 		for (fo=flag.first();fo; fo=flag.next() )
   251 		{
   252 			// save flag to xml, if flag is set 
   253 			s+=valueElement("standardflag",fo->getName() );
   254 
   255 			// and tell parentRow, that this flag is used
   256 			parentRow->activate(fo->getName() );
   257 		}	
   258 	else
   259 		// Save icons to dir, if verbose is set (xml export)
   260 		// and I am a parentRow 
   261 		// and this flag is really used somewhere
   262 		if (writeflags)
   263 			for (fo=flag.first();fo; fo=flag.next() )
   264 				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
   265 	return s;		
   266 
   267 }
   268 
   269 void FlagRowObj::setName (const QString &n)
   270 {
   271 	name=n;
   272 }
   273 
   274 void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
   275 {
   276 	//Only make toolbar for the parentrow, not each row in branches
   277 	if (!parentRow)
   278 	{
   279 		// create bar and buttons
   280 		QToolBar* tb = new QToolBar( w);
   281 		tb->setLabel (n);
   282 		QAction *a;
   283 		FlagObj *fo;
   284 		for (fo=flag.first();fo; fo=flag.next() )
   285 		{
   286 			a=new QAction (
   287 				fo->getToolTip(),
   288 				fo->getPixmap(),
   289 				fo->getName(),
   290 				0,
   291 				w,
   292 				fo->getName()
   293 			);
   294 			a->setToggleAction(true);
   295 			// FIXME should not be enabled by default, later in updateToolbar
   296 			a->setEnabled(true);
   297 			a->addTo (tb);
   298 			fo->setButton (a);
   299 			connect(a, SIGNAL( activated() ), 
   300 					w, SLOT( standardFlagChanged() ) );
   301 		}
   302 	} else
   303 		qWarning ("FlagRowObj::makeToolbar must not be called for ordinary rows");
   304 }
   305 
   306 void  FlagRowObj::updateToolbar()
   307 {
   308 	FlagObj *fo;
   309 	if (parentRow)
   310 	{
   311 		// We are just a branch, not the toolbar default
   312 		parentRow->deactivateAll();
   313 		// In parentRow activate all existing (==active) flags
   314 		for (fo=flag.first();fo; fo=flag.next() ) 
   315 			parentRow->activate(fo->getName());
   316 		parentRow->updateToolbar();	
   317 	} else
   318 	{
   319 		// We are the toolbar default
   320 		for (fo=flag.first();fo; fo=flag.next() ) 
   321 			fo->updateButton();
   322 	}
   323 }
   324 
   325 FlagObj* FlagRowObj::findFlag (const QString &name)
   326 {
   327 	FlagObj *fo;
   328 	for (fo=flag.first();fo; fo=flag.next() )
   329 	{
   330 		if (fo->getName()==name) return fo;
   331 	}
   332 	return NULL;
   333 }
   334