|
| 1 | +#include <QFile> |
| 2 | +#include <fstream> |
| 3 | + |
| 4 | +#include <QDropEvent> |
| 5 | +#include <QMimeData> |
| 6 | + |
| 7 | +#include <QMessageBox> |
| 8 | +#include <QInputDialog> |
| 9 | +#include <QFileDialog> |
| 10 | + |
| 11 | +#include "ConverterWidget.h" |
| 12 | + |
| 13 | + |
| 14 | +////////////// Initialize widget |
| 15 | + |
| 16 | + |
| 17 | +ConverterWidget::ConverterWidget (RecordingsManagerWidget* fileManagerTab) : QWidget () |
| 18 | +{ |
| 19 | + fileManager = fileManagerTab; |
| 20 | + |
| 21 | + setAcceptDrops (true); |
| 22 | + |
| 23 | + converter = new Converter; |
| 24 | + connect (converter, SIGNAL (duplicateFile (QString&, bool&)), this, SLOT (askForNewFileName (QString&, bool&)), Qt::BlockingQueuedConnection); |
| 25 | + connect (converter, SIGNAL (progress (unsigned short int)), this, SLOT (updateProgressBar (unsigned short int)), Qt::BlockingQueuedConnection); |
| 26 | + connect (converter, SIGNAL (finishedConverting (const QStringList&)), this, SLOT (reactivateUI (const QStringList&))); |
| 27 | + connect (converter, SIGNAL (nextFileReached (const QString&)), this, SLOT (updateCurrentFileLabel (const QString&))); |
| 28 | + |
| 29 | + layout = new QVBoxLayout (this); |
| 30 | + |
| 31 | + |
| 32 | + helpLabel = new QLabel (tr("You can easily convert your audio files thanks to this tool.\nSelect the files to convert or just drag and drop them to the window !")); |
| 33 | + helpLabel->setAlignment (Qt::AlignCenter); |
| 34 | + |
| 35 | + |
| 36 | + initFilesWidget (); |
| 37 | + |
| 38 | + initOptionsBox (); |
| 39 | + |
| 40 | + |
| 41 | + bStart = new QPushButton (tr("&Start")); |
| 42 | + connect (bStart, SIGNAL (clicked ()), this, SLOT (start ())); |
| 43 | + |
| 44 | + currentFileLabel = new QLabel; |
| 45 | + currentFileLabel->setAlignment (Qt::AlignCenter); |
| 46 | + currentFileLabel->hide (); |
| 47 | + |
| 48 | + progressBar = new QProgressBar; |
| 49 | + progressBar->setRange (0, 100); |
| 50 | + progressBar->hide (); |
| 51 | + |
| 52 | + |
| 53 | + layout->addWidget (helpLabel); |
| 54 | + |
| 55 | + layout->addWidget (filesWidget); |
| 56 | + |
| 57 | + layout->addWidget (optionsBox); |
| 58 | + |
| 59 | + layout->addWidget (bStart); |
| 60 | + layout->addWidget (currentFileLabel); |
| 61 | + layout->addWidget (progressBar); |
| 62 | + |
| 63 | + |
| 64 | + loadOptions (); |
| 65 | + updateUI (); |
| 66 | +} |
| 67 | + |
| 68 | +void ConverterWidget::initFilesWidget () |
| 69 | +{ |
| 70 | + filesWidget = new QWidget; |
| 71 | + filesWidgetLayout = new QGridLayout (filesWidget); |
| 72 | + |
| 73 | + |
| 74 | + filesList = new QListWidget; |
| 75 | + filesList->setSortingEnabled (true); |
| 76 | + connect (filesList, SIGNAL (itemClicked (QListWidgetItem*)), this, SLOT (onFileClicked ())); |
| 77 | + |
| 78 | + bAddFiles = new QPushButton (tr("&Add files")); |
| 79 | + connect (bAddFiles, SIGNAL (clicked ()), this, SLOT (addFiles ())); |
| 80 | + |
| 81 | + bRemoveFile = new QPushButton (tr("&Remove")); |
| 82 | + connect (bRemoveFile, SIGNAL (clicked ()), this, SLOT (removeFile ())); |
| 83 | + |
| 84 | + bClear = new QPushButton (tr("&Clear list")); |
| 85 | + connect (bClear, SIGNAL (clicked ()), this, SLOT (clear ())); |
| 86 | + |
| 87 | + |
| 88 | + filesWidgetLayout->addWidget (filesList, 0, 0, 3, 1); |
| 89 | + filesWidgetLayout->addWidget (bAddFiles, 0, 1); |
| 90 | + filesWidgetLayout->addWidget (bRemoveFile, 1, 1); |
| 91 | + filesWidgetLayout->addWidget (bClear, 2, 1); |
| 92 | +} |
| 93 | + |
| 94 | +void ConverterWidget::initOptionsBox () |
| 95 | +{ |
| 96 | + optionsBox = new QGroupBox (tr("Options")); |
| 97 | + optionsBoxLayout = new QGridLayout (optionsBox); |
| 98 | + optionsBoxLayout->setAlignment (Qt::AlignLeft); |
| 99 | + |
| 100 | + |
| 101 | + chooseCodecLabel = new QLabel (tr("Destination codec :")); |
| 102 | + codecSelecter = new QComboBox; |
| 103 | + codecSelecter->addItem (tr("Vorbis (OGG) : compressed, good quality"), QVariant ("ogg")); |
| 104 | + codecSelecter->addItem (tr("FLAC : compressed, best quality"), QVariant ("flac")); |
| 105 | + codecSelecter->addItem (tr("PCM (WAV) : not compressed, best quality"), QVariant ("wav")); |
| 106 | + |
| 107 | + chooseSpeedLabel = new QLabel (tr("Conversion speed :")); |
| 108 | + speedSelecter = new QSpinBox; |
| 109 | + speedSelecter->setMinimum (100); |
| 110 | + speedSelecter->setMaximum (10000); |
| 111 | + |
| 112 | + bResetSettings = new QPushButton (tr("Reset conversion settings")); |
| 113 | + connect (bResetSettings, SIGNAL (clicked ()), this, SLOT (resetSettings ())); |
| 114 | + |
| 115 | + |
| 116 | + optionsBoxLayout->addWidget (chooseCodecLabel, 0, 0); |
| 117 | + optionsBoxLayout->addWidget (codecSelecter, 0, 1); |
| 118 | + optionsBoxLayout->addWidget (chooseSpeedLabel, 1, 0); |
| 119 | + optionsBoxLayout->addWidget (speedSelecter, 1, 1); |
| 120 | + optionsBoxLayout->addWidget (bResetSettings, 2, 0); |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +void ConverterWidget::loadOptions () |
| 125 | +{ |
| 126 | + QStringList settings = {"0", "1000"}; |
| 127 | + |
| 128 | + |
| 129 | + QFile settingsFile ("Converter Options.pastouche"); |
| 130 | + |
| 131 | + if (settingsFile.open (QIODevice::ReadOnly | QIODevice::Text)) |
| 132 | + { |
| 133 | + QStringList readSettings = QString (settingsFile.readAll ()).split ("\n"); |
| 134 | + |
| 135 | + if (readSettings.length () == settings.length ()) |
| 136 | + settings = readSettings; |
| 137 | + } |
| 138 | + |
| 139 | + codecSelecter->setCurrentIndex (settings.at (0).toUShort ()); |
| 140 | + speedSelecter->setValue (settings.at (1).toUInt ()); |
| 141 | +} |
| 142 | + |
| 143 | +ConverterWidget::~ConverterWidget () |
| 144 | +{ |
| 145 | + std::ofstream settingsFile ("Converter Options.pastouche"); |
| 146 | + |
| 147 | + if (settingsFile) |
| 148 | + settingsFile<<codecSelecter->currentIndex ()<<"\n" |
| 149 | + <<speedSelecter->value (); |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +////////////// Slots |
| 154 | + |
| 155 | + |
| 156 | +void ConverterWidget::resetSettings () |
| 157 | +{ |
| 158 | + if (QMessageBox::question (this, tr("Confirmation"), tr("Do you really want to go back\nto recommanded settings ?")) == QMessageBox::Yes) |
| 159 | + { |
| 160 | + codecSelecter->setCurrentIndex (0); |
| 161 | + speedSelecter->setValue (1000); |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | + |
| 166 | +void ConverterWidget::onFileClicked () |
| 167 | +{ |
| 168 | + bRemoveFile->setEnabled (true); |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +void ConverterWidget::addFile (const QString& newFile) |
| 173 | +{ |
| 174 | + if (filesList->findItems (newFile, Qt::MatchExactly).length () == 0) |
| 175 | + filesList->addItem (newFile); |
| 176 | + |
| 177 | + updateUI (); |
| 178 | +} |
| 179 | + |
| 180 | +void ConverterWidget::addFiles () |
| 181 | +{ |
| 182 | + QStringList files = QFileDialog::getOpenFileNames (this, tr("Add files to conversion list"), "", tr("Audio files (*.ogg *.flac *.wav)"), nullptr, QFileDialog::DontUseNativeDialog); |
| 183 | + |
| 184 | + for (short int i = 0 ; i != files.length () ; i++) |
| 185 | + { |
| 186 | + if (filesList->findItems (files.at (i), Qt::MatchExactly).length () == 0) |
| 187 | + filesList->addItem (files.at (i)); |
| 188 | + } |
| 189 | + |
| 190 | + updateUI (); |
| 191 | +} |
| 192 | + |
| 193 | +void ConverterWidget::removeFile () |
| 194 | +{ |
| 195 | + filesList->takeItem (filesList->currentRow ()); |
| 196 | + |
| 197 | + updateUI (); |
| 198 | +} |
| 199 | + |
| 200 | +void ConverterWidget::clear () |
| 201 | +{ |
| 202 | + filesList->clear (); |
| 203 | + |
| 204 | + updateUI (); |
| 205 | +} |
| 206 | + |
| 207 | + |
| 208 | +void ConverterWidget::start () |
| 209 | +{ |
| 210 | + progressBar->setValue (0); |
| 211 | + |
| 212 | + |
| 213 | + QStringList files; |
| 214 | + |
| 215 | + for (unsigned short int i = 0 ; i != filesList->count () ; i++) |
| 216 | + files += filesList->item (i)->text (); |
| 217 | + |
| 218 | + |
| 219 | + QStringList outputFiles (files); |
| 220 | + bool ok = true; |
| 221 | + |
| 222 | + for (unsigned short int i = 0 ; i != files.count () ; i++) |
| 223 | + { |
| 224 | + outputFiles[i] = QFileInfo (files.at (i)).dir ().path () + "/" + |
| 225 | + QFileInfo (files.at (i)).baseName () + " -conv." + codecSelecter->currentData ().toString (); |
| 226 | + |
| 227 | + while (QFile::exists (outputFiles.at (i))) |
| 228 | + { |
| 229 | + outputFiles[i] = QFileInfo (files.at (i)).dir ().path () + "/" + |
| 230 | + QInputDialog::getText (this, tr("Confirmation"), outputFiles.at (i) + tr("\nalready exists, could you give it another name ?"), QLineEdit::Normal, QFileInfo (outputFiles.at (i)).baseName (), &ok) + |
| 231 | + "." + codecSelecter->currentData ().toString (); |
| 232 | + |
| 233 | + if (!ok) |
| 234 | + { |
| 235 | + outputFiles[i] = ""; |
| 236 | + files[i] = ""; |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + outputFiles.removeAll (""); |
| 241 | + files.removeAll (""); |
| 242 | + |
| 243 | + |
| 244 | + if (files.length () > 0) |
| 245 | + { |
| 246 | + progressBar->show (); |
| 247 | + currentFileLabel->show (); |
| 248 | + setOptionsEnabled (false); |
| 249 | + |
| 250 | + converter->convert (files, outputFiles, speedSelecter->value ()); |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | + |
| 255 | +void ConverterWidget::dragEnterEvent (QDragEnterEvent* event) |
| 256 | +{ |
| 257 | + if (event->mimeData ()->hasUrls ()) |
| 258 | + event->acceptProposedAction (); |
| 259 | + |
| 260 | + else |
| 261 | + event->ignore (); |
| 262 | +} |
| 263 | + |
| 264 | +void ConverterWidget::dropEvent (QDropEvent* event) |
| 265 | +{ |
| 266 | + if (event->mimeData ()->hasUrls ()) |
| 267 | + { |
| 268 | + QList<QUrl> droppedFiles (event->mimeData ()->urls ()); |
| 269 | + QString currentFile; |
| 270 | + |
| 271 | + for (short int i = 0 ; i != droppedFiles.length () ; i++) |
| 272 | + { |
| 273 | + currentFile = droppedFiles.at (i).toString ().remove ("file:///"); |
| 274 | + |
| 275 | + if (QStringList ({"ogg", "flac", "wav"}).contains (QFileInfo (currentFile).suffix ().toLower ())) |
| 276 | + addFile (currentFile); |
| 277 | + |
| 278 | + else |
| 279 | + QMessageBox::warning (this, tr("Error"), tr("Impossible to import ") + currentFile + tr(",\nyou can only import OGG, FLAC and WAV files !")); |
| 280 | + } |
| 281 | + |
| 282 | + updateUI (); |
| 283 | + event->acceptProposedAction (); |
| 284 | + } |
| 285 | + else |
| 286 | + event->ignore (); |
| 287 | +} |
| 288 | + |
| 289 | + |
| 290 | +void ConverterWidget::updateUI () |
| 291 | +{ |
| 292 | + if (filesList->count () == 0) |
| 293 | + { |
| 294 | + bRemoveFile->setEnabled (false); |
| 295 | + bClear->setEnabled (false); |
| 296 | + |
| 297 | + bStart->setEnabled (false); |
| 298 | + } |
| 299 | + else |
| 300 | + { |
| 301 | + bClear->setEnabled (true); |
| 302 | + |
| 303 | + bStart->setEnabled (true); |
| 304 | + } |
| 305 | +} |
| 306 | + |
| 307 | +void ConverterWidget::setOptionsEnabled (bool state) |
| 308 | +{ |
| 309 | + helpLabel->setEnabled (state); |
| 310 | + |
| 311 | + filesList->setEnabled (state); |
| 312 | + bAddFiles->setEnabled (state); |
| 313 | + bRemoveFile->setEnabled (state); |
| 314 | + bClear->setEnabled (state); |
| 315 | + |
| 316 | + optionsBox->setEnabled (state); |
| 317 | + |
| 318 | + bStart->setEnabled (state); |
| 319 | + |
| 320 | + if (filesList->currentItem () == nullptr) |
| 321 | + bRemoveFile->setEnabled (false); |
| 322 | +} |
| 323 | + |
| 324 | +void ConverterWidget::reactivateUI (const QStringList& outputFiles) |
| 325 | +{ |
| 326 | + if (QMessageBox::question (this, tr("Conversion's finished !"), tr("All your files are converted !\nDo you want to add them to your recordings ?")) == QMessageBox::Yes) |
| 327 | + for (short int i = 0 ; i != outputFiles.length () ; i++) |
| 328 | + fileManager->addRecording (outputFiles.at (i)); |
| 329 | + |
| 330 | + setOptionsEnabled (true); |
| 331 | + |
| 332 | + progressBar->hide (); |
| 333 | + currentFileLabel->hide (); |
| 334 | +} |
| 335 | + |
| 336 | + |
| 337 | +////////////// Conversion slots |
| 338 | + |
| 339 | + |
| 340 | +void ConverterWidget::updateProgressBar (unsigned short int progression) |
| 341 | +{ |
| 342 | + progressBar->setValue (progression); |
| 343 | +} |
| 344 | + |
| 345 | +void ConverterWidget::updateCurrentFileLabel (const QString& currentFile) |
| 346 | +{ |
| 347 | + currentFileLabel->setText (tr("Processing file ") + currentFile); |
| 348 | +} |
| 349 | + |
0 commit comments