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