| 1 |
""" |
|---|
| 2 |
Toolbox. |
|---|
| 3 |
""" |
|---|
| 4 |
|
|---|
| 5 |
import gobject |
|---|
| 6 |
import gtk |
|---|
| 7 |
|
|---|
| 8 |
from gaphor.core import inject |
|---|
| 9 |
|
|---|
| 10 |
from wrapbox import Wrapbox |
|---|
| 11 |
|
|---|
| 12 |
class Toolbox(gtk.VBox): |
|---|
| 13 |
""" |
|---|
| 14 |
A toolbox is a widget that contains a set of buttons (a Wrapbox widget) |
|---|
| 15 |
with a name above it. When the user clicks on the name the box's content |
|---|
| 16 |
shows/hides. |
|---|
| 17 |
|
|---|
| 18 |
The 'toggled' signal is emited everytime a box shows/hides. |
|---|
| 19 |
|
|---|
| 20 |
The toolbox is generated based on a definition with the form: |
|---|
| 21 |
('name', ('boxAction1', 'boxAction2',...), 'name2', ('BoxActionN',...)) |
|---|
| 22 |
|
|---|
| 23 |
1 Create action pool for placement actions |
|---|
| 24 |
2 Create gtk.RadioButtons for each item. |
|---|
| 25 |
3 connect to action |
|---|
| 26 |
""" |
|---|
| 27 |
|
|---|
| 28 |
__gsignals__ = { |
|---|
| 29 |
'toggled': (gobject.SIGNAL_RUN_FIRST, |
|---|
| 30 |
gobject.TYPE_NONE, (gobject.TYPE_STRING, gobject.TYPE_INT)) |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
properties = inject('properties') |
|---|
| 34 |
|
|---|
| 35 |
def __init__(self, toolboxdef): |
|---|
| 36 |
""" |
|---|
| 37 |
Create a new Toolbox instance. Wrapbox objects are generated |
|---|
| 38 |
using the menu_factory and based on the toolboxdef definition. |
|---|
| 39 |
""" |
|---|
| 40 |
self.__gobject_init__() |
|---|
| 41 |
self.toolboxdef = toolboxdef |
|---|
| 42 |
|
|---|
| 43 |
self.buttons = [] |
|---|
| 44 |
self._construct() |
|---|
| 45 |
|
|---|
| 46 |
def on_wrapbox_decorator_toggled(self, button, content): |
|---|
| 47 |
""" |
|---|
| 48 |
This function is called when the Wrapbox decorator is clicked. It |
|---|
| 49 |
changes the visibility of the content and the arrow in front of the |
|---|
| 50 |
button label. |
|---|
| 51 |
""" |
|---|
| 52 |
|
|---|
| 53 |
arrow = button.get_children()[0].get_children()[0] |
|---|
| 54 |
if not content.get_property('visible'): |
|---|
| 55 |
content.show() |
|---|
| 56 |
arrow.set(gtk.ARROW_DOWN, gtk.SHADOW_IN) |
|---|
| 57 |
self.emit('toggled', button.toggle_id, True) |
|---|
| 58 |
else: |
|---|
| 59 |
content.hide() |
|---|
| 60 |
arrow.set(gtk.ARROW_RIGHT, gtk.SHADOW_IN) |
|---|
| 61 |
self.emit('toggled', button.toggle_id, False) |
|---|
| 62 |
|
|---|
| 63 |
self.properties.set('ui.toolbox.%s' % button.toggle_id, |
|---|
| 64 |
content.get_property('visible')) |
|---|
| 65 |
|
|---|
| 66 |
def make_wrapbox_decorator(self, title, content): |
|---|
| 67 |
""" |
|---|
| 68 |
Create a gtk.VBox with in the top compartment a label that can be |
|---|
| 69 |
clicked to show/hide the lower compartment. |
|---|
| 70 |
""" |
|---|
| 71 |
vbox = gtk.VBox() |
|---|
| 72 |
|
|---|
| 73 |
button = gtk.Button() |
|---|
| 74 |
button.set_relief(gtk.RELIEF_NONE) |
|---|
| 75 |
button.toggle_id = title.replace(' ', '-').lower() |
|---|
| 76 |
hbox = gtk.HBox() |
|---|
| 77 |
button.add(hbox) |
|---|
| 78 |
|
|---|
| 79 |
arrow = gtk.Arrow(gtk.ARROW_RIGHT, gtk.SHADOW_IN) |
|---|
| 80 |
hbox.pack_start(arrow, False, False, 0) |
|---|
| 81 |
|
|---|
| 82 |
label = gtk.Label(title) |
|---|
| 83 |
hbox.pack_start(label, expand=False, fill=False) |
|---|
| 84 |
|
|---|
| 85 |
sep = gtk.HSeparator() |
|---|
| 86 |
hbox.pack_start(sep, expand=True, fill=True) |
|---|
| 87 |
hbox.set_spacing(3) |
|---|
| 88 |
|
|---|
| 89 |
vbox.pack_start(button, False, False, 1) |
|---|
| 90 |
vbox.pack_start(content, False, False, 1) |
|---|
| 91 |
|
|---|
| 92 |
vbox.show_all() |
|---|
| 93 |
|
|---|
| 94 |
button.connect('clicked', self.on_wrapbox_decorator_toggled, content) |
|---|
| 95 |
|
|---|
| 96 |
vbox.label = label |
|---|
| 97 |
vbox.content = content |
|---|
| 98 |
|
|---|
| 99 |
expanded = self.properties.get('ui.toolbox.%s' % button.toggle_id, False) |
|---|
| 100 |
content.set_property('visible', not expanded) |
|---|
| 101 |
self.on_wrapbox_decorator_toggled(button, content) |
|---|
| 102 |
|
|---|
| 103 |
return vbox |
|---|
| 104 |
|
|---|
| 105 |
def toolbox_button(self, action_name, stock_id, |
|---|
| 106 |
icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR): |
|---|
| 107 |
button = gtk.ToggleButton() |
|---|
| 108 |
if stock_id: |
|---|
| 109 |
icon = gtk.Image() |
|---|
| 110 |
icon.set_from_stock(stock_id, icon_size) |
|---|
| 111 |
button.add(icon) |
|---|
| 112 |
icon.show() |
|---|
| 113 |
else: |
|---|
| 114 |
button.props.label = action_name |
|---|
| 115 |
button.action_name = action_name |
|---|
| 116 |
return button |
|---|
| 117 |
|
|---|
| 118 |
def _construct(self): |
|---|
| 119 |
|
|---|
| 120 |
self.set_border_width(3) |
|---|
| 121 |
|
|---|
| 122 |
self.tooltips = gtk.Tooltips() |
|---|
| 123 |
|
|---|
| 124 |
for title, items in self.toolboxdef: |
|---|
| 125 |
wrapbox = Wrapbox() |
|---|
| 126 |
action = None |
|---|
| 127 |
for action_name, label, stock_id in items: |
|---|
| 128 |
button = self.toolbox_button(action_name, stock_id) |
|---|
| 129 |
if label: |
|---|
| 130 |
self.tooltips.set_tip(button, label) |
|---|
| 131 |
self.buttons.append(button) |
|---|
| 132 |
wrapbox.add(button) |
|---|
| 133 |
button.show() |
|---|
| 134 |
if title: |
|---|
| 135 |
wrapbox_dec = self.make_wrapbox_decorator(title, wrapbox) |
|---|
| 136 |
self.pack_start(wrapbox_dec, expand=False) |
|---|
| 137 |
else: |
|---|
| 138 |
self.pack_start(wrapbox, expand=False) |
|---|
| 139 |
wrapbox.show() |
|---|
| 140 |
|
|---|
| 141 |
self.tooltips.enable() |
|---|
| 142 |
|
|---|
| 143 |
|
|---|