| 1 |
/* |
| 2 |
* Copyright (C) 2018-2022 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 "pattern_editor.hpp" |
| 27 |
#include "ui_pattern_editor.h" |
| 28 |
|
| 29 |
PatternEditor::PatternEditor(QWidget *parent) : |
| 30 |
QFrame(parent), |
| 31 |
ui(new Ui::PatternEditor), |
| 32 |
freezed_(false), |
| 33 |
songLoaded_(false), |
| 34 |
hScrollCellMove_(true) |
| 35 |
{ |
| 36 |
ui->setupUi(this); |
| 37 |
|
| 38 |
installEventFilter(this); |
| 39 |
ui->panel->installEventFilter(this); |
| 40 |
ui->verticalScrollBar->installEventFilter(this); |
| 41 |
|
| 42 |
ui->panel->setFocus(); |
| 43 |
QObject::connect(ui->panel, &PatternEditorPanel::hScrollBarChangeRequested, |
| 44 |
ui->horizontalScrollBar, &QScrollBar::setValue); |
| 45 |
QObject::connect(ui->panel, &PatternEditorPanel::vScrollBarChangeRequested, |
| 46 |
this, [&](int num, int max) { |
| 47 |
if (ui->verticalScrollBar->maximum() < num) { |
| 48 |
ui->verticalScrollBar->setMaximum(max); |
| 49 |
ui->verticalScrollBar->setValue(num); |
| 50 |
} |
| 51 |
else { |
| 52 |
ui->verticalScrollBar->setValue(num); |
| 53 |
ui->verticalScrollBar->setMaximum(max); |
| 54 |
} |
| 55 |
}); |
| 56 |
QObject::connect(ui->panel, &PatternEditorPanel::currentTrackChanged, |
| 57 |
this, [&](int idx) { emit currentTrackChanged(idx); }); |
| 58 |
QObject::connect(ui->panel, &PatternEditorPanel::currentOrderChanged, |
| 59 |
this, [&](int num, int max) { emit currentOrderChanged(num, max); }); |
| 60 |
|
| 61 |
QObject::connect(ui->panel, &PatternEditorPanel::effectColsCompanded, |
| 62 |
this, [&](int num, int max) { |
| 63 |
if (ui->horizontalScrollBar->maximum() < num) { |
| 64 |
ui->horizontalScrollBar->setMaximum(max); |
| 65 |
ui->horizontalScrollBar->setValue(num); |
| 66 |
} |
| 67 |
else { |
| 68 |
ui->horizontalScrollBar->setValue(num); |
| 69 |
ui->horizontalScrollBar->setMaximum(max); |
| 70 |
} |
| 71 |
}); |
| 72 |
QObject::connect(ui->panel, &PatternEditorPanel::selected, |
| 73 |
this, [&](bool isSelected) { emit selected(isSelected); }); |
| 74 |
QObject::connect(ui->panel, &PatternEditorPanel::instrumentEntered, |
| 75 |
this, [&](int num) { emit instrumentEntered(num); }); |
| 76 |
QObject::connect(ui->panel, &PatternEditorPanel::volumeEntered, |
| 77 |
this, [&](int volume) { emit volumeEntered(volume); }); |
| 78 |
QObject::connect(ui->panel, &PatternEditorPanel::effectEntered, |
| 79 |
this, [&](QString text) { emit effectEntered(text); }); |
| 80 |
|
| 81 |
auto focusSlot = [&] { ui->panel->setFocus(); }; |
| 82 |
|
| 83 |
QObject::connect(ui->horizontalScrollBar, &QScrollBar::valueChanged, |
| 84 |
ui->panel, &PatternEditorPanel::onHScrollBarChanged); |
| 85 |
QObject::connect(ui->horizontalScrollBar, &QScrollBar::sliderPressed, this, focusSlot); |
| 86 |
|
| 87 |
QObject::connect(ui->verticalScrollBar, &QScrollBar::valueChanged, |
| 88 |
ui->panel, &PatternEditorPanel::onVScrollBarChanged); |
| 89 |
QObject::connect(ui->verticalScrollBar, &QScrollBar::sliderPressed, this, focusSlot); |
| 90 |
} |
| 91 |
|
| 92 |
PatternEditor::~PatternEditor() |
| 93 |
{ |
| 94 |
delete ui; |
| 95 |
} |
| 96 |
|
| 97 |
void PatternEditor::setCore(std::shared_ptr<BambooTracker> core) |
| 98 |
{ |
| 99 |
bt_ = core; |
| 100 |
ui->panel->setCore(core); |
| 101 |
} |
| 102 |
|
| 103 |
void PatternEditor::setCommandStack(std::weak_ptr<QUndoStack> stack) |
| 104 |
{ |
| 105 |
ui->panel->setCommandStack(stack); |
| 106 |
} |
| 107 |
|
| 108 |
void PatternEditor::setConfiguration(std::shared_ptr<Configuration> config) |
| 109 |
{ |
| 110 |
ui->panel->setConfiguration(config); |
| 111 |
} |
| 112 |
|
| 113 |
void PatternEditor::setColorPallete(std::shared_ptr<ColorPalette> palette) |
| 114 |
{ |
| 115 |
ui->panel->setColorPallete(palette); |
| 116 |
} |
| 117 |
|
| 118 |
void PatternEditor::addActionToPanel(QAction* action) |
| 119 |
{ |
| 120 |
ui->panel->addAction(action); |
| 121 |
} |
| 122 |
|
| 123 |
void PatternEditor::changeEditable() |
| 124 |
{ |
| 125 |
ui->panel->changeEditable(); |
| 126 |
} |
| 127 |
|
| 128 |
void PatternEditor::updatePositionByStepUpdate(bool isFirstUpdate) |
| 129 |
{ |
| 130 |
ui->panel->updatePositionByStepUpdate(isFirstUpdate); |
| 131 |
} |
| 132 |
|
| 133 |
void PatternEditor::updatepositionByPositionJump(bool trackChanged) |
| 134 |
{ |
| 135 |
ui->panel->updatePositionByStepUpdate(false, true, trackChanged); |
| 136 |
} |
| 137 |
|
| 138 |
void PatternEditor::changeMarker() |
| 139 |
{ |
| 140 |
ui->panel->changeMarker(); |
| 141 |
} |
| 142 |
|
| 143 |
void PatternEditor::copySelectedCells() |
| 144 |
{ |
| 145 |
ui->panel->copySelectedCells(); |
| 146 |
} |
| 147 |
|
| 148 |
void PatternEditor::cutSelectedCells() |
| 149 |
{ |
| 150 |
ui->panel->cutSelectedCells(); |
| 151 |
} |
| 152 |
|
| 153 |
void PatternEditor::freeze() |
| 154 |
{ |
| 155 |
setUpdatesEnabled(false); |
| 156 |
freezed_ = true; |
| 157 |
ui->panel->waitPaintFinish(); |
| 158 |
} |
| 159 |
|
| 160 |
void PatternEditor::unfreeze() |
| 161 |
{ |
| 162 |
freezed_ = false; |
| 163 |
setUpdatesEnabled(true); |
| 164 |
} |
| 165 |
|
| 166 |
QFont PatternEditor::getHeaderFont() const |
| 167 |
{ |
| 168 |
return ui->panel->getHeaderFont(); |
| 169 |
} |
| 170 |
|
| 171 |
QFont PatternEditor::getRowsFont() const |
| 172 |
{ |
| 173 |
return ui->panel->getRowsFont(); |
| 174 |
} |
| 175 |
|
| 176 |
QFont PatternEditor::getDefaultHeaderFont() const |
| 177 |
{ |
| 178 |
return ui->panel->getDefaultHeaderFont(); |
| 179 |
} |
| 180 |
|
| 181 |
QFont PatternEditor::getDefaultRowsFont() const |
| 182 |
{ |
| 183 |
return ui->panel->getDefaultRowsFont(); |
| 184 |
} |
| 185 |
|
| 186 |
void PatternEditor::setFonts(const QFont& headerFont, const QFont& rowsFont) |
| 187 |
{ |
| 188 |
ui->panel->setFonts(headerFont, rowsFont); |
| 189 |
} |
| 190 |
|
| 191 |
void PatternEditor::setHorizontalScrollMode(bool cellBased, bool refresh) |
| 192 |
{ |
| 193 |
hScrollCellMove_ = cellBased; |
| 194 |
if (refresh) updateHorizontalSliderMaximum(); |
| 195 |
} |
| 196 |
|
| 197 |
void PatternEditor::setVisibleTracks(std::vector<int> tracks) |
| 198 |
{ |
| 199 |
ui->horizontalScrollBar->setMaximum(200); // Dummy |
| 200 |
ui->panel->setVisibleTracks(tracks); |
| 201 |
updateHorizontalSliderMaximum(); |
| 202 |
} |
| 203 |
|
| 204 |
std::vector<int> PatternEditor::getVisibleTracks() const |
| 205 |
{ |
| 206 |
return ui->panel->getVisibleTracks(); |
| 207 |
} |
| 208 |
|
| 209 |
bool PatternEditor::eventFilter(QObject *watched, QEvent *event) |
| 210 |
{ |
| 211 |
if (watched == this) { |
| 212 |
if (event->type() == QEvent::FocusIn) { |
| 213 |
ui->panel->setFocus(); |
| 214 |
} |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
if (watched == ui->panel) { |
| 219 |
if (freezed_ && event->type() != QEvent::Paint) return true; |
| 220 |
|
| 221 |
switch (event->type()) { |
| 222 |
case QEvent::FocusIn: |
| 223 |
ui->panel->redrawByFocusChanged(); |
| 224 |
emit focusIn(); |
| 225 |
return false; |
| 226 |
case QEvent::FocusOut: |
| 227 |
ui->panel->redrawByFocusChanged(); |
| 228 |
emit focusOut(); |
| 229 |
return false; |
| 230 |
case QEvent::HoverEnter: |
| 231 |
case QEvent::HoverLeave: |
| 232 |
ui->panel->redrawByHoverChanged(); |
| 233 |
return false; |
| 234 |
default: |
| 235 |
return false; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
if (watched == ui->verticalScrollBar) { |
| 240 |
if (freezed_) return true; // Ignore every events |
| 241 |
|
| 242 |
switch (event->type()) { |
| 243 |
case QEvent::MouseButtonPress: |
| 244 |
case QEvent::MouseButtonRelease: |
| 245 |
case QEvent::MouseButtonDblClick: |
| 246 |
case QEvent::DragMove: |
| 247 |
case QEvent::Wheel: |
| 248 |
return (bt_->isPlaySong() && bt_->isFollowPlay()); |
| 249 |
default: |
| 250 |
return false; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
void PatternEditor::resizeEvent(QResizeEvent*) |
| 258 |
{ |
| 259 |
// For view-based scroll |
| 260 |
updateHorizontalSliderMaximum(); |
| 261 |
} |
| 262 |
|
| 263 |
/********** Slots **********/ |
| 264 |
void PatternEditor::onOrderListCurrentTrackChanged(int idx) |
| 265 |
{ |
| 266 |
ui->panel->onOrderListCurrentTrackChanged(idx); |
| 267 |
} |
| 268 |
|
| 269 |
void PatternEditor::onOrderListCrrentOrderChanged(int num) |
| 270 |
{ |
| 271 |
ui->panel->onOrderListCurrentOrderChanged(num); |
| 272 |
} |
| 273 |
|
| 274 |
void PatternEditor::onOrderListEdited() |
| 275 |
{ |
| 276 |
ui->panel->onOrderListEdited(); |
| 277 |
} |
| 278 |
|
| 279 |
void PatternEditor::onDefaultPatternSizeChanged() |
| 280 |
{ |
| 281 |
ui->panel->onDefaultPatternSizeChanged(); |
| 282 |
} |
| 283 |
|
| 284 |
void PatternEditor::onShortcutUpdated() |
| 285 |
{ |
| 286 |
ui->panel->onShortcutUpdated(); |
| 287 |
} |
| 288 |
|
| 289 |
void PatternEditor::onPatternDataGlobalChanged() |
| 290 |
{ |
| 291 |
ui->panel->redrawByPatternChanged(); |
| 292 |
} |
| 293 |
|
| 294 |
void PatternEditor::setPatternHighlight1Count(int count) |
| 295 |
{ |
| 296 |
ui->panel->setPatternHighlight1Count(count); |
| 297 |
} |
| 298 |
|
| 299 |
void PatternEditor::setPatternHighlight2Count(int count) |
| 300 |
{ |
| 301 |
ui->panel->setPatternHighlight2Count(count); |
| 302 |
} |
| 303 |
|
| 304 |
void PatternEditor::setEditableStep(int n) |
| 305 |
{ |
| 306 |
ui->panel->setEditableStep(n); |
| 307 |
} |
| 308 |
|
| 309 |
void PatternEditor::onSongLoaded() |
| 310 |
{ |
| 311 |
ui->horizontalScrollBar->setValue(0); |
| 312 |
ui->panel->onSongLoaded(); |
| 313 |
songLoaded_ = true; |
| 314 |
updateHorizontalSliderMaximum(); |
| 315 |
ui->verticalScrollBar->setMaximum(static_cast<int>(bt_->getPatternSizeFromOrderNumber( |
| 316 |
bt_->getCurrentSongNumber(), |
| 317 |
bt_->getCurrentOrderNumber())) - 1); |
| 318 |
ui->verticalScrollBar->setValue(0); |
| 319 |
} |
| 320 |
|
| 321 |
void PatternEditor::onDeletePressed() |
| 322 |
{ |
| 323 |
ui->panel->onDeletePressed(); |
| 324 |
} |
| 325 |
|
| 326 |
void PatternEditor::onPastePressed() |
| 327 |
{ |
| 328 |
ui->panel->onPastePressed(); |
| 329 |
} |
| 330 |
|
| 331 |
void PatternEditor::onPasteMixPressed() |
| 332 |
{ |
| 333 |
ui->panel->onPasteMixPressed(); |
| 334 |
} |
| 335 |
|
| 336 |
void PatternEditor::onPasteOverwritePressed() |
| 337 |
{ |
| 338 |
ui->panel->onPasteOverwritePressed(); |
| 339 |
} |
| 340 |
|
| 341 |
void PatternEditor::onPasteInsertPressed() |
| 342 |
{ |
| 343 |
ui->panel->onPasteInsertPressed(); |
| 344 |
} |
| 345 |
|
| 346 |
void PatternEditor::onSelectPressed(int type) |
| 347 |
{ |
| 348 |
ui->panel->onSelectPressed(type); |
| 349 |
} |
| 350 |
|
| 351 |
void PatternEditor::onTransposePressed(bool isOctave, bool isIncreased) |
| 352 |
{ |
| 353 |
int semitone = isOctave ? (isIncreased ? 12 : -12) : (isIncreased ? 1 : -1); |
| 354 |
ui->panel->onNoteTransposePressed(semitone); |
| 355 |
} |
| 356 |
|
| 357 |
void PatternEditor::onChangeValuesPressed(bool isCoarse, bool isIncreased) |
| 358 |
{ |
| 359 |
int val = isCoarse ? (isIncreased ? 16 : -16) : (isIncreased ? 1 : -1); |
| 360 |
ui->panel->onChangeValuesPressed(val); |
| 361 |
} |
| 362 |
|
| 363 |
void PatternEditor::onToggleTrackPressed() |
| 364 |
{ |
| 365 |
ui->panel->onToggleTrackPressed(); |
| 366 |
} |
| 367 |
|
| 368 |
void PatternEditor::onSoloTrackPressed() |
| 369 |
{ |
| 370 |
ui->panel->onSoloTrackPressed(); |
| 371 |
} |
| 372 |
|
| 373 |
void PatternEditor::onExpandPressed() |
| 374 |
{ |
| 375 |
ui->panel->onExpandPressed(); |
| 376 |
} |
| 377 |
|
| 378 |
void PatternEditor::onShrinkPressed() |
| 379 |
{ |
| 380 |
ui->panel->onShrinkPressed(); |
| 381 |
} |
| 382 |
|
| 383 |
void PatternEditor::onInterpolatePressed() |
| 384 |
{ |
| 385 |
ui->panel->onInterpolatePressed(); |
| 386 |
} |
| 387 |
|
| 388 |
void PatternEditor::onReversePressed() |
| 389 |
{ |
| 390 |
ui->panel->onReversePressed(); |
| 391 |
} |
| 392 |
|
| 393 |
void PatternEditor::onReplaceInstrumentPressed() |
| 394 |
{ |
| 395 |
ui->panel->onReplaceInstrumentPressed(); |
| 396 |
} |
| 397 |
|
| 398 |
void PatternEditor::onFollowModeChanged() |
| 399 |
{ |
| 400 |
ui->panel->onFollowModeChanged(); |
| 401 |
} |
| 402 |
|
| 403 |
void PatternEditor::onStoppedPlaySong() |
| 404 |
{ |
| 405 |
ui->panel->redrawPatterns(); |
| 406 |
} |
| 407 |
|
| 408 |
void PatternEditor::onDuplicateInstrumentsRemoved() |
| 409 |
{ |
| 410 |
ui->panel->redrawByPatternChanged(); |
| 411 |
} |
| 412 |
|
| 413 |
void PatternEditor::onPlayStepPressed() |
| 414 |
{ |
| 415 |
ui->panel->onPlayStepPressed(); |
| 416 |
} |
| 417 |
|
| 418 |
void PatternEditor::updateHorizontalSliderMaximum() |
| 419 |
{ |
| 420 |
if (!ui->panel->isReadyCore() || !songLoaded_) return; |
| 421 |
int max = hScrollCellMove_ ? ui->panel->getFullColmunSize() : ui->panel->getScrollableCountByTrack(); |
| 422 |
ui->horizontalScrollBar->setMaximum(max); |
| 423 |
} |