1 /***************************************************************************
2 **                                                                        **
3 **  Polyphone, a soundfont editor                                         **
4 **  Copyright (C) 2013-2020 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 "dialog_list.h"
26 #include "ui_dialog_list.h"
27 #include "soundfontmanager.h"
28 
DialogList(QWidget * parent)29 DialogList::DialogList(QWidget *parent) :
30     QDialog(parent),
31     ui(new Ui::DialogList)
32 {
33     ui->setupUi(this);
34     this->setWindowFlags((windowFlags() & ~Qt::WindowContextHelpButtonHint));
35 }
36 
~DialogList()37 DialogList::~DialogList()
38 {
39     delete ui;
40 }
41 
showDialog(EltID idSrc,bool isAssociation)42 void DialogList::showDialog(EltID idSrc, bool isAssociation)
43 {
44     _isAssociation = isAssociation;
45     if ((_isAssociation && (idSrc.typeElement != elementInst && idSrc.typeElement != elementSmpl)) ||
46             (!_isAssociation && (idSrc.typeElement != elementInstSmpl && idSrc.typeElement != elementPrstInst)))
47         return;
48 
49     // Title and type of elements to display
50     ElementType element;
51     if (idSrc.typeElement == elementInstSmpl)
52     {
53         this->setWindowTitle(tr("Sample list"));
54         element = elementSmpl;
55     }
56     else if (idSrc.typeElement == elementSmpl || idSrc.typeElement == elementPrstInst)
57     {
58         this->setWindowTitle(tr("Instrument list"));
59         element = elementInst;
60     }
61     else
62     {
63         this->setWindowTitle(tr("Preset list"));
64         element = elementPrst;
65     }
66 
67     // Id to select
68     int selectedId = -1;
69     SoundfontManager * sm = SoundfontManager::getInstance();
70     if (!isAssociation)
71         selectedId = sm->get(idSrc, idSrc.typeElement == elementInstSmpl ? champ_sampleID : champ_instrument).wValue;
72 
73     // Fill the list
74     ui->listWidget->clear();
75     ui->listWidget->clearSelection();
76     ui->listWidget->scrollToTop();
77     EltID id(element, idSrc.indexSf2, 0, 0, 0);
78     ListWidgetItem *item;
79     ListWidgetItem * selectedItem = nullptr;
80     foreach (int i, sm->getSiblings(id))
81     {
82         id.indexElt = i;
83         if (id.typeElement == elementPrst)
84             item = new ListWidgetItem(QString("%1:%2 %3")
85                                       .arg(sm->get(id, champ_wBank).wValue, 3, 10, QChar('0'))
86                                       .arg(sm->get(id, champ_wPreset).wValue, 3, 10, QChar('0'))
87                                       .arg(sm->getQstr(id, champ_name)));
88         else
89             item = new ListWidgetItem(sm->getQstr(id, champ_name));
90         item->id = id;
91         ui->listWidget->addItem(item);
92         if (i == selectedId)
93             selectedItem = item;
94     }
95     ui->listWidget->sortItems();
96 
97     // Selection
98     if (selectedItem != nullptr)
99         ui->listWidget->setCurrentItem(selectedItem);
100 
101     // Display the dialog
102     this->show();
103 
104     // Scroll
105     if (selectedItem != nullptr)
106         ui->listWidget->scrollToItem(selectedItem, QAbstractItemView::PositionAtCenter);
107 }
108 
on_pushCancel_clicked()109 void DialogList::on_pushCancel_clicked()
110 {
111     QDialog::reject();
112 }
113 
on_pushOk_clicked()114 void DialogList::on_pushOk_clicked()
115 {
116     // élément sélectionné ?
117     if (ui->listWidget->selectedItems().count())
118     {
119         ListWidgetItem *item = dynamic_cast<ListWidgetItem *>(ui->listWidget->currentItem());
120         emit(elementSelected(item->id, _isAssociation));
121     }
122     QDialog::accept();
123 }
124 
on_listWidget_itemDoubleClicked(QListWidgetItem * item)125 void DialogList::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
126 {
127     Q_UNUSED(item)
128     on_pushOk_clicked();
129 }
130