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