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