summaryrefslogtreecommitdiff
path: root/support/autolatex/libs/gtk3/autolatex/widgets/inherit_button.py
blob: f8086f6defa4aa2b6bd30954dcf70bb4829c76f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013-14  Stephane Galland <galland@arakhne.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

#---------------------------------
# IMPORTS
#---------------------------------

# Standard libraries
import weakref
# Include the Gtk libraries
from gi.repository import Gtk

#---------------------------------
# INTERNATIONALIZATION
#---------------------------------

import gettext
_T = gettext.gettext

#---------------------------------
# CLASS InheritButton
#---------------------------------

#
# Gtk button that is managing the inheriting flag.
#
# The inheriting flag indicates if the value(s)
# is inherited or locally set.
#
# This button is displaying an icon that is indicating
# if the inheriting flag is on or off.
#
# When the button state changed (toggled), the associated
# widgets have their enabling states changed.
#
# The attribute 'autolatex_overriding_configuration_value'
# in the associated widgets will reference this button.
# This attribute may be used to detect if the value of
# a given attribute by be taken from the inherited
# configuration or locally set.
#
class InheritButton(Gtk.ToggleButton):
  __gtype_name__ = "AutoLaTeXInheritButton"

  # Constructor.
  # @param container - widget that is containing this button.
  # @param widgets - array of widgets that are associated to this
  #                  button.
  def __init__(self, container, *widgets):
    Gtk.ToggleButton.__init__(self)
    self._is_init = False
    self._container = container
    self._widgets = list(widgets)
    self._inherit_icon = Gtk.Image.new_from_stock(Gtk.STOCK_DISCONNECT, Gtk.IconSize.BUTTON)
    self._override_icon = Gtk.Image.new_from_stock(Gtk.STOCK_CONNECT, Gtk.IconSize.BUTTON)
    self.set_relief(Gtk.ReliefStyle.NONE)
    self.set_active(False)
    #
    for widget in self._widgets:
      widget.autolatex_overriding_configuration_value = weakref.ref(self)

  # Event Handler: Button is pressed or unpressed.
  def on_button_toggled(self, widget, data=None):
    self._update_icon()
    self._update_widget_sensitivities(True)

  # Associate a widget to this button.
  def bind_widget(self, widget):
    if widget:
      widget.autolatex_overriding_configuration_value = weakref.ref(self)
      self._widgets.append(widget)

  # Disassociate a widget to this button.
  def unbind_widget(self, widget):
    if widget and widget in self._widgets:
      self._widgets.remove(widget)
      widget.autolatex_overriding_configuration_value = None

  # Replies if the inheriting flag is on or off.
  def get_overriding_value(self):
    return self.get_active()

  # Set the value of the inheriting flag.
  def set_overriding_value(self, override):
    self.set_active(override)
    if not self._is_init:
      self._is_init = True
      self._update_icon(override)
      self._update_widget_sensitivities(False, override)
      self.connect('toggled', self.on_button_toggled)

  # Update the content of the button (icon and tooltip)
  # according to the value of the inheriting flag.
  def _update_icon(self, is_over=None):
    if is_over is None:
      is_over = self.get_overriding_value()
    if is_over:
      self.set_tooltip_text(_T("Overriding the value in the current configuration"))
      self.set_image(self._override_icon)
    else:
      self.set_tooltip_text(_T("Get the value from the inherited configuration"))
      self.set_image(self._inherit_icon)

  # Update the sensitivity of the button
  # according to the value of the inheriting flag.
  def _update_widget_sensitivities(self, update_container, is_over=None):
    if is_over is None:
      is_over = self.get_overriding_value()
    for widget in self._widgets:
      widget.set_sensitive(is_over)
    if update_container:
      self._container.update_widget_states()

  # Override
  def set_widget_sensitivity(self, is_sensitive):
    override_value = self.get_overriding_value()
    if not override_value:
      is_sensitive = False
    for widget in self._widgets:
      widget.set_sensitive(is_sensitive)
    return is_sensitive

  # Override
  def get_widget_sensitivity(self, widget):
    override_value = self.get_overriding_value()
    if override_value:
      return widget.get_sensitive()
    else:
      return False