renderVector.cpp 8.83 KB
Newer Older
1 2 3 4 5 6
#include "renderVector.h"

#include "mapHandler.h"

#include "Algo/Import/import.h"

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

PerMapParameterSet::PerMapParameterSet(MapHandlerGen* map) :
	positionVBO(NULL),
	vectorsScaleFactor(1.0f)
{
	QList<Utils::VBO*> vbos = map->getVBOList();
	for(int i = 0; i < vbos.count(); ++i)
	{
		if(vbos[i]->name() == "position") // try to select a VBO named "position"
			positionVBO = vbos[i];
	}

	if(positionVBO == NULL && vbos.count() > 0)
		positionVBO = vbos[0];
}


24 25
bool RenderVectorPlugin::enable()
{
26
	m_dockTab = new RenderVectorDockTab(this);
27 28
	addTabInDock(m_dockTab, "RenderVector");

29 30
	m_vectorShader = new Utils::ShaderVectorPerVertex();
	m_vectorShader->setColor(Geom::Vec4f(1.0f, 0.0f, 0.0f, 1.0f));
31 32 33

	registerShader(m_vectorShader);

34 35
	connect(m_dockTab->mapList, SIGNAL(itemSelectionChanged()), this, SLOT(cb_selectedMapChanged()));
	connect(m_dockTab->combo_positionVBO, SIGNAL(currentIndexChanged(int)), this, SLOT(cb_positionVBOChanged(int)));
36
	connect(m_dockTab->list_vectorVBO, SIGNAL(itemSelectionChanged()), this, SLOT(cb_selectedVectorsVBOChanged()));
37 38
	connect(m_dockTab->slider_vectorsScaleFactor, SIGNAL(valueChanged(int)), this, SLOT(cb_vectorsScaleFactorChanged(int)));

39 40 41 42
	connect(m_window, SIGNAL(viewAndPluginLinked(View*, Plugin*)), this, SLOT(viewLinked(View*, Plugin*)));
	connect(m_window, SIGNAL(viewAndPluginUnlinked(View*, Plugin*)), this, SLOT(viewUnlinked(View*, Plugin*)));
	connect(m_window, SIGNAL(currentViewChanged(View*)), this, SLOT(currentViewChanged(View*)));

43 44 45 46 47 48 49 50 51 52
	return true;
}

void RenderVectorPlugin::disable()
{
	delete m_vectorShader;
}

void RenderVectorPlugin::redraw(View* view)
{
53
	ParameterSet* params = h_viewParams[view];
54 55 56 57

	const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
	foreach(MapHandlerGen* m, maps)
	{
58
		const PerMapParameterSet& p = params->perMap[m->getName()];
59 60
		m_vectorShader->setScale(m->getBBdiagSize() / 100.0f * p.vectorsScaleFactor) ;
		if(p.positionVBO != NULL)
61 62
		{
			m_vectorShader->setAttributePosition(p.positionVBO) ;
63 64 65 66 67 68
			for(std::vector<Utils::VBO*>::const_iterator it = p.vectorVBO.begin(); it != p.vectorVBO.end(); ++it)
			{
				m_vectorShader->setAttributeVector(*it) ;
				glLineWidth(1.0f) ;
				m->draw(m_vectorShader, Algo::Render::GL2::POINTS) ;
			}
69
		}
70 71 72
	}
}

73
void RenderVectorPlugin::viewLinked(View* view, Plugin* plugin)
74
{
75
	if(plugin == this)
76
	{
77 78 79 80 81 82 83 84 85 86
		ParameterSet* params = new ParameterSet();
		h_viewParams.insert(view, params);
		const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
		foreach(MapHandlerGen* map, maps)
		{
			PerMapParameterSet p(map);
			params->perMap.insert(map->getName(), p);
		}
		if (!maps.empty())
			changeSelectedMap(view, maps[0]);
87

88 89
		connect(view, SIGNAL(mapLinked(MapHandlerGen*)), this, SLOT(mapLinked(MapHandlerGen*)));
		connect(view, SIGNAL(mapUnlinked(MapHandlerGen*)), this, SLOT(mapUnlinked(MapHandlerGen*)));
90

91 92 93
		if(view->isCurrentView())
			m_dockTab->refreshUI(params);
	}
94 95
}

