1 |
// qsamplerInstrumentListForm.cpp |
// qsamplerInstrumentListForm.cpp |
2 |
// |
// |
3 |
/**************************************************************************** |
/**************************************************************************** |
4 |
Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved. |
Copyright (C) 2003-2010, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
Copyright (C) 2007, Christian Schoenebeck |
Copyright (C) 2007, Christian Schoenebeck |
6 |
|
|
7 |
This program is free software; you can redistribute it and/or |
This program is free software; you can redistribute it and/or |
20 |
|
|
21 |
*****************************************************************************/ |
*****************************************************************************/ |
22 |
|
|
23 |
|
#include "qsamplerAbout.h" |
24 |
#include "qsamplerInstrumentListForm.h" |
#include "qsamplerInstrumentListForm.h" |
25 |
|
|
26 |
|
#include "qsamplerInstrumentList.h" |
27 |
|
|
28 |
#include "qsamplerInstrumentForm.h" |
#include "qsamplerInstrumentForm.h" |
29 |
#include "qsamplerMainForm.h" |
|
30 |
#include "qsamplerOptions.h" |
#include "qsamplerOptions.h" |
31 |
#include "qsamplerInstrument.h" |
#include "qsamplerInstrument.h" |
32 |
|
#include "qsamplerMainForm.h" |
33 |
|
|
34 |
|
#include <QHeaderView> |
35 |
|
#include <QMessageBox> |
36 |
|
|
37 |
|
|
38 |
namespace QSampler { |
namespace QSampler { |
39 |
|
|
40 |
|
//------------------------------------------------------------------------- |
41 |
|
// QSampler::InstrumentListForm -- Instrument map list form implementation. |
42 |
|
// |
43 |
|
|
44 |
InstrumentListForm::InstrumentListForm ( |
InstrumentListForm::InstrumentListForm ( |
45 |
QWidget* pParent, Qt::WindowFlags wflags ) |
QWidget* pParent, Qt::WindowFlags wflags ) |
46 |
: QMainWindow(pParent, wflags) |
: QMainWindow(pParent, wflags) |
47 |
{ |
{ |
48 |
ui.setupUi(this); |
m_ui.setupUi(this); |
|
|
|
|
// Setup toolbar widgets. |
|
|
m_pMapComboBox = new QComboBox(ui.InstrumentToolbar); |
|
|
m_pMapComboBox->setMinimumWidth(120); |
|
|
m_pMapComboBox->setEnabled(false); |
|
|
m_pMapComboBox->setToolTip(tr("Instrument Map")); |
|
|
ui.InstrumentToolbar->addWidget(m_pMapComboBox); |
|
|
|
|
|
ui.InstrumentToolbar->addSeparator(); |
|
|
ui.InstrumentToolbar->addAction(ui.newInstrumentAction); |
|
|
ui.InstrumentToolbar->addAction(ui.editInstrumentAction); |
|
|
ui.InstrumentToolbar->addAction(ui.deleteInstrumentAction); |
|
|
ui.InstrumentToolbar->addSeparator(); |
|
|
ui.InstrumentToolbar->addAction(ui.refreshInstrumentsAction); |
|
49 |
|
|
50 |
ui.InstrumentTable->setModel(&model); |
// Setup toolbar widgets. |
51 |
ui.InstrumentTable->setItemDelegate(&delegate); |
m_pMapComboBox = new QComboBox(m_ui.instrumentToolbar); |
52 |
|
m_pMapComboBox->setMinimumWidth(120); |
53 |
|
m_pMapComboBox->setEnabled(false); |
54 |
|
m_pMapComboBox->setToolTip(tr("Instrument Map")); |
55 |
|
m_ui.instrumentToolbar->addWidget(m_pMapComboBox); |
56 |
|
|
57 |
|
m_ui.instrumentToolbar->addSeparator(); |
58 |
|
m_ui.instrumentToolbar->addAction(m_ui.newInstrumentAction); |
59 |
|
m_ui.instrumentToolbar->addAction(m_ui.editInstrumentAction); |
60 |
|
m_ui.instrumentToolbar->addAction(m_ui.deleteInstrumentAction); |
61 |
|
m_ui.instrumentToolbar->addSeparator(); |
62 |
|
m_ui.instrumentToolbar->addAction(m_ui.refreshInstrumentsAction); |
63 |
|
|
64 |
|
m_pInstrumentListView = new InstrumentListView(this); |
65 |
|
m_pInstrumentListView->setContextMenuPolicy(Qt::CustomContextMenu); |
66 |
|
QMainWindow::setCentralWidget(m_pInstrumentListView); |
67 |
|
|
68 |
QObject::connect(m_pMapComboBox, |
QObject::connect(m_pMapComboBox, |
69 |
SIGNAL(activated(int)), |
SIGNAL(activated(int)), |
70 |
SLOT(activateMap(int))); |
SLOT(activateMap(int))); |
|
|
|
71 |
QObject::connect( |
QObject::connect( |
72 |
ui.refreshInstrumentsAction, |
m_pInstrumentListView, |
73 |
SIGNAL(triggered()), |
SIGNAL(customContextMenuRequested(const QPoint&)), |
74 |
SLOT(refreshInstruments(void)) |
SLOT(contextMenu(const QPoint&))); |
|
); |
|
|
|
|
75 |
QObject::connect( |
QObject::connect( |
76 |
ui.InstrumentTable, |
m_pInstrumentListView, |
77 |
SIGNAL(activated(const QModelIndex&)), |
SIGNAL(activated(const QModelIndex&)), |
78 |
SLOT(editInstrument(const QModelIndex&)) |
SLOT(stabilizeForm())); |
79 |
); |
QObject::connect( |
80 |
|
m_pInstrumentListView, |
81 |
|
SIGNAL(doubleClicked(const QModelIndex&)), |
82 |
|
SLOT(editInstrument(const QModelIndex&))); |
83 |
QObject::connect( |
QObject::connect( |
84 |
ui.newInstrumentAction, |
m_ui.newInstrumentAction, |
85 |
SIGNAL(triggered()), |
SIGNAL(triggered()), |
86 |
SLOT(newInstrument()) |
SLOT(newInstrument())); |
|
); |
|
87 |
QObject::connect( |
QObject::connect( |
88 |
ui.deleteInstrumentAction, |
m_ui.deleteInstrumentAction, |
89 |
SIGNAL(triggered()), |
SIGNAL(triggered()), |
90 |
SLOT(deleteInstrument()) |
SLOT(deleteInstrument())); |
|
); |
|
91 |
QObject::connect( |
QObject::connect( |
92 |
ui.editInstrumentAction, |
m_ui.editInstrumentAction, |
93 |
SIGNAL(triggered()), |
SIGNAL(triggered()), |
94 |
SLOT(editInstrument()) |
SLOT(editInstrument())); |
95 |
); |
QObject::connect( |
96 |
|
m_ui.refreshInstrumentsAction, |
97 |
|
SIGNAL(triggered()), |
98 |
|
SLOT(refreshInstruments())); |
99 |
|
|
100 |
|
// Things must be stable from the start. |
101 |
|
stabilizeForm(); |
102 |
} |
} |
103 |
|
|
104 |
|
|
105 |
InstrumentListForm::~InstrumentListForm (void) |
InstrumentListForm::~InstrumentListForm (void) |
106 |
{ |
{ |
107 |
|
delete m_pInstrumentListView; |
108 |
delete m_pMapComboBox; |
delete m_pMapComboBox; |
109 |
} |
} |
110 |
|
|
149 |
if (pMainForm == NULL) |
if (pMainForm == NULL) |
150 |
return; |
return; |
151 |
|
|
152 |
qsamplerOptions *pOptions = pMainForm->options(); |
Options *pOptions = pMainForm->options(); |
153 |
if (pOptions == NULL) |
if (pOptions == NULL) |
154 |
return; |
return; |
155 |
|
|
161 |
// Populate maps list. |
// Populate maps list. |
162 |
m_pMapComboBox->clear(); |
m_pMapComboBox->clear(); |
163 |
m_pMapComboBox->addItem(tr("(All)")); |
m_pMapComboBox->addItem(tr("(All)")); |
164 |
m_pMapComboBox->insertItems(1, qsamplerInstrument::getMapNames()); |
m_pMapComboBox->insertItems(1, Instrument::getMapNames()); |
165 |
|
|
166 |
// Adjust to saved selection... |
// Adjust to saved selection... |
167 |
if (iMap < 0 || iMap >= m_pMapComboBox->count()) |
if (iMap < 0 || iMap >= m_pMapComboBox->count()) |
180 |
if (pMainForm == NULL) |
if (pMainForm == NULL) |
181 |
return; |
return; |
182 |
|
|
183 |
qsamplerOptions *pOptions = pMainForm->options(); |
Options *pOptions = pMainForm->options(); |
184 |
if (pOptions == NULL) |
if (pOptions == NULL) |
185 |
return; |
return; |
186 |
|
|
188 |
if (iMidiMap >= 0) |
if (iMidiMap >= 0) |
189 |
pOptions->iMidiMap = iMidiMap; |
pOptions->iMidiMap = iMidiMap; |
190 |
|
|
191 |
model.setMidiMap(iMidiMap); |
m_pInstrumentListView->setMidiMap(iMidiMap); |
192 |
model.refresh(); |
m_pInstrumentListView->refresh(); |
193 |
|
|
194 |
|
stabilizeForm(); |
195 |
} |
} |
196 |
|
|
197 |
|
|
198 |
void InstrumentListForm::editInstrument (void) |
void InstrumentListForm::editInstrument (void) |
199 |
{ |
{ |
200 |
editInstrument(ui.InstrumentTable->currentIndex()); |
editInstrument(m_pInstrumentListView->currentIndex()); |
201 |
} |
} |
202 |
|
|
203 |
|
|
204 |
void InstrumentListForm::editInstrument ( const QModelIndex& index ) |
void InstrumentListForm::editInstrument ( const QModelIndex& index ) |
205 |
{ |
{ |
206 |
if (!index.isValid() || !index.data(Qt::UserRole).isValid()) |
if (!index.isValid()) |
207 |
return; |
return; |
208 |
|
|
209 |
qsamplerInstrument* pInstrument |
Instrument *pInstrument |
210 |
= static_cast<qsamplerInstrument *> ( |
= static_cast<Instrument *> (index.internalPointer()); |
|
index.data(Qt::UserRole).value<void *> ()); |
|
|
|
|
211 |
if (pInstrument == NULL) |
if (pInstrument == NULL) |
212 |
return; |
return; |
213 |
|
|
214 |
// Save current key values... |
// Save current key values... |
215 |
qsamplerInstrument oldInstrument(*pInstrument); |
int iMap = pInstrument->map(); |
216 |
|
int iBank = pInstrument->bank(); |
217 |
|
int iProg = pInstrument->prog(); |
218 |
|
|
219 |
|
Instrument instrument(iMap, iBank, iProg); |
220 |
|
|
221 |
// Do the edit dance... |
// Do the edit dance... |
222 |
InstrumentForm form(this); |
InstrumentForm form(this); |
223 |
form.setup(pInstrument); |
form.setup(&instrument); |
224 |
if (form.exec()) { |
if (form.exec()) { |
225 |
// Commit... |
// Commit... |
226 |
pInstrument->mapInstrument(); |
instrument.mapInstrument(); |
227 |
// Check whether we changed instrument key... |
// Check whether we changed instrument key... |
228 |
if (oldInstrument.map() == pInstrument->map() && |
if (instrument.map() == iMap && |
229 |
oldInstrument.bank() == pInstrument->bank() && |
instrument.bank() == iBank && |
230 |
oldInstrument.prog() == pInstrument->prog()) { |
instrument.prog() == iProg) { |
231 |
// just update tree item... |
// Just update tree item... |
232 |
//pItem->update(); |
//pItem->update(); |
233 |
} else { |
} else { |
234 |
// Unmap old instance... |
// Unmap old instance... |
235 |
oldInstrument.unmapInstrument(); |
Instrument(iMap, iBank, iProg).unmapInstrument(); |
236 |
// correct the position of the instrument in the model |
// correct the position of the instrument in the model |
237 |
model.resort(*pInstrument); |
m_pInstrumentListView->updateInstrument(pInstrument); |
238 |
} |
} |
|
// Notify we've changes... |
|
|
emit model.reset(); |
|
239 |
} |
} |
240 |
|
|
241 |
|
stabilizeForm(); |
242 |
} |
} |
243 |
|
|
244 |
|
|
245 |
void InstrumentListForm::newInstrument (void) |
void InstrumentListForm::newInstrument (void) |
246 |
{ |
{ |
247 |
qsamplerInstrument instrument; |
Instrument instrument; |
248 |
|
|
249 |
InstrumentForm form(this); |
InstrumentForm form(this); |
250 |
form.setup(&instrument); |
form.setup(&instrument); |
253 |
|
|
254 |
// Commit... |
// Commit... |
255 |
instrument.mapInstrument(); |
instrument.mapInstrument(); |
256 |
|
|
257 |
// add new item to the table model |
// add new item to the table model |
258 |
model.resort(instrument); |
m_pInstrumentListView->addInstrument( |
259 |
// Notify we've changes... |
instrument.map(), |
260 |
//emit model.reset(); |
instrument.bank(), |
261 |
//FIXME: call above didnt really refresh, so we use this for now ... |
instrument.prog()); |
262 |
refreshInstruments(); |
|
263 |
|
stabilizeForm(); |
264 |
} |
} |
265 |
|
|
266 |
|
|
267 |
void InstrumentListForm::deleteInstrument (void) |
void InstrumentListForm::deleteInstrument (void) |
268 |
{ |
{ |
269 |
const QModelIndex& index = ui.InstrumentTable->currentIndex(); |
const QModelIndex& index = m_pInstrumentListView->currentIndex(); |
270 |
if (!index.isValid() || !index.data(Qt::UserRole).isValid()) |
if (!index.isValid()) |
271 |
return; |
return; |
272 |
|
|
273 |
qsamplerInstrument* pInstrument = |
Instrument *pInstrument |
274 |
static_cast<qsamplerInstrument*> ( |
= static_cast<Instrument *> (index.internalPointer()); |
|
index.data(Qt::UserRole).value<void *> ()); |
|
|
|
|
275 |
if (pInstrument == NULL) |
if (pInstrument == NULL) |
276 |
return; |
return; |
277 |
|
|
278 |
|
MainForm *pMainForm = MainForm::getInstance(); |
279 |
|
if (pMainForm == NULL) |
280 |
|
return; |
281 |
|
|
282 |
|
// Prompt user if this is for real... |
283 |
|
Options *pOptions = pMainForm->options(); |
284 |
|
if (pOptions && pOptions->bConfirmRemove) { |
285 |
|
if (QMessageBox::warning(this, |
286 |
|
QSAMPLER_TITLE ": " + tr("Warning"), |
287 |
|
tr("About to delete instrument map entry:\n\n" |
288 |
|
"%1\n\n" |
289 |
|
"Are you sure?") |
290 |
|
.arg(pInstrument->name()), |
291 |
|
QMessageBox::Ok | QMessageBox::Cancel) |
292 |
|
== QMessageBox::Cancel) |
293 |
|
return; |
294 |
|
} |
295 |
|
|
296 |
pInstrument->unmapInstrument(); |
pInstrument->unmapInstrument(); |
297 |
|
|
298 |
// let the instrument vanish from the table model |
// let the instrument vanish from the table model |
299 |
model.removeInstrument(*pInstrument); |
m_pInstrumentListView->removeInstrument(pInstrument); |
300 |
// Notify we've changes... |
|
301 |
emit model.reset(); |
stabilizeForm(); |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
|
// Update form actions enablement... |
306 |
|
void InstrumentListForm::stabilizeForm (void) |
307 |
|
{ |
308 |
|
MainForm *pMainForm = MainForm::getInstance(); |
309 |
|
|
310 |
|
bool bEnabled = (pMainForm && pMainForm->client()); |
311 |
|
m_ui.newInstrumentAction->setEnabled(bEnabled); |
312 |
|
const QModelIndex& index = m_pInstrumentListView->currentIndex(); |
313 |
|
bEnabled = (bEnabled && index.isValid()); |
314 |
|
m_ui.editInstrumentAction->setEnabled(bEnabled); |
315 |
|
m_ui.deleteInstrumentAction->setEnabled(bEnabled); |
316 |
} |
} |
317 |
|
|
318 |
|
|
319 |
|
// Handle custom context menu here... |
320 |
|
void InstrumentListForm::contextMenu ( const QPoint& pos ) |
321 |
|
{ |
322 |
|
if (!m_ui.newInstrumentAction->isEnabled()) |
323 |
|
return; |
324 |
|
|
325 |
|
m_ui.contextMenu->exec( |
326 |
|
(m_pInstrumentListView->viewport())->mapToGlobal(pos)); |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
} // namespace QSampler |
} // namespace QSampler |
331 |
|
|
332 |
|
|