1 |
// qsamplerInstrumentListForm.cpp |
2 |
// |
3 |
/**************************************************************************** |
4 |
Copyright (C) 2003-2010, 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" |
25 |
|
26 |
#include "qsamplerInstrumentList.h" |
27 |
|
28 |
#include "qsamplerInstrumentForm.h" |
29 |
|
30 |
#include "qsamplerOptions.h" |
31 |
#include "qsamplerInstrument.h" |
32 |
#include "qsamplerMainForm.h" |
33 |
|
34 |
#include <QHeaderView> |
35 |
#include <QMessageBox> |
36 |
|
37 |
|
38 |
namespace QSampler { |
39 |
|
40 |
//------------------------------------------------------------------------- |
41 |
// QSampler::InstrumentListForm -- Instrument map list form implementation. |
42 |
// |
43 |
|
44 |
InstrumentListForm::InstrumentListForm ( |
45 |
QWidget* pParent, Qt::WindowFlags wflags ) |
46 |
: QMainWindow(pParent, wflags) |
47 |
{ |
48 |
m_ui.setupUi(this); |
49 |
|
50 |
// Setup toolbar widgets. |
51 |
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, |
69 |
SIGNAL(activated(int)), |
70 |
SLOT(activateMap(int))); |
71 |
QObject::connect( |
72 |
m_pInstrumentListView, |
73 |
SIGNAL(customContextMenuRequested(const QPoint&)), |
74 |
SLOT(contextMenu(const QPoint&))); |
75 |
QObject::connect( |
76 |
m_pInstrumentListView, |
77 |
SIGNAL(activated(const QModelIndex&)), |
78 |
SLOT(stabilizeForm())); |
79 |
QObject::connect( |
80 |
m_pInstrumentListView, |
81 |
SIGNAL(doubleClicked(const QModelIndex&)), |
82 |
SLOT(editInstrument(const QModelIndex&))); |
83 |
QObject::connect( |
84 |
m_ui.newInstrumentAction, |
85 |
SIGNAL(triggered()), |
86 |
SLOT(newInstrument())); |
87 |
QObject::connect( |
88 |
m_ui.deleteInstrumentAction, |
89 |
SIGNAL(triggered()), |
90 |
SLOT(deleteInstrument())); |
91 |
QObject::connect( |
92 |
m_ui.editInstrumentAction, |
93 |
SIGNAL(triggered()), |
94 |
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) |
106 |
{ |
107 |
delete m_pInstrumentListView; |
108 |
delete m_pMapComboBox; |
109 |
} |
110 |
|
111 |
|
112 |
// Notify our parent that we're emerging. |
113 |
void InstrumentListForm::showEvent ( QShowEvent *pShowEvent ) |
114 |
{ |
115 |
MainForm* pMainForm = MainForm::getInstance(); |
116 |
if (pMainForm) |
117 |
pMainForm->stabilizeForm(); |
118 |
|
119 |
QWidget::showEvent(pShowEvent); |
120 |
} |
121 |
|
122 |
|
123 |
// Notify our parent that we're closing. |
124 |
void InstrumentListForm::hideEvent ( QHideEvent *pHideEvent ) |
125 |
{ |
126 |
QWidget::hideEvent(pHideEvent); |
127 |
|
128 |
MainForm* pMainForm = MainForm::getInstance(); |
129 |
if (pMainForm) |
130 |
pMainForm->stabilizeForm(); |
131 |
} |
132 |
|
133 |
|
134 |
// Just about to notify main-window that we're closing. |
135 |
void InstrumentListForm::closeEvent ( QCloseEvent * /*pCloseEvent*/ ) |
136 |
{ |
137 |
QWidget::hide(); |
138 |
|
139 |
MainForm *pMainForm = MainForm::getInstance(); |
140 |
if (pMainForm) |
141 |
pMainForm->stabilizeForm(); |
142 |
} |
143 |
|
144 |
|
145 |
// Refresh all instrument list and views. |
146 |
void InstrumentListForm::refreshInstruments (void) |
147 |
{ |
148 |
MainForm* pMainForm = MainForm::getInstance(); |
149 |
if (pMainForm == NULL) |
150 |
return; |
151 |
|
152 |
Options *pOptions = pMainForm->options(); |
153 |
if (pOptions == NULL) |
154 |
return; |
155 |
|
156 |
// Get/save current map selection... |
157 |
int iMap = m_pMapComboBox->currentIndex(); |
158 |
if (iMap < 0 || m_pMapComboBox->count() < 2) |
159 |
iMap = pOptions->iMidiMap + 1; |
160 |
|
161 |
// Populate maps list. |
162 |
m_pMapComboBox->clear(); |
163 |
m_pMapComboBox->addItem(tr("(All)")); |
164 |
m_pMapComboBox->insertItems(1, Instrument::getMapNames()); |
165 |
|
166 |
// Adjust to saved selection... |
167 |
if (iMap < 0 || iMap >= m_pMapComboBox->count()) |
168 |
iMap = 0; |
169 |
m_pMapComboBox->setCurrentIndex(iMap); |
170 |
m_pMapComboBox->setEnabled(m_pMapComboBox->count() > 1); |
171 |
|
172 |
activateMap(iMap); |
173 |
} |
174 |
|
175 |
|
176 |
// Refresh instrument maps selector. |
177 |
void InstrumentListForm::activateMap ( int iMap ) |
178 |
{ |
179 |
MainForm* pMainForm = MainForm::getInstance(); |
180 |
if (pMainForm == NULL) |
181 |
return; |
182 |
|
183 |
Options *pOptions = pMainForm->options(); |
184 |
if (pOptions == NULL) |
185 |
return; |
186 |
|
187 |
int iMidiMap = iMap - 1; |
188 |
if (iMidiMap >= 0) |
189 |
pOptions->iMidiMap = iMidiMap; |
190 |
|
191 |
m_pInstrumentListView->setMidiMap(iMidiMap); |
192 |
m_pInstrumentListView->refresh(); |
193 |
|
194 |
stabilizeForm(); |
195 |
} |
196 |
|
197 |
|
198 |
void InstrumentListForm::editInstrument (void) |
199 |
{ |
200 |
editInstrument(m_pInstrumentListView->currentIndex()); |
201 |
} |
202 |
|
203 |
|
204 |
void InstrumentListForm::editInstrument ( const QModelIndex& index ) |
205 |
{ |
206 |
if (!index.isValid()) |
207 |
return; |
208 |
|
209 |
Instrument *pInstrument |
210 |
= static_cast<Instrument *> (index.internalPointer()); |
211 |
if (pInstrument == NULL) |
212 |
return; |
213 |
|
214 |
// Save current key values... |
215 |
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... |
222 |
InstrumentForm form(this); |
223 |
form.setup(&instrument); |
224 |
if (form.exec()) { |
225 |
// Commit... |
226 |
instrument.mapInstrument(); |
227 |
// Check whether we changed instrument key... |
228 |
if (instrument.map() == iMap && |
229 |
instrument.bank() == iBank && |
230 |
instrument.prog() == iProg) { |
231 |
// Just update tree item... |
232 |
//pItem->update(); |
233 |
} else { |
234 |
// Unmap old instance... |
235 |
Instrument(iMap, iBank, iProg).unmapInstrument(); |
236 |
// correct the position of the instrument in the model |
237 |
m_pInstrumentListView->updateInstrument(pInstrument); |
238 |
} |
239 |
} |
240 |
|
241 |
stabilizeForm(); |
242 |
} |
243 |
|
244 |
|
245 |
void InstrumentListForm::newInstrument (void) |
246 |
{ |
247 |
Instrument instrument; |
248 |
|
249 |
InstrumentForm form(this); |
250 |
form.setup(&instrument); |
251 |
if (!form.exec()) |
252 |
return; |
253 |
|
254 |
// Commit... |
255 |
instrument.mapInstrument(); |
256 |
|
257 |
// add new item to the table model |
258 |
m_pInstrumentListView->addInstrument( |
259 |
instrument.map(), |
260 |
instrument.bank(), |
261 |
instrument.prog()); |
262 |
|
263 |
stabilizeForm(); |
264 |
} |
265 |
|
266 |
|
267 |
void InstrumentListForm::deleteInstrument (void) |
268 |
{ |
269 |
const QModelIndex& index = m_pInstrumentListView->currentIndex(); |
270 |
if (!index.isValid()) |
271 |
return; |
272 |
|
273 |
Instrument *pInstrument |
274 |
= static_cast<Instrument *> (index.internalPointer()); |
275 |
if (pInstrument == NULL) |
276 |
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(); |
297 |
|
298 |
// let the instrument vanish from the table model |
299 |
m_pInstrumentListView->removeInstrument(pInstrument); |
300 |
|
301 |
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 |
331 |
|
332 |
|
333 |
// end of qsamplerInstrumentListForm.cpp |