Subversion Repositories basico

Rev

Rev 243 | Rev 250 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
232 t00m 1
#!/usr/bin/python
246 t00m 2
"""
232 t00m 3
# -*- coding: utf-8 -*-
246 t00m 4
# File: wdg_cols.py
232 t00m 5
# Author: Tomás Vírseda
6
# License: GPL v3
7
# Description: Collections Widget
246 t00m 8
"""
232 t00m 9
 
10
import gi
11
from gi.repository import Gtk
12
 
246 t00m 13
gi.require_version('Gtk', '3.0')
232 t00m 14
 
243 t00m 15
class CollectionsMgtView(Gtk.VBox):
246 t00m 16
    """
17
    Missing class docstring (missing-docstring)
18
    """
232 t00m 19
    def __init__(self, app, sid, popover):
20
        Gtk.VBox.__init__(self)
21
        self.app = app
246 t00m 22
        self.error = self.app.error
232 t00m 23
        self.sid = sid
24
        self.popover = popover
241 t00m 25
        self.current_cid = None
232 t00m 26
        self.get_services()
27
        self.setup()
28
        self.setup_treeview()
29
        self.update()
30
 
31
 
32
    def get_services(self):
33
        """
246 t00m 34
        Load services to be used in this class
35
        """
36
        self.srvgui = self.app.get_service("GUI")
37
        self.srvicm = self.app.get_service("IM")
38
        self.srvuif = self.app.get_service("UIF")
39
        self.srvclt = self.app.get_service('Collections')
40
        self.srvdtb = self.app.get_service('DB')
232 t00m 41
 
42
 
43
    def setup(self):
246 t00m 44
        """
45
        Missing method docstring (missing-docstring)
46
        """
232 t00m 47
        # Setup Widget properties
48
        self.set_size_request(400, 680)
49
        self.set_property('margin', 3)
50
        self.set_hexpand(True)
51
        self.set_vexpand(True)
52
 
53
        # CollectionMgt Header
54
        header = Gtk.VBox()
55
        hbox = Gtk.HBox()
246 t00m 56
        icon = self.srvicm.get_new_image_icon('basico-collection')
232 t00m 57
        title = Gtk.Label()
241 t00m 58
        if self.sid != '0000000000':
59
            title.set_markup('<big><b>Collections for SAP Note %s</b></big>' % str(int(self.sid)))
60
        else:
61
            title.set_markup('<big><b>Manage collections</b></big>')
232 t00m 62
        title.set_xalign(0.0)
63
        hbox.pack_start(icon, False, False, 6)
64
        hbox.pack_start(title, True, True, 0)
65
        separator = Gtk.Separator()
66
        header.pack_start(hbox, False, False, 0)
67
        header.pack_start(separator, False, False, 3)
68
        self.pack_start(header, False, False, 3)
69
 
70
        # Entry Widget /  Delete entry button
71
        hbox = Gtk.HBox()
246 t00m 72
        self.entry = self.srvgui.add_widget('gtk_entry_collection_new', Gtk.Entry())
232 t00m 73
        self.entry.connect('activate', self.update)
241 t00m 74
        self.entry.connect('changed', self.filter)
246 t00m 75
        delete = self.srvuif.create_button('basico-delete', 24, 24, '')
232 t00m 76
        delete.connect('clicked', self.delete)
77
        hbox.pack_start(self.entry, True, True, 6)
78
        hbox.pack_start(delete, False, False, 0)
79
        self.pack_start(hbox, False, False, 0)
80
 
81
        # Collection Treeview
82
        scr = Gtk.ScrolledWindow()
83
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
84
        scr.set_shadow_type(Gtk.ShadowType.IN)
85
        viewport = Gtk.Viewport()
86
        self.treeview = Gtk.TreeView()
87
        viewport.add(self.treeview)
88
        scr.add(viewport)
89
        self.pack_start(scr, True, True, 6)
90
 
91
        # Footer
92
        separator = Gtk.Separator()
93
        self.pack_start(separator, False, False, 0)
94
 
95
        ## Buttons
96
        footer = Gtk.HBox()
246 t00m 97
        accept = self.srvuif.create_button('basico-check-ok', 24, 24, '<b>Apply changes</b>')
232 t00m 98
        accept.connect('clicked', self.accept)
99
        footer.pack_start(accept, True, False, 0)
100
        self.pack_start(footer, False, False, 3)
101
 
102
 
103
    def setup_treeview(self):
246 t00m 104
        """
105
        Missing method docstring (missing-docstring)
106
        """
232 t00m 107
        # Setup model
108
        self.model = Gtk.ListStore(
109
            str,        # key
110
            int,        # checkbox
111
            str,        # title
112
        )
113
 
114
        # Setup columns
115
        # Collection key
116
        self.renderer_key = Gtk.CellRendererText()
117
        self.column_key = Gtk.TreeViewColumn('Key', self.renderer_key, text=0)
118
        self.column_key.set_visible(False)
119
        self.column_key.set_expand(False)
120
        self.column_key.set_clickable(False)
121
        self.column_key.set_sort_indicator(False)
122
        self.treeview.append_column(self.column_key)
123
 
124
        # Collection Checkbox
125
        self.renderer_checkbox = Gtk.CellRendererToggle()
126
        self.renderer_checkbox.connect("toggled", self.toggle_checkbox)
127
        self.column_checkbox = Gtk.TreeViewColumn('Selected', self.renderer_checkbox, active=1)
128
        self.column_checkbox.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
129
        self.column_checkbox.set_visible(True)
130
        self.column_checkbox.set_expand(False)
131
        self.column_checkbox.set_clickable(True)
132
        self.column_checkbox.set_sort_indicator(False)
133
        self.treeview.append_column(self.column_checkbox)
134
 
