aboutsummaryrefslogtreecommitdiff
path: root/qmidiplayer-desktop/qmpchannelswindow.cpp
blob: 41f545e9901ab4104d6a6cfbcb36cd29c3a2f6f8 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <cstdio>
#include <functional>
#include <set>
#include <QCheckBox>
#include <QPushButton>
#include <QComboBox>
#include <QTimer>
#include "qmpchannelswindow.hpp"
#include "ui_qmpchannelswindow.h"
#include "qmpmainwindow.hpp"

qmpChannelsModel::qmpChannelsModel(QObject*parent):QAbstractTableModel(parent)
{
	evh=qmpMainWindow::getInstance()->getPlayer()->registerEventHandler(
		[this](const void* _e,void*){
			if(!updatequeued)
			{
				updatequeued=true;
				const SEvent *e=(const SEvent*)(_e);
				if((e->p1&0xF0)==0xC0)
					emit dataChanged(index(e->p1&0xF0,4),index(e->p1&0xF0,4),{Qt::ItemDataRole::DisplayRole});
				QMetaObject::invokeMethod(this, &qmpChannelsModel::updateChannelActivity, Qt::ConnectionType::QueuedConnection);
			}
		}
	,nullptr,false);
	QTimer*t=new QTimer(this);
	t->setInterval(500);
	t->setSingleShot(false);
	connect(t,&QTimer::timeout,[this](){emit this->dataChanged(this->index(0,4),this->index(15,4),{Qt::ItemDataRole::DisplayRole});});
	memset(mute,0,sizeof(mute));
	memset(solo,0,sizeof(solo));
}
int qmpChannelsModel::columnCount(const QModelIndex&parent)const
{return parent.isValid()?0:6;}
int qmpChannelsModel::rowCount(const QModelIndex&parent)const
{return parent.isValid()?0:16;}
QModelIndex qmpChannelsModel::parent(const QModelIndex&child)const
{
	Q_UNUSED(child)
	return QModelIndex();
}
QVariant qmpChannelsModel::data(const QModelIndex&index,int role)const
{
	switch(index.column())
	{
		case 0:
			if(role==Qt::ItemDataRole::DecorationRole)
			{
				using namespace std::chrono_literals;
				bool lit=(std::chrono::system_clock::now()-qmpMainWindow::getInstance()->getPlayer()->getLastEventTS()[index.row()])<50ms;
				return lit?QIcon(":/img/ledon.svg"):QIcon(":/img/ledoff.svg");
			}
		break;
		case 1:
			if(role==Qt::ItemDataRole::CheckStateRole)
				return mute[index.row()]?Qt::CheckState::Checked:Qt::CheckState::Unchecked;
		break;
		case 2:
			if(role==Qt::ItemDataRole::CheckStateRole)
				return solo[index.row()]?Qt::CheckState::Checked:Qt::CheckState::Unchecked;
		break;
		case 3:
			if(role==Qt::ItemDataRole::DisplayRole)
			{
				std::vector<std::string> devs=qmpMainWindow::getInstance()->getPlayer()->getMidiOutDevices();
				return QString::fromStdString(devs[qmpMainWindow::getInstance()->getPlayer()->getChannelOutput(index.row())]);
			}
		break;
		case 4:
		{
			if(role==Qt::ItemDataRole::DisplayRole)
			{
				int ch=index.row();
				uint16_t b;uint8_t p;
				std::string nm;
				char data[256];
				CMidiPlayer *plyr=qmpMainWindow::getInstance()->getPlayer();
				bool r=plyr->getChannelOutputDevice(ch)->getChannelPreset(ch,&b,&p,nm);
				sprintf(data,"%03d:%03d %s",b,p,nm.c_str());
				if(!r)
				{
					nm=plyr->getChannelOutputDevice(ch)->getPresetName(plyr->getCC(ch,0)<<7|plyr->getCC(ch,32),plyr->getCC(ch,128));
					sprintf(data,"%03d:%03d:%03d %s",plyr->getCC(ch,0),plyr->getCC(ch,32),plyr->getCC(ch,128),nm.c_str());
				}
				return QString(data);
			}
		}
		break;
		case 5:
			if(role==Qt::ItemDataRole::DisplayRole)
				return "...";
			if(role==Qt::ItemDataRole::TextAlignmentRole)
				return Qt::AlignmentFlag::AlignCenter;
		break;
	}
	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<std::string> 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();
	if(orientation==Qt::Orientation::Vertical)
		return section+1;
	switch(section)
	{
		case 0:return QString("A");
		case 1:return QString("M");
		case 2:return QString("S");
		case 3:return QString("Device");
		case 4:return QString("Preset");
		case 5:return QString("...");
	}
	return QString();
}
Qt::ItemFlags qmpChannelsModel::flags(const QModelIndex&idx)const
{
	Qt::ItemFlags ret=Qt::ItemFlag::ItemIsEnabled|Qt::ItemFlag::ItemIsSelectable;
	if(idx.column()==1||idx.column()==2)
		ret|=Qt::ItemFlag::ItemIsUserCheckable;
	if(idx.column()==3)
		ret|=Qt::ItemFlag::ItemIsEditable;
	return ret;
}
void qmpChannelsModel::updateChannelActivity()
{
	emit dataChanged(index(0,0),index(15,0),{Qt::ItemDataRole::DecorationRole});
	updatequeued=false;
}
void qmpChannelsModel::channelMSClicked(const QModelIndex&idx)
{
	bool*x[3]={nullptr,mute,solo};
	if(x[idx.column()][idx.row()]^=1)
		x[3-idx.column()][idx.row()]=0;
	qmpMainWindow::getInstance()->getPlayer()->setMute(idx.row(),mute[idx.row()]);
	qmpMainWindow::getInstance()->getPlayer()->setSolo(idx.row(),solo[idx.row()]);
	emit dataChanged(index(idx.row(),1),index(idx.row(),2),{Qt::ItemDataRole::CheckStateRole});
}
void qmpChannelsModel::channelMSClearAll(int type)
{
	if(type==1)
	{
		memset(mute,0,sizeof(mute));
		for(int i=0;i<16;++i)
			qmpMainWindow::getInstance()->getPlayer()->setMute(i,0);
		emit dataChanged(index(0,1),index(15,1),{Qt::ItemDataRole::CheckStateRole});
	}
	if(type==2)
	{
		memset(solo,0,sizeof(solo));
		for(int i=0;i<16;++i)
			qmpMainWindow::getInstance()->getPlayer()->setSolo(i,0);
		emit dataChanged(index(0,2),index(15,2),{Qt::ItemDataRole::CheckStateRole});
	}
}

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;
	initStyleOption(&opt,index);
	QStyleOptionComboBox socb;
	socb.currentText=opt.text;
	socb.editable=false;
	socb.rect=option.rect;
	socb.state=opt.state;
	par->style()->drawComplexControl(QStyle::ComplexControl::CC_ComboBox,&socb,painter,option.widget);
	par->style()->drawControl(QStyle::CE_ComboBoxLabel,&socb,painter,option.widget);
}
QSize qmpDeviceItemDelegate::sizeHint(const QStyleOptionViewItem&option,const QModelIndex&index)const
{
	QStyleOptionViewItem opt;
	initStyleOption(&opt,index);
	QStyleOptionComboBox socb;
	socb.currentText=opt.text;
	socb.editable=false;
	socb.rect=option.rect;
	QSize sz=par->fontMetrics().size(Qt::TextFlag::TextSingleLine,socb.currentText);
	return par->style()->sizeFromContents(QStyle::ContentsType::CT_ComboBox,&socb,sz,option.widget);
}
QWidget* qmpDeviceItemDelegate::createEditor(QWidget*parent,const QStyleOptionViewItem&option,const QModelIndex&index)const
{
	Q_UNUSED(option)
	Q_UNUSED(index)
	QComboBox *cb=new QComboBox(parent);
	cb->setEditable(false);
	connect(cb,static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this,[index,cb](int){
		const_cast<QAbstractItemModel*>(index.model())->setData(index,cb->currentText(),Qt::ItemDataRole::DisplayRole);
		cb->hidePopup();
	});
	return cb;
}
void qmpDeviceItemDelegate::setEditorData(QWidget*widget,const QModelIndex&index)const
{
	/*
	 * We want to quit editing as soon as the popup of the combobox is closed.
	 * Unfortunately QTableView does not do that. And I don't feel like sub-classing
	 * it. So here are some dirty tricks to make it work that way.
	 */
	QComboBox *cb=qobject_cast<QComboBox*>(widget);
	QSignalBlocker sblk(cb);
	cb->clear();
	std::vector<std::string> devs=qmpMainWindow::getInstance()->getPlayer()->getMidiOutDevices();
	for(auto s:devs)
		if(!nofs||(nofs&&s!="Internal FluidSynth"))
			cb->addItem(QString::fromStdString(s));
	cb->setCurrentText(index.data().toString());
	cb->showPopup();
}
void qmpDeviceItemDelegate::setModelData(QWidget*editor,QAbstractItemModel*model,const QModelIndex&index)const
{
	QComboBox *cb=qobject_cast<QComboBox*>(editor);
	model->setData(index,cb->currentText(),Qt::ItemDataRole::DisplayRole);
}
void qmpDeviceItemDelegate::updateEditorGeometry(QWidget*editor,const QStyleOptionViewItem&option,const QModelIndex&index)const
{
	Q_UNUSED(index)
	editor->setGeometry(option.rect);
}

