qtSimple.h 11.4 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1
2
3
/*******************************************************************************
 * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
 * version 0.1                                                                  *
4
 * Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Sylvain Thery's avatar
Sylvain Thery committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *                                                                              *
 * 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.           *
 *                                                                              *
20
 * Web site: http://cgogn.unistra.fr/                                           *
Sylvain Thery's avatar
Sylvain Thery committed
21
22
23
24
25
26
27
28
29
30
31
32
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#ifndef __SIMPLE_QT_GL2__
#define __SIMPLE_QT_GL2__

#include <QApplication>
#include <QDesktopWidget>
#include <QMainWindow>
#include <QWidget>
#include <QtGui>
33
#include "Utils/Qt/qtgl.h"
34

35
#include <set>
Sylvain Thery's avatar
Sylvain Thery committed
36
#include <string>
37
#include "Geometry/vector_gen.h"
38
#include "Utils/gl_matrices.h"
Sylvain Thery's avatar
Sylvain Thery committed
39

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
40
namespace CGoGN { namespace Utils { class GLSLShader; } }
Sylvain Thery's avatar
Sylvain Thery committed
41
42
43

namespace CGoGN
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
44

Sylvain Thery's avatar
Sylvain Thery committed
45
46
namespace Utils
{
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
47

Sylvain Thery's avatar
Sylvain Thery committed
48
49
50
51
52
53
54
55
56
57
58
59
namespace QT
{

// forward definition
class GLWidget;

class SimpleQT : public QMainWindow
{
	Q_OBJECT

public:
	SimpleQT();
60

61
	SimpleQT(const SimpleQT&) ;
Sylvain Thery's avatar
Sylvain Thery committed
62
63
64

	virtual ~SimpleQT();

65
66
	void operator=(const SimpleQT& v) ;

Sylvain Thery's avatar
Sylvain Thery committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
	/**
	 * set the main widget of the dock
	 */
//	void setDockWidget(QWidget *wdg);
	void setDock(QDockWidget *dock);

	/**
	 * set the main widget of the dock
	 */
	QDockWidget* dockWidget();

	/**
	 * connect a widget/signal with a method of inherited object
	 * @param sender the widget sending signal
	 * @param signal use macro SIGNAL(qt_signal)
	 * @param method use macro SLOT(name_of_method(params))
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
84
	void setCallBack(const QObject* sender, const char* signal, const char* method);
Sylvain Thery's avatar
Sylvain Thery committed
85
86
87
88
89
90
91
92
93
94
95

	/**
	 * set window Title
	 */
	void windowTitle(const char* windowTitle);

	/**
	 * set dock Title
	 */
	void dockTitle(const char* dockTitle);

96
97
	/**
	 * draw a message in status bar
Sylvain Thery's avatar
Sylvain Thery committed
98
99
	 * @param msg message in C format (if NULL swp show/hidden)
	 * @param timeoutms number of ms message stay (0 = until next msg)
100
	 */
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
101
102
	void statusMsg(const char* msg, int timeoutms = 0);

Sylvain Thery's avatar
Sylvain Thery committed
103
104
105
106
107
108
109
110
111
112
	/**
	 * add an empty dock to the window
	 */
	QDockWidget* addEmptyDock();

	/**
	 * change dock visibility
	 */
	void visibilityDock(bool visible);

Sylvain Thery's avatar
Sylvain Thery committed
113
114
115
116
117
118
119
120
121
	/**
	 * change console visibility
	 */
	void visibilityConsole(bool visible);

	/**
	 * change dock visibility
	 */
	void toggleVisibilityDock();
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
122

Sylvain Thery's avatar
Sylvain Thery committed
123
124
125
126
127
	/**
	 * change console visibility
	 */
	void toggleVisibilityConsole();

Sylvain Thery's avatar
Sylvain Thery committed
128
129
	/**
	 * add an entry to popup menu
130
131
	 * @param label label printed in menu
	 * @param method qt slot (function name)
Sylvain Thery's avatar
Sylvain Thery committed
132
	 */
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
133
	void add_menu_entry(const std::string label, const char* method);
Sylvain Thery's avatar
Sylvain Thery committed
134
135
136
137
138
139
140
141
142
143
144

	/**
	 * re-initialize popup menu
	 */
	void init_app_menu();

	/**
	 * set the help message
	 */
	void setHelpMsg(const std::string& msg);

