flagrowobj.cpp
author insilmaril
Tue Feb 08 14:07:19 2005 +0000 (2005-02-08)
changeset 89 9db3eaa21237
child 2 608f976aa7bb
child 103 c810a11d11d9
permissions -rw-r--r--
added contextmenu to follow links
     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)
   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 		} else
   154 			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
   155 	}	
   156 	calcBBoxSize();
   157 	positionBBox();	
   158 }
   159 
   160 void FlagRowObj::activate (const QString &foname)
   161 {
   162 	FlagObj *fo=findFlag (foname);
   163 	if (parentRow)
   164 	{
   165 		if (!fo)
   166 		{
   167 			// FlagObj is not present in this row.
   168 			// Copy it from parentRow and activate there
   169 			fo=parentRow->findFlag (foname);
   170 			if (fo)
   171 			{
   172 				fo=addFlag (fo);
   173 				fo->activate();
   174 				fo->setVisibility (visible);
   175 				calcBBoxSize();
   176 				positionBBox();
   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 void FlagRowObj::deactivate (const QString &foname)
   194 {
   195 	FlagObj *fo=findFlag (foname);
   196 	if (fo) flag.remove(fo);
   197 	calcBBoxSize();
   198 	positionBBox();
   199 }
   200 
   201 void FlagRowObj::deactivateAll ()
   202 {
   203 	if (!parentRow)
   204 	{
   205 		FlagObj *fo;
   206 		for (fo=flag.first();fo; fo=flag.next() )
   207 		{
   208 			fo->deactivate();
   209 		}
   210 	} else
   211 		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
   212 }
   213 
   214 void FlagRowObj::setEnabled (bool b)
   215 {
   216 	// If we have no parent, we are the default FlagRowObj
   217 	// and have QToolbarButtons
   218 	if (!parentRow)
   219 	{
   220 		FlagObj *fo;
   221 		for (fo=flag.first();fo; fo=flag.next() )
   222 			fo->setEnabled (b);
   223 	}
   224 }
   225 
   226 void FlagRowObj::resetUsedCounter()
   227 {
   228 	FlagObj *fo;
   229 	for (fo=flag.first();fo; fo=flag.next() )
   230 		fo->setUsed (false);
   231 }
   232 
   233 QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
   234 {
   235 	// Build xml string
   236 	QString s;
   237 	FlagObj *fo;
   238 	if (parentRow)
   239 		for (fo=flag.first();fo; fo=flag.next() )
   240 		{
   241 			// save flag to xml, if flag is set 
   242 			s+=valueElement("standardflag",fo->getName() );
   243 
   244 			// and tell parentRow, that this flag is used
   245 			parentRow->activate(fo->getName() );
   246 		}	
   247 	else
   248 		// Save icons to dir, if verbose is set (xml export)
   249 		// and I am a parentRow 
   250 		// and this flag is really used somewhere
   251 		if (writeflags)
   252 			for (fo=flag.first();fo; fo=flag.next() )
   253 				if (fo->isUsed()) fo->saveToDir (tmpdir,prefix);
   254 	return s;		
   255 
   256 }
   257 
   258 void FlagRowObj::setName (const QString &n)
   259 {
   260 	name=n;
   261 }
   262 
   263 void FlagRowObj::makeToolbar (QMainWindow *w, const QString &n)
   264 {
   265 	if (!parentRow)
   266 	{
   267 		// create bar and buttons
   268 		QToolBar* tb = new QToolBar( w);
   269 		tb->setLabel (n);
   270 		QAction *a;
   271 		FlagObj *fo;
   272 		for (fo=flag.first();fo; fo=flag.next() )
   273 		{
   274 			a=new QAction (
   275 				fo->getToolTip(),
   276 				fo->getPixmap(),
   277 				fo->getName(),
   278 				0,
   279 				w,
   280 				fo->getName()
   281 			);
   282 			a->setToggleAction(true);
   283 			// FIXME should not be enabled by default, later in updateToolbar
   284 			a->setEnabled(true);
   285 			a->addTo (tb);
   286 			fo->setButton (a);
   287 			connect(a, SIGNAL( activated() ), 
   288 					w, SLOT( standardFlagChanged() ) );
   289 		}
   290 	} else
   291 		qWarning ("FlagRowObj::makeToolbar mustn't be called for ordinary rows");
   292 }
   293 
   294 void  FlagRowObj::updateToolBar()
   295 {
   296 	FlagObj *fo;
   297 	if (parentRow)
   298 	{
   299 		// We are just a branch, not the toolbar default
   300 		parentRow->deactivateAll();
   301 		// In parentRow activate all existing (==active) flags
   302 		for (fo=flag.first();fo; fo=flag.next() ) 
   303 			parentRow->activate(fo->getName());
   304 		parentRow->updateToolBar();	
   305 	} else
   306 	{
   307 		// We are the toolbar default
   308 		for (fo=flag.first();fo; fo=flag.next() ) 
   309 			fo->updateButton();
   310 	}
   311 }
   312 
   313 FlagObj* FlagRowObj::findFlag (const QString &name)
   314 {
   315 	FlagObj *fo;
   316 	for (fo=flag.first();fo; fo=flag.next() )
   317 	{
   318 		if (fo->getName()==name) return fo;
   319 	}
   320 	return NULL;
   321 }
   322