Debian Codegraph @

BambooTracker/gui/command/instrument/swap_instruments_qt_command.cpp

raw
1 /*
2 * Copyright (C) 2020-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 "swap_instruments_qt_command.hpp"
27 #include <QListWidgetItem>
28 #include "instrument.hpp"
29 #include "command/command_id.hpp"
30 #include "gui/pattern_editor/pattern_editor.hpp"
31 #include "gui/instrument_editor/instrument_editor.hpp"
32 #include "instrument_command_qt_utils.hpp"
33
34 SwapInstrumentsQtCommand::SwapInstrumentsQtCommand(QListWidget* list, int inst1Row, int inst2Row, const QString& inst1Name,
35 const QString& inst2Name, std::weak_ptr<InstrumentEditorManager> editorMan,
36 PatternEditor* pattern, QUndoCommand* parent)
37 : QUndoCommand(parent),
38 list_(list),
39 ptn_(pattern),
40 editorMan_(editorMan)
41 {
42 if (inst1Row < inst2Row) {
43 inst1Row_ = inst1Row;
44 inst2Row_ = inst2Row;
45 inst1Name_ = inst1Name;
46 inst2Name_ = inst2Name;
47 }
48 else {
49 inst1Row_ = inst2Row;
50 inst2Row_ = inst1Row;
51 inst1Name_ = inst2Name;
52 inst2Name_ = inst1Name;
53 }
54 }
55
56 void SwapInstrumentsQtCommand::undo()
57 {
58 swap(inst1Row_, inst2Row_, inst2Name_, inst1Name_);
59 }
60
61 void SwapInstrumentsQtCommand::redo()
62 {
63 swap(inst1Row_, inst2Row_, inst1Name_, inst2Name_);
64 }
65
66 int SwapInstrumentsQtCommand::id() const
67 {
68 return CommandId::SwapInstruments;
69 }
70
71 void SwapInstrumentsQtCommand::swap(int above, int below, QString aboveName, QString belowName)
72 {
73 QListWidgetItem* belowItem = list_->takeItem(below);
74 int belowId = belowItem->data(Qt::UserRole).toInt();
75 QListWidgetItem* aboveItem = list_->takeItem(above);
76 int aboveId = aboveItem->data(Qt::UserRole).toInt();
77
78 QString newBelowName = gui_command_utils::makeInstrumentListText(belowId, aboveName);
79 aboveItem->setText(newBelowName);
80 aboveItem->setData(Qt::UserRole, belowId);
81 QString newAboveName = gui_command_utils::makeInstrumentListText(aboveId, belowName);
82 belowItem->setText(newAboveName);
83 belowItem->setData(Qt::UserRole, aboveId);
84
85 list_->insertItem(above, belowItem);
86 list_->insertItem(below, aboveItem);
87
88 editorMan_.lock()->swap(aboveId, belowId);
89
90 ptn_->onPatternDataGlobalChanged();
91 }