Subversion Repositories basico

Rev

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