flagrowobj.cpp
author jhilmer
Mon Aug 01 19:33:16 2005 +0000 (2005-08-01)
changeset 144 f4bbdc809fec
parent 0 7a96bd401351
child 166 325958acb69b
permissions -rw-r--r--
Fix: closing of editor when text are modified
     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.setX (absPos.x() );
    90     bbox.setY (absPos.y() );
    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 (QSize(boxsize.width(), boxsize.height() ));
   108 }
   109 
   110 QString FlagRowObj::getFlagName (const QPoint &p)
   111 {
   112 	if (!inBBox (p)) return "";
   113 	FlagObj *fo;
   114 	for (fo=flag.first();fo; fo=flag.next() )
   115 		if (fo->inBBox (p)) return fo->getName();
   116 	return "";	
   117 
   118 	
   119 }
   120 
   121 bool FlagRowObj::isActive (const QString &foname)
   122 {
   123 	FlagObj *fo=findFlag (foname);
   124 	if (parentRow)
   125 	{
   126 		if (fo)
   127 			return fo->isActive();
   128 		else
   129 			qWarning ("FlagRowObj::isActive of "+name+" couldn't find "+foname);
   130 			
   131 	} else
   132 		if (fo) return true;
   133 	return false;
   134 }
   135 
   136 void FlagRowObj::toggle (const QString &foname, bool exclusive)
   137 {
   138 	FlagObj *fo=findFlag (foname);
   139 	if (fo)
   140 	{
   141 		// FlagObj is here, it will be active, too.
   142 		// Deactivate it by removing it from this row.
   143 		flag.remove (fo);
   144 	} else
   145 	{
   146 		// FlagObj is not present in this row.
   147 		// Copy it from parentRow
   148 		fo=parentRow->findFlag (foname);
   149 		if (fo)
   150 		{
   151 			fo=addFlag (fo);
   152 			fo->activate();
   153 			if (exclusive) 
   154 			{
   155 				deactivateGroup (fo);
   156 				updateToolbar();
   157 			}
   158 		} else
   159 			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
   160 	}	
   161 	calcBBoxSize();
   162 	positionBBox();	
   163 }
   164 
   165 void FlagRowObj::activate (const QString &foname)
   166 {
   167 	FlagObj *fo=findFlag (foname);
   168 	if (parentRow)
   169 	{
   170 		if (!fo)
   171 		{
   172 			// FlagObj is not present in this row.
   173 			// Copy it from parentRow and activate there
   174 			fo=parentRow->findFlag (foname);
   175 			if (fo)
   176 			{
   177 				fo=addFlag (fo);
   178 				fo->activate();
   179 				fo->setVisibility (visible);
   180 				calcBBoxSize();
   181 				positionBBox();
   182 			} else
   183 				qWarning ("FlagRowObj ("+name+")::activate ("+foname+")  failed - could not find it in parentRow");
   184 		}	
   185 	} else
   186 	{
   187 		// I am the parentRow, mark flag as used
   188 		if (fo)
   189 		{
   190 			fo->setUsed(true);
   191 			fo->activate();
   192 		}	
   193 		else
   194 			qWarning ("FlagRowObj::activate no FlagObj \""+foname+"\" found in parentRow");
   195 	}
   196 }
   197 
   198 
   199 void FlagRowObj::deactivate (const QString &foname)
   200 {
   201 	FlagObj *fo=findFlag (foname);
   202 	if (fo) flag.remove(fo);
   203 	calcBBoxSize();
   204 	positionBBox();
   205 }
   206 
   207 void FlagRowObj::deactivateAll ()
   208 {
   209 	if (!parentRow)
   210 	{
   211 		FlagObj *fo;
   212 		for (fo=flag.first();fo; fo=flag.next() )
   213 			fo->deactivate();
   214 	} else
   215 		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
   216 }
   217 
   218 void FlagRowObj::deactivateGroup (FlagObj *keepfo)
   219 {
   220 	// deactivate all flags in keepof, but keep keepfo [sic!]
   221 	if (keepfo)
   222 	{
   223 		FlagObj *fo;
   224 		for (fo=flag.first();fo; fo=flag.next() )
   225 			if (keepfo->getGroup()==fo->getGroup() && keepfo!=fo) 
   226 				flag.remove(fo);
   227 	}	
   228 }
   229 
   230 void FlagRowObj::setEnabled (bool b)
   231 {
   232 	// If we have no parent, we are the default FlagRowObj
   233 	// and have QToolbarButtons
   234 	if (!parentRow)
   235 	{
   236 		FlagObj *fo;
   237 		for (fo=flag.first();fo; fo=flag.next() )
   238 			fo->setEnabled (b);
   239 	}
   240 }
   241 
   242 void FlagRowObj::resetUsedCounter()
   243 {
   244 	FlagObj *fo;
   245 	for (fo=flag.first();fo; fo=flag.next() )
   246 		fo->setUsed (false);
   247 }
   248 
   249 QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
   250 {
   251 	// Build xml string
   252 	QString s;
   253 	FlagObj *fo;
   254 	if (parentRow)
   255 		for (fo=flag.first();fo; fo=flag.next() )
   256 		{
   257 			// save flag to xml, if flag is set 
   258 			s+=valueElement("standardflag",fo->getName() );
   259 
   260 			// and tell parentRow, that this flag is used
   261 			parentRow->activate(fo->getName() );
   262 		}	
   263 	else
   264 		// Save icons to dir, if verbose is set (xml export)
   265 		// and I am a parentRow 
   266 		// and this flag is really used somewhere
   267 		if (writeflags)
   268 			for (fo=flag.first();fo; fo=flag.next() )
   269 				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
   270 	return s;		
   271 
   272 }
   273 
   274 void FlagRowObj::setName (const QString &n)
   275 {
   276 	name=n;
   277 }
   278 
   279 void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
   280 {
   281 	//Only make toolbar for the parentrow, not each row in branches
   282 	if (!parentRow)
   283 	{
   284 		// create bar and buttons
   285 		QToolBar* tb = new QToolBar( w);
   286 		tb->setLabel (n);
   287 		QAction *a;
   288 		FlagObj *fo;
   289 		for (fo=flag.first();fo; fo=flag.next() )
   290 		{
   291 			a=new QAction (
   292 				fo->getToolTip(),
   293 				fo->getPixmap(),
   294 				fo->getName(),
   295 				0,
   296 				w,
   297 				fo->getName()
   298 			);
   299 			a->setToggleAction(true);
   300 			// FIXME should not be enabled by default, later in updateToolbar
   301 			a->setEnabled(true);
   302 			a->addTo (tb);
   303 			fo->setButton (a);
   304 			connect(a, SIGNAL( activated() ), 
   305 					w, SLOT( standardFlagChanged() ) );
   306 		}
   307 	} else
   308 		qWarning ("FlagRowObj::makeToolbar mustn't be called for ordinary rows");
   309 }
   310 
   311 void  FlagRowObj::updateToolbar()
   312 {
   313 	FlagObj *fo;
   314 	if (parentRow)
   315 	{
   316 		// We are just a branch, not the toolbar default
   317 		parentRow->deactivateAll();
   318 		// In parentRow activate all existing (==active) flags
   319 		for (fo=flag.first();fo; fo=flag.next() ) 
   320 			parentRow->activate(fo->getName());
   321 		parentRow->updateToolbar();	
   322 	} else
   323 	{
   324 		// We are the toolbar default
   325 		for (fo=flag.first();fo; fo=flag.next() ) 
   326 			fo->updateButton();
   327 	}
   328 }
   329 
   330 FlagObj* FlagRowObj::findFlag (const QString &name)
   331 {
   332 	FlagObj *fo;
   333 	for (fo=flag.first();fo; fo=flag.next() )
   334 	{
   335 		if (fo->getName()==name) return fo;
   336 	}
   337 	return NULL;
   338 }
   339