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