Subversion Repositories basico

Rev

Rev 255 | Rev 263 | 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
251 t00m 7
# Description: Collections Managemnet Widget
246 t00m 8
"""
232 t00m 9
 
10
import gi
11
from gi.repository import Gtk
255 t00m 12
from gi.repository import GObject
232 t00m 13
 
246 t00m 14
gi.require_version('Gtk', '3.0')
232 t00m 15
 
243 t00m 16
class CollectionsMgtView(Gtk.VBox):
246 t00m 17
    """
18
    Missing class docstring (missing-docstring)
19
    """
232 t00m 20
    def __init__(self, app, sid, popover):
21
        Gtk.VBox.__init__(self)
22
        self.app = app
246 t00m 23
        self.error = self.app.error
232 t00m 24
        self.sid = sid
25
        self.popover = popover
241 t00m 26
        self.current_cid = None
232 t00m 27
        self.get_services()
28
        self.setup()
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())
255 t00m 73
        self.entry.connect('changed', self.on_entry_changed)
232 t00m 74
        self.entry.connect('activate', self.update)
255 t00m 75
        # ~ self.entry.connect('changed', self.filter)
246 t00m 76
        delete = self.srvuif.create_button('basico-delete', 24, 24, '')
232 t00m 77
        delete.connect('clicked', self.delete)
78
        hbox.pack_start(self.entry, True, True, 6)
79
        hbox.pack_start(delete, False, False, 0)
80
        self.pack_start(hbox, False, False, 0)
81
 
82
        # Collection Treeview
83
        scr = Gtk.ScrolledWindow()
84
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
85
        scr.set_shadow_type(Gtk.ShadowType.IN)
86
        viewport = Gtk.Viewport()
255 t00m 87
        # ~ self.treeview = Gtk.TreeView()
88
        self.treeview = self.setup_treeview()
232 t00m 89
        viewport.add(self.treeview)
90
        scr.add(viewport)
91
        self.pack_start(scr, True, True, 6)
92
 
250 t00m 93
        if self.sid != '0000000000':
94
            # Footer
95
            separator = Gtk.Separator()
96
            self.pack_start(separator, False, False, 0)
232 t00m 97
 
250 t00m 98
            ## Buttons
99
            footer = Gtk.HBox()
100
            accept = self.srvuif.create_button('basico-check-ok', 24, 24, '<b>Apply changes</b>')
101
            accept.connect('clicked', self.accept)
102
            footer.pack_start(accept, True, False, 0)
103
            self.pack_start(footer, False, False, 3)
232 t00m 104
 
105
 
106
    def setup_treeview(self):
246 t00m 107
        """
108
        Missing method docstring (missing-docstring)
