1 /***************************************************************************
2 **                                                                        **
3 **  Polyphone, a soundfont editor                                         **
4 **  Copyright (C) 2013-2019 Davy Triponney                                **
5 **                                                                        **
6 **  This program is free software: you can redistribute it and/or modify  **
7 **  it under the terms of the GNU General Public License as published by  **
8 **  the Free Software Foundation, either version 3 of the License, or     **
9 **  (at your option) any later version.                                   **
10 **                                                                        **
11 **  This program is distributed in the hope that it will be useful,       **
12 **  but WITHOUT ANY WARRANTY; without even the implied warranty of        **
13 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          **
14 **  GNU General Public License for more details.                          **
15 **                                                                        **
16 **  You should have received a copy of the GNU General Public License     **
17 **  along with this program. If not, see http://www.gnu.org/licenses/.    **
18 **                                                                        **
19 ****************************************************************************
20 **           Author: Davy Triponney                                       **
21 **  Website/Contact: https://www.polyphone-soundfonts.com                 **
22 **             Date: 01.01.2013                                           **
23 ***************************************************************************/
24 
25 #include "instprst.h"
26 #include "soundfont.h"
27 #include "utils.h"
28 
InstPrst(Soundfont * soundfont,int row,TreeItem * parent,EltID id)29 InstPrst::InstPrst(Soundfont * soundfont, int row, TreeItem * parent, EltID id) : TreeItem(id, parent),
30     _soundfont(soundfont),
31     _globalDivision(new Division(nullptr, _soundfont, nullptr, EltID())),
32     _row(row)
33 {
34 }
35 
~InstPrst()36 InstPrst::~InstPrst()
37 {
38     for (int i = _divisions.indexCount() - 1; i >= 0; i--)
39     {
40         Division * elt = _divisions.atIndex(i);
41         if (elt != nullptr)
42         {
43             elt->notifyDeletion(false);
44             delete _divisions.takeAtIndex(i);
45         }
46     }
47 
48     delete _globalDivision;
49 }
50 
addDivision()51 int InstPrst::addDivision()
52 {
53     EltID childId = this->getId();
54     childId.typeElement = (this->getId().typeElement == elementPrst ? elementPrstInst : elementInstSmpl);
55     childId.indexElt2 = _divisions.indexCount();
56 
57     int index = _divisions.add(new Division(this, _soundfont, this, childId));
58     _divisions.atIndex(index)->notifyCreated();
59     return index;
60 }
61 
getDivision(int index)62 Division * InstPrst::getDivision(int index)
63 {
64     if (index < _divisions.indexCount())
65         return _divisions.atIndex(index);
66     return nullptr;
67 }
68 
deleteDivision(int index)69 bool InstPrst::deleteDivision(int index)
70 {
71     if (index < _divisions.indexCount())
72     {
73         Division * elt = _divisions.atIndex(index);
74         if (elt != nullptr)
75         {
76             elt->notifyDeletion();
77             delete _divisions.takeAtIndex(index);
78             return true;
79         }
80     }
81 
82     return false;
83 }
84 
indexOfId(int id)85 int InstPrst::indexOfId(int id)
86 {
87     return _divisions.positionOfIndex(id);
88 }
89 
childCount() const90 int InstPrst::childCount() const
91 {
92     return _divisions.positionCount();
93 }
94 
child(int row)95 TreeItem * InstPrst::child(int row)
96 {
97     return _divisions.atPosition(row);
98 }
99 
display()100 QString InstPrst::display()
101 {
102     QString display = "";
103     if (_extraFields.contains(champ_wBank) && _extraFields.contains(champ_wPreset))
104         display = QString("%1:%2 ").arg(_extraFields[champ_wBank], 3, 10, QChar('0')).arg(_extraFields[champ_wPreset], 3, 10, QChar('0'));
105     display += (_name.isEmpty() ? "..." : _name);
106     return display;
107 }
108 
sortText()109 QString InstPrst::sortText()
110 {
111     QString sortText = "";
112     if (_extraFields.contains(champ_wBank) && _extraFields.contains(champ_wPreset))
113         sortText = QString("%1:%2 ").arg(_extraFields[champ_wBank], 3, 10, QChar('0')).arg(_extraFields[champ_wPreset], 3, 10, QChar('0'));
114     sortText += _nameSort;
115     return sortText;
116 }
117 
setName(QString name)118 void InstPrst::setName(QString name)
119 {
120     _name = name;
121     _nameSort = Utils::removeAccents(_name).toLower();
122     notifyRename();
123 }
124 
setExtraField(AttributeType champ,int value)125 void InstPrst::setExtraField(AttributeType champ, int value)
126 {
127     _extraFields[champ] = value;
128     if (champ == champ_wPreset || champ == champ_wBank)
129         notifyRename();
130 }
131 
getExtraField(AttributeType champ)132 int InstPrst::getExtraField(AttributeType champ)
133 {
134     return _extraFields.contains(champ) ? _extraFields[champ] : 0;
135 }
136