Subversion Repositories basico

Rev

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