aboutsummaryrefslogtreecommitdiff
path: root/qmidiplayer-desktop/qmpdeviceprioritydialog.cpp
blob: bf358d832bc92af9f4581a1b763699e22f2387bd (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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?"});
}

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);
	model->removeRows(0,model->rowCount());
	for(auto dev:setdevs)
	{
		QStandardItem *e=new QStandardItem;
		e->setCheckable(true);
		e->setEditable(false);
		e->setCheckState(Qt::CheckState::Checked);
		QStandardItem *a=new QStandardItem;
		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->setText("Connected");
		a->setEditable(false);
		QStandardItem *devn=new QStandardItem(QString::fromStdString(dev));
		devn->setEditable(false);
		model->appendRow({e,devn,a});
	}
}

void qmpDevicePriorityDialog::load(void *data)
{
	setdevs=static_cast<QVariant*>(data)->toList();
	setupRegisteredDevices();
}

void *qmpDevicePriorityDialog::save()
{
	QList<QVariant> ret;
	for(int i=0;i<model->rowCount();++i)
		if(model->item(i,0)->checkState()==Qt::CheckState::Checked)
			ret.push_back(model->item(i,1)->text());
	return new QVariant(ret);
}

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()
{
	accept();
}

void qmpDevicePriorityDialog::on_buttonBox_rejected()
{
	reject();
}