root/branches/midiAutosense/gui/src/widgets/midiTable.cpp @ 352

Revision 352, 6.3 KB (checked in by smoors, 5 years ago)

added new Wigdet for midiAutoSense

Line 
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 "midiTable.h"
24#include "MidiSenseWidget.h"
25
26#include <hydrogen/midiMap.h>
27#include <hydrogen/Preferences.h>
28#include <hydrogen/globals.h>
29#include <hydrogen/action.h>
30#include <hydrogen/midiMap.h>
31
32
33midiTable::midiTable( QWidget *pParent )
34 : QTableWidget( pParent ) , Object("midiTable")
35{
36        rowCount = 0;
37        setupMidiTable();
38}
39
40
41
42midiTable::~midiTable()
43{
44       
45        int myRow = 0;
46       
47        for( myRow = 0; myRow <=  rowCount ; myRow++)
48        {
49                delete cellWidget(myRow,0);
50                delete cellWidget(myRow,1);
51                delete cellWidget(myRow,2);
52                delete cellWidget(myRow,3);
53        }
54}
55
56void midiTable::midiSensePressed( int row ){
57        MidiSenseWidget mW(this );
58        mW.exec();
59       
60        QComboBox * eventCombo =  dynamic_cast <QComboBox *> ( cellWidget( row, 1 ) );
61        QSpinBox * eventSpinner = dynamic_cast <QSpinBox *> ( cellWidget( row, 2 ) );
62
63
64        eventCombo->setCurrentIndex( eventCombo->findText( mW.lastMidiEvent ) );
65
66        eventSpinner->setValue( mW.lastMidiEventParameter );
67       
68       
69}
70
71void midiTable::updateTable(){
72       
73        if( rowCount > 0 )
74        {
75                QComboBox * eventCombo =  dynamic_cast <QComboBox *> ( cellWidget( rowCount-1, 1 ) );
76                QComboBox * actionCombo = dynamic_cast <QComboBox *> ( cellWidget( rowCount-1, 3 ) );
77
78                if( eventCombo == NULL or actionCombo == NULL) return;
79
80                if( actionCombo->currentText() != "" and eventCombo->currentText() != "" ){
81                        insertNewRow("", "", 0, 0);
82                }
83        }
84}
85
86void midiTable::insertNewRow(QString actionString , QString eventString, int eventParameter , int actionParameter)
87{
88        actionManager *aH = actionManager::getInstance();
89
90        insertRow( rowCount );
91       
92        int oldRowCount = rowCount;
93
94        rowCount++;
95
96       
97
98        QPushButton *midiSenseButton = new QPushButton("Rec");
99        QSignalMapper *signalMapper = new QSignalMapper(this);
100
101        connect(midiSenseButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
102        signalMapper->setMapping(midiSenseButton, oldRowCount);
103        connect(signalMapper, SIGNAL(mapped(int)),
104         this, SLOT(midiSensePressed(int)));
105        setCellWidget( oldRowCount, 0, midiSenseButton );
106
107
108
109        QComboBox *eventBox = new QComboBox();
110        connect( eventBox , SIGNAL( currentIndexChanged( int ) ) , this , SLOT( updateTable() ) );
111        eventBox->insertItems( oldRowCount , aH->getEventList() );
112        eventBox->setCurrentIndex( eventBox->findText(eventString) );
113        setCellWidget( oldRowCount, 1, eventBox );
114       
115       
116        QSpinBox *eventParameterSpinner = new QSpinBox();
117        setCellWidget( oldRowCount , 2, eventParameterSpinner );
118        eventParameterSpinner->setValue( eventParameter );
119
120
121        QComboBox *actionBox = new QComboBox();
122        connect( actionBox , SIGNAL( currentIndexChanged( int ) ) , this , SLOT( updateTable() ) );
123        actionBox->insertItems( oldRowCount, aH->getActionList());
124        actionBox->setCurrentIndex ( actionBox->findText( actionString ) );
125        setCellWidget( oldRowCount , 3, actionBox );
126       
127
128        QSpinBox *actionParameterSpinner = new QSpinBox();
129       
130        setCellWidget( oldRowCount , 4, actionParameterSpinner );
131        actionParameterSpinner->setValue( actionParameter);
132
133
134}
135
136void midiTable::setupMidiTable()
137{
138        midiMap *mM = midiMap::getInstance();
139
140        QStringList items;
141        items <<  "" <<trUtf8("Event")  <<  trUtf8("Param.")  <<  trUtf8("Action")
142           <<  trUtf8("Param.") ;
143
144        setRowCount( 0 );
145        setColumnCount( 5 );
146
147        verticalHeader()->hide();
148
149        setHorizontalHeaderLabels( items );
150       
151       
152        setFixedWidth( 500 );
153       
154        setColumnWidth( 0 , 25 );
155        setColumnWidth( 1 , 155 );
156        setColumnWidth( 2, 73 );
157        setColumnWidth( 3, 175 );
158        setColumnWidth( 4 , 73 );
159
160
161        bool ok;
162        std::map< QString , action *> mmcMap = mM->getMMCMap();
163        std::map< QString , action *>::iterator dIter(mmcMap.begin());
164
165       
166        for( dIter = mmcMap.begin(); dIter != mmcMap.end(); dIter++ )
167        {
168                action * pAction = dIter->second;
169                QString actionParameter;
170                int actionParameterInteger = 0;
171
172                if( pAction->getParameterList().size() != 0 ){
173                        actionParameter = pAction->getParameterList().at(0);
174                        actionParameterInteger = actionParameter.toInt(&ok,10);
175                }
176               
177                insertNewRow(pAction->getType() , dIter->first , 0 , actionParameterInteger );
178        }
179
180        for( int note = 0; note < 128; note++ )
181        {
182                action * pAction = mM->getNoteAction( note );
183                QString actionParameter;
184                int actionParameterInteger = 0;
185
186                if( pAction->getParameterList().size() != 0 ){
187                        actionParameter = pAction->getParameterList().at(0);
188                        actionParameterInteger = actionParameter.toInt(&ok,10);
189                }
190
191                if ( pAction->getType() == "NOTHING" ) continue;
192
193                insertNewRow(pAction->getType() , "NOTE" , note , actionParameterInteger );
194        }
195       
196        insertNewRow("","",0,0);
197
198}
199
200
201void midiTable::saveMidiTable(){
202       
203        delete  midiMap::getInstance();
204        midiMap *mM  = midiMap::getInstance();
205       
206        int row = 0;
207       
208        for( row = 0; row <  rowCount; row++ ){
209
210                QComboBox * eventCombo =  dynamic_cast <QComboBox *> ( cellWidget( row, 1 ) );
211       
212                QSpinBox * eventSpinner = dynamic_cast <QSpinBox *> ( cellWidget( row, 2 ) );
213
214                QComboBox * actionCombo = dynamic_cast <QComboBox *> ( cellWidget( row, 3 ) );
215
216                QSpinBox * actionSpinner = dynamic_cast <QSpinBox *> ( cellWidget( row, 4 ) );
217
218               
219
220                QString eventString;
221                QString actionString;
222               
223
224                if( eventCombo->currentText() != "" && actionCombo->currentText() != "" ){
225                        eventString = eventCombo->currentText();
226
227                        actionString = actionCombo->currentText();
228               
229                        action * pAction = new action( actionString );
230
231
232
233                        if( actionSpinner->cleanText() != ""){
234                                pAction->addParameter( actionSpinner->cleanText() );
235                        }
236       
237                        if( eventString.left(3) == "MMC" ){
238                                mM->registerMMCEvent( eventString , pAction );
239                        }
240                       
241                        if( eventString.left(4) == "NOTE" ){
242                                mM->registerNoteEvent( eventSpinner->cleanText().toInt() , pAction );
243                        }
244                }
245        }
246}
Note: See TracBrowser for help on using the browser.