2017-04-27 20:26:05 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// SPDX-License-Identifier: GPL-2.0
  
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								# include  "TabDivePhotos.h" 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  "ui_TabDivePhotos.h" 
  
						 
					
						
							
								
									
										
										
										
											2018-03-10 16:36:20 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  "core/imagedownloader.h" 
  
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <qt-models/divepicturemodel.h> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <QDesktopServices> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <QContextMenuEvent> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <QMenu> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <QUrl> 
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# include  <QMessageBox> 
  
						 
					
						
							
								
									
										
										
										
											2018-07-18 23:09:31 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# include  <QFileInfo> 
  
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//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 14:03:16 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ui - > photosView - > setSelectionMode ( QAbstractItemView : : ExtendedSelection ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-12-01 01:38:55 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ui - > photosView - > setResizeMode ( QListView : : Adjust ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									connect ( ui - > photosView ,  & DivePictureWidget : : photoDoubleClicked , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										[ ] ( const  QString &  path )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											QDesktopServices : : openUrl ( QUrl : : fromLocalFile ( path ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-12-17 16:17:38 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									connect ( ui - > photosView ,  & DivePictureWidget : : zoomLevelChanged , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										this ,  & TabDivePhotos : : changeZoomLevel ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									connect ( ui - > zoomSlider ,  & QAbstractSlider : : valueChanged , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										DivePictureModel : : instance ( ) ,  & DivePictureModel : : setZoomLevel ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								TabDivePhotos : : ~ TabDivePhotos ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									delete  ui ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  TabDivePhotos : : clear ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									updateData ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  TabDivePhotos : : contextMenuEvent ( QContextMenuEvent  * event )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									QMenu  popup ( this ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-13 20:46:45 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									popup . addAction ( tr ( " Load media from file(s) " ) ,  this ,  SLOT ( addPhotosFromFile ( ) ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									popup . addAction ( tr ( " Load media file(s) from web " ) ,  this ,  SLOT ( addPhotosFromURL ( ) ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									popup . addSeparator ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-13 20:46:45 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									popup . addAction ( tr ( " Delete selected media files " ) ,  this ,  SLOT ( removeSelectedPhotos ( ) ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									popup . addAction ( tr ( " Delete all media files " ) ,  this ,  SLOT ( removeAllPhotos ( ) ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-18 23:09:31 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									popup . addAction ( tr ( " Open folder of selected media files " ) ,  this ,  SLOT ( openFolderOfSelectedFiles ( ) ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-27 14:03:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									popup . addAction ( tr ( " Recalculate selected thumbnails " ) ,  this ,  SLOT ( recalculateSelectedThumbnails ( ) ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									popup . exec ( event - > globalPos ( ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									event - > accept ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-05-27 14:03:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								QVector < QString >  TabDivePhotos : : getSelectedFilenames ( )  const  
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2018-05-27 14:03:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									QVector < QString >  selectedPhotos ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  ( ! ui - > photosView - > selectionModel ( ) - > hasSelection ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-27 14:03:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  selectedPhotos ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									QModelIndexList  indexes  =  ui - > photosView - > selectionModel ( ) - > selectedRows ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  ( indexes . count ( )  = =  0 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										indexes  =  ui - > photosView - > selectionModel ( ) - > selectedIndexes ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-27 14:03:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									selectedPhotos . reserve ( indexes . count ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-18 21:57:18 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									for  ( const  auto  & photo :  indexes )  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										if  ( photo . isValid ( ) )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											QString  fileUrl  =  photo . data ( Qt : : DisplayPropertyRole ) . toString ( ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-18 21:57:18 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  ( ! fileUrl . isEmpty ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-27 14:03:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												selectedPhotos . push_back ( fileUrl ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-18 21:57:18 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-27 14:03:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  selectedPhotos ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  TabDivePhotos : : removeSelectedPhotos ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									DivePictureModel : : instance ( ) - > removePictures ( getSelectedFilenames ( ) ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-07-18 23:09:31 +02: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 14:03:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								void  TabDivePhotos : : recalculateSelectedThumbnails ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Thumbnailer : : instance ( ) - > calculateThumbnails ( getSelectedFilenames ( ) ) ; 
							 
						 
					
						
							
								
									
										
										
										
											2017-04-04 19:21:30 +02: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 ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
									
										
										
										
											2018-07-13 20:46:45 +02: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 19:21:30 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										ui - > photosView - > selectAll ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										removeSelectedPhotos ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								void  TabDivePhotos : : updateData ( )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									divePictureModel - > updateDivePictures ( ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-12-17 16:17:38 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								void  TabDivePhotos : : changeZoomLevel ( int  delta )  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// We count on QSlider doing bound checks
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ui - > zoomSlider - > setValue ( ui - > zoomSlider - > value ( )  +  delta ) ; 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}