qmpChannelsWindow::qmpChannelsWindow(QWidget *parent) :
	QWidget(parent,Qt::Dialog),
	ui(new Ui::qmpChannelsWindow)
{
	ui->setupUi(this);
	ui->tvChannels->setHorizontalHeader(new QHeaderView(Qt::Orientation::Horizontal));
	ui->tvChannels->setModel(chmodel=new qmpChannelsModel);
	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);
	ui->tvChannels->horizontalHeader()->setSectionResizeMode(4, QHeaderView::ResizeMode::Stretch);
	connect(ui->tvChannels,&QTableView::clicked,[this](const QModelIndex&idx){
		if(idx.column()==1||idx.column()==2)
			this->chmodel->channelMSClicked(idx);
		if(idx.column()==3)
			this->ui->tvChannels->edit(idx);
		if(idx.column()==5)
			this->showChannelEditorWindow(idx.row());
	});
	connect(ui->tvChannels,&QTableView::activated,[this](const QModelIndex&idx){
		if(idx.column()==4)
		{
			pselectw->show();
			pselectw->setupWindow(idx.row());
		}
	});
	pselectw=new qmpPresetSelector(this);
	ceditw=new qmpChannelEditor(this);
	cha=new QIcon(":/img/ledon.svg");chi=new QIcon(":/img/ledoff.svg");
	eh=qmpMainWindow::getInstance()->getPlayer()->registerEventHandler(
		[this](const void *ee,void*){
			const SEvent *e=(const SEvent*)ee;
			if((e->type&0xF0)==0x90&&e->p2>0&&(e->flags&0x01))
				emit this->noteOn();
		}
	,nullptr,false);
	std::vector<std::string> devs=qmpMainWindow::getInstance()->getPlayer()->getMidiOutDevices();
	size_t devc=devs.size();
	std::set<std::string> devset;
	for(auto dev:devs)devset.insert(dev);
	std::string selecteddev;
	for(auto setdev:qmpSettingsWindow::getSettingsIntf()->value("Midi/DevicePriority",QList<QVariant>{"Internal FluidSynth"}).toList())
		if(devset.find(setdev.toString().toStdString())!=devset.end())
		{
			selecteddev=setdev.toString().toStdString();
			break;
		}
	for(int ch=0;ch<16;++ch)
	{
		for(size_t j=0;j<devc;++j)
		{
			if(selecteddev==devs[j])
				qmpMainWindow::getInstance()->getPlayer()->setChannelOutput(ch,j);
		}
	}
	qmpMainWindow::getInstance()->registerFunctionality(
		chnlf=new qmpChannelFunc(this),
		std::string("Channel"),
		tr("Channel").toStdString(),
		getThemedIconc(":/img/channel.svg"),
		0,
		true
	);
	if(qmpSettingsWindow::getSettingsIntf()->value("DialogStatus/ChnlW",QRect(-999,-999,999,999)).toRect()!=QRect(-999,-999,999,999))
		setGeometry(qmpSettingsWindow::getSettingsIntf()->value("DialogStatus/ChnlW",QRect(-999,-999,999,999)).toRect());
	if(qmpSettingsWindow::getSettingsIntf()->value("DialogStatus/ChnlWShown",0).toInt())
	{show();qmpMainWindow::getInstance()->setFuncState("Channel",true);}
}

