PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/desktop-widgets/tab-widgets/TabDivePhotos.cpp

https://github.com/dirkhh/subsurface
C++ | 170 lines | 147 code | 18 blank | 5 comment | 17 complexity | 3321a98e52a29656a9b49f7944ca9c61 MD5 | raw file
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "TabDivePhotos.h"
  3. #include "ui_TabDivePhotos.h"
  4. #include "core/imagedownloader.h"
  5. #include <qt-models/divepicturemodel.h>
  6. #include <QDesktopServices>
  7. #include <QContextMenuEvent>
  8. #include <QMenu>
  9. #include <QUrl>
  10. #include <QMessageBox>
  11. #include <QFileInfo>
  12. #include "core/save-profiledata.h"
  13. #include "core/membuffer.h"
  14. //TODO: Remove those in the future.
  15. #include "../mainwindow.h"
  16. #include "../divelistview.h"
  17. TabDivePhotos::TabDivePhotos(QWidget *parent)
  18. : TabBase(parent),
  19. ui(new Ui::TabDivePhotos()),
  20. divePictureModel(DivePictureModel::instance())
  21. {
  22. ui->setupUi(this);
  23. ui->photosView->setModel(divePictureModel);
  24. ui->photosView->setSelectionMode(QAbstractItemView::ExtendedSelection);
  25. ui->photosView->setSelectionBehavior(QAbstractItemView::SelectRows);
  26. ui->photosView->setResizeMode(QListView::Adjust);
  27. connect(ui->photosView, &DivePictureWidget::photoDoubleClicked,
  28. [](const QString& path) {
  29. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  30. }
  31. );
  32. connect(ui->photosView, &DivePictureWidget::zoomLevelChanged,
  33. this, &TabDivePhotos::changeZoomLevel);
  34. connect(ui->zoomSlider, &QAbstractSlider::valueChanged,
  35. DivePictureModel::instance(), &DivePictureModel::setZoomLevel);
  36. }
  37. TabDivePhotos::~TabDivePhotos()
  38. {
  39. delete ui;
  40. }
  41. void TabDivePhotos::clear()
  42. {
  43. updateData();
  44. }
  45. void TabDivePhotos::contextMenuEvent(QContextMenuEvent *event)
  46. {
  47. QMenu popup(this);
  48. popup.addAction(tr("Load media from file(s)"), this, &TabDivePhotos::addPhotosFromFile);
  49. popup.addAction(tr("Load media file(s) from web"), this, &TabDivePhotos::addPhotosFromURL);
  50. popup.addSeparator();
  51. popup.addAction(tr("Delete selected media files"), this, &TabDivePhotos::removeSelectedPhotos);
  52. popup.addAction(tr("Delete all media files"), this, &TabDivePhotos::removeAllPhotos);
  53. popup.addAction(tr("Open folder of selected media files"), this, &TabDivePhotos::openFolderOfSelectedFiles);
  54. popup.addAction(tr("Recalculate selected thumbnails"), this, &TabDivePhotos::recalculateSelectedThumbnails);
  55. popup.addAction(tr("Save dive data as subtitles"), this, &TabDivePhotos::saveSubtitles);
  56. popup.exec(event->globalPos());
  57. event->accept();
  58. }
  59. QVector<QString> TabDivePhotos::getSelectedFilenames() const
  60. {
  61. QVector<QString> selectedPhotos;
  62. if (!ui->photosView->selectionModel()->hasSelection())
  63. return selectedPhotos;
  64. QModelIndexList indices = ui->photosView->selectionModel()->selectedRows();
  65. selectedPhotos.reserve(indices.count());
  66. for (const auto &photo: indices) {
  67. if (photo.isValid()) {
  68. QString fileUrl = photo.data(Qt::DisplayPropertyRole).toString();
  69. if (!fileUrl.isEmpty())
  70. selectedPhotos.push_back(fileUrl);
  71. }
  72. }
  73. return selectedPhotos;
  74. }
  75. void TabDivePhotos::removeSelectedPhotos()
  76. {
  77. QModelIndexList indices = ui->photosView->selectionModel()->selectedRows();
  78. DivePictureModel::instance()->removePictures(indices);
  79. }
  80. void TabDivePhotos::openFolderOfSelectedFiles()
  81. {
  82. QVector<QString> directories;
  83. for (const QString &filename: getSelectedFilenames()) {
  84. QFileInfo info(filename);
  85. if (!info.exists())
  86. continue;
  87. QString path = info.absolutePath();
  88. if (path.isEmpty() || directories.contains(path))
  89. continue;
  90. directories.append(path);
  91. }
  92. for (const QString &dir: directories)
  93. QDesktopServices::openUrl(QUrl::fromLocalFile(dir));
  94. }
  95. void TabDivePhotos::recalculateSelectedThumbnails()
  96. {
  97. Thumbnailer::instance()->calculateThumbnails(getSelectedFilenames());
  98. }
  99. void TabDivePhotos::saveSubtitles()
  100. {
  101. if (!current_dive)
  102. return;
  103. if (!ui->photosView->selectionModel()->hasSelection())
  104. return;
  105. QModelIndexList indices = ui->photosView->selectionModel()->selectedRows();
  106. for (const auto &photo: indices) {
  107. if (photo.isValid()) {
  108. QString fileUrl = photo.data(Qt::DisplayPropertyRole).toString();
  109. if (!fileUrl.isEmpty()) {
  110. QFileInfo fi = QFileInfo(fileUrl);
  111. QFile subtitlefile;
  112. subtitlefile.setFileName(QString(fi.path()) + "/" + fi.completeBaseName() + ".ass");
  113. int offset = photo.data(Qt::UserRole + 1).toInt();
  114. int duration = photo.data(Qt::UserRole + 2).toInt();
  115. // Only videos have non-zero duration
  116. if (!duration)
  117. continue;
  118. struct membufferpp b;
  119. save_subtitles_buffer(&b, current_dive, offset, duration);
  120. const char *data = mb_cstring(&b);
  121. subtitlefile.open(QIODevice::WriteOnly);
  122. subtitlefile.write(data, strlen(data));
  123. subtitlefile.close();
  124. }
  125. }
  126. }
  127. }
  128. //TODO: This looks overly wrong. We shouldn't call MainWindow to retrieve the DiveList to add Images.
  129. void TabDivePhotos::addPhotosFromFile()
  130. {
  131. MainWindow::instance()->diveList->loadImages();
  132. }
  133. void TabDivePhotos::addPhotosFromURL()
  134. {
  135. MainWindow::instance()->diveList->loadWebImages();
  136. }
  137. void TabDivePhotos::removeAllPhotos()
  138. {
  139. if (QMessageBox::warning(this, tr("Deleting media files"), tr("Are you sure you want to delete all media files?"), QMessageBox::Cancel | QMessageBox::Ok, QMessageBox::Cancel) != QMessageBox::Cancel ) {
  140. ui->photosView->selectAll();
  141. removeSelectedPhotos();
  142. }
  143. }
  144. void TabDivePhotos::updateData()
  145. {
  146. divePictureModel->updateDivePictures();
  147. }
  148. void TabDivePhotos::changeZoomLevel(int delta)
  149. {
  150. // We count on QSlider doing bound checks
  151. ui->zoomSlider->setValue(ui->zoomSlider->value() + delta);
  152. }