File: qprojectmwidget.hpp

package info (click to toggle)
projectm 2.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 32,256 kB
  • ctags: 13,810
  • sloc: cpp: 31,087; ansic: 26,914; sh: 816; makefile: 20
file content (318 lines) | stat: -rw-r--r-- 7,335 bytes parent folder | download | duplicates (3)
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
34
35
36
37
38
39
40
41
42
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
110
111
112
113
114
115
116
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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
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
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
/**
 * projectM-qt -- Qt4 based projectM GUI 
 * Copyright (C)2003-2004 projectM Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU 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 General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * See 'LICENSE.txt' included within this release
 *
 */

#ifndef QPROJECTM_WIDGET_HPP
#define QPROJECTM_WIDGET_HPP

#include <iostream>
#include "qprojectm.hpp"
#include <QGLWidget>
#include <QMutex>
#include <QtDebug>
#include <QKeyEvent>
#include <QTimer>
#include <QApplication>
#include <QSettings>

class QProjectMWidget : public QGLWidget
{

		Q_OBJECT        // must include this if you use Qt signals/slots

	public:
		static const int MOUSE_VISIBLE_TIMEOUT_MS = 5000;
		QProjectMWidget ( const std::string & config_file, QWidget * parent, QMutex * audioMutex = 0 )
				: QGLWidget ( parent ), m_config_file ( config_file ), m_projectM ( 0 ), m_audioMutex ( audioMutex ), m_mouseTimer ( 0 )
		{

			m_mouseTimer = new QTimer ( this );

			QSettings settings("projectM", "qprojectM");
			mouseHideTimeoutSeconds = 
				settings.value("MouseHideOnTimeout", MOUSE_VISIBLE_TIMEOUT_MS/1000).toInt();
			
			if (mouseHideTimeoutSeconds > 0)
				m_mouseTimer->start ( mouseHideTimeoutSeconds * 1000);

			connect ( m_mouseTimer, SIGNAL ( timeout() ), this, SLOT ( hideMouse() ) );
			this->setMouseTracking ( true );

		}

		~QProjectMWidget() { destroyProjectM(); }



		void resizeGL ( int w, int h )
		{
			// Setup viewport, projection etc
			setup_opengl ( w,h );
			m_projectM->projectM_resetGL ( w, h );
		}

		inline const std::string & configFile()
		{
			return m_config_file;
		}

		inline void seizePresetLock()
		{

			m_presetSeizeMutex.lock();
			m_presetWasLocked = qprojectM()->isPresetLocked();
			qprojectM()->setPresetLock ( true );

		}

		inline void releasePresetLock()
		{
			qprojectM()->setPresetLock ( m_presetWasLocked );
			m_presetSeizeMutex.unlock();
		}


		inline QProjectM * qprojectM() { return m_projectM; }

	protected slots:
		inline void mouseMoveEvent ( QMouseEvent * event )
		{

			m_mouseTimer->stop();
			QApplication::restoreOverrideCursor();
			if (mouseHideTimeoutSeconds > 0)
				m_mouseTimer->start ( mouseHideTimeoutSeconds*1000 );

		}

		inline void leaveEvent ( QEvent * event )
		{
			/// @bug testing if this resolves a bug for ubuntu users
			QApplication::restoreOverrideCursor();
		}

	public slots:

		void resetProjectM()
		{
			std::cout << "reseting" << std::endl;
			qDebug() << "reset start";

			emit ( projectM_BeforeDestroy() );

			if ( m_audioMutex )
				m_audioMutex->lock();

			destroyProjectM();

			// Make a new projectM instance and reset the opengl state
			initializeGL();

			// Allow audio thread to continue its business
			if ( m_audioMutex )
			{
				m_audioMutex->unlock();
			}
			qDebug() << "reinit'ed";
		}

		void setAudioMutex ( QMutex * mutex )
		{
			m_audioMutex = mutex;
		}

		void setPresetLock ( int state )
		{
			m_projectM->setPresetLock ( ( bool ) state );
			emit ( presetLockChanged ( ( bool ) state ) );
		}

		void setShuffleEnabled ( int state )
		{
			m_projectM->setShuffleEnabled ( ( bool ) state );
			emit ( shuffleEnabledChanged ( ( bool ) state ) );
		}

		void mousePressEvent ( QMouseEvent * event )
		{
			this->setFocus();
		}

