subsurface/desktop-widgets/tab-widgets/TabDivePhotos.cpp
Berthold Stoeger 9f455b1457 selection: move current dive and divecomputer to selection.cpp
This tries to encapsulate the management of the current dive and
divecomputer in the selection code. The current dive is alreay
set by setSelection(). Add a new parameter to also set the
current divecomputer. If -1 is passed, then the current
computer number is remained. This will allow us to audit the code.
Because for now, the whole "current dive computer" thing seems
to be ill-defined.

This fixes a bug: the dive-computer number wasn't validated
when making a new dive the current dive. The new code has some
drawbacks though: when selecting a whole trip, the validation
will be called for all dives in the trip and thus the dive computer
number will depend on the dive with the lowest amount of dive
computers in the trip. This will need to be fixed.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2023-04-16 20:23:59 +02:00

171 lines
5.2 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#include "TabDivePhotos.h"
#include "ui_TabDivePhotos.h"
#include "core/imagedownloader.h"
#include <qt-models/divepicturemodel.h>
#include <QDesktopServices>
#include <QContextMenuEvent>
#include <QMenu>
#include <QUrl>
#include <QMessageBox>
#include <QFileInfo>
#include "core/membuffer.h"
#include "core/save-profiledata.h"
#include "core/selection.h"
//TODO: Remove those in the future.
#include "../mainwindow.h"
#include "../divelistview.h"
TabDivePhotos::TabDivePhotos(QWidget *parent)
: TabBase(parent),
ui(new Ui::TabDivePhotos()),
divePictureModel(DivePictureModel::instance())
{
ui->setupUi(this);
ui->photosView->setModel(divePictureModel);
ui->photosView->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->photosView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->photosView->setResizeMode(QListView::Adjust);
connect(ui->photosView, &DivePictureWidget::photoDoubleClicked,
[](const QString& path) {
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
}
);
connect(ui->photosView, &DivePictureWidget::zoomLevelChanged,
this, &TabDivePhotos::changeZoomLevel);
connect(ui->zoomSlider, &QAbstractSlider::valueChanged,
DivePictureModel::instance(), &DivePictureModel::setZoomLevel);
}
TabDivePhotos::~TabDivePhotos()
{
delete ui;
}
void TabDivePhotos::clear()
{
updateData();
}
void TabDivePhotos::contextMenuEvent(QContextMenuEvent *event)
{
QMenu popup(this);
popup.addAction(tr("Load media from file(s)"), this, &TabDivePhotos::addPhotosFromFile);
popup.addAction(tr("Load media file(s) from web"), this, &TabDivePhotos::addPhotosFromURL);
popup.addSeparator();
popup.addAction(tr("Delete selected media files"), this, &TabDivePhotos::removeSelectedPhotos);
popup.addAction(tr("Delete all media files"), this, &TabDivePhotos::removeAllPhotos);
popup.addAction(tr("Open folder of selected media files"), this, &TabDivePhotos::openFolderOfSelectedFiles);
popup.addAction(tr("Recalculate selected thumbnails"), this, &TabDivePhotos::recalculateSelectedThumbnails);
popup.addAction(tr("Save dive data as subtitles"), this, &TabDivePhotos::saveSubtitles);
popup.exec(event->globalPos());
event->accept();
}
QVector<QString> TabDivePhotos::getSelectedFilenames() const
{
QVector<QString> selectedPhotos;
if (!ui->photosView->selectionModel()->hasSelection())
return selectedPhotos;
QModelIndexList indices = ui->photosView->selectionModel()->selectedRows();
selectedPhotos.reserve(indices.count());
for (const auto &photo: indices) {
if (photo.isValid()) {
QString fileUrl = photo.data(Qt::DisplayPropertyRole).toString();
if (!fileUrl.isEmpty())
selectedPhotos.push_back(fileUrl);
}
}
return selectedPhotos;
}
void TabDivePhotos::removeSelectedPhotos()
{
QModelIndexList indices = ui->photosView->selectionModel()->selectedRows();
DivePictureModel::instance()->removePictures(indices);
}
void TabDivePhotos::openFolderOfSelectedFiles()
{
QVector<QString> directories;
for (const QString &filename: getSelectedFilenames()) {
QFileInfo info(filename);
if (!info.exists())
continue;
QString path = info.absolutePath();
if (path.isEmpty() || directories.contains(path))
continue;
directories.append(path);
}
for (const QString &dir: directories)
QDesktopServices::openUrl(QUrl::fromLocalFile(dir));
}
void TabDivePhotos::recalculateSelectedThumbnails()
{
Thumbnailer::instance()->calculateThumbnails(getSelectedFilenames());
}
void TabDivePhotos::saveSubtitles()
{
if (!current_dive)
return;
if (!ui->photosView->selectionModel()->hasSelection())
return;
QModelIndexList indices = ui->photosView->selectionModel()->selectedRows();
for (const auto &photo: indices) {
if (photo.isValid()) {
QString fileUrl = photo.data(Qt::DisplayPropertyRole).toString();
if (!fileUrl.isEmpty()) {
QFileInfo fi = QFileInfo(fileUrl);
QFile subtitlefile;
subtitlefile.setFileName(QString(fi.path()) + "/" + fi.completeBaseName() + ".ass");
int offset = photo.data(Qt::UserRole + 1).toInt();
int duration = photo.data(Qt::UserRole + 2).toInt();
// Only videos have non-zero duration
if (!duration)
continue;
struct membufferpp b;
save_subtitles_buffer(&b, current_dive, offset, duration);
const char *data = mb_cstring(&b);
subtitlefile.open(QIODevice::WriteOnly);
subtitlefile.write(data, strlen(data));
subtitlefile.close();
}
}
}
}
//TODO: This looks overly wrong. We shouldn't call MainWindow to retrieve the DiveList to add Images.
void TabDivePhotos::addPhotosFromFile()
{
MainWindow::instance()->diveList->loadImages();
}
void TabDivePhotos::addPhotosFromURL()
{
MainWindow::instance()->diveList->loadWebImages();
}
void TabDivePhotos::removeAllPhotos()
{
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 ) {
ui->photosView->selectAll();
removeSelectedPhotos();
}
}
void TabDivePhotos::updateData()
{
divePictureModel->updateDivePictures();
}
void TabDivePhotos::changeZoomLevel(int delta)
{
// We count on QSlider doing bound checks
ui->zoomSlider->setValue(ui->zoomSlider->value() + delta);
}