From 3032c0021389b2a5211b4a6e5d1c2929ce307bf5 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Tue, 8 Oct 2019 11:18:27 +0800 Subject: Continue reducing usage of widgets in QTableWidget. --- midifmt-plugin/midifmtplugin.cpp | 2 +- qmidiplayer-desktop/qmpchannelswindow.cpp | 39 ++++++++++++------- qmidiplayer-desktop/qmpchannelswindow.hpp | 4 +- qmidiplayer-desktop/qmpdeviceprioritydialog.cpp | 2 - qmidiplayer-desktop/qmpdevpropdialog.cpp | 51 +++++++++++++------------ qmidiplayer-desktop/qmpdevpropdialog.ui | 6 +++ qmidiplayer-desktop/qmpsettingswindow.cpp | 13 ++++--- qmidiplayer-desktop/qmpsettingswindow.ui | 6 +++ simple-visualization/simplevisualization.cpp | 2 +- visualization/qmpvisualization.cpp | 2 +- 10 files changed, 78 insertions(+), 49 deletions(-) diff --git a/midifmt-plugin/midifmtplugin.cpp b/midifmt-plugin/midifmtplugin.cpp index fa9936d..fccb7cc 100644 --- a/midifmt-plugin/midifmtplugin.cpp +++ b/midifmt-plugin/midifmtplugin.cpp @@ -113,4 +113,4 @@ void qmpMidiFmtPlugin::deinit() const char* qmpMidiFmtPlugin::pluginGetName() {return "QMidiPlayer extra midi formats plugin";} const char* qmpMidiFmtPlugin::pluginGetVersion() -{return "0.8.6";} +{return "0.8.7";} diff --git a/qmidiplayer-desktop/qmpchannelswindow.cpp b/qmidiplayer-desktop/qmpchannelswindow.cpp index 029daff..fe0eeff 100644 --- a/qmidiplayer-desktop/qmpchannelswindow.cpp +++ b/qmidiplayer-desktop/qmpchannelswindow.cpp @@ -95,6 +95,20 @@ QVariant qmpChannelsModel::data(const QModelIndex&index,int role)const } return QVariant(); } +bool qmpChannelsModel::setData(const QModelIndex&index,const QVariant&value,int role) +{ + if(index.column()==3) + { + if(role!=Qt::ItemDataRole::DisplayRole)return false; + std::vector dsv=CMidiPlayer::getInstance()->getMidiOutDevices(); + int idx=std::find(dsv.begin(),dsv.end(),value.toString().toStdString())-dsv.begin(); + if(idx==CMidiPlayer::getInstance()->getChannelOutput(index.row()))return false; + CMidiPlayer::getInstance()->setChannelOutput(index.row(),idx); + emit dataChanged(index,index,{Qt::DisplayRole}); + return true; + } + return false; +} QVariant qmpChannelsModel::headerData(int section,Qt::Orientation orientation,int role)const { if(role!=Qt::ItemDataRole::DisplayRole)return QVariant(); @@ -152,8 +166,8 @@ void qmpChannelsModel::channelMSClearAll(int type) } } -qmpDeviceItemDelegate::qmpDeviceItemDelegate(QWidget*parent):QStyledItemDelegate(parent),par(parent) -{} +qmpDeviceItemDelegate::qmpDeviceItemDelegate(bool ignoreInternal,QWidget*parent): + QStyledItemDelegate(parent),par(parent),nofs(ignoreInternal){} void qmpDeviceItemDelegate::paint(QPainter*painter,const QStyleOptionViewItem&option,const QModelIndex&index)const { QStyleOptionViewItem opt; @@ -162,6 +176,7 @@ void qmpDeviceItemDelegate::paint(QPainter*painter,const QStyleOptionViewItem&op socb.currentText=opt.text; socb.editable=false; socb.rect=option.rect; + socb.state=opt.state; par->style()->drawComplexControl(QStyle::ComplexControl::CC_ComboBox,&socb,painter); par->style()->drawControl(QStyle::CE_ComboBoxLabel,&socb,painter); } @@ -182,6 +197,10 @@ QWidget* qmpDeviceItemDelegate::createEditor(QWidget*parent,const QStyleOptionVi Q_UNUSED(index) QComboBox *cb=new QComboBox(parent); cb->setEditable(false); + connect(cb,static_cast(&QComboBox::currentIndexChanged),this,[index,cb](int){ + const_cast(index.model())->setData(index,cb->currentText(),Qt::ItemDataRole::DisplayRole); + cb->hidePopup(); + }); return cb; } void qmpDeviceItemDelegate::setEditorData(QWidget*widget,const QModelIndex&index)const @@ -196,21 +215,15 @@ void qmpDeviceItemDelegate::setEditorData(QWidget*widget,const QModelIndex&index cb->clear(); std::vector devs=qmpMainWindow::getInstance()->getPlayer()->getMidiOutDevices(); for(auto s:devs) - cb->addItem(QString::fromStdString(s)); - cb->setCurrentIndex(qmpMainWindow::getInstance()->getPlayer()->getChannelOutput(index.row())); + if(!nofs||(nofs&&s!="Internal FluidSynth")) + cb->addItem(QString::fromStdString(s)); + cb->setCurrentText(index.data().toString()); cb->showPopup(); - connect(cb,static_cast(&QComboBox::currentIndexChanged),[this,index,cb](int id){ - QTableView*pv=qobject_cast(this->parent()); - qmpMainWindow::getInstance()->getPlayer()->setChannelOutput(index.row(),id); - emit pv->model()->dataChanged(index,index,{Qt::DisplayRole}); - cb->hidePopup(); - }); } void qmpDeviceItemDelegate::setModelData(QWidget*editor,QAbstractItemModel*model,const QModelIndex&index)const { QComboBox *cb=qobject_cast(editor); - qmpMainWindow::getInstance()->getPlayer()->setChannelOutput(index.row(),cb->currentIndex()); - emit model->dataChanged(index,index,{Qt::DisplayRole}); + model->setData(index,cb->currentText(),Qt::ItemDataRole::DisplayRole); } void qmpDeviceItemDelegate::updateEditorGeometry(QWidget*editor,const QStyleOptionViewItem&option,const QModelIndex&index)const { @@ -225,7 +238,7 @@ qmpChannelsWindow::qmpChannelsWindow(QWidget *parent) : ui->setupUi(this); ui->tvChannels->setHorizontalHeader(new QHeaderView(Qt::Orientation::Horizontal)); ui->tvChannels->setModel(chmodel=new qmpChannelsModel); - ui->tvChannels->setItemDelegateForColumn(3,new qmpDeviceItemDelegate(ui->tvChannels)); + ui->tvChannels->setItemDelegateForColumn(3,new qmpDeviceItemDelegate(false,ui->tvChannels)); ui->tvChannels->setAlternatingRowColors(true); ui->tvChannels->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection); ui->tvChannels->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents); diff --git a/qmidiplayer-desktop/qmpchannelswindow.hpp b/qmidiplayer-desktop/qmpchannelswindow.hpp index 93742a2..072f9d3 100644 --- a/qmidiplayer-desktop/qmpchannelswindow.hpp +++ b/qmidiplayer-desktop/qmpchannelswindow.hpp @@ -40,6 +40,7 @@ class qmpChannelsModel:public QAbstractTableModel int rowCount(const QModelIndex&parent=QModelIndex())const override; QModelIndex parent(const QModelIndex&child)const override; QVariant data(const QModelIndex&index,int role=Qt::ItemDataRole::DisplayRole)const override; + bool setData(const QModelIndex &index,const QVariant &value,int role=Qt::EditRole)override; QVariant headerData(int section,Qt::Orientation orientation,int role=Qt::ItemDataRole::DisplayRole)const override; Qt::ItemFlags flags(const QModelIndex&idx)const override; public slots: @@ -56,7 +57,7 @@ class qmpDeviceItemDelegate:public QStyledItemDelegate { Q_OBJECT public: - explicit qmpDeviceItemDelegate(QWidget*parent=nullptr); + explicit qmpDeviceItemDelegate(bool ignoreInternal=false,QWidget*parent=nullptr); void paint(QPainter*painter,const QStyleOptionViewItem&option,const QModelIndex&index)const override; QSize sizeHint(const QStyleOptionViewItem&option,const QModelIndex&index)const override; QWidget* createEditor(QWidget*parent,const QStyleOptionViewItem&option,const QModelIndex&index)const override; @@ -65,6 +66,7 @@ class qmpDeviceItemDelegate:public QStyledItemDelegate void updateEditorGeometry(QWidget*editor,const QStyleOptionViewItem&option,const QModelIndex&index)const override; private: QWidget *par; + bool nofs; }; class qmpChannelsWindow:public QWidget diff --git a/qmidiplayer-desktop/qmpdeviceprioritydialog.cpp b/qmidiplayer-desktop/qmpdeviceprioritydialog.cpp index a319fd1..d26fb45 100644 --- a/qmidiplayer-desktop/qmpdeviceprioritydialog.cpp +++ b/qmidiplayer-desktop/qmpdeviceprioritydialog.cpp @@ -37,7 +37,6 @@ void qmpDevicePriorityDialog::setupRegisteredDevices() e->setEditable(false); e->setCheckState(Qt::CheckState::Checked); QStandardItem *a=new QStandardItem; - a->setEnabled(false); a->setText(sconn.find(dev.toString().toStdString())!=sconn.end()?"Connected":"Disconnected"); a->setEditable(false); QStandardItem *devn=new QStandardItem(dev.toString()); @@ -53,7 +52,6 @@ void qmpDevicePriorityDialog::setupRegisteredDevices() e->setEditable(false); e->setCheckState(Qt::CheckState::Unchecked); QStandardItem *a=new QStandardItem; - a->setEnabled(false); a->setText("Connected"); a->setEditable(false); QStandardItem *devn=new QStandardItem(QString::fromStdString(dev)); diff --git a/qmidiplayer-desktop/qmpdevpropdialog.cpp b/qmidiplayer-desktop/qmpdevpropdialog.cpp index 150f54b..30f1568 100644 --- a/qmidiplayer-desktop/qmpdevpropdialog.cpp +++ b/qmidiplayer-desktop/qmpdevpropdialog.cpp @@ -3,10 +3,10 @@ #include #include #include -#include #include "qmpdevpropdialog.hpp" #include "qmpmainwindow.hpp" #include "qmpsettingswindow.hpp" +#include "qmpchannelswindow.hpp" #include "ui_qmpdevpropdialog.h" qmpDevPropDialog::qmpDevPropDialog(QWidget *parent) : @@ -14,6 +14,19 @@ qmpDevPropDialog::qmpDevPropDialog(QWidget *parent) : ui(new Ui::qmpDevPropDialog) { ui->setupUi(this); + ui->twProps->setItemDelegateForColumn(0,new qmpDeviceItemDelegate(true,ui->twProps)); + ui->twProps->setEditTriggers(QAbstractItemView::EditTrigger::NoEditTriggers); + connect(ui->twProps,&QTableWidget::cellClicked,[this](int r,int c){ + if(c!=0)return; + this->ui->twProps->edit(ui->twProps->model()->index(r,c)); + }); + connect(ui->twProps,&QTableWidget::cellChanged,this,[this](int r,int c){ + if(c!=0)return; + QString connst("Disconnected"); + for(auto&ds:qmpMainWindow::getInstance()->getPlayer()->getMidiOutDevices()) + if(ui->twProps->item(r,c)->text()==QString::fromStdString(ds)){connst="Connected";break;} + ui->twProps->item(r,1)->setText(connst); + }); } qmpDevPropDialog::~qmpDevPropDialog() @@ -48,10 +61,10 @@ void qmpDevPropDialog::setupRow(const QString&dn,const QString&din) int r; ui->twProps->insertRow(r=ui->twProps->rowCount()); ui->twProps->setRowHeight(r,32); - QComboBox *cb; - ui->twProps->setCellWidget(r,0,cb=new QComboBox); - for(auto&s:qmpMainWindow::getInstance()->getPlayer()->getMidiOutDevices()) - if(s!="Internal FluidSynth")cb->addItem(QString::fromStdString(s)); + QTableWidgetItem *cbx=new QTableWidgetItem; + ui->twProps->setItem(r,1,cbx); + QTableWidgetItem *cb; + ui->twProps->setItem(r,0,cb=new QTableWidgetItem); QWidget *fw=new QWidget; QLabel *lb;QPushButton *pb; fw->setLayout(new QHBoxLayout); @@ -61,29 +74,19 @@ void qmpDevPropDialog::setupRow(const QString&dn,const QString&din) fw->layout()->addWidget(pb=new QPushButton); pb->setText("..."); pb->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred); - QCheckBox *cbx; - ui->twProps->setCellWidget(r,1,cbx=new QCheckBox); - cbx->setEnabled(false); - cbx->setChecked(false); + cbx->setFlags(Qt::ItemFlag::ItemIsEnabled|Qt::ItemFlag::ItemIsSelectable); + cbx->setText("Disconnected"); ui->twProps->setCellWidget(r,2,fw); ui->twProps->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents); - connect(cb,&QComboBox::currentTextChanged,this,[this,r](const QString&s){ - bool conn=false; - for(auto&ds:qmpMainWindow::getInstance()->getPlayer()->getMidiOutDevices()) - if(s==QString::fromStdString(ds)){conn=true;break;} - ((QCheckBox*)ui->twProps->cellWidget(r,1))->setChecked(conn); - }); if(dn.length()) { - cb->setCurrentText(dn); - if(cb->currentText()!=dn) - { - cb->setEditable(true); - cb->setCurrentText(dn); - cb->setEnabled(false); - } + cb->setText(dn); + cb->setFlags(cb->flags()|Qt::ItemFlag::ItemIsEditable); + std::vector dsv=CMidiPlayer::getInstance()->getMidiOutDevices(); + if(std::find(dsv.begin(),dsv.end(),dn.toStdString())==dsv.end()) + cb->setFlags(cb->flags()&(~Qt::ItemFlag::ItemIsEnabled)); + else cb->setFlags(cb->flags()|Qt::ItemFlag::ItemIsEnabled); } - emit cb->currentTextChanged(cb->currentText()); connect(pb,&QPushButton::clicked,this,[this,lb,fw]{ lb->setText(QFileDialog::getOpenFileUrl(this,"Select Device Initialization File",QUrl()).path()); fw->setProperty("fn",lb->text()); @@ -97,7 +100,7 @@ void qmpDevPropDialog::on_buttonBox_accepted() s->remove(""); for(int i=0;itwProps->rowCount();++i) { - s->setValue(((QComboBox*)ui->twProps->cellWidget(i,0))->currentText(), + s->setValue(ui->twProps->item(i,0)->text(), ui->twProps->cellWidget(i,2)->property("fn").toString()); } s->endGroup(); diff --git a/qmidiplayer-desktop/qmpdevpropdialog.ui b/qmidiplayer-desktop/qmpdevpropdialog.ui index 6cea5c0..641a511 100644 --- a/qmidiplayer-desktop/qmpdevpropdialog.ui +++ b/qmidiplayer-desktop/qmpdevpropdialog.ui @@ -16,6 +16,12 @@ + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + Device diff --git a/qmidiplayer-desktop/qmpsettingswindow.cpp b/qmidiplayer-desktop/qmpsettingswindow.cpp index 3aef529..ee08fb9 100644 --- a/qmidiplayer-desktop/qmpsettingswindow.cpp +++ b/qmidiplayer-desktop/qmpsettingswindow.cpp @@ -288,7 +288,7 @@ void qmpSettingsWindow::settingsUpdate() for(int i=0;itwPluginList->rowCount();++i) settings->setValue( QString("PluginSwitch/")+ui->twPluginList->item(i,1)->text(), - ((QCheckBox*)ui->twPluginList->cellWidget(i,0))->isChecked()?1:0); + ui->twPluginList->item(i,0)->checkState()==Qt::CheckState::Checked?1:0); updateCustomOptions(); settings->sync(); } @@ -360,11 +360,12 @@ void qmpSettingsWindow::updatePluginList(qmpPluginManager *pmgr) for(unsigned i=0;isize();++i) { ui->twPluginList->insertRow(i); - ui->twPluginList->setCellWidget(i,0,new QCheckBox("")); - if(settings->value(QString("PluginSwitch/")+QString(plugins->at(i).name.c_str()),1).toInt()) - {((QCheckBox*)ui->twPluginList->cellWidget(i,0))->setChecked(true);plugins->at(i).enabled=true;} - else - {((QCheckBox*)ui->twPluginList->cellWidget(i,0))->setChecked(false);plugins->at(i).enabled=false;} + QTableWidgetItem *icb; + ui->twPluginList->setItem(i,0,icb=new QTableWidgetItem()); + bool enabled=settings->value(QString("PluginSwitch/")+QString(plugins->at(i).name.c_str()),1).toInt(); + icb->setCheckState(enabled?Qt::CheckState::Checked:Qt::CheckState::Unchecked); + icb->setFlags(Qt::ItemFlag::ItemIsEnabled|Qt::ItemFlag::ItemIsSelectable|Qt::ItemFlag::ItemIsUserCheckable); + plugins->at(i).enabled=enabled; ui->twPluginList->setItem(i,1,new QTableWidgetItem(plugins->at(i).name.c_str())); ui->twPluginList->setItem(i,2,new QTableWidgetItem(plugins->at(i).version.c_str())); ui->twPluginList->setItem(i,3,new QTableWidgetItem(plugins->at(i).path.c_str())); diff --git a/qmidiplayer-desktop/qmpsettingswindow.ui b/qmidiplayer-desktop/qmpsettingswindow.ui index c773958..6cdd665 100644 --- a/qmidiplayer-desktop/qmpsettingswindow.ui +++ b/qmidiplayer-desktop/qmpsettingswindow.ui @@ -730,6 +730,12 @@ + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + E diff --git a/simple-visualization/simplevisualization.cpp b/simple-visualization/simplevisualization.cpp index e4c0c83..d24db67 100644 --- a/simple-visualization/simplevisualization.cpp +++ b/simple-visualization/simplevisualization.cpp @@ -20,4 +20,4 @@ void qmpSimpleVisualization::deinit() const char* qmpSimpleVisualization::pluginGetName() {return "QMidiPlayer Simple Visualization Plugin";} const char* qmpSimpleVisualization::pluginGetVersion() -{return "0.8.6";} +{return "0.8.7";} diff --git a/visualization/qmpvisualization.cpp b/visualization/qmpvisualization.cpp index 6d4acf4..c72cbf5 100644 --- a/visualization/qmpvisualization.cpp +++ b/visualization/qmpvisualization.cpp @@ -845,7 +845,7 @@ void qmpVisualization::deinit() const char* qmpVisualization::pluginGetName() {return "QMidiPlayer Default Visualization Plugin";} const char* qmpVisualization::pluginGetVersion() -{return "0.8.6";} +{return "0.8.7";} void qmpVisualization::pushNoteOn(uint32_t tc,uint32_t ch,uint32_t key,uint32_t vel) { -- cgit v1.2.3