-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatchesDialog.cpp
More file actions
483 lines (401 loc) · 13.5 KB
/
PatchesDialog.cpp
File metadata and controls
483 lines (401 loc) · 13.5 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* PatchesDialog.cpp - display sf2 patches
*
* Copyright (c) 2008 Paul Giblock <drfaygo/at/gmail/dot/com>
*
* This file is part of LMMS - https://lmms.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "PatchesDialog.h"
#include <fluidsynth.h>
#include <QHeaderView>
//#include <QFileInfo>
#include <QLabel>
#include <QKeyEvent>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QStandardItem>
#include "embed.h"
#include "fluidsynthshims.h"
namespace lmms::gui
{
// Custom list-view item (as for numerical sort purposes...)
class PatchItem : public QTreeWidgetItem
{
public:
// Constructor.
PatchItem( QTreeWidget *pListView,
QTreeWidgetItem *pItemAfter )
: QTreeWidgetItem( pListView, pItemAfter ) {}
// Sort/compare overriden method.
bool operator< ( const QTreeWidgetItem& other ) const override
{
int iColumn = QTreeWidgetItem::treeWidget()->sortColumn();
const QString& s1 = text( iColumn );
const QString& s2 = other.text( iColumn );
if( iColumn == 0 || iColumn == 2 )
{
return( s1.toInt() < s2.toInt() );
}
else
{
return( s1 < s2 );
}
}
};
// Constructor.
PatchesDialog::PatchesDialog( QWidget *pParent, Qt::WindowFlags wflags )
: QDialog(pParent, wflags)
{
// Setup UI struct...
setupUi( this );
m_pSynth = nullptr;
m_iChan = 0;
m_iBank = 0;
m_iProg = 0;
// Configure bank list view
auto bankHeader = m_bankListView->header();
bankHeader->setSectionResizeMode(0, QHeaderView::Stretch);
bankHeader->setStretchLastSection(true);
bankHeader->resizeSection(0, 30);
m_splitter->setStretchFactor(0, 2);
m_splitter->setStretchFactor(1, 6);
// Configure program list models
m_progListSourceModel.setHorizontalHeaderLabels({tr("Patch"), tr("Name")});
m_progListProxyModel.setSourceModel(&m_progListSourceModel);
m_progListProxyModel.setFilterCaseSensitivity(Qt::CaseInsensitive);
m_progListProxyModel.setFilterKeyColumn(1); // "Name" column
m_progListProxyModel.setDynamicSortFilter(true);
// Configure program list view
m_progListView->setModel(&m_progListProxyModel);
m_progListView->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_progListView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_progListView->setSelectionMode(QAbstractItemView::SingleSelection);
m_progListView->setSortingEnabled(true);
m_progListView->sortByColumn(0, Qt::AscendingOrder); // Initial sort by column 0 (Name)
constexpr int rowHeight = 14;
auto progVHeader = m_progListView->verticalHeader();
// progVHeader->setSectionResizeMode(QHeaderView::ResizeToContents);
progVHeader->setSectionResizeMode(QHeaderView::Fixed);
progVHeader->setMinimumSectionSize(rowHeight);
progVHeader->setMaximumSectionSize(rowHeight);
progVHeader->setDefaultSectionSize(rowHeight);
progVHeader->hide();
auto progHeader = m_progListView->horizontalHeader();
progHeader->setDefaultAlignment(Qt::AlignLeft);
progHeader->setSectionResizeMode(0, QHeaderView::ResizeToContents);
progHeader->setSectionResizeMode(1, QHeaderView::Stretch);
progHeader->setSectionsMovable(false);
progHeader->setStretchLastSection(true);
// Initial sort order...
m_bankListView->sortItems(0, Qt::AscendingOrder);
m_filterEdit->setPlaceholderText(tr("Search"));
m_filterEdit->setClearButtonEnabled(true);
m_filterEdit->addAction(embed::getIconPixmap("zoom"), QLineEdit::LeadingPosition);
// Configure focus (only allow for search bar and dialog buttons)
m_filterEdit->setFocus();
m_filterEdit->setFocusPolicy(Qt::StrongFocus);
m_bankListView->setFocusPolicy(Qt::NoFocus);
m_progListView->setFocusPolicy(Qt::NoFocus);
// UI connections...
QObject::connect(m_filterEdit, &QLineEdit::textChanged, this, [this](const QString& text) {
m_progListProxyModel.setFilterRegularExpression(
QRegularExpression(text, QRegularExpression::CaseInsensitiveOption));
diffSelectProgRow(0); // just fix the selection if it has been invalidated
});
QObject::connect(m_bankListView,
SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
SLOT(bankChanged()));
QObject::connect(m_progListView,
&QTableView::doubleClicked, this, &PatchesDialog::accept);
QObject::connect(m_progListView->selectionModel(),
&QItemSelectionModel::currentRowChanged, this,
[this](auto& cur, auto& prev) { progChanged(cur, prev); });
QObject::connect(m_okButton,
SIGNAL(clicked()),
SLOT(accept()));
QObject::connect(m_cancelButton,
SIGNAL(clicked()),
SLOT(reject()));
installEventFilter(this);
}
// Dialog setup loader.
void PatchesDialog::setup ( fluid_synth_t * pSynth, int iChan,
const QString & _chanName,
LcdSpinBoxModel * _bankModel,
LcdSpinBoxModel * _progModel,
QLabel * _patchLabel )
{
// We'll going to changes the whole thing...
m_dirty = 0;
m_bankModel = _bankModel;
m_progModel = _progModel;
m_patchLabel = _patchLabel;
// Set the proper caption...
setWindowTitle( _chanName + " - Soundfont patches" );
// set m_pSynth to NULL so we don't trigger any progChanged events
m_pSynth = nullptr;
// Load bank list from actual synth stack...
m_bankListView->setSortingEnabled(false);
m_bankListView->clear();
// now it should be safe to set internal stuff
m_pSynth = pSynth;
m_iChan = iChan;
QTreeWidgetItem *pBankItem = nullptr;
// For all soundfonts (in reversed stack order) fill the available banks...
int cSoundFonts = ::fluid_synth_sfcount(m_pSynth);
for (int i = 0; i < cSoundFonts; i++) {
fluid_sfont_t *pSoundFont = ::fluid_synth_get_sfont(m_pSynth, i);
if (pSoundFont) {
#ifdef CONFIG_FLUID_BANK_OFFSET
int iBankOffset = ::fluid_synth_get_bank_offset(m_pSynth, fluid_sfont_get_id(pSoundFont));
#endif
fluid_sfont_iteration_start(pSoundFont);
#if FLUIDSYNTH_VERSION_MAJOR < 2
fluid_preset_t preset;
fluid_preset_t *pCurPreset = &preset;
#else
fluid_preset_t *pCurPreset = nullptr;
#endif
while ((pCurPreset = fluid_sfont_iteration_next_wrapper(pSoundFont, pCurPreset))) {
int iBank = fluid_preset_get_banknum(pCurPreset);
#ifdef CONFIG_FLUID_BANK_OFFSET
iBank += iBankOffset;
#endif
if (!findBankItem(iBank)) {
pBankItem = new PatchItem(m_bankListView, pBankItem);
if (pBankItem)
pBankItem->setText(0, QString::number(iBank));
}
}
}
}
m_bankListView->setSortingEnabled(true);
// Set the selected bank.
m_iBank = 0;
fluid_preset_t *pPreset = ::fluid_synth_get_channel_preset(m_pSynth, m_iChan);
if (pPreset) {
m_iBank = fluid_preset_get_banknum(pPreset);
#ifdef CONFIG_FLUID_BANK_OFFSET
m_iBank += ::fluid_synth_get_bank_offset(m_pSynth, fluid_sfont_get_id(fluid_preset_get_sfont(sfont)));
#endif
}
pBankItem = findBankItem(m_iBank);
m_bankListView->setCurrentItem(pBankItem);
m_bankListView->scrollToItem(pBankItem);
bankChanged();
// Set the selected program.
if (pPreset)
m_iProg = fluid_preset_get_num(pPreset);
if (auto progItem = findProgItem(m_iProg); progItem != nullptr)
{
auto sourceIdx = progItem->index();
auto proxyIdx = m_progListProxyModel.mapFromSource(sourceIdx);
if (proxyIdx.isValid())
{
int row = proxyIdx.row();
auto idx = m_progListView->model()->index(row, 0);
m_progListView->selectionModel()->setCurrentIndex(idx,
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
m_progListView->scrollTo(idx);
}
}
// Done with setup...
//m_iDirtySetup--;
}
// Stabilize current state form.
void PatchesDialog::stabilizeForm()
{
m_okButton->setEnabled(validateForm());
}
// Validate form fields.
bool PatchesDialog::validateForm()
{
bool bValid = true;
bValid = bValid && (m_bankListView->currentItem() != nullptr);
bValid = bValid && (m_selProg != -1);
return bValid;
}
// Realize a bank-program selection preset.
void PatchesDialog::setBankProg ( int iBank, int iProg )
{
if (m_pSynth == nullptr)
return;
// just select the synth's program preset...
::fluid_synth_bank_select(m_pSynth, m_iChan, iBank);
::fluid_synth_program_change(m_pSynth, m_iChan, iProg);
// Maybe this is needed to stabilize things around.
::fluid_synth_program_reset(m_pSynth);
}
// Validate form fields and accept it valid.
void PatchesDialog::accept()
{
if (validateForm()) {
bool updateUi = m_dirty > 0;
updatePatch(updateUi);
// Do remember preview state...
// if (m_pOptions)
// m_pOptions->bPresetPreview = m_ui.PreviewCheckBox->isChecked();
// We got it.
QDialog::accept();
}
}
// Reject settings (Cancel button slot).
void PatchesDialog::reject ()
{
// Reset selection to initial selection, if applicable...
if (m_dirty > 0)
setBankProg(m_bankModel->value(), m_progModel->value());
// Done (hopefully nothing).
QDialog::reject();
}
// Find the bank item of given bank number id.
QTreeWidgetItem *PatchesDialog::findBankItem ( int iBank )
{
QList<QTreeWidgetItem *> banks
= m_bankListView->findItems(
QString::number(iBank), Qt::MatchExactly, 0);
QListIterator<QTreeWidgetItem *> iter(banks);
if (iter.hasNext())
return iter.next();
else
return nullptr;
}
QStandardItem *PatchesDialog::findProgItem(int iProg)
{
QList<QStandardItem*> progs = m_progListSourceModel.findItems(
QString::number(iProg), Qt::MatchExactly, 0);
QListIterator<QStandardItem*> iter(progs);
if (iter.hasNext())
return iter.next();
else
return nullptr;
}
// Bank change slot.
void PatchesDialog::bankChanged ()
{
if (m_pSynth == nullptr)
return;
QTreeWidgetItem *pBankItem = m_bankListView->currentItem();
if (pBankItem == nullptr)
return;
int iBankSelected = pBankItem->text(0).toInt();
// Clear up the program list to refill
m_progListView->setSortingEnabled(false);
m_progListSourceModel.setRowCount(0);
// For all soundfonts (in reversed stack order) fill the available programs...
bool stop = false; // replaces the `pProgItem` check that used to exist here
int cSoundFonts = ::fluid_synth_sfcount(m_pSynth);
for (int i = 0; i < cSoundFonts && !stop; i++) {
fluid_sfont_t *pSoundFont = ::fluid_synth_get_sfont(m_pSynth, i);
if (pSoundFont) {
#ifdef CONFIG_FLUID_BANK_OFFSET
int iBankOffset = ::fluid_synth_get_bank_offset(m_pSynth, fluid_sfont_get_id(pSoundFont));
#endif
fluid_sfont_iteration_start(pSoundFont);
#if FLUIDSYNTH_VERSION_MAJOR < 2
fluid_preset_t preset;
fluid_preset_t *pCurPreset = &preset;
#else
fluid_preset_t *pCurPreset = nullptr;
#endif
while ((pCurPreset = fluid_sfont_iteration_next_wrapper(pSoundFont, pCurPreset))) {
int iBank = fluid_preset_get_banknum(pCurPreset);
#ifdef CONFIG_FLUID_BANK_OFFSET
iBank += iBankOffset;
#endif
int iProg = fluid_preset_get_num(pCurPreset);
if (iBank == iBankSelected && !findProgItem(iProg)) {
// Numeric value on the batch number column - allows for numerical sorting
auto patchNumItem = new QStandardItem();
patchNumItem->setData(iProg, Qt::DisplayRole);
auto patchNameItem = new QStandardItem(fluid_preset_get_name(pCurPreset));
stop = true;
m_progListSourceModel.appendRow({patchNumItem, patchNameItem});
// Old columns:
// Col. 2: QString::number(fluid_sfont_get_id(pSoundFont))
// Col. 3: QFileInfo(fluid_sfont_get_name(pSoundFont).baseName())
}
}
}
}
m_progListView->setSortingEnabled(true);
// Stabilize the form.
stabilizeForm();
}
void PatchesDialog::updatePatch(bool updateUi)
{
if (m_selProg < 0)
return;
int iBank = m_bankListView->currentItem()->text(0).toInt();
setBankProg(iBank, m_selProg);
if (updateUi)
{
m_bankModel->setValue(iBank);
m_progModel->setValue(m_selProg);
m_patchLabel->setText(m_selProgName);
}
}
// Program change slot.
void PatchesDialog::progChanged(const QModelIndex& cur, const QModelIndex& prev)
{
if (m_pSynth == nullptr)
return;
auto curRow = m_progListProxyModel.mapToSource(cur).row();
if (curRow < 0)
return;
auto progIdx = m_progListSourceModel.index(curRow, 0);
m_selProg = m_progListSourceModel.data(progIdx).toInt();
auto nameIdx = m_progListSourceModel.index(curRow, 1);
m_selProgName = m_progListSourceModel.data(nameIdx).toString();
// Which preview state...
if (validateForm())
{
updatePatch(false);
// Now we're dirty nuff.
m_dirty++;
}
// Stabilize the form.
stabilizeForm();
}
bool PatchesDialog::eventFilter(QObject *obj, QEvent *event)
{
if (obj == this && event->type() == QEvent::KeyPress)
{
auto key = static_cast<QKeyEvent*>(event)->key();
if (key == Qt::Key_Up || key == Qt::Key_Down)
{
int rowDiff = (key == Qt::Key_Up) ? -1 : +1;
diffSelectProgRow(rowDiff);
return true;
}
}
return QDialog::eventFilter(obj, event);
}
void PatchesDialog::diffSelectProgRow(int offset)
{
QItemSelectionModel* selectionModel = m_progListView->selectionModel();
int curRow = selectionModel->currentIndex().row();
int newRow = curRow + offset;
int rowCount = m_progListView->model()->rowCount();
newRow = qBound(0, newRow, rowCount - 1);
selectionModel->setCurrentIndex(m_progListView->model()->index(newRow, 0),
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
m_progListView->scrollTo(m_progListView->model()->index(newRow, 0));
}
} // namespace lmms::gui