135
        # Collection title
136
        self.renderer_title = Gtk.CellRendererText()
137
        self.column_title = Gtk.TreeViewColumn('Collection', self.renderer_title, markup=2)
138
        self.column_title.set_visible(True)
139
        self.column_title.set_expand(True)
140
        self.column_title.set_clickable(True)
141
        self.column_title.set_sort_indicator(True)
142
        self.model.set_sort_column_id(2, Gtk.SortType.ASCENDING)
143
        self.treeview.append_column(self.column_title)
144
 
145
        # TreeView common
146
        # ~ self.sorted_model = Gtk.TreeModelSort(model=self.model)
147
        # ~ self.sorted_model.set_sort_column_id(2, Gtk.SortType.ASCENDING)
148
        # ~ self.treeview.set_model(self.sorted_model)
149
 
150
        self.treeview.set_can_focus(False)
151
        self.treeview.set_headers_visible(True)
152
        self.treeview.set_enable_search(True)
153
        self.treeview.set_hover_selection(False)
154
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.HORIZONTAL)
155
        # ~ self.treeview.modify_font(Pango.FontDescription('Monospace 10'))
156
 
157
        # Selection
158
        self.selection = self.treeview.get_selection()
159
        self.selection.set_mode(Gtk.SelectionMode.SINGLE)
160
        self.selection.connect('changed', self.row_changed)
161
 
241 t00m 162
        # Filter
163
        self.visible_filter = self.model.filter_new()
164
        self.visible_filter.set_visible_func(self.visible_function)
232 t00m 165
 
241 t00m 166
        self.treeview.set_model(self.visible_filter)
167
 
232 t00m 168
        self.show_all()
169
 
170
 
171
    def toggle_checkbox(self, cell, path):
246 t00m 172
        """
173
        Missing method docstring (missing-docstring)
174
        """
232 t00m 175
        self.model[path][1] = not self.model[path][1]
176
 
177
 
178
    def accept(self, button):
246 t00m 179
        """
180
        Missing method docstring (missing-docstring)
181
        """
232 t00m 182
        selected = []
246 t00m 183
 
232 t00m 184
        def get_linked_collections(model, path, itr):
246 t00m 185
            """
186
            Missing method docstring (missing-docstring)
187
            """
241 t00m 188
            cid = model.get(itr, 0)[0]
232 t00m 189
            linked = model.get(itr, 1)[0]
190
            if linked:
241 t00m 191
                selected.append(cid)
232 t00m 192
 
193
        self.model.foreach(get_linked_collections)
194
        if self.sid != '0000000000':
246 t00m 195
            self.srvdtb.set_collections(self.sid, selected)
232 t00m 196
        else:
246 t00m 197
            visor = self.srvgui.get_widget('visor')
232 t00m 198
            bag = visor.get_bag()
199
            for sid in bag:
246 t00m 200
                self.srvdtb.set_collections(sid, selected)
232 t00m 201
 
202
        self.popover.hide()
203
 
204
 
205
    def row_changed(self, selection):
246 t00m 206
        """
207
        Missing method docstring (missing-docstring)
208
        """
232 t00m 209
        model, treeiter = selection.get_selected() #_rows()
246 t00m 210
 
232 t00m 211
        try:
241 t00m 212
            self.current_cid = model[treeiter][0]
232 t00m 213
            title = model[treeiter][2]
214
            self.entry.set_text(title)
215
        except TypeError:
216
            pass
217
        except Exception as error:
236 t00m 218
            self.error("ERROR: collections->row_changed->error: %s" % error)
232 t00m 219
            raise
220
 
221
 
222
    def update(self, entry=None):
246 t00m 223
        """
224
        Missing method docstring (missing-docstring)
225
        """
226
        linked = self.srvdtb.get_collections(self.sid)
232 t00m 227
 
228
        if entry is not None and isinstance(entry, Gtk.Entry):
239 t00m 229
            name = entry.get_text()
246 t00m 230
            self.srvclt.create(name)
232 t00m 231
 
232
        self.model.clear()
246 t00m 233
        collections = self.srvclt.get_all()
241 t00m 234
        for cid in collections:
235
            name = collections[cid]
236
            if cid in linked:
237
                self.model.append([cid, 1, name])
232 t00m 238
            else:
241 t00m 239
                self.model.append([cid, 0, name])
240
        self.current_cid = None
232 t00m 241
 
242
 
243
    def delete(self, button):
246 t00m 244
        """
245
        Missing method docstring (missing-docstring)
246
        """
241 t00m 247
        self.debug("Deleting collection with id: %s" % self.current_cid)
246 t00m 248
        deleted = self.srvclt.delete(self.current_cid)
241 t00m 249
        if deleted:
246 t00m 250
            viewmenu = self.srvgui.get_widget('viewmenu')
241 t00m 251
            viewmenu.refresh()
252
            viewmenu.grab_focus()
253
        else:
246 t00m 254
            colname = self.srvclt.get_name_by_cid(self.current_cid)
241 t00m 255
            title = "Collection '%s' not deleted" % colname
256
            message = "Make sure there are not SAP Notes linked to this collection"
246 t00m 257
            self.srvuif.dialog_info(title, message)
241 t00m 258
 
259
    def filter(self, *args):
246 t00m 260
        """
261
        Missing method docstring (missing-docstring)
262
        """
241 t00m 263
        self.visible_filter.refilter()
264
 
265
 
266
    def visible_function(self, model, itr, data):
246 t00m 267
        """
268
        Missing method docstring (missing-docstring)
269
        """
270
        entry = self.srvgui.get_widget('gtk_entry_collection_new')
241 t00m 271
        text = entry.get_text()
272
        title = model.get(itr, 2)[0]
273
        match = text.upper() in title.upper()
274
        return match