summaryrefslogtreecommitdiff
path: root/muse_qt4_evolution/midiplugins/drumglue/globalinstrumentview.cpp
blob: 5ca81054ed39d27b0cf1dc099742976e1ea290e2 (plain)
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
//=========================================================
//  MusE
//  Linux Music Editor
//
//  (C) Copyright 2008 Robert Jonsson (rj@spamatica.se)
//  (C) Copyright 2005- Werner Schweer (ws@seh.de)
// Copyright: See COPYING file that comes with this distribution
//=========================================================

#include "globalinstrumentview.h"
#include "outputinstrumentview.h"
#include "drumglue.h"

//---------------------------------------------------------
//   GlobalInstrumentView
//---------------------------------------------------------

GlobalInstrumentView::GlobalInstrumentView(DrumGlue* f, QWidget* parent, QString name)
  : QWidget(parent)
    {
      setupUi(this);
      drumGlue = f;
      
      instrumentName=name;
      
      DrumInstrument *di =getCurrentOutputInstrument();
      printf("di->inKey=%d\n", di->inKey);
      if (di)
      		inKeySpinBox->setValue(di->inKey);
      
      connect (addOutputButton, SIGNAL(clicked()), this, SLOT(addOutput()));
      connect (editOutputButton, SIGNAL(clicked()), this, SLOT(editOutput()));
      connect (removeOutputButton, SIGNAL(clicked()), this, SLOT(removeOutput()));
      connect (inKeySpinBox, SIGNAL(valueChanged(int)), this, SLOT(updateInKey()));
      updateList();
    }


DrumInstrument *GlobalInstrumentView::getCurrentOutputInstrument()
{
	// get the global instrument belonging to this instance
	QList<DrumInstrument *>::iterator iter = drumGlue->drumInstruments.begin();
	while(iter != drumGlue->drumInstruments.end()) {
		printf("name = %s instrumentName= %s\n", (*iter)->name.toLatin1().data(), instrumentName.toLatin1().data());
		if ((*iter)->name == instrumentName) {
			break;
		}
		iter++;
	}
	if (iter == drumGlue->drumInstruments.end()) {
		printf("Reached the end without getting a hit\n");
		return NULL;
	}
	return *iter;
}

void GlobalInstrumentView::addOutput()
	{
	DrumInstrument *di = getCurrentOutputInstrument();
	if (!di)
		return;
	// create new output
	DrumOutputInstrument *doi = new DrumOutputInstrument;
	di->outputInstruments.append(doi);
	
	OutputInstrumentView *outputView = new OutputInstrumentView(doi,this);
	int res = outputView->exec();
	if (res == QDialog::Rejected) {
		// roll back
		di->outputInstruments.removeAll(doi);
		delete doi;
	}
	delete outputView;
	updateList();
	}

void GlobalInstrumentView::editOutput()
	{
	DrumInstrument *di = getCurrentOutputInstrument();
	
	int currIdx = outputListWidget->currentRow();
	if (currIdx < 0 || currIdx >= di->outputInstruments.size()) {
		printf("out of range!!\n");
		return;
	}
		
	DrumOutputInstrument *doi = di->outputInstruments[currIdx];
	
	DrumOutputInstrument doi_backup = *doi;
	OutputInstrumentView *outputView = new OutputInstrumentView(doi,this);
	int res = outputView->exec();
	if (res == QDialog::Rejected) {
		// roll back
		*doi = doi_backup;
	}
	delete outputView;
	updateList();
	}

void GlobalInstrumentView::removeOutput()
	{
 	int ret = QMessageBox::warning(this, tr("Remove output"),
		tr("Are you sure you want to remove current output?"),
		QMessageBox::No,
		QMessageBox::Yes);
	if (ret == QMessageBox::Yes) {
		DrumInstrument *di = getCurrentOutputInstrument();
		
		int currIdx = outputListWidget->currentRow();
		if (currIdx < 0 || currIdx >= di->outputInstruments.size()) {
			printf("out of range!!\n");
			return;
		}
			
		DrumOutputInstrument *doi = di->outputInstruments[currIdx];
		di->outputInstruments.removeAll(doi);
		delete doi;
	}		
	updateList();
	}
	
void GlobalInstrumentView::updateList()
{
	printf("updateList\n");
	outputListWidget->clear();
	
	QList<DrumInstrument *>::iterator iter = drumGlue->drumInstruments.begin();
	while(iter != drumGlue->drumInstruments.end()) {
		printf("name = %s instrumentName= %s\n", (*iter)->name.toLatin1().data(), instrumentName.toLatin1().data());
		if ((*iter)->name == instrumentName) {
			printf("updating current list\n");
			foreach (DrumOutputInstrument *doi, (*iter)->outputInstruments) {
				QListWidgetItem *outDrumItem = new QListWidgetItem(outputListWidget);
				QString str = QString("%1 %2 %3 %4 %5").arg(doi->outKey).arg(doi->lowestVelocity).arg(doi->highestVelocity).arg(doi->prefer).arg(doi->preferFast);
				printf("setting item to %s\n",str.toLatin1().data());
				outDrumItem->setText(str);
			}
		}
		iter++;
	}
}

void GlobalInstrumentView::updateInKey()
{
	QList<DrumInstrument *>::iterator iter = drumGlue->drumInstruments.begin();
	while(iter != drumGlue->drumInstruments.end()) {
		if ((*iter)->name == instrumentName) {
			(*iter)->inKey = inKeySpinBox->value();
		}
		iter++;
	}

}