Subversion Repositories basico

Rev

Rev 335 | 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
315 t00mlabs 11
gi.require_version('Gtk', '3.0')
232 t00m 12
from gi.repository import Gtk
255 t00m 13
from gi.repository import GObject
297 t00mlabs 14
from basico.core.mod_wdg import BasicoWidget
315 t00mlabs 15
from basico.services.srv_cols import COL_DOWNLOADED
232 t00m 16
 
17
 
297 t00mlabs 18
class CollectionsMgtView(BasicoWidget, Gtk.VBox):
271 t00m 19
    __gtype_name__ = 'CollectionsMgtView'
246 t00m 20
    """
21
    Missing class docstring (missing-docstring)
22
    """
335 t00mlabs 23
    def __init__(self, app, sid, overwrite=False):
297 t00mlabs 24
        super().__init__(app, __class__.__name__)
232 t00m 25
        Gtk.VBox.__init__(self)
297 t00mlabs 26
        self.sid = sid    
241 t00m 27
        self.current_cid = None
335 t00mlabs 28
        self.overwrite = overwrite
29
        self.log.debug("CollectionsMgtView widget overwrite mode is %s", overwrite)
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
        """
311 t00mlabs 39
        self.srvgui = self.get_service("GUI")
40
        self.srvicm = self.get_service("IM")
41
        self.srvuif = self.get_service("UIF")
42
        self.srvclt = self.get_service('Collections')
43
        self.srvdtb = self.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()
317 t00mlabs 61
        if self.sid == '0000000000':
62
            title.set_markup('<big><b>Manage collections</b></big>')
63
        elif self.sid == 'view':
64
            title.set_markup('<big><b>Manage collections for this view</b></big>')
65
        else:
241 t00m 66
            title.set_markup('<big><b>Collections for SAP Note %s</b></big>' % str(int(self.sid)))
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)
246 t00m 80
        delete = self.srvuif.create_button('basico-delete', 24, 24, '')
232 t00m 81
        delete.connect('clicked', self.delete)
82
        hbox.pack_start(self.entry, True, True, 6)
83
        hbox.pack_start(delete, False, False, 0)
84
        self.pack_start(hbox, False, False, 0)
85
 
86
        # Collection Treeview
87
        scr = Gtk.ScrolledWindow()
88
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
89
        scr.set_shadow_type(Gtk.ShadowType.IN)
90
        viewport = Gtk.Viewport()
255 t00m 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)
337 t00mlabs 177
        # ~ 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]
315 t00mlabs 204
            if cid != COL_DOWNLOADED:
205
                linked = model.get(itr, 1)[0]
206
                if linked:
207
                    selected.append(cid)
335 t00mlabs 208
                    self.log.debug("Collection to be linked: %s (%s)", self.srvclt.get_name_by_cid(cid), cid)
232 t00m 209
 
210
        self.model.foreach(get_linked_collections)
317 t00mlabs 211
 
212
        if len(selected) == 0:
213
            selected.append(COL_DOWNLOADED)
214
 
215
        try:
216
            int(self.sid)
335 t00mlabs 217
            self.srvdtb.set_collections(self.sid, selected, self.overwrite)
332 t00mlabs 218
            self.srvuif.statusbar_msg("Selected collection linked to SAP Note %s" % str(int(self.sid)), True)
317 t00mlabs 219
        except:
254 t00m 220
            visor = self.srvgui.get_widget('visor_sapnotes')
317 t00mlabs 221
            bag = visor.get_filtered_bag()
232 t00m 222
            for sid in bag:
335 t00mlabs 223
                self.srvdtb.set_collections(sid, selected, self.overwrite)
332 t00mlabs 224
            self.srvuif.statusbar_msg("Selected collections linked to all SAP Note in this view", True)
317 t00mlabs 225
 
264 t00m 226
        visor = self.srvgui.get_widget('visor_sapnotes')
315 t00mlabs 227
        visor.populate_sapnotes()
267 t00m 228
        viewmenu = self.srvgui.get_widget('viewmenu')
315 t00mlabs 229
        viewmenu.populate()
277 t00m 230
        viewmenu.grab_focus()
318 t00mlabs 231
 
232 t00m 232
 
233
 
234
    def row_changed(self, selection):
246 t00m 235
        """
236
        Missing method docstring (missing-docstring)
237
        """
232 t00m 238
        model, treeiter = selection.get_selected() #_rows()
246 t00m 239
 
232 t00m 240
        try:
241 t00m 241
            self.current_cid = model[treeiter][0]
232 t00m 242
            title = model[treeiter][2]
243
            self.entry.set_text(title)
244
        except TypeError:
245
            pass
246
        except Exception as error:
279 t00m 247
            self.log.error("ERROR: collections->row_changed->error: %s" % error)
232 t00m 248
            raise
249
 
250
 
251
    def update(self, entry=None):
246 t00m 252
        """
