1 |
|
// qsamplerInstrumentListForm.cpp |
2 |
|
// |
3 |
|
/**************************************************************************** |
4 |
|
Copyright (C) 2003-2007, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
|
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 |
|
#include "qsamplerAbout.h" |
24 |
#include "qsamplerInstrumentListForm.h" |
#include "qsamplerInstrumentListForm.h" |
25 |
|
|
26 |
#include "qsamplerMainForm.h" |
#include "qsamplerInstrumentForm.h" |
27 |
|
|
28 |
#include "qsamplerOptions.h" |
#include "qsamplerOptions.h" |
29 |
#include "qsamplerInstrument.h" |
#include "qsamplerInstrument.h" |
30 |
|
#include "qsamplerMainForm.h" |
31 |
|
|
32 |
|
#include <QHeaderView> |
33 |
|
#include <QMessageBox> |
34 |
|
|
|
#include <QToolTip> |
|
35 |
|
|
36 |
namespace QSampler { |
namespace QSampler { |
37 |
|
|
38 |
InstrumentListForm::InstrumentListForm(QWidget* parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) { |
InstrumentListForm::InstrumentListForm ( |
39 |
ui.setupUi(this); |
QWidget* pParent, Qt::WindowFlags wflags ) |
40 |
|
: QMainWindow(pParent, wflags) |
41 |
|
{ |
42 |
|
m_ui.setupUi(this); |
43 |
|
|
44 |
ui.newInstrumentAction->setText(tr("New &Instrument...")); |
// Setup toolbar widgets. |
45 |
ui.newInstrumentAction->setShortcut(Qt::Key_Insert); |
m_pMapComboBox = new QComboBox(m_ui.InstrumentToolbar); |
46 |
ui.editInstrumentAction->setText(tr("&Edit...")); |
m_pMapComboBox->setMinimumWidth(120); |
47 |
ui.editInstrumentAction->setShortcut(Qt::Key_Enter); |
m_pMapComboBox->setEnabled(false); |
48 |
ui.deleteInstrumentAction->setText(tr("&Delete")); |
m_pMapComboBox->setToolTip(tr("Instrument Map")); |
49 |
ui.deleteInstrumentAction->setShortcut(Qt::Key_Delete); |
m_ui.InstrumentToolbar->addWidget(m_pMapComboBox); |
50 |
ui.refreshInstrumentsAction->setText(tr("&Refresh")); |
|
51 |
ui.refreshInstrumentsAction->setShortcut(Qt::Key_F5); |
m_ui.InstrumentToolbar->addSeparator(); |
52 |
|
m_ui.InstrumentToolbar->addAction(m_ui.newInstrumentAction); |
53 |
// Setup toolbar widgets. |
m_ui.InstrumentToolbar->addAction(m_ui.editInstrumentAction); |
54 |
InstrumentToolbar = addToolBar(tr("MIDI Instruments")); |
m_ui.InstrumentToolbar->addAction(m_ui.deleteInstrumentAction); |
55 |
m_pMapComboBox = new QComboBox(InstrumentToolbar); |
m_ui.InstrumentToolbar->addSeparator(); |
56 |
m_pMapComboBox->setMinimumWidth(120); |
m_ui.InstrumentToolbar->addAction(m_ui.refreshInstrumentsAction); |
57 |
m_pMapComboBox->setEnabled(false); |
|
58 |
QToolTip::add(m_pMapComboBox, tr("Instrument Map")); |
int iRowHeight = m_ui.InstrumentTable->fontMetrics().height() + 4; |
59 |
|
m_ui.InstrumentTable->verticalHeader()->setDefaultSectionSize(iRowHeight); |
60 |
InstrumentToolbar->addSeparator(); |
|
61 |
ui.newInstrumentAction->addTo(InstrumentToolbar); |
m_ui.InstrumentTable->setModel(&m_model); |
62 |
ui.editInstrumentAction->addTo(InstrumentToolbar); |
m_ui.InstrumentTable->setItemDelegate(&m_delegate); |
63 |
ui.deleteInstrumentAction->addTo(InstrumentToolbar); |
m_ui.InstrumentTable->verticalHeader()->hide(); |
64 |
InstrumentToolbar->addSeparator(); |
|
65 |
ui.refreshInstrumentsAction->addTo(InstrumentToolbar); |
QHeaderView *pHeader = m_ui.InstrumentTable->horizontalHeader(); |
66 |
|
pHeader->setDefaultAlignment(Qt::AlignLeft); |
67 |
|
pHeader->setMovable(false); |
68 |
|
pHeader->setStretchLastSection(true); |
69 |
|
pHeader->resizeSection(0, 120); // Name |
70 |
|
m_ui.InstrumentTable->resizeColumnToContents(1); // Map |
71 |
|
m_ui.InstrumentTable->resizeColumnToContents(2); // Bank |
72 |
|
m_ui.InstrumentTable->resizeColumnToContents(3); // Prog |
73 |
|
m_ui.InstrumentTable->resizeColumnToContents(4); // Engine |
74 |
|
pHeader->resizeSection(5, 240); // File |
75 |
|
m_ui.InstrumentTable->resizeColumnToContents(6); // Nr |
76 |
|
pHeader->resizeSection(7, 60); // Vol |
77 |
|
|
78 |
ui.InstrumentTable->setModel(&model); |
// Enable custom context menu... |
79 |
//ui.InstrumentTable->setDelegate(delegate); |
m_ui.InstrumentTable->setContextMenuPolicy(Qt::CustomContextMenu); |
80 |
|
|
81 |
QObject::connect(m_pMapComboBox, |
QObject::connect(m_pMapComboBox, |
82 |
SIGNAL(activated(int)), |
SIGNAL(activated(int)), |
83 |
SLOT(activateMap(int))); |
SLOT(activateMap(int))); |
84 |
|
QObject::connect( |
85 |
|
m_ui.InstrumentTable, |
86 |
|
SIGNAL(customContextMenuRequested(const QPoint&)), |
87 |
|
SLOT(contextMenu(const QPoint&))); |
88 |
|
QObject::connect( |
89 |
|
m_ui.InstrumentTable, |
90 |
|
SIGNAL(pressed(const QModelIndex&)), |
91 |
|
SLOT(stabilizeForm())); |
92 |
|
QObject::connect( |
93 |
|
m_ui.InstrumentTable, |
94 |
|
SIGNAL(activated(const QModelIndex&)), |
95 |
|
SLOT(editInstrument(const QModelIndex&))); |
96 |
|
QObject::connect( |
97 |
|
m_ui.newInstrumentAction, |
98 |
|
SIGNAL(triggered()), |
99 |
|
SLOT(newInstrument())); |
100 |
|
QObject::connect( |
101 |
|
m_ui.deleteInstrumentAction, |
102 |
|
SIGNAL(triggered()), |
103 |
|
SLOT(deleteInstrument())); |
104 |
|
QObject::connect( |
105 |
|
m_ui.editInstrumentAction, |
106 |
|
SIGNAL(triggered()), |
107 |
|
SLOT(editInstrument())); |
108 |
|
QObject::connect( |
109 |
|
m_ui.refreshInstrumentsAction, |
110 |
|
SIGNAL(triggered()), |
111 |
|
SLOT(refreshInstruments())); |
112 |
|
|
113 |
|
MainForm *pMainForm = MainForm::getInstance(); |
114 |
|
if (pMainForm) { |
115 |
|
QObject::connect(&m_model, |
116 |
|
SIGNAL(instrumentsChanged()), |
117 |
|
pMainForm, SLOT(sessionDirty())); |
118 |
|
} |
119 |
|
|
120 |
connect( |
// Things must be stable from the start. |
121 |
ui.refreshInstrumentsAction, |
stabilizeForm(); |
|
SIGNAL(triggered()), SLOT(refreshInstruments(void)) |
|
|
); |
|
122 |
} |
} |
123 |
|
|
124 |
InstrumentListForm::~InstrumentListForm() { |
|
125 |
|
InstrumentListForm::~InstrumentListForm (void) |
126 |
|
{ |
127 |
delete m_pMapComboBox; |
delete m_pMapComboBox; |
128 |
} |
} |
129 |
|
|
131 |
// Notify our parent that we're emerging. |
// Notify our parent that we're emerging. |
132 |
void InstrumentListForm::showEvent ( QShowEvent *pShowEvent ) |
void InstrumentListForm::showEvent ( QShowEvent *pShowEvent ) |
133 |
{ |
{ |
134 |
//MainForm* pMainForm = MainForm::getInstance(); |
MainForm* pMainForm = MainForm::getInstance(); |
135 |
//if (pMainForm) |
if (pMainForm) |
136 |
// pMainForm->stabilizeForm(); |
pMainForm->stabilizeForm(); |
137 |
|
|
138 |
QWidget::showEvent(pShowEvent); |
QWidget::showEvent(pShowEvent); |
139 |
} |
} |
144 |
{ |
{ |
145 |
QWidget::hideEvent(pHideEvent); |
QWidget::hideEvent(pHideEvent); |
146 |
|
|
147 |
//MainForm* pMainForm = MainForm::getInstance(); |
MainForm* pMainForm = MainForm::getInstance(); |
148 |
//if (pMainForm) |
if (pMainForm) |
149 |
// pMainForm->stabilizeForm(); |
pMainForm->stabilizeForm(); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
// Just about to notify main-window that we're closing. |
154 |
|
void InstrumentListForm::closeEvent ( QCloseEvent * /*pCloseEvent*/ ) |
155 |
|
{ |
156 |
|
QWidget::hide(); |
157 |
|
|
158 |
|
MainForm *pMainForm = MainForm::getInstance(); |
159 |
|
if (pMainForm) |
160 |
|
pMainForm->stabilizeForm(); |
161 |
} |
} |
162 |
|
|
163 |
|
|
173 |
return; |
return; |
174 |
|
|
175 |
// Get/save current map selection... |
// Get/save current map selection... |
176 |
int iMap = m_pMapComboBox->currentItem(); |
int iMap = m_pMapComboBox->currentIndex(); |
177 |
if (iMap < 0 || m_pMapComboBox->count() < 2) |
if (iMap < 0 || m_pMapComboBox->count() < 2) |
178 |
iMap = pOptions->iMidiMap + 1; |
iMap = pOptions->iMidiMap + 1; |
179 |
|
|
180 |
// Populate maps list. |
// Populate maps list. |
181 |
m_pMapComboBox->clear(); |
m_pMapComboBox->clear(); |
182 |
m_pMapComboBox->insertItem(tr("(All)")); |
m_pMapComboBox->addItem(tr("(All)")); |
183 |
m_pMapComboBox->insertStringList(qsamplerInstrument::getMapNames()); |
m_pMapComboBox->insertItems(1, qsamplerInstrument::getMapNames()); |
184 |
|
|
185 |
// Adjust to saved selection... |
// Adjust to saved selection... |
186 |
if (iMap < 0 || iMap >= m_pMapComboBox->count()) |
if (iMap < 0 || iMap >= m_pMapComboBox->count()) |
187 |
iMap = 0; |
iMap = 0; |
188 |
m_pMapComboBox->setCurrentItem(iMap); |
m_pMapComboBox->setCurrentIndex(iMap); |
189 |
m_pMapComboBox->setEnabled(m_pMapComboBox->count() > 1); |
m_pMapComboBox->setEnabled(m_pMapComboBox->count() > 1); |
190 |
|
|
191 |
activateMap(iMap); |
activateMap(iMap); |
207 |
if (iMidiMap >= 0) |
if (iMidiMap >= 0) |
208 |
pOptions->iMidiMap = iMidiMap; |
pOptions->iMidiMap = iMidiMap; |
209 |
|
|
210 |
model.setMidiMap(iMidiMap); |
m_model.setMidiMap(iMidiMap); |
211 |
model.refresh(); |
m_model.refresh(); |
212 |
|
|
213 |
|
stabilizeForm(); |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
void InstrumentListForm::editInstrument (void) |
218 |
|
{ |
219 |
|
editInstrument(m_ui.InstrumentTable->currentIndex()); |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
void InstrumentListForm::editInstrument ( const QModelIndex& index ) |
224 |
|
{ |
225 |
|
if (!index.isValid() || !index.data(Qt::UserRole).isValid()) |
226 |
|
return; |
227 |
|
|
228 |
|
qsamplerInstrument* pInstrument |
229 |
|
= static_cast<qsamplerInstrument *> ( |
230 |
|
index.data(Qt::UserRole).value<void *> ()); |
231 |
|
|
232 |
|
if (pInstrument == NULL) |
233 |
|
return; |
234 |
|
|
235 |
|
// Save current key values... |
236 |
|
qsamplerInstrument oldInstrument(*pInstrument); |
237 |
|
// Do the edit dance... |
238 |
|
InstrumentForm form(this); |
239 |
|
form.setup(pInstrument); |
240 |
|
if (form.exec()) { |
241 |
|
// Commit... |
242 |
|
pInstrument->mapInstrument(); |
243 |
|
// Check whether we changed instrument key... |
244 |
|
if (oldInstrument.map() == pInstrument->map() && |
245 |
|
oldInstrument.bank() == pInstrument->bank() && |
246 |
|
oldInstrument.prog() == pInstrument->prog()) { |
247 |
|
// Just update tree item... |
248 |
|
//pItem->update(); |
249 |
|
} else { |
250 |
|
// Unmap old instance... |
251 |
|
oldInstrument.unmapInstrument(); |
252 |
|
// correct the position of the instrument in the model |
253 |
|
m_model.resort(*pInstrument); |
254 |
|
} |
255 |
|
// Notify we've changes... |
256 |
|
emit m_model.reset(); |
257 |
|
} |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
void InstrumentListForm::newInstrument (void) |
262 |
|
{ |
263 |
|
qsamplerInstrument instrument; |
264 |
|
|
265 |
|
InstrumentForm form(this); |
266 |
|
form.setup(&instrument); |
267 |
|
if (!form.exec()) |
268 |
|
return; |
269 |
|
|
270 |
|
// Commit... |
271 |
|
instrument.mapInstrument(); |
272 |
|
// add new item to the table model |
273 |
|
m_model.resort(instrument); |
274 |
|
// Notify we've changes... |
275 |
|
//emit model.reset(); |
276 |
|
//FIXME: call above didnt really refresh, so we use this for now ... |
277 |
|
refreshInstruments(); |
278 |
|
} |
279 |
|
|
280 |
|
|
281 |
|
void InstrumentListForm::deleteInstrument (void) |
282 |
|
{ |
283 |
|
const QModelIndex& index = m_ui.InstrumentTable->currentIndex(); |
284 |
|
if (!index.isValid() || !index.data(Qt::UserRole).isValid()) |
285 |
|
return; |
286 |
|
|
287 |
|
qsamplerInstrument *pInstrument = |
288 |
|
static_cast<qsamplerInstrument*> ( |
289 |
|
index.data(Qt::UserRole).value<void *> ()); |
290 |
|
|
291 |
|
if (pInstrument == NULL) |
292 |
|
return; |
293 |
|
|
294 |
|
MainForm *pMainForm = MainForm::getInstance(); |
295 |
|
if (pMainForm == NULL) |
296 |
|
return; |
297 |
|
|
298 |
|
// Prompt user if this is for real... |
299 |
|
qsamplerOptions *pOptions = pMainForm->options(); |
300 |
|
if (pOptions && pOptions->bConfirmRemove) { |
301 |
|
if (QMessageBox::warning(this, |
302 |
|
QSAMPLER_TITLE ": " + tr("Warning"), |
303 |
|
tr("About to delete instrument map entry:\n\n" |
304 |
|
"%1\n\n" |
305 |
|
"Are you sure?") |
306 |
|
.arg(pInstrument->name()), |
307 |
|
tr("OK"), tr("Cancel")) > 0) |
308 |
|
return; |
309 |
|
} |
310 |
|
|
311 |
|
pInstrument->unmapInstrument(); |
312 |
|
// let the instrument vanish from the table model |
313 |
|
m_model.removeInstrument(*pInstrument); |
314 |
|
// Notify we've changes... |
315 |
|
emit m_model.reset(); |
316 |
|
} |
317 |
|
|
318 |
|
|
319 |
|
// Update form actions enablement... |
320 |
|
void InstrumentListForm::stabilizeForm (void) |
321 |
|
{ |
322 |
|
MainForm *pMainForm = MainForm::getInstance(); |
323 |
|
|
324 |
|
bool bEnabled = (pMainForm && pMainForm->client()); |
325 |
|
m_ui.newInstrumentAction->setEnabled(bEnabled); |
326 |
|
const QModelIndex& index = m_ui.InstrumentTable->currentIndex(); |
327 |
|
bEnabled = (bEnabled && index.isValid()); |
328 |
|
m_ui.editInstrumentAction->setEnabled(bEnabled); |
329 |
|
m_ui.deleteInstrumentAction->setEnabled(bEnabled); |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
|
// Handle custom context menu here... |
334 |
|
void InstrumentListForm::contextMenu ( const QPoint& pos ) |
335 |
|
{ |
336 |
|
if (!m_ui.newInstrumentAction->isEnabled()) |
337 |
|
return; |
338 |
|
|
339 |
|
m_ui.contextMenu->exec( |
340 |
|
(m_ui.InstrumentTable->viewport())->mapToGlobal(pos)); |
341 |
} |
} |
342 |
|
|
343 |
|
|
344 |
} // namespace QSampler |
} // namespace QSampler |
345 |
|
|
346 |
|
|
347 |
|
// end of qsamplerInstrumentListForm.cpp |