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 "soundfonteditorfiles.h"
26 #include "ui_soundfonteditorfiles.h"
27 #include "soundfontdownloaddata.h"
28 #include "soundfontfilecell.h"
29 #include "contextmanager.h"
30 #include <QFileDialog>
31 #include <QMessageBox>
32
33 const int SoundfontEditorFiles::MAX_FILES = 10;
34 const int SoundfontEditorFiles::MAX_MB = 100;
35
SoundfontEditorFiles(QWidget * parent)36 SoundfontEditorFiles::SoundfontEditorFiles(QWidget *parent) :
37 QWidget(parent),
38 ui(new Ui::SoundfontEditorFiles),
39 _newFileIndex(-1)
40 {
41 ui->setupUi(this);
42 }
43
~SoundfontEditorFiles()44 SoundfontEditorFiles::~SoundfontEditorFiles()
45 {
46 while (!_cells.isEmpty())
47 delete _cells.take(_cells.keys()[0]);
48 delete ui;
49 }
50
sortFunction(const SoundfontDownloadData * s1,const SoundfontDownloadData * s2)51 static bool sortFunction(const SoundfontDownloadData *s1, const SoundfontDownloadData *s2)
52 {
53 return s1->getOrdering() < s2->getOrdering();
54 }
55
initialize(QList<SoundfontDownloadData * > data)56 void SoundfontEditorFiles::initialize(QList<SoundfontDownloadData *> data)
57 {
58 // First order the list
59 qSort(data.begin(), data.end(), sortFunction);
60
61 // Clear and add each file information
62 while (!_cells.isEmpty())
63 delete _cells.take(_cells.keys()[0]);
64 for (int i = 0; i < data.count(); i++)
65 {
66 SoundfontFileCell * cell = new SoundfontFileCell(this);
67 connect(cell, SIGNAL(removed(int)), this, SLOT(deleteCell(int)));
68 connect(cell, SIGNAL(replaced(int,QString)), this, SLOT(replaceCell(int,QString)));
69 cell->initialize(data[i]);
70 ui->verticalLayout->addWidget(cell);
71 _cells[data[i]->getId()] = cell;
72 }
73 }
74
addFile()75 void SoundfontEditorFiles::addFile()
76 {
77 // 10 files max
78 if (_cells.count() >= MAX_FILES)
79 {
80 QMessageBox::warning(this, tr("Warning"), tr("The number of files cannot exceed %0.").arg(MAX_FILES));
81 return;
82 }
83
84 // Get a new file
85 QString newFilePath = getFilePath(ContextManager::recentFile()->getLastDirectory(RecentFileManager::FILE_TYPE_UPLOAD));
86 if (newFilePath.isEmpty())
87 return;
88
89 // Add a cell
90 SoundfontFileCell * cell = new SoundfontFileCell(this);
91 connect(cell, SIGNAL(removed(int)), this, SLOT(deleteCell(int)));
92 connect(cell, SIGNAL(replaced(int,QString)), this, SLOT(replaceCell(int,QString)));
93 cell->initialize(newFilePath, _newFileIndex);
94 ui->verticalLayout->addWidget(cell);
95 _cells[_newFileIndex] = cell;
96 _newFileIndex--;
97 }
98
replaceCell(int id,QString previousFilePath)99 void SoundfontEditorFiles::replaceCell(int id, QString previousFilePath)
100 {
101 // Check that the id is known
102 if (!_cells.contains(id))
103 return;
104
105 // Get a new file
106 if (previousFilePath.isEmpty())
107 previousFilePath = ContextManager::recentFile()->getLastDirectory(RecentFileManager::FILE_TYPE_UPLOAD);
108 QString newFilePath = getFilePath(previousFilePath);
109 if (newFilePath.isEmpty() || newFilePath == previousFilePath)
110 return;
111
112 // Replace it in the cell
113 _cells[id]->fileReplaced(newFilePath);
114 }
115
deleteCell(int id)116 void SoundfontEditorFiles::deleteCell(int id)
117 {
118 if (_cells.contains(id))
119 delete _cells.take(id);
120 }
121
getFilePath(QString path)122 QString SoundfontEditorFiles::getFilePath(QString path)
123 {
124 // Open an executable
125 QString newFile = QFileDialog::getOpenFileName(this, tr("File to upload"), path, tr("Files") + " (*.sf2 *.sf3 *.doc *.odt *.pdf *.txt *.jpg *.png *.zip *.rar)");
126 if (newFile.isEmpty())
127 return "";
128 QFileInfo info(newFile);
129 if (!info.exists())
130 return "";
131
132 // Add the selected file in the recent files
133 ContextManager::recentFile()->addRecentFile(RecentFileManager::FILE_TYPE_UPLOAD, newFile);
134
135 // Check the size
136 if (info.size() > MAX_MB * 1000000)
137 {
138 QMessageBox::warning(this, tr("Warning"), tr("The file size cannot exceed %0 MB.").arg(MAX_MB));
139 return "";
140 }
141
142 return newFile;
143 }
144
fillArguments(QMap<QString,QString> & arguments)145 void SoundfontEditorFiles::fillArguments(QMap<QString, QString> &arguments)
146 {
147 int count = 0;
148 arguments["file_number"] = QString::number(_cells.count());
149 foreach (int id, _cells.keys())
150 {
151 QString prefix = "file_" + QString::number(count) + "_";
152 arguments[prefix + "id"] = (id >= 0) ? QString::number(id) : "";
153 arguments[prefix + "title"] = _cells[id]->getTitle();
154 arguments[prefix + "description"] = _cells[id]->getDescription();
155 arguments[prefix + "name"] = QFileInfo(_cells[id]->getFilePath()).fileName();
156 count++;
157 }
158 }
159
getFileArguments()160 QMap<QString, QString> SoundfontEditorFiles::getFileArguments()
161 {
162 QMap<QString, QString> map;
163 int count = 0;
164 foreach (int id, _cells.keys())
165 {
166 QString prefix = "file_" + QString::number(count) + "_";
167 map[prefix + "data"] = _cells[id]->getFilePath();
168 count++;
169 }
170 return map;
171 }
172