145
146
147
148
149
150
	/**
	 * set mouse tracking on the GLWidget
	 * if true : mouseMove events are generated for each mouse move
	 * if false : mouseMove events are only generated when a button is pressed
	 */
	void setGLWidgetMouseTracking(bool b);
151
	
152

Sylvain Thery's avatar
Sylvain Thery committed
153
protected:
Pierre Kraemer's avatar
Pierre Kraemer committed
154
	GLWidget* m_glWidget;
155

Pierre Kraemer's avatar
Pierre Kraemer committed
156
	QDockWidget* m_dock;
157

Pierre Kraemer's avatar
Pierre Kraemer committed
158
	QDockWidget* m_dockConsole;
Sylvain Thery's avatar
Sylvain Thery committed
159
160
161

	QTextEdit* m_textConsole;

Sylvain Thery's avatar
Sylvain Thery committed
162
	bool m_dockOn;
163
164

	// mouse & matrix
165
166
167
168
169
170
171
172
173
	Utils::GL_Matrices m_mat;

	/// ref to projection matrix
	glm::mat4& m_projection_matrix;
	/// ref to modelview matrix
	glm::mat4& m_modelView_matrix;
	/// ref to transformation matrix
	glm::mat4& m_transfo_matrix;

174
175
176
177
178
	float m_curquat[4];
	float m_lastquat[4];
	float m_trans_x;
	float m_trans_y;
	float m_trans_z;
Sylvain Thery's avatar
Sylvain Thery committed
179
180

	QMenu* m_fileMenu;
181

Sylvain Thery's avatar
Sylvain Thery committed
182
	QMenu* m_appMenu;
183

Sylvain Thery's avatar
Sylvain Thery committed
184
185
	std::string m_helpString;

186
187
	std::stack<glm::mat4> m_stack_trf;

188
	void closeEvent(QCloseEvent *event);
189

190
191
192
	void keyPressEvent(QKeyEvent *event);

	void keyReleaseEvent(QKeyEvent *e);
193
	
Sylvain Thery's avatar
Sylvain Thery committed
194
195
196
197
public:
	/**
	 * set width and pos center of object to draw
	 */
Sylvain Thery's avatar
Sylvain Thery committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
	template<typename T>
	inline void setParamObject(T width, T* pos)
	{
		float posF[3];
		posF[0]=float(pos[0]);
		posF[1]=float(pos[1]);
		posF[2]=float(pos[2]);
		m_glWidget->setParamObject(float(width), posF);
	}

	template<typename T>
	inline void resetCenterOfRotation(T width, T* pos)
	{
		float posF[3];
		posF[0]=float(pos[0]);
		posF[1]=float(pos[1]);
		posF[2]=float(pos[2]);
		m_glWidget->resetCenterOfRotation(float(width), posF);
	}
Sylvain Thery's avatar
Sylvain Thery committed
217

218

219
220
221
222
223
	/**
	 * make the contex of glWidget current
	 */
	void makeCurrent() { m_glWidget->makeCurrent();}

224
225
226
227
228
	/**
	 * set focal
	 */
	void setFocal(float f) { m_glWidget->setFocal(f); }

229
230
231
232
233
234

	/**
	 * set geometry (override buggy Qt function)
	 */
	void setGeometry(int x, int y, int w, int h);

235
236
237
238
239
240
241
242
243
	/**
	 * get the mouse position in GL widget
	 */
	void glMousePosition(int& x, int& y);

	/**
	 * get a ray (2 points) from a pick point in GL area
	 * @param x mouse position
	 * @param y mouse position
244
	 * @param rayA first computed point of ray
245
	 * @param rayB second computed point of ray
246
247
248
	 * @param radius radius on pixel for clicking precision
	 * @return the distance in modelview world corresponding to radius pixel in screen
	 */
249
	GLfloat getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius=4) { return m_glWidget->getOrthoScreenRay(x,y,rayA,rayB,radius);}
250

251
252
253
254
255
	/**
	 * transform a pixel distance on screen in distance in world
	 * @param pixel_width width on pixel on screen
	 * @param center reference point on world to use (defaut 0,0,0)
	 */
256
	float getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center) { return m_glWidget->getWidthInWorld(pixel_width,center);}
