Loading

PersonsForm

  1. """
  2. This module contains the persons form
  3. """
  4.  
  5. import gi
  6. gi.require_version('Gtk', '3.0')
  7.  
  8. from others import inout
  9. from .globals import UIS_PATH
  10. from .globals import get_treeview_header_button as get_trvw_headerbtt
  11. from .globals import load_configs_to_widgets as load_configs
  12. from .globals import ImgChooserBttWithCapture
  13. from gi.repository import Gtk
  14.  
  15. __author__ = "Matheus Saraiva"
  16. __email__ = "matheus.saraiva@gmail.com"
  17. __phone__ = "55 49 88070350"
  18. __copyright__ = "2016, The PyLoca Project "
  19. __version__ = "0.0.1"
  20. __license__ = "GPL3"
  21.  
  22.  
  23. class PersonsFom(Gtk.Box):
  24.     """
  25.    Class that inherits of Gtk.Box and that contains all persons form component
  26.    """
  27.  
  28.     def __init__(self, parent):
  29.         super().__init__(orientation=Gtk.Orientation.VERTICAL)
  30.  
  31.         self.builder = Gtk.Builder.new_from_file(UIS_PATH + 'form_persons.xml')
  32.  
  33.         # ImageChooserButton
  34.         img_chser_btt = ImgChooserBttWithCapture(inout.get_configs_file(
  35.             'pyloca.config')['external_apps']['webcam_app'], parent)
  36.  
  37.         img_chser_btt.set_margin_bottom(10)
  38.  
  39.         fp_entries_vbox = self.builder.get_object('fp_entries_vbox')
  40.         fp_entries_vbox.pack_start(
  41.             img_chser_btt, expand=False, fill=True, padding=0)
  42.  
  43.         fp_entries_vbox.reorder_child(img_chser_btt, 1)
  44.  
  45.         # Get widgets for button [NEW]
  46.         cleanable_widgets = [img_chser_btt] + [wid for wid in self.builder.get_objects() if type(wid) in (
  47.             Gtk.Entry, Gtk.SearchEntry, Gtk.RadioButton, Gtk.ComboBox, Gtk.Switch, Gtk.CheckButton, Gtk.FileChooserButton)]
  48.  
  49.         # Configuring columns of table
  50.         columns_of_table = {widget.get_name(): widget for widget in self.builder.get_objects(
  51.         ) if type(widget) is Gtk.TreeViewColumn}
  52.  
  53.         for button in [get_trvw_headerbtt(columns_of_table[key]) for key in columns_of_table]:
  54.             button.connect('button-press-event', self._on_col_btts_right_clicked,
  55.                            self.builder.get_object('fp_table_columns_menu'))
  56.  
  57.         load_configs([wid for wid in columns_of_table.values() if wid.get_name(
  58.         ) != 'fp_name_table_treevwcol'], Gtk.TreeViewColumn.set_visible, 'persons_columns', 'pyloca.config')
  59.  
  60.         # Configuring add/remove columns menu
  61.         load_configs([wid for wid in self.builder.get_objects() if type(
  62.             wid) is Gtk.CheckMenuItem], Gtk.CheckMenuItem.set_active, 'persons_columns_menu', 'pyloca.config')
  63.  
  64.         self.pack_start(self.builder.get_object('fp_notebook'), expand=True, fill=True, padding=0)
  65.  
  66.         spec_person_stack = self.builder.get_object('fp_spec_person_stack')
  67.  
  68.         # Dictionary of handlers
  69.         self.dict_handlers = {
  70.             'onClientBttToggled': (self._on_any_specifc_person_radbtt_tog, spec_person_stack, 'fp_client_grid'),
  71.             'onDependentBttToggled': (self._on_any_specifc_person_radbtt_tog, spec_person_stack, 'fp_dependent_grid'),
  72.             'onFunctionaryBttToggled': (self._on_any_specifc_person_radbtt_tog, spec_person_stack, 'fp_functionary_root_chkbtt'),
  73.             'onPhone1ColMitemToggled': (self._on_mitem_cols_menu_toggled,
  74.                                         columns_of_table['fp_phone1_table_treevwcol']),
  75.             'onPhone2ColMitemToggled': (self._on_mitem_cols_menu_toggled,
  76.                                         columns_of_table['fp_phone2_table_treevwcol']),
  77.             'onEmailColMitemToggled': (self._on_mitem_cols_menu_toggled, columns_of_table['fp_email_table_treevwcol']),
  78.             'onZipcodeColMitemToggled': (self._on_mitem_cols_menu_toggled, columns_of_table['fp_zipcode_table_treevwcol']),
  79.             'onStateColMitemToggled': (self._on_mitem_cols_menu_toggled, columns_of_table['fp_state_table_treevwcol']),
  80.             'onCityColMitemToggled': (self._on_mitem_cols_menu_toggled, columns_of_table['fp_city_table_treevwcol']),
  81.             'onNeighborColMitemToggled': (self._on_mitem_cols_menu_toggled, columns_of_table['fp_neighbor_table_treevwcol']),
  82.             'onStreetColMitemToggled': (self._on_mitem_cols_menu_toggled,
  83.                                         columns_of_table['fp_street_table_treevwcol']),
  84.             'onNumberColMitemToggled': (self._on_mitem_cols_menu_toggled,
  85.                                         columns_of_table['fp_number_table_treevwcol']),
  86.             'onComplementColMitemToggled': (self._on_mitem_cols_menu_toggled, columns_of_table['fp_complement_table_treevwcol']),
  87.             'onLoginColMitemToggled': (self._on_mitem_cols_menu_toggled, columns_of_table['fp_login_table_treevwcol']),
  88.             'onNewButtonClicked': (self._on_new_button_clicked, cleanable_widgets)
  89.         }
  90.  
  91.         self.builder.connect_signals(self.dict_handlers)
  92.  
  93.     # Handlers
  94.     def _on_any_specifc_person_radbtt_tog(self, button, stack, child_name):
  95.         if button.get_active():
  96.             stack.set_visible_child_name(child_name)
  97.  
  98.     def _on_col_btts_right_clicked(self, button, event, menu):
  99.         if event.button == 3:
  100.             menu.show_all()
  101.             menu.popup(None, button, None, None, event.button, event.time)
  102.  
  103.     def _on_mitem_cols_menu_toggled(self, menuitem, column):
  104.         column.set_visible(menuitem.get_active())
  105.  
  106.         configs = {
  107.             'persons_columns': {
  108.                 column.get_name(): str(column.get_visible())
  109.             },
  110.             'persons_columns_menu': {
  111.                 menuitem.get_name(): str(menuitem.get_active())
  112.             }
  113.         }
  114.  
  115.         inout.set_configs_file('pyloca.config', configs)
  116.  
  117.     def _on_new_button_clicked(self, button, widgets):
  118.         self._form_clear(widgets)
  119.  
  120.     def _on_find_button_clicked(self, button, searchbar):
  121.         searchbar.set_search_mode(not searchbar.get_search_mode())
  122.  
  123.     # Internal functions
  124.     def _form_clear(self, widgets):
  125.         """
  126.        Reset all widgets for a initial state
  127.        """
  128.         widget_type = None
  129.  
  130.         for widget in widgets:
  131.             widget_type = type(widget)
  132.  
  133.             if widget_type is ImgChooserBttWithCapture:
  134.                 widget.unselect_all()
  135.             elif widget_type in (Gtk.Entry, Gtk.SearchEntry):
  136.                 widget.set_text('')
  137.             elif widget_type is Gtk.Switch:
  138.                 widget.set_active(True)
  139.             elif widget_type is Gtk.CheckButton:
  140.                 widget.set_active(False)
  141.             elif widget_type is Gtk.ComboBox:
  142.                 widget.set_active(-1)
  143.             elif widget_type is Gtk.RadioButton and (widget.get_name() == 'fp_cel_phone1_radbtt' or widget.get_name() == 'fp_cel_phone2_radbtt'):
  144.                 widget.set_active(True)