109
        """
232 t00m 110
        # Setup model
255 t00m 111
        treeview = Gtk.TreeView()
232 t00m 112
        self.model = Gtk.ListStore(
113
            str,        # key
114
            int,        # checkbox
115
            str,        # title
116
        )
117
 
118
        # Setup columns
119
        # Collection key
120
        self.renderer_key = Gtk.CellRendererText()
121
        self.column_key = Gtk.TreeViewColumn('Key', self.renderer_key, text=0)
122
        self.column_key.set_visible(False)
123
        self.column_key.set_expand(False)
124
        self.column_key.set_clickable(False)
125
        self.column_key.set_sort_indicator(False)
255 t00m 126
        treeview.append_column(self.column_key)
232 t00m 127
 
128
        # Collection Checkbox
129
        self.renderer_checkbox = Gtk.CellRendererToggle()
130
        self.renderer_checkbox.connect("toggled", self.toggle_checkbox)
131
        self.column_checkbox = Gtk.TreeViewColumn('Selected', self.renderer_checkbox, active=1)
132
        self.column_checkbox.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
250 t00m 133
        if self.sid == '0000000000':
134
            self.column_checkbox.set_visible(False)
135
        else:
136
            self.column_checkbox.set_visible(True)
232 t00m 137
        self.column_checkbox.set_expand(False)
138
        self.column_checkbox.set_clickable(True)
139
        self.column_checkbox.set_sort_indicator(False)
255 t00m 140
        treeview.append_column(self.column_checkbox)
232 t00m 141
 
142
        # Collection title
143
        self.renderer_title = Gtk.CellRendererText()
255 t00m 144
        self.renderer_title.connect('edited', self.edit_title)
145
        self.renderer_title.set_property("editable", True)
232 t00m 146
        self.column_title = Gtk.TreeViewColumn('Collection', self.renderer_title, markup=2)
147
        self.column_title.set_visible(True)
148
        self.column_title.set_expand(True)
149
        self.column_title.set_clickable(True)
150
        self.column_title.set_sort_indicator(True)
151
        self.model.set_sort_column_id(2, Gtk.SortType.ASCENDING)
255 t00m 152
        treeview.append_column(self.column_title)
232 t00m 153
 
154
        # TreeView common
255 t00m 155
        self.sorted_model = Gtk.TreeModelSort(model=self.model)
156
        self.sorted_model.set_sort_column_id(2, Gtk.SortType.ASCENDING)
157
        treeview.set_model(self.sorted_model)
232 t00m 158
 
255 t00m 159
        treeview.set_can_focus(False)
160
        treeview.set_headers_visible(True)
161
        treeview.set_enable_search(True)
162
        treeview.set_hover_selection(False)
163
        treeview.set_grid_lines(Gtk.TreeViewGridLines.HORIZONTAL)
232 t00m 164
        # ~ self.treeview.modify_font(Pango.FontDescription('Monospace 10'))
165
 
166
        # Selection
255 t00m 167
        self.selection = treeview.get_selection()
232 t00m 168
        self.selection.set_mode(Gtk.SelectionMode.SINGLE)
255 t00m 169
        self.trv_signal_changed = self.selection.connect('changed', self.row_changed)
232 t00m 170
 
241 t00m 171
        # Filter
255 t00m 172
        # ~ self.visible_filter = self.model.filter_new()
173
        # ~ self.visible_filter.set_visible_func(self.visible_function)
174
        # ~ self.treeview.set_model(self.visible_filter)
232 t00m 175
 
255 t00m 176
        treeview.set_model(self.sorted_model)
241 t00m 177
 
232 t00m 178
        self.show_all()
179
 
255 t00m 180
        return treeview
232 t00m 181
 
255 t00m 182
 
232 t00m 183
    def toggle_checkbox(self, cell, path):
246 t00m 184
        """
185
        Missing method docstring (missing-docstring)
186
        """
232 t00m 187
        self.model[path][1] = not self.model[path][1]
188
 
189
 
190
    def accept(self, button):
246 t00m 191
        """
192
        Missing method docstring (missing-docstring)
193
        """
232 t00m 194
        selected = []
246 t00m 195
 
232 t00m 196
        def get_linked_collections(model, path, itr):
246 t00m 197
            """
198
            Missing method docstring (missing-docstring)
199
            """
241 t00m 200
            cid = model.get(itr, 0)[0]
232 t00m 201
            linked = model.get(itr, 1)[0]
202
            if linked:
241 t00m 203
                selected.append(cid)
232 t00m 204
 
205
        self.model.foreach(get_linked_collections)
206
        if self.sid != '0000000000':
246 t00m 207
            self.srvdtb.set_collections(self.sid, selected)
232 t00m 208
        else:
254 t00m 209
            visor = self.srvgui.get_widget('visor_sapnotes')
232 t00m 210
            bag = visor.get_bag()
211
            for sid in bag:
246 t00m 212
                self.srvdtb.set_collections(sid, selected)
250 t00m 213
        viewmenu = self.srvgui.get_widget('viewmenu')
214
        viewmenu.populate()
215
        viewmenu.grab_focus()
232 t00m 216
        self.popover.hide()
217
 
218
 
219
    def row_changed(self, selection):
246 t00m 220
        """
221
        Missing method docstring (missing-docstring)
