qtSimple.cpp 7.87 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Utils/qtSimple.h"
#include "Utils/qtgl.h"
27
28
29
#include "Utils/GLSLShader.h"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_precision.hpp"
Sylvain Thery's avatar
Sylvain Thery committed
30
31
32
33
34
35
36
37
38
39
40
41

namespace CGoGN
{
namespace Utils
{
namespace QT
{


SimpleQT::SimpleQT():
m_dock(NULL)
{
42

Sylvain Thery's avatar
Sylvain Thery committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
	m_glWidget = new GLWidget(this);
	setCentralWidget(m_glWidget);
	setWindowTitle(tr("CGoGN"));

	m_fileMenu =menuBar()->addMenu(tr("&File"));

	QAction* action= new QAction(tr("New"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_New()));
	m_fileMenu->addAction(action);

	action= new QAction(tr("Open"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_Open()));
	m_fileMenu->addAction(action);

	action= new QAction(tr("Save"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_Save()));
	m_fileMenu->addAction(action);

	m_fileMenu->addSeparator();

	action= new QAction(tr("Quit"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_Quit()));
	m_fileMenu->addAction(action);

	m_appMenu = menuBar()->addMenu(tr("&Application"));


	QMenu* m_helpMenu = menuBar()->addMenu(tr("&Help"));

	action= new QAction(tr("About"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_about()));
	m_helpMenu->addAction(action);

	action= new QAction(tr("About CGoGN"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_about_cgogn()));
	m_helpMenu->addAction(action);

}

SimpleQT::~SimpleQT()
{
	delete m_glWidget; // ??
}



void SimpleQT::cb_about_cgogn()
{
	QString str("CGoGN:\nCombinatorial and Geometric modeling\n"
			"with Generic N-dimensional Maps\n"
			"Web site: https://cgogn.u-strasbg.fr \n"
			"Contact information: cgogn@unistra.fr");
	QMessageBox::about(this, tr("About CGoGN"),str);
}

void SimpleQT::cb_about()
{
   QMessageBox::about(this, tr("About App"), m_helpString.c_str());
}

void SimpleQT::setHelpMsg(const std::string& msg)
{
	m_helpString = msg;
}



110
111
112
113
114
115
116
void SimpleQT::glMousePosition(int& x, int& y)
{
	QPoint xy = m_glWidget->mapFromGlobal(QCursor::pos());
	x = xy.x();
	y = m_glWidget->getHeight()- xy.y();
}

Sylvain Thery's avatar
Sylvain Thery committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
QDockWidget* SimpleQT::addEmptyDock()
{
		m_dock = new QDockWidget(tr("Control"), this);
		m_dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea|Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
		m_dock->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable);
		addDockWidget(Qt::RightDockWidgetArea, m_dock);

		m_dock->hide();
		m_dockOn=false;

