32 import xml.etree.ElementTree
39 from classes.logger
import log
40 from classes
import settings
42 DEFAULT_THEME_NAME =
"Humanity" 52 if QIcon.themeName() ==
'' and not s.get(
"theme") ==
"No Theme":
55 if os.getenv(
'DESKTOP_SESSION') ==
'ubuntu':
56 QIcon.setThemeName(
'unity-icon-theme')
60 QIcon.setThemeName(DEFAULT_THEME_NAME)
68 uic.loadUi(path, window)
71 window.uiTree = xml.etree.ElementTree.parse(path)
79 start_path =
":/icons/" + DEFAULT_THEME_NAME +
"/" 81 return QIcon(icon_path), icon_path
89 base_dir = QDir(base_path)
90 for e
in base_dir.entryList():
92 path = base_dir.path() +
"/" + e
93 base_filename = e.split(
'.')[0]
96 if base_filename == theme_name:
117 has_icon = QIcon.hasThemeIcon(theme_name)
119 log.warn(
'Icon theme {} not found. Will use backup icon.'.
format(theme_name))
122 if has_icon
or fallback_icon:
123 return QIcon.fromTheme(theme_name, fallback_icon)
131 type_filter =
'action' 132 if isinstance(elem, QWidget):
133 type_filter =
'widget' 135 iconset = window.uiTree.find(
'.//' + type_filter +
'[@name="' + name +
'"]/property[@name="icon"]/iconset')
136 if iconset !=
None or theme_name:
138 theme_name = iconset.get(
'theme',
'')
149 _translate = QApplication.instance().translate
152 if hasattr(elem,
'objectName'):
153 name = elem.objectName()
157 if hasattr(elem,
'setText')
and hasattr(elem,
'text')
and elem.text() !=
"":
158 elem.setText(_translate(
"", elem.text()))
159 if hasattr(elem,
'setToolTip')
and hasattr(elem,
'toolTip')
and elem.toolTip() !=
"":
160 elem.setToolTip(_translate(
"", elem.toolTip()))
161 if hasattr(elem,
'setWindowTitle')
and hasattr(elem,
'windowTitle')
and elem.windowTitle() !=
"":
162 elem.setWindowTitle(_translate(
"", elem.windowTitle()))
163 if hasattr(elem,
'setTitle')
and hasattr(elem,
'title')
and elem.title() !=
"":
164 elem.setTitle(_translate(
"", elem.title()))
165 if hasattr(elem,
'setPlaceholderText')
and hasattr(elem,
'placeholderText')
and elem.placeholderText() !=
"":
166 elem.setPlaceholderText(_translate(
"", elem.placeholderText()))
167 if hasattr(elem,
'setLocale'):
168 elem.setLocale(QLocale().system())
170 if isinstance(elem, QTabWidget):
171 for i
in range(elem.count()):
172 elem.setTabText(i, _translate(
"", elem.tabText(i)))
173 elem.setTabToolTip(i, _translate(
"", elem.tabToolTip(i)))
175 if hasattr(elem,
'setIcon')
and name !=
'':
184 if hasattr(elem,
'trigger'):
185 func_name = name +
"_trigger" 186 if hasattr(window, func_name)
and callable(getattr(window, func_name)):
187 func = getattr(window, func_name)
188 log.info(
"Binding event {}:{}".
format(window.objectName(), func_name))
189 elem.triggered.connect(getattr(window, func_name))
190 if hasattr(elem,
'click'):
191 func_name = name +
"_click" 192 if hasattr(window, func_name)
and callable(getattr(window, func_name)):
193 func = getattr(window, func_name)
194 log.info(
"Binding event {}:{}".
format(window.objectName(), func_name))
195 elem.clicked.connect(getattr(window, func_name))
201 log.info(
'Initializing UI for {}'.
format(window.objectName()))
205 if hasattr(window,
'setWindowTitle')
and window.windowTitle() !=
"":
206 _translate = QApplication.instance().translate
207 window.setWindowTitle(_translate(
"", window.windowTitle()))
210 for widget
in window.findChildren(QWidget):
214 for action
in window.findChildren(QAction):
218 log.info(
'Failed to initialize an element on {}'.
format(window.objectName()))
222 log.info(
"Transferring children from '{}' to '{}'".
format(from_widget.objectName(), to_widget.objectName()))
def setup_icon(window, elem, name, theme_name=None)
Using the window xml, set the icon on the given element, or if theme_name passed load that icon...
def get_icon(theme_name)
Get either the current theme icon or fallback to default theme (for custom icons).
def get_default_icon(theme_name)
Get a QIcon, and fallback to default theme if OS does not support themes.
def transfer_children(from_widget, to_widget)
def get_settings()
Get the current QApplication's settings instance.
def connect_auto_events(window, elem, name)
Connect any events in a *.ui file with matching Python method names.
def load_theme()
Load the current OS theme, or fallback to a default one.
def search_dir(base_path, theme_name)
Search for theme name.
def init_ui(window)
Initialize all child widgets and action of a window or dialog.
def init_element(window, elem)
Initialize language and icons of the given element.
def load_ui(window, path)
Load a Qt *.ui file, and also load an XML parsed version.