/apps/src/libvideostitch-gui/widgets/welcome/projectselectionwidget.cpp

https://github.com/stitchEm/stitchEm · C++ · 95 lines · 83 code · 10 blank · 2 comment · 14 complexity · 9d0c56c6795431067de7d6da2dced03f MD5 · raw file

  1. // Copyright (c) 2012-2017 VideoStitch SAS
  2. // Copyright (c) 2018 stitchEm
  3. #include "projectselectionwidget.hpp"
  4. #include "ui_projectselectionwidget.h"
  5. #include "mainwindow/vssettings.hpp"
  6. #include "libvideostitch-base/file.hpp"
  7. #include <QFileInfo>
  8. #include <QDropEvent>
  9. #include <QMimeData>
  10. #include <QPair>
  11. #include <QDesktopServices>
  12. static const int FULL_PATH(1);
  13. static const int ITEM_HEIGHT(30);
  14. static const int LIST_MARGIN(20);
  15. ProjectSelectionWidget::ProjectSelectionWidget(QWidget *parent) : QFrame(parent), ui(new Ui::ProjectSelectionWidget) {
  16. ui->setupUi(this);
  17. connect(ui->buttonOpenProject, &QPushButton::clicked, this, &ProjectSelectionWidget::notifyProjectOpened);
  18. connect(ui->buttonNewProject, &QPushButton::clicked, this, &ProjectSelectionWidget::notifyNewProject);
  19. connect(ui->listRecentlyOpened, &QListWidget::clicked, this, &ProjectSelectionWidget::onProjectSelected);
  20. ui->dropArea->setAcceptDrops(true);
  21. ui->dropArea->installEventFilter(this);
  22. ui->labelDropIcon->hide();
  23. ui->labelDropHere->hide();
  24. ui->buttonNewProject->setProperty("vs-button-action", true);
  25. ui->buttonOpenProject->setProperty("vs-button-action", true);
  26. const QString supported = ui->labelDragDropHere->text();
  27. QString formats;
  28. if (QCoreApplication::applicationName() == VIDEOSTITCH_STUDIO_APP_NAME) {
  29. formats = tr("media, PTV or PTVB");
  30. } else {
  31. formats = tr("VAH");
  32. }
  33. ui->labelDragDropHere->setText(supported.arg(formats));
  34. loadRecentProjects();
  35. }
  36. ProjectSelectionWidget::~ProjectSelectionWidget() { delete ui; }
  37. void ProjectSelectionWidget::onContentUpdated() { loadRecentProjects(); }
  38. void ProjectSelectionWidget::onProjectSelected(const QModelIndex &index) {
  39. emit notifyProjectSelected(index.data(FULL_PATH).toString());
  40. }
  41. void ProjectSelectionWidget::onSampleSelected(const QModelIndex &index) {
  42. QDesktopServices::openUrl(index.data(FULL_PATH).toString());
  43. }
  44. bool ProjectSelectionWidget::eventFilter(QObject *watched, QEvent *event) {
  45. if (watched == ui->dropArea) {
  46. if (event->type() == QEvent::Type::DragEnter) {
  47. event->setAccepted(true);
  48. ui->labelDropIcon->show();
  49. ui->labelDragIcon->hide();
  50. ui->labelDropHere->show();
  51. ui->labelDragDropHere->hide();
  52. } else if (event->type() == QEvent::Type::DragLeave) {
  53. ui->labelDropIcon->hide();
  54. ui->labelDragIcon->show();
  55. ui->labelDropHere->hide();
  56. ui->labelDragDropHere->show();
  57. } else if (event->type() == QEvent::Type::Drop) {
  58. QDropEvent *dropEvent = static_cast<QDropEvent *>(event);
  59. emit notifyFilesDropped(dropEvent);
  60. ui->labelDropIcon->hide();
  61. ui->labelDragIcon->show();
  62. ui->labelDropHere->hide();
  63. ui->labelDragDropHere->show();
  64. }
  65. }
  66. return QObject::eventFilter(watched, event);
  67. }
  68. void ProjectSelectionWidget::loadRecentProjects() {
  69. ui->listRecentlyOpened->clear();
  70. const QStringList files = VSSettings::getSettings()->getRecentFileList();
  71. foreach (auto name, files) {
  72. if (QFileInfo(name).exists()) {
  73. const QString text = tr("%0. %1").arg(ui->listRecentlyOpened->count() + 1).arg(File::strippedName(name));
  74. QListWidgetItem *item = new QListWidgetItem(text, ui->listRecentlyOpened);
  75. item->setTextAlignment(Qt::AlignVCenter);
  76. item->setData(FULL_PATH, name);
  77. item->setSizeHint(QSize(ui->listRecentlyOpened->width() - 10, ITEM_HEIGHT));
  78. ui->listRecentlyOpened->addItem(item);
  79. }
  80. }
  81. ui->labelRecentTitle->setVisible(!files.isEmpty());
  82. ui->listRecentlyOpened->setVisible(!files.isEmpty());
  83. ui->listRecentlyOpened->adjustSize();
  84. ui->listRecentlyOpened->setFixedHeight(ui->listRecentlyOpened->count() * ITEM_HEIGHT + LIST_MARGIN);
  85. }