summaryrefslogtreecommitdiff
path: root/support/autolatex/libs/gtk3/autolatex/config/figure_panel.py
blob: 94f362722d60a11a66557b18dd8846627d20176a (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 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 os
# Include the Glib, Gtk and Gedit libraries
from gi.repository import GObject, Gtk, Gio
# AutoLaTeX internal libs
from ..utils import utils
from . import abstract_panel

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

import gettext
_T = gettext.gettext

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

# Gtk panel that is managing the configuration of the figures
class Panel(abstract_panel.AbstractPanel):
  __gtype_name__ = "AutoLaTeXFigurePanel"

  def __init__(self, is_document_level, directory, window):
    abstract_panel.AbstractPanel.__init__(self, is_document_level, directory, window)
    

  #
  # Fill the grid
  #
  def _init_widgets(self):
    # Automatic generation of figures
    self._ui_is_figure_generated_checkbox = self._create_switch(
        _T("Automatic generation of pictures with translators"))[1]
    # Toolbar for the search paths
    self._ui_figure_path_label = self._create_label(
          _T("Search paths for the pictures"))
    hbox = Gtk.Box()
    hbox.set_property('orientation', Gtk.Orientation.HORIZONTAL)
    hbox.set_property('hexpand', False)
    hbox.set_property('vexpand', False)
    hbox.set_property('halign', Gtk.Align.END)
    hbox.set_property('valign', Gtk.Align.CENTER)
    inherting_button = self._insert_row(self._ui_figure_path_label, hbox, 2)[2]
    inherting_button.unbind_widget(hbox)

    # Button 1
    self._ui_figure_path_add_button = Gtk.Button()
    self._ui_figure_path_add_button.set_image(Gtk.Image.new_from_stock(Gtk.STOCK_ADD, Gtk.IconSize.BUTTON))
    inherting_button.bind_widget(self._ui_figure_path_add_button)
    hbox.add(self._ui_figure_path_add_button)
    # Button 2
    self._ui_figure_path_remove_button = Gtk.Button()
    self._ui_figure_path_remove_button.set_image(Gtk.Image.new_from_stock(Gtk.STOCK_REMOVE, Gtk.IconSize.BUTTON))
    inherting_button.bind_widget(self._ui_figure_path_remove_button)
    hbox.add(self._ui_figure_path_remove_button)
    # Button 3
    self._ui_figure_path_up_button = Gtk.Button()
    self._ui_figure_path_up_button.set_image(Gtk.Image.new_from_stock(Gtk.STOCK_GO_UP, Gtk.IconSize.BUTTON))
    inherting_button.bind_widget(self._ui_figure_path_up_button)
    hbox.add(self._ui_figure_path_up_button)
    # Button 4
    self._ui_figure_path_down_button = Gtk.Button()
    self._ui_figure_path_down_button.set_image(Gtk.Image.new_from_stock(Gtk.STOCK_GO_DOWN, Gtk.IconSize.BUTTON))
    inherting_button.bind_widget(self._ui_figure_path_down_button)
    hbox.add(self._ui_figure_path_down_button)
    # List
    self._ui_figure_path_store = Gtk.ListStore(str)
    self._ui_figure_path_widget = Gtk.TreeView()
    self._ui_figure_path_widget.set_model(self._ui_figure_path_store)
    self._ui_figure_path_widget.append_column(Gtk.TreeViewColumn("path", Gtk.CellRendererText(), text=0))
    self._ui_figure_path_widget.set_headers_clickable(False)
    self._ui_figure_path_widget.set_headers_visible(False)
    self._ui_figure_path_selection = self._ui_figure_path_widget.get_selection()
    self._ui_figure_path_selection.set_mode(Gtk.SelectionMode.MULTIPLE)
    inherting_button.bind_widget(self._ui_figure_path_widget)
    # Scroll
    ui_figure_path_scroll = self._create_scroll_for(
            self._ui_figure_path_widget, 400, 100)
    self._insert_row(ui_figure_path_scroll, None, False)


  #
  # Initialize the content
  #
  def _init_content(self):
    self._read_settings('generation')
    #
    inh = self._get_settings_bool_inh('generate images')
    cur = self._get_settings_bool('generate images')
    self._init_overriding(self._ui_is_figure_generated_checkbox, cur is not None)
    self._ui_is_figure_generated_checkbox.set_active(utils.first_of(cur, inh, True))
    #
    inh = self._get_settings_str_inh('image directory')
    cur = self._get_settings_str('image directory')
    self._init_overriding(self._ui_figure_path_widget, cur is not None)
    full_path = utils.first_of(cur, inh, '')
    if full_path:
      full_path = full_path.split(os.pathsep)
      for path in full_path:
        self._ui_figure_path_store.append( [ path.strip() ] )
    self._tmp_figure_path_moveup = False
    self._tmp_figure_path_movedown = False


  #
  # Connect signals
  #
  def _connect_signals(self):
    self._ui_is_figure_generated_checkbox.connect('notify::active',self.on_generate_image_toggled)
    self._ui_figure_path_selection.connect('changed',self.on_figure_path_selection_changed)
    self._ui_figure_path_add_button.connect('clicked',self.on_figure_path_add_button_clicked)
    self._ui_figure_path_remove_button.connect('clicked',self.on_figure_path_remove_button_clicked)
    self._ui_figure_path_up_button.connect('clicked',self.on_figure_path_up_button_clicked)
    self._ui_figure_path_down_button.connect('clicked',self.on_figure_path_down_button_clicked)


  # Change the state of the widgets according to the state of other widgets
  def update_widget_states(self):
    is_active = self._ui_is_figure_generated_checkbox.get_active()
    if not self._get_overriding(self._ui_is_figure_generated_checkbox):
      inh = self._get_settings_bool_inh('generate images', True)
      if (inh!=is_active):
        GObject.idle_add(self._ui_is_figure_generated_checkbox.set_active, inh)
        is_active = inh
    is_active = self._update_sentitivity(self._ui_figure_path_label, is_active)
    if is_active:
      if self._ui_figure_path_selection.count_selected_rows() > 0:
        self._ui_figure_path_up_button.set_sensitive(self._tmp_figure_path_moveup)
        self._ui_figure_path_down_button.set_sensitive(self._tmp_figure_path_movedown)
      else:
        self._ui_figure_path_remove_button.set_sensitive(False)
        self._ui_figure_path_up_button.set_sensitive(False)
        self._ui_figure_path_down_button.set_sensitive(False)

  # Invoke when the flag 'generate images' has changed
  def on_generate_image_toggled(self, widget, data=None):
    self.update_widget_states()

  def _check_figure_path_up_down(self, selection):
    n_data = len(self._ui_figure_path_store)
    self._tmp_figure_path_moveup = False
    self._tmp_figure_path_movedown = False
    selected_rows = selection.get_selected_rows()[1]
    i = 0
    last_row = len(selected_rows)-1
    while (i<=last_row and (not self._tmp_figure_path_moveup or not self._tmp_figure_path_movedown)):
      c_idx = selected_rows[i].get_indices()[0]
      if (i==0 and c_idx>0) or (i>0 and c_idx-1 > selected_rows[i-1].get_indices()[0]):
        self._tmp_figure_path_moveup = True
      if (i==last_row and c_idx<n_data-1) or (i<last_row and c_idx+1 < selected_rows[i+1].get_indices()[0]):
        self._tmp_figure_path_movedown = True
      i = i + 1

  # Invoked when the selection in the lsit of figure paths has changed
  def on_figure_path_selection_changed(self, selection, data=None):
    self._check_figure_path_up_down(selection)
    self.update_widget_states()

  # Invoked when the button "Add figure figure" was clicked
  def on_figure_path_add_button_clicked(self, button, data=None):
    dialog = Gtk.FileChooserDialog(_T("Select a figure path"), 
            self._window,
            Gtk.FileChooserAction.SELECT_FOLDER,
            [ Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
              Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT ])
    dialog.set_modal(True)
    response = dialog.run()
    filename = dialog.get_filename()
    dialog.destroy()
    if response == Gtk.ResponseType.ACCEPT:
      if self._is_document_level:
        c = Gio.File.new_for_path(filename)
        p = Gio.File.new_for_path(self._directory)
        r = p.get_relative_path(c)
        if r: filename = r
      self._ui_figure_path_store.append( [filename] )

  # Invoked when the button "Remove figure figure" was clicked
  def on_figure_path_remove_button_clicked(self, button, data=None):
    count = self._ui_figure_path_selection.count_selected_rows()
    if count > 0:
      selections = self._ui_figure_path_selection.get_selected_rows()[1]
      for i in range(len(selections)-1, -1, -1):
        list_iter = self._ui_figure_path_store.get_iter(selections[i])
        self._ui_figure_path_store.remove(list_iter)

  # Invoked when the button "Move up the figure paths" was clicked
  def on_figure_path_up_button_clicked(self, button, data=None):
    n_sel = self._ui_figure_path_selection.count_selected_rows()
    if n_sel > 0:
      selected_rows = self._ui_figure_path_selection.get_selected_rows()[1]
      movable = False
      p_idx = -1
      for i in range(0, n_sel):
        c_idx = selected_rows[i].get_indices()[0]
        if not movable and c_idx-1>p_idx: movable = True
        if movable:
          self._ui_figure_path_store.swap(
              self._ui_figure_path_store.get_iter(
                Gtk.TreePath(c_idx-1)),
              self._ui_figure_path_store.get_iter(selected_rows[i]))
        else: p_idx = c_idx
    self._check_figure_path_up_down(self._ui_figure_path_selection)
    self.update_widget_states()

  # Invoked when the button "Move down the figure paths" was clicked
  def on_figure_path_down_button_clicked(self, button, data=None):
    n_sel = self._ui_figure_path_selection.count_selected_rows()
    if n_sel > 0:
      selected_rows = self._ui_figure_path_selection.get_selected_rows()[1]
      movable = False
      p_idx = len(self._ui_figure_path_store)
      for i in range(n_sel-1, -1, -1):
        c_idx = selected_rows[i].get_indices()[0]
        if not movable and c_idx+1<p_idx: movable = True
        if movable:
          self._ui_figure_path_store.swap(
              self._ui_figure_path_store.get_iter(
                Gtk.TreePath(c_idx+1)),
              self._ui_figure_path_store.get_iter(selected_rows[i]))
        else: p_idx = c_idx
    self._check_figure_path_up_down(self._ui_figure_path_selection)
    self.update_widget_states()

  # Invoked when the changes in the panel must be saved
  def save(self):
    self._reset_settings_section()
    #
    if self._get_sentitivity(self._ui_is_figure_generated_checkbox):
      v = self._ui_is_figure_generated_checkbox.get_active()
    else:
      v = None
    self._set_settings_bool('generate images', v)
    #
    if self._get_sentitivity(self._ui_figure_path_label):
      path = ''
      for row in self._ui_figure_path_store:
        if path: path = path + os.pathsep
        path = path + row[0].strip()
    else:
      path = None
    self._set_settings_str('image directory', path)
    #
    return utils.backend_set_configuration(
        self._directory,
        'project' if self._is_document_level else 'user',
        self._settings)