qtSimple.h 8.08 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 27 28 29 30 31 32 33
/*******************************************************************************
 * 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                                        *
 *                                                                              *
 *******************************************************************************/

#ifndef __SIMPLE_QT_GL2__
#define __SIMPLE_QT_GL2__

#include <QApplication>
#include <QDesktopWidget>
#include <QMainWindow>
#include <QWidget>
#include <QtGui>
#include "Utils/qtgl.h"
34
#include <set>
Sylvain Thery's avatar
Sylvain Thery committed
35
#include <string>
36
#include "Geometry/vector_gen.h"
Sylvain Thery's avatar
Sylvain Thery committed
37

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

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

Sylvain Thery's avatar
Sylvain Thery committed
43 44
namespace Utils
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
45

Sylvain Thery's avatar
Sylvain Thery committed
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
namespace QT
{

// forward definition
class GLWidget;

class SimpleQT : public QMainWindow
{
	Q_OBJECT

public:
	SimpleQT();

	virtual ~SimpleQT();

	/**
	 * 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))
	 */
	void setCallBack( const QObject* sender, const char* signal, const char* method);

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

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

90 91
	/**
	 * draw a message in status bar
Sylvain Thery's avatar
Sylvain Thery committed
92 93
	 * @param msg message in C format (if NULL swp show/hidden)
	 * @param timeoutms number of ms message stay (0 = until next msg)
94
	 */
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
95 96
	void statusMsg(const char* msg, int timeoutms = 0);

Sylvain Thery's avatar
Sylvain Thery committed
97 98 99 100 101 102 103 104 105 106
	/**
	 * add an empty dock to the window
	 */
	QDockWidget* addEmptyDock();

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

Sylvain Thery's avatar
Sylvain Thery committed
107 108 109 110 111 112 113 114 115
	/**
	 * change console visibility
	 */
	void visibilityConsole(bool visible);

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

Sylvain Thery's avatar
Sylvain Thery committed
117 118 119 120 121
	/**
	 * change console visibility
	 */
	void toggleVisibilityConsole();

Sylvain Thery's avatar
Sylvain Thery committed
122 123 124
	/**
	 * add an entry to popup menu
	 */
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
125
	void add_menu_entry(const std::string label, const char* method);
Sylvain Thery's avatar
Sylvain Thery committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

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

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

//	void contextMenuEvent(QContextMenuEvent *event);

protected:
	GLWidget *m_glWidget;
141

Sylvain Thery's avatar
Sylvain Thery committed
142
	QDockWidget *m_dock;
143

Sylvain Thery's avatar
Sylvain Thery committed
144 145 146 147
	QDockWidget *m_dockConsole;

	QTextEdit* m_textConsole;

Sylvain Thery's avatar
Sylvain Thery committed
148
	bool m_dockOn;
149 150

	// mouse & matrix
Sylvain Thery's avatar
Sylvain Thery committed
151 152
	glm::mat4 m_projection_matrix;
	glm::mat4 m_modelView_matrix;
153 154 155 156 157
	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
158 159

	QMenu* m_fileMenu;
160

Sylvain Thery's avatar
Sylvain Thery committed
161
	QMenu* m_appMenu;
162

Sylvain Thery's avatar
Sylvain Thery committed
163 164
	std::string m_helpString;

165 166 167 168
	void keyPressEvent(QKeyEvent *event);

	void keyReleaseEvent(QKeyEvent *e);

Sylvain Thery's avatar
Sylvain Thery committed
169 170 171 172
public:
	/**
	 * set width and pos center of object to draw
	 */
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
173
	void setParamObject(float width, float* pos) { m_glWidget->setParamObject(width, pos); }
Sylvain Thery's avatar
Sylvain Thery committed
174

175 176 177 178 179
	/**
	 * set focal
	 */
	void setFocal(float f) { m_glWidget->setFocal(f); }

180 181 182 183 184 185 186 187 188 189 190 191 192 193
	/**
	 * 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
	 * @param rayA first computed point
	 * @param rayA second computed point
	 * @param radius radius on pixel for clicking precision
	 * @return the distance in modelview world corresponding to radius pixel in screen
	 */
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
194
	GLfloat getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius = 4);
195

Sylvain Thery's avatar
Sylvain Thery committed
196 197 198
	/**
	 * current modelview matrix
	 */
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
199
	glm::mat4& modelViewMatrix () { return m_modelView_matrix; }
Sylvain Thery's avatar
Sylvain Thery committed
200 201 202 203

