Subversion Repositories basico

Rev

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