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