Subversion Repositories basico

Rev

Rev 236 | Rev 241 | 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
28
        self.current_tid = None
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()
56
        title.set_markup('<big><b>Collections for SAP Note %s</b></big>' % str(int(self.sid)))
57
        title.set_xalign(0.0)
58
        hbox.pack_start(icon, False, False, 6)
59
        hbox.pack_start(title, True, True, 0)
60
        separator = Gtk.Separator()
61
        header.pack_start(hbox, False, False, 0)
62
        header.pack_start(separator, False, False, 3)
63
        self.pack_start(header, False, False, 3)
64
 
65
        # Entry Widget /  Delete entry button
66
        hbox = Gtk.HBox()
67
        self.entry = self.gui.add_widget('gtk_entry_collection_new', Gtk.Entry())
68
        self.entry.connect('activate', self.update)
69
        delete = self.uif.create_button('basico-delete', 24, 24, '')
70
        delete.connect('clicked', self.delete)
71
        hbox.pack_start(self.entry, True, True, 6)
72
        hbox.pack_start(delete, False, False, 0)
73
        self.pack_start(hbox, False, False, 0)
74
 
75
        # Collection Treeview
76
        scr = Gtk.ScrolledWindow()
77
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
78
        scr.set_shadow_type(Gtk.ShadowType.IN)
79
        viewport = Gtk.Viewport()
80
        self.treeview = Gtk.TreeView()
81
        viewport.add(self.treeview)
82
        scr.add(viewport)
83
        self.pack_start(scr, True, True, 6)
84
 
85
        # Footer
86
        separator = Gtk.Separator()
87
        self.pack_start(separator, False, False, 0)
88
 
89
        ## Buttons
90
        footer = Gtk.HBox()
91
        accept = self.uif.create_button('basico-check-ok', 24, 24, '<b>Apply changes</b>')
92
        accept.connect('clicked', self.accept)
93
        footer.pack_start(accept, True, False, 0)
94
        self.pack_start(footer, False, False, 3)
95
 
96
 
97
    def setup_treeview(self):
98
        # Setup model
99
        self.model = Gtk.ListStore(
100
            str,        # key
101
            int,        # checkbox
102
            str,        # title
103
        )
104
 
105
        # Setup columns
106
        # Collection key
107
        self.renderer_key = Gtk.CellRendererText()
108
        self.column_key = Gtk.TreeViewColumn('Key', self.renderer_key, text=0)
109
        self.column_key.set_visible(False)
110
        self.column_key.set_expand(False)
111
        self.column_key.set_clickable(False)
112
        self.column_key.set_sort_indicator(False)
113
        self.treeview.append_column(self.column_key)
114
 
115
        # Collection Checkbox
116
        self.renderer_checkbox = Gtk.CellRendererToggle()
117
        self.renderer_checkbox.connect("toggled", self.toggle_checkbox)
118
        self.column_checkbox = Gtk.TreeViewColumn('Selected', self.renderer_checkbox, active=1)
119
        self.column_checkbox.set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
120
        self.column_checkbox.set_visible(True)
121
        self.column_checkbox.set_expand(False)
122
        self.column_checkbox.set_clickable(True)
123
        self.column_checkbox.set_sort_indicator(False)
124
        self.treeview.append_column(self.column_checkbox)
125
 
126
        # Collection title
127
        self.renderer_title = Gtk.CellRendererText()
128
        self.column_title = Gtk.TreeViewColumn('Collection', self.renderer_title, markup=2)
129
        self.column_title.set_visible(True)
130
        self.column_title.set_expand(True)
131
        self.column_title.set_clickable(True)
132
        self.column_title
133
        self.column_title.set_sort_indicator(True)
134
        self.model.set_sort_column_id(2, Gtk.SortType.ASCENDING)
135
        self.treeview.append_column(self.column_title)
136
 
137
        # TreeView common
138
        # ~ self.sorted_model = Gtk.TreeModelSort(model=self.model)
139
        # ~ self.sorted_model.set_sort_column_id(2, Gtk.SortType.ASCENDING)
140
        # ~ self.treeview.set_model(self.sorted_model)
141
 
142
        self.treeview.set_can_focus(False)
143
        self.treeview.set_headers_visible(True)
144
        self.treeview.set_enable_search(True)
145
        self.treeview.set_hover_selection(False)
146
        self.treeview.set_grid_lines(Gtk.TreeViewGridLines.HORIZONTAL)
147
        # ~ self.treeview.modify_font(Pango.FontDescription('Monospace 10'))
148
 
149
        # Selection
150
        self.selection = self.treeview.get_selection()
151
        self.selection.set_mode(Gtk.SelectionMode.SINGLE)
152
        self.selection.connect('changed', self.row_changed)
153
 
154
        self.treeview.set_model(self.model)
155
 
156
        self.show_all()
157
 
158
 
159
    def toggle_checkbox(self, cell, path):
160
        self.model[path][1] = not self.model[path][1]
161
 
162
 
163
    def accept(self, button):
164
        db = self.app.get_service('DB')
165
        selected = []
166
        def get_linked_collections(model, path, itr):
167
            tid = model.get(itr, 0)[0]
168
            linked = model.get(itr, 1)[0]
169
            if linked:
170
                selected.append(tid)
171
 
172
        self.model.foreach(get_linked_collections)
173
        if self.sid != '0000000000':
174
            db.set_collections(self.sid, selected)
175
        else:
176
            visor = self.gui.get_widget('visor')
177
            bag = visor.get_bag()
178
            for sid in bag:
179
                db.set_collections(sid, selected)
180
 
181
        self.popover.hide()
182
 
183
 
184
    def row_changed(self, selection):
185
        model, treeiter = selection.get_selected() #_rows()
186
        visor = self.gui.get_widget('visor')
187
        try:
188
            self.current_tid = model[treeiter][0]
189
            title = model[treeiter][2]
190
            self.entry.set_text(title)
191
        except TypeError:
192
            pass
193
        except Exception as error:
236 t00m 194
            self.error("ERROR: collections->row_changed->error: %s" % error)
232 t00m 195
            raise
196
 
197
 
198
    def update(self, entry=None):
199
        db = self.app.get_service('DB')
200
        linked = db.get_collections(self.sid)
201
 
202
        if entry is not None and isinstance(entry, Gtk.Entry):
239 t00m 203
            name = entry.get_text()
204
            self.clts.create(name)
232 t00m 205
 
206
        self.model.clear()
207
        collections = self.clts.get_all()
208
        for tid in collections:
209
            name = collections[tid]
210
            if tid in linked:
211
                self.model.append([tid, 1, name])
212
            else:
213
                self.model.append([tid, 0, name])
214
        self.current_tid = None
215
 
216
 
217
    def delete(self, button):
218
        self.debug("Deleting collection with id: %s" % self.current_tid)
219
        res = self.clts.delete(self.current_tid)
220
        if res:
221
            self.update()