Subversion Repositories basico

Rev

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