Debian Codegraph @

BambooTracker/gui/bookmark_manager_form.cpp

raw
1 /*
2 * Copyright (C) 2020-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 "bookmark_manager_form.hpp"
27 #include "ui_bookmark_manager_form.h"
28 #include <QKeyEvent>
29 #include <vector>
30 #include "song.hpp"
31 #include "gui/gui_utils.hpp"
32
33 BookmarkManagerForm::BookmarkManagerForm(std::weak_ptr<BambooTracker> core, bool showHex,
34 QWidget *parent) :
35 QWidget(parent),
36 ui(new Ui::BookmarkManagerForm),
37 bt_(core),
38 curSong_(core.lock()->getCurrentSongNumber())
39 {
40 ui->setupUi(this);
41 setWindowFlags((windowFlags()
42 & ~(Qt::WindowContextHelpButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint))
43 | Qt::WindowStaysOnTopHint);
44
45 setNumberSettings(showHex);
46 initList();
47 ui->orderSpinBox->setDisplayIntegerBase(numBase_);
48 ui->stepSpinBox->setDisplayIntegerBase(numBase_);
49
50 insSc_ = std::make_unique<QShortcut>(Qt::Key_Insert, ui->listWidget, nullptr, nullptr, Qt::WidgetShortcut);
51 QObject::connect(insSc_.get(), &QShortcut::activated, this, &BookmarkManagerForm::on_createPushButton_clicked);
52 delSc_ = std::make_unique<QShortcut>(Qt::Key_Delete, ui->listWidget, nullptr, nullptr, Qt::WidgetShortcut);
53 QObject::connect(delSc_.get(), &QShortcut::activated, this, &BookmarkManagerForm::on_removePushButton_clicked);
54 mvUpSc_ = std::make_unique<QShortcut>(Qt::CTRL | Qt::Key_Up, ui->listWidget, nullptr, nullptr, Qt::WidgetShortcut);
55 QObject::connect(mvUpSc_.get(), &QShortcut::activated, this, &BookmarkManagerForm::on_upToolButton_clicked);
56 mvDnSc_ = std::make_unique<QShortcut>(Qt::CTRL | Qt::Key_Down, ui->listWidget, nullptr, nullptr, Qt::WidgetShortcut);
57 QObject::connect(mvDnSc_.get(), &QShortcut::activated, this, &BookmarkManagerForm::on_downToolButton_clicked);
58 }
59
60 BookmarkManagerForm::~BookmarkManagerForm()
61 {
62 delete ui;
63 }
64
65 void BookmarkManagerForm::initList()
66 {
67 int size = static_cast<int>(bt_.lock()->getBookmarkSize(curSong_));
68 for (int i = 0; i < size; ++i) {
69 Bookmark bm = bt_.lock()->getBookmark(curSong_, i);
70 addBookmark(gui_utils::utf8ToQString(bm.name), bm.order, bm.step, true);
71 }
72 }
73
74 void BookmarkManagerForm::addBookmark(QString name, int order, int step, bool onlyUi)
75 {
76 ui->listWidget->addItem(createText(name, order, step));
77
78 if (!onlyUi) {
79 bt_.lock()->addBookmark(curSong_, name.toUtf8().toStdString(), order, step);
80 }
81 }
82
83 void BookmarkManagerForm::removeBookmark(int i)
84 {
85 delete ui->listWidget->takeItem(i);
86 bt_.lock()->removeBookmark(curSong_, i);
87 }
88
89 QString BookmarkManagerForm::createText(QString name, int order, int step)
90 {
91 auto pos = QString("(%1,%2)").arg(order, numWidth_, numBase_, QChar('0'))
92 .arg(step, numWidth_, numBase_, QChar('0')).toUpper();
93 return ((name.isEmpty() ? tr("Bookmark") : name) + " " + pos);
94 }
95
96 void BookmarkManagerForm::sortList(bool byPos)
97 {
98 int row = ui->listWidget->currentRow();
99 QString text = (row == -1) ? "" : ui->listWidget->currentItem()->text();
100
101 ui->listWidget->clear();
102 if (byPos) bt_.lock()->sortBookmarkByPosition(curSong_);
103 else bt_.lock()->sortBookmarkByName(curSong_);
104 initList();
105
106 for (int i = 0; i < ui->listWidget->count(); ++i) {
107 QListWidgetItem* item = ui->listWidget->item(i);
108 if (item->text() == text) {
109 ui->listWidget->setCurrentRow(i);
110 break;
111 }
112 }
113
114 emit modified();
115 }
116
117 void BookmarkManagerForm::onCurrentSongNumberChanged()
118 {
119 curSong_ = bt_.lock()->getCurrentSongNumber();
120
121 ui->listWidget->clear();
122 initList();
123 }
124
125 void BookmarkManagerForm::onConfigurationChanged(bool showHex)
126 {
127 if ((showHex && numBase_ == 16) || (!showHex && numBase_ == 10)) return;
128
129 setNumberSettings(showHex);
130
131 for (int i = 0; i < ui->listWidget->count(); ++i) {
132 Bookmark bm = bt_.lock()->getBookmark(curSong_, i);
133 ui->listWidget->item(i)->setText(createText(gui_utils::utf8ToQString(bm.name), bm.order, bm.step));
134 }
135
136 ui->orderSpinBox->setDisplayIntegerBase(numBase_);
137 ui->stepSpinBox->setDisplayIntegerBase(numBase_);
138 }
139
140 void BookmarkManagerForm::onBookmarkToggleRequested(int order, int step)
141 {
142 std::vector<int> idcs = bt_.lock()->findBookmarks(curSong_, order, step);
143 if (idcs.empty()) {
144 if (bt_.lock()->getBookmarkSize(curSong_) == 127) return; // Maximum size
145 int i = static_cast<int>(bt_.lock()->getBookmarkSize(curSong_));
146 addBookmark(tr("Bookmark %1").arg(i), order, step);
147 }
148 else {
149 for (auto&& it = idcs.rbegin(); it != idcs.rend(); ++it) {
150 removeBookmark(*it); // Remove from back to remain the position
151 }
152 }
153 emit modified();
154 }
155
156 void BookmarkManagerForm::onBookmarkJumpRequested(bool toNext, int order, int step)
157 {
158 if (!bt_.lock()->getBookmarkSize(curSong_)) return;
159 Bookmark bm = toNext ? bt_.lock()->getNextBookmark(curSong_, order, step)
160 : bt_.lock()->getPreviousBookmark(curSong_, order, step);
161 ui->listWidget->setCurrentRow(bt_.lock()->findBookmarks(curSong_, bm.order, bm.step).front());
162 emit positionJumpRequested(bm.order, bm.step);
163 }
164
165 void BookmarkManagerForm::on_createPushButton_clicked()
166 {
167 if (bt_.lock()->getBookmarkSize(curSong_) == 127) return; // Maximum size
168 addBookmark(ui->nameLineEdit->text(), ui->orderSpinBox->value(), ui->stepSpinBox->value());
169 emit modified();
170 }
171
172 void BookmarkManagerForm::on_removePushButton_clicked()
173 {
174 int row = ui->listWidget->currentRow();
175 if (row == -1) return;
176
177 removeBookmark(row);
178 emit modified();
179 }
180
181 void BookmarkManagerForm::on_clearPushButton_clicked()
182 {
183 if (!ui->listWidget->count()) return;
184
185 ui->listWidget->clear();
186 bt_.lock()->clearBookmark(curSong_);
187 emit modified();
188 }
189
190 void BookmarkManagerForm::on_upToolButton_clicked()
191 {
192 int row = ui->listWidget->currentRow();
193 if (row < 1) return;
194
195 bt_.lock()->swapBookmarks(curSong_, row, row - 1);
196 ui->listWidget->clear();
197 initList();
198 ui->listWidget->setCurrentRow(row - 1);
199
200 emit modified();
201 }
202
203 void BookmarkManagerForm::on_downToolButton_clicked()
204 {
205 int row = ui->listWidget->currentRow();
206 if (row == -1 || ui->listWidget->count() - 2 < row) return;
207
208 bt_.lock()->swapBookmarks(curSong_, row, row + 1);
209 ui->listWidget->clear();
210 initList();
211 ui->listWidget->setCurrentRow(row + 1);
212
213 emit modified();
214 }
215
216 void BookmarkManagerForm::on_positionPushButton_clicked()
217 {
218 sortList(true);
219 }
220
221 void BookmarkManagerForm::on_namePushButton_clicked()
222 {
223 sortList(false);
224 }
225
226 void BookmarkManagerForm::on_listWidget_currentRowChanged(int currentRow)
227 {
228 if (currentRow == -1) return;
229
230 Bookmark bm = bt_.lock()->getBookmark(curSong_, currentRow);
231 ui->nameLineEdit->setText(gui_utils::utf8ToQString(bm.name));
232 ui->orderSpinBox->setValue(bm.order);
233 ui->stepSpinBox->setValue(bm.step);
234 }
235
236 void BookmarkManagerForm::on_updatePushButton_clicked()
237 {
238 int row = ui->listWidget->currentRow();
239 if (row == -1) return;
240
241 QString name = ui->nameLineEdit->text();
242 int order = ui->orderSpinBox->value();
243 int step = ui->stepSpinBox->value();
244 ui->listWidget->item(row)->setText(createText(name, order, step));
245
246 bt_.lock()->changeBookmark(curSong_, row, name.toUtf8().toStdString(), order, step);
247 emit modified();
248 }
249
250 void BookmarkManagerForm::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
251 {
252 int row = ui->listWidget->row(item);
253 Bookmark bm = bt_.lock()->getBookmark(curSong_, row);
254 emit positionJumpRequested(bm.order, bm.step);
255 }