1 /*
2  * Hydrogen
3  * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4  *
5  * http://www.hydrogen-music.org
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY, without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22 
23 #include "hydrogen/midi_map.h"
24 #include "MidiSenseWidget.h"
25 #include <hydrogen/hydrogen.h>
26 
27 const char* MidiSenseWidget::__class_name = "MidiSenseWidget";
28 
MidiSenseWidget(QWidget * pParent,bool directWr,Action * midiAction)29 MidiSenseWidget::MidiSenseWidget(QWidget* pParent, bool directWr, Action* midiAction): QDialog( pParent ) , Object(__class_name)
30 {
31 	m_DirectWrite = directWr;
32 	m_pAction = midiAction;
33 
34 	setWindowTitle( "Waiting.." );
35 	setFixedSize( 280, 100 );
36 
37 	bool midiOperable = false;
38 
39 	m_pURLLabel = new QLabel( this );
40 	m_pURLLabel->setAlignment( Qt::AlignCenter );
41 
42 	if(m_pAction != nullptr){
43 		m_pURLLabel->setText( "Waiting for midi input..." );
44 		midiOperable = true;
45 	} else {
46 
47 		/*
48 		 *   Check if this widget got called from the midiTable in the preferences
49 		 *   window(directWrite=false) or by clicking on a midiLearn-capable gui item(directWrite=true)
50 		 */
51 
52 		if(m_DirectWrite){
53 			m_pURLLabel->setText( tr("This element is not midi operable.") );
54 			midiOperable = false;
55 		} else {
56 			m_pURLLabel->setText( tr("Waiting for midi input...") );
57 			midiOperable = true;
58 		}
59 	}
60 
61 	QVBoxLayout* pVBox = new QVBoxLayout( this );
62 	pVBox->addWidget( m_pURLLabel );
63 	setLayout( pVBox );
64 
65 	H2Core::Hydrogen *pEngine = H2Core::Hydrogen::get_instance();
66 	pEngine->lastMidiEvent = "";
67 	pEngine->lastMidiEventParameter = 0;
68 
69 	m_LastMidiEventParameter = 0;
70 
71 	m_pUpdateTimer = new QTimer( this );
72 
73 	if(midiOperable)
74 	{
75 		/*
76 		 * If the widget is not midi operable, we can omit
77 		 * starting the timer which listens to midi input..
78 		 */
79 
80 		connect( m_pUpdateTimer, SIGNAL( timeout() ), this, SLOT( updateMidi() ) );
81 		m_pUpdateTimer->start( 100 );
82 	}
83 };
84 
~MidiSenseWidget()85 MidiSenseWidget::~MidiSenseWidget(){
86 	INFOLOG("DESTROY");
87 	m_pUpdateTimer->stop();
88 }
89 
updateMidi()90 void MidiSenseWidget::updateMidi(){
91 	H2Core::Hydrogen *pEngine = H2Core::Hydrogen::get_instance();
92 	if(	!pEngine->lastMidiEvent.isEmpty() ){
93 		m_sLastMidiEvent = pEngine->lastMidiEvent;
94 		m_LastMidiEventParameter = pEngine->lastMidiEventParameter;
95 
96 
97 		if( m_DirectWrite ){
98 			//write the action / parameter combination to the midiMap
99 			MidiMap *pMidiMap = MidiMap::get_instance();
100 
101 			assert(m_pAction);
102 
103 			Action* pAction = new Action( m_pAction->getType() );
104 
105 			pAction->setParameter1( m_pAction->getParameter1() );
106 
107 			if( m_sLastMidiEvent.left(2) == "CC" ){
108 				pMidiMap->registerCCEvent( m_LastMidiEventParameter , pAction );
109 			} else if( m_sLastMidiEvent.left(3) == "MMC" ){
110 				pMidiMap->registerMMCEvent( m_sLastMidiEvent , pAction );
111 			} else if( m_sLastMidiEvent.left(4) == "NOTE" ){
112 				pMidiMap->registerNoteEvent( m_LastMidiEventParameter , pAction );
113 			} else if (m_sLastMidiEvent.left(14) == "PROGRAM_CHANGE" ){
114 				pMidiMap->registerPCEvent( pAction );
115 			} else {
116 				/* In all other cases, the midiMap cares for deleting the pointer */
117 
118 				delete pAction;
119 			}
120 		}
121 
122 		close();
123 	}
124 
125 }
126 
127