Subversion Repositories basico

Rev

Rev 136 | Rev 138 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: window.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Gtk.ApplicationWindow implementation
7
 
8
import os
9
import stat
10
import threading
11
import time
12
import platform
13
 
14
import gi
15
gi.require_version('Gtk', '3.0')
16
gi.require_version('WebKit', '3.0')
17
 
18
from gi.repository import GLib
19
from gi.repository import GObject
20
from gi.repository import Gtk
21
from gi.repository import Gdk
22
from gi.repository import Gio
23
from gi.repository import Pango
24
from gi.repository import WebKit
25
from gi.repository.GdkPixbuf import Pixbuf
26
 
133 t00mlabs 27
from .service import Service
4 t00mlabs 28
 
29
 
30
MONOSPACE_FONT = 'Lucida Console' if platform.system() == 'Windows' else 'monospace'
31
 
133 t00mlabs 32
class GtkAppWindow(Gtk.ApplicationWindow, Service):
4 t00mlabs 33
    def __init__(self, uiapp):
34
        self.setup_controller(uiapp)
35
        self.get_services()
36
        self.gui.add_widget('uiapp', uiapp)
37
        self.gui.set_key('cmbvalue', 'search')
38
        self.settings = {}
39
        self.settings['fullscreen'] = False
40
        self.current_notes = {}
41
 
42
        self.setup_window()
43
        self.setup_widgets()
137 t00mlabs 44
        #~ self.setup_app()
4 t00mlabs 45
 
106 t00mlabs 46
        self.gui.get_service('Workplace')
47
 
4 t00mlabs 48
 
49
    def setup_controller(self, uiapp):
50
        self.uiapp = uiapp
51
        self.controller = uiapp.get_controller()
52
 
53
 
54
    def setup_window(self):
55
        app_title = self.controller.get_app_info('name')
56
        Gtk.Window.__init__(self, title=app_title, application=self.uiapp)
57
        self.gui.add_widget('mainwindow', self)
58
        icon = self.im.get_icon('basico')
59
        self.set_icon(icon)
60
        # FIXME
61
        # From docs: Don’t use this function. It sets the X xlib.Window
62
        # System “class” and “name” hints for a window.
63
        # But I have to do it or it doesn't shows the right title. ???
64
        self.set_wmclass (app_title, app_title)
65
        self.set_role(app_title)
66
        self.set_default_size(1024, 728)
67
        self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
93 t00mlabs 68
 
69
        # HEADERBAR WIDGETS
70
        self.setup_headerbar()
4 t00mlabs 71
        self.show_all()
72
 
73
 
77 t00mlabs 74
    def setup_headerbar(self):
75
        #~# Header Bar
76
        hb = self.gui.add_widget("HeaderBar", Gtk.HeaderBar())
77
        hb.set_show_close_button(True)
78
        hb.props.title = "Basico"
79 t00mlabs 79
        hb.props.subtitle = "SAP Notes Manager for SAP Consultants"
77 t00mlabs 80
 
79 t00mlabs 81
        lhbox = Gtk.HBox() # Left side of title bar
82
        mhbox = Gtk.HBox() # Mid side of title bar
83
        rhbox = Gtk.HBox() # Right side of title bar
77 t00mlabs 84
 
79 t00mlabs 85
        # Left side titlebar
131 t00mlabs 86
        button = self.gui.add_widget('tgbShowMenuView', Gtk.ToggleButton())
87
        icon = self.im.get_image_icon('gtk-select-color')
88
        button.add(icon)
89
        lhbox.pack_start(button, False, False, 3)
90
 
130 t00mlabs 91
        menuviews = self.gui.add_widget('mnuBtnViews', Gtk.MenuButton())
92
        hbox = Gtk.HBox()
93
        label = self.gui.add_widget('lblViewCurrent', Gtk.Label())
94
        image = self.gui.add_widget('imgViewCurrent', Gtk.Image())
95
        hbox.pack_start(image, False, False, 3)
96
        hbox.pack_start(label, False, False, 3)
97
        menuviews.add(hbox)
98
        lhbox.pack_start(menuviews, False, False, 3)
99
 
81 t00mlabs 100
        imgtitlebar = self.gui.add_widget('imgtitlebar', Gtk.Image())
101 t00mlabs 101
        imgtitlebar.set_visible(False)
102
        imgtitlebar.set_no_show_all(True)
