Subversion Repositories basico

Rev

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