--- qsampler/trunk/src/qsamplerChannel.cpp 2007/10/28 23:30:36 1461 +++ qsampler/trunk/src/qsamplerChannel.cpp 2014/05/20 10:59:37 2563 @@ -1,7 +1,8 @@ // qsamplerChannel.cpp // /**************************************************************************** - Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved. + Copyright (C) 2004-2014, rncbc aka Rui Nuno Capela. All rights reserved. + Copyright (C) 2007, 2008 Christian Schoenebeck This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -19,30 +20,34 @@ *****************************************************************************/ -#include "qsamplerUtilities.h" #include "qsamplerAbout.h" #include "qsamplerChannel.h" +#include "qsamplerUtilities.h" #include "qsamplerMainForm.h" #include "qsamplerChannelForm.h" -#include -#include +#include +#include #ifdef CONFIG_LIBGIG #include "gig.h" +#include "SF.h" #endif -#define QSAMPLER_INSTRUMENT_MAX 100 +namespace QSampler { + +#define QSAMPLER_INSTRUMENT_MAX 128 + +#define UNICODE_RIGHT_ARROW QChar(char(0x92), char(0x21)) -using namespace QSampler; //------------------------------------------------------------------------- -// qsamplerChannel - Sampler channel structure. +// QSampler::Channel - Sampler channel structure. // // Constructor. -qsamplerChannel::qsamplerChannel ( int iChannelID ) +Channel::Channel ( int iChannelID ) { m_iChannelID = iChannelID; @@ -58,19 +63,19 @@ m_iMidiMap = -1; m_sAudioDriver = "ALSA"; m_iAudioDevice = -1; - m_fVolume = 0.0; + m_fVolume = 0.0f; m_bMute = false; m_bSolo = false; } // Default destructor. -qsamplerChannel::~qsamplerChannel (void) +Channel::~Channel (void) { } // Create a new sampler channel, if not already. -bool qsamplerChannel::addChannel (void) +bool Channel::addChannel (void) { MainForm* pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -95,7 +100,7 @@ // Remove sampler channel. -bool qsamplerChannel::removeChannel (void) +bool Channel::removeChannel (void) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -121,31 +126,31 @@ // Channel-ID (aka Sammpler-Channel) accessors. -int qsamplerChannel::channelID (void) const +int Channel::channelID (void) const { return m_iChannelID; } -void qsamplerChannel::setChannelID ( int iChannelID ) +void Channel::setChannelID ( int iChannelID ) { m_iChannelID = iChannelID; } // Readable channel name. -QString qsamplerChannel::channelName (void) const +QString Channel::channelName (void) const { return (m_iChannelID < 0 ? QObject::tr("New Channel") : QObject::tr("Channel %1").arg(m_iChannelID)); } // Engine name accessors. -const QString& qsamplerChannel::engineName (void) const +const QString& Channel::engineName (void) const { return m_sEngineName; } -bool qsamplerChannel::loadEngine ( const QString& sEngineName ) +bool Channel::loadEngine ( const QString& sEngineName ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -155,7 +160,8 @@ if (m_iInstrumentStatus == 100 && m_sEngineName == sEngineName) return true; - if (::lscp_load_engine(pMainForm->client(), sEngineName.latin1(), m_iChannelID) != LSCP_OK) { + if (::lscp_load_engine(pMainForm->client(), + sEngineName.toUtf8().constData(), m_iChannelID) != LSCP_OK) { appendMessagesClient("lscp_load_engine"); return false; } @@ -168,49 +174,50 @@ // Instrument filename accessor. -const QString& qsamplerChannel::instrumentFile (void) const +const QString& Channel::instrumentFile (void) const { return m_sInstrumentFile; } // Instrument index accessor. -int qsamplerChannel::instrumentNr (void) const +int Channel::instrumentNr (void) const { return m_iInstrumentNr; } // Instrument name accessor. -const QString& qsamplerChannel::instrumentName (void) const +const QString& Channel::instrumentName (void) const { return m_sInstrumentName; } // Instrument status accessor. -int qsamplerChannel::instrumentStatus (void) const +int Channel::instrumentStatus (void) const { return m_iInstrumentStatus; } // Instrument file loader. -bool qsamplerChannel::loadInstrument ( const QString& sInstrumentFile, int iInstrumentNr ) +bool Channel::loadInstrument ( const QString& sInstrumentFile, int iInstrumentNr ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) return false; if (pMainForm->client() == NULL || m_iChannelID < 0) return false; - if (!isInstrumentFile(sInstrumentFile)) + if (!QFileInfo(sInstrumentFile).exists()) return false; - if (m_iInstrumentStatus == 100 && m_sInstrumentFile == sInstrumentFile && m_iInstrumentNr == iInstrumentNr) + if (m_iInstrumentStatus == 100 + && m_sInstrumentFile == sInstrumentFile + && m_iInstrumentNr == iInstrumentNr) return true; - if ( - ::lscp_load_instrument_non_modal( + if (::lscp_load_instrument_non_modal( pMainForm->client(), - qsamplerUtilities::lscpEscapePath(sInstrumentFile).latin1(), + qsamplerUtilities::lscpEscapePath( + sInstrumentFile).toUtf8().constData(), iInstrumentNr, m_iChannelID - ) != LSCP_OK - ) { + ) != LSCP_OK) { appendMessagesClient("lscp_load_instrument"); return false; } @@ -223,7 +230,7 @@ // Special instrument file/name/number settler. -bool qsamplerChannel::setInstrument ( const QString& sInstrumentFile, int iInstrumentNr ) +bool Channel::setInstrument ( const QString& sInstrumentFile, int iInstrumentNr ) { m_sInstrumentFile = sInstrumentFile; m_iInstrumentNr = iInstrumentNr; @@ -239,12 +246,12 @@ // MIDI driver type accessors (DEPRECATED). -const QString& qsamplerChannel::midiDriver (void) const +const QString& Channel::midiDriver (void) const { return m_sMidiDriver; } -bool qsamplerChannel::setMidiDriver ( const QString& sMidiDriver ) +bool Channel::setMidiDriver ( const QString& sMidiDriver ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -254,7 +261,8 @@ if (m_iInstrumentStatus == 100 && m_sMidiDriver == sMidiDriver) return true; - if (::lscp_set_channel_midi_type(pMainForm->client(), m_iChannelID, sMidiDriver.latin1()) != LSCP_OK) { + if (::lscp_set_channel_midi_type(pMainForm->client(), + m_iChannelID, sMidiDriver.toUtf8().constData()) != LSCP_OK) { appendMessagesClient("lscp_set_channel_midi_type"); return false; } @@ -267,12 +275,12 @@ // MIDI device accessors. -int qsamplerChannel::midiDevice (void) const +int Channel::midiDevice (void) const { return m_iMidiDevice; } -bool qsamplerChannel::setMidiDevice ( int iMidiDevice ) +bool Channel::setMidiDevice ( int iMidiDevice ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -295,12 +303,12 @@ // MIDI port number accessor. -int qsamplerChannel::midiPort (void) const +int Channel::midiPort (void) const { return m_iMidiPort; } -bool qsamplerChannel::setMidiPort ( int iMidiPort ) +bool Channel::setMidiPort ( int iMidiPort ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -323,12 +331,12 @@ // MIDI channel accessor. -int qsamplerChannel::midiChannel (void) const +int Channel::midiChannel (void) const { return m_iMidiChannel; } -bool qsamplerChannel::setMidiChannel ( int iMidiChannel ) +bool Channel::setMidiChannel ( int iMidiChannel ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -351,12 +359,12 @@ // MIDI instrument map accessor. -int qsamplerChannel::midiMap (void) const +int Channel::midiMap (void) const { return m_iMidiMap; } -bool qsamplerChannel::setMidiMap ( int iMidiMap ) +bool Channel::setMidiMap ( int iMidiMap ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -379,12 +387,12 @@ // Audio device accessor. -int qsamplerChannel::audioDevice (void) const +int Channel::audioDevice (void) const { return m_iAudioDevice; } -bool qsamplerChannel::setAudioDevice ( int iAudioDevice ) +bool Channel::setAudioDevice ( int iAudioDevice ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -407,12 +415,12 @@ // Audio driver type accessors (DEPRECATED). -const QString& qsamplerChannel::audioDriver (void) const +const QString& Channel::audioDriver (void) const { return m_sAudioDriver; } -bool qsamplerChannel::setAudioDriver ( const QString& sAudioDriver ) +bool Channel::setAudioDriver ( const QString& sAudioDriver ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -422,7 +430,8 @@ if (m_iInstrumentStatus == 100 && m_sAudioDriver == sAudioDriver) return true; - if (::lscp_set_channel_audio_type(pMainForm->client(), m_iChannelID, sAudioDriver.latin1()) != LSCP_OK) { + if (::lscp_set_channel_audio_type(pMainForm->client(), + m_iChannelID, sAudioDriver.toUtf8().constData()) != LSCP_OK) { appendMessagesClient("lscp_set_channel_audio_type"); return false; } @@ -435,12 +444,12 @@ // Channel volume accessors. -float qsamplerChannel::volume (void) const +float Channel::volume (void) const { return m_fVolume; } -bool qsamplerChannel::setVolume ( float fVolume ) +bool Channel::setVolume ( float fVolume ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -463,12 +472,12 @@ // Sampler channel mute state. -bool qsamplerChannel::channelMute (void) const +bool Channel::channelMute (void) const { return m_bMute; } -bool qsamplerChannel::setChannelMute ( bool bMute ) +bool Channel::setChannelMute ( bool bMute ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -493,12 +502,12 @@ // Sampler channel solo state. -bool qsamplerChannel::channelSolo (void) const +bool Channel::channelSolo (void) const { return m_bSolo; } -bool qsamplerChannel::setChannelSolo ( bool bSolo ) +bool Channel::setChannelSolo ( bool bSolo ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -523,12 +532,12 @@ // Audio routing accessors. -int qsamplerChannel::audioChannel ( int iAudioOut ) const +int Channel::audioChannel ( int iAudioOut ) const { return m_audioRouting[iAudioOut]; } -bool qsamplerChannel::setAudioChannel ( int iAudioOut, int iAudioIn ) +bool Channel::setAudioChannel ( int iAudioOut, int iAudioIn ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -553,14 +562,14 @@ } // The audio routing map itself. -const qsamplerChannelRoutingMap& qsamplerChannel::audioRouting (void) const +const ChannelRoutingMap& Channel::audioRouting (void) const { return m_audioRouting; } // Istrument name remapper. -void qsamplerChannel::updateInstrumentName (void) +void Channel::updateInstrumentName (void) { #ifndef CONFIG_INSTRUMENT_NAME m_sInstrumentName = getInstrumentName(m_sInstrumentFile, @@ -570,7 +579,7 @@ // Update whole channel info state. -bool qsamplerChannel::updateChannelInfo (void) +bool Channel::updateChannelInfo (void) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -664,7 +673,7 @@ // Reset channel method. -bool qsamplerChannel::channelReset (void) +bool Channel::channelReset (void) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -684,7 +693,7 @@ // Spawn instrument editor method. -bool qsamplerChannel::editChannel (void) +bool Channel::editChannel (void) { #ifdef CONFIG_EDIT_INSTRUMENT @@ -699,9 +708,9 @@ appendMessagesClient("lscp_edit_channel_instrument"); appendMessagesError(QObject::tr( "Could not launch an appropriate instrument editor " - "for the given instrument!\n" + "for the given instrument!\n\n" "Make sure you have an appropriate " - "instrument editor like 'gigedit' installed\n" + "instrument editor like 'gigedit' installed " "and that it placed its mandatory DLL file " "into the sampler's plugin directory.") ); @@ -716,7 +725,7 @@ appendMessagesError(QObject::tr( "Sorry, QSampler was compiled for a version of liblscp " - "which lacks this feature.\n" + "which lacks this feature.\n\n" "You may want to update liblscp and recompile QSampler afterwards.") ); @@ -727,7 +736,7 @@ // Channel setup dialog form. -bool qsamplerChannel::channelSetup ( QWidget *pParent ) +bool Channel::channelSetup ( QWidget *pParent ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm == NULL) @@ -749,14 +758,14 @@ // Redirected messages output methods. -void qsamplerChannel::appendMessages( const QString& s ) const +void Channel::appendMessages( const QString& s ) const { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm) pMainForm->appendMessages(channelName() + ' ' + s); } -void qsamplerChannel::appendMessagesColor( const QString& s, +void Channel::appendMessagesColor( const QString& s, const QString& c ) const { MainForm *pMainForm = MainForm::getInstance(); @@ -764,21 +773,21 @@ pMainForm->appendMessagesColor(channelName() + ' ' + s, c); } -void qsamplerChannel::appendMessagesText( const QString& s ) const +void Channel::appendMessagesText( const QString& s ) const { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm) pMainForm->appendMessagesText(channelName() + ' ' + s); } -void qsamplerChannel::appendMessagesError( const QString& s ) const +void Channel::appendMessagesError( const QString& s ) const { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm) pMainForm->appendMessagesError(channelName() + "\n\n" + s); } -void qsamplerChannel::appendMessagesClient( const QString& s ) const +void Channel::appendMessagesClient( const QString& s ) const { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm) @@ -787,7 +796,7 @@ // Context menu event handler. -void qsamplerChannel::contextMenuEvent( QContextMenuEvent *pEvent ) +void Channel::contextMenuEvent( QContextMenuEvent *pEvent ) { MainForm *pMainForm = MainForm::getInstance(); if (pMainForm) @@ -795,15 +804,15 @@ } -// FIXME: Check whether a given file is an instrument file. -bool qsamplerChannel::isInstrumentFile ( const QString& sInstrumentFile ) +// FIXME: Check whether a given file is an instrument file (DLS only). +bool Channel::isDlsInstrumentFile ( const QString& sInstrumentFile ) { bool bResult = false; QFile file(sInstrumentFile); - if (file.open(IO_ReadOnly)) { + if (file.open(QIODevice::ReadOnly)) { char achHeader[16]; - if (file.readBlock(achHeader, 16)) { + if (file.read(achHeader, 16) > 0) { bResult = (::memcmp(&achHeader[0], "RIFF", 4) == 0 && ::memcmp(&achHeader[8], "DLS LIST", 8) == 0); } @@ -814,18 +823,47 @@ } +// FIXME: Check whether a given file is an instrument file (SF2 only). +bool Channel::isSf2InstrumentFile ( const QString& sInstrumentFile ) +{ + bool bResult = false; + + QFile file(sInstrumentFile); + if (file.open(QIODevice::ReadOnly)) { + char achHeader[8]; + if (file.read(achHeader, 8) > 0) { + bResult = (::memcmp(&achHeader[0], "RIFF", 4) == 0 + && ::memcmp(&achHeader[4], "sfbk", 4) == 0); + } + file.close(); + } + + return bResult; +} + + // Retrieve the instrument list of a instrument file (.gig). -QStringList qsamplerChannel::getInstrumentList( const QString& sInstrumentFile, - bool bInstrumentNames ) +QStringList Channel::getInstrumentList( + const QString& sInstrumentFile, bool bInstrumentNames ) { - QString sInstrumentName = QFileInfo(sInstrumentFile).fileName(); QStringList instlist; - if (isInstrumentFile(sInstrumentFile)) { + const QFileInfo fi(sInstrumentFile); + if (!fi.exists()) { + instlist.append(noInstrumentName()); + return instlist; + } + #ifdef CONFIG_LIBGIG - if (bInstrumentNames) { - RIFF::File *pRiff = new RIFF::File(sInstrumentFile.latin1()); + if (bInstrumentNames) { + if (isDlsInstrumentFile(sInstrumentFile)) { + RIFF::File *pRiff + = new RIFF::File(sInstrumentFile.toUtf8().constData()); gig::File *pGig = new gig::File(pRiff); + #if HAVE_LIBGIG_SETAUTOLOAD + // prevent sleepy response time on large .gig files + pGig->SetAutoLoad(false); + #endif gig::Instrument *pInstrument = pGig->GetFirstInstrument(); while (pInstrument) { instlist.append((pInstrument->pInfo)->Name.c_str()); @@ -835,28 +873,57 @@ delete pRiff; } else + if (isSf2InstrumentFile(sInstrumentFile)) { + RIFF::File *pRiff + = new RIFF::File(sInstrumentFile.toUtf8().constData()); + sf2::File *pSf2 = new sf2::File(pRiff); + const int iPresetCount = pSf2->GetPresetCount(); + for (int iIndex = 0; iIndex < iPresetCount; ++iIndex) { + sf2::Preset *pPreset = pSf2->GetPreset(iIndex); + if (pPreset) { + instlist.append(pPreset->Name.c_str()); + } else { + instlist.append(fi.fileName() + + " [" + QString::number(iIndex) + "]"); + } + } + delete pSf2; + delete pRiff; + } + } #endif - for (int iInstrumentNr = 0; iInstrumentNr < QSAMPLER_INSTRUMENT_MAX; iInstrumentNr++) - instlist.append(sInstrumentName + " [" + QString::number(iInstrumentNr) + "]"); + + if (instlist.isEmpty()) { + for (int iIndex = 0; iIndex < QSAMPLER_INSTRUMENT_MAX; ++iIndex) { + instlist.append(fi.fileName() + + " [" + QString::number(iIndex) + "]"); + } } - else instlist.append(noInstrumentName()); return instlist; } // Retrieve the spacific instrument name of a instrument file (.gig), given its index. -QString qsamplerChannel::getInstrumentName( const QString& sInstrumentFile, - int iInstrumentNr, bool bInstrumentNames ) +QString Channel::getInstrumentName ( + const QString& sInstrumentFile, int iInstrumentNr, bool bInstrumentNames ) { + const QFileInfo fi(sInstrumentFile); + if (!fi.exists()) + return noInstrumentName(); + QString sInstrumentName; - if (isInstrumentFile(sInstrumentFile)) { - sInstrumentName = QFileInfo(sInstrumentFile).fileName(); #ifdef CONFIG_LIBGIG - if (bInstrumentNames) { - RIFF::File *pRiff = new RIFF::File(sInstrumentFile.latin1()); - gig::File *pGig = new gig::File(pRiff); + if (bInstrumentNames) { + if (isDlsInstrumentFile(sInstrumentFile)) { + RIFF::File *pRiff + = new RIFF::File(sInstrumentFile.toUtf8().constData()); + gig::File *pGig = new gig::File(pRiff); + #if HAVE_LIBGIG_SETAUTOLOAD + // prevent sleepy response time on large .gig files + pGig->SetAutoLoad(false); + #endif int iIndex = 0; gig::Instrument *pInstrument = pGig->GetFirstInstrument(); while (pInstrument) { @@ -871,275 +938,202 @@ delete pRiff; } else + if (isSf2InstrumentFile(sInstrumentFile)) { + RIFF::File *pRiff + = new RIFF::File(sInstrumentFile.toUtf8().constData()); + sf2::File *pSf2 = new sf2::File(pRiff); + sf2::Preset *pPreset = pSf2->GetPreset(iInstrumentNr); + if (pPreset) + sInstrumentName = pPreset->Name.c_str(); + delete pSf2; + delete pRiff; + } + } #endif + + if (sInstrumentName.isEmpty()) { + sInstrumentName = fi.fileName(); sInstrumentName += " [" + QString::number(iInstrumentNr) + "]"; } - else sInstrumentName = noInstrumentName(); return sInstrumentName; } // Common invalid name-helpers. -QString qsamplerChannel::noEngineName (void) +QString Channel::noEngineName (void) { return QObject::tr("(No engine)"); } -QString qsamplerChannel::noInstrumentName (void) +QString Channel::noInstrumentName (void) { return QObject::tr("(No instrument)"); } -QString qsamplerChannel::loadingInstrument (void) { +QString Channel::loadingInstrument (void) { return QObject::tr("(Loading instrument...)"); } - //------------------------------------------------------------------------- -// qsamplerChannelRoutingTable - Channel routing table. -// -#if 0 -// Constructor. -qsamplerChannelRoutingTable::qsamplerChannelRoutingTable ( - QWidget *pParent, const char *pszName ) - : QTable(pParent, pszName) -{ - // Set fixed number of columns. - QTable::setNumCols(2); - QTable::setShowGrid(false); - QTable::setSorting(false); - QTable::setFocusStyle(QTable::FollowStyle); - QTable::setSelectionMode(QTable::NoSelection); - // No vertical header. - QTable::verticalHeader()->hide(); - QTable::setLeftMargin(0); - // Initialize the fixed table column headings. - QHeader *pHeader = QTable::horizontalHeader(); - pHeader->setLabel(0, tr("Sampler Channel")); - pHeader->setLabel(1, tr("Device Channel")); - // Set read-onlyness of each column - QTable::setColumnReadOnly(0, true); -// QTable::setColumnReadOnly(1, false); -- of course not. - QTable::setColumnStretchable(1, true); -} +// QSampler::ChannelRoutingModel - data model for audio routing +// (used for QTableView) -// Default destructor. -qsamplerChannelRoutingTable::~qsamplerChannelRoutingTable (void) +ChannelRoutingModel::ChannelRoutingModel ( QObject *pParent ) + : QAbstractTableModel(pParent), m_pDevice(NULL) { } -// Routing map table renderer. -void qsamplerChannelRoutingTable::refresh ( qsamplerDevice *pDevice, - const qsamplerChannelRoutingMap& routing ) +int ChannelRoutingModel::rowCount ( const QModelIndex& /*parent*/) const { - if (pDevice == NULL) - return; - - // Always (re)start it empty. - QTable::setUpdatesEnabled(false); - QTable::setNumRows(0); - - // The common device port item list. - QStringList opts; - qsamplerDevicePortList& ports = pDevice->ports(); - qsamplerDevicePort *pPort; - for (pPort = ports.first(); pPort; pPort = ports.next()) { - opts.append(pDevice->deviceTypeName() - + ' ' + pDevice->driverName() - + ' ' + pPort->portName()); - } - - // Those items shall have a proper pixmap... - QPixmap pmChannel = QPixmap::fromMimeSource("qsamplerChannel.png"); - QPixmap pmDevice; - switch (pDevice->deviceType()) { - case qsamplerDevice::Audio: - pmDevice = QPixmap::fromMimeSource("audio2.png"); - break; - case qsamplerDevice::Midi: - pmDevice = QPixmap::fromMimeSource("midi2.png"); - break; - case qsamplerDevice::None: - break; - } - - // Fill the routing table... - QTable::insertRows(0, routing.count()); - int iRow = 0; - qsamplerChannelRoutingMap::ConstIterator iter; - for (iter = routing.begin(); iter != routing.end(); ++iter) { - QTable::setPixmap(iRow, 0, pmChannel); - QTable::setText(iRow, 0, pDevice->deviceTypeName() - + ' ' + QString::number(iter.key())); - qsamplerChannelRoutingComboBox *pComboItem = - new qsamplerChannelRoutingComboBox(this, opts, pmDevice); - pComboItem->setCurrentItem(iter.data()); - QTable::setItem(iRow, 1, pComboItem); - ++iRow; - } - - // Adjust optimal column widths. - QTable::adjustColumn(0); - QTable::adjustColumn(1); - - QTable::setUpdatesEnabled(true); - QTable::updateContents(); + return (m_pDevice) ? m_routing.size() : 0; } -// Commit any pending editing. -void qsamplerChannelRoutingTable::flush (void) +int ChannelRoutingModel::columnCount ( const QModelIndex& /*parent*/) const { - if (QTable::isEditing()) - QTable::endEdit(QTable::currEditRow(), QTable::currEditCol(), true, true); + return 1; } -#endif -ChannelRoutingModel::ChannelRoutingModel(QObject* parent) : QAbstractTableModel(parent), pDevice(NULL) { -} -int ChannelRoutingModel::rowCount(const QModelIndex& /*parent*/) const { - return routing.size(); -} - -int ChannelRoutingModel::columnCount(const QModelIndex& /*parent*/) const { - return 1; +Qt::ItemFlags ChannelRoutingModel::flags ( const QModelIndex& /*index*/) const +{ + return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled; } -QVariant ChannelRoutingModel::data(const QModelIndex &index, int role) const { - if (!index.isValid()) - return QVariant(); - if (role != Qt::DisplayRole) - return QVariant(); - ChannelRoutingItem item; - - // The common device port item list. - qsamplerDevicePortList& ports = pDevice->ports(); - qsamplerDevicePort* pPort; - for (pPort = ports.first(); pPort; pPort = ports.next()) { - item.options.append( - pDevice->deviceTypeName() - + ' ' + pDevice->driverName() - + ' ' + pPort->portName() - ); - } +bool ChannelRoutingModel::setData ( const QModelIndex& index, + const QVariant& value, int /*role*/) +{ + if (!index.isValid()) + return false; - item.selection = routing[index.column()]; + m_routing[index.row()] = value.toInt(); - return QVariant::fromValue(item); + emit dataChanged(index, index); + return true; } -QVariant ChannelRoutingModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (role != Qt::DisplayRole) return QVariant(); - - if (orientation == Qt::Horizontal) - return QObject::tr("Device Channel"); - if (orientation == Qt::Vertical) - return QObject::tr("Sampler Channel Output ") + - QString(section); - - return QVariant(); -} - -void ChannelRoutingModel::refresh ( qsamplerDevice *pDevice, - const qsamplerChannelRoutingMap& routing ) +QVariant ChannelRoutingModel::data ( const QModelIndex &index, int role ) const { - this->pDevice = pDevice; - this->routing = routing; -} + if (!index.isValid()) + return QVariant(); + if (role != Qt::DisplayRole) + return QVariant(); + if (index.column() != 0) + return QVariant(); + ChannelRoutingItem item; + // The common device port item list. + DevicePortList& ports = m_pDevice->ports(); + QListIterator iter(ports); + while (iter.hasNext()) { + DevicePort *pPort = iter.next(); + item.options.append( + m_pDevice->deviceTypeName() + + ' ' + m_pDevice->driverName() + + ' ' + pPort->portName() + ); + } + item.selection = m_routing[index.row()]; -ChannelRoutingDelegate::ChannelRoutingDelegate(QObject *parent) : QItemDelegate(parent) { + return QVariant::fromValue(item); } -QWidget* ChannelRoutingDelegate::createEditor(QWidget *parent, - const QStyleOptionViewItem &/* option */, - const QModelIndex& index) const -{ - ChannelRoutingItem item = index.model()->data(index, Qt::DisplayRole).value(); - QComboBox* editor = new QComboBox(parent); - editor->addItems(item.options); - editor->setCurrentIndex(item.selection); - editor->installEventFilter(const_cast(this)); - return editor; -} +QVariant ChannelRoutingModel::headerData ( int section, + Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); -void ChannelRoutingDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { - ChannelRoutingItem item = index.model()->data(index, Qt::DisplayRole).value(); - QComboBox* comboBox = static_cast(editor); - comboBox->setCurrentIndex(item.selection); + switch (orientation) { + case Qt::Horizontal: + return UNICODE_RIGHT_ARROW + QObject::tr(" Device Channel"); + case Qt::Vertical: + return QObject::tr("Audio Channel ") + + QString::number(section) + " " + UNICODE_RIGHT_ARROW; + default: + return QVariant(); + } } -void ChannelRoutingDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { - QComboBox* comboBox = static_cast(editor); - model->setData(index, comboBox->currentIndex()); -} -void ChannelRoutingDelegate::updateEditorGeometry(QWidget *editor, - const QStyleOptionViewItem &option, const QModelIndex &/* index */) const +void ChannelRoutingModel::refresh ( Device *pDevice, + const ChannelRoutingMap& routing ) { - editor->setGeometry(option.rect); + m_pDevice = pDevice; + m_routing = routing; + // inform the outer world (QTableView) that our data changed +#if QT_VERSION < 0x050000 + QAbstractTableModel::reset(); +#else + QAbstractTableModel::beginResetModel(); + QAbstractTableModel::endResetModel(); +#endif } - //------------------------------------------------------------------------- -// qsamplerChannelRoutingComboBox - Custom combo box for routing table. +// QSampler::ChannelRoutingDelegate - table cell renderer for audio routing // -#if 0 -// Constructor. -qsamplerChannelRoutingComboBox::qsamplerChannelRoutingComboBox ( - QTable *pTable, const QStringList& list, const QPixmap& pixmap ) - : QTableItem(pTable, QTableItem::WhenCurrent, QString::null, pixmap), - m_list(list) +ChannelRoutingDelegate::ChannelRoutingDelegate ( QObject *pParent ) + : QItemDelegate(pParent) { - m_iCurrentItem = 0; } -// Public accessors. -void qsamplerChannelRoutingComboBox::setCurrentItem ( int iCurrentItem ) + +QWidget* ChannelRoutingDelegate::createEditor ( QWidget *pParent, + const QStyleOptionViewItem & option, const QModelIndex& index ) const { - m_iCurrentItem = iCurrentItem; + if (!index.isValid()) + return NULL; + + if (index.column() != 0) + return NULL; - QTableItem::setText(m_list[iCurrentItem]); + ChannelRoutingItem item = index.model()->data(index, Qt::DisplayRole).value(); + + QComboBox* pComboBox = new QComboBox(pParent); + pComboBox->addItems(item.options); + pComboBox->setCurrentIndex(item.selection); + pComboBox->setEnabled(true); + pComboBox->setGeometry(option.rect); + return pComboBox; } -int qsamplerChannelRoutingComboBox::currentItem (void) const + +void ChannelRoutingDelegate::setEditorData ( QWidget *pEditor, + const QModelIndex &index) const { - return m_iCurrentItem; + ChannelRoutingItem item = index.model()->data(index, + Qt::DisplayRole).value (); + QComboBox* pComboBox = static_cast (pEditor); + pComboBox->setCurrentIndex(item.selection); } -// Virtual implemetations. -QWidget *qsamplerChannelRoutingComboBox::createEditor (void) const + +void ChannelRoutingDelegate::setModelData ( QWidget* pEditor, + QAbstractItemModel *pModel, const QModelIndex& index ) const { - QComboBox *pComboBox = new QComboBox(QTableItem::table()->viewport()); - QObject::connect(pComboBox, SIGNAL(activated(int)), - QTableItem::table(), SLOT(doValueChanged())); - for (QStringList::ConstIterator iter = m_list.begin(); - iter != m_list.end(); iter++) { - pComboBox->insertItem(QTableItem::pixmap(), *iter); - } - pComboBox->setCurrentItem(m_iCurrentItem); - return pComboBox; + QComboBox *pComboBox = static_cast (pEditor); + pModel->setData(index, pComboBox->currentIndex()); } -void qsamplerChannelRoutingComboBox::setContentFromEditor ( QWidget *pWidget ) + +void ChannelRoutingDelegate::updateEditorGeometry ( QWidget *pEditor, + const QStyleOptionViewItem& option, const QModelIndex &/* index */) const { - if (pWidget->inherits("QComboBox")) { - QComboBox *pComboBox = (QComboBox *) pWidget; - m_iCurrentItem = pComboBox->currentItem(); - QTableItem::setText(pComboBox->currentText()); - } - else QTableItem::setContentFromEditor(pWidget); + pEditor->setGeometry(option.rect); } -#endif +} // namespace QSampler + + +// end of qsamplerChannel.cpp