79 t00mlabs 103
        lbltitlebar = self.gui.add_widget("lbltitlebar", Gtk.Label())
130 t00mlabs 104
        lbltitlebar.set_visible(False)
105
        lbltitlebar.set_no_show_all(True)
81 t00mlabs 106
        lhbox.pack_start(imgtitlebar, False, False, 3)
107
        lhbox.pack_start(lbltitlebar, False, False, 3)
108
        lhbox.show_all()
79 t00mlabs 109
        hb.pack_start(lhbox)
77 t00mlabs 110
 
79 t00mlabs 111
        # Middle side titlebar
130 t00mlabs 112
        label = self.gui.add_widget('lblSelectedNotes', Gtk.Label())
113
        #~ label.set_markup('<b><small>0</small>/0 of <big>%d</big></b>' % self.db.get_total())
114
        button = self.gui.add_widget('mnuBtnActions', Gtk.MenuButton())
115
        button.set_relief(Gtk.ReliefStyle.NONE)
116
        button.set_no_show_all(False)
117
        button.set_visible(False)
118
        button.add(label)
119
        mhbox.pack_start(button, False, False, 0)
120
 
78 t00mlabs 121
        # Right side titlebar
130 t00mlabs 122
        # MnuButton valid with any modern version of Gtk (?> 3.10)
123
        btnactions = self.gui.add_widget('mnuBtnImport', Gtk.MenuButton())
124
        btnactions.set_always_show_image(True)
125
        btnactions.set_property("use-popover", True)
126
        btnactions.set_relief(Gtk.ReliefStyle.NONE)
127
        imgwpimport = Gtk.Image()
128
        icon = self.im.get_pixbuf_icon('download', 22, 22)
129
        imgwpimport.set_from_pixbuf(icon)
130
        btnactions.add(imgwpimport)
131
        #~ actions_menu = self.gui.get_widget('actions_menu')
132
        #~ btnactions.set_menu_model(actions_menu)
133
        rhbox.pack_start(btnactions, False, False, 0)
134
 
122 t00mlabs 135
        # Menu
77 t00mlabs 136
        button = Gtk.Button()
122 t00mlabs 137
        #~ button.set_tooltip_markup('<big><b>About</b></big>')
138
        icon = self.im.get_pixbuf_icon('menu', 24, 24)
81 t00mlabs 139
        image = Gtk.Image()
140
        image.set_from_pixbuf(icon)
141
        button.set_image(image)
77 t00mlabs 142
        button.set_relief(Gtk.ReliefStyle.NONE)
122 t00mlabs 143
        popover = Gtk.Popover.new(button)
144
        button.connect('clicked', self.cb.show_popover_menu, popover)
145
        #~ button.connect('clicked', self.uiapp.cb_show_about)
77 t00mlabs 146
        rhbox.pack_end(button, False, False, 0)
79 t00mlabs 147
 
122 t00mlabs 148
        # Popover body
149
        box = Gtk.Box(spacing = 0, orientation="vertical")
150
 
151
        # About
152
        hbox = Gtk.Box(spacing = 0, orientation="horizontal")
153
        icon = self.im.get_pixbuf_icon('about', 24, 24)
154
        image = Gtk.Image()
155
        image.set_from_pixbuf(icon)
156
        label = Gtk.Label("About")
157
        hbox.pack_start(image, False, False, 3)
158
        hbox.pack_start(label, False, False, 3)
159
        button = Gtk.Button()
160
        button.add(hbox)
161
        button.set_relief(Gtk.ReliefStyle.NONE)
136 t00mlabs 162
        button.connect('clicked', self.cb.show_about)
122 t00mlabs 163
        box.pack_end(button, False, False, 0)
164
 
93 t00mlabs 165
        # Help
122 t00mlabs 166
        hbox = Gtk.Box(spacing = 0, orientation="horizontal")
93 t00mlabs 167
        icon = self.im.get_pixbuf_icon('basico_help', 24, 24)
168
        image = Gtk.Image()
169
        image.set_from_pixbuf(icon)
122 t00mlabs 170
        label = Gtk.Label("Help")
171
        hbox.pack_start(image, False, False, 3)
172
        hbox.pack_start(label, False, False, 3)
173
        button = Gtk.Button()
174
        button.add(hbox)
93 t00mlabs 175
        button.set_relief(Gtk.ReliefStyle.NONE)
176
        button.connect('clicked', self.cb.show_help)
122 t00mlabs 177
        box.pack_end(button, False, False, 0)
