2019-12-05 05:29:32 +00:00
import os
2020-04-22 21:00:35 +00:00
2019-12-05 05:29:32 +00:00
from qtpy import QtWidgets as QW
2020-04-22 21:00:35 +00:00
from hydrus . core import HydrusConstants as HC
from hydrus . core import HydrusData
from hydrus . core import HydrusExceptions
2024-03-28 00:59:26 +00:00
from hydrus . client . gui import QtInit
2020-04-22 21:00:35 +00:00
2020-12-16 22:29:51 +00:00
STYLESHEET_DIR = os . path . join ( HC . STATIC_DIR , ' qss ' )
2019-12-05 05:29:32 +00:00
2020-02-26 22:28:52 +00:00
DEFAULT_HYDRUS_STYLESHEET = ' '
2019-12-11 23:18:37 +00:00
ORIGINAL_STYLE_NAME = None
CURRENT_STYLE_NAME = None
2019-12-05 05:29:32 +00:00
ORIGINAL_STYLESHEET = None
2019-12-11 23:18:37 +00:00
CURRENT_STYLESHEET = None
2019-12-05 05:29:32 +00:00
def ClearStylesheet ( ) :
2019-12-11 23:18:37 +00:00
SetStyleSheet ( ORIGINAL_STYLESHEET )
2019-12-05 05:29:32 +00:00
def GetAvailableStyles ( ) :
# so eventually expand this to do QStylePlugin or whatever we are doing to add more QStyles
return list ( QW . QStyleFactory . keys ( ) )
def GetAvailableStylesheets ( ) :
if not os . path . exists ( STYLESHEET_DIR ) or not os . path . isdir ( STYLESHEET_DIR ) :
raise HydrusExceptions . DataMissing ( ' Stylesheet dir " {} " is missing or not a directory! ' . format ( STYLESHEET_DIR ) )
stylesheet_filenames = [ ]
extensions = [ ' .qss ' , ' .css ' ]
for filename in os . listdir ( STYLESHEET_DIR ) :
if True in ( filename . endswith ( ext ) for ext in extensions ) :
stylesheet_filenames . append ( filename )
return stylesheet_filenames
def InitialiseDefaults ( ) :
2020-02-26 22:28:52 +00:00
global DEFAULT_HYDRUS_STYLESHEET
try :
with open ( os . path . join ( STYLESHEET_DIR , ' default_hydrus.qss ' ) , ' r ' , encoding = ' utf-8 ' ) as f :
DEFAULT_HYDRUS_STYLESHEET = f . read ( )
except Exception as e :
HydrusData . Print ( ' Failed to load default hydrus qss: ' )
HydrusData . PrintException ( e )
DEFAULT_HYDRUS_STYLESHEET = ' '
2019-12-11 23:18:37 +00:00
global ORIGINAL_STYLE_NAME
2019-12-05 05:29:32 +00:00
2024-03-28 00:59:26 +00:00
if QtInit . WE_ARE_QT5 :
ORIGINAL_STYLE_NAME = QW . QApplication . instance ( ) . style ( ) . objectName ( )
else :
ORIGINAL_STYLE_NAME = QW . QApplication . instance ( ) . style ( ) . name ( )
2019-12-05 05:29:32 +00:00
global ORIGINAL_STYLESHEET
2019-12-11 23:18:37 +00:00
global CURRENT_STYLESHEET
2019-12-05 05:29:32 +00:00
ORIGINAL_STYLESHEET = QW . QApplication . instance ( ) . styleSheet ( )
2019-12-11 23:18:37 +00:00
CURRENT_STYLESHEET = ORIGINAL_STYLESHEET
2019-12-05 05:29:32 +00:00
2024-03-16 18:27:48 +00:00
def SetStyleFromName ( name : str ) :
2019-12-05 05:29:32 +00:00
2024-03-28 00:59:26 +00:00
if QtInit . WE_ARE_QT5 :
2024-03-16 18:27:48 +00:00
2024-03-28 00:59:26 +00:00
current_style_name = QW . QApplication . instance ( ) . style ( ) . objectName ( )
else :
current_style_name = QW . QApplication . instance ( ) . style ( ) . name ( )
2024-03-16 18:27:48 +00:00
if name . casefold ( ) == current_style_name . casefold ( ) :
2019-12-05 05:29:32 +00:00
return
2024-03-16 18:27:48 +00:00
try :
2019-12-05 05:29:32 +00:00
2024-03-16 18:27:48 +00:00
new_style = QW . QApplication . instance ( ) . setStyle ( name )
if new_style is None :
2019-12-11 23:18:37 +00:00
2024-03-16 18:27:48 +00:00
raise HydrusExceptions . DataMissing ( ' Style " {} " does not exist! If this is the default, perhaps a third-party custom style, you may have to restart the client to re-set it. ' . format ( name ) )
2019-12-11 23:18:37 +00:00
2019-12-05 05:29:32 +00:00
2024-03-16 18:27:48 +00:00
except Exception as e :
2019-12-05 05:29:32 +00:00
2024-03-16 18:27:48 +00:00
raise HydrusExceptions . DataMissing ( ' Style " {} " could not be generated/applied. If this is the default, perhaps a third-party custom style, you may have to restart the client to re-set it. Extra error info: {} ' . format ( name , e ) )
2019-12-11 23:18:37 +00:00
2022-11-09 22:24:07 +00:00
def SetStyleSheet ( stylesheet , prepend_hydrus = True ) :
2019-12-11 23:18:37 +00:00
2022-11-09 22:24:07 +00:00
stylesheet_to_use = stylesheet
2020-02-26 22:28:52 +00:00
2022-11-09 22:24:07 +00:00
if prepend_hydrus :
global DEFAULT_HYDRUS_STYLESHEET
stylesheet_to_use = DEFAULT_HYDRUS_STYLESHEET + os . linesep * 2 + stylesheet
2020-02-26 22:28:52 +00:00
2019-12-11 23:18:37 +00:00
global CURRENT_STYLESHEET
2020-02-26 22:28:52 +00:00
if CURRENT_STYLESHEET != stylesheet_to_use :
2019-12-11 23:18:37 +00:00
2020-02-26 22:28:52 +00:00
QW . QApplication . instance ( ) . setStyleSheet ( stylesheet_to_use )
2019-12-11 23:18:37 +00:00
2020-02-26 22:28:52 +00:00
CURRENT_STYLESHEET = stylesheet_to_use
2019-12-05 05:29:32 +00:00
2019-12-11 23:18:37 +00:00
def SetStylesheetFromPath ( filename ) :
2019-12-05 05:29:32 +00:00
path = os . path . join ( STYLESHEET_DIR , filename )
if not os . path . exists ( path ) :
raise HydrusExceptions . DataMissing ( ' Stylesheet " {} " does not exist! ' . format ( path ) )
with open ( path , ' r ' , encoding = ' utf-8 ' ) as f :
qss = f . read ( )
2019-12-11 23:18:37 +00:00
SetStyleSheet ( qss )
2019-12-05 05:29:32 +00:00