--- qsampler/trunk/src/qsamplerChannel.cpp 2007/11/01 13:01:27 1463 +++ qsampler/trunk/src/qsamplerChannel.cpp 2007/11/26 18:24:38 1528 @@ -2,6 +2,7 @@ // /**************************************************************************** Copyright (C) 2004-2007, rncbc aka Rui Nuno Capela. All rights reserved. + Copyright (C) 2007, 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,15 +20,15 @@ *****************************************************************************/ -#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" @@ -35,6 +36,9 @@ #define QSAMPLER_INSTRUMENT_MAX 100 +#define UNICODE_RIGHT_ARROW QChar(char(0x92), char(0x21)) + + using namespace QSampler; //------------------------------------------------------------------------- @@ -58,7 +62,7 @@ m_iMidiMap = -1; m_sAudioDriver = "ALSA"; m_iAudioDevice = -1; - m_fVolume = 0.0; + m_fVolume = 0.0f; m_bMute = false; m_bSolo = false; } @@ -155,7 +159,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; } @@ -207,7 +212,8 @@ if ( ::lscp_load_instrument_non_modal( pMainForm->client(), - qsamplerUtilities::lscpEscapePath(sInstrumentFile).latin1(), + qsamplerUtilities::lscpEscapePath( + sInstrumentFile).toUtf8().constData(), iInstrumentNr, m_iChannelID ) != LSCP_OK ) { @@ -254,7 +260,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; } @@ -422,7 +429,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; } @@ -699,9 +707,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 +724,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.") ); @@ -801,9 +809,9 @@ 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); } @@ -824,8 +832,13 @@ if (isInstrumentFile(sInstrumentFile)) { #ifdef CONFIG_LIBGIG if (bInstrumentNames) { - RIFF::File *pRiff = new RIFF::File(sInstrumentFile.latin1()); + 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()); @@ -855,8 +868,13 @@ sInstrumentName = QFileInfo(sInstrumentFile).fileName(); #ifdef CONFIG_LIBGIG if (bInstrumentNames) { - RIFF::File *pRiff = new RIFF::File(sInstrumentFile.latin1()); - gig::File *pGig = new gig::File(pRiff); + 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) { @@ -896,250 +914,157 @@ } - //------------------------------------------------------------------------- -// qsamplerChannelRoutingTable - Channel routing table. +// ChannelRoutingModel - data model for audio routing (used for QTableView) // -#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); -} -// 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(":/icons/qsamplerChannel.png"); - QPixmap pmDevice; - switch (pDevice->deviceType()) { - case qsamplerDevice::Audio: - pmDevice = QPixmap(":/icons/audio2.png"); - break; - case qsamplerDevice::Midi: - pmDevice = QPixmap(":/icons/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_routing.size(); } -// 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. + qsamplerDevicePortList& ports = m_pDevice->ports(); + QListIterator iter(ports); + while (iter.hasNext()) { + qsamplerDevicePort *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("Sampler 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 ( qsamplerDevice *pDevice, + const qsamplerChannelRoutingMap& routing ) { - editor->setGeometry(option.rect); + m_pDevice = pDevice; + m_routing = routing; + // inform the outer world (QTableView) that our data changed + QAbstractTableModel::reset(); } - //------------------------------------------------------------------------- -// qsamplerChannelRoutingComboBox - Custom combo box for routing table. +// 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; + + ChannelRoutingItem item = index.model()->data(index, Qt::DisplayRole).value(); - QTableItem::setText(m_list[iCurrentItem]); + 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 + +// end of qsamplerChannel.cpp