--- qsampler/trunk/src/qsamplerInstrumentListForm.cpp 2007/11/01 17:14:21 1464 +++ qsampler/trunk/src/qsamplerInstrumentListForm.cpp 2007/11/19 21:08:18 1492 @@ -22,6 +22,7 @@ #include "qsamplerInstrumentListForm.h" +#include "qsamplerInstrumentForm.h" #include "qsamplerMainForm.h" #include "qsamplerOptions.h" #include "qsamplerInstrument.h" @@ -30,7 +31,9 @@ namespace QSampler { -InstrumentListForm::InstrumentListForm(QWidget* parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) { +InstrumentListForm::InstrumentListForm ( QWidget* parent, Qt::WindowFlags flags ) + : QMainWindow(parent, flags) +{ ui.setupUi(this); ui.newInstrumentAction->setText(tr("New &Instrument...")); @@ -43,30 +46,52 @@ ui.refreshInstrumentsAction->setShortcut(Qt::Key_F5); // Setup toolbar widgets. - InstrumentToolbar = addToolBar(tr("MIDI Instruments")); - m_pMapComboBox = new QComboBox(InstrumentToolbar); + m_pMapComboBox = new QComboBox(ui.InstrumentToolbar); m_pMapComboBox->setMinimumWidth(120); m_pMapComboBox->setEnabled(false); QToolTip::add(m_pMapComboBox, tr("Instrument Map")); + ui.InstrumentToolbar->addWidget(m_pMapComboBox); - InstrumentToolbar->addSeparator(); - ui.newInstrumentAction->addTo(InstrumentToolbar); - ui.editInstrumentAction->addTo(InstrumentToolbar); - ui.deleteInstrumentAction->addTo(InstrumentToolbar); - InstrumentToolbar->addSeparator(); - ui.refreshInstrumentsAction->addTo(InstrumentToolbar); + ui.InstrumentToolbar->addSeparator(); + ui.newInstrumentAction->addTo(ui.InstrumentToolbar); + ui.editInstrumentAction->addTo(ui.InstrumentToolbar); + ui.deleteInstrumentAction->addTo(ui.InstrumentToolbar); + ui.InstrumentToolbar->addSeparator(); + ui.refreshInstrumentsAction->addTo(ui.InstrumentToolbar); ui.InstrumentTable->setModel(&model); - //ui.InstrumentTable->setDelegate(delegate); + ui.InstrumentTable->setItemDelegate(&delegate); QObject::connect(m_pMapComboBox, SIGNAL(activated(int)), SLOT(activateMap(int))); - connect( - ui.refreshInstrumentsAction, - SIGNAL(triggered()), SLOT(refreshInstruments(void)) + QObject::connect( + ui.refreshInstrumentsAction, + SIGNAL(triggered()), + SLOT(refreshInstruments(void)) ); + + connect( + ui.InstrumentTable, + SIGNAL(activated(const QModelIndex&)), + SLOT(editInstrument(const QModelIndex&)) + ); + connect( + ui.newInstrumentAction, + SIGNAL(triggered()), + SLOT(newInstrument()) + ); + connect( + ui.deleteInstrumentAction, + SIGNAL(triggered()), + SLOT(deleteInstrument()) + ); + connect( + ui.editInstrumentAction, + SIGNAL(triggered()), + SLOT(editInstrument()) + ); } InstrumentListForm::~InstrumentListForm() { @@ -146,6 +171,78 @@ model.refresh(); } +void InstrumentListForm::editInstrument() { + const QModelIndex index = ui.InstrumentTable->currentIndex(); + editInstrument(index); +} + +void InstrumentListForm::editInstrument(const QModelIndex& index) { + if (!index.isValid() || !index.data(Qt::UserRole).isValid()) + return; + + qsamplerInstrument* pInstrument = + (qsamplerInstrument*) index.data(Qt::UserRole).value(); + + if (!pInstrument) return; + + // Save current key values... + qsamplerInstrument oldInstrument(*pInstrument); + // Do the edit dance... + InstrumentForm form(this); + form.setup(pInstrument); + if (form.exec()) { + // Commit... + pInstrument->mapInstrument(); + // Check whether we changed instrument key... + if (oldInstrument.map() == pInstrument->map() && + oldInstrument.bank() == pInstrument->bank() && + oldInstrument.prog() == pInstrument->prog()) { + // just update tree item... + //pItem->update(); + } else { + // Unmap old instance... + oldInstrument.unmapInstrument(); + // correct the position of the instrument in the model + model.resort(*pInstrument); + } + // Notify we've changes... + emit model.reset(); + } +} + +void InstrumentListForm::newInstrument() { + qsamplerInstrument instrument; + + InstrumentForm form(this); + form.setup(&instrument); + if (!form.exec()) return; + + // Commit... + instrument.mapInstrument(); + // add new item to the table model + model.resort(instrument); + // Notify we've changes... + //emit model.reset(); + //FIXME: call above didnt really refresh, so we use this for now ... + refreshInstruments(); +} + +void InstrumentListForm::deleteInstrument() { + const QModelIndex index = ui.InstrumentTable->currentIndex(); + if (!index.isValid() || !index.data(Qt::UserRole).isValid()) return; + + qsamplerInstrument* pInstrument = + (qsamplerInstrument*) index.data(Qt::UserRole).value(); + + if (!pInstrument) return; + + pInstrument->unmapInstrument(); + // let the instrument vanish from the table model + model.removeInstrument(*pInstrument); + // Notify we've changes... + emit model.reset(); +} + } // namespace QSampler