96
void RenderVectorPlugin::viewUnlinked(View* view, Plugin* plugin)
97
{
98 99 100
	if(plugin == this)
	{
		h_viewParams.remove(view);
101

102 103 104
		disconnect(view, SIGNAL(mapLinked(MapHandlerGen*)), this, SLOT(mapLinked(MapHandlerGen*)));
		disconnect(view, SIGNAL(mapUnlinked(MapHandlerGen*)), this, SLOT(mapUnlinked(MapHandlerGen*)));
	}
105 106 107 108
}

void RenderVectorPlugin::currentViewChanged(View* view)
{
109 110
	if(isLinkedToView(view))
		m_dockTab->refreshUI(h_viewParams[view]);
111 112
}

113
void RenderVectorPlugin::mapLinked(MapHandlerGen* m)
114
{
115
	View* view = static_cast<View*>(QObject::sender());
116
	assert(isLinkedToView(view));
117

118
	ParameterSet* params = h_viewParams[view];
119
	PerMapParameterSet p(m);
120
	params->perMap.insert(m->getName(), p);
121 122 123 124
	if(params->selectedMap == NULL || params->perMap.count() == 1)
		changeSelectedMap(view, m);
	else
		m_dockTab->refreshUI(params);
125 126
}

127
void RenderVectorPlugin::mapUnlinked(MapHandlerGen* m)
128
{
129
	View* view = static_cast<View*>(QObject::sender());
130
	assert(isLinkedToView(view));
131

132
	ParameterSet* params = h_viewParams[view];
133
	params->perMap.remove(m->getName());
134 135 136

	if(params->selectedMap == m)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
137
		if(!params->perMap.empty())
138
			changeSelectedMap(view, m_window->getMap(params->perMap.begin().key()));
Pierre Kraemer's avatar
Pierre Kraemer committed
139
		else
140 141 142
			changeSelectedMap(view, NULL);
	}
	else
143
		m_dockTab->refreshUI(params);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
}

void RenderVectorPlugin::vboAdded(Utils::VBO* vbo)
{
	m_dockTab->refreshUI(h_viewParams[m_window->getCurrentView()]);
}

void RenderVectorPlugin::vboRemoved(Utils::VBO* vbo)
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());

	View* view = m_window->getCurrentView();
	ParameterSet* params = h_viewParams[view];
	if(params->perMap[map->getName()].positionVBO == vbo)
		changePositionVBO(view, map, NULL);
}

void RenderVectorPlugin::changeSelectedMap(View* view, MapHandlerGen* map)
{
	ParameterSet* params = h_viewParams[view];

	MapHandlerGen* prev = params->selectedMap;
	params->selectedMap = map;

	if(view->isCurrentView())
	{
		if(prev)
			disconnect(map, SIGNAL(vboAdded(Utils::VBO*)), this, SLOT(vboAdded(Utils::VBO*)));
		if(map)
			connect(map, SIGNAL(vboAdded(Utils::VBO*)), this, SLOT(vboAdded(Utils::VBO*)));

		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void RenderVectorPlugin::changePositionVBO(View* view, MapHandlerGen* map, Utils::VBO* vbo)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].positionVBO = vbo;

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void RenderVectorPlugin::changeSelectedVectorsVBO(View* view, MapHandlerGen* map, const std::vector<Utils::VBO*>& vbos)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].vectorVBO = vbos;

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void RenderVectorPlugin::changeVectorsScaleFactor(View* view, MapHandlerGen* map, int i)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].vectorsScaleFactor = i / 50.0;

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
		view->updateGL();
213
	}
214 215
}

216
void RenderVectorPlugin::cb_selectedMapChanged()
217
{
218
	if(!b_refreshingUI)
219
	{
220 221
		QList<QListWidgetItem*> currentItems = m_dockTab->mapList->selectedItems();
		if(!currentItems.empty())
222
			changeSelectedMap(m_window->getCurrentView(), m_window->getMap(currentItems[0]->text()));
223 224 225
	}
}

226
void RenderVectorPlugin::cb_positionVBOChanged(int index)
227
{
228
	if(!b_refreshingUI)
229
	{
230 231 232
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
		changePositionVBO(view, map, map->getVBO(m_dockTab->combo_positionVBO->currentText()));
233
	}
234
}
235

236
void RenderVectorPlugin::cb_selectedVectorsVBOChanged()
237 238
{
	if(!b_refreshingUI)
239
	{
240 241
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
242
		QList<QListWidgetItem*> currentItems = m_dockTab->list_vectorVBO->selectedItems();
243
		std::vector<Utils::VBO*> vbos;
244
		foreach(QListWidgetItem* item, currentItems)
245 246
			vbos.push_back(map->getVBO(item->text()));
		changeSelectedVectorsVBO(view, map, vbos);
247 248 249
	}
}

250 251 252
void RenderVectorPlugin::cb_vectorsScaleFactorChanged(int i)
{
	if(!b_refreshingUI)
253
	{
254 255 256
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
		changeVectorsScaleFactor(view, map, i);
257 258 259 260 261
	}
}



262
void RenderVectorDockTab::refreshUI(ParameterSet* params)
263
{
264
	plugin->setRefreshingUI(true);
265

266 267
	mapList->clear();
	combo_positionVBO->clear();
268
	list_vectorVBO->clear();
269

270
	MapHandlerGen* map = params->selectedMap;
271

272 273
	QHash<QString, PerMapParameterSet>::const_iterator i = params->perMap.constBegin();
	while (i != params->perMap.constEnd())
274
	{
275 276
		mapList->addItem(i.key());
		if(map != NULL && i.key() == map->getName())
277
		{
278 279 280 281 282 283
			QList<QListWidgetItem*> item = mapList->findItems(map->getName(), Qt::MatchExactly);
			item[0]->setSelected(true);

			PerMapParameterSet& p = params->perMap[map->getName()];

			QList<Utils::VBO*> vbos = map->getVBOList();
284
			unsigned int j = 0;
285 286
			for(int i = 0; i < vbos.count(); ++i)
			{
287 288
				if(vbos[i]->dataSize() == 3)
				{
289 290 291 292 293 294 295 296 297 298 299 300
					combo_positionVBO->addItem(QString::fromStdString(vbos[i]->name()));
					if(p.positionVBO == NULL)
					{										// if nothing is specified in the parameter set
						if(vbos[i]->name() == "position")	// try to select a VBO named "position"
						{
							p.positionVBO = vbos[i];
							combo_positionVBO->setCurrentIndex(j);
						}
					}
					else if(vbos[i] == p.positionVBO)
						combo_positionVBO->setCurrentIndex(j);

301 302 303
					list_vectorVBO->addItem(QString::fromStdString(vbos[i]->name()));
					if(std::find(p.vectorVBO.begin(), p.vectorVBO.end(), vbos[i]) != p.vectorVBO.end())
						list_vectorVBO->item(j)->setSelected(true);
304

305 306
					++j;
				}
307
			}
308

309 310 311 312 313
			if(p.positionVBO == NULL && vbos.count() > 0)
			{
				p.positionVBO = vbos[0];
				combo_positionVBO->setCurrentIndex(0);
			}
314 315

			slider_vectorsScaleFactor->setSliderPosition(p.vectorsScaleFactor * 50.0);
316
		}
317
		++i;
318
	}
319 320

	plugin->setRefreshingUI(false);
321 322 323 324 325 326 327 328 329
}



#ifndef DEBUG
Q_EXPORT_PLUGIN2(RenderVectorPlugin, RenderVectorPlugin)
#else
Q_EXPORT_PLUGIN2(RenderVectorPluginD, RenderVectorPlugin)
#endif