hydrus/hydrus/client/gui/ClientGUISerialisable.py

284 lines
8.5 KiB
Python
Raw Normal View History

2018-02-07 23:40:33 +00:00
import os
2020-04-22 21:00:35 +00:00
2019-11-14 03:56:30 +00:00
from qtpy import QtWidgets as QW
2020-04-22 21:00:35 +00:00
from hydrus.core import HydrusData
from hydrus.core import HydrusPaths
from hydrus.core import HydrusSerialisable
2020-07-29 20:52:44 +00:00
2024-02-14 21:20:24 +00:00
from hydrus.client import ClientGlobals as CG
2020-04-22 21:00:35 +00:00
from hydrus.client import ClientSerialisable
from hydrus.client.gui import ClientGUIFunctions
from hydrus.client.gui import ClientGUIScrolledPanels
from hydrus.client.gui import QtPorting as QP
2021-03-17 21:59:28 +00:00
from hydrus.client.gui.widgets import ClientGUICommon
2016-11-16 20:21:43 +00:00
class PNGExportPanel( ClientGUIScrolledPanels.ReviewPanel ):
2016-11-16 20:21:43 +00:00
2018-09-19 21:54:51 +00:00
def __init__( self, parent, payload_obj, title = None, description = None, payload_description = None ):
2016-11-16 20:21:43 +00:00
ClientGUIScrolledPanels.ReviewPanel.__init__( self, parent )
self._payload_obj = payload_obj
2019-11-14 03:56:30 +00:00
self._filepicker = QP.FilePickerCtrl( self, wildcard = 'PNG (*.png)' )
self._filepicker.SetSaveMode( True )
2018-06-20 20:20:22 +00:00
2019-06-26 21:27:18 +00:00
flp_width = ClientGUIFunctions.ConvertTextToPixelWidth( self._filepicker, 64 )
2018-06-20 20:20:22 +00:00
2019-11-14 03:56:30 +00:00
self._filepicker.setMinimumWidth( flp_width )
2016-11-16 20:21:43 +00:00
2019-11-14 03:56:30 +00:00
self._title = QW.QLineEdit( self )
2016-11-16 20:21:43 +00:00
2019-11-14 03:56:30 +00:00
self._payload_description = QW.QLineEdit( self )
2016-11-16 20:21:43 +00:00
2019-11-14 03:56:30 +00:00
self._text = QW.QLineEdit( self )
2016-11-16 20:21:43 +00:00
2022-03-30 20:28:13 +00:00
self._width = ClientGUICommon.BetterSpinBox( self, min=100, max=4096 )
2016-12-14 21:19:07 +00:00
2016-11-16 20:21:43 +00:00
self._export = ClientGUICommon.BetterButton( self, 'export', self.Export )
#
2018-09-19 21:54:51 +00:00
if payload_description is None:
2019-01-09 22:59:03 +00:00
( payload_description, payload_bytes ) = ClientSerialisable.GetPayloadDescriptionAndBytes( self._payload_obj )
2018-09-19 21:54:51 +00:00
else:
2021-12-22 22:31:23 +00:00
( payload_bytes, payload_length ) = ClientSerialisable.GetPayloadBytesAndLength( self._payload_obj )
2018-09-19 21:54:51 +00:00
2021-12-22 22:31:23 +00:00
payload_description += ' - {}'.format( HydrusData.ToHumanBytes( payload_length ) )
2018-09-19 21:54:51 +00:00
2016-12-14 21:19:07 +00:00
2019-11-14 03:56:30 +00:00
self._payload_description.setText( payload_description )
2016-11-16 20:21:43 +00:00
2019-11-14 03:56:30 +00:00
self._payload_description.setEnabled( False )
2016-11-16 20:21:43 +00:00
2019-11-14 03:56:30 +00:00
self._width.setValue( 512 )
2016-11-16 20:21:43 +00:00
2024-02-14 21:20:24 +00:00
last_png_export_dir = CG.client_controller.new_options.GetNoneableString( 'last_png_export_dir' )
2018-06-20 20:20:22 +00:00
2018-09-19 21:54:51 +00:00
if title is not None:
name = title
elif isinstance( self._payload_obj, HydrusSerialisable.SerialisableBaseNamed ):
2018-02-07 23:40:33 +00:00
2018-06-20 20:20:22 +00:00
name = self._payload_obj.GetName()
else:
name = payload_description
2019-11-14 03:56:30 +00:00
self._title.setText( name )
2018-06-20 20:20:22 +00:00
2018-09-19 21:54:51 +00:00
if description is not None:
2019-11-14 03:56:30 +00:00
self._text.setText( description )
2018-09-19 21:54:51 +00:00
2018-06-20 20:20:22 +00:00
if last_png_export_dir is not None:
filename = name + '.png'
2018-09-26 19:05:12 +00:00
filename = HydrusPaths.SanitizeFilename( filename )
2018-06-20 20:20:22 +00:00
path = os.path.join( last_png_export_dir, filename )
self._filepicker.SetPath( path )
2018-02-07 23:40:33 +00:00
self._Update()
2016-11-16 20:21:43 +00:00
#
rows = []
rows.append( ( 'export path: ', self._filepicker ) )
rows.append( ( 'title: ', self._title ) )
2016-12-14 21:19:07 +00:00
rows.append( ( 'payload description: ', self._payload_description ) )
rows.append( ( 'your description (optional): ', self._text ) )
rows.append( ( 'png width: ', self._width ) )
2016-11-16 20:21:43 +00:00
rows.append( ( '', self._export ) )
gridbox = ClientGUICommon.WrapInGrid( self, rows )
2019-11-14 03:56:30 +00:00
self.widget().setLayout( gridbox )
2016-11-16 20:21:43 +00:00
2019-11-14 03:56:30 +00:00
self._filepicker.filePickerChanged.connect( self._Update )
self._title.textChanged.connect( self._Update )
2018-06-20 20:20:22 +00:00
2016-11-16 20:21:43 +00:00
2018-02-07 23:40:33 +00:00
def _Update( self ):
2016-11-16 20:21:43 +00:00
problems = []
path = self._filepicker.GetPath()
if path == '' or path is None:
problems.append( 'select a path' )
2019-11-14 03:56:30 +00:00
if path is not None and not os.path.exists( os.path.dirname( path ) ):
problems.append( 'please select a directory that exists' )
if self._title.text() == '':
2016-11-16 20:21:43 +00:00
problems.append( 'set a title' )
if len( problems ) == 0:
2019-11-14 03:56:30 +00:00
self._export.setText( 'export' )
2016-11-16 20:21:43 +00:00
2019-11-14 03:56:30 +00:00
self._export.setEnabled( True )
2016-11-16 20:21:43 +00:00
else:
2019-11-14 03:56:30 +00:00
self._export.setText( ' and '.join(problems) )
2016-11-16 20:21:43 +00:00
2019-11-14 03:56:30 +00:00
self._export.setEnabled( False )
2016-11-16 20:21:43 +00:00
def Export( self ):
2019-11-14 03:56:30 +00:00
width = self._width.value()
2016-12-14 21:19:07 +00:00
2019-11-14 03:56:30 +00:00
payload_description = self._payload_description.text()
2021-12-22 22:31:23 +00:00
( payload_bytes, payload_length ) = ClientSerialisable.GetPayloadBytesAndLength( self._payload_obj )
2016-11-16 20:21:43 +00:00
2019-11-14 03:56:30 +00:00
title = self._title.text()
text = self._text.text()
2019-01-09 22:59:03 +00:00
path = self._filepicker.GetPath()
2016-11-16 20:21:43 +00:00
2018-06-20 20:20:22 +00:00
if path is not None and path != '':
base_dir = os.path.dirname( path )
2024-02-14 21:20:24 +00:00
CG.client_controller.new_options.SetNoneableString( 'last_png_export_dir', base_dir )
2018-06-20 20:20:22 +00:00
2016-11-16 20:21:43 +00:00
if not path.endswith( '.png' ):
path += '.png'
ClientSerialisable.DumpToPNG( width, payload_bytes, title, payload_description, text, path )
2016-11-16 20:21:43 +00:00
2019-11-14 03:56:30 +00:00
self._export.setText( 'done!' )
2016-11-30 20:24:17 +00:00
2024-02-14 21:20:24 +00:00
CG.client_controller.CallLaterQtSafe( self._export, 2.0, 'png export set text', self._export.setText, 'export' )
2016-11-30 20:24:17 +00:00
2016-12-14 21:19:07 +00:00
class PNGsExportPanel( ClientGUIScrolledPanels.ReviewPanel ):
2018-02-07 23:40:33 +00:00
def __init__( self, parent, payload_objs ):
ClientGUIScrolledPanels.ReviewPanel.__init__( self, parent )
self._payload_objs = payload_objs
2019-11-14 03:56:30 +00:00
self._directory_picker = QP.DirPickerCtrl( self )
2018-06-20 20:20:22 +00:00
2019-06-26 21:27:18 +00:00
dp_width = ClientGUIFunctions.ConvertTextToPixelWidth( self._directory_picker, 52 )
2018-06-20 20:20:22 +00:00
2019-11-14 03:56:30 +00:00
self._directory_picker.setMinimumWidth( dp_width )
2018-02-07 23:40:33 +00:00
2022-03-30 20:28:13 +00:00
self._width = ClientGUICommon.BetterSpinBox( self, min=100, max=4096 )
2018-02-07 23:40:33 +00:00
self._export = ClientGUICommon.BetterButton( self, 'export', self.Export )
#
2024-02-14 21:20:24 +00:00
last_png_export_dir = CG.client_controller.new_options.GetNoneableString( 'last_png_export_dir' )
2018-06-20 20:20:22 +00:00
if last_png_export_dir is not None:
self._directory_picker.SetPath( last_png_export_dir )
2019-11-14 03:56:30 +00:00
self._width.setValue( 512 )
2018-02-07 23:40:33 +00:00
self._Update()
#
rows = []
rows.append( ( 'export path: ', self._directory_picker ) )
rows.append( ( 'png width: ', self._width ) )
rows.append( ( '', self._export ) )
gridbox = ClientGUICommon.WrapInGrid( self, rows )
2019-11-14 03:56:30 +00:00
self.widget().setLayout( gridbox )
2018-02-07 23:40:33 +00:00
2019-11-14 03:56:30 +00:00
self._directory_picker.dirPickerChanged.connect( self._Update )
2018-06-20 20:20:22 +00:00
2018-02-07 23:40:33 +00:00
def _Update( self ):
problems = []
path = self._directory_picker.GetPath()
2018-06-20 20:20:22 +00:00
if path is None or path == '':
2018-02-07 23:40:33 +00:00
problems.append( 'select a path' )
if len( problems ) == 0:
2019-11-14 03:56:30 +00:00
self._export.setText( 'export' )
2018-02-07 23:40:33 +00:00
2019-11-14 03:56:30 +00:00
self._export.setEnabled( True )
2018-02-07 23:40:33 +00:00
else:
2019-11-14 03:56:30 +00:00
self._export.setText( ' and '.join(problems) )
2018-02-07 23:40:33 +00:00
2019-11-14 03:56:30 +00:00
self._export.setEnabled( False )
2018-02-07 23:40:33 +00:00
def Export( self ):
2019-11-14 03:56:30 +00:00
width = self._width.value()
2018-02-07 23:40:33 +00:00
2019-01-09 22:59:03 +00:00
directory = self._directory_picker.GetPath()
2018-02-07 23:40:33 +00:00
2018-06-20 20:20:22 +00:00
last_png_export_dir = directory
if last_png_export_dir is not None and last_png_export_dir != '':
2024-02-14 21:20:24 +00:00
CG.client_controller.new_options.SetNoneableString( 'last_png_export_dir', last_png_export_dir )
2018-06-20 20:20:22 +00:00
2018-02-07 23:40:33 +00:00
for obj in self._payload_objs:
2019-01-09 22:59:03 +00:00
( payload_description, payload_bytes ) = ClientSerialisable.GetPayloadDescriptionAndBytes( obj )
2018-02-07 23:40:33 +00:00
title = obj.GetName()
text = ''
path = os.path.join( directory, title )
if not path.endswith( '.png' ):
path += '.png'
ClientSerialisable.DumpToPNG( width, payload_bytes, title, payload_description, text, path )
2018-02-07 23:40:33 +00:00
2019-11-14 03:56:30 +00:00
self._export.setText( 'done!' )
2018-02-07 23:40:33 +00:00
2024-02-14 21:20:24 +00:00
CG.client_controller.CallLaterQtSafe( self._export, 2.0, 'png export set text', self._export.setText, 'export' )
2018-02-07 23:40:33 +00:00