Subversion Repositories basico

Rev

Rev 162 | Rev 166 | 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)
164 t00m 306
        paned.set_position(300)
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)
164 t00m 315
        scr.set_shadow_type(Gtk.ShadowType.IN)
137 t00mlabs 316
        vwp = Gtk.Viewport()
317
        vwp.set_hexpand(True)
318
        viewsbox = self.gui.add_widget('viewsbox', Gtk.Box())
319
        viewsbox.set_hexpand(True)
320
        vwp.add(viewsbox)
321
        scr.add(vwp)
322
        box.add(scr)
323
        paned.add1(box)
82 t00mlabs 324
 
137 t00mlabs 325
        ## Right view
326
        box = self.gui.add_widget('boxVisor', Gtk.Box())
327
        box.set_hexpand(True)
328
        scr = self.gui.add_widget('scrVisor', Gtk.ScrolledWindow())
329
        scr.set_hexpand(True)
330
        scr.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
331
        vwp = Gtk.Viewport()
332
        vwp.set_hexpand(True)
333
        notesbox = self.gui.add_widget('notesbox', Gtk.Box())
334
        notesbox.set_hexpand(True)
335
        vwp.add(notesbox)
336
        scr.add(vwp)
337
        box.add(scr)
338
        paned.add2(box)
142 t00mlabs 339
        mainbox.pack_start(paned, True, True, 0)
82 t00mlabs 340
 
162 t00m 341
        # Statusbar
142 t00mlabs 342
        statusbar = self.gui.add_widget('statusbar', Statusbar(self.controller))
343
        mainbox.pack_start(statusbar, False, False, 0)
344
 
137 t00mlabs 345
        self.add(mainbox)
346
        self.show_all()
82 t00mlabs 347
 
141 t00mlabs 348
        # Finally, activate Workplace
159 t00m 349
        self.gui.add_widget('viewfilter', Gtk.Entry())
141 t00mlabs 350
        self.controller.get_service('Workplace')
82 t00mlabs 351
 
137 t00mlabs 352
 
353
 
74 t00mlabs 354
    def setup_props_window(self):
355
        imgtitle = self.gui.get_widget('imgPropsTitle')
356
        icon = self.im.get_icon('fingerprint', 48, 48)
357
        imgtitle.set_from_pixbuf (icon)
358
        model = Gtk.TreeStore(str, str)
359
        trvprops = self.gui.add_widget('trvprops', Gtk.TreeView())
360
        trvprops.set_model(model)
361
 
362
        # Property
363
        renderer = Gtk.CellRendererText()
364
        renderer.set_alignment(1.0, 0.5)
365
        column = Gtk.TreeViewColumn('Property', renderer, markup=0)
366
        column.set_visible(True)
367
        column.set_expand(False)
368
        column.set_clickable(True)
369
        column.set_sort_indicator(True)
370
        trvprops.append_column(column)
371
 
372
        # Value
373
        renderer = Gtk.CellRendererText()
374
        renderer.set_alignment(0.0, 0.5)
375
        column = Gtk.TreeViewColumn('Value', renderer, markup=1)
376
        column.set_visible(True)
377
        column.set_expand(True)
378
        column.set_clickable(True)
379
        column.set_sort_indicator(True)
380
        trvprops.append_column(column)
381
        trvprops.show_all()
382
 
383
        trvprops.set_headers_visible(True)
384
        trvprops.set_enable_search(True)
385
        trvprops.set_grid_lines(Gtk.TreeViewGridLines.NONE)
386
        trvprops.set_search_column(1)
387
        #~ trvprops.connect('row-activated', self.double_click)
388
        selection = trvprops.get_selection()
389
        selection.set_mode(Gtk.SelectionMode.SINGLE)
390
        #~ trvprops.set_search_entry(self.pname)
391
 
392
        container = self.gui.get_widget('boxProps')
393
        self.gui.swap_widget(container, trvprops)
