1 |
capela |
1464 |
// qsamplerInstrumentListForm.cpp |
2 |
|
|
// |
3 |
|
|
/**************************************************************************** |
4 |
capela |
1840 |
Copyright (C) 2003-2009, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
capela |
1464 |
Copyright (C) 2007, Christian Schoenebeck |
6 |
|
|
|
7 |
|
|
This program is free software; you can redistribute it and/or |
8 |
|
|
modify it under the terms of the GNU General Public License |
9 |
|
|
as published by the Free Software Foundation; either version 2 |
10 |
|
|
of the License, or (at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License |
18 |
|
|
along with this program; if not, write to the Free Software |
19 |
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
20 |
|
|
|
21 |
|
|
*****************************************************************************/ |
22 |
|
|
|
23 |
capela |
1513 |
#include "qsamplerAbout.h" |
24 |
schoenebeck |
1461 |
#include "qsamplerInstrumentListForm.h" |
25 |
|
|
|
26 |
schoenebeck |
1492 |
#include "qsamplerInstrumentForm.h" |
27 |
capela |
1513 |
|
28 |
schoenebeck |
1461 |
#include "qsamplerOptions.h" |
29 |
|
|
#include "qsamplerInstrument.h" |
30 |
capela |
1513 |
#include "qsamplerMainForm.h" |
31 |
schoenebeck |
1461 |
|
32 |
capela |
1519 |
#include <QHeaderView> |
33 |
capela |
1528 |
#include <QMessageBox> |
34 |
schoenebeck |
1461 |
|
35 |
capela |
1519 |
|
36 |
schoenebeck |
1461 |
namespace QSampler { |
37 |
|
|
|
38 |
capela |
1558 |
//------------------------------------------------------------------------- |
39 |
|
|
// QSampler::InstrumentListForm -- Instrument map list form implementation. |
40 |
|
|
// |
41 |
|
|
|
42 |
capela |
1504 |
InstrumentListForm::InstrumentListForm ( |
43 |
|
|
QWidget* pParent, Qt::WindowFlags wflags ) |
44 |
|
|
: QMainWindow(pParent, wflags) |
45 |
capela |
1473 |
{ |
46 |
capela |
1509 |
m_ui.setupUi(this); |
47 |
schoenebeck |
1461 |
|
48 |
capela |
1509 |
// Setup toolbar widgets. |
49 |
|
|
m_pMapComboBox = new QComboBox(m_ui.InstrumentToolbar); |
50 |
|
|
m_pMapComboBox->setMinimumWidth(120); |
51 |
|
|
m_pMapComboBox->setEnabled(false); |
52 |
|
|
m_pMapComboBox->setToolTip(tr("Instrument Map")); |
53 |
|
|
m_ui.InstrumentToolbar->addWidget(m_pMapComboBox); |
54 |
schoenebeck |
1461 |
|
55 |
capela |
1509 |
m_ui.InstrumentToolbar->addSeparator(); |
56 |
|
|
m_ui.InstrumentToolbar->addAction(m_ui.newInstrumentAction); |
57 |
|
|
m_ui.InstrumentToolbar->addAction(m_ui.editInstrumentAction); |
58 |
|
|
m_ui.InstrumentToolbar->addAction(m_ui.deleteInstrumentAction); |
59 |
|
|
m_ui.InstrumentToolbar->addSeparator(); |
60 |
|
|
m_ui.InstrumentToolbar->addAction(m_ui.refreshInstrumentsAction); |
61 |
schoenebeck |
1461 |
|
62 |
capela |
1519 |
int iRowHeight = m_ui.InstrumentTable->fontMetrics().height() + 4; |
63 |
|
|
m_ui.InstrumentTable->verticalHeader()->setDefaultSectionSize(iRowHeight); |
64 |
|
|
|
65 |
capela |
1509 |
m_ui.InstrumentTable->setModel(&m_model); |
66 |
|
|
m_ui.InstrumentTable->setItemDelegate(&m_delegate); |
67 |
capela |
1519 |
m_ui.InstrumentTable->verticalHeader()->hide(); |
68 |
schoenebeck |
1461 |
|
69 |
capela |
1526 |
QHeaderView *pHeader = m_ui.InstrumentTable->horizontalHeader(); |
70 |
|
|
pHeader->setDefaultAlignment(Qt::AlignLeft); |
71 |
|
|
pHeader->setMovable(false); |
72 |
|
|
pHeader->setStretchLastSection(true); |
73 |
|
|
pHeader->resizeSection(0, 120); // Name |
74 |
|
|
m_ui.InstrumentTable->resizeColumnToContents(1); // Map |
75 |
|
|
m_ui.InstrumentTable->resizeColumnToContents(2); // Bank |
76 |
|
|
m_ui.InstrumentTable->resizeColumnToContents(3); // Prog |
77 |
|
|
m_ui.InstrumentTable->resizeColumnToContents(4); // Engine |
78 |
|
|
pHeader->resizeSection(5, 240); // File |
79 |
|
|
m_ui.InstrumentTable->resizeColumnToContents(6); // Nr |
80 |
capela |
1528 |
pHeader->resizeSection(7, 60); // Vol |
81 |
capela |
1526 |
|
82 |
capela |
1528 |
// Enable custom context menu... |
83 |
|
|
m_ui.InstrumentTable->setContextMenuPolicy(Qt::CustomContextMenu); |
84 |
|
|
|
85 |
schoenebeck |
1461 |
QObject::connect(m_pMapComboBox, |
86 |
|
|
SIGNAL(activated(int)), |
87 |
|
|
SLOT(activateMap(int))); |
88 |
capela |
1466 |
QObject::connect( |
89 |
capela |
1528 |
m_ui.InstrumentTable, |
90 |
|
|
SIGNAL(customContextMenuRequested(const QPoint&)), |
91 |
|
|
SLOT(contextMenu(const QPoint&))); |
92 |
capela |
1504 |
QObject::connect( |
93 |
capela |
1509 |
m_ui.InstrumentTable, |
94 |
capela |
1528 |
SIGNAL(pressed(const QModelIndex&)), |
95 |
|
|
SLOT(stabilizeForm())); |
96 |
|
|
QObject::connect( |
97 |
|
|
m_ui.InstrumentTable, |
98 |
schoenebeck |
1492 |
SIGNAL(activated(const QModelIndex&)), |
99 |
capela |
1509 |
SLOT(editInstrument(const QModelIndex&))); |
100 |
capela |
1504 |
QObject::connect( |
101 |
capela |
1509 |
m_ui.newInstrumentAction, |
102 |
schoenebeck |
1492 |
SIGNAL(triggered()), |
103 |
capela |
1509 |
SLOT(newInstrument())); |
104 |
capela |
1504 |
QObject::connect( |
105 |
capela |
1509 |
m_ui.deleteInstrumentAction, |
106 |
schoenebeck |
1492 |
SIGNAL(triggered()), |
107 |
capela |
1509 |
SLOT(deleteInstrument())); |
108 |
capela |
1504 |
QObject::connect( |
109 |
capela |
1509 |
m_ui.editInstrumentAction, |
110 |
schoenebeck |
1492 |
SIGNAL(triggered()), |
111 |
capela |
1509 |
SLOT(editInstrument())); |
112 |
capela |
1528 |
QObject::connect( |
113 |
|
|
m_ui.refreshInstrumentsAction, |
114 |
|
|
SIGNAL(triggered()), |
115 |
|
|
SLOT(refreshInstruments())); |
116 |
capela |
1509 |
|
117 |
|
|
MainForm *pMainForm = MainForm::getInstance(); |
118 |
|
|
if (pMainForm) { |
119 |
|
|
QObject::connect(&m_model, |
120 |
|
|
SIGNAL(instrumentsChanged()), |
121 |
|
|
pMainForm, SLOT(sessionDirty())); |
122 |
|
|
} |
123 |
capela |
1528 |
|
124 |
|
|
// Things must be stable from the start. |
125 |
|
|
stabilizeForm(); |
126 |
schoenebeck |
1461 |
} |
127 |
|
|
|
128 |
capela |
1504 |
|
129 |
|
|
InstrumentListForm::~InstrumentListForm (void) |
130 |
|
|
{ |
131 |
schoenebeck |
1461 |
delete m_pMapComboBox; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
// Notify our parent that we're emerging. |
136 |
|
|
void InstrumentListForm::showEvent ( QShowEvent *pShowEvent ) |
137 |
|
|
{ |
138 |
capela |
1504 |
MainForm* pMainForm = MainForm::getInstance(); |
139 |
|
|
if (pMainForm) |
140 |
|
|
pMainForm->stabilizeForm(); |
141 |
schoenebeck |
1461 |
|
142 |
|
|
QWidget::showEvent(pShowEvent); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
|
146 |
|
|
// Notify our parent that we're closing. |
147 |
|
|
void InstrumentListForm::hideEvent ( QHideEvent *pHideEvent ) |
148 |
|
|
{ |
149 |
|
|
QWidget::hideEvent(pHideEvent); |
150 |
|
|
|
151 |
capela |
1504 |
MainForm* pMainForm = MainForm::getInstance(); |
152 |
|
|
if (pMainForm) |
153 |
|
|
pMainForm->stabilizeForm(); |
154 |
schoenebeck |
1461 |
} |
155 |
|
|
|
156 |
|
|
|
157 |
capela |
1504 |
// Just about to notify main-window that we're closing. |
158 |
|
|
void InstrumentListForm::closeEvent ( QCloseEvent * /*pCloseEvent*/ ) |
159 |
|
|
{ |
160 |
|
|
QWidget::hide(); |
161 |
|
|
|
162 |
|
|
MainForm *pMainForm = MainForm::getInstance(); |
163 |
|
|
if (pMainForm) |
164 |
|
|
pMainForm->stabilizeForm(); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
|
168 |
schoenebeck |
1461 |
// Refresh all instrument list and views. |
169 |
|
|
void InstrumentListForm::refreshInstruments (void) |
170 |
|
|
{ |
171 |
|
|
MainForm* pMainForm = MainForm::getInstance(); |
172 |
|
|
if (pMainForm == NULL) |
173 |
|
|
return; |
174 |
|
|
|
175 |
capela |
1558 |
Options *pOptions = pMainForm->options(); |
176 |
schoenebeck |
1461 |
if (pOptions == NULL) |
177 |
|
|
return; |
178 |
|
|
|
179 |
|
|
// Get/save current map selection... |
180 |
capela |
1499 |
int iMap = m_pMapComboBox->currentIndex(); |
181 |
schoenebeck |
1461 |
if (iMap < 0 || m_pMapComboBox->count() < 2) |
182 |
|
|
iMap = pOptions->iMidiMap + 1; |
183 |
|
|
|
184 |
|
|
// Populate maps list. |
185 |
|
|
m_pMapComboBox->clear(); |
186 |
capela |
1499 |
m_pMapComboBox->addItem(tr("(All)")); |
187 |
capela |
1558 |
m_pMapComboBox->insertItems(1, Instrument::getMapNames()); |
188 |
schoenebeck |
1461 |
|
189 |
|
|
// Adjust to saved selection... |
190 |
|
|
if (iMap < 0 || iMap >= m_pMapComboBox->count()) |
191 |
|
|
iMap = 0; |
192 |
capela |
1499 |
m_pMapComboBox->setCurrentIndex(iMap); |
193 |
schoenebeck |
1461 |
m_pMapComboBox->setEnabled(m_pMapComboBox->count() > 1); |
194 |
|
|
|
195 |
|
|
activateMap(iMap); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
|
199 |
|
|
// Refresh instrument maps selector. |
200 |
|
|
void InstrumentListForm::activateMap ( int iMap ) |
201 |
|
|
{ |
202 |
|
|
MainForm* pMainForm = MainForm::getInstance(); |
203 |
|
|
if (pMainForm == NULL) |
204 |
|
|
return; |
205 |
|
|
|
206 |
capela |
1558 |
Options *pOptions = pMainForm->options(); |
207 |
schoenebeck |
1461 |
if (pOptions == NULL) |
208 |
|
|
return; |
209 |
|
|
|
210 |
|
|
int iMidiMap = iMap - 1; |
211 |
|
|
if (iMidiMap >= 0) |
212 |
|
|
pOptions->iMidiMap = iMidiMap; |
213 |
|
|
|
214 |
capela |
1509 |
m_model.setMidiMap(iMidiMap); |
215 |
|
|
m_model.refresh(); |
216 |
capela |
1528 |
|
217 |
|
|
stabilizeForm(); |
218 |
schoenebeck |
1461 |
} |
219 |
|
|
|
220 |
capela |
1504 |
|
221 |
|
|
void InstrumentListForm::editInstrument (void) |
222 |
|
|
{ |
223 |
capela |
1509 |
editInstrument(m_ui.InstrumentTable->currentIndex()); |
224 |
schoenebeck |
1492 |
} |
225 |
|
|
|
226 |
capela |
1504 |
|
227 |
|
|
void InstrumentListForm::editInstrument ( const QModelIndex& index ) |
228 |
|
|
{ |
229 |
schoenebeck |
1492 |
if (!index.isValid() || !index.data(Qt::UserRole).isValid()) |
230 |
|
|
return; |
231 |
|
|
|
232 |
capela |
1558 |
Instrument* pInstrument |
233 |
|
|
= static_cast<Instrument *> ( |
234 |
capela |
1504 |
index.data(Qt::UserRole).value<void *> ()); |
235 |
schoenebeck |
1492 |
|
236 |
capela |
1504 |
if (pInstrument == NULL) |
237 |
|
|
return; |
238 |
schoenebeck |
1492 |
|
239 |
|
|
// Save current key values... |
240 |
capela |
1558 |
Instrument oldInstrument(*pInstrument); |
241 |
schoenebeck |
1492 |
// Do the edit dance... |
242 |
|
|
InstrumentForm form(this); |
243 |
|
|
form.setup(pInstrument); |
244 |
|
|
if (form.exec()) { |
245 |
|
|
// Commit... |
246 |
|
|
pInstrument->mapInstrument(); |
247 |
|
|
// Check whether we changed instrument key... |
248 |
|
|
if (oldInstrument.map() == pInstrument->map() && |
249 |
|
|
oldInstrument.bank() == pInstrument->bank() && |
250 |
|
|
oldInstrument.prog() == pInstrument->prog()) { |
251 |
capela |
1523 |
// Just update tree item... |
252 |
schoenebeck |
1492 |
//pItem->update(); |
253 |
|
|
} else { |
254 |
|
|
// Unmap old instance... |
255 |
|
|
oldInstrument.unmapInstrument(); |
256 |
|
|
// correct the position of the instrument in the model |
257 |
capela |
1509 |
m_model.resort(*pInstrument); |
258 |
schoenebeck |
1492 |
} |
259 |
|
|
// Notify we've changes... |
260 |
capela |
1509 |
emit m_model.reset(); |
261 |
schoenebeck |
1492 |
} |
262 |
|
|
} |
263 |
|
|
|
264 |
capela |
1504 |
|
265 |
|
|
void InstrumentListForm::newInstrument (void) |
266 |
|
|
{ |
267 |
capela |
1558 |
Instrument instrument; |
268 |
schoenebeck |
1492 |
|
269 |
|
|
InstrumentForm form(this); |
270 |
|
|
form.setup(&instrument); |
271 |
capela |
1504 |
if (!form.exec()) |
272 |
|
|
return; |
273 |
schoenebeck |
1492 |
|
274 |
|
|
// Commit... |
275 |
|
|
instrument.mapInstrument(); |
276 |
|
|
// add new item to the table model |
277 |
capela |
1509 |
m_model.resort(instrument); |
278 |
schoenebeck |
1492 |
// Notify we've changes... |
279 |
|
|
//emit model.reset(); |
280 |
|
|
//FIXME: call above didnt really refresh, so we use this for now ... |
281 |
|
|
refreshInstruments(); |
282 |
|
|
} |
283 |
|
|
|
284 |
|
|
|
285 |
capela |
1504 |
void InstrumentListForm::deleteInstrument (void) |
286 |
|
|
{ |
287 |
capela |
1509 |
const QModelIndex& index = m_ui.InstrumentTable->currentIndex(); |
288 |
capela |
1504 |
if (!index.isValid() || !index.data(Qt::UserRole).isValid()) |
289 |
|
|
return; |
290 |
|
|
|
291 |
capela |
1558 |
Instrument *pInstrument = |
292 |
|
|
static_cast<Instrument*> ( |
293 |
capela |
1504 |
index.data(Qt::UserRole).value<void *> ()); |
294 |
schoenebeck |
1492 |
|
295 |
capela |
1504 |
if (pInstrument == NULL) |
296 |
|
|
return; |
297 |
schoenebeck |
1492 |
|
298 |
capela |
1528 |
MainForm *pMainForm = MainForm::getInstance(); |
299 |
|
|
if (pMainForm == NULL) |
300 |
|
|
return; |
301 |
|
|
|
302 |
|
|
// Prompt user if this is for real... |
303 |
capela |
1558 |
Options *pOptions = pMainForm->options(); |
304 |
capela |
1528 |
if (pOptions && pOptions->bConfirmRemove) { |
305 |
|
|
if (QMessageBox::warning(this, |
306 |
|
|
QSAMPLER_TITLE ": " + tr("Warning"), |
307 |
|
|
tr("About to delete instrument map entry:\n\n" |
308 |
|
|
"%1\n\n" |
309 |
|
|
"Are you sure?") |
310 |
|
|
.arg(pInstrument->name()), |
311 |
capela |
1840 |
QMessageBox::Ok | QMessageBox::Cancel) |
312 |
|
|
== QMessageBox::Cancel) |
313 |
capela |
1528 |
return; |
314 |
|
|
} |
315 |
|
|
|
316 |
schoenebeck |
1492 |
pInstrument->unmapInstrument(); |
317 |
|
|
// let the instrument vanish from the table model |
318 |
capela |
1509 |
m_model.removeInstrument(*pInstrument); |
319 |
schoenebeck |
1492 |
// Notify we've changes... |
320 |
capela |
1509 |
emit m_model.reset(); |
321 |
schoenebeck |
1492 |
} |
322 |
|
|
|
323 |
capela |
1528 |
|
324 |
|
|
// Update form actions enablement... |
325 |
|
|
void InstrumentListForm::stabilizeForm (void) |
326 |
|
|
{ |
327 |
|
|
MainForm *pMainForm = MainForm::getInstance(); |
328 |
|
|
|
329 |
|
|
bool bEnabled = (pMainForm && pMainForm->client()); |
330 |
|
|
m_ui.newInstrumentAction->setEnabled(bEnabled); |
331 |
|
|
const QModelIndex& index = m_ui.InstrumentTable->currentIndex(); |
332 |
|
|
bEnabled = (bEnabled && index.isValid()); |
333 |
|
|
m_ui.editInstrumentAction->setEnabled(bEnabled); |
334 |
|
|
m_ui.deleteInstrumentAction->setEnabled(bEnabled); |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
|
338 |
|
|
// Handle custom context menu here... |
339 |
|
|
void InstrumentListForm::contextMenu ( const QPoint& pos ) |
340 |
|
|
{ |
341 |
|
|
if (!m_ui.newInstrumentAction->isEnabled()) |
342 |
|
|
return; |
343 |
|
|
|
344 |
|
|
m_ui.contextMenu->exec( |
345 |
|
|
(m_ui.InstrumentTable->viewport())->mapToGlobal(pos)); |
346 |
|
|
} |
347 |
|
|
|
348 |
|
|
|
349 |
schoenebeck |
1461 |
} // namespace QSampler |
350 |
capela |
1464 |
|
351 |
|
|
|
352 |
|
|
// end of qsamplerInstrumentListForm.cpp |