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"
# 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>
//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 ) ;
2017-11-29 13:03:16 +00:00
ui - > photosView - > setSelectionMode ( QAbstractItemView : : ExtendedSelection ) ;
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 ( )
{
updateData ( ) ;
}
void TabDivePhotos : : contextMenuEvent ( QContextMenuEvent * event )
{
QMenu popup ( this ) ;
2017-07-12 15:25:40 +00:00
popup . addAction ( tr ( " Load image(s) from file(s) " ) , this , SLOT ( addPhotosFromFile ( ) ) ) ;
popup . addAction ( tr ( " Load image(s) from web " ) , this , SLOT ( addPhotosFromURL ( ) ) ) ;
2017-04-04 17:21:30 +00:00
popup . addSeparator ( ) ;
2017-07-12 15:25:40 +00:00
popup . addAction ( tr ( " Delete selected images " ) , this , SLOT ( removeSelectedPhotos ( ) ) ) ;
popup . addAction ( tr ( " Delete all images " ) , this , SLOT ( removeAllPhotos ( ) ) ) ;
2017-04-04 17:21:30 +00:00
popup . exec ( event - > globalPos ( ) ) ;
event - > accept ( ) ;
}
void TabDivePhotos : : removeSelectedPhotos ( )
{
if ( ! ui - > photosView - > selectionModel ( ) - > hasSelection ( ) )
return ;
QModelIndexList indexes = ui - > photosView - > selectionModel ( ) - > selectedRows ( ) ;
if ( indexes . count ( ) = = 0 )
indexes = ui - > photosView - > selectionModel ( ) - > selectedIndexes ( ) ;
2018-05-18 19:57:18 +00:00
QVector < QString > photosToDelete ;
photosToDelete . reserve ( indexes . count ( ) ) ;
for ( const auto & photo : indexes ) {
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 ( ) )
photosToDelete . push_back ( fileUrl ) ;
2017-04-04 17:21:30 +00:00
}
2018-05-18 19:57:18 +00:00
}
DivePictureModel : : instance ( ) - > removePictures ( photosToDelete ) ;
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 ( )
{
MainWindow : : instance ( ) - > dive_list ( ) - > loadImages ( ) ;
}
void TabDivePhotos : : addPhotosFromURL ( )
{
MainWindow : : instance ( ) - > dive_list ( ) - > loadWebImages ( ) ;
}
void TabDivePhotos : : removeAllPhotos ( )
{
if ( QMessageBox : : warning ( this , tr ( " Deleting Images " ) , tr ( " Are you sure you want to delete all images? " ) , QMessageBox : : Cancel | QMessageBox : : Ok , QMessageBox : : Cancel ) ! = QMessageBox : : Cancel ) {
ui - > photosView - > selectAll ( ) ;
removeSelectedPhotos ( ) ;
}
}
void TabDivePhotos : : updateData ( )
{
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 ) ;
}