refactoring: Add PamhyrTranslate class for GUI translation and document it.

This commit is contained in:
Pierre-Antoine Rouby committed 2023-09-26 17:16:53 +02:00
1 parent e7fd2077bd
commit 2e94348bb8
5 files changed
+145 -70

No files matched your search

+62 -41
View File
@@ -443,49 +443,83 @@ than hand made windows and widget creation, and saves us some purely
descriptive code. The UI files are saved into =src/View/ui= for
window, and =/src/View/ui/Widgets= for custom widget.
*** Translate
#+NAME: pamhyr-trad
#+CAPTION: Example of =PamhyrTranslate= class implementation with a global traduction for /FooBar/ and a additional dictionary =table_headers=
#+begin_src python :python python3 :results output :noweb yes
from PyQt5.QtCore import QCoreApplication
from View.Tools.PamhyrTranslate import PamhyrTranslate
_translate = QCoreApplication.translate
class MyTranslate(PamhyrTranslate):
def __init__(self):
super(MyTranslate, self).__init__()
# Add traduction to global dictionary
self._dict["My"] = _translate("My", "FooBar")
# Add an additional translate dictionary
self._sub_dict["table_headers"] = {
"foo": _translate("My", "Foo"),
"bar": _translate("My", "Bar"),
"baz": _translate("My", "Baz"),
}
#+end_src
*** Window
The abstract class PamhyrWindow and PamhyrDialog are used for most of
Pamhyr2 window. These class allow to create an window for Pamhyr2 GUI
and implemente some useful methods.
and implemente some useful methods. The super class method difine some
generic value from optional parameters, for examples:
- =self._study=: The study giving in constructor parameters =study=
(typically a =Model.Study= class object)
- =self._config=: The configuration giving in constructor parameters
=config= (typically a =Config= class object)
- =self._trad=: The traductor dictionary giving in constructor
parameters =trad= (typically a =Model.Tools.PamhyrTranslate= class
object)
#+NAME: window
#+CAPTION: Example of Pamhyr2 window
#+begin_src python :python python3 :results output :noweb yes
from View.Tools.PamhyrWindow import PamhyrWindow
from View.Tools.PamhyrWindow import PamhyrWindow
from View.My.Translate import MyTranslate
class MyWindow(PamhyrWindow):
_pamhyr_ui = "MyUI"
_pamhyr_name = "My window"
class MyWindow(PamhyrWindow):
_pamhyr_ui = "MyUI"
_pamhyr_name = "My window"
def __init__(self, my_data=None,
study=None, config=None,
parent=None):
self._my_data = my_data
def __init__(self, my_data=None,
study=None, config=None,
parent=None):
self._my_data = my_data
super(MyWindow, self).__init__(
# Window title
title = self._pamhyr_name + " - " + study.name,
# Window standard data
study = study, config = config, parent = parent,
# Activate undo/redo and copy/paste shortcut
options = ["undo", "copy"]
)
super(MyWindow, self).__init__(
# Window title
title = self._pamhyr_name + " - " + study.name,
# Window standard data
study = study, config = config,
trad = MyTranslate(),
parent = parent,
# Activate undo/redo and copy/paste shortcut
options = ["undo", "copy"]
)
# Add custom data to hash window computation
self._hash_data.append(self._my_data)
# Add custom data to hash window computation
self._hash_data.append(self._my_data)
# Setup custom window components
self.setup_table()
self.setup_connections()
# Setup custom window components
self.setup_table()
self.setup_connections()
def setup_table(self):
# Init table(s)...
def setup_table(self):
# Init table(s)...
def setup_connections(self):
# Init action connection(s)...
def setup_connections(self):
# Init action connection(s)...
# ...
# ...
#+end_src
Typically we called method =setup_*=, the method to initialize some
@@ -499,20 +533,6 @@ and =setData= methode to implement, but the constructor needs more
information than a classic QAbstractTableModel class.
#+begin_src python :python python3 :results output :noweb yes
# Table headers with display name translatable (translate.py)
table_headers = {
"foo": _translate("My", "Foo"),
"bar": _translate("My", "Bar"),
"baz": _translate("My", "Baz"),
}
def retranslate():
# Hack because dict definition is not able to be
# translate... (need to be called in window '__init__')
table_headers["foo"] = _translate("My", "Foo")
table_headers["bar"] = _translate("My", "Bar")
table_headers["baz"] = _translate("My", "Baz")
# Table model definition (Table.py)
class MyTableModel(PamhyrTableModel):
def data(self, index, role):
@@ -523,6 +543,7 @@ information than a classic QAbstractTableModel class.
# Set VALUE at INDEX...
# Table model creation (Window.py)
table_headers = self._trad.get_dict("table_headers")
self._model = MyTableModel(
table_view = table, # The table view object
table_headers = table_headers, # The table column headers dict