summaryrefslogtreecommitdiff
path: root/support/autolatex/plugins/gedit2/autolatex/config/cli/abstract_panel.py
blob: 8e8f997d39dbae1af8e7faddc122e4e425b603a1 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# autolatex/config/cli/figure_assignment_panel.py
# Copyright (C) 2013  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
#---------------------------------

# Include the Glib, Gtk and Gedit libraries
from gi.repository import GObject, Gtk
# AutoLaTeX internal libs
from ...utils import utils
from ...widgets import inherit_button

#---------------------------------
# CLASS Panel
#---------------------------------

# Gtk panel that is managing the configuration of the figure assignments
class AbstractPanel(Gtk.Box):
	__gtype_name__ = "AutoLaTeXAbstractConfigurationPanel"

	def __init__(self, is_document_level, directory, window):
		# Use an intermediate GtkBox to be sure that
		# the child GtkGrid will not be expanded vertically
		Gtk.Box.__init__(self)
		self._is_document_level = is_document_level
		self._directory = directory
		self._window = window
		self._settings = None
		#
		# Create the grid for the panel
		#
		self.set_property('orientation', Gtk.Orientation.VERTICAL)
		self._grid = Gtk.Grid()
		self.pack_start(self._grid, False, False, 0)
		self._grid.set_row_homogeneous(False)
		self._grid.set_column_homogeneous(False)
		self._grid.set_row_spacing(5)
		self._grid.set_column_spacing(5)
		self._grid.set_property('margin', 5)
		self._grid.set_property('vexpand', False)
		self._grid.set_property('hexpand', True)
		self._grid_row = 0
		#
		# Create the panel's widgets
		#
		self._init_widgets()
		#
		# Initialize the content
		#
		self._init_content()
		#
		# Update the state of the widgets
		#
		self.update_widget_states()
		#
		# Connext the signals
		#
		self._connect_signals()

	def _init_widgets(self):
		"""Invoked to fill the given grid with the widgets"""
		raise NotImplementedError("Please implement this method")

	def _init_content(self):
		"""Invoked to initialize the values in the widgets"""
		raise NotImplementedError("Please implement this method")

	def _connect_signals(self):
		"""Invoked to connect methods to the widgets' signals"""
		raise NotImplementedError("Please implement this method")

	def update_widget_states(self):
		"""Invoked to change the states of the widgets"""
		raise NotImplementedError("Please implement this method")

	def save(self):
		"""Invoked when the changes in the panel must be saved"""
		raise NotImplementedError("Please implement this method")

	# Utility function that permits to change the sensitivity
	# of a widget according to a given flag and the "inheriting" flag
	def _update_sentitivity(self, widget, is_sensitive):
		inherit_flag = widget.get_data('autolatex_overriding_configuration_value')
		if inherit_flag:
			inherit_flag = inherit_flag()
			if inherit_flag:
				return inherit_flag.set_widget_sensitivity(is_sensitive)
		widget.set_sensitive(is_sensitive)
		return is_sensitive

	# Utility function that permits to change the overriding of a widget
	def _init_overriding(self, widget, is_overriding):
		inherit_flag = widget.get_data('autolatex_overriding_configuration_value')
		if inherit_flag:
			inherit_flag = inherit_flag()
			if inherit_flag:
				inherit_flag.set_overriding_value(is_overriding)

	# Utility function that permits to get the sensitivity
	# of a widget according to a given flag and the "inheriting" flag
	def _get_sentitivity(self, widget):
		inherit_flag = widget.get_data('autolatex_overriding_configuration_value')
		if inherit_flag:
			inherit_flag = inherit_flag()
			if inherit_flag:
				return inherit_flag.get_widget_sensitivity(widget)
		return widget.get_sensitive()

	# Utility function that permits to get the overriding
	# of a widget according to a given flag and the "inheriting" flag
	def _get_overriding(self, widget):
		inherit_flag = widget.get_data('autolatex_overriding_configuration_value')
		if inherit_flag:
			inherit_flag = inherit_flag()
			if inherit_flag:
				return inherit_flag.get_overriding_value()
		return True

	# Utility function that permits to read the settings.
	def _read_settings(self, section):
		self._settings = utils.backend_get_configuration(
			self._directory,
			'project' if self._is_document_level else 'user',
			section)
		self._settings_section = section

	# Utility function to extract a string value from the settings
	def _get_settings_str(self, key, default_value=None):
		if self._settings and self._settings.has_option(self._settings_section, key):
			return str(self._settings.get(self._settings_section, key))
		else:
			return default_value

	# Utility function to extract a boolean value from the settings
	def _get_settings_bool(self, key, default_value=None):
		if self._settings and self._settings.has_option(self._settings_section, key):
			return bool(self._settings.getboolean(self._settings_section, key))
		else:
			return default_value

	# Utility function to extract an inherited string value from the settings
	def _get_settings_str_inh(self, key, default_value=None):
		return self._get_settings_str(key+'_INHERITED', default_value)

	# Utility function to extract an inherited boolean value from the settings
	def _get_settings_bool_inh(self, key, default_value=None):
		return self._get_settings_bool(key+'_INHERITED', default_value)

	# Utility function to set a string value from the settings
	def _set_settings_str(self, key, value):
		if self._settings:
			if not value:
				value = utils.CONFIG_EMPTY_VALUE
			self._settings.set(self._settings_section, key, value)

	# Utility function to set a boolean value from the settings
	def _set_settings_bool(self, key, value):
		if self._settings:
			if value is None:
				value = utils.CONFIG_EMPTY_VALUE
			else:
				value = ('true' if value else 'false')
			self._settings.set(self._settings_section, key, value)

	# Utility function to reset a section in the settings
	def _reset_settings_section(self, section=None):
		if self._settings:
			if not section:
				section = self._settings_section
			self._settings.remove_section(section)
			self._settings.add_section(section)

	# Utility function to create a label
	def _create_label(self, text, hexpand=False):
		ui_label = Gtk.Label(text)
		ui_label.set_property('hexpand', hexpand)
		ui_label.set_property('vexpand', False)
		ui_label.set_property('halign', Gtk.Align.START)
		ui_label.set_property('valign', Gtk.Align.CENTER)
		return ui_label

	# Utility function to create a row in a grid
	def _insert_row(self, left_widget, right_widget=None, enable_inherit=True):
		if right_widget:
			self._grid.attach(	left_widget,
					0,self._grid_row,1,1) # left, top, width, height
			self._grid.attach(	right_widget, 
					1,self._grid_row,1,1) # left, top, width, height
		else:
			self._grid.attach(	left_widget, 
					0,self._grid_row,2,1) # left, top, width, height
		inheriting_widget = None
		if enable_inherit:
			height = 1
			if isinstance(enable_inherit, (int, long)) and int(enable_inherit)>1:
				height = int(enable_inherit)
			inheriting_widget = inherit_button.InheritButton(self, left_widget, right_widget)
			inheriting_widget.set_property('expand', False)
			inheriting_widget.set_property('halign', Gtk.Align.CENTER)
			inheriting_widget.set_property('valign', Gtk.Align.CENTER)
			self._grid.attach(	inheriting_widget, 
					2,self._grid_row,1,height) # left, top, width, height
		self._grid_row = self._grid_row + 1
		return [ left_widget, right_widget, inheriting_widget ]

	# Utility function to create a row in a grid
	def _create_row(self, label_text, right_widget, enable_inherit=True):
		ui_label = self._create_label(label_text)
		right_widget.set_property('hexpand', True)
		right_widget.set_property('vexpand', False)
		return self._insert_row(ui_label, right_widget, enable_inherit)

	# Utility function to create a row in a grid with a Switch
	def _create_switch(self, label_text, enable_inherit=True):
		widget = Gtk.Switch()
		tab = self._create_row(label_text, widget, enable_inherit)
		widget.set_property('hexpand', False)
		widget.set_property('vexpand', False)
		widget.set_property('halign', Gtk.Align.END)
		widget.set_property('valign', Gtk.Align.CENTER)
		return tab


	# Utility function to create a row in a grid with an Entry
	def _create_entry(self, label_text, enable_inherit=True):
		widget = Gtk.Entry()
		return self._create_row(label_text, widget, enable_inherit)


	# Utility function to create a row in a grid with a ComboText
	def _create_combo(self, label_text, values=None, combo_name=None, enable_inherit=True):
		widget = Gtk.ComboBoxText()
		if combo_name:
			widget.set_name(combo_name)
		if values:
			for value in values:
				widget.append_text(value)
		return self._create_row(label_text, widget, enable_inherit)


	# Utility function to create a scroll panel for the given widget
	def _create_scroll_for(self, widget, width=400, height=400):
		scroll = Gtk.ScrolledWindow()
		scroll.add(widget)
		scroll.set_size_request(width, height)
		scroll.set_policy(
			Gtk.PolicyType.AUTOMATIC,
			Gtk.PolicyType.AUTOMATIC)
		scroll.set_shadow_type(Gtk.ShadowType.IN)
		scroll.set_property('hexpand', True)
		scroll.set_property('vexpand', True)
		return scroll