hydrus/hydrus/client/gui/ClientGUIOptionsPanels.py

237 lines
7.6 KiB
Python
Raw Normal View History

2019-11-14 03:56:30 +00:00
from qtpy import QtCore as QC
from qtpy import QtWidgets as QW
2020-04-22 21:00:35 +00:00
from hydrus.core import HydrusConstants as HC
2020-07-29 20:52:44 +00:00
2020-04-22 21:00:35 +00:00
from hydrus.client import ClientConstants as CC
from hydrus.client.gui import ClientGUIFunctions
from hydrus.client.gui import QtPorting as QP
2021-03-17 21:59:28 +00:00
from hydrus.client.gui.widgets import ClientGUICommon
2015-03-18 21:46:29 +00:00
2019-11-14 03:56:30 +00:00
class OptionsPanel( QW.QWidget ):
2015-03-18 21:46:29 +00:00
2017-02-01 21:11:17 +00:00
def GetValue( self ): raise NotImplementedError()
2015-03-18 21:46:29 +00:00
2017-02-01 21:11:17 +00:00
def SetValue( self, info ): raise NotImplementedError()
2015-03-18 21:46:29 +00:00
2015-09-09 22:04:39 +00:00
class OptionsPanelMimes( OptionsPanel ):
2020-01-22 21:04:43 +00:00
BUTTON_CURRENTLY_HIDDEN = '\u25B6'
BUTTON_CURRENTLY_SHOWING = '\u25BC'
2015-09-09 22:04:39 +00:00
def __init__( self, parent, selectable_mimes ):
OptionsPanel.__init__( self, parent )
2020-01-16 02:08:23 +00:00
self._selectable_mimes = set( selectable_mimes )
2015-09-09 22:04:39 +00:00
self._mimes_to_checkboxes = {}
2020-01-22 21:04:43 +00:00
self._general_mime_types_to_checkboxes = {}
self._general_mime_types_to_buttons = {}
general_mime_types = []
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
general_mime_types.append( HC.GENERAL_IMAGE )
general_mime_types.append( HC.GENERAL_ANIMATION )
general_mime_types.append( HC.GENERAL_VIDEO )
general_mime_types.append( HC.GENERAL_AUDIO )
general_mime_types.append( HC.GENERAL_APPLICATION )
2020-01-16 02:08:23 +00:00
2020-01-22 21:04:43 +00:00
gridbox = QP.GridLayout( cols = 3 )
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
gridbox.setColumnStretch( 2, 1 )
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
for general_mime_type in general_mime_types:
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
mimes_in_type = self._GetMimesForGeneralMimeType( general_mime_type )
if len( mimes_in_type ) == 0:
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
continue
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
general_mime_checkbox = QW.QCheckBox( HC.mime_string_lookup[ general_mime_type ], self )
general_mime_checkbox.clicked.connect( self.EventMimeGroupCheckbox )
self._general_mime_types_to_checkboxes[ general_mime_type ] = general_mime_checkbox
2020-07-29 20:52:44 +00:00
QP.AddToLayout( gridbox, general_mime_checkbox, CC.FLAGS_CENTER_PERPENDICULAR )
2020-01-22 21:04:43 +00:00
2021-02-17 18:22:44 +00:00
show_hide_button = ClientGUICommon.BetterButton( self, self.BUTTON_CURRENTLY_HIDDEN, self._ButtonShowHide, general_mime_type )
2016-08-17 20:07:22 +00:00
2020-01-22 21:04:43 +00:00
max_width = ClientGUIFunctions.ConvertTextToPixelWidth( show_hide_button, 5 )
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
show_hide_button.setMaximumWidth( max_width )
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
self._general_mime_types_to_buttons[ general_mime_type ] = show_hide_button
2015-09-09 22:04:39 +00:00
2020-07-29 20:52:44 +00:00
QP.AddToLayout( gridbox, show_hide_button, CC.FLAGS_CENTER_PERPENDICULAR )
2015-09-09 22:04:39 +00:00
2019-11-14 03:56:30 +00:00
vbox = QP.VBoxLayout()
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
for mime in mimes_in_type:
2015-09-09 22:04:39 +00:00
2020-01-16 02:08:23 +00:00
m_checkbox = QW.QCheckBox( HC.mime_string_lookup[ mime ], self )
2019-11-14 03:56:30 +00:00
m_checkbox.clicked.connect( self.EventMimeCheckbox )
2015-09-09 22:04:39 +00:00
2021-02-17 18:22:44 +00:00
m_checkbox.setVisible( False )
2015-09-09 22:04:39 +00:00
self._mimes_to_checkboxes[ mime ] = m_checkbox
2019-11-14 03:56:30 +00:00
QP.AddToLayout( vbox, m_checkbox, CC.FLAGS_EXPAND_PERPENDICULAR )
2015-09-09 22:04:39 +00:00
2019-11-14 03:56:30 +00:00
QP.AddToLayout( gridbox, vbox, CC.FLAGS_EXPAND_SIZER_BOTH_WAYS )
2015-09-09 22:04:39 +00:00
2019-11-14 03:56:30 +00:00
self.setLayout( gridbox )
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
def _DoInitialHideShow( self ):
for ( general_mime_type, general_mime_checkbox ) in list( self._general_mime_types_to_checkboxes.items() ):
mimes_in_type = self._GetMimesForGeneralMimeType( general_mime_type )
should_show = general_mime_checkbox.checkState() == QC.Qt.PartiallyChecked
if not should_show:
self._ButtonShowHide( general_mime_type )
def _GetMimesForGeneralMimeType( self, general_mime_type ):
mimes_in_type = HC.general_mimetypes_to_mime_groups[ general_mime_type ]
mimes_in_type = [ mime for mime in mimes_in_type if mime in self._selectable_mimes ]
return mimes_in_type
2020-01-16 02:08:23 +00:00
2020-01-22 21:04:43 +00:00
def _ButtonShowHide( self, general_mime_type ):
2020-01-16 02:08:23 +00:00
2020-01-22 21:04:43 +00:00
button = self._general_mime_types_to_buttons[ general_mime_type ]
2020-01-16 02:08:23 +00:00
2020-01-22 21:04:43 +00:00
mimes_in_type = self._GetMimesForGeneralMimeType( general_mime_type )
2020-01-16 02:08:23 +00:00
2020-01-22 21:04:43 +00:00
should_show = button.text() == self.BUTTON_CURRENTLY_HIDDEN
for mime in mimes_in_type:
2020-01-16 02:08:23 +00:00
2020-01-22 21:04:43 +00:00
self._mimes_to_checkboxes[ mime ].setVisible( should_show )
2020-01-16 02:08:23 +00:00
2020-01-22 21:04:43 +00:00
if should_show:
button.setText( self.BUTTON_CURRENTLY_SHOWING )
else:
button.setText( self.BUTTON_CURRENTLY_HIDDEN )
2020-01-16 02:08:23 +00:00
2020-01-22 21:04:43 +00:00
2015-09-09 22:04:39 +00:00
def _UpdateMimeGroupCheckboxes( self ):
2020-01-22 21:04:43 +00:00
for ( general_mime_type, general_mime_checkbox ) in self._general_mime_types_to_checkboxes.items():
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
mimes_in_type = self._GetMimesForGeneralMimeType( general_mime_type )
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
all_checkbox_values = { self._mimes_to_checkboxes[ mime ].isChecked() for mime in mimes_in_type }
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
all_false = True not in all_checkbox_values
all_true = False not in all_checkbox_values
if all_false:
check_state = QC.Qt.Unchecked
elif all_true:
check_state = QC.Qt.Checked
else:
check_state = QC.Qt.PartiallyChecked
if check_state == QC.Qt.PartiallyChecked:
general_mime_checkbox.setTristate( True )
general_mime_checkbox.setCheckState( check_state )
if check_state != QC.Qt.PartiallyChecked:
general_mime_checkbox.setTristate( False )
2015-09-09 22:04:39 +00:00
2020-01-16 02:08:23 +00:00
2015-09-09 22:04:39 +00:00
2019-11-14 03:56:30 +00:00
def EventMimeCheckbox( self ):
2015-09-09 22:04:39 +00:00
self._UpdateMimeGroupCheckboxes()
2019-11-14 03:56:30 +00:00
def EventMimeGroupCheckbox( self ):
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
for ( general_mime_type, general_mime_checkbox ) in list( self._general_mime_types_to_checkboxes.items() ):
check_state = general_mime_checkbox.checkState()
mime_check_state = None
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
if check_state == QC.Qt.Unchecked:
mime_check_state = False
elif check_state == QC.Qt.Checked:
mime_check_state = True
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
if mime_check_state is not None:
2015-09-09 22:04:39 +00:00
2021-02-17 18:22:44 +00:00
general_mime_checkbox.setTristate( False )
2020-01-22 21:04:43 +00:00
mimes_in_type = self._GetMimesForGeneralMimeType( general_mime_type )
for mime in mimes_in_type:
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
self._mimes_to_checkboxes[ mime ].setChecked( mime_check_state )
2015-09-09 22:04:39 +00:00
2017-02-01 21:11:17 +00:00
def GetValue( self ):
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
mimes = tuple( [ mime for ( mime, checkbox ) in list( self._mimes_to_checkboxes.items() ) if checkbox.isChecked() ] )
2015-09-09 22:04:39 +00:00
return mimes
2020-01-22 21:04:43 +00:00
def SetValue( self, checked_mimes ):
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
for ( mime, checkbox ) in self._mimes_to_checkboxes.items():
2015-09-09 22:04:39 +00:00
2020-01-22 21:04:43 +00:00
if mime in checked_mimes:
2015-09-09 22:04:39 +00:00
2019-11-14 03:56:30 +00:00
checkbox.setChecked( True )
2015-09-09 22:04:39 +00:00
else:
2019-11-14 03:56:30 +00:00
checkbox.setChecked( False )
2015-09-09 22:04:39 +00:00
self._UpdateMimeGroupCheckboxes()
2021-02-17 18:22:44 +00:00
#self._DoInitialHideShow()
2020-01-22 21:04:43 +00:00
2015-09-09 22:04:39 +00:00