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