flagrowobj.cpp
author insilmaril
Thu, 01 Oct 2009 11:28:50 +0000
changeset 798 d251c7b2de54
parent 767 6d2b32f305f9
permissions -rw-r--r--
Various fixes for relinking and selecting
     1 #include <QToolBar>
     2 
     3 #include <iostream>
     4 using namespace std;
     5 
     6 #include "flag.h"
     7 #include "flagrowobj.h"
     8 
     9 #include "geometry.h"
    10 
    11 /////////////////////////////////////////////////////////////////
    12 // FlagRowObj
    13 /////////////////////////////////////////////////////////////////
    14 FlagRowObj::FlagRowObj()
    15 {
    16 //    cout << "Const FlagRowObj ()\n";
    17     init ();
    18 }
    19 
    20 FlagRowObj::FlagRowObj(QGraphicsScene* s):MapObj(s) 
    21 {
    22 //    cout << "Const FlagRowObj (s)\n";
    23     init ();
    24 }
    25 
    26 FlagRowObj::~FlagRowObj()
    27 {
    28 	//cout << "Destr FlagRowObj\n";
    29 	while (!flag.isEmpty())
    30 		delete (flag.takeFirst() );
    31 }
    32 
    33 void FlagRowObj::init ()
    34 {
    35 	showFlags=true;
    36 }
    37 
    38 void FlagRowObj::copy (FlagRowObj* other)
    39 {
    40     MapObj::copy(other);
    41 	flag.clear();
    42 	for (int i=0; i<flag.size(); ++i)
    43 		addFlag (flag.at(i));
    44 }
    45 
    46 void FlagRowObj::move(double x, double y)
    47 {
    48     MapObj::move(x,y);
    49 	qreal dx=0;
    50 	for (int i=0; i<flag.size(); ++i)
    51 	{
    52 		flag.at(i)->move(x+dx,y);
    53 		dx+=QSizeF(flag.at(i)->getSize() ).width();
    54 	}
    55 }
    56 
    57 void FlagRowObj::moveBy(double x, double y)
    58 {
    59     move (x+absPos.x(),y+absPos.y() );
    60 }
    61 
    62 void FlagRowObj::setVisibility (bool v)
    63 {
    64 	MapObj::setVisibility(v);
    65 	for (int i=0; i<flag.size(); ++i)
    66 		flag.at(i)->setVisibility (v);
    67 }
    68 
    69 FlagObj* FlagRowObj::addFlag (FlagObj *fo)
    70 {
    71 	FlagObj *newfo=new FlagObj (scene);
    72 	newfo->copy (fo);	// create a deep copy of fo
    73 	newfo->move (absPos.x() + bbox.width(), absPos.y() );
    74 	flag.append(newfo);
    75 	calcBBoxSize();
    76 	positionBBox();
    77 	return newfo;
    78 }
    79 
    80 QStringList FlagRowObj::activeFlagNames()
    81 {
    82 	QStringList list;
    83 	for (int i=0; i<flag.size(); ++i)
    84 		list.append (flag.at(i)->getName());
    85 	return list;
    86 }
    87 
    88 void FlagRowObj::positionBBox()
    89 {
    90     bbox.moveTopLeft(absPos );
    91     clickBox.moveTopLeft(absPos );
    92 }
    93 
    94 void FlagRowObj::calcBBoxSize()
    95 {
    96 	QSizeF size(0,0);
    97 	QSizeF boxsize(0,0);
    98 	for (int i=0; i<flag.size(); ++i)
    99 	{
   100 		size=flag.at(i)->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 QPointF &p)
   112 {
   113 	if (!isInBox (p,clickBox)) return "";
   114 	for (int i=0; i<flag.size(); ++i)
   115 		if (isInBox (p,flag.at(i)->getClickBox ())) return flag.at(i)->getName();
   116 	return "";	
   117 
   118 	
   119 }
   120 
   121 bool FlagRowObj::isActive (const QString &foname)
   122 {
   123 
   124 	FlagObj *fo=findFlag (foname);
   125 	if (fo) 
   126 		return true;
   127 	else
   128 		return false;
   129 }
   130 
   131 void FlagRowObj::activate (Flag *flag)	
   132 {
   133 	if (flag) 
   134 	{
   135 		FlagObj *fo=addFlag (new FlagObj (flag));
   136 		fo->activate();
   137 		if (showFlags)	// FIXME-3 necessary? only called from FIO::init
   138 			fo->setVisibility (visible);
   139 		else
   140 			fo->setVisibility (false);
   141 		calcBBoxSize();
   142 	}
   143 }
   144 
   145 void FlagRowObj::deactivate (const QString &foname)
   146 {
   147 	FlagObj *fo=findFlag (foname);
   148 	if (fo) 
   149 	{
   150 		flag.remove(fo);
   151 		delete (fo);
   152 	}	
   153 	calcBBoxSize();
   154 	positionBBox();
   155 }
   156 
   157 void FlagRowObj::setShowFlags (bool b)
   158 {
   159 	showFlags=b;
   160 }
   161 
   162 FlagObj* FlagRowObj::findFlag (const QString &name)
   163 {
   164 	for (int i=0; i<flag.size(); ++i)
   165 		if (flag.at(i)->getName()==name) return flag.at(i);
   166 	return NULL;
   167 }
   168