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