257
258


259
260
261
	const glm::mat4& transfoMatrix() const { return m_transfo_matrix; }
	glm::mat4& transfoMatrix() { return m_transfo_matrix; }

Sylvain Thery's avatar
Sylvain Thery committed
262
263
264
	/**
	 * current modelview matrix
	 */
265
266
	const glm::mat4& modelViewMatrix() const { return m_modelView_matrix; }
	glm::mat4& modelViewMatrix() { return m_modelView_matrix; }
Sylvain Thery's avatar
Sylvain Thery committed
267
268
269
270

	/**
	 * current projection matrix
	 */
271
272
	const glm::mat4& projectionMatrix() const { return m_projection_matrix; }
	glm::mat4& projectionMatrix() { return m_projection_matrix; }
Sylvain Thery's avatar
Sylvain Thery committed
273

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
274
275
	float* curquat() { return m_curquat; }
	float* lastquat() { return m_lastquat; }
276

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
277
278
279
	float& trans_x() { return m_trans_x; }
	float& trans_y() { return m_trans_y; }
	float& trans_z() { return m_trans_z; }
280

281
282
//	glm::mat4* matricesPtr() { return  m_mat.m_matrices;}
	Utils::GL_Matrices* matricesPtr() { return &m_mat;}
Sylvain Thery's avatar
Sylvain Thery committed
283
284
285
286
	/**
	 * shift key pressed ?
	 */
	bool Shift() const { return m_glWidget->Shift(); }
287

Sylvain Thery's avatar
Sylvain Thery committed
288
289
290
291
292
293
294
295
296
297
298
	/**
	 * control key pressed ?
	 */
	bool Control() const { return m_glWidget->Control(); }

	/**
	 * alt key pressed ?
	 */
	bool Alt() const  { return m_glWidget->Alt(); }

	/**
Thery Sylvain's avatar
Thery Sylvain committed
299
	 * height of OpenGL widget (for classic yy = H-y)
Sylvain Thery's avatar
Sylvain Thery committed
300
	 */
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
301
	int getHeight() const { return m_glWidget->getHeight(); }
Sylvain Thery's avatar
Sylvain Thery committed
302

Thery Sylvain's avatar
Thery Sylvain committed
303
304
305
306
307
308
	/**
	 * width of OpenGL widget
	 */
	int getWidth() const { return m_glWidget->getWidth(); }


Sylvain Thery's avatar
Sylvain Thery committed
309
310
311
	/**
	 * console QTextEdit ptr
	 */
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
312
	QTextEdit* console() { return m_textConsole; }
Sylvain Thery's avatar
Sylvain Thery committed
313
314
315
316

	/**
	 * syncronization between SimpleQTs (experimental)
	 */
317
318
319
320
321
322
	void synchronize(SimpleQT* sqt);

	/**
	 * Register a shader as running in this Widget.
	 * Needed for automatic matrices update
	 */
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
323
	void registerShader(GLSLShader* ptr);
324
325
326
327

	/**
	 * Unregister a shader as running in this Widget.
	 */
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
328
	void unregisterShader(GLSLShader* ptr);
329

Sylvain Thery's avatar
Sylvain Thery committed
330
331
332
333
334
335
336
337
338
339
340
341
342
	/**
	 * GL initialization CB (context is ok)
	 */
	virtual void cb_initGL() {}

	/**
	 * what to you want to draw ? (need to be implemented)
	 */
	virtual void cb_redraw() = 0;

	/**
	 * Mouse button has been pressed
	 */
343
	virtual void cb_mousePress(int /*button*/, int /*x*/, int /*y*/) {}
Sylvain Thery's avatar
Sylvain Thery committed
344
345
346
347

	/**
	 * Mouse button has been released
	 */
348
	virtual void cb_mouseRelease(int /*button*/, int /*x*/, int /*y*/) {}
Sylvain Thery's avatar
Sylvain Thery committed
349

350
351
352
	/**
	 * Mouse button has been clicked
	 */
353
	virtual void cb_mouseClick(int /*button*/, int /*x*/, int /*y*/) {}
354

Sylvain Thery's avatar
Sylvain Thery committed
355
	/**
356
	 * the mouse has been move (with buttons still pressed)
Sylvain Thery's avatar
Sylvain Thery committed
357
	 */