222
        """
232 t00m 223
        model, treeiter = selection.get_selected() #_rows()
246 t00m 224
 
232 t00m 225
        try:
241 t00m 226
            self.current_cid = model[treeiter][0]
232 t00m 227
            title = model[treeiter][2]
228
            self.entry.set_text(title)
229
        except TypeError:
230
            pass
231
        except Exception as error:
236 t00m 232
            self.error("ERROR: collections->row_changed->error: %s" % error)
232 t00m 233
            raise
234
 
235
 
236
    def update(self, entry=None):
246 t00m 237
        """
238
        Missing method docstring (missing-docstring)
239
        """
240
        linked = self.srvdtb.get_collections(self.sid)
232 t00m 241
 
242
        if entry is not None and isinstance(entry, Gtk.Entry):
239 t00m 243
            name = entry.get_text()
246 t00m 244
            self.srvclt.create(name)
232 t00m 245
 
246
        self.model.clear()
246 t00m 247
        collections = self.srvclt.get_all()
256 t00m 248
        self.app.debug("NUM COLS:: %d" % len(collections))
249
        if len(collections) > 0:
250
            for cid in collections:
251
                name = collections[cid]
252
                if cid in linked:
253
                    self.model.append([cid, 1, name])
254
                else:
255
                    self.model.append([cid, 0, name])
256
            self.current_cid = None
232 t00m 257
 
258
 
259
    def delete(self, button):
246 t00m 260
        """
261
        Missing method docstring (missing-docstring)
262
        """
250 t00m 263
        # ~ self.debug("Deleting collection with id: %s" % self.current_cid)
246 t00m 264
        deleted = self.srvclt.delete(self.current_cid)
241 t00m 265
        if deleted:
255 t00m 266
            self.update()
246 t00m 267
            viewmenu = self.srvgui.get_widget('viewmenu')
250 t00m 268
            viewmenu.populate()
241 t00m 269
        else:
250 t00m 270
            if self.current_cid is not None:
271
                colname = self.srvclt.get_name_by_cid(self.current_cid)
272
                title = "Collection '%s' not deleted" % colname
273
                message = "Make sure there are not SAP Notes linked to this collection"
274
                self.srvuif.dialog_info(title, message)
241 t00m 275
 
276
    def filter(self, *args):
246 t00m 277
        """
278
        Missing method docstring (missing-docstring)
279
        """
241 t00m 280
        self.visible_filter.refilter()
281
 
282
 
283
    def visible_function(self, model, itr, data):
246 t00m 284
        """
285
        Missing method docstring (missing-docstring)
286
        """
287
        entry = self.srvgui.get_widget('gtk_entry_collection_new')
241 t00m 288
        text = entry.get_text()
289
        title = model.get(itr, 2)[0]
290
        match = text.upper() in title.upper()
291
        return match
255 t00m 292
 
293
 
294
    def on_entry_changed(self, *args):
295
        entry = self.srvgui.get_widget('gtk_entry_collection_new')
296
        filter = entry.get_text()
297
        selection = self.treeview.get_selection()
298
 
299
 
300
        def gui_iterate_over_data(model, path, itr):
301
            title = self.sorted_model.get(itr, 2)[0]
302
            if len(filter) > 0:
303
                if filter.upper() in title.upper():
304
                    self.treeview.scroll_to_cell(path, self.column_title, True, 0.0, 0.0)
305
                    self.treeview.set_cursor_on_cell(path, self.column_title, self.renderer_title, False)
306
                else:
307
                    return
308
 
309
        GObject.signal_handler_block(self.selection, self.trv_signal_changed)
310
        self.sorted_model.foreach(gui_iterate_over_data)
311
        GObject.signal_handler_unblock(self.selection, self.trv_signal_changed)
312
 
313
 
314
    def edit_title(self, widget, path, target):
315
        model = self.treeview.get_model()
316
        treeiter = model.get_iter(path)
317
        cid = model[treeiter][0]
318
        iter_has_child = model.iter_has_child(treeiter)
319
 
320
        if not iter_has_child:
321
            if len(target) > 0:
322
                self.srvclt.rename(cid, target)
323
                self.update()