Subversion Repositories basico

Rev

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