Subversion Repositories basico

Rev

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