Debian Codegraph @

BambooTracker/gui/module_properties_dialog.cpp

raw
1 /*
2 * Copyright (C) 2018-2020 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 "module_properties_dialog.hpp"
27 #include "ui_module_properties_dialog.h"
28 #include <vector>
29 #include <utility>
30 #include <unordered_map>
31 #include <QCoreApplication>
32 #include "gui/gui_utils.hpp"
33 #include "enum_hash.hpp"
34
35 namespace
36 {
37 const std::unordered_map<SongType, const char*> SONG_TYPE_TEXT = {
38 { SongType::Standard, QT_TRANSLATE_NOOP("SongType", "Standard") },
39 { SongType::FM3chExpanded, QT_TRANSLATE_NOOP("SongType", "FM3ch expanded") }
40 };
41 }
42
43 ModulePropertiesDialog::ModulePropertiesDialog(std::weak_ptr<BambooTracker> core, double configFmMixer,
44 double configSsgMixer, QWidget *parent)
45 : QDialog(parent),
46 ui(new Ui::ModulePropertiesDialog),
47 bt_(core),
48 configFmMixer_(configFmMixer),
49 configSsgMixer_(configSsgMixer)
50 {
51 ui->setupUi(this);
52
53 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
54
55 int tickFreq = static_cast<int>(core.lock()->getModuleTickFrequency());
56 ui->customTickFreqSpinBox->setValue(tickFreq);
57 switch (tickFreq) {
58 case 60: ui->ntscRadioButton->setChecked(true); break;
59 case 50: ui->palRadioButton->setChecked(true); break;
60 default: ui->customTickFreqRadioButton->setChecked(true); break;
61 }
62
63 MixerType mixType = core.lock()->getModuleMixerType();
64 if (mixType == MixerType::UNSPECIFIED) {
65 ui->mixerGroupBox->setChecked(false);
66 }
67 else {
68 ui->mixerGroupBox->setChecked(true);
69 ui->mixerTypeComboBox->setCurrentIndex(static_cast<int>(mixType) - 1);
70 }
71 setCustomMixerLevels(core.lock()->getModuleCustomMixerFMLevel(), core.lock()->getModuleCustomMixerSSGLevel());
72
73 ui->songTreeWidget->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
74 int songCnt = static_cast<int>(core.lock()->getSongCount());
75 for (int i = 0; i < songCnt; ++i) {
76 auto title = core.lock()->getSongTitle(i);
77 insertSong(i, gui_utils::utf8ToQString(title), core.lock()->getSongStyle(i).type, i);
78 }
79
80 ui->sngTypeComboBox->addItem(QCoreApplication::translate("SongType", SONG_TYPE_TEXT.at(SongType::Standard)), static_cast<int>(SongType::Standard));
81 ui->sngTypeComboBox->addItem(QCoreApplication::translate("SongType", SONG_TYPE_TEXT.at(SongType::FM3chExpanded)), static_cast<int>(SongType::FM3chExpanded));
82 }
83
84 ModulePropertiesDialog::~ModulePropertiesDialog()
85 {
86 delete ui;
87 }
88
89 void ModulePropertiesDialog::insertSong(int row, QString title, SongType type, int prevNum)
90 {
91 QTreeWidgetItem* item = new QTreeWidgetItem();
92 item->setText(0, QString::number(row));
93 item->setData(0, Qt::UserRole, prevNum);
94 item->setText(1, title);
95 item->setText(2, SONG_TYPE_TEXT.at(type));
96 item->setData(2, Qt::UserRole, static_cast<int>(type));
97 ui->songTreeWidget->insertTopLevelItem(row, item);
98
99 for (int i = row + 1; i < ui->songTreeWidget->topLevelItemCount(); ++i) {
100 ui->songTreeWidget->topLevelItem(i)->setText(0, QString::number(i));
101 }
102
103 checkButtonsEnabled();
104 }
105
106 void ModulePropertiesDialog::checkButtonsEnabled()
107 {
108 if (ui->songTreeWidget->currentItem() != nullptr
109 && ui->songTreeWidget->topLevelItemCount() > 1) {
110 ui->upToolButton->setEnabled(true);
111 ui->downToolButton->setEnabled(true);
112 ui->removePushButton->setEnabled(true);
113 }
114 else {
115 ui->upToolButton->setEnabled(false);
116 ui->downToolButton->setEnabled(false);
117 ui->removePushButton->setEnabled(false);
118 }
119 }
120
121 void ModulePropertiesDialog::swapset(int aboveRow, int belowRow)
122 {
123 auto* tree = ui->songTreeWidget;
124 QTreeWidgetItem* below = tree->takeTopLevelItem(belowRow);
125 if (tree->topLevelItemCount() > 2) {
126 QTreeWidgetItem* above = tree->takeTopLevelItem(aboveRow);
127 tree->insertTopLevelItem(aboveRow, below);
128 tree->insertTopLevelItem(belowRow, above);
129 }
130 else {
131 tree->insertTopLevelItem(aboveRow, below);
132 }
133
134 for (int i = aboveRow; i < ui->songTreeWidget->topLevelItemCount(); ++i) {
135 ui->songTreeWidget->topLevelItem(i)->setText(0, QString::number(i));
136 }
137 }
138
139 void ModulePropertiesDialog::setCustomMixerLevels(double fm, double ssg)
140 {
141 fmMixer_ = fm;
142 ssgMixer_ = ssg;
143
144 ui->customMixerFMLevelLabel->setText(QString::asprintf("%+.1fdB", fmMixer_));
145 ui->customMixerSSGLevelLabel->setText(QString::asprintf("%+.1fdB", ssgMixer_));
146 }
147
148 /******************************/
149 void ModulePropertiesDialog::on_upToolButton_clicked()
150 {
151 int curRow = ui->songTreeWidget->currentIndex().row();
152 if (!curRow) return;
153
154 swapset(curRow - 1, curRow);
155 ui->songTreeWidget->setCurrentItem(ui->songTreeWidget->topLevelItem(curRow - 1));
156 }
157
158 void ModulePropertiesDialog::on_downToolButton_clicked()
159 {
160 int curRow = ui->songTreeWidget->currentIndex().row();
161 if (curRow == ui->songTreeWidget->topLevelItemCount() - 1) return;
162
163 swapset(curRow, curRow + 1);
164 ui->songTreeWidget->setCurrentItem(ui->songTreeWidget->topLevelItem(curRow + 1));
165 }
166
167 void ModulePropertiesDialog::on_removePushButton_clicked()
168 {
169 int row = ui->songTreeWidget->currentIndex().row();
170 auto del = ui->songTreeWidget->takeTopLevelItem(row);
171 delete del;
172 for (int i = row; i < ui->songTreeWidget->topLevelItemCount(); ++ i) {
173 ui->songTreeWidget->topLevelItem(i)->setText(0, QString::number(i));
174 }
175
176 checkButtonsEnabled();
177 }
178
179 void ModulePropertiesDialog::on_insertPushButton_clicked()
180 {
181 int row = ui->songTreeWidget->currentIndex().row();
182 if (row == -1) row = ui->songTreeWidget->topLevelItemCount();
183
184 insertSong(row,
185 ui->sngTitleLineEdit->text(),
186 static_cast<SongType>(ui->sngTypeComboBox->currentData(Qt::UserRole).toInt()));
187 ui->songTreeWidget->setCurrentItem(ui->songTreeWidget->topLevelItem(row));
188 }
189
190 void ModulePropertiesDialog::on_songTreeWidget_itemSelectionChanged()
191 {
192 auto item = ui->songTreeWidget->currentItem();
193 ui->sngTitleLineEdit->setText(item->text(1));
194 int type = item->data(2, Qt::UserRole).toInt();
195 for (int i = 0; i < ui->sngTypeComboBox->count(); ++i) {
196 if (ui->sngTypeComboBox->itemData(i, Qt::UserRole).toInt() == type) {
197 ui->sngTypeComboBox->setCurrentIndex(i);
198 break;
199 }
200 }
201 checkButtonsEnabled();
202 }
203
204 void ModulePropertiesDialog::onAccepted()
205 {
206 // Set tick frequency
207 unsigned int tickFreq;
208 if (ui->ntscRadioButton->isChecked()) tickFreq = 60;
209 else if (ui->palRadioButton->isChecked()) tickFreq = 50;
210 else tickFreq = static_cast<unsigned int>(ui->customTickFreqSpinBox->value());
211 bt_.lock()->setModuleTickFrequency(tickFreq);
212
213 // Set mixer
214 if (ui->mixerGroupBox->isChecked()) {
215 auto mixType = static_cast<MixerType>(ui->mixerTypeComboBox->currentIndex() + 1);
216 bt_.lock()->setModuleMixerType(mixType);
217 if (mixType == MixerType::CUSTOM) {
218 bt_.lock()->setModuleCustomMixerFMLevel(fmMixer_);
219 bt_.lock()->setModuleCustomMixerSSGLevel(ssgMixer_);
220 }
221 }
222 else {
223 bt_.lock()->setModuleMixerType(MixerType::UNSPECIFIED);
224 }
225
226 auto* tree = ui->songTreeWidget;
227 std::vector<int> newSongNums;
228
229 for (int i = 0; i < tree->topLevelItemCount(); ++i) {
230 QTreeWidgetItem* item = tree->topLevelItem(i);
231 SongType type = static_cast<SongType>(item->data(2, Qt::UserRole).toInt());
232 std::string title = item->text(1).toUtf8().toStdString();
233 if (item->data(0, Qt::UserRole).toInt() == -1) { // Add new song
234 int n = static_cast<int>(bt_.lock()->getSongCount());
235 bt_.lock()->addSong(type, title);
236 newSongNums.push_back(n);
237 }
238 else { // Update song data
239 int n = item->data(0, Qt::UserRole).toInt();
240 bt_.lock()->setSongTitle(n, title);
241 if (bt_.lock()->getSongStyle(n).type != type)
242 bt_.lock()->changeSongType(n, type);
243 newSongNums.push_back(n);
244 }
245 }
246
247 // Sort songs
248 bt_.lock()->sortSongs(std::move(newSongNums));
249 }
250
251 void ModulePropertiesDialog::on_mixerTypeComboBox_currentIndexChanged(int index)
252 {
253 ui->mixerCustomGroupBox->setEnabled(index == 0);
254 }
255
256 void ModulePropertiesDialog::on_customMixerSetPushButton_clicked()
257 {
258 setCustomMixerLevels(configFmMixer_, configSsgMixer_);
259 }
260
261 void ModulePropertiesDialog::on_updateButton_clicked()
262 {
263 if (auto item = ui->songTreeWidget->currentItem()) {
264 item->setText(1, ui->sngTitleLineEdit->text());
265 auto typeInt = ui->sngTypeComboBox->currentData(Qt::UserRole).toInt();
266 item->setText(2, SONG_TYPE_TEXT.at(static_cast<SongType>(typeInt)));
267 item->setData(2, Qt::UserRole, typeInt);
268 }
269 }