| 1 |
/* |
| 2 |
* Copyright (C) 2018-2021 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "instrument_selection_dialog.hpp" |
| 27 |
#include "ui_instrument_selection_dialog.h" |
| 28 |
#include <stdexcept> |
| 29 |
#include <QKeyEvent> |
| 30 |
#include "bank.hpp" |
| 31 |
#include "gui/jam_layout.hpp" |
| 32 |
|
| 33 |
InstrumentSelectionDialog::InstrumentSelectionDialog(const AbstractBank &bank, const QString &text, |
| 34 |
std::weak_ptr<Configuration> config, QWidget *parent) |
| 35 |
: QDialog(parent), bank_(bank), config_(config), ui_(new Ui::InstrumentSelectionDialog) |
| 36 |
{ |
| 37 |
ui_->setupUi(this); |
| 38 |
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); |
| 39 |
ui_->label->setText(text); |
| 40 |
setupContents(); |
| 41 |
|
| 42 |
ui_->listWidget->installEventFilter(this); |
| 43 |
} |
| 44 |
|
| 45 |
InstrumentSelectionDialog::~InstrumentSelectionDialog() |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
void InstrumentSelectionDialog::setupContents() |
| 50 |
{ |
| 51 |
QListWidget* lw = ui_->listWidget; |
| 52 |
lw->setSelectionMode(QListWidget::MultiSelection); |
| 53 |
|
| 54 |
size_t instCount = bank_.getNumInstruments(); |
| 55 |
for (size_t i = 0; i < instCount; ++i) { |
| 56 |
QString id = QString::fromStdString(bank_.getInstrumentIdentifier(i)); |
| 57 |
QString name = QString::fromStdString(bank_.getInstrumentName(i)); |
| 58 |
|
| 59 |
QListWidgetItem* item = new QListWidgetItem(QString("%1 %2").arg(id, name)); |
| 60 |
item->setData(Qt::UserRole, static_cast<qulonglong>(i)); |
| 61 |
lw->addItem(item); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
QVector<size_t> InstrumentSelectionDialog::currentInstrumentSelection() const |
| 66 |
{ |
| 67 |
QListWidget *lw = ui_->listWidget; |
| 68 |
const QList<QListWidgetItem *> items = lw->selectedItems(); |
| 69 |
|
| 70 |
QVector<size_t> selection; |
| 71 |
selection.reserve(items.size()); |
| 72 |
|
| 73 |
for (const QListWidgetItem* item : items) { |
| 74 |
size_t index = static_cast<size_t>(item->data(Qt::UserRole).toULongLong()); |
| 75 |
selection.push_back(index); |
| 76 |
} |
| 77 |
|
| 78 |
return selection; |
| 79 |
} |
| 80 |
|
| 81 |
void InstrumentSelectionDialog::onJamKeyOnByMidi(int key) |
| 82 |
{ |
| 83 |
auto item = ui_->listWidget->currentItem(); |
| 84 |
if (item) emit jamKeyOnMidiEvent(static_cast<size_t>(item->data(Qt::UserRole).toULongLong()), key); |
| 85 |
} |
| 86 |
|
| 87 |
void InstrumentSelectionDialog::onJamKeyOffByMidi(int key) |
| 88 |
{ |
| 89 |
auto item = ui_->listWidget->currentItem(); |
| 90 |
if (item) emit jamKeyOffMidiEvent(key); |
| 91 |
} |
| 92 |
|
| 93 |
bool InstrumentSelectionDialog::eventFilter(QObject* watched, QEvent* event) |
| 94 |
{ |
| 95 |
Q_UNUSED(watched) // Only list widget |
| 96 |
|
| 97 |
auto item = ui_->listWidget->currentItem(); |
| 98 |
if (!item) return false; |
| 99 |
size_t n = static_cast<size_t>(item->data(Qt::UserRole).toULongLong()); |
| 100 |
|
| 101 |
if (event->type() == QEvent::KeyPress) { |
| 102 |
auto ke = reinterpret_cast<QKeyEvent*>(event); |
| 103 |
if (!ke->isAutoRepeat()) { |
| 104 |
Qt::Key qtKey = static_cast<Qt::Key>(ke->key()); |
| 105 |
try { |
| 106 |
JamKey jk = getJamKeyFromLayoutMapping(qtKey, config_); |
| 107 |
emit jamKeyOnEvent(n, jk); |
| 108 |
} catch (std::invalid_argument&) {} |
| 109 |
} |
| 110 |
} |
| 111 |
else if (event->type() == QEvent::KeyRelease) { |
| 112 |
auto ke = reinterpret_cast<QKeyEvent*>(event); |
| 113 |
if (!ke->isAutoRepeat()) { |
| 114 |
Qt::Key qtKey = static_cast<Qt::Key>(ke->key()); |
| 115 |
try { |
| 116 |
JamKey jk = getJamKeyFromLayoutMapping(qtKey, config_); |
| 117 |
emit jamKeyOffEvent(jk); |
| 118 |
} catch (std::invalid_argument&) {} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
void InstrumentSelectionDialog::on_searchLineEdit_textChanged(const QString &search) |
| 126 |
{ |
| 127 |
QListWidget *lw = ui_->listWidget; |
| 128 |
unsigned count = static_cast<unsigned>(lw->count()); |
| 129 |
|
| 130 |
for (unsigned row = 0; row < count; ++row) { |
| 131 |
QListWidgetItem *item = lw->item(static_cast<int>(row)); |
| 132 |
bool accept = search.isEmpty() || item->text().contains(search, Qt::CaseInsensitive); |
| 133 |
item->setHidden(!accept); |
| 134 |
} |
| 135 |
} |