selection.cpp
author insilmaril
Fri, 05 Mar 2010 19:15:08 +0000
branchrelease-1-12-maintained
changeset 79 32f499cbe874
parent 78 1a72c8f24c6e
permissions -rw-r--r--
Fixed changelog
     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 	for (int i=0; i< selectList.count(); ++i) 
    44 	{
    45 		bbox=selectList.at(i)->getBBox();
    46 		selboxList.at(i)->setRect (
    47 			bbox.x(),bbox.y(), 
    48 			bbox.width(), bbox.height());
    49 		selboxList.at(i)->setPen (color);	
    50 		selboxList.at(i)->setBrush (color);	
    51 		selboxList.at(i)->show();
    52 	}	
    53 	model->getMapEditor()->getScene()->update();
    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->getMapEditor() );	
    81 	return true;
    82 }
    83 
    84 bool Selection::select (const QString &s)	// TODO no multiselections yet
    85 {
    86 	if (s.isEmpty())
    87 	{
    88 		unselect();
    89 		return true;
    90 	}
    91 
    92 	LinkableMapObj *lmo=model->findObjBySelect(s);
    93 
    94 	// Finally select the found object
    95 	if (lmo)
    96 	{
    97 		unselect();
    98 		select (lmo);
    99 		return true;
   100 	} 
   101 	return false;
   102 
   103 }
   104 
   105 bool Selection::reselect ()	// TODO no multiselections yet
   106 {
   107 	if (!lastSelectList.isEmpty())
   108 	{
   109 		select (lastSelectList.first());
   110 		return true;
   111 	}
   112 	return false;
   113 
   114 }
   115 
   116 void Selection::unselect()
   117 {
   118 	if (!selectList.isEmpty() )
   119 	{
   120 		for (int i=0; i< selectList.count(); ++i) 
   121 			selectList.at(i)->unselect();
   122 		lastSelectList=selectList;
   123 		selectList.clear();
   124 		while (!selboxList.isEmpty() )
   125 			delete selboxList.takeFirst();
   126 
   127 	}	
   128 }
   129 
   130 bool Selection::isEmpty()
   131 {
   132 	return selectList.isEmpty();
   133 }
   134 
   135 uint Selection::count()
   136 {
   137 	return selectList.count();
   138 }
   139 
   140 Selection::Type Selection::type() // TODO no multiselections yet
   141 {
   142 	if (!selectList.isEmpty())
   143 	{
   144 		LinkableMapObj *sel=selectList.first();
   145 		if (typeid (*sel)==typeid (BranchObj)) return Branch;
   146 		if (typeid (*sel)==typeid (MapCenterObj)) return MapCenter;
   147 		if (typeid (*sel)==typeid (FloatImageObj)) return FloatImage;
   148 	}
   149 	return Undefined;
   150 }
   151 
   152 LinkableMapObj* Selection::first()
   153 {
   154 	if (!selectList.isEmpty())
   155 		return selectList.first();
   156 	else	
   157 		return NULL;
   158 }
   159 
   160 LinkableMapObj* Selection::single()
   161 {
   162 	if (selectList.count() == 1)
   163 		return selectList.first();
   164 	else	
   165 		return NULL;
   166 }
   167 
   168 BranchObj* Selection::getBranch()
   169 {
   170 	if (!selectList.isEmpty())
   171 	{
   172 		LinkableMapObj *sel=selectList.first();
   173 		if (typeid (*sel)==typeid (BranchObj) ||
   174 		    typeid (*sel)==typeid (MapCenterObj)) 
   175 			return (BranchObj*)sel;
   176 	}
   177 		return NULL;
   178 }
   179 
   180 FloatImageObj* Selection::getFloatImage()
   181 {
   182 	if (!selectList.isEmpty())
   183 	{
   184 		LinkableMapObj *sel=selectList.first();
   185 		if (typeid (*sel)==typeid (FloatImageObj)) 
   186 			return (FloatImageObj*)sel;
   187 	}
   188 		return NULL;
   189 }
   190 
   191 QString Selection::getSelectString()// TODO no multiselections yet
   192 {
   193 	if (selectList.count()==1)
   194 	{
   195 		return model->getSelectString (selectList.first() );
   196 	}
   197 	else
   198 		return"";
   199 }
   200 
   201