selection.cpp
author insilmaril
Tue, 05 Aug 2008 07:36:53 +0000
changeset 725 7ea31701156e
parent 723 11f9124c1cca
child 726 7f43b93242aa
permissions -rw-r--r--
Preview: Added QTreeView to mainwindow (not yet functional)
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@721
    16
	blocked=false;
insilmaril@0
    17
}
insilmaril@0
    18
insilmaril@0
    19
Selection::~Selection()
insilmaril@0
    20
{
insilmaril@0
    21
}
insilmaril@0
    22
insilmaril@641
    23
void Selection::setModel (VymModel *m)
insilmaril@0
    24
{
insilmaril@641
    25
	model=m;
insilmaril@641
    26
	scene=model->getScene();
insilmaril@0
    27
}
insilmaril@0
    28
insilmaril@0
    29
void Selection::copy(const Selection &other)
insilmaril@0
    30
{
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@721
    41
void Selection::update() // FIXME this needs to be adapted to several views
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@723
    80
	mainWindow->updateSatellites (model);	
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@641
    86
	LinkableMapObj *lmo=model->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@721
   124
bool Selection::isBlocked()
insilmaril@721
   125
{
insilmaril@721
   126
	return blocked;
insilmaril@721
   127
}
insilmaril@721
   128
insilmaril@721
   129
void Selection::block()
insilmaril@721
   130
{
insilmaril@721
   131
	blocked=true;
insilmaril@721
   132
}
insilmaril@721
   133
insilmaril@721
   134
void Selection::unblock()
insilmaril@721
   135
{
insilmaril@721
   136
	blocked=false;
insilmaril@721
   137
}
insilmaril@721
   138
insilmaril@0
   139
bool Selection::isEmpty()
insilmaril@0
   140
{
insilmaril@0
   141
	return selectList.isEmpty();
insilmaril@0
   142
}
insilmaril@0
   143
insilmaril@0
   144
uint Selection::count()
insilmaril@0
   145
{
insilmaril@0
   146
	return selectList.count();
insilmaril@0
   147
}
insilmaril@16
   148
insilmaril@492
   149
Selection::Type Selection::type() // TODO no multiselections yet
insilmaril@16
   150
{
insilmaril@421
   151
	if (!selectList.isEmpty())
insilmaril@421
   152
	{
insilmaril@421
   153
		LinkableMapObj *sel=selectList.first();
insilmaril@421
   154
		if (typeid (*sel)==typeid (BranchObj)) return Branch;
insilmaril@421
   155
		if (typeid (*sel)==typeid (MapCenterObj)) return MapCenter;
insilmaril@421
   156
		if (typeid (*sel)==typeid (FloatImageObj)) return FloatImage;
insilmaril@421
   157
	}
insilmaril@421
   158
	return Undefined;
insilmaril@421
   159
}
insilmaril@421
   160
insilmaril@424
   161
LinkableMapObj* Selection::first()
insilmaril@424
   162
{
insilmaril@424
   163
	if (!selectList.isEmpty())
insilmaril@424
   164
		return selectList.first();
insilmaril@424
   165
	else	
insilmaril@424
   166
		return NULL;
insilmaril@424
   167
}
insilmaril@424
   168
insilmaril@424
   169
LinkableMapObj* Selection::single()
insilmaril@424
   170
{
insilmaril@424
   171
	if (selectList.count() == 1)
insilmaril@424
   172
		return selectList.first();
insilmaril@424
   173
	else	
insilmaril@424
   174
		return NULL;
insilmaril@424
   175
}
insilmaril@424
   176
insilmaril@424
   177
BranchObj* Selection::getBranch()
insilmaril@424
   178
{
insilmaril@424
   179
	if (!selectList.isEmpty())
insilmaril@424
   180
	{
insilmaril@424
   181
		LinkableMapObj *sel=selectList.first();
insilmaril@424
   182
		if (typeid (*sel)==typeid (BranchObj) ||
insilmaril@424
   183
		    typeid (*sel)==typeid (MapCenterObj)) 
insilmaril@424
   184
			return (BranchObj*)sel;
insilmaril@424
   185
	}
insilmaril@424
   186
		return NULL;
insilmaril@424
   187
}
insilmaril@424
   188
insilmaril@424
   189
FloatImageObj* Selection::getFloatImage()
insilmaril@424
   190
{
insilmaril@424
   191
	if (!selectList.isEmpty())
insilmaril@424
   192
	{
insilmaril@424
   193
		LinkableMapObj *sel=selectList.first();
insilmaril@424
   194
		if (typeid (*sel)==typeid (FloatImageObj)) 
insilmaril@424
   195
			return (FloatImageObj*)sel;
insilmaril@424
   196
	}
insilmaril@424
   197
		return NULL;
insilmaril@424
   198
}
insilmaril@424
   199
insilmaril@421
   200
QString Selection::getSelectString()// TODO no multiselections yet
insilmaril@421
   201
{
insilmaril@16
   202
	if (selectList.count()==1)
insilmaril@650
   203
	{
insilmaril@650
   204
		return model->getSelectString (selectList.first() );
insilmaril@650
   205
	}
insilmaril@16
   206
	else
insilmaril@16
   207
		return"";
insilmaril@16
   208
}
insilmaril@16
   209
insilmaril@16
   210