		return m_dock;
}

void SimpleQT::visibilityDock(bool visible)
{
	if (visible)
	{
		m_dock->show();
		m_dockOn=true;
	}
	else
	{
		m_dock->hide();
		m_dockOn=false;
	}
}


void SimpleQT::windowTitle(const char* windowTitle)
{
	setWindowTitle(tr(windowTitle));
}

void SimpleQT::dockTitle(const char* dockTitle)
{
	if (m_dock)
		m_dock->setWindowTitle(tr(dockTitle));
}

156
157
158
159
160
161
162
163
164
165
void SimpleQT::statusMsg(const char* msg)
{
	if (msg)
	{
		QString message = tr(msg);
		statusBar()->showMessage(message);
	}
	else
		statusBar()->hide();
}
Sylvain Thery's avatar
Sylvain Thery committed
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

void SimpleQT::setCallBack( const QObject* sender, const char* signal, const char* method)
{
	connect(sender, signal, this, method);
}


void SimpleQT::keyPressEvent(QKeyEvent *e)
{
	if ((e->key() == Qt::Key_Return) && m_dock != NULL) // m for menu
	{

		if (m_dockOn)
		{
			m_dock->hide();
			m_dockOn=false;
		}
		else
		{
			m_dock->show();
			m_dockOn=true;
		}
	}

    if (e->key() == Qt::Key_Escape)
    {
    	close();
    }
    else
        QWidget::keyPressEvent(e);

    m_glWidget->keyPressEvent(e);
}


void SimpleQT::keyReleaseEvent(QKeyEvent *e)
{
    m_glWidget->keyReleaseEvent(e);
}



void SimpleQT::setDock(QDockWidget *dock)
{
	m_dock = dock;
	addDockWidget(Qt::RightDockWidgetArea, m_dock);
	m_dockOn=true;
	m_dock->show();
}


QDockWidget* SimpleQT::dockWidget()
{
	return m_dock;
}


void SimpleQT::updateGL()
{
	m_glWidget->updateGL();
}

228
229
230
231
232
233
234
235
236
237
238
void SimpleQT::updateGLMatrices()
{
	cb_updateMatrix();
	m_glWidget->updateGL();
}

void SimpleQT::cb_updateMatrix()
{
	for (std::set< std::pair<void*, GLSLShader*> >::iterator it=GLSLShader::m_registredRunning.begin(); it!=GLSLShader::m_registredRunning.end(); ++it)
	{
		if ((it->first == NULL) || (it->first == this))
239
		{
240
			it->second->updateMatrices(m_projection_matrix, m_modelView_matrix);
241
		}
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
	}

}

void SimpleQT::synchronize(SimpleQT* sqt)
{
	m_projection_matrix = sqt->m_projection_matrix;
	m_modelView_matrix = sqt->m_modelView_matrix;
	for (unsigned int i=0; i< 4; ++i)
	{
		m_curquat[i] = sqt->m_curquat[i];
		m_lastquat[i] = sqt->m_lastquat[i];
	}
	m_trans_x = sqt->trans_x();
	m_trans_y = sqt->trans_y();
	m_trans_z = sqt->trans_z();

	SimpleQT::cb_updateMatrix();

	m_glWidget->modelModified();
	m_glWidget->updateGL();
}


Sylvain Thery's avatar
Sylvain Thery committed
266
267
268
269
270
271
272
273
274
275

void SimpleQT::add_menu_entry(const std::string label, const char* method )
{
	QAction * action = new QAction(tr(label.c_str()), this);
	connect(action, SIGNAL(triggered()), this, method);
	m_appMenu->addAction(action);
}


/**
276
 * initialize app menu
Sylvain Thery's avatar
Sylvain Thery committed
277
278
279
280
281
282
 */
void SimpleQT::init_app_menu()
{
	m_appMenu->clear();
}

283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
void SimpleQT::registerRunning(GLSLShader* ptr)
{
	GLSLShader::m_registredRunning.insert(std::pair<void*,GLSLShader*>(this,ptr));
}

void SimpleQT::unregisterRunning(GLSLShader* ptr)
{
	GLSLShader::m_registredRunning.erase(std::pair<void*,GLSLShader*>(this,ptr));
}


GLfloat SimpleQT::getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius)
{
	// get Z from depth buffer
//	int yy =  m_glWidget->getHeight() - y;
	int yy = y;
	GLfloat depth;
	glReadPixels(x, yy,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&depth);


	glm::i32vec4 viewport;
	glGetIntegerv(GL_VIEWPORT,&(viewport[0]));

	glm::vec3 win(x, yy, 0.0f);

	glm::vec3 P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);

	rayA[0] = P[0];
	rayA[1] = P[1];
	rayA[2] = P[2];

	win[2]=depth;

	P =  glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
	rayB[0] = P[0];
	rayB[1] = P[1];
	rayB[2] = P[2];

	if (depth == 1.0f)	// depth vary in [0-1]
		depth = 0.5f;

	win[0] += radius;

	P =  glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
	Geom::Vec3f Q;
	Q[0] = P[0];
	Q[1] = P[1];
	Q[2] = P[2];

	// compute & return distance
	Q -= rayB;
	return float(Q.norm());
}

Sylvain Thery's avatar
Sylvain Thery committed
337
338
339
340
341


}
}
}