Premier commit : Code et doc (état de publication)
[gedit/flake8] / flake8_integration / configuration.py
1 #-*- coding: utf8 -*-
2
3 """Module used to configure plugin from an easy-to-use widget
4
5 :author: Sébastien CHAZALLET <s.chazallet@gmail.com>
6 :organization: InsPyration EURL
7 :copyright: Copyright © InsPyration EURL <www.inspyration.org>
8 :license: GPL 3 <http://www.gnu.org/licenses/gpl.html>
9
10 :version: 0.1
11 """
12
13 from gettext import gettext as _
14
15 from gi.repository import Gtk
16
17
18 STUFF = 1
19
20
21 class GeditFlake8ConfigWidget(Gtk.Box):
22
23     def __init__(self, settings):
24         """Initialize the configuration widget from SettingsManager properties
25
26         :type  self: GeditFlake8ConfigWidget
27         :param self: Current widget
28
29         :type  settings: SettingsManager
30         :param settings: Object used to define all configurable properties
31
32         :rtype: None
33         """
34
35         # the current widget is a box
36         Gtk.Box.__init__(self, orientation=Gtk.Orientation.VERTICAL, spacing=6)
37
38         # reference to settings
39         self.settings = settings
40
41         # store useful entries into a dictionary
42         # in order to get their values later
43         self.entries = {}
44
45         # Add a label on the top ot widget
46         self.pack_start(Gtk.Label(_("Add to ignore list")), True, True, 0)
47
48         # create one checkbox for each warning that can be ignored
49         for key, option in self.settings.get_ignore_items():
50             button = Gtk.CheckButton(option)
51             button.set_active(self.settings.is_in_ignore_list(key))
52             self.pack_start(button, True, True, 0)
53             # add the check button to entries
54             self.entries[key] = button
55
56         # create an Hbox to get the label and the entry on the same line
57         hbox = Gtk.Box(spacing=3)
58         self.pack_start(hbox, True, True, 0)
59
60         # label for complexity entry
61         hbox.pack_start(Gtk.Label(_("Complexity")), True, True, 0)
62
63         # complexity entry is an widget allowing us to be sure to have an int
64         complexity_entry = Gtk.SpinButton()
65         complexity_entry.set_adjustment(Gtk.Adjustment(
66             self.settings.get_complexity(), -1, 21, 1, 1, 1))
67         hbox.pack_start(complexity_entry, True, True, 0)
68         # add the widget to entries
69         self.entries[self.settings.get_complexity_key()] = complexity_entry
70
71         # add an event to get back values and use them
72         self.connect("destroy", self.on_configure_destroy)
73
74     def on_configure_destroy(self, widget):
75         """Called when the widget is destroyed
76
77         :type  self: GeditFlake8ConfigWidget
78         :param self: Current widget
79
80         :type  widget: GeditFlake8ConfigWidget
81         :param widget: Current widget
82
83         :rtype: None
84         """
85
86         # extract complexity form element and get complexity value
87         complexity = int(
88             self.entries.pop(self.settings.get_complexity_key()).get_text())
89
90         # extract all other values
91         values = {k: v.get_active() for k, v in self.entries.items()}
92
93         # add back complexity value
94         values[self.settings.get_complexity_key()] = complexity
95
96         # update settings
97         self.settings.update_setting(values)