93 t00mlabs 178
 
79 t00mlabs 179
        # Settings
122 t00mlabs 180
        hbox = Gtk.Box(spacing = 0, orientation="horizontal")
81 t00mlabs 181
        icon = self.im.get_pixbuf_icon('settings', 24, 24)
182
        image = Gtk.Image()
183
        image.set_from_pixbuf(icon)
122 t00mlabs 184
        label = Gtk.Label("Settings")
185
        hbox.pack_start(image, False, False, 3)
186
        hbox.pack_start(label, False, False, 3)
187
        button = Gtk.Button()
188
        button.add(hbox)
79 t00mlabs 189
        button.set_relief(Gtk.ReliefStyle.NONE)
122 t00mlabs 190
        button.connect('clicked', self.cb.show_settings)
130 t00mlabs 191
        box.pack_start(button, False, False, 0)
122 t00mlabs 192
        popover.add(box)
77 t00mlabs 193
        hb.pack_end(rhbox)
79 t00mlabs 194
        hb.set_custom_title(mhbox)
77 t00mlabs 195
        self.set_titlebar(hb)
196
        hb.show_all()
197
 
198
 
84 t00mlabs 199
 
110 t00mlabs 200
    def setup_popover_settings(self, widget):
201
        #Creating a popover
202
        popover = Gtk.Popover.new(widget)
203
        #~ popover.set_size_request(50,100)
204
 
205
        box = Gtk.Box(spacing = 10, orientation="vertical")
112 t00mlabs 206
        hboxfs = Gtk.Box(spacing = 10, orientation="horizontal")
207
        lblfs = Gtk.Label('Fullscreen')
208
        swFullScreen = self.gui.add_widget('fullscreen', Gtk.Switch())
209
        swFullScreen.connect('state-set', self.uif.fullscreen)
210
        hboxfs.pack_start(lblfs, True, True, 6)
211
        hboxfs.pack_start(swFullScreen, True, True, 6)
212
        hboxfs.set_homogeneous(True)
213
        box.add(hboxfs)
110 t00mlabs 214
        popover.add(box)
215
 
216
        return popover
217
 
218
 
4 t00mlabs 219
    def setup_menus(self):
106 t00mlabs 220
        pass
4 t00mlabs 221
 
74 t00mlabs 222
 
4 t00mlabs 223
    def show_home_page(self, *args):
224
        notebook = self.gui.get_widget('notebook')
225
        notebook.set_current_page(0)
226
 
74 t00mlabs 227
 
4 t00mlabs 228
    def show_settings_page(self, *args):
101 t00mlabs 229
        sapnoteview = self.gui.get_widget('viewmenu')
4 t00mlabs 230
        sapnoteview.set_view('settings')
83 t00mlabs 231
        notebook = self.gui.get_widget('mainbox')
232
        notebook.set_current_page(5)
4 t00mlabs 233
 
74 t00mlabs 234
 
235
    def show_stats_page(self, *args):
101 t00mlabs 236
        sapnoteview = self.gui.get_widget('viewmenu')
74 t00mlabs 237
        sapnoteview.set_view('stats')
238
        notebook = self.gui.get_widget('notebook')
239
        notebook.set_current_page(4)
240
 
241
 
4 t00mlabs 242
    def show_browser_page(self, *args):
243
        notebook = self.gui.get_widget('notebook')
244
        notebook.set_current_page(1)
245
 
246
 
74 t00mlabs 247
    def hide_props_window(self):
248
        hpnnotes = self.gui.get_widget('hpnNotesManager')
249
        boxprops = self.gui.get_widget('boxPropsWidget')
250
        size = hpnnotes.get_allocation()
4 t00mlabs 251
 
74 t00mlabs 252
        boxprops.set_no_show_all(True)
253
        boxprops.hide()
254
        hpnnotes.set_position(size.width)
4 t00mlabs 255
 
74 t00mlabs 256
 
257
    def show_props_window(self):
258
        hpnnotes = self.gui.get_widget('hpnNotesManager')
259
        boxprops = self.gui.get_widget('boxPropsWidget')
260
        size = hpnnotes.get_allocation()
261
 
262
        boxprops.set_no_show_all(False)
263
        boxprops.show_all()
264
        hpnnotes.show_all()
265
        hpnnotes.set_position(size.width/5*3)
266
 
267
 
268
    def switch_props_window(self, *args):
