--- qsampler/trunk/src/qsamplerInstrumentListForm.cpp 2007/11/01 19:25:10 1466 +++ qsampler/trunk/src/qsamplerInstrumentListForm.cpp 2007/11/25 11:40:47 1523 @@ -20,56 +20,83 @@ *****************************************************************************/ +#include "qsamplerAbout.h" #include "qsamplerInstrumentListForm.h" -#include "qsamplerMainForm.h" +#include "qsamplerInstrumentForm.h" + #include "qsamplerOptions.h" #include "qsamplerInstrument.h" +#include "qsamplerMainForm.h" -#include +#include -namespace QSampler { -InstrumentListForm::InstrumentListForm(QWidget* parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) { - ui.setupUi(this); +namespace QSampler { - ui.newInstrumentAction->setText(tr("New &Instrument...")); - ui.newInstrumentAction->setShortcut(Qt::Key_Insert); - ui.editInstrumentAction->setText(tr("&Edit...")); - ui.editInstrumentAction->setShortcut(Qt::Key_Enter); - ui.deleteInstrumentAction->setText(tr("&Delete")); - ui.deleteInstrumentAction->setShortcut(Qt::Key_Delete); - ui.refreshInstrumentsAction->setText(tr("&Refresh")); - ui.refreshInstrumentsAction->setShortcut(Qt::Key_F5); - - // Setup toolbar widgets. - m_pMapComboBox = new QComboBox(ui.InstrumentToolbar); - m_pMapComboBox->setMinimumWidth(120); - m_pMapComboBox->setEnabled(false); - QToolTip::add(m_pMapComboBox, tr("Instrument Map")); - - 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); +InstrumentListForm::InstrumentListForm ( + QWidget* pParent, Qt::WindowFlags wflags ) + : QMainWindow(pParent, wflags) +{ + m_ui.setupUi(this); - ui.InstrumentTable->setModel(&model); - //ui.InstrumentTable->setDelegate(delegate); + // Setup toolbar widgets. + m_pMapComboBox = new QComboBox(m_ui.InstrumentToolbar); + m_pMapComboBox->setMinimumWidth(120); + m_pMapComboBox->setEnabled(false); + m_pMapComboBox->setToolTip(tr("Instrument Map")); + m_ui.InstrumentToolbar->addWidget(m_pMapComboBox); + + m_ui.InstrumentToolbar->addSeparator(); + m_ui.InstrumentToolbar->addAction(m_ui.newInstrumentAction); + m_ui.InstrumentToolbar->addAction(m_ui.editInstrumentAction); + m_ui.InstrumentToolbar->addAction(m_ui.deleteInstrumentAction); + m_ui.InstrumentToolbar->addSeparator(); + m_ui.InstrumentToolbar->addAction(m_ui.refreshInstrumentsAction); + + int iRowHeight = m_ui.InstrumentTable->fontMetrics().height() + 4; + m_ui.InstrumentTable->verticalHeader()->setDefaultSectionSize(iRowHeight); + m_ui.InstrumentTable->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft); + + m_ui.InstrumentTable->setModel(&m_model); + m_ui.InstrumentTable->setItemDelegate(&m_delegate); + m_ui.InstrumentTable->verticalHeader()->hide(); QObject::connect(m_pMapComboBox, SIGNAL(activated(int)), SLOT(activateMap(int))); - QObject::connect( - ui.refreshInstrumentsAction, + m_ui.refreshInstrumentsAction, + SIGNAL(triggered()), + SLOT(refreshInstruments(void))); + QObject::connect( + m_ui.InstrumentTable, + SIGNAL(activated(const QModelIndex&)), + SLOT(editInstrument(const QModelIndex&))); + QObject::connect( + m_ui.newInstrumentAction, + SIGNAL(triggered()), + SLOT(newInstrument())); + QObject::connect( + m_ui.deleteInstrumentAction, + SIGNAL(triggered()), + SLOT(deleteInstrument())); + QObject::connect( + m_ui.editInstrumentAction, SIGNAL(triggered()), - SLOT(refreshInstruments(void)) - ); + SLOT(editInstrument())); + + MainForm *pMainForm = MainForm::getInstance(); + if (pMainForm) { + QObject::connect(&m_model, + SIGNAL(instrumentsChanged()), + pMainForm, SLOT(sessionDirty())); + } } -InstrumentListForm::~InstrumentListForm() { + +InstrumentListForm::~InstrumentListForm (void) +{ delete m_pMapComboBox; } @@ -77,9 +104,9 @@ // Notify our parent that we're emerging. void InstrumentListForm::showEvent ( QShowEvent *pShowEvent ) { - //MainForm* pMainForm = MainForm::getInstance(); - //if (pMainForm) - // pMainForm->stabilizeForm(); + MainForm* pMainForm = MainForm::getInstance(); + if (pMainForm) + pMainForm->stabilizeForm(); QWidget::showEvent(pShowEvent); } @@ -90,9 +117,20 @@ { QWidget::hideEvent(pHideEvent); - //MainForm* pMainForm = MainForm::getInstance(); - //if (pMainForm) - // pMainForm->stabilizeForm(); + MainForm* pMainForm = MainForm::getInstance(); + if (pMainForm) + pMainForm->stabilizeForm(); +} + + +// Just about to notify main-window that we're closing. +void InstrumentListForm::closeEvent ( QCloseEvent * /*pCloseEvent*/ ) +{ + QWidget::hide(); + + MainForm *pMainForm = MainForm::getInstance(); + if (pMainForm) + pMainForm->stabilizeForm(); } @@ -108,19 +146,19 @@ return; // Get/save current map selection... - int iMap = m_pMapComboBox->currentItem(); + int iMap = m_pMapComboBox->currentIndex(); if (iMap < 0 || m_pMapComboBox->count() < 2) iMap = pOptions->iMidiMap + 1; // Populate maps list. m_pMapComboBox->clear(); - m_pMapComboBox->insertItem(tr("(All)")); - m_pMapComboBox->insertStringList(qsamplerInstrument::getMapNames()); + m_pMapComboBox->addItem(tr("(All)")); + m_pMapComboBox->insertItems(1, qsamplerInstrument::getMapNames()); // Adjust to saved selection... if (iMap < 0 || iMap >= m_pMapComboBox->count()) iMap = 0; - m_pMapComboBox->setCurrentItem(iMap); + m_pMapComboBox->setCurrentIndex(iMap); m_pMapComboBox->setEnabled(m_pMapComboBox->count() > 1); activateMap(iMap); @@ -142,8 +180,93 @@ if (iMidiMap >= 0) pOptions->iMidiMap = iMidiMap; - model.setMidiMap(iMidiMap); - model.refresh(); + m_model.setMidiMap(iMidiMap); + m_model.refresh(); +} + + +void InstrumentListForm::editInstrument (void) +{ + editInstrument(m_ui.InstrumentTable->currentIndex()); +} + + +void InstrumentListForm::editInstrument ( const QModelIndex& index ) +{ + if (!index.isValid() || !index.data(Qt::UserRole).isValid()) + return; + + qsamplerInstrument* pInstrument + = static_cast ( + index.data(Qt::UserRole).value ()); + + if (pInstrument == NULL) + 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 + m_model.resort(*pInstrument); + } + // Notify we've changes... + emit m_model.reset(); + } +} + + +void InstrumentListForm::newInstrument (void) +{ + qsamplerInstrument instrument; + + InstrumentForm form(this); + form.setup(&instrument); + if (!form.exec()) + return; + + // Commit... + instrument.mapInstrument(); + // add new item to the table model + m_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 (void) +{ + const QModelIndex& index = m_ui.InstrumentTable->currentIndex(); + if (!index.isValid() || !index.data(Qt::UserRole).isValid()) + return; + + qsamplerInstrument* pInstrument = + static_cast ( + index.data(Qt::UserRole).value ()); + + if (pInstrument == NULL) + return; + + pInstrument->unmapInstrument(); + // let the instrument vanish from the table model + m_model.removeInstrument(*pInstrument); + // Notify we've changes... + emit m_model.reset(); } } // namespace QSampler