flagrowobj.cpp
author insilmaril
Tue, 14 Mar 2006 14:27:04 +0000
changeset 240 bcfd5aad58d6
parent 236 22a885118d50
child 241 f5243654fe86
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 	{	
   117 		cout << " "<<fo->getName()<<endl;
   118 		if (fo->inBox (p)) return fo->getName();
   119 	}	
   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 	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 				fo->setVisibility (visible);
   179 				calcBBoxSize();
   180 			} else
   181 				qWarning ("FlagRowObj ("+name+")::activate ("+foname+")  failed - could not find it in parentRow");
   182 		}	
   183 	} else
   184 	{
   185 		// I am the parentRow, mark flag as used
   186 		if (fo)
   187 		{
   188 			fo->setUsed(true);
   189 			fo->activate();
   190 		}	
   191 		else
   192 			qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
   193 	}
   194 }
   195 
   196 
   197 void FlagRowObj::deactivate (const QString &foname)
   198 {
   199 	FlagObj *fo=findFlag (foname);
   200 	if (fo) flag.remove(fo);
   201 	calcBBoxSize();
   202 	positionBBox();
   203 }
   204 
   205 void FlagRowObj::deactivateAll ()
   206 {
   207 	if (!parentRow)
   208 	{
   209 		FlagObj *fo;
   210 		for (fo=flag.first();fo; fo=flag.next() )
   211 			fo->deactivate();
   212 	} else
   213 		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
   214 }
   215 
   216 void FlagRowObj::deactivateGroup (FlagObj *keepfo)
   217 {
   218 	// deactivate all flags in keepof, but keep keepfo [sic!]
   219 	if (keepfo)
   220 	{
   221 		FlagObj *fo;
   222 		for (fo=flag.first();fo; fo=flag.next() )
   223 			if (keepfo->getGroup()==fo->getGroup() && keepfo!=fo) 
   224 				flag.remove(fo);
   225 	}	
   226 }
   227 
   228 void FlagRowObj::setEnabled (bool b)
   229 {
   230 	// If we have no parent, we are the default FlagRowObj
   231 	// and have QToolbarButtons
   232 	if (!parentRow)
   233 	{
   234 		FlagObj *fo;
   235 		for (fo=flag.first();fo; fo=flag.next() )
   236 			fo->setEnabled (b);
   237 	}
   238 }
   239 
   240 void FlagRowObj::resetUsedCounter()
   241 {
   242 	FlagObj *fo;
   243 	for (fo=flag.first();fo; fo=flag.next() )
   244 		fo->setUsed (false);
   245 }
   246 
   247 QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
   248 {
   249 	// Build xml string
   250 	QString s;
   251 	FlagObj *fo;
   252 	if (parentRow)
   253 		for (fo=flag.first();fo; fo=flag.next() )
   254 		{
   255 			// save flag to xml, if flag is set 
   256 			s+=valueElement("standardflag",fo->getName() );
   257 
   258 			// and tell parentRow, that this flag is used
   259 			parentRow->activate(fo->getName() );
   260 		}	
   261 	else
   262 		// Save icons to dir, if verbose is set (xml export)
   263 		// and I am a parentRow 
   264 		// and this flag is really used somewhere
   265 		if (writeflags)
   266 			for (fo=flag.first();fo; fo=flag.next() )
   267 				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
   268 	return s;		
   269 
   270 }
   271 
   272 void FlagRowObj::setName (const QString &n)
   273 {
   274 	name=n;
   275 }
   276 
   277 void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
   278 {
   279 	//Only make toolbar for the parentrow, not each row in branches
   280 	if (!parentRow)
   281 	{
   282 		// create bar and buttons
   283 		QToolBar* tb = new QToolBar( w);
   284 		tb->setLabel (n);
   285 		QAction *a;
   286 		FlagObj *fo;
   287 		for (fo=flag.first();fo; fo=flag.next() )
   288 		{
   289 			a=new QAction (
   290 				fo->getToolTip(),
   291 				fo->getPixmap(),
   292 				fo->getName(),
   293 				0,
   294 				w,
   295 				fo->getName()
   296 			);
   297 			a->setToggleAction(true);
   298 			// FIXME should not be enabled by default, later in updateToolbar
   299 			a->setEnabled(true);
   300 			a->addTo (tb);
   301 			fo->setButton (a);
   302 			connect(a, SIGNAL( activated() ), 
   303 					w, SLOT( standardFlagChanged() ) );
   304 		}
   305 	} else
   306 		qWarning ("FlagRowObj::makeToolbar must not be called for ordinary rows");
   307 }
   308 
   309 void  FlagRowObj::updateToolbar()
   310 {
   311 	FlagObj *fo;
   312 	if (parentRow)
   313 	{
   314 		// We are just a branch, not the toolbar default
   315 		parentRow->deactivateAll();
   316 		// In parentRow activate all existing (==active) flags
   317 		for (fo=flag.first();fo; fo=flag.next() ) 
   318 			parentRow->activate(fo->getName());
   319 		parentRow->updateToolbar();	
   320 	} else
   321 	{
   322 		// We are the toolbar default
   323 		for (fo=flag.first();fo; fo=flag.next() ) 
   324 			fo->updateButton();
   325 	}
   326 }
   327 
   328 FlagObj* FlagRowObj::findFlag (const QString &name)
   329 {
   330 	FlagObj *fo;
   331 	for (fo=flag.first();fo; fo=flag.next() )
   332 	{
   333 		if (fo->getName()==name) return fo;
   334 	}
   335 	return NULL;
   336 }
   337