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