Premier commit : Code et doc (état de publication)
[gedit/flake8] / flake8_integration / tree.py
1 #-*- coding: utf-8 -*-
2
3 """Module managing the tree and all stuff around it
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
14 from gettext import gettext as _
15
16 from gi.repository import Gtk
17
18 SP_COL_TITLES = (_("Code"), _("L"), _("C"), _("Errors / Warnings"))
19 SP_COL_TYPES = (str, int, int, str)
20
21 PYTHON_ICON = 'gnome-mime-text-x-python'
22
23
24 class GeditFlake8Tree(Gtk.TreeView):
25     """Tree inside gedit side panel containing all flake8 warnings"""
26
27     def __init__(self, window):
28         """Configure tree columns, tree selection and add it to side panel
29
30         :type  self: GeditFlake8Tree
31         :param self: Current tree
32         :type  window: Gedit.Window
33         :param window: GEdit window object
34
35         :rtype: None
36         """
37
38         # use a list store to manage warnings
39         Gtk.TreeView.__init__(self, Gtk.ListStore(*SP_COL_TYPES))
40
41         # compose window
42         self.window = window
43
44         # create columns
45         for index, title in enumerate(SP_COL_TITLES):
46             self.append_column(Gtk.TreeViewColumn(
47                 _(title),
48                 Gtk.CellRendererText(),
49                 text=index))
50
51         # Allow to do something when a tree line is selected
52         self.get_selection().connect("changed", self.on_tree_selection)
53
54         # create an icon for the side panel
55         image = Gtk.Image()
56         image.set_from_icon_name(PYTHON_ICON, Gtk.IconSize.MENU)
57
58         # get the side panel (created by GEdit)
59         sp = self.window.get_side_panel()
60
61         # Add the tree in the side panel
62         sp.add_item(
63             self,
64             'flake8_results',
65             _("Flake8 results"),
66             image
67         )
68
69         # set the side panel invisible
70         sp.set_property("visible", False)
71
72     def remove(self):
73         """remove the tree from the side panel
74
75         :type  self: GeditFlake8Tree
76         :param self: Current tree
77
78         :rtype: None
79         """
80
81         # get the side panel (created by GEdit)
82         sp = self.window.get_side_panel()
83         sp.remove_item(self)
84
85     def activate(self):
86         """show the tree in the side panel
87         
88         :type  self: GeditFlake8Tree
89         :param self: Current tree
90         
91         :rtype: None
92         """
93
94         sp = self.window.get_side_panel()
95         sp.set_property("visible", True)
96         sp.activate_item(self)
97
98     def on_tree_selection(self, selection):
99         """When a tree line is selected, go to the file linked line
100
101         :type  self: GeditFlake8Tree
102         :param self: Current tree
103         :type  selection: Gtk.TreeSelection
104         :param selection: Current selection
105
106         :rtype: None
107         """
108
109         doc = self.window.get_active_document()
110         if not doc:
111             return
112
113         # get the tree line selected and put the cursor to the file linked line
114         model, treeiter = selection.get_selected()
115         doc.goto_line(model[treeiter][1] - 1)
116
117         # scroll the view to the cursor
118         self.window.get_active_view().scroll_to_cursor()
119
120     def update_model(self, datas):
121         """Update the tree with new datas
122
123         :type  self: GeditFlake8Tree
124         :param self: Current tree
125         :type  datas: list
126         :param datas: list of ready to use (str, int, int, str) tuples
127
128         :rtype: None
129         """
130
131         # get the model (Gtk.ListStore)
132         model = self.get_model()
133
134         # Empty model
135         model.clear()
136
137         # Fill model with new values
138         for data in datas:
139             model.append(data)