selection.cpp
author insilmaril
Mon, 06 Oct 2008 11:10:20 +0000
changeset 726 7f43b93242aa
parent 723 11f9124c1cca
child 728 a8a98a94899a
permissions -rw-r--r--
Various fixes, also from 1.12. branch
     1 #include <typeinfo>
     2 
     3 #include "selection.h"
     4 
     5 #include "mainwindow.h"
     6 #include "vymmodel.h"
     7 
     8 
     9 
    10 extern Main *mainWindow;
    11 
    12 Selection::Selection()
    13 {
    14 	color= QColor(255,255,0);
    15 	blocked=false;
    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() // FIXME this needs to be adapted to several views
    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);	
    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::isBlocked()
   124 {
   125 	return blocked;
   126 }
   127 
   128 void Selection::block()
   129 {
   130 	blocked=true;
   131 }
   132 
   133 void Selection::unblock()
   134 {
   135 	blocked=false;
   136 }
   137 
   138 bool Selection::isEmpty()
   139 {
   140 	return selectList.isEmpty();
   141 }
   142 
   143 uint Selection::count()
   144 {
   145 	return selectList.count();
   146 }
   147 
   148 Selection::Type Selection::type() // TODO no multiselections yet
   149 {
   150 	if (!selectList.isEmpty())
   151 	{
   152 		LinkableMapObj *sel=selectList.first();
   153 		if (typeid (*sel)==typeid (BranchObj)) return Branch;
   154 		if (typeid (*sel)==typeid (MapCenterObj)) return MapCenter;
   155 		if (typeid (*sel)==typeid (FloatImageObj)) return FloatImage;
   156 	}
   157 	return Undefined;
   158 }
   159 
   160 LinkableMapObj* Selection::first()
   161 {
   162 	if (!selectList.isEmpty())
   163 		return selectList.first();
   164 	else	
   165 		return NULL;
   166 }
   167 
   168 LinkableMapObj* Selection::single()
   169 {
   170 	if (selectList.count() == 1)
   171 		return selectList.first();
   172 	else	
   173 		return NULL;
   174 }
   175 
   176 BranchObj* Selection::getBranch()
   177 {
   178 	if (!selectList.isEmpty())
   179 	{
   180 		LinkableMapObj *sel=selectList.first();
   181 		if (typeid (*sel)==typeid (BranchObj) ||
   182 		    typeid (*sel)==typeid (MapCenterObj)) 
   183 			return (BranchObj*)sel;
   184 	}
   185 		return NULL;
   186 }
   187 
   188 TreeItem* Selection::getBranchItem()
   189 {
   190 	BranchObj* bo=getBranch();
   191 	if (bo) return bo->getTreeItem();
   192 	return NULL;
   193 }
   194 
   195 FloatImageObj* Selection::getFloatImage()
   196 {
   197 	if (!selectList.isEmpty())
   198 	{
   199 		LinkableMapObj *sel=selectList.first();
   200 		if (typeid (*sel)==typeid (FloatImageObj)) 
   201 			return (FloatImageObj*)sel;
   202 	}
   203 		return NULL;
   204 }
   205 
   206 QString Selection::getSelectString()// TODO no multiselections yet
   207 {
   208 	if (selectList.count()==1)
   209 	{
   210 		return model->getSelectString (selectList.first() );
   211 	}
   212 	else
   213 		return"";
   214 }
   215 
   216