| 1 |
/* |
| 2 |
* Copyright (C) 2018-2019 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 "groove_settings_dialog.hpp" |
| 27 |
#include "ui_groove_settings_dialog.h" |
| 28 |
#include <algorithm> |
| 29 |
#include <numeric> |
| 30 |
#include <QRegularExpression> |
| 31 |
#include <QRegularExpressionMatch> |
| 32 |
#include <QApplication> |
| 33 |
#include <QClipboard> |
| 34 |
#include "utils.hpp" |
| 35 |
|
| 36 |
GrooveSettingsDialog::GrooveSettingsDialog(QWidget *parent) : |
| 37 |
QDialog(parent), |
| 38 |
ui(new Ui::GrooveSettingsDialog) |
| 39 |
{ |
| 40 |
ui->setupUi(this); |
| 41 |
|
| 42 |
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); |
| 43 |
ui->grooveListWidget->setSelectionMode(QAbstractItemView::SingleSelection); |
| 44 |
} |
| 45 |
|
| 46 |
GrooveSettingsDialog::~GrooveSettingsDialog() |
| 47 |
{ |
| 48 |
delete ui; |
| 49 |
} |
| 50 |
|
| 51 |
void GrooveSettingsDialog::setGrooveSquences(std::vector<std::vector<int>> seqs) |
| 52 |
{ |
| 53 |
seqs_ = seqs; |
| 54 |
|
| 55 |
for (size_t i = 0; i < seqs_.size(); ++i) { |
| 56 |
auto text = QString("%1: ").arg(i, 2, 16, QChar('0')).toUpper(); |
| 57 |
for (auto& g : seqs_[i]) { |
| 58 |
text = text + QString::number(g) + " "; |
| 59 |
} |
| 60 |
ui->grooveListWidget->addItem(text); |
| 61 |
} |
| 62 |
|
| 63 |
ui->grooveListWidget->setCurrentRow(0); |
| 64 |
ui->removeButton->setEnabled(ui->grooveListWidget->count() > 1); |
| 65 |
} |
| 66 |
|
| 67 |
std::vector<std::vector<int>> GrooveSettingsDialog::getGrooveSequences() |
| 68 |
{ |
| 69 |
return seqs_; |
| 70 |
} |
| 71 |
|
| 72 |
void GrooveSettingsDialog::keyPressEvent(QKeyEvent* event) |
| 73 |
{ |
| 74 |
switch (event->key()) { |
| 75 |
case Qt::Key_Return: |
| 76 |
case Qt::Key_Enter: |
| 77 |
// Prevent dialog from closing when it finished line edit |
| 78 |
break; |
| 79 |
default: |
| 80 |
QDialog::keyPressEvent(event); |
| 81 |
break; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void GrooveSettingsDialog::on_addButton_clicked() |
| 86 |
{ |
| 87 |
ui->grooveListWidget->addItem(QString("%1: 6 6").arg(seqs_.size(), 2, 16, QChar('0')).toUpper()); |
| 88 |
seqs_.push_back({ 6, 6}); |
| 89 |
ui->removeButton->setEnabled(true); |
| 90 |
ui->grooveListWidget->setCurrentRow(ui->grooveListWidget->count() - 1); |
| 91 |
if (ui->grooveListWidget->count() == 128) ui->addButton->setEnabled(false); |
| 92 |
} |
| 93 |
|
| 94 |
void GrooveSettingsDialog::on_removeButton_clicked() |
| 95 |
{ |
| 96 |
int row = ui->grooveListWidget->currentRow(); |
| 97 |
if (row == -1) return; |
| 98 |
|
| 99 |
seqs_.erase(seqs_.begin() + row); |
| 100 |
delete ui->grooveListWidget->takeItem(row); |
| 101 |
for (int i = row; i < ui->grooveListWidget->count(); ++i) { |
| 102 |
QString text = QString::number(i) + ": "; |
| 103 |
for (auto& g : seqs_[static_cast<size_t>(i)]) { |
| 104 |
text = text + QString::number(g) + " "; |
| 105 |
} |
| 106 |
ui->grooveListWidget->item(i)->setText(text); |
| 107 |
} |
| 108 |
|
| 109 |
on_grooveListWidget_currentRowChanged(ui->grooveListWidget->currentRow()); |
| 110 |
|
| 111 |
ui->addButton->setEnabled(true); |
| 112 |
if (ui->grooveListWidget->count() == 1) |
| 113 |
ui->removeButton->setEnabled(false); |
| 114 |
} |
| 115 |
|
| 116 |
void GrooveSettingsDialog::on_lineEdit_editingFinished() |
| 117 |
{ |
| 118 |
int row = ui->grooveListWidget->currentRow(); |
| 119 |
if (row == -1) return; |
| 120 |
|
| 121 |
std::vector<int> seq; |
| 122 |
QString text = ui->lineEdit->text(); |
| 123 |
while (!text.isEmpty()) { |
| 124 |
QRegularExpressionMatch m = QRegularExpression("^(\\d+)").match(text); |
| 125 |
if (m.hasMatch()) { |
| 126 |
seq.push_back(m.captured(1).toInt()); |
| 127 |
text.remove(QRegularExpression("^\\d+")); |
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
m = QRegularExpression("^ +").match(text); |
| 132 |
if (m.hasMatch()) { |
| 133 |
text.remove(QRegularExpression("^ +")); |
| 134 |
continue; |
| 135 |
} |
| 136 |
|
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
if (seq.empty()) return; |
| 141 |
|
| 142 |
seqs_.at(static_cast<size_t>(row)) = std::move(seq); |
| 143 |
changeSequence(row); |
| 144 |
} |
| 145 |
|
| 146 |
void GrooveSettingsDialog::on_grooveListWidget_currentRowChanged(int currentRow) |
| 147 |
{ |
| 148 |
if (currentRow > -1) updateSequence(static_cast<size_t>(currentRow)); |
| 149 |
} |
| 150 |
|
| 151 |
void GrooveSettingsDialog::changeSequence(int seqNum) |
| 152 |
{ |
| 153 |
QString text = updateSequence(static_cast<size_t>(seqNum)); |
| 154 |
ui->grooveListWidget->item(seqNum)->setText(QString("%1: ").arg(seqNum, 2, 16, QChar('0')).toUpper() + text); |
| 155 |
} |
| 156 |
|
| 157 |
QString GrooveSettingsDialog::updateSequence(size_t seqNum) |
| 158 |
{ |
| 159 |
ui->seqListWidget->clear(); |
| 160 |
QString text; |
| 161 |
auto& seq = seqs_.at(seqNum); |
| 162 |
for (size_t i = 0; i < seq.size(); ++i) { |
| 163 |
ui->seqListWidget->addItem(QString("%1: %2").arg(i, 2, 16, QChar('0')).toUpper().arg(seq[i])); |
| 164 |
text = text + QString::number(seq[i]) + " "; |
| 165 |
} |
| 166 |
ui->seqListWidget->setCurrentRow(0); |
| 167 |
ui->lineEdit->setText(text); |
| 168 |
double speed = std::accumulate(seq.begin(), seq.end(), 0) / static_cast<double>(seq.size()); |
| 169 |
ui->speedLabel->setText(tr("Speed: %1").arg(QString::number(speed, 'f', 3))); |
| 170 |
return text; |
| 171 |
} |
| 172 |
|
| 173 |
void GrooveSettingsDialog::on_upToolButton_clicked() |
| 174 |
{ |
| 175 |
int curRow = ui->seqListWidget->currentRow(); |
| 176 |
if (!curRow) return; |
| 177 |
|
| 178 |
swapSequenceItem(static_cast<size_t>(ui->grooveListWidget->currentRow()), curRow - 1, curRow); |
| 179 |
ui->seqListWidget->setCurrentRow(curRow - 1); |
| 180 |
} |
| 181 |
|
| 182 |
void GrooveSettingsDialog::on_downToolButton_clicked() |
| 183 |
{ |
| 184 |
int curRow = ui->seqListWidget->currentRow(); |
| 185 |
if (curRow == ui->seqListWidget->count() - 1) return; |
| 186 |
|
| 187 |
swapSequenceItem(static_cast<size_t>(ui->grooveListWidget->currentRow()), curRow, curRow + 1); |
| 188 |
ui->seqListWidget->setCurrentRow(curRow + 1); |
| 189 |
} |
| 190 |
|
| 191 |
void GrooveSettingsDialog::swapSequenceItem(size_t seqNum, int index1, int index2) |
| 192 |
{ |
| 193 |
std::iter_swap(seqs_.at(seqNum).begin() + index1, seqs_.at(seqNum).begin() + index2); |
| 194 |
changeSequence(static_cast<int>(seqNum)); |
| 195 |
} |
| 196 |
|
| 197 |
void GrooveSettingsDialog::on_expandPushButton_clicked() |
| 198 |
{ |
| 199 |
size_t id = static_cast<size_t>(ui->grooveListWidget->currentRow()); |
| 200 |
auto& ref = seqs_[id]; |
| 201 |
if (utils::find(ref, 1) != ref.end()) return; |
| 202 |
|
| 203 |
std::vector<int> seq; |
| 204 |
for (auto v : ref) { |
| 205 |
int tmp = v / 2; |
| 206 |
seq.push_back(v - tmp); |
| 207 |
seq.push_back(tmp); |
| 208 |
} |
| 209 |
seqs_.at(id) = std::move(seq); |
| 210 |
changeSequence(static_cast<int>(id)); |
| 211 |
} |
| 212 |
|
| 213 |
void GrooveSettingsDialog::on_shrinkPushButton_clicked() |
| 214 |
{ |
| 215 |
size_t id = static_cast<size_t>(ui->grooveListWidget->currentRow()); |
| 216 |
auto& ref = seqs_[id]; |
| 217 |
if (ref.size() % 2) return; |
| 218 |
|
| 219 |
std::vector<int> seq; |
| 220 |
for (auto it = ref.begin(); it != ref.end(); it += 2) seq.push_back(*it + *(it + 1)); |
| 221 |
seqs_.at(id) = std::move(seq); |
| 222 |
changeSequence(static_cast<int>(id)); |
| 223 |
} |
| 224 |
|
| 225 |
void GrooveSettingsDialog::on_genPushButton_clicked() |
| 226 |
{ |
| 227 |
int num = ui->numeratorSpinBox->value(); |
| 228 |
int denom = ui->denominatorSpinBox->value(); |
| 229 |
if (num < denom) return; |
| 230 |
|
| 231 |
std::vector<int> seq(static_cast<size_t>(denom)); |
| 232 |
for (int i = 0; i < num * denom; i += num) |
| 233 |
seq.at(static_cast<size_t>(denom - i / num - 1)) = (i + num) / denom - i / denom; |
| 234 |
seqs_.at(static_cast<size_t>(ui->grooveListWidget->currentRow())) = std::move(seq); |
| 235 |
changeSequence(ui->grooveListWidget->currentRow()); |
| 236 |
} |
| 237 |
|
| 238 |
void GrooveSettingsDialog::on_padPushButton_clicked() |
| 239 |
{ |
| 240 |
int pad = ui->padSpinBox->value(); |
| 241 |
size_t id = static_cast<size_t>(ui->grooveListWidget->currentRow()); |
| 242 |
auto& ref = seqs_[id]; |
| 243 |
if (std::any_of(ref.begin(), ref.end(), [pad](int x) {return (x <= pad); })) return; |
| 244 |
|
| 245 |
std::vector<int> seq; |
| 246 |
for (auto v : ref) { |
| 247 |
seq.push_back(v - pad); |
| 248 |
seq.push_back(pad); |
| 249 |
} |
| 250 |
seqs_.at(id) = std::move(seq); |
| 251 |
changeSequence(static_cast<int>(id)); |
| 252 |
} |
| 253 |
|
| 254 |
void GrooveSettingsDialog::on_copyPushButton_clicked() |
| 255 |
{ |
| 256 |
auto& seq = seqs_[static_cast<size_t>(ui->grooveListWidget->currentRow())]; |
| 257 |
auto text = QString("PATTERN_COPY:3,2,%1,").arg(seq.size()); |
| 258 |
for (auto v : seq) text += QString("0F,%1,").arg(v); |
| 259 |
|
| 260 |
QApplication::clipboard()->setText(text); |
| 261 |
} |