2017-04-27 18:26:05 +00:00
// SPDX-License-Identifier: GPL-2.0
2017-04-04 17:21:30 +00:00
# include "TabDivePhotos.h"
2022-09-17 14:21:17 +00:00
# include "maintab.h"
2017-04-04 17:21:30 +00:00
# include "ui_TabDivePhotos.h"
2018-03-10 15:36:20 +00:00
# include "core/imagedownloader.h"
2017-04-04 17:21:30 +00:00
# include <qt-models/divepicturemodel.h>
# include <QDesktopServices>
# include <QContextMenuEvent>
# include <QMenu>
# include <QUrl>
# include <QMessageBox>
2018-07-18 21:09:31 +00:00
# include <QFileInfo>
2019-04-14 14:19:23 +00:00
# include "core/membuffer.h"
2022-04-04 16:57:28 +00:00
# include "core/save-profiledata.h"
# include "core/selection.h"
2017-04-04 17:21:30 +00:00
//TODO: Remove those in the future.
# include "../mainwindow.h"
# include "../divelistview.h"
2022-09-09 16:12:31 +00:00
TabDivePhotos : : TabDivePhotos ( MainTab * parent )
2017-04-04 17:21:30 +00:00
: TabBase ( parent ) ,
ui ( new Ui : : TabDivePhotos ( ) ) ,
divePictureModel ( DivePictureModel : : instance ( ) )
{
ui - > setupUi ( this ) ;
ui - > photosView - > setModel ( divePictureModel ) ;
2017-11-29 13:03:16 +00:00
ui - > photosView - > setSelectionMode ( QAbstractItemView : : ExtendedSelection ) ;
2020-04-15 20:35:24 +00:00
ui - > photosView - > setSelectionBehavior ( QAbstractItemView : : SelectRows ) ;
2017-12-01 00:38:55 +00:00
ui - > photosView - > setResizeMode ( QListView : : Adjust ) ;
2017-04-04 17:21:30 +00:00
connect ( ui - > photosView , & DivePictureWidget : : photoDoubleClicked ,
[ ] ( const QString & path ) {
QDesktopServices : : openUrl ( QUrl : : fromLocalFile ( path ) ) ;
}
) ;
2017-12-17 15:17:38 +00:00
connect ( ui - > photosView , & DivePictureWidget : : zoomLevelChanged ,
this , & TabDivePhotos : : changeZoomLevel ) ;
connect ( ui - > zoomSlider , & QAbstractSlider : : valueChanged ,
DivePictureModel : : instance ( ) , & DivePictureModel : : setZoomLevel ) ;
2017-04-04 17:21:30 +00:00
}
TabDivePhotos : : ~ TabDivePhotos ( )
{
delete ui ;
}
void TabDivePhotos : : clear ( )
{
2022-09-04 09:04:01 +00:00
// TODO: clear model
divePictureModel - > updateDivePictures ( ) ;
2017-04-04 17:21:30 +00:00
}
void TabDivePhotos : : contextMenuEvent ( QContextMenuEvent * event )
{
QMenu popup ( this ) ;
2019-10-14 18:57:13 +00:00
popup . addAction ( tr ( " Load media from file(s) " ) , this , & TabDivePhotos : : addPhotosFromFile ) ;
popup . addAction ( tr ( " Load media file(s) from web " ) , this , & TabDivePhotos : : addPhotosFromURL ) ;
2017-04-04 17:21:30 +00:00
popup . addSeparator ( ) ;
2019-10-14 18:57:13 +00:00
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 ) ;
2017-04-04 17:21:30 +00:00
popup . exec ( event - > globalPos ( ) ) ;
event - > accept ( ) ;
}
2018-05-27 12:03:29 +00:00
QVector < QString > TabDivePhotos : : getSelectedFilenames ( ) const
2017-04-04 17:21:30 +00:00
{
2018-05-27 12:03:29 +00:00
QVector < QString > selectedPhotos ;
2017-04-04 17:21:30 +00:00
if ( ! ui - > photosView - > selectionModel ( ) - > hasSelection ( ) )
2018-05-27 12:03:29 +00:00
return selectedPhotos ;
2020-03-11 10:30:51 +00:00
QModelIndexList indices = ui - > photosView - > selectionModel ( ) - > selectedRows ( ) ;
selectedPhotos . reserve ( indices . count ( ) ) ;
for ( const auto & photo : indices ) {
2017-04-04 17:21:30 +00:00
if ( photo . isValid ( ) ) {
QString fileUrl = photo . data ( Qt : : DisplayPropertyRole ) . toString ( ) ;
2018-05-18 19:57:18 +00:00
if ( ! fileUrl . isEmpty ( ) )
2018-05-27 12:03:29 +00:00
selectedPhotos . push_back ( fileUrl ) ;
2017-04-04 17:21:30 +00:00
}
2018-05-18 19:57:18 +00:00
}
2018-05-27 12:03:29 +00:00
return selectedPhotos ;
}
void TabDivePhotos : : removeSelectedPhotos ( )
{
2020-04-17 21:18:58 +00:00
QModelIndexList indices = ui - > photosView - > selectionModel ( ) - > selectedRows ( ) ;
DivePictureModel : : instance ( ) - > removePictures ( indices ) ;
2018-05-27 12:03:29 +00:00
}
2018-07-18 21:09:31 +00:00
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 ) ) ;
}
2018-05-27 12:03:29 +00:00
void TabDivePhotos : : recalculateSelectedThumbnails ( )
{
Thumbnailer : : instance ( ) - > calculateThumbnails ( getSelectedFilenames ( ) ) ;
2017-04-04 17:21:30 +00:00
}
2019-04-14 14:19:23 +00:00
void TabDivePhotos : : saveSubtitles ( )
{
2022-09-17 14:21:17 +00:00
if ( ! parent . currentDive )
2020-04-13 17:09:35 +00:00
return ;
2019-04-14 14:19:23 +00:00
if ( ! ui - > photosView - > selectionModel ( ) - > hasSelection ( ) )
return ;
2020-03-11 10:30:51 +00:00
QModelIndexList indices = ui - > photosView - > selectionModel ( ) - > selectedRows ( ) ;
for ( const auto & photo : indices ) {
2019-04-14 14:19:23 +00:00
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 ;
2021-07-20 05:45:31 +00:00
struct membufferpp b ;
2022-09-17 14:21:17 +00:00
save_subtitles_buffer ( & b , parent . currentDive , offset , duration ) ;
2021-07-20 05:45:31 +00:00
const char * data = mb_cstring ( & b ) ;
2019-04-14 14:19:23 +00:00
subtitlefile . open ( QIODevice : : WriteOnly ) ;
subtitlefile . write ( data , strlen ( data ) ) ;
subtitlefile . close ( ) ;
}
}
}
}
2017-04-04 17:21:30 +00:00
//TODO: This looks overly wrong. We shouldn't call MainWindow to retrieve the DiveList to add Images.
void TabDivePhotos : : addPhotosFromFile ( )
{
2018-10-12 14:13:42 +00:00
MainWindow : : instance ( ) - > diveList - > loadImages ( ) ;
2017-04-04 17:21:30 +00:00
}
void TabDivePhotos : : addPhotosFromURL ( )
{
2018-10-12 14:13:42 +00:00
MainWindow : : instance ( ) - > diveList - > loadWebImages ( ) ;
2017-04-04 17:21:30 +00:00
}
void TabDivePhotos : : removeAllPhotos ( )
{
2018-07-13 18:46:45 +00:00
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 ) {
2017-04-04 17:21:30 +00:00
ui - > photosView - > selectAll ( ) ;
removeSelectedPhotos ( ) ;
}
}
2022-09-04 09:04:01 +00:00
void TabDivePhotos : : updateData ( const std : : vector < dive * > & , dive * currentDive , int )
2017-04-04 17:21:30 +00:00
{
2022-09-04 09:04:01 +00:00
// TODO: pass dive
2017-04-04 17:21:30 +00:00
divePictureModel - > updateDivePictures ( ) ;
}
2017-12-17 15:17:38 +00:00
void TabDivePhotos : : changeZoomLevel ( int delta )
{
// We count on QSlider doing bound checks
ui - > zoomSlider - > setValue ( ui - > zoomSlider - > value ( ) + delta ) ;
}