-
Notifications
You must be signed in to change notification settings - Fork 400
/
pageview.py
692 lines (605 loc) · 22 KB
/
pageview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2001-2007 Donald N. Allingham
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
"""
Provide the base class for GRAMPS' DataView classes
"""
# ----------------------------------------------------------------
#
# python modules
#
# ----------------------------------------------------------------
from abc import ABCMeta, abstractmethod
import logging
_LOG = logging.getLogger(".pageview")
# ----------------------------------------------------------------
#
# gtk
#
# ----------------------------------------------------------------
from gi.repository import Gtk
from gi.repository import Gdk
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
# ----------------------------------------------------------------
#
# Gramps
#
# ----------------------------------------------------------------
from gramps.gen.errors import WindowActiveError
from ..dbguielement import DbGUIElement
from ..widgets.grampletbar import GrampletBar
from ..configure import ConfigureDialog
from gramps.gen.config import config
from ..uimanager import ActionGroup
# ------------------------------------------------------------------------------
#
# PageView
#
# ------------------------------------------------------------------------------
class PageView(DbGUIElement, metaclass=ABCMeta):
"""
The PageView class is the base class for all Data Views in Gramps. All
Views should derive from this class. The ViewManager understands the public
interface of this class
The following attributes are available
..attribute:: active
is the view active at the moment (visible in Gramps as the main view)
..attribute:: dirty
bool, is the view current with the database or not. A dirty view triggers
a rebuild when it becomes active
..attribute:: _dirty_on_change_inactive
read/write bool by inheriting classes.
Views can behave two ways to signals:
1. if the view is inactive, set it dirty, and rebuild the view when
it becomes active. In this case, this method returns True
2. if the view is inactive, try to stay in sync with database. Only
rebuild or other large changes make view dirty
..attribute:: title
title of the view
..attribute:: dbstate
the dbstate
..attribute:: uistate
the displaystate
..attribute:: category
the category to which the view belongs. Views in the same category are
placed behind the same button in the sidebar
"""
CONFIGSETTINGS = []
def __init__(self, title, pdata, dbstate, uistate):
self.title = title
self.pdata = pdata
self.dbstate = dbstate
self.uistate = uistate
self.action_list = []
self.action_toggle_list = []
self.action_group = None
self.additional_action_groups = []
self.additional_uis = []
self.ui_def = [
"""
<placeholder id="Bars">
<item>
<attribute name="action">win.Sidebar</attribute>
<attribute name="label" translatable="yes">_Sidebar</attribute>
</item>
<item>
<attribute name="action">win.Bottombar</attribute>
<attribute name="label" translatable="yes">_Bottombar</attribute>
</item>
</placeholder>
"""
]
self.dirty = True
self.active = False
self._dirty_on_change_inactive = True
if isinstance(self.pdata.category, tuple):
self.category, self.translated_category = self.pdata.category
else:
raise AttributeError("View category must be (name, translated-name)")
self.ident = self.category + "_" + self.pdata.id
self.dbstate.connect("no-database", self.disable_action_group)
self.dbstate.connect("database-changed", self.enable_action_group)
self.uistate.window.connect("key-press-event", self.key_press_handler)
self.model = None
self.selection = None
self.handle_col = 0
self._config = None
self.init_config()
self.filter_class = None
self.top = None
self.sidebar = None
self.bottombar = None
self.widget = None
self.vpane = None
DbGUIElement.__init__(self, dbstate.db)
def build_interface(self):
"""
Builds the container widget for the interface.
Returns a gtk container widget.
"""
defaults = self.get_default_gramplets()
self.sidebar = GrampletBar(
self.dbstate,
self.uistate,
self,
self.ident + "_sidebar",
defaults[0],
Gtk.Orientation.VERTICAL,
)
self.bottombar = GrampletBar(
self.dbstate,
self.uistate,
self,
self.ident + "_bottombar",
defaults[1],
Gtk.Orientation.HORIZONTAL,
)
hpane = Gtk.Paned()
self.vpane = Gtk.Paned(orientation=Gtk.Orientation.VERTICAL)
hpane.pack1(self.vpane, resize=True, shrink=False)
hpane.pack2(self.sidebar, resize=False, shrink=False)
hpane.show()
self.vpane.show()
self.widget = self.build_widget()
self.widget.show_all()
self.widget.set_name("view")
self.vpane.pack1(self.widget, resize=True, shrink=False)
self.vpane.pack2(self.bottombar, resize=False, shrink=False)
self.vpane.show()
self._setup_slider_config(self.vpane, "vpane.slider-position", position=-1)
self.sidebar_toggled(self.sidebar.get_property("visible"))
self.hpane_sig = hpane.connect("draw", self.set_page_slider)
return hpane
def set_page_slider(self, widget, dummy):
"""Setup slider. We have the page realized at this point."""
widget.disconnect(self.hpane_sig)
# get current width of pane
width = widget.get_allocated_width()
# default will use natural size for sidebar until it gets to 400 pix
side_ch = self.sidebar.get_children() # Gtk Notebook
try:
vp_ch = side_ch[0].get_children() # Gtk Viewport child
ch_width = vp_ch[0].get_preferred_width()[0] + 3
except AttributeError:
ch_width = 300 # needed if no Gramplet installed
pos = width - min(ch_width, 400)
self._setup_slider_config(widget, "hpane.slider-position", position=pos)
def _setup_slider_config(self, widget, setting, position=-1):
"""
Setup the slider configuration setting.
"""
self._config.register(setting, position)
widget.set_position(self._config.get(setting))
widget.connect("notify::position", self._position_changed, setting)
def _position_changed(self, widget, position, setting):
"""
Called when the position property of the pane is changed.
"""
self._config.set(setting, widget.get_position())
def __sidebar_toggled(self, action, value):
"""
Called when the sidebar is toggled.
"""
action.set_state(value) # change GUI
if value.get_boolean():
self.sidebar.show()
self.sidebar_toggled(True)
else:
self.sidebar.hide()
self.sidebar_toggled(False)
def __bottombar_toggled(self, action, value):
"""
Called when the bottombar is toggled.
"""
action.set_state(value) # change GUI
if value.get_boolean():
self.bottombar.show()
else:
self.bottombar.hide()
def sidebar_toggled(self, active):
"""
Called when the sidebar is toggled.
"""
pass
def get_default_gramplets(self):
"""
Get the default gramplets for the Gramps sidebar and bottombar.
Returns a 2-tuple. The first element is a tuple of sidebar gramplets
and the second element is a tuple of bottombar gramplets.
Views should override this method to define default gramplets.
"""
return ((), ())
def key_press_handler(self, widget, event):
"""
A general keypress handler. Override if you want to handle
special control characters, like control+c (copy) or control+v
(paste).
"""
return False
def copy_to_clipboard(self, objclass, handles):
"""
This code is called on Control+C in a navigation view. If the
copy can be handled, it returns true, otherwise false.
The code brings up the Clipboard (if already exists) or
creates it. The copy is handled through the drag and drop
system.
"""
import pickle
from ..clipboard import ClipboardWindow, obj2target
handled = False
for handle in handles:
if handle is None:
continue
clipboard = None
for widget in self.uistate.gwm.window_tree:
if isinstance(widget, ClipboardWindow):
clipboard = widget
if clipboard is None:
clipboard = ClipboardWindow(self.dbstate, self.uistate)
# Construct a drop:
drag_type = obj2target(objclass)
if drag_type:
class Selection:
def __init__(self, data):
self.data = data
def get_data(self):
return self.data
class Context:
targets = [Gdk.atom_intern(drag_type.name(), False)]
action = 1
def list_targets(self):
return Context.targets
def get_actions(self):
return Context.action
# eg: ('person-link', 23767, '27365123671', 0)
data = (drag_type.name(), id(self), handle, 0)
clipboard.object_list.object_drag_data_received(
clipboard.object_list._widget, # widget
Context(), # drag type and action
0,
0, # x, y
Selection(pickle.dumps(data)), # pickled data
None, # info (not used)
-1,
) # time
handled = True
return handled
def call_paste(self):
"""
This code is called on Control+V in a navigation view. If the
copy can be handled, it returns true, otherwise false.
The code creates the Clipboard if it does not already exist.
"""
from ..clipboard import ClipboardWindow
clipboard = None
for widget in self.uistate.gwm.window_tree:
if isinstance(widget, ClipboardWindow):
clipboard = widget
if clipboard is None:
clipboard = ClipboardWindow(self.dbstate, self.uistate)
return True
return False
def post(self):
"""
Called after a page is created.
"""
pass
def set_active(self):
"""
Called with the PageView is set as active. If the page is "dirty",
then we rebuild the data.
"""
self.sidebar.set_active()
self.bottombar.set_active()
self.active = True
new_title = "%s - %s - Gramps" % (
self.dbstate.db.get_dbname(),
self.get_title(),
)
self.uistate.window.set_title(new_title)
if self.dirty:
self.uistate.set_busy_cursor(True)
self.build_tree()
self.uistate.set_busy_cursor(False)
def set_inactive(self):
"""
Marks page as being inactive (not currently displayed)
"""
self.sidebar.set_inactive()
self.bottombar.set_inactive()
self.active = False
def post_create(self):
if self.vpane:
self._setup_slider_config(self.vpane, "vpane.slider-position")
self.vpane = None
@abstractmethod
def build_tree(self):
"""
Rebuilds the current display. This must be overridden by the derived
class.
"""
def ui_definition(self):
"""
returns the XML UI definition for the UIManager
"""
return self.ui_def
def additional_ui_definitions(self):
"""
Return any additional interfaces for the UIManager that the view
needs to define.
"""
return self.additional_uis
def disable_action_group(self):
"""
Turns off the visibility of the View's action group, if defined
"""
if self.action_group:
self.uistate.uimanager.set_actions_visible(self.action_group, False)
def enable_action_group(self, obj):
"""
Turns on the visibility of the View's action group, if defined
"""
if self.action_group:
self.uistate.uimanager.set_actions_visible(self.action_group, True)
def get_stock(self):
"""
Return image associated with the view category, which is used for the
icon for the button.
"""
return "image-missing"
def get_viewtype_stock(self):
"""
Return immage associated with the viewtype inside a view category, it
will be used for the icon on the button to select view in the category
"""
return "image-missing"
def get_title(self):
"""
Return the title of the view. This is used to define the text for the
button, and for the tab label.
"""
return self.title
def get_category(self):
"""
Return the category name of the view. This is used to define
the text for the button, and for the tab label.
"""
return self.category
def get_translated_category(self):
"""
Return the translated category name of the view. This is used
to define the text for the button, and for the tab label.
"""
return self.translated_category
def get_display(self):
"""
Builds the graphical display, returning the top level widget.
"""
if not self.top:
self.top = self.build_interface()
return self.top
@abstractmethod
def build_widget(self):
"""
Builds the container widget for the main view pane. Must be overridden
by the base class. Returns a gtk container widget.
"""
def define_actions(self):
"""
Defines the UIManager actions. Called by the ViewManager to set up the
View. The user typically defines self.action_list and
self.action_toggle_list in this function.
"""
self._add_toggle_action(
"Sidebar",
self.__sidebar_toggled,
"<shift><PRIMARY>R",
self.sidebar.get_property("visible"),
)
self._add_toggle_action(
"Bottombar",
self.__bottombar_toggled,
"<shift><PRIMARY>B",
self.bottombar.get_property("visible"),
)
def __build_action_group(self):
"""
Create an UIManager ActionGroup from the values in self.action_list
and self.action_toggle_list. The user should define these in
self.define_actions
"""
self.action_group = ActionGroup(name=self.title)
if len(self.action_list) > 0:
self.action_group.add_actions(self.action_list)
if len(self.action_toggle_list) > 0:
self.action_group.add_actions(self.action_toggle_list)
def _add_action(self, name, callback=None, accel=None):
"""
Add an action to the action list for the current view.
"""
self.action_list.append((name, callback, accel))
def _add_toggle_action(self, name, callback=None, accel=None, value=False):
"""
Add a toggle action to the action list for the current view.
"""
self.action_toggle_list.append((name, callback, accel, value))
def get_actions(self):
"""
Return the actions that should be used for the view. This includes the
standard action group (which handles the main toolbar), along with
additional action groups.
If the action group is not defined, we build it the first time. This
allows us to delay the intialization until it is really needed.
The ViewManager uses this function to extract the actions to install
into the UIManager.
"""
if not self.action_group:
self.__build_action_group()
return [self.action_group] + self.additional_action_groups
def _add_action_group(self, group):
"""
Allows additional action groups to be added to the view.
"""
self.additional_action_groups.append(group)
def change_page(self):
"""
Called when the page changes.
"""
self.uistate.clear_filter_results()
def on_delete(self):
"""
Method called on shutdown. Data views should put code here
that should be called when quiting the main application.
"""
self.sidebar.on_delete()
self.bottombar.on_delete()
self._config.save()
def init_config(self):
"""
If you need a view with a config, then call this method in the
build_widget or __init__ method. It will set up a config file for the
view, and use CONFIGSETTINGS to set the config defaults.
The config is later accessbile via self._config
So you can do
self._config.get("section.variable1")
self._config.set("section.variable1", value)
self._config.save()
CONFIGSETTINGS should be a list with tuples like
("section.variable1", value)
"""
if self._config:
return
self._config = config.register_manager(self.ident, use_config_path=True)
for section, value in self.CONFIGSETTINGS:
self._config.register(section, value)
self._config.init()
self.config_connect()
def config_connect(self):
"""
Overwrite this method to set connects to the config file to monitor
changes. This method will be called after the ini file is initialized
Eg:
self.config.connect("section.option", self.callback)
"""
pass
def config_callback(self, callback):
"""
Convenience wrappen to create a callback for a config setting
:param callback: a callback function to call.
"""
return lambda arg1, arg2, arg3, arg4: callback()
def can_configure(self):
"""
Inheriting classes should set if the view has a configure window or not
:return: bool
"""
return False
def _get_configure_page_funcs(self):
"""
Return a list of functions that create gtk elements to use in the
notebook pages of the Configure view
:return: list of functions
"""
return []
def configure(self):
"""
Open the configure dialog for the view.
"""
title = _("Configure %(cat)s - %(view)s") % {
"cat": self.get_translated_category(),
"view": self.get_title(),
}
if self.can_configure():
config_funcs = self._get_configure_page_funcs()
else:
config_funcs = []
if self.sidebar:
config_funcs += self.sidebar.get_config_funcs()
if self.bottombar:
config_funcs += self.bottombar.get_config_funcs()
try:
ViewConfigureDialog(
self.uistate,
self.dbstate,
config_funcs,
self,
self._config,
dialogtitle=title,
ident=_("%(cat)s - %(view)s")
% {"cat": self.get_translated_category(), "view": self.get_title()},
)
except WindowActiveError:
return
class ViewConfigureDialog(ConfigureDialog):
"""
All views can have their own configuration dialog
"""
def __init__(
self,
uistate,
dbstate,
configure_page_funcs,
configobj,
configmanager,
dialogtitle=_("Preferences"),
on_close=None,
ident="",
):
self.ident = ident
ConfigureDialog.__init__(
self,
uistate,
dbstate,
configure_page_funcs,
configobj,
configmanager,
dialogtitle=dialogtitle,
on_close=on_close,
)
self.setup_configs("interface.viewconfiguredialog", 420, 500)
def build_menu_names(self, obj):
return (_("Configure %s View") % self.ident, None)
class DummyPage(PageView):
"""
A Dummy page for testing or errors
"""
def __init__(self, title, pdata, dbstate, uistate, msg1="", msg2=""):
self.msg = msg1
self.msg2 = msg2
PageView.__init__(self, title, pdata, dbstate, uistate)
def build_widget(self):
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=1)
# top widget at the top
box.pack_start(
Gtk.Label(
label=_("View %(name)s: %(msg)s")
% {"name": self.title, "msg": self.msg}
),
False,
False,
0,
)
tv = Gtk.TextView()
tb = tv.get_buffer()
tb.insert_at_cursor(self.msg2)
box.pack_start(tv, False, False, 0)
return box
def build_tree(self):
pass