	signals:
		void projectM_Initialized ( QProjectM * );
		void projectM_BeforeDestroy();
		void presetLockChanged ( bool isLocked );
		void shuffleEnabledChanged ( bool isShuffleEnabled );

	private slots:
		void hideMouse()
		{
			if ( this->underMouse() && this->hasFocus() )
				QApplication::setOverrideCursor ( Qt::BlankCursor );
		}
	private:
		std::string m_config_file;
		QProjectM * m_projectM;
		void destroyProjectM()
		{

			if ( m_projectM )
			{
				delete ( m_projectM );
				m_projectM = 0;
			}
		}

		QTimer * m_mouseTimer;
		QMutex * m_audioMutex;
		QMutex m_presetSeizeMutex;
		bool m_presetWasLocked;
	protected:


		void keyReleaseEvent ( QKeyEvent * e )
		{

			projectMKeycode pkey;
			bool ignore = false;
			switch ( e->key() )
			{
				case Qt::Key_F4:
					pkey =  PROJECTM_K_F4;
					break;
				case Qt::Key_F3:
					pkey =  PROJECTM_K_F3;
					break;
				case Qt::Key_F2:
					pkey =  PROJECTM_K_F2;
					break;
				case Qt::Key_F1:
					pkey =  PROJECTM_K_F1;
					break;
				case Qt::Key_R:
					if (e->modifiers() & Qt::ShiftModifier)
						pkey =  PROJECTM_K_R;
					else
						pkey =  PROJECTM_K_r;
					break;				
				case Qt::Key_L:
					pkey =  PROJECTM_K_l;
					ignore = true;
					break;
				case Qt::Key_N:	
					if (e->modifiers() & Qt::ShiftModifier)
						pkey =  PROJECTM_K_N;
					else
						pkey =  PROJECTM_K_n;
					break;
				case Qt::Key_P:
					if (e->modifiers() & Qt::ShiftModifier)
						pkey =  PROJECTM_K_P;
					else
						pkey =  PROJECTM_K_p;
					break;
				case Qt::Key_F5:
					pkey =  PROJECTM_K_F5;
					break;
				case Qt::Key_Plus:
					pkey =  PROJECTM_K_PLUS;
					break;
				case Qt::Key_Minus:
					pkey =  PROJECTM_K_MINUS;
					break;
				case Qt::Key_Equal:
					pkey = PROJECTM_K_EQUALS;
					break;
				default:
					e->ignore();
					return;
			}
			projectMModifier modifier;

			m_projectM->key_handler ( PROJECTM_KEYDOWN, pkey, modifier );
			if ( ignore )
				e->ignore();


		}

		void initializeGL()
		{

		        if (m_projectM == 0) {
			    this->m_projectM = new QProjectM ( m_config_file );
			    projectM_Initialized ( m_projectM );
			}
		}

		inline void paintGL()
		{
			m_projectM->renderFrame();
		}

	private:
		int mouseHideTimeoutSeconds;
		void setup_opengl ( int w, int h )
		{

			/* Our shading model--Gouraud (smooth). */
			glShadeModel ( GL_SMOOTH );
			/* Culling. */
			//    glCullFace( GL_BACK );
			//    glFrontFace( GL_CCW );
			//    glEnable( GL_CULL_FACE );
			/* Set the clear color. */
			glClearColor ( 0, 0, 0, 0 );
			/* Setup our viewport. */
			glViewport ( 0, 0, w, h );
			/*
					* Change to the projection matrix and set
					* our viewing volume.
			*/
			glMatrixMode ( GL_TEXTURE );
			glLoadIdentity();

			//    gluOrtho2D(0.0, (GLfloat) width, 0.0, (GLfloat) height);
			glMatrixMode ( GL_PROJECTION );
			glLoadIdentity();

			//    glFrustum(0.0, height, 0.0,width,10,40);
			glMatrixMode ( GL_MODELVIEW );
			glLoadIdentity();

			glDrawBuffer ( GL_BACK );
			glReadBuffer ( GL_BACK );
			glEnable ( GL_BLEND );

			glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
			// glBlendFunc(GL_SRC_ALPHA, GL_ONE);
			glEnable ( GL_LINE_SMOOTH );
			glEnable ( GL_POINT_SMOOTH );
			glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
//   glClear(GL_COLOR_BUFFER_BIT);

			// glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGB,0,0,texsize,texsize,0);
			//glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,texsize,texsize);
			glLineStipple ( 2, 0xAAAA );
		}


};
#endif