394
 
395
    def set_notes_manager_pane_position(self, pane, rectangle):
133 t00mlabs 396
        #~ self.controller.debug(type(pane.get_allocation()))
74 t00mlabs 397
        position = rectangle.width/5*3
398
        pane.set_position(position)
133 t00mlabs 399
        self.debug("Pane position: %s" % position)
74 t00mlabs 400
 
401
 
4 t00mlabs 402
    def __completion_func(self, completion, key, iter):
403
        model = completion.get_model()
404
        text = model.get_value(iter, 0)
405
        if key.upper() in text.upper():
406
            return True
407
        return False
408
 
409
 
410
    def setup_combobox_completions(self, key):
411
        model = Gtk.ListStore(str)
412
        search = self.gui.get_widget("stySearchInfo")
413
        completion = Gtk.EntryCompletion()
414
        completion.set_model(model)
415
        completion.set_text_column(0)
416
        completion.set_match_func(self.__completion_func)
417
        search.set_completion(completion)
418
 
74 t00mlabs 419
        stats = self.db.get_stats()
4 t00mlabs 420
 
421
        try:
422
            items = list(stats[key])
423
            items.sort()
424
            for item in items:
425
                model.append([item])
426
        except:
427
            pass
428
 
429
 
430
    def cb_combobox_changed(self, combobox):
431
        model = combobox.get_model()
432
        treeiter = combobox.get_active_iter()
433
        key = model[treeiter][0]
434
        self.gui.set_key('cmbvalue', key)
435
        self.setup_combobox_completions(key)
436
 
437
 
74 t00mlabs 438
    def setup_combobox_search(self):
439
        combobox = self.gui.get_widget('cmbSearchInfo')
4 t00mlabs 440
        model = Gtk.TreeStore(str, str)
441
        combobox.set_model(model)
442
 
443
        ## key
444
        cell = Gtk.CellRendererText()
445
        cell.set_visible(False)
446
        combobox.pack_start(cell, True)
447
        combobox.add_attribute(cell, 'text', 0)
448
 
449
        ## value
450
        cell = Gtk.CellRendererText()
451
        cell.set_alignment(0.0, 0.5)
452
        combobox.pack_start(cell, expand=False)
453
        combobox.add_attribute(cell, 'markup', 1)
454
        combobox.connect('changed', self.cb_combobox_changed)
455
 
101 t00mlabs 456
        model.append(None, ['search', 'Search in all database'])
457
        model.append(None, ['project', 'Filter by project name'])
458
        model.append(None, ['task', 'Filter by task name'])
459
        model.append(None, ['component', 'Filter by component'])
460
        model.append(None, ['category', 'Filter by category'])
461
        model.append(None, ['type', 'Filter by type'])
462
        model.append(None, ['id', 'Filter by Id'])
463
        model.append(None, ['title', 'Filter by title'])
464
        model.append(None, ['priority', 'Filter by priority'])
465
        model.append(None, ['version', 'Filter by version'])
4 t00mlabs 466
        #~ model.append(None, ['released', 'Filter by release date'])
467
 
468
        combobox.set_active(0)
469
 
470
 
471
    def get_services(self):
472
        """Load services to be used in this class
473
        """
474
        self.gui = self.controller.get_service("GUI")
475
        self.app = self.gui.get_app()
74 t00mlabs 476
        self.db = self.controller.get_service("DB")
4 t00mlabs 477
        self.uif = self.controller.get_service("UIF")
478
        self.prefs = self.controller.get_service("Settings")
479
        self.im = self.controller.get_service('IM')
480
        self.cb = self.controller.get_service('Callbacks')
141 t00mlabs 481
        #~ self.workplace = self.controller.get_service('Workplace')
4 t00mlabs 482
 
483
 
484
    def swap_widget(self, container, combobox):
485
        """Shortcut to GUI.swap_widget method
486
        """
487
        self.gui.swap_widget(container, combobox)