	/**
	 * current projection matrix
	 */
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
204
	glm::mat4& projectionMatrix () { return m_projection_matrix; }
Sylvain Thery's avatar
Sylvain Thery committed
205

206

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
207 208
	float* curquat() { return m_curquat; }
	float* lastquat() { return m_lastquat; }
209

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
210 211 212
	float& trans_x() { return m_trans_x; }
	float& trans_y() { return m_trans_y; }
	float& trans_z() { return m_trans_z; }
213

Sylvain Thery's avatar
Sylvain Thery committed
214 215 216 217
	/**
	 * shift key pressed ?
	 */
	bool Shift() const { return m_glWidget->Shift(); }
218

Sylvain Thery's avatar
Sylvain Thery committed
219 220 221 222 223 224 225 226 227 228 229 230 231
	/**
	 * control key pressed ?
	 */
	bool Control() const { return m_glWidget->Control(); }

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

	/**
	 * height of OpenGL widget (for classic yy = H-y
	 */
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
232
	int getHeight() const { return m_glWidget->getHeight(); }
Sylvain Thery's avatar
Sylvain Thery committed
233 234 235 236

	/**
	 * console QTextEdit ptr
	 */
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
237
	QTextEdit* console() { return m_textConsole; }
Sylvain Thery's avatar
Sylvain Thery committed
238 239 240 241

	/**
	 * syncronization between SimpleQTs (experimental)
	 */
242 243 244 245 246 247
	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
248
	void registerShader(GLSLShader* ptr);
249 250 251 252

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

Sylvain Thery's avatar
Sylvain Thery committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	/**
	 * 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
	 */
	virtual void cb_mousePress(int button, int x, int y) {}

	/**
	 * Mouse button has been released
	 */
	virtual void cb_mouseRelease(int button, int x, int y) {}

	/**
	 * the mouse has been move (with button still pressed)
	 */
	virtual void cb_mouseMove(int x, int y) {}

	/**
	 * key press CB (context is ok)
	 */
	virtual void cb_keyPress(int code) {}

	/**
	 * key releaase CB (context is ok)
	 */
	virtual void cb_keyRelease(int code) {}

	/**
	 * matrices need to be updated (context is ok)
	 */
293
	virtual void cb_updateMatrix();
Sylvain Thery's avatar
Sylvain Thery committed
294 295 296 297 298 299 300 301 302 303 304 305

	/**
	 * end of program, some things to clean ?
	 */
	virtual void cb_exit() {}

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

306 307 308 309 310
	/**
	 * Ask to Qt to update matrices and then the GL widget.
	 */
	void updateGLMatrices();

311 312 313 314 315 316
	/**
	 * 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);; ...")
	 */
317
	std::string selectFile(const std::string& title = "open file", const std::string& dir = ".", const std::string& filters = "all (*.*)");
318

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
319 320 321 322 323
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
324 325
	void cb_about_cgogn();
	void cb_about();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
326 327
	void cb_consoleOnOff() { toggleVisibilityConsole(); }
	void cb_consoleClear() { m_textConsole->clear(); }
Sylvain Thery's avatar
Sylvain Thery committed
328 329
};

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
330
} // namespace QT
Sylvain Thery's avatar
Sylvain Thery committed
331

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
332
} // namespace Utils
Sylvain Thery's avatar
Sylvain Thery committed
333

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
334
} // namespace CGoGN
Sylvain Thery's avatar
Sylvain Thery committed
335 336

#endif