void qmpChannelsWindow::showEvent(QShowEvent *event)
{
	if(qmpSettingsWindow::getSettingsIntf()->value("Behavior/DialogStatus","").toInt())
	{
		qmpSettingsWindow::getSettingsIntf()->setValue("DialogStatus/ChnlWShown",1);
	}
	if(qmpSettingsWindow::getSettingsIntf()->value("DialogStatus/ChnlW",QRect(-999,-999,999,999)).toRect()!=QRect(-999,-999,999,999))
		setGeometry(qmpSettingsWindow::getSettingsIntf()->value("DialogStatus/ChnlW",QRect(-999,-999,999,999)).toRect());
	event->accept();
}

void qmpChannelsWindow::closeEvent(QCloseEvent *event)
{
	if(qmpSettingsWindow::getSettingsIntf()->value("Behavior/DialogStatus","").toInt())
	{
		qmpSettingsWindow::getSettingsIntf()->setValue("DialogStatus/ChnlW",geometry());
	}
	setVisible(false);
	if(!qmpMainWindow::getInstance()->isFinalizing()&&qmpSettingsWindow::getSettingsIntf()->value("Behavior/DialogStatus","").toInt())
	{
		qmpSettingsWindow::getSettingsIntf()->setValue("DialogStatus/ChnlWShown",0);
	}
	qmpMainWindow::getInstance()->setFuncState("Channel",false);
	event->accept();
}

qmpChannelsWindow::~qmpChannelsWindow()
{
	qmpMainWindow::getInstance()->unregisterFunctionality("Channel");
	qmpMainWindow::getInstance()->getPlayer()->unregisterEventHandler(eh);
	delete chnlf;
	delete chi;delete cha;
	delete ui;
}

void qmpChannelsWindow::on_pbUnmute_clicked()
{
	chmodel->channelMSClearAll(1);
}

void qmpChannelsWindow::on_pbUnsolo_clicked()
{
	chmodel->channelMSClearAll(2);
}

void qmpChannelsWindow::showChannelEditorWindow(int chid)
{
	ceditw->show();
	ceditw->setupWindow(chid);
}

qmpChannelFunc::qmpChannelFunc(qmpChannelsWindow *par)
{p=par;}
void qmpChannelFunc::show()
{p->show();}
void qmpChannelFunc::close()
{p->close();}