269
        tgbprops = self.gui.get_widget('tgbShowManage')
270
 
271
        if tgbprops.get_active():
272
            self.show_props_window()
273
        else:
274
            self.hide_props_window()
275
 
276
 
4 t00mlabs 277
    def setup_widgets(self):
137 t00mlabs 278
        #~ self.mainbox = self.gui.get_widget('mainbox')
279
        #~ self.mainbox.reparent(self)
4 t00mlabs 280
 
137 t00mlabs 281
        # Mainbox
282
        mainbox = self.gui.add_widget('mainbox', Gtk.Box())
283
        mainbox.set_hexpand(True)
284
        paned = self.gui.add_widget('paned', Gtk.HPaned())
285
        paned.set_wide_handle(True)
286
        paned.set_position(250)
82 t00mlabs 287
 
137 t00mlabs 288
        # Paned
289
        ## Left view
290
        box = self.gui.add_widget('boxMenuView', Gtk.Box())
291
        box.set_hexpand(True)
292
        scr = Gtk.ScrolledWindow()
293
        #~ scr.set_min_content_width(250)
294
        scr.set_propagate_natural_width(True)
295
        scr.set_hexpand(True)
296
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
297
        vwp = Gtk.Viewport()
298
        vwp.set_hexpand(True)
299
        viewsbox = self.gui.add_widget('viewsbox', Gtk.Box())
300
        viewsbox.set_hexpand(True)
301
        vwp.add(viewsbox)
302
        scr.add(vwp)
303
        box.add(scr)
304
        paned.add1(box)
82 t00mlabs 305
 
137 t00mlabs 306
        ## Right view
307
        box = self.gui.add_widget('boxVisor', Gtk.Box())
308
        box.set_hexpand(True)
309
        scr = self.gui.add_widget('scrVisor', Gtk.ScrolledWindow())
310
        scr.set_hexpand(True)
311
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
312
        vwp = Gtk.Viewport()
313
        vwp.set_hexpand(True)
314
        notesbox = self.gui.add_widget('notesbox', Gtk.Box())
315
        notesbox.set_hexpand(True)
316
        vwp.add(notesbox)
317
        scr.add(vwp)
318
        box.add(scr)
319
        paned.add2(box)
82 t00mlabs 320
 
137 t00mlabs 321
        mainbox.add(paned)
322
        self.add(mainbox)
323
        self.show_all()
82 t00mlabs 324
 
137 t00mlabs 325
        # SETUP MENUS
326
        #~ self.setup_menus()
82 t00mlabs 327
 
137 t00mlabs 328
 
82 t00mlabs 329
        # SAPNOTEVIEW WIDGETS
137 t00mlabs 330
        #~ self.gui.add_widget('combobox')
331
        #~ search = self.gui.add_widget('stySearchInfo')
332
        #~ search.connect('activate', self.cb.search_notes)
333
        #~ self.setup_combobox_search()
334
 
133 t00mlabs 335
        #~ self.debug("GUI loaded")
137 t00mlabs 336
        #~ self.cb.show_workplace()
4 t00mlabs 337
 
84 t00mlabs 338
 
74 t00mlabs 339
    def setup_props_window(self):
340
        imgtitle = self.gui.get_widget('imgPropsTitle')
341
        icon = self.im.get_icon('fingerprint', 48, 48)
342
        imgtitle.set_from_pixbuf (icon)
343
        model = Gtk.TreeStore(str, str)
344
        trvprops = self.gui.add_widget('trvprops', Gtk.TreeView())
345
        trvprops.set_model(model)
346
 
347
        # Property
348
        renderer = Gtk.CellRendererText()
349
        renderer.set_alignment(1.0, 0.5)
350
        column = Gtk.TreeViewColumn('Property', renderer, markup=0)
351
        column.set_visible(True)
352
        column.set_expand(False)
353
        column.set_clickable(True)
354
        column.set_sort_indicator(True)
355
        trvprops.append_column(column)
356
 
357
        # Value
358
        renderer = Gtk.CellRendererText()
359
        renderer.set_alignment(0.0, 0.5)
360
        column = Gtk.TreeViewColumn('Value', renderer, markup=1)
361
        column.set_visible(True)
362
        column.set_expand(True)
363
        column.set_clickable(True)
364
        column.set_sort_indicator(True)
365
        trvprops.append_column(column)
366
        trvprops.show_all()
367
 
368
        trvprops.set_headers_visible(True)
