/sources/repository/widgets/downloadprogresscell.cpp

https://github.com/davy7125/polyphone · C++ · 109 lines · 67 code · 12 blank · 30 comment · 6 complexity · 3243d9ad7b462ba4c51da6f168430304 MD5 · raw file

  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. #include "downloadprogresscell.h"
  25. #include "ui_downloadprogresscell.h"
  26. #include "contextmanager.h"
  27. #include "downloadmanager.h"
  28. #include "windowmanager.h"
  29. #include "inputfactory.h"
  30. #include <QFileInfo>
  31. #include <QDesktopServices>
  32. #include <QUrl>
  33. #include <QMessageBox>
  34. DownloadProgressCell::DownloadProgressCell(int soundfontId, QString soundfontName, QWidget *parent) :
  35. QWidget(parent),
  36. ui(new Ui::DownloadProgressCell),
  37. _soundfontId(soundfontId),
  38. _soundfontName(soundfontName),
  39. _percent(0)
  40. {
  41. ui->setupUi(this);
  42. // Style
  43. ui->pushCancel->setIcon(ContextManager::theme()->getColoredSvg(":/icons/close.svg", QSize(16, 16), ThemeManager::LIST_TEXT));
  44. ui->pushOpen->setIcon(ContextManager::theme()->getColoredSvg(":/icons/document-open.svg", QSize(16, 16), ThemeManager::LIST_TEXT));
  45. ui->pushOpen->hide();
  46. ui->iconFile->setPixmap(ContextManager::theme()->getColoredSvg(":/icons/file-description.svg", QSize(16, 16), ThemeManager::LIST_TEXT));
  47. // Data
  48. ui->labelTitle->setText(_soundfontName);
  49. }
  50. DownloadProgressCell::~DownloadProgressCell()
  51. {
  52. delete ui;
  53. }
  54. void DownloadProgressCell::progressChanged(int percent, QString finalFilename)
  55. {
  56. // Update data
  57. _percent = percent;
  58. _filename = finalFilename;
  59. // Update GUI
  60. ui->labelPercent->setText(QString::number(_percent) + "%");
  61. if (finalFilename != "")
  62. {
  63. ui->pushOpen->setToolTip(tr("Open \"%1\"").arg(_filename));
  64. ui->pushCancel->hide();
  65. ui->pushOpen->show();
  66. }
  67. }
  68. void DownloadProgressCell::on_pushOpen_clicked()
  69. {
  70. if (_filename == "")
  71. return;
  72. // Is it possible to open the file?
  73. if (InputFactory::isSuffixSupported(QFileInfo(_filename).suffix()))
  74. WindowManager::getInstance()->openSoundfont(_filename);
  75. else
  76. {
  77. if (!QDesktopServices::openUrl(QUrl(_filename, QUrl::TolerantMode)))
  78. {
  79. // Warning message
  80. QMessageBox::warning(QApplication::activeWindow(), tr("Warning"),
  81. tr("Couldn't open file \"%1\". If this is an archive, you may have to extract it first.").arg(_filename));
  82. }
  83. }
  84. emit(closeMenu());
  85. }
  86. void DownloadProgressCell::on_pushCancel_clicked()
  87. {
  88. DownloadManager::getInstance()->cancel(_soundfontId);
  89. }
  90. void DownloadProgressCell::cancel()
  91. {
  92. // Nothing more to do
  93. _percent = 100;
  94. ui->pushCancel->setIcon(ContextManager::theme()->getColoredSvg(":/icons/canceled.svg", QSize(16, 16), ThemeManager::LIST_TEXT));
  95. ui->pushCancel->setToolTip(tr("Download canceled"));
  96. ui->pushCancel->setEnabled(false);
  97. }