253
        Missing method docstring (missing-docstring)
254
        """
255
        linked = self.srvdtb.get_collections(self.sid)
232 t00m 256
 
257
        if entry is not None and isinstance(entry, Gtk.Entry):
239 t00m 258
            name = entry.get_text()
332 t00mlabs 259
            res, msg = self.srvclt.create(name)            
260
            self.srvuif.statusbar_msg(msg, True)
261
 
232 t00m 262
        self.model.clear()
246 t00m 263
        collections = self.srvclt.get_all()
256 t00m 264
        if len(collections) > 0:
265
            for cid in collections:
266
                name = collections[cid]
267
                if cid in linked:
268
                    self.model.append([cid, 1, name])
269
                else:
270
                    self.model.append([cid, 0, name])
271
            self.current_cid = None
232 t00m 272
 
318 t00mlabs 273
        self.srvuif.statusbar_msg("Collections updated")
274
 
232 t00m 275
 
276
    def delete(self, button):
246 t00m 277
        """
278
        Missing method docstring (missing-docstring)
279
        """
268 t00m 280
        name = self.srvclt.get_name_by_cid(self.current_cid)
281
 
246 t00m 282
        deleted = self.srvclt.delete(self.current_cid)
241 t00m 283
        if deleted:
255 t00m 284
            self.update()
246 t00m 285
            viewmenu = self.srvgui.get_widget('viewmenu')
250 t00m 286
            viewmenu.populate()
318 t00mlabs 287
            msg = "Collection '%s' deleted" % name
288
            self.log.info(msg)
332 t00mlabs 289
            self.srvuif.statusbar_msg(msg, True)
241 t00m 290
        else:
250 t00m 291
            if self.current_cid is not None:
268 t00m 292
                title = "Collection '%s' not deleted" % name
250 t00m 293
                message = "Make sure there are not SAP Notes linked to this collection"
294
                self.srvuif.dialog_info(title, message)
241 t00m 295
 
318 t00mlabs 296
 
241 t00m 297
    def filter(self, *args):
246 t00m 298
        """
299
        Missing method docstring (missing-docstring)
300
        """
241 t00m 301
        self.visible_filter.refilter()
302
 
303
 
304
    def visible_function(self, model, itr, data):
246 t00m 305
        """
306
        Missing method docstring (missing-docstring)
307
        """
308
        entry = self.srvgui.get_widget('gtk_entry_collection_new')
241 t00m 309
        text = entry.get_text()
310
        title = model.get(itr, 2)[0]
311
        match = text.upper() in title.upper()
312
        return match
255 t00m 313
 
314
 
315
    def on_entry_changed(self, *args):
316
        entry = self.srvgui.get_widget('gtk_entry_collection_new')
317
        filter = entry.get_text()
318
        selection = self.treeview.get_selection()
319
 
320
 
321
        def gui_iterate_over_data(model, path, itr):
322
            title = self.sorted_model.get(itr, 2)[0]
323
            if len(filter) > 0:
324
                if filter.upper() in title.upper():
325
                    self.treeview.scroll_to_cell(path, self.column_title, True, 0.0, 0.0)
326
                    self.treeview.set_cursor_on_cell(path, self.column_title, self.renderer_title, False)
327
                else:
328
                    return
329
 
330
        GObject.signal_handler_block(self.selection, self.trv_signal_changed)
331
        self.sorted_model.foreach(gui_iterate_over_data)
332
        GObject.signal_handler_unblock(self.selection, self.trv_signal_changed)
333
 
334
 
335
    def edit_title(self, widget, path, target):
336
        model = self.treeview.get_model()
337
        treeiter = model.get_iter(path)
338
        cid = model[treeiter][0]
318 t00mlabs 339
        if cid == COL_DOWNLOADED:
340
            self.srvuif.statusbar_msg("You can't rename this collection")
341
            return
342
 
268 t00m 343
        name_old = self.srvclt.get_name_by_cid(cid)
255 t00m 344
        iter_has_child = model.iter_has_child(treeiter)
345
 
346
        if not iter_has_child:
347
            if len(target) > 0:
348
                self.srvclt.rename(cid, target)
349
                self.update()
318 t00mlabs 350
                msg = "Collection '%s' renamed to '%s'" % (name_old, target)
351
                self.log.info(msg)
352
                self.srvuif.statusbar_msg(msg)