selection.cpp
author insilmaril
Thu, 08 Nov 2007 15:28:03 +0000
changeset 617 7ee5bf3647d3
parent 611 a1ae877b438d
child 641 687833b29c4e
permissions -rw-r--r--
1.11.2 split up of xml helper functions. started to work on attributes
     1 #include <typeinfo>
     2 
     3 #include "selection.h"
     4 
     5 #include "mainwindow.h"
     6 #include "mapeditor.h"
     7 
     8 
     9 
    10 extern Main *mainWindow;
    11 
    12 Selection::Selection()
    13 {
    14 	color= QColor(255,255,0);
    15 }
    16 
    17 Selection::~Selection()
    18 {
    19 }
    20 
    21 void Selection::setMapEditor (MapEditor *me)
    22 {
    23 	mapEditor=me;
    24 	mapCenter=me->getMapCenter();
    25 	scene=mapCenter->getScene();
    26 }
    27 
    28 void Selection::copy(const Selection &other)
    29 {
    30 	mapCenter=other.mapCenter;
    31 	selectList=other.selectList;
    32 	lastSelectList=other.lastSelectList;
    33 }
    34 
    35 void Selection::clear()
    36 {
    37 	unselect();
    38 	lastSelectList.clear();
    39 }
    40 
    41 void Selection::update()
    42 {
    43 	QRectF bbox;
    44 	int w=0;
    45 	for (int i=0; i< selectList.count(); ++i) 
    46 	{
    47 		bbox=selectList.at(i)->getBBox();
    48 		selboxList.at(i)->setRect (
    49 			bbox.x()-w,bbox.y()-w, 
    50 			bbox.width()+2*w, bbox.height()+2*w);
    51 		selboxList.at(i)->setPen (color);	
    52 		selboxList.at(i)->setBrush (color);	
    53 	}	
    54 }
    55 
    56 void Selection::setColor (QColor col)
    57 {
    58 	color=col;
    59 	update();
    60 }
    61 
    62 QColor Selection::getColor ()
    63 {
    64 	return color;
    65 }
    66 
    67 bool Selection::select(LinkableMapObj *lmo)	// TODO no multiselections yet
    68 {
    69 	if (!selectList.isEmpty()) unselect();
    70 	selectList.append (lmo);
    71 	QGraphicsRectItem *sb = scene->addRect(
    72 		QRectF(0,0,0,0), 
    73 		QPen(color),
    74 		color);
    75 	sb->setZValue(Z_SELBOX);
    76 	sb->show();
    77 	selboxList.append (sb);
    78 	lmo->select();
    79 	update();
    80 	mainWindow->updateSatellites (mapEditor);	// update branchPropWindow...
    81 	return true;
    82 }
    83 
    84 bool Selection::select (const QString &s)	// TODO no multiselections yet
    85 {
    86 	LinkableMapObj *lmo=mapCenter->findObjBySelect(s);
    87 
    88 	// Finally select the found object
    89 	if (lmo)
    90 	{
    91 		unselect();
    92 		select (lmo);
    93 		return true;
    94 	} 
    95 	return false;
    96 
    97 }
    98 
    99 bool Selection::reselect ()	// TODO no multiselections yet
   100 {
   101 	if (!lastSelectList.isEmpty())
   102 	{
   103 		select (lastSelectList.first());
   104 		return true;
   105 	}
   106 	return false;
   107 
   108 }
   109 
   110 void Selection::unselect()
   111 {
   112 	if (!selectList.isEmpty() )
   113 	{
   114 		for (int i=0; i< selectList.count(); ++i) 
   115 			selectList.at(i)->unselect();
   116 		lastSelectList=selectList;
   117 		selectList.clear();
   118 		while (!selboxList.isEmpty() )
   119 			delete selboxList.takeFirst();
   120 
   121 	}	
   122 }
   123 
   124 bool Selection::isEmpty()
   125 {
   126 	return selectList.isEmpty();
   127 }
   128 
   129 uint Selection::count()
   130 {
   131 	return selectList.count();
   132 }
   133 
   134 Selection::Type Selection::type() // TODO no multiselections yet
   135 {
   136 	if (!selectList.isEmpty())
   137 	{
   138 		LinkableMapObj *sel=selectList.first();
   139 		if (typeid (*sel)==typeid (BranchObj)) return Branch;
   140 		if (typeid (*sel)==typeid (MapCenterObj)) return MapCenter;
   141 		if (typeid (*sel)==typeid (FloatImageObj)) return FloatImage;
   142 	}
   143 	return Undefined;
   144 }
   145 
   146 LinkableMapObj* Selection::first()
   147 {
   148 	if (!selectList.isEmpty())
   149 		return selectList.first();
   150 	else	
   151 		return NULL;
   152 }
   153 
   154 LinkableMapObj* Selection::single()
   155 {
   156 	if (selectList.count() == 1)
   157 		return selectList.first();
   158 	else	
   159 		return NULL;
   160 }
   161 
   162 BranchObj* Selection::getBranch()
   163 {
   164 	if (!selectList.isEmpty())
   165 	{
   166 		LinkableMapObj *sel=selectList.first();
   167 		if (typeid (*sel)==typeid (BranchObj) ||
   168 		    typeid (*sel)==typeid (MapCenterObj)) 
   169 			return (BranchObj*)sel;
   170 	}
   171 		return NULL;
   172 }
   173 
   174 FloatImageObj* Selection::getFloatImage()
   175 {
   176 	if (!selectList.isEmpty())
   177 	{
   178 		LinkableMapObj *sel=selectList.first();
   179 		if (typeid (*sel)==typeid (FloatImageObj)) 
   180 			return (FloatImageObj*)sel;
   181 	}
   182 		return NULL;
   183 }
   184 
   185 QString Selection::getSelectString()// TODO no multiselections yet
   186 {
   187 	if (selectList.count()==1)
   188 		return selectList.first()->getSelectString();
   189 	else
   190 		return"";
   191 }
   192 
   193