358
	virtual void cb_mouseMove(int /*buttons*/, int /*x*/, int /*y*/) {}
Sylvain Thery's avatar
Sylvain Thery committed
359

Pierre Kraemer's avatar
Pierre Kraemer committed
360
361
362
	/**
	 * the mouse has been move (with button still pressed)
	 */
363
	virtual void cb_wheelEvent(int /*delta*/, int /*x*/, int /*y*/) {}
Pierre Kraemer's avatar
Pierre Kraemer committed
364

Sylvain Thery's avatar
Sylvain Thery committed
365
366
367
	/**
	 * key press CB (context is ok)
	 */
368
	virtual void cb_keyPress(int /*code*/) {}
Sylvain Thery's avatar
Sylvain Thery committed
369
370
371
372

	/**
	 * key releaase CB (context is ok)
	 */
373
	virtual void cb_keyRelease(int /*code*/) {}
Sylvain Thery's avatar
Sylvain Thery committed
374
375
376
377

	/**
	 * matrices need to be updated (context is ok)
	 */
378
	virtual void cb_updateMatrix();
Sylvain Thery's avatar
Sylvain Thery committed
379
380
381
382

	/**
	 * end of program, some things to clean ?
	 */
383
	virtual void cb_exit() {}
Sylvain Thery's avatar
Sylvain Thery committed
384
385
386
387
388
389
390

	/**
	 * Ask to Qt to update the GL widget.
	 * Equivalent of glutPostRedisplay()
	 */
	void updateGL();

391
392
393
394
395
	/**
	 * Ask to Qt to update matrices and then the GL widget.
	 */
	void updateGLMatrices();

396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
	/**
	 * apply rotation to transformation matrix
	 */
	void transfoRotate(float angle, float x, float y, float z);

	/**
	 * apply translation to transformation matrix
	 */
	void transfoTranslate(float tx, float ty, float tz);

	/**
	 * apply scale to transformation matrix
	 */
	void transfoScale(float sx, float sy, float sz);

	/**
	 * push the transfo matrix on stack
	 */
	void pushTransfoMatrix();

	/**
	 * pop the transfo matrix from stack
	 */
	bool popTransfoMatrix();

421
422
423
424
425
426
	/**
	 * Open a file selector and return the filename
	 * @param title title of window
	 * @param dir base directory
	 * @param filters file filters (syntax: "label1 (filter1);; label2 (filter2);; ...")
	 */
427
	std::string selectFile(const std::string& title = "open file", const std::string& dir = ".", const std::string& filters = "all (*.*)");
428

429
430
431
432
433
434
	/**
	 * Open a file selector and return the filename (for saving a file)
	 * @param title title of window
	 * @param dir base directory
	 * @param filters file filters (syntax: "label1 (filter1);; label2 (filter2);; ...")
	 */
435
	std::string selectFileSave(const std::string& title = "save file", const std::string& dir =  ".", const std::string& filters = "all (*.*)");
436

437
	void snapshot(const QString& filename, const char* format = 0, const int& quality = -1);
Sylvain Thery's avatar
Sylvain Thery committed
438

439
440
441
442
443

    void exportPOV2file(const QString& filename);

    void importFile2POV(const QString& filename);

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
444
445
446
447
448
public slots:
	virtual void cb_New() { std::cerr << "callback not implemented" << std::endl; }
	virtual void cb_Open() { std::cerr << "callback not implemented" << std::endl; }
	virtual void cb_Save() { std::cerr << "callback not implemented" << std::endl; }
	virtual void cb_Quit() { exit(0); }
Sylvain Thery's avatar
Sylvain Thery committed
449
450
	void cb_about_cgogn();
	void cb_about();
Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
451
452
	void cb_consoleOnOff() { toggleVisibilityConsole(); }
	void cb_consoleClear() { m_textConsole->clear(); }
Sylvain Thery's avatar
Sylvain Thery committed
453
454
};

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
455
} // namespace QT
Sylvain Thery's avatar
Sylvain Thery committed
456

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
457
} // namespace Utils
Sylvain Thery's avatar
Sylvain Thery committed
458

Pierre Kraemer's avatar
merge..    
Pierre Kraemer committed
459
} // namespace CGoGN
Sylvain Thery's avatar
Sylvain Thery committed
460
461

#endif