| 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 "../Skin.h" |
|---|
| 24 | |
|---|
| 25 | #include "MidiSenseWidget.h" |
|---|
| 26 | #include "midiTable.h" |
|---|
| 27 | #include <hydrogen/midiMap.h> |
|---|
| 28 | #include <hydrogen/Preferences.h> |
|---|
| 29 | #include <hydrogen/globals.h> |
|---|
| 30 | #include <hydrogen/action.h> |
|---|
| 31 | #include <hydrogen/midiMap.h> |
|---|
| 32 | #include <hydrogen/hydrogen.h> |
|---|
| 33 | |
|---|
| 34 | midiTable::midiTable( QWidget *pParent ) |
|---|
| 35 | : QTableWidget( pParent ) , Object("midiTable") |
|---|
| 36 | { |
|---|
| 37 | rowCount = 0; |
|---|
| 38 | setupMidiTable(); |
|---|
| 39 | |
|---|
| 40 | m_pUpdateTimer = new QTimer( this ); |
|---|
| 41 | //connect( m_pUpdateTimer, SIGNAL( timeout() ), this, SLOT( updateMidi() ) ); |
|---|
| 42 | |
|---|
| 43 | currentMidiAutosenseRow = 0; |
|---|
| 44 | |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | midiTable::~midiTable() |
|---|
| 50 | { |
|---|
| 51 | |
|---|
| 52 | int myRow = 0; |
|---|
| 53 | |
|---|
| 54 | for( myRow = 0; myRow <= rowCount ; myRow++) |
|---|
| 55 | { |
|---|
| 56 | delete cellWidget(myRow,0); |
|---|
| 57 | delete cellWidget(myRow,1); |
|---|
| 58 | delete cellWidget(myRow,2); |
|---|
| 59 | delete cellWidget(myRow,3); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | //delete m_pUpdateTimer; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /* |
|---|
| 66 | void midiTable::updateMidi(){ |
|---|
| 67 | |
|---|
| 68 | H2Core::Hydrogen *pEngine = H2Core::Hydrogen::get_instance(); |
|---|
| 69 | |
|---|
| 70 | if( pEngine->lastMidiEvent != "" ){ |
|---|
| 71 | QString lastMidiEvent = pEngine->lastMidiEvent; |
|---|
| 72 | int lastMidiEventParameter = pEngine->lastMidiEventParameter; |
|---|
| 73 | |
|---|
| 74 | m_pUpdateTimer->stop(); |
|---|
| 75 | |
|---|
| 76 | QComboBox * eventCombo = dynamic_cast <QComboBox *> ( cellWidget( currentMidiAutosenseRow, 1 ) ); |
|---|
| 77 | QSpinBox * eventSpinner = dynamic_cast <QSpinBox *> ( cellWidget( currentMidiAutosenseRow, 2 ) ); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | eventCombo->setCurrentIndex( eventCombo->findText( lastMidiEvent ) ); |
|---|
| 81 | |
|---|
| 82 | eventSpinner->setValue( lastMidiEventParameter ); |
|---|
| 83 | } |
|---|
| 84 | }*/ |
|---|
| 85 | |
|---|
| 86 | void midiTable::midiSensePressed( int row ){ |
|---|
| 87 | |
|---|
| 88 | currentMidiAutosenseRow = row; |
|---|
| 89 | MidiSenseWidget mW( this ); |
|---|
| 90 | mW.exec(); |
|---|
| 91 | |
|---|
| 92 | QComboBox * eventCombo = dynamic_cast <QComboBox *> ( cellWidget( row, 1 ) ); |
|---|
| 93 | QSpinBox * eventSpinner = dynamic_cast <QSpinBox *> ( cellWidget( row, 2 ) ); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | eventCombo->setCurrentIndex( eventCombo->findText( mW.lastMidiEvent ) ); |
|---|
| 97 | eventSpinner->setValue( mW.lastMidiEventParameter ); |
|---|
| 98 | |
|---|
| 99 | m_pUpdateTimer->start( 100 ); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | void midiTable::updateTable(){ |
|---|
| 103 | |
|---|
| 104 | if( rowCount > 0 ) |
|---|
| 105 | { |
|---|
| 106 | QComboBox * eventCombo = dynamic_cast <QComboBox *> ( cellWidget( rowCount-1, 1 ) ); |
|---|
| 107 | QComboBox * actionCombo = dynamic_cast <QComboBox *> ( cellWidget( rowCount-1, 3 ) ); |
|---|
| 108 | |
|---|
| 109 | if( eventCombo == NULL or actionCombo == NULL) return; |
|---|
| 110 | |
|---|
| 111 | if( actionCombo->currentText() != "" and eventCombo->currentText() != "" ){ |
|---|
| 112 | insertNewRow("", "", 0, 0); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | void midiTable::insertNewRow(QString actionString , QString eventString, int eventParameter , int actionParameter) |
|---|
| 118 | { |
|---|
| 119 | ActionManager *aH = ActionManager::getInstance(); |
|---|
| 120 | |
|---|
| 121 | insertRow( rowCount ); |
|---|
| 122 | |
|---|
| 123 | int oldRowCount = rowCount; |
|---|
| 124 | |
|---|
| 125 | rowCount++; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | QPushButton *midiSenseButton = new QPushButton(this); |
|---|
| 130 | midiSenseButton->setIcon(QIcon(Skin::getImagePath() + "/preferencesDialog/rec.png")); |
|---|
| 131 | midiSenseButton->setToolTip( trUtf8("press button to record midi event") ); |
|---|
| 132 | |
|---|
| 133 | QSignalMapper *signalMapper = new QSignalMapper(this); |
|---|
| 134 | |
|---|
| 135 | connect(midiSenseButton, SIGNAL( clicked()), signalMapper, SLOT( map() )); |
|---|
| 136 | signalMapper->setMapping( midiSenseButton, oldRowCount ); |
|---|
| 137 | connect( signalMapper, SIGNAL(mapped( int ) ), |
|---|
| 138 | this, SLOT( midiSensePressed(int) ) ); |
|---|
| 139 | setCellWidget( oldRowCount, 0, midiSenseButton ); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | QComboBox *eventBox = new QComboBox(); |
|---|
| 144 | connect( eventBox , SIGNAL( currentIndexChanged( int ) ) , this , SLOT( updateTable() ) ); |
|---|
| 145 | eventBox->insertItems( oldRowCount , aH->getEventList() ); |
|---|
| 146 | eventBox->setCurrentIndex( eventBox->findText(eventString) ); |
|---|
| 147 | setCellWidget( oldRowCount, 1, eventBox ); |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | QSpinBox *eventParameterSpinner = new QSpinBox(); |
|---|
| 151 | setCellWidget( oldRowCount , 2, eventParameterSpinner ); |
|---|
| 152 | eventParameterSpinner->setValue( eventParameter ); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | QComboBox *actionBox = new QComboBox(); |
|---|
| 156 | connect( actionBox , SIGNAL( currentIndexChanged( int ) ) , this , SLOT( updateTable() ) ); |
|---|
| 157 | actionBox->insertItems( oldRowCount, aH->getActionList()); |
|---|
| 158 | actionBox->setCurrentIndex ( actionBox->findText( actionString ) ); |
|---|
| 159 | setCellWidget( oldRowCount , 3, actionBox ); |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | QSpinBox *actionParameterSpinner = new QSpinBox(); |
|---|
| 163 | |
|---|
| 164 | setCellWidget( oldRowCount , 4, actionParameterSpinner ); |
|---|
| 165 | actionParameterSpinner->setValue( actionParameter); |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void midiTable::setupMidiTable() |
|---|
| 171 | { |
|---|
| 172 | midiMap *mM = midiMap::getInstance(); |
|---|
| 173 | |
|---|
| 174 | QStringList items; |
|---|
| 175 | items << "" <<trUtf8("Event") << trUtf8("Param.") << trUtf8("Action") |
|---|
| 176 | << trUtf8("Param.") ; |
|---|
| 177 | |
|---|
| 178 | setRowCount( 0 ); |
|---|
| 179 | setColumnCount( 5 ); |
|---|
| 180 | |
|---|
| 181 | verticalHeader()->hide(); |
|---|
| 182 | |
|---|
| 183 | setHorizontalHeaderLabels( items ); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | setFixedWidth( 500 ); |
|---|
| 187 | |
|---|
| 188 | setColumnWidth( 0 , 25 ); |
|---|
| 189 | setColumnWidth( 1 , 155 ); |
|---|
| 190 | setColumnWidth( 2, 73 ); |
|---|
| 191 | setColumnWidth( 3, 175 ); |
|---|
| 192 | setColumnWidth( 4 , 73 ); |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | bool ok; |
|---|
| 196 | std::map< QString , Action *> mmcMap = mM->getMMCMap(); |
|---|
| 197 | std::map< QString , Action *>::iterator dIter(mmcMap.begin()); |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | for( dIter = mmcMap.begin(); dIter != mmcMap.end(); dIter++ ) |
|---|
| 201 | { |
|---|
| 202 | Action * pAction = dIter->second; |
|---|
| 203 | QString actionParameter; |
|---|
| 204 | int actionParameterInteger = 0; |
|---|
| 205 | |
|---|
| 206 | actionParameter = pAction->getParameter1(); |
|---|
| 207 | actionParameterInteger = actionParameter.toInt(&ok,10); |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | insertNewRow(pAction->getType() , dIter->first , 0 , actionParameterInteger ); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | for( int note = 0; note < 128; note++ ) |
|---|
| 214 | { |
|---|
| 215 | Action * pAction = mM->getNoteAction( note ); |
|---|
| 216 | QString actionParameter; |
|---|
| 217 | int actionParameterInteger = 0; |
|---|
| 218 | |
|---|
| 219 | actionParameter = pAction->getParameter1(); |
|---|
| 220 | actionParameterInteger = actionParameter.toInt(&ok,10); |
|---|
| 221 | |
|---|
| 222 | if ( pAction->getType() == "NOTHING" ) continue; |
|---|
| 223 | |
|---|
| 224 | insertNewRow(pAction->getType() , "NOTE" , note , actionParameterInteger ); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | for( int parameter = 0; parameter < 128; parameter++ ) |
|---|
| 228 | { |
|---|
| 229 | Action * pAction = mM->getCCAction( parameter ); |
|---|
| 230 | QString actionParameter; |
|---|
| 231 | int actionParameterInteger = 0; |
|---|
| 232 | |
|---|
| 233 | actionParameter = pAction->getParameter1(); |
|---|
| 234 | actionParameterInteger = actionParameter.toInt(&ok,10); |
|---|
| 235 | |
|---|
| 236 | if ( pAction->getType() == "NOTHING" ) continue; |
|---|
| 237 | |
|---|
| 238 | insertNewRow(pAction->getType() , "CC" , parameter , actionParameterInteger ); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | insertNewRow("","",0,0); |
|---|
| 243 | |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | void midiTable::saveMidiTable(){ |
|---|
| 248 | |
|---|
| 249 | delete midiMap::getInstance(); |
|---|
| 250 | midiMap *mM = midiMap::getInstance(); |
|---|
| 251 | |
|---|
| 252 | int row = 0; |
|---|
| 253 | |
|---|
| 254 | for( row = 0; row < rowCount; row++ ){ |
|---|
| 255 | |
|---|
| 256 | QComboBox * eventCombo = dynamic_cast <QComboBox *> ( cellWidget( row, 1 ) ); |
|---|
| 257 | |
|---|
| 258 | QSpinBox * eventSpinner = dynamic_cast <QSpinBox *> ( cellWidget( row, 2 ) ); |
|---|
| 259 | |
|---|
| 260 | QComboBox * actionCombo = dynamic_cast <QComboBox *> ( cellWidget( row, 3 ) ); |
|---|
| 261 | |
|---|
| 262 | QSpinBox * actionSpinner = dynamic_cast <QSpinBox *> ( cellWidget( row, 4 ) ); |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | QString eventString; |
|---|
| 267 | QString actionString; |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | if( eventCombo->currentText() != "" && actionCombo->currentText() != "" ){ |
|---|
| 271 | eventString = eventCombo->currentText(); |
|---|
| 272 | |
|---|
| 273 | actionString = actionCombo->currentText(); |
|---|
| 274 | |
|---|
| 275 | Action * pAction = new Action( actionString ); |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | if( actionSpinner->cleanText() != ""){ |
|---|
| 280 | pAction->setParameter1( actionSpinner->cleanText() ); |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | if( eventString.left(3) == "MMC" ){ |
|---|
| 284 | mM->registerMMCEvent( eventString , pAction ); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | if( eventString.left(4) == "NOTE" ){ |
|---|
| 288 | mM->registerNoteEvent( eventSpinner->cleanText().toInt() , pAction ); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | if( eventString.left(2) == "CC" ){ |
|---|
| 292 | mM->registerCCEvent( eventSpinner->cleanText().toInt() , pAction ); |
|---|
| 293 | } |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | } |
|---|