--- qsampler/trunk/src/qsamplerInstrumentListForm.cpp 2007/11/23 09:32:06 1513 +++ qsampler/trunk/src/qsamplerInstrumentListForm.cpp 2019/08/13 10:19:32 3555 @@ -1,7 +1,7 @@ // qsamplerInstrumentListForm.cpp // /**************************************************************************** - Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved. + Copyright (C) 2003-2019, rncbc aka Rui Nuno Capela. All rights reserved. Copyright (C) 2007, Christian Schoenebeck This program is free software; you can redistribute it and/or @@ -23,47 +23,58 @@ #include "qsamplerAbout.h" #include "qsamplerInstrumentListForm.h" +#include "qsamplerInstrumentList.h" + #include "qsamplerInstrumentForm.h" #include "qsamplerOptions.h" #include "qsamplerInstrument.h" #include "qsamplerMainForm.h" +#include +#include +#include + +#include + namespace QSampler { +//------------------------------------------------------------------------- +// QSampler::InstrumentListForm -- Instrument map list form implementation. +// + InstrumentListForm::InstrumentListForm ( QWidget* pParent, Qt::WindowFlags wflags ) : QMainWindow(pParent, wflags) { m_ui.setupUi(this); + m_pInstrumentListView = new InstrumentListView(this); + QMainWindow::setCentralWidget(m_pInstrumentListView); + // Setup toolbar widgets. - m_pMapComboBox = new QComboBox(m_ui.InstrumentToolbar); + 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); - - m_ui.InstrumentTable->setModel(&m_model); - m_ui.InstrumentTable->setItemDelegate(&m_delegate); + + 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( - m_ui.refreshInstrumentsAction, - SIGNAL(triggered()), - SLOT(refreshInstruments(void))); - QObject::connect( - m_ui.InstrumentTable, + m_pInstrumentListView, SIGNAL(activated(const QModelIndex&)), SLOT(editInstrument(const QModelIndex&))); QObject::connect( @@ -78,19 +89,20 @@ m_ui.editInstrumentAction, SIGNAL(triggered()), SLOT(editInstrument())); + QObject::connect( + m_ui.refreshInstrumentsAction, + SIGNAL(triggered()), + SLOT(refreshInstruments())); - MainForm *pMainForm = MainForm::getInstance(); - if (pMainForm) { - QObject::connect(&m_model, - SIGNAL(instrumentsChanged()), - pMainForm, SLOT(sessionDirty())); - } + // Things must be stable from the start. + stabilizeForm(); } InstrumentListForm::~InstrumentListForm (void) { delete m_pMapComboBox; + delete m_pInstrumentListView; } @@ -131,11 +143,11 @@ void InstrumentListForm::refreshInstruments (void) { MainForm* pMainForm = MainForm::getInstance(); - if (pMainForm == NULL) + if (pMainForm == nullptr) return; - qsamplerOptions *pOptions = pMainForm->options(); - if (pOptions == NULL) + Options *pOptions = pMainForm->options(); + if (pOptions == nullptr) return; // Get/save current map selection... @@ -146,7 +158,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()) @@ -162,106 +174,180 @@ void InstrumentListForm::activateMap ( int iMap ) { MainForm* pMainForm = MainForm::getInstance(); - if (pMainForm == NULL) + if (pMainForm == nullptr) return; - qsamplerOptions *pOptions = pMainForm->options(); - if (pOptions == NULL) + Options *pOptions = pMainForm->options(); + if (pOptions == nullptr) return; int iMidiMap = iMap - 1; if (iMidiMap >= 0) pOptions->iMidiMap = iMidiMap; - m_model.setMidiMap(iMidiMap); - m_model.refresh(); + m_pInstrumentListView->setMidiMap(iMidiMap); + m_pInstrumentListView->refresh(); + + stabilizeForm(); +} + + +void InstrumentListForm::newInstrument (void) +{ + Instrument instrument; + + InstrumentForm form(this); + form.setup(&instrument); + if (!form.exec()) + return; + + // Commit... + instrument.mapInstrument(); + + // add new item to the table model + m_pInstrumentListView->addInstrument( + instrument.map(), + instrument.bank(), + instrument.prog()); + + stabilizeForm(); } void InstrumentListForm::editInstrument (void) { - editInstrument(m_ui.InstrumentTable->currentIndex()); + editInstrument(m_pInstrumentListView->currentIndex()); } void InstrumentListForm::editInstrument ( const QModelIndex& index ) { - if (!index.isValid() || !index.data(Qt::UserRole).isValid()) + if (!index.isValid()) return; - qsamplerInstrument* pInstrument - = static_cast ( - index.data(Qt::UserRole).value ()); - - if (pInstrument == NULL) + Instrument *pInstrument + = static_cast (index.internalPointer()); + if (pInstrument == nullptr) return; // Save current key values... - qsamplerInstrument oldInstrument(*pInstrument); + 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(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(); + if (!form.exec()) + return; + + // Commit... + pInstrument->mapInstrument(); + + // Check whether we changed instrument key... + if (pInstrument->map() == iMap && + pInstrument->bank() == iBank && + pInstrument->prog() == iProg) { + // Just update tree item... + m_pInstrumentListView->updateInstrument(pInstrument); + } else { + // Unmap old instance... + instrument.unmapInstrument(); + // Correct the position of the instrument in the model + m_pInstrumentListView->resortInstrument(pInstrument); } + + stabilizeForm(); } -void InstrumentListForm::newInstrument (void) +void InstrumentListForm::deleteInstrument (void) { - qsamplerInstrument instrument; + const QModelIndex& index = m_pInstrumentListView->currentIndex(); + if (!index.isValid()) + return; - InstrumentForm form(this); - form.setup(&instrument); - if (!form.exec()) + Instrument *pInstrument + = static_cast (index.internalPointer()); + if (pInstrument == nullptr) 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(); + MainForm *pMainForm = MainForm::getInstance(); + if (pMainForm == nullptr) + return; + + // Prompt user if this is for real... + Options *pOptions = pMainForm->options(); + if (pOptions && pOptions->bConfirmRemove) { + const QString& sTitle = QSAMPLER_TITLE ": " + tr("Warning"); + const QString& sText = tr( + "About to delete instrument map entry:\n\n" + "%1\n\n" + "Are you sure?") + .arg(pInstrument->name()); + #if 0 + if (QMessageBox::warning(this, sTitle, sText, + QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Cancel) + return; + #else + QMessageBox mbox(this); + mbox.setIcon(QMessageBox::Warning); + mbox.setWindowTitle(sTitle); + mbox.setText(sText); + mbox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel); + QCheckBox cbox(tr("Don't ask this again")); + cbox.setChecked(false); + cbox.blockSignals(true); + mbox.addButton(&cbox, QMessageBox::ActionRole); + if (mbox.exec() == QMessageBox::Cancel) + return; + if (cbox.isChecked()) + pOptions->bConfirmRemove = false; + #endif + } + + pInstrument->unmapInstrument(); + + // Let the instrument vanish from the table model... + m_pInstrumentListView->removeInstrument(pInstrument); + + stabilizeForm(); } -void InstrumentListForm::deleteInstrument (void) +// Update form actions enablement... +void InstrumentListForm::stabilizeForm (void) { - const QModelIndex& index = m_ui.InstrumentTable->currentIndex(); - if (!index.isValid() || !index.data(Qt::UserRole).isValid()) - return; + MainForm *pMainForm = MainForm::getInstance(); - qsamplerInstrument* pInstrument = - static_cast ( - index.data(Qt::UserRole).value ()); + 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); +} - 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(); +// 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