Debian Codegraph @

BambooTracker/gui/command/instrument/add_instrument_qt_command.cpp

raw
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 "add_instrument_qt_command.hpp"
27 #include <utility>
28 #include "command/command_id.hpp"
29 #include "instrument_command_qt_utils.hpp"
30
31 AddInstrumentQtCommand::AddInstrumentQtCommand(QListWidget *list, int num, const QString& name, InstrumentType type,
32 std::weak_ptr<InstrumentEditorManager> dialogMan, MainWindow* mainWin,
33 bool onlyUsed, bool preventFirstStore, QUndoCommand *parent)
34 : QUndoCommand(parent),
35 list_(list),
36 num_(num),
37 name_(name),
38 type_(type),
39 dialogMan_(dialogMan),
40 mainWin_(mainWin),
41 onlyUsed_(onlyUsed),
42 hasDone_(!preventFirstStore)
43 {
44 }
45
46 void AddInstrumentQtCommand::undo()
47 {
48 auto&& item = list_->takeItem(num_);
49 delete item;
50
51 dialogMan_.lock()->remove(num_);
52
53 if ((type_ == InstrumentType::ADPCM || type_ == InstrumentType::Drumkit) && onlyUsed_) {
54 mainWin_->assignADPCMSamples();
55 }
56 }
57
58 void AddInstrumentQtCommand::redo()
59 {
60 list_->insertItem(num_, gui_command_utils::createInstrumentListItem(num_, type_, name_));
61
62 if (hasDone_ && (type_ == InstrumentType::ADPCM || type_ == InstrumentType::Drumkit)) {
63 mainWin_->assignADPCMSamples();
64 }
65 hasDone_ = true;
66 }
67
68 int AddInstrumentQtCommand::id() const
69 {
70 return CommandId::AddInstrument;
71 }