--- qsampler/trunk/src/qsamplerInstrumentListForm.cpp 2007/11/20 16:48:04 1499 +++ qsampler/trunk/src/qsamplerInstrumentListForm.cpp 2010/03/13 20:14:38 2067 @@ -1,7 +1,7 @@ // qsamplerInstrumentListForm.cpp // /**************************************************************************** - Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved. + Copyright (C) 2003-2010, rncbc aka Rui Nuno Capela. All rights reserved. Copyright (C) 2007, Christian Schoenebeck This program is free software; you can redistribute it and/or @@ -20,90 +20,96 @@ *****************************************************************************/ +#include "qsamplerAbout.h" #include "qsamplerInstrumentListForm.h" +#include "qsamplerInstrumentList.h" + #include "qsamplerInstrumentForm.h" -#include "qsamplerMainForm.h" + #include "qsamplerOptions.h" #include "qsamplerInstrument.h" +#include "qsamplerMainForm.h" + +#include +#include +#include namespace QSampler { -InstrumentListForm::InstrumentListForm ( QWidget* parent, Qt::WindowFlags flags ) - : QMainWindow(parent, flags) +//------------------------------------------------------------------------- +// QSampler::InstrumentListForm -- Instrument map list form implementation. +// + +InstrumentListForm::InstrumentListForm ( + QWidget* pParent, Qt::WindowFlags wflags ) + : QMainWindow(pParent, wflags) { - ui.setupUi(this); + m_ui.setupUi(this); - 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); - m_pMapComboBox->setToolTip(tr("Instrument Map")); - ui.InstrumentToolbar->addWidget(m_pMapComboBox); - - ui.InstrumentToolbar->addSeparator(); - ui.InstrumentToolbar->addAction(ui.newInstrumentAction); - ui.InstrumentToolbar->addAction(ui.editInstrumentAction); - ui.InstrumentToolbar->addAction(ui.deleteInstrumentAction); - ui.InstrumentToolbar->addSeparator(); - ui.InstrumentToolbar->addAction(ui.refreshInstrumentsAction); + m_pInstrumentListView = new InstrumentListView(this); + QMainWindow::setCentralWidget(m_pInstrumentListView); - ui.InstrumentTable->setModel(&model); - ui.InstrumentTable->setItemDelegate(&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); QObject::connect(m_pMapComboBox, SIGNAL(activated(int)), SLOT(activateMap(int))); - + QObject::connect(m_pInstrumentListView->selectionModel(), + SIGNAL(currentRowChanged(const QModelIndex&,const QModelIndex&)), + SLOT(stabilizeForm())); QObject::connect( - ui.refreshInstrumentsAction, - SIGNAL(triggered()), - SLOT(refreshInstruments(void)) - ); - - connect( - ui.InstrumentTable, + m_pInstrumentListView, SIGNAL(activated(const QModelIndex&)), - SLOT(editInstrument(const QModelIndex&)) - ); - connect( - ui.newInstrumentAction, + SLOT(editInstrument(const QModelIndex&))); + QObject::connect( + m_ui.newInstrumentAction, SIGNAL(triggered()), - SLOT(newInstrument()) - ); - connect( - ui.deleteInstrumentAction, + SLOT(newInstrument())); + QObject::connect( + m_ui.deleteInstrumentAction, SIGNAL(triggered()), - SLOT(deleteInstrument()) - ); - connect( - ui.editInstrumentAction, + SLOT(deleteInstrument())); + QObject::connect( + m_ui.editInstrumentAction, + SIGNAL(triggered()), + SLOT(editInstrument())); + QObject::connect( + m_ui.refreshInstrumentsAction, SIGNAL(triggered()), - SLOT(editInstrument()) - ); + SLOT(refreshInstruments())); + + // Things must be stable from the start. + stabilizeForm(); } -InstrumentListForm::~InstrumentListForm() { + +InstrumentListForm::~InstrumentListForm (void) +{ delete m_pMapComboBox; + delete m_pInstrumentListView; } // 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); } @@ -114,9 +120,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(); } @@ -127,7 +144,7 @@ if (pMainForm == NULL) return; - qsamplerOptions *pOptions = pMainForm->options(); + Options *pOptions = pMainForm->options(); if (pOptions == NULL) return; @@ -139,7 +156,7 @@ // Populate maps list. m_pMapComboBox->clear(); m_pMapComboBox->addItem(tr("(All)")); - m_pMapComboBox->insertItems(1, qsamplerInstrument::getMapNames()); + m_pMapComboBox->insertItems(1, Instrument::getMapNames()); // Adjust to saved selection... if (iMap < 0 || iMap >= m_pMapComboBox->count()) @@ -158,7 +175,7 @@ if (pMainForm == NULL) return; - qsamplerOptions *pOptions = pMainForm->options(); + Options *pOptions = pMainForm->options(); if (pOptions == NULL) return; @@ -166,82 +183,153 @@ if (iMidiMap >= 0) pOptions->iMidiMap = iMidiMap; - model.setMidiMap(iMidiMap); - model.refresh(); -} + m_pInstrumentListView->setMidiMap(iMidiMap); + m_pInstrumentListView->refresh(); -void InstrumentListForm::editInstrument() { - const QModelIndex index = ui.InstrumentTable->currentIndex(); - editInstrument(index); + stabilizeForm(); } -void InstrumentListForm::editInstrument(const QModelIndex& index) { - if (!index.isValid() || !index.data(Qt::UserRole).isValid()) + +void InstrumentListForm::newInstrument (void) +{ + Instrument instrument; + + InstrumentForm form(this); + form.setup(&instrument); + if (!form.exec()) return; - qsamplerInstrument* pInstrument = - (qsamplerInstrument*) index.data(Qt::UserRole).value(); + // Commit... + instrument.mapInstrument(); - if (!pInstrument) return; + // add new item to the table model + m_pInstrumentListView->addInstrument( + instrument.map(), + instrument.bank(), + instrument.prog()); - // 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(); - } + stabilizeForm(); } -void InstrumentListForm::newInstrument() { - qsamplerInstrument instrument; +void InstrumentListForm::editInstrument (void) +{ + editInstrument(m_pInstrumentListView->currentIndex()); +} + + +void InstrumentListForm::editInstrument ( const QModelIndex& index ) +{ + if (!index.isValid()) + return; + + Instrument *pInstrument + = static_cast (index.internalPointer()); + if (pInstrument == NULL) + return; + + // Save current key values... + int iMap = pInstrument->map(); + int iBank = pInstrument->bank(); + int iProg = pInstrument->prog(); + + Instrument instrument(iMap, iBank, iProg); + + // Do the edit dance... InstrumentForm form(this); form.setup(&instrument); - if (!form.exec()) return; - + 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(); + + // Check whether we changed instrument key... + if (instrument.map() == iMap && + instrument.bank() == iBank && + instrument.prog() == iProg) { + // Just update tree item... + //pItem->update(); + } else { + // Unmap old instance... + Instrument(iMap, iBank, iProg).unmapInstrument(); + // correct the position of the instrument in the model + m_pInstrumentListView->updateInstrument(pInstrument); + } + + stabilizeForm(); } -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(); +void InstrumentListForm::deleteInstrument (void) +{ + const QModelIndex& index = m_pInstrumentListView->currentIndex(); + if (!index.isValid()) + return; - if (!pInstrument) return; + Instrument *pInstrument + = static_cast (index.internalPointer()); + if (pInstrument == NULL) + return; + + MainForm *pMainForm = MainForm::getInstance(); + if (pMainForm == NULL) + return; + + // Prompt user if this is for real... + Options *pOptions = pMainForm->options(); + if (pOptions && pOptions->bConfirmRemove) { + if (QMessageBox::warning(this, + QSAMPLER_TITLE ": " + tr("Warning"), + tr("About to delete instrument map entry:\n\n" + "%1\n\n" + "Are you sure?") + .arg(pInstrument->name()), + QMessageBox::Ok | QMessageBox::Cancel) + == QMessageBox::Cancel) + return; + } pInstrument->unmapInstrument(); + // let the instrument vanish from the table model - model.removeInstrument(*pInstrument); - // Notify we've changes... - emit model.reset(); + m_pInstrumentListView->removeInstrument(pInstrument); + + stabilizeForm(); } + +// Update form actions enablement... +void InstrumentListForm::stabilizeForm (void) +{ + MainForm *pMainForm = MainForm::getInstance(); + + bool bEnabled = (pMainForm && pMainForm->client()); + m_ui.newInstrumentAction->setEnabled(bEnabled); + const QModelIndex& index = m_pInstrumentListView->currentIndex(); + bEnabled = (bEnabled && index.isValid()); + m_ui.editInstrumentAction->setEnabled(bEnabled); + m_ui.deleteInstrumentAction->setEnabled(bEnabled); +} + + +// Context menu request. +void InstrumentListForm::contextMenuEvent ( + QContextMenuEvent *pContextMenuEvent ) +{ + QMenu menu(this); + + menu.addAction(m_ui.newInstrumentAction); + menu.addSeparator(); + menu.addAction(m_ui.editInstrumentAction); + menu.addAction(m_ui.deleteInstrumentAction); + menu.addSeparator(); + menu.addAction(m_ui.refreshInstrumentsAction); + + menu.exec(pContextMenuEvent->globalPos()); +} + + } // namespace QSampler