Subversion Repositories basico

Rev

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