369
        trvprops.set_enable_search(True)
370
        trvprops.set_grid_lines(Gtk.TreeViewGridLines.NONE)
371
        trvprops.set_search_column(1)
372
        #~ trvprops.connect('row-activated', self.double_click)
373
        selection = trvprops.get_selection()
374
        selection.set_mode(Gtk.SelectionMode.SINGLE)
375
        #~ trvprops.set_search_entry(self.pname)
376
 
377
        container = self.gui.get_widget('boxProps')
378
        self.gui.swap_widget(container, trvprops)
379
 
380
    def set_notes_manager_pane_position(self, pane, rectangle):
133 t00mlabs 381
        #~ self.controller.debug(type(pane.get_allocation()))
74 t00mlabs 382
        position = rectangle.width/5*3
383
        pane.set_position(position)
133 t00mlabs 384
        self.debug("Pane position: %s" % position)
74 t00mlabs 385
 
386
 
4 t00mlabs 387
    def __completion_func(self, completion, key, iter):
388
        model = completion.get_model()
389
        text = model.get_value(iter, 0)
390
        if key.upper() in text.upper():
391
            return True
392
        return False
393
 
394
 
395
    def setup_combobox_completions(self, key):
396
        model = Gtk.ListStore(str)
397
        search = self.gui.get_widget("stySearchInfo")
398
        completion = Gtk.EntryCompletion()
399
        completion.set_model(model)
400
        completion.set_text_column(0)
401
        completion.set_match_func(self.__completion_func)
402
        search.set_completion(completion)
403
 
74 t00mlabs 404
        stats = self.db.get_stats()
4 t00mlabs 405
 
406
        try:
407
            items = list(stats[key])
408
            items.sort()
409
            for item in items:
410
                model.append([item])
411
        except:
412
            pass
413
 
414
 
415
    def cb_combobox_changed(self, combobox):
416
        model = combobox.get_model()
417
        treeiter = combobox.get_active_iter()
418
        key = model[treeiter][0]
419
        self.gui.set_key('cmbvalue', key)
420
        self.setup_combobox_completions(key)
421
 
422
 
74 t00mlabs 423
    def setup_combobox_search(self):
424
        combobox = self.gui.get_widget('cmbSearchInfo')
4 t00mlabs 425
        model = Gtk.TreeStore(str, str)
426
        combobox.set_model(model)
427
 
428
        ## key
429
        cell = Gtk.CellRendererText()
430
        cell.set_visible(False)
431
        combobox.pack_start(cell, True)
432
        combobox.add_attribute(cell, 'text', 0)
433
 
434
        ## value
435
        cell = Gtk.CellRendererText()
436
        cell.set_alignment(0.0, 0.5)
437
        combobox.pack_start(cell, expand=False)
438
        combobox.add_attribute(cell, 'markup', 1)
439
        combobox.connect('changed', self.cb_combobox_changed)
440
 
101 t00mlabs 441
        model.append(None, ['search', 'Search in all database'])
442
        model.append(None, ['project', 'Filter by project name'])
443
        model.append(None, ['task', 'Filter by task name'])
444
        model.append(None, ['component', 'Filter by component'])
445
        model.append(None, ['category', 'Filter by category'])
446
        model.append(None, ['type', 'Filter by type'])
447
        model.append(None, ['id', 'Filter by Id'])
448
        model.append(None, ['title', 'Filter by title'])
449
        model.append(None, ['priority', 'Filter by priority'])
450
        model.append(None, ['version', 'Filter by version'])
4 t00mlabs 451
        #~ model.append(None, ['released', 'Filter by release date'])
452
 
453
        combobox.set_active(0)
454
 
455
 
456
    def get_services(self):
457
        """Load services to be used in this class
458
        """
459
        self.gui = self.controller.get_service("GUI")
460
        self.app = self.gui.get_app()
74 t00mlabs 461
        self.db = self.controller.get_service("DB")
4 t00mlabs 462
        self.uif = self.controller.get_service("UIF")
463
        self.prefs = self.controller.get_service("Settings")
464
        self.im = self.controller.get_service('IM')
465
        self.cb = self.controller.get_service('Callbacks')
74 t00mlabs 466
        self.tasks = self.controller.get_service('Tasks')
4 t00mlabs 467
 
468
 
469
    def swap_widget(self, container, combobox):
470
        """Shortcut to GUI.swap_widget method
471
        """
472
        self.gui.swap_widget(container, combobox)