selection.cpp
author insilmaril
Tue, 08 Apr 2008 08:28:37 +0000
changeset 683 7df0b7986274
parent 651 1e51ba080947
child 721 12958f987bcf
permissions -rw-r--r--
1.11.7: Fixed hanging find function
     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 	LinkableMapObj *lmo=model->findObjBySelect(s);
    86 
    87 	// Finally select the found object
    88 	if (lmo)
    89 	{
    90 		unselect();
    91 		select (lmo);
    92 		return true;
    93 	} 
    94 	return false;
    95 
    96 }
    97 
    98 bool Selection::reselect ()	// TODO no multiselections yet
    99 {
   100 	if (!lastSelectList.isEmpty())
   101 	{
   102 		select (lastSelectList.first());
   103 		return true;
   104 	}
   105 	return false;
   106 
   107 }
   108 
   109 void Selection::unselect()
   110 {
   111 	if (!selectList.isEmpty() )
   112 	{
   113 		for (int i=0; i< selectList.count(); ++i) 
   114 			selectList.at(i)->unselect();
   115 		lastSelectList=selectList;
   116 		selectList.clear();
   117 		while (!selboxList.isEmpty() )
   118 			delete selboxList.takeFirst();
   119 
   120 	}	
   121 }
   122 
   123 bool Selection::isEmpty()
   124 {
   125 	return selectList.isEmpty();
   126 }
   127 
   128 uint Selection::count()
   129 {
   130 	return selectList.count();
   131 }
   132 
   133 Selection::Type Selection::type() // TODO no multiselections yet
   134 {
   135 	if (!selectList.isEmpty())
   136 	{
   137 		LinkableMapObj *sel=selectList.first();
   138 		if (typeid (*sel)==typeid (BranchObj)) return Branch;
   139 		if (typeid (*sel)==typeid (MapCenterObj)) return MapCenter;
   140 		if (typeid (*sel)==typeid (FloatImageObj)) return FloatImage;
   141 	}
   142 	return Undefined;
   143 }
   144 
   145 LinkableMapObj* Selection::first()
   146 {
   147 	if (!selectList.isEmpty())
   148 		return selectList.first();
   149 	else	
   150 		return NULL;
   151 }
   152 
   153 LinkableMapObj* Selection::single()
   154 {
   155 	if (selectList.count() == 1)
   156 		return selectList.first();
   157 	else	
   158 		return NULL;
   159 }
   160 
   161 BranchObj* Selection::getBranch()
   162 {
   163 	if (!selectList.isEmpty())
   164 	{
   165 		LinkableMapObj *sel=selectList.first();
   166 		if (typeid (*sel)==typeid (BranchObj) ||
   167 		    typeid (*sel)==typeid (MapCenterObj)) 
   168 			return (BranchObj*)sel;
   169 	}
   170 		return NULL;
   171 }
   172 
   173 FloatImageObj* Selection::getFloatImage()
   174 {
   175 	if (!selectList.isEmpty())
   176 	{
   177 		LinkableMapObj *sel=selectList.first();
   178 		if (typeid (*sel)==typeid (FloatImageObj)) 
   179 			return (FloatImageObj*)sel;
   180 	}
   181 		return NULL;
   182 }
   183 
   184 QString Selection::getSelectString()// TODO no multiselections yet
   185 {
   186 	if (selectList.count()==1)
   187 	{
   188 		return model->getSelectString (selectList.first() );
   189 	}
   190 	else
   191 		return"";
   192 }
   193 
   194