flagrowobj.cpp
author insilmaril
Thu, 23 Nov 2006 13:53:08 +0000
changeset 406 1c8ff1928b97
parent 366 e95081c21da2
child 408 c2a05fa925a1
permissions -rw-r--r--
Removed more QT3 stuff. Drag & Drop not 100% functional at the moment
     1 #include "flagrowobj.h"
     2 
     3 #include <QToolBar>
     4 
     5 /////////////////////////////////////////////////////////////////
     6 // FlagRowObj
     7 /////////////////////////////////////////////////////////////////
     8 FlagRowObj::FlagRowObj()
     9 {
    10 //    cout << "Const FlagRowObj ()\n";
    11     init ();
    12 }
    13 
    14 FlagRowObj::FlagRowObj(Q3Canvas* c):MapObj(c) 
    15 {
    16 //    cout << "Const FlagRowObj\n";
    17     init ();
    18 }
    19 
    20 FlagRowObj::~FlagRowObj()
    21 {
    22  //   cout << "Destr FlagRowObj\n";
    23 	while (!flag.isEmpty())
    24 		delete flag.takeFirst();
    25 }
    26 
    27 void FlagRowObj::init ()
    28 {
    29 	parentRow=NULL;
    30 	showFlags=true;
    31 }
    32 
    33 void FlagRowObj::copy (FlagRowObj* other)
    34 {
    35     MapObj::copy(other);
    36 	parentRow=other->parentRow;
    37 	flag.clear();
    38 	for (int i=0; i<flag.size(); ++i)
    39 		addFlag (flag.at(i));
    40 }
    41 
    42 void FlagRowObj::clone (FlagRowObj* pr)
    43 {
    44 	// Difference to copy:
    45 	// We don't copy the flags here, they
    46 	// are created on the fly by toggle and activate
    47 	// This saves lots of canvas objects.
    48 	MapObj::copy(pr);
    49 	flag.clear();
    50 	parentRow=pr;
    51 }
    52 
    53 void FlagRowObj::move(double x, double y)
    54 {
    55     MapObj::move(x,y);
    56 	int dx=0;
    57 	for (int i=0; i<flag.size(); ++i)
    58 	{
    59 		flag.at(i)->move(x+dx,y);
    60 		dx+=QSize(flag.at(i)->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 	for (int i=0; i<flag.size(); ++i)
    73 		flag.at(i)->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 	for (int i=0; i<flag.size(); ++i)
    98 	{
    99 		size=flag.at(i)->getSize();
   100 		// add widths
   101 		boxsize.setWidth(boxsize.width() + size.width() );
   102 		// maximize height
   103 		if (size.height() > boxsize.height() ) 
   104 			boxsize.setHeight(size.height() );
   105 	}
   106 	bbox.setSize (boxsize);
   107 	clickBox.setSize (boxsize);
   108 }
   109 
   110 QString FlagRowObj::getFlagName (const QPoint &p)
   111 {
   112 	if (!inBox (p)) return "";
   113 	for (int i=0; i<flag.size(); ++i)
   114 		if (flag.at(i)->inBox (p)) return flag.at(i)->getName();
   115 	return "";	
   116 
   117 	
   118 }
   119 
   120 bool FlagRowObj::isActive (const QString &foname)
   121 {
   122 	FlagObj *fo=findFlag (foname);
   123 	if (parentRow && fo)
   124 		return fo->isActive();
   125 	else
   126 		if (fo) return true;
   127 	return false;
   128 }
   129 
   130 void FlagRowObj::toggle (const QString &foname, bool exclusive)
   131 {
   132 	FlagObj *fo=findFlag (foname);
   133 	if (fo)
   134 	{
   135 		// FlagObj is here, it will be active, too.
   136 		// Deactivate it by removing it from this row.
   137 		flag.remove (fo);
   138 	} else
   139 	{
   140 		// FlagObj is not present in this row.
   141 		// Copy it from parentRow
   142 		fo=parentRow->findFlag (foname);
   143 		if (fo)
   144 		{
   145 			fo=addFlag (fo);
   146 			fo->activate();
   147 			if (exclusive) 
   148 			{
   149 				deactivateGroup (fo);
   150 				updateToolbar();
   151 			}
   152 		} else
   153 			qWarning ("FlagRowObj ("+name+")::toggle ("+foname+")  failed - could not find it in parentRow");
   154 	}	
   155 	calcBBoxSize();
   156 	positionBBox();	
   157 }
   158 
   159 void FlagRowObj::activate (const QString &foname)
   160 {
   161 	// Note: "activate" is also called during loading of a map
   162 	// Here we do not check for exclusive flags!
   163 	FlagObj *fo=findFlag (foname);
   164 	if (parentRow)
   165 	{
   166 		if (!fo)
   167 		{
   168 			// FlagObj is not present in this row.
   169 			// Copy it from parentRow and activate there
   170 			fo=parentRow->findFlag (foname);
   171 			if (fo)
   172 			{
   173 				fo=addFlag (fo);
   174 				fo->activate();
   175 				if (showFlags) 
   176 					fo->setVisibility (visible);
   177 				else
   178 					fo->setVisibility (false);
   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 		for (int i=0; i<flag.size(); ++i)
   210 			flag.at(i)->deactivate();
   211 	} else
   212 		qWarning ("FlagRowObj::deactivateAll mustn't be called for ordinary rows");
   213 }
   214 
   215 void FlagRowObj::deactivateGroup (FlagObj *keepfo)
   216 {
   217 	// deactivate all flags in keepof, but keep keepfo [sic!]
   218 	if (keepfo)
   219 	{
   220 		QString g=keepfo->getGroup();
   221 		if (g!="undefined")
   222 		{
   223 			for (int i=0; i<flag.size(); ++i)
   224 				if (g==flag.at(i)->getGroup() && keepfo!=flag.at(i)) 
   225 					flag.remove(flag.at(i));
   226 		}		
   227 	}	
   228 }
   229 
   230 void FlagRowObj::setToolBar(QToolBar *tb)
   231 {
   232 	toolbar=tb;
   233 }
   234 
   235 void FlagRowObj::setEnabled (bool b)
   236 {
   237 	if (toolbar)
   238 	{
   239 		toolbar->setEnabled (b);
   240 	}
   241 }
   242 
   243 void FlagRowObj::setShowFlags (bool b)
   244 {
   245 	showFlags=b;
   246 }
   247 
   248 void FlagRowObj::resetUsedCounter()
   249 {
   250 	for (int i=0; i<flag.size(); ++i)
   251 		flag.at(i)->setUsed (false);
   252 }
   253 
   254 QString FlagRowObj::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags)
   255 {
   256 	// Build xml string
   257 	QString s;
   258 	if (parentRow)
   259 		for (int i=0; i<flag.size(); ++i)
   260 		{
   261 			// save flag to xml, if flag is set 
   262 			s+=valueElement("standardflag",flag.at(i)->getName() );
   263 
   264 			// and tell parentRow, that this flag is used
   265 			parentRow->activate(flag.at(i)->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 (int i=0; i<flag.size(); ++i)
   273 				if (flag.at(i)->isUsed()) flag.at(i)->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::updateToolbar()
   284 {
   285 	if (parentRow)
   286 	{
   287 		// We are just a branch, not the toolbar default
   288 		// but state has to be copied from ourselves to parentrow!
   289 		parentRow->deactivateAll();
   290 		// In parentRow activate all existing (==active) flags
   291 		for (int i=0; i<flag.size(); ++i)
   292 			parentRow->activate(flag.at(i)->getName());
   293 		parentRow->updateToolbar();	
   294 	} else
   295 	{
   296 		// We are the toolbar default
   297 		if (toolbar)
   298 		{
   299 			// Update state of actions in toolbar
   300 			for (int i=0; i<flag.size(); ++i)
   301 				flag.at(i)->updateAction();
   302 		}	
   303 	}
   304 }
   305 
   306 FlagObj* FlagRowObj::findFlag (const QString &name)
   307 {
   308 	for (int i=0; i<flag.size(); ++i)
   309 		if (flag.at(i)->getName()==name) return flag.at(i);
   310 	return NULL;
   311 }
   312