PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/torvalds/subsurface
C++ | 174 lines | 150 code | 19 blank | 5 comment | 20 complexity | dd534f0d6fda92e310e69e9ba5916298 MD5 | raw file
Possible License(s): GPL-2.0
  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->setResizeMode(QListView::Adjust);
  26. connect(ui->photosView, &DivePictureWidget::photoDoubleClicked,
  27. [](const QString& path) {
  28. QDesktopServices::openUrl(QUrl::fromLocalFile(path));
  29. }
  30. );
  31. connect(ui->photosView, &DivePictureWidget::zoomLevelChanged,
  32. this, &TabDivePhotos::changeZoomLevel);
  33. connect(ui->zoomSlider, &QAbstractSlider::valueChanged,
  34. DivePictureModel::instance(), &DivePictureModel::setZoomLevel);
  35. }
  36. TabDivePhotos::~TabDivePhotos()
  37. {
  38. delete ui;
  39. }
  40. void TabDivePhotos::clear()
  41. {
  42. updateData();
  43. }
  44. void TabDivePhotos::contextMenuEvent(QContextMenuEvent *event)
  45. {
  46. QMenu popup(this);
  47. popup.addAction(tr("Load media from file(s)"), this, SLOT(addPhotosFromFile()));
  48. popup.addAction(tr("Load media file(s) from web"), this, SLOT(addPhotosFromURL()));
  49. popup.addSeparator();
  50. popup.addAction(tr("Delete selected media files"), this, SLOT(removeSelectedPhotos()));
  51. popup.addAction(tr("Delete all media files"), this, SLOT(removeAllPhotos()));
  52. popup.addAction(tr("Open folder of selected media files"), this, SLOT(openFolderOfSelectedFiles()));
  53. popup.addAction(tr("Recalculate selected thumbnails"), this, SLOT(recalculateSelectedThumbnails()));
  54. popup.addAction(tr("Save dive data as subtitles"), this, SLOT(saveSubtitles()));
  55. popup.exec(event->globalPos());
  56. event->accept();
  57. }
  58. QVector<QString> TabDivePhotos::getSelectedFilenames() const
  59. {
  60. QVector<QString> selectedPhotos;
  61. if (!ui->photosView->selectionModel()->hasSelection())
  62. return selectedPhotos;
  63. QModelIndexList indexes = ui->photosView->selectionModel()->selectedRows();
  64. if (indexes.count() == 0)
  65. indexes = ui->photosView->selectionModel()->selectedIndexes();
  66. selectedPhotos.reserve(indexes.count());
  67. for (const auto &photo: indexes) {
  68. if (photo.isValid()) {
  69. QString fileUrl = photo.data(Qt::DisplayPropertyRole).toString();
  70. if (!fileUrl.isEmpty())
  71. selectedPhotos.push_back(fileUrl);
  72. }
  73. }
  74. return selectedPhotos;
  75. }
  76. void TabDivePhotos::removeSelectedPhotos()
  77. {
  78. DivePictureModel::instance()->removePictures(getSelectedFilenames());
  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. QVector<QString> selectedPhotos;
  102. if (!ui->photosView->selectionModel()->hasSelection())
  103. return;
  104. QModelIndexList indexes = ui->photosView->selectionModel()->selectedRows();
  105. if (indexes.count() == 0)
  106. indexes = ui->photosView->selectionModel()->selectedIndexes();
  107. selectedPhotos.reserve(indexes.count());
  108. for (const auto &photo: indexes) {
  109. if (photo.isValid()) {
  110. QString fileUrl = photo.data(Qt::DisplayPropertyRole).toString();
  111. if (!fileUrl.isEmpty()) {
  112. QFileInfo fi = QFileInfo(fileUrl);
  113. QFile subtitlefile;
  114. subtitlefile.setFileName(QString(fi.path()) + "/" + fi.completeBaseName() + ".ass");
  115. int offset = photo.data(Qt::UserRole + 1).toInt();
  116. int duration = photo.data(Qt::UserRole + 2).toInt();
  117. // Only videos have non-zero duration
  118. if (!duration)
  119. continue;
  120. struct membuffer b = { 0 };
  121. save_subtitles_buffer(&b, &displayed_dive, offset, duration);
  122. char *data = detach_buffer(&b);
  123. subtitlefile.open(QIODevice::WriteOnly);
  124. subtitlefile.write(data, strlen(data));
  125. subtitlefile.close();
  126. free(data);
  127. }
  128. }
  129. }
  130. }
  131. //TODO: This looks overly wrong. We shouldn't call MainWindow to retrieve the DiveList to add Images.
  132. void TabDivePhotos::addPhotosFromFile()
  133. {
  134. MainWindow::instance()->diveList->loadImages();
  135. }
  136. void TabDivePhotos::addPhotosFromURL()
  137. {
  138. MainWindow::instance()->diveList->loadWebImages();
  139. }
  140. void TabDivePhotos::removeAllPhotos()
  141. {
  142. 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 ) {
  143. ui->photosView->selectAll();
  144. removeSelectedPhotos();
  145. }
  146. }
  147. void TabDivePhotos::updateData()
  148. {
  149. divePictureModel->updateDivePictures();
  150. }
  151. void TabDivePhotos::changeZoomLevel(int delta)
  152. {
  153. // We count on QSlider doing bound checks
  154. ui->zoomSlider->setValue(ui->zoomSlider->value() + delta);
  155. }