selection.cpp
author insilmaril
Mon, 04 Aug 2008 13:35:54 +0000
changeset 724 cf14046909cd
parent 723 11f9124c1cca
child 726 7f43b93242aa
permissions -rw-r--r--
Adding/moving MCO works again
     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 	blocked=false;
    17 }
    18 
    19 Selection::~Selection()
    20 {
    21 }
    22 
    23 void Selection::setModel (VymModel *m)
    24 {
    25 	model=m;
    26 	scene=model->getScene();
    27 }
    28 
    29 void Selection::copy(const Selection &other)
    30 {
    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() // FIXME this needs to be adapted to several views
    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 (model);	
    81 	return true;
    82 }
    83 
    84 bool Selection::select (const QString &s)	// TODO no multiselections yet
    85 {
    86 	LinkableMapObj *lmo=model->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::isBlocked()
   125 {
   126 	return blocked;
   127 }
   128 
   129 void Selection::block()
   130 {
   131 	blocked=true;
   132 }
   133 
   134 void Selection::unblock()
   135 {
   136 	blocked=false;
   137 }
   138 
   139 bool Selection::isEmpty()
   140 {
   141 	return selectList.isEmpty();
   142 }
   143 
   144 uint Selection::count()
   145 {
   146 	return selectList.count();
   147 }
   148 
   149 Selection::Type Selection::type() // TODO no multiselections yet
   150 {
   151 	if (!selectList.isEmpty())
   152 	{
   153 		LinkableMapObj *sel=selectList.first();
   154 		if (typeid (*sel)==typeid (BranchObj)) return Branch;
   155 		if (typeid (*sel)==typeid (MapCenterObj)) return MapCenter;
   156 		if (typeid (*sel)==typeid (FloatImageObj)) return FloatImage;
   157 	}
   158 	return Undefined;
   159 }
   160 
   161 LinkableMapObj* Selection::first()
   162 {
   163 	if (!selectList.isEmpty())
   164 		return selectList.first();
   165 	else	
   166 		return NULL;
   167 }
   168 
   169 LinkableMapObj* Selection::single()
   170 {
   171 	if (selectList.count() == 1)
   172 		return selectList.first();
   173 	else	
   174 		return NULL;
   175 }
   176 
   177 BranchObj* Selection::getBranch()
   178 {
   179 	if (!selectList.isEmpty())
   180 	{
   181 		LinkableMapObj *sel=selectList.first();
   182 		if (typeid (*sel)==typeid (BranchObj) ||
   183 		    typeid (*sel)==typeid (MapCenterObj)) 
   184 			return (BranchObj*)sel;
   185 	}
   186 		return NULL;
   187 }
   188 
   189 FloatImageObj* Selection::getFloatImage()
   190 {
   191 	if (!selectList.isEmpty())
   192 	{
   193 		LinkableMapObj *sel=selectList.first();
   194 		if (typeid (*sel)==typeid (FloatImageObj)) 
   195 			return (FloatImageObj*)sel;
   196 	}
   197 		return NULL;
   198 }
   199 
   200 QString Selection::getSelectString()// TODO no multiselections yet
   201 {
   202 	if (selectList.count()==1)
   203 	{
   204 		return model->getSelectString (selectList.first() );
   205 	}
   206 	else
   207 		return"";
   208 }
   209 
   210