Debian Codegraph @

BambooTracker/gui/key_signature_manager_form.cpp

raw
1 /*
2 * Copyright (C) 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 "key_signature_manager_form.hpp"
27 #include "ui_key_signature_manager_form.h"
28 #include "gui/note_name_manager.hpp"
29
30 KeySignatureManagerForm::KeySignatureManagerForm(std::weak_ptr<BambooTracker> core, bool showHex,
31 QWidget *parent)
32 : QWidget(parent),
33 ui(new Ui::KeySignatureManagerForm),
34 bt_(core),
35 curSong_(core.lock()->getCurrentSongNumber())
36 {
37 ui->setupUi(this);
38 setWindowFlags((windowFlags()
39 & ~(Qt::WindowContextHelpButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint))
40 | Qt::WindowStaysOnTopHint);
41
42 for (int i = KeySignature::FISRT; i <= KeySignature::LAST; ++i) {
43 ui->keyComboBox->addItem(NoteNameManager::getManager().getKeyName(static_cast<KeySignature::Type>(i)), i);
44 }
45
46 setNumberSettings(showHex);
47 initList();
48 ui->orderSpinBox->setDisplayIntegerBase(numBase_);
49 ui->stepSpinBox->setDisplayIntegerBase(numBase_);
50
51 insSc_ = std::make_unique<QShortcut>(Qt::Key_Insert, ui->listWidget, nullptr, nullptr, Qt::WidgetShortcut);
52 QObject::connect(insSc_.get(), &QShortcut::activated, this, &KeySignatureManagerForm::on_createPushButton_clicked);
53 delSc_ = std::make_unique<QShortcut>(Qt::Key_Delete, ui->listWidget, nullptr, nullptr, Qt::WidgetShortcut);
54 QObject::connect(delSc_.get(), &QShortcut::activated, this, &KeySignatureManagerForm::on_removePushButton_clicked);
55 }
56
57 KeySignatureManagerForm::~KeySignatureManagerForm()
58 {
59 delete ui;
60 }
61
62 void KeySignatureManagerForm::initList()
63 {
64 int size = static_cast<int>(bt_.lock()->getKeySignatureSize(curSong_));
65 for (int i = 0; i < size; ++i) {
66 KeySignature ks = bt_.lock()->getKeySignature(curSong_, i);
67 addKeySignature(ks.type, ks.order, ks.step, true);
68 }
69 }
70
71 void KeySignatureManagerForm::addKeySignature(KeySignature::Type key, int order, int step, bool onlyUi)
72 {
73 if (onlyUi) {
74 ui->listWidget->addItem(createText(key, order, step));
75 }
76 else {
77 bt_.lock()->addKeySignature(curSong_, key, order, step);
78 ui->listWidget->clear();
79 initList();
80 }
81 }
82
83 void KeySignatureManagerForm::removeKeySignature(int i)
84 {
85 if (!i) return;
86 delete ui->listWidget->takeItem(i);
87 bt_.lock()->removeKeySignature(curSong_, i);
88 }
89
90 QString KeySignatureManagerForm::createText(KeySignature::Type key, int order, int step)
91 {
92 auto pos = QString("%1,%2").arg(order, numWidth_, numBase_, QChar('0'))
93 .arg(step, numWidth_, numBase_, QChar('0')).toUpper();
94 return (pos + ": " + NoteNameManager::getManager().getKeyName(key));
95 }
96
97 void KeySignatureManagerForm::onCurrentSongNumberChanged()
98 {
99 curSong_ = bt_.lock()->getCurrentSongNumber();
100
101 ui->listWidget->clear();
102 initList();
103 }
104
105 void KeySignatureManagerForm::onConfigurationChanged(bool showHex)
106 {
107 for (int i = KeySignature::FISRT; i <= KeySignature::LAST; ++i) {
108 ui->keyComboBox->setItemText(i, NoteNameManager::getManager().getKeyName(static_cast<KeySignature::Type>(i)));
109 }
110
111 setNumberSettings(showHex);
112
113 for (int i = 0; i < ui->listWidget->count(); ++i) {
114 KeySignature ks = bt_.lock()->getKeySignature(curSong_, i);
115 ui->listWidget->item(i)->setText(createText(ks.type, ks.order, ks.step));
116 }
117
118 ui->orderSpinBox->setDisplayIntegerBase(numBase_);
119 ui->stepSpinBox->setDisplayIntegerBase(numBase_);
120 }
121
122 void KeySignatureManagerForm::on_createPushButton_clicked()
123 {
124 if (bt_.lock()->getKeySignatureSize(curSong_) == 127) return; // Maximum size
125 addKeySignature(static_cast<KeySignature::Type>(ui->keyComboBox->currentData().toInt()),
126 ui->orderSpinBox->value(), ui->stepSpinBox->value());
127 emit modified();
128 }
129
130 void KeySignatureManagerForm::on_updatePushButton_clicked()
131 {
132 int row = ui->listWidget->currentRow();
133 if (row == -1) return;
134
135 auto key = static_cast<KeySignature::Type>(ui->keyComboBox->currentData().toInt());
136 int order = ui->orderSpinBox->value();
137 int step = ui->stepSpinBox->value();
138 if (!row && (order || step)) bt_.lock()->addKeySignature(curSong_, key, order, step);
139 else bt_.lock()->changeKeySignature(curSong_, row, key, order, step);
140
141 ui->listWidget->clear();
142 initList();
143
144 emit modified();
145 }
146
147 void KeySignatureManagerForm::on_removePushButton_clicked()
148 {
149 int row = ui->listWidget->currentRow();
150 if (row < 2) return;
151
152 removeKeySignature(row);
153 emit modified();
154 }
155
156 void KeySignatureManagerForm::on_clearAllPushButton_clicked()
157 {
158 if (!ui->listWidget->count()) return;
159
160 ui->listWidget->clear();
161 bt_.lock()->clearKeySignature(curSong_);
162 initList();
163
164 emit modified();
165 }
166
167 void KeySignatureManagerForm::on_listWidget_currentRowChanged(int currentRow)
168 {
169 if (currentRow == -1) return;
170
171 KeySignature ks = bt_.lock()->getKeySignature(curSong_, currentRow);
172 for (int i = 0; i < ui->keyComboBox->count(); ++i) {
173 if (static_cast<KeySignature::Type>(ui->keyComboBox->itemData(i).toInt()) == ks.type) {
174 ui->keyComboBox->setCurrentIndex(i);
175 break;
176 }
177 }
178 ui->orderSpinBox->setValue(ks.order);
179 ui->stepSpinBox->setValue(ks.step);
180 }
181
182 void KeySignatureManagerForm::on_listWidget_itemDoubleClicked(QListWidgetItem* item)
183 {
184 int row = ui->listWidget->row(item);
185 KeySignature ks = bt_.lock()->getKeySignature(curSong_, row);
186 emit positionJumpRequested(ks.order, ks.step);
187 }