aboutsummaryrefslogtreecommitdiff
path: root/qmidiplayer-desktop/qmpdeviceprioritydialog.cpp
blob: a319fd1e31973838e389a7f53f3b86b4d310b3ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <set>
#include <string>
#include "qmpdeviceprioritydialog.hpp"
#include "qmpsettingswindow.hpp"
#include "../core/qmpmidiplay.hpp"
#include "ui_qmpdeviceprioritydialog.h"

qmpDevicePriorityDialog::qmpDevicePriorityDialog(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::qmpDevicePriorityDialog)
{
	ui->setupUi(this);
	model=new QStandardItemModel(this);
	ui->tvDevices->setModel(model);
	ui->tvDevices->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
	ui->tvDevices->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents);
	ui->tvDevices->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
	model->setHorizontalHeaderLabels({"E","Device","Connected?"});
	setdevs=qmpSettingsWindow::getSettingsIntf()->value("Midi/DevicePriority",QList<QVariant>{"Internal FluidSynth"}).toList();
}

qmpDevicePriorityDialog::~qmpDevicePriorityDialog()
{
	delete ui;
}

void qmpDevicePriorityDialog::setupRegisteredDevices()
{
	std::set<std::string> sset,sconn;
	auto conndevs=CMidiPlayer::getInstance()->getMidiOutDevices();
	for(auto dev:conndevs)
		sconn.insert(dev);
	for(auto dev:setdevs)
	{
		QStandardItem *e=new QStandardItem;
		e->setCheckable(true);
		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());
		devn->setEditable(false);
		model->appendRow({e,devn,a});
		sset.insert(dev.toString().toStdString());
	}
	for(auto dev:conndevs)
	{
		if(sset.find(dev)!=sset.end())continue;
		QStandardItem *e=new QStandardItem;
		e->setCheckable(true);
		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));
		devn->setEditable(false);
		model->appendRow({e,devn,a});
	}
}

void qmpDevicePriorityDialog::on_pbUp_clicked()
{
	const QModelIndex &idx=ui->tvDevices->selectionModel()->currentIndex();
	if(idx.isValid()&&idx.row()>0)
	{
		int row=idx.row();
		auto r=model->takeRow(row);
		model->insertRow(row-1,r);
		ui->tvDevices->clearSelection();
		ui->tvDevices->selectionModel()->setCurrentIndex(model->index(row-1,idx.column()),QItemSelectionModel::Rows|QItemSelectionModel::Select);
	}
}

void qmpDevicePriorityDialog::on_pbDown_clicked()
{
	const QModelIndex &idx=ui->tvDevices->selectionModel()->currentIndex();
	if(idx.isValid()&&idx.row()<model->rowCount()-1)
	{
		int row=idx.row();
		auto r=model->takeRow(row);
		model->insertRow(row+1,r);
		ui->tvDevices->clearSelection();
		ui->tvDevices->selectionModel()->setCurrentIndex(model->index(row+1,idx.column()),QItemSelectionModel::Rows|QItemSelectionModel::Select);
	}
}

void qmpDevicePriorityDialog::on_buttonBox_accepted()
{
	QList<QVariant> setdevs;
	for(int i=0;i<model->rowCount();++i)
		if(model->item(i,0)->checkState()==Qt::CheckState::Checked)
			setdevs.push_back(model->item(i,1)->text());
	qmpSettingsWindow::getSettingsIntf()->setValue("Midi/DevicePriority",setdevs);
}