Subversion Repositories basico

Rev

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