renderVector.cpp 6.1 KB
Newer Older
1 2 3 4
#include "renderVector.h"

#include "mapHandler.h"

5 6
namespace CGoGN
{
7

8 9
namespace SCHNApps
{
10 11 12 13 14

PerMapParameterSet::PerMapParameterSet(MapHandlerGen* map) :
	positionVBO(NULL),
	vectorsScaleFactor(1.0f)
{
15 16
	bool positionFound = false;

17 18 19
	QList<Utils::VBO*> vbos = map->getVBOList();
	for(int i = 0; i < vbos.count(); ++i)
	{
20 21 22 23 24 25 26 27 28
		if(vbos[i]->dataSize() == 3)
		{
			if(!positionFound) positionVBO = vbos[i];
			if(vbos[i]->name() == "position") // try to select a VBO named "position"
			{
				positionVBO = vbos[i];
				positionFound = true;
			}
		}
29 30 31 32
	}
}


33 34
bool RenderVectorPlugin::enable()
{
35
	m_dockTab = new RenderVectorDockTab(m_window, this);
36 37
	addTabInDock(m_dockTab, "RenderVector");

38 39
	m_vectorShader = new Utils::ShaderVectorPerVertex();
	m_vectorShader->setColor(Geom::Vec4f(1.0f, 0.0f, 0.0f, 1.0f));
40 41 42

	registerShader(m_vectorShader);

43 44 45 46
	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*)));

47 48 49 50 51 52 53 54 55 56
	return true;
}

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

void RenderVectorPlugin::redraw(View* view)
{
57
	ParameterSet* params = h_viewParams[view];
58 59 60 61

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

77
void RenderVectorPlugin::viewLinked(View* view, Plugin* plugin)
78
{
79
	if(plugin == this)
80
	{
81 82
		ParameterSet* params = new ParameterSet();
		h_viewParams.insert(view, params);
83

84
		const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
85 86
		foreach(MapHandlerGen* mh, maps)
			addManagedMap(view, mh);
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
	if(plugin == this)
	{
100 101 102 103 104 105
		const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
		foreach(MapHandlerGen* mh, maps)
			removeManagedMap(view, mh);

		ParameterSet* params = h_viewParams[view];
		delete params;
106
		h_viewParams.remove(view);
107

108 109 110
		disconnect(view, SIGNAL(mapLinked(MapHandlerGen*)), this, SLOT(mapLinked(MapHandlerGen*)));
		disconnect(view, SIGNAL(mapUnlinked(MapHandlerGen*)), this, SLOT(mapUnlinked(MapHandlerGen*)));
	}
111 112 113 114
}

void RenderVectorPlugin::currentViewChanged(View* view)
{
115 116
	if(isLinkedToView(view))
		m_dockTab->refreshUI(h_viewParams[view]);
117 118
}

119
void RenderVectorPlugin::mapLinked(MapHandlerGen* m)
120
{
121
	View* view = static_cast<View*>(QObject::sender());
122
	assert(isLinkedToView(view));
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	addManagedMap(view, m);
}

void RenderVectorPlugin::mapUnlinked(MapHandlerGen* m)
{
	View* view = static_cast<View*>(QObject::sender());
	assert(isLinkedToView(view));
	removeManagedMap(view, m);
}

void RenderVectorPlugin::addManagedMap(View* v, MapHandlerGen *m)
{
//	connect(m, SIGNAL(attributeModified(unsigned int, QString)), this, SLOT(attributeModified(unsigned int, QString)));
//	connect(m, SIGNAL(connectivityModified()), this, SLOT(connectivityModified()));

	ParameterSet* params = h_viewParams[v];
	PerMapParameterSet* perMap = new PerMapParameterSet(m);

	params->perMap.insert(m->getName(), perMap);
142 143

	if(params->selectedMap == NULL || params->perMap.count() == 1)
144
		changeSelectedMap(v, m);
145 146
	else
		m_dockTab->refreshUI(params);
147 148
}

149
void RenderVectorPlugin::removeManagedMap(View *v, MapHandlerGen *m)
150
{
151 152
//	disconnect(m, SIGNAL(attributeModified(unsigned int, QString)), this, SLOT(attributeModified(unsigned int, QString)));
//	disconnect(m, SIGNAL(connectivityModified()), this, SLOT(connectivityModified()));
153

154 155 156 157
	ParameterSet* params = h_viewParams[v];
	PerMapParameterSet* perMap = params->perMap[m->getName()];

	delete perMap;
158
	params->perMap.remove(m->getName());
159 160 161

	if(params->selectedMap == m)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
162
		if(!params->perMap.empty())
163
			changeSelectedMap(v, m_window->getMap(params->perMap.begin().key()));
Pierre Kraemer's avatar
Pierre Kraemer committed
164
		else
165
			changeSelectedMap(v, NULL);
166 167
	}
	else
168
		m_dockTab->refreshUI(params);
169 170
}

171
void RenderVectorPlugin::changeSelectedMap(View* view, MapHandlerGen* map)
172 173 174 175 176 177 178 179 180
{
	ParameterSet* params = h_viewParams[view];

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

	if(view->isCurrentView())
	{
		if(prev)
181
			disconnect(prev, SIGNAL(vboAdded(Utils::VBO*)), m_dockTab, SLOT(addVBOToList(Utils::VBO*)));
182
		if(map)
183
			connect(map, SIGNAL(vboAdded(Utils::VBO*)), m_dockTab, SLOT(addVBOToList(Utils::VBO*)));
184

185
		m_dockTab->refreshUI(params);
186 187 188
	}
}

189
void RenderVectorPlugin::changePositionVBO(View* view, MapHandlerGen* map, Utils::VBO* vbo, bool fromUI)
190 191
{
	ParameterSet* params = h_viewParams[view];
192
	params->perMap[map->getName()]->positionVBO = vbo;
193 194 195

	if(view->isCurrentView())
	{
196 197
		if(!fromUI)
			m_dockTab->refreshUI(params);
198 199 200 201
		view->updateGL();
	}
}

202
void RenderVectorPlugin::changeSelectedVectorsVBO(View* view, MapHandlerGen* map, const std::vector<Utils::VBO*>& vbos, bool fromUI)
203 204
{
	ParameterSet* params = h_viewParams[view];
205
	params->perMap[map->getName()]->vectorVBO = vbos;
206 207 208

	if(view->isCurrentView())
	{
209 210
		if(!fromUI)
			m_dockTab->refreshUI(params);
211 212 213 214
		view->updateGL();
	}
}

215
void RenderVectorPlugin::changeVectorsScaleFactor(View* view, MapHandlerGen* map, int i, bool fromUI)
216 217
{
	ParameterSet* params = h_viewParams[view];
218
	params->perMap[map->getName()]->vectorsScaleFactor = i / 50.0;
219 220 221

	if(view->isCurrentView())
	{
222 223
		if(!fromUI)
			m_dockTab->refreshUI(params);
224
		view->updateGL();
225
	}
226 227
}

228 229 230 231 232
#ifndef DEBUG
Q_EXPORT_PLUGIN2(RenderVectorPlugin, RenderVectorPlugin)
#else
Q_EXPORT_PLUGIN2(RenderVectorPluginD, RenderVectorPlugin)
#endif
233 234 235 236

} // namespace SCHNApps

} // namespace CGoGN