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
|
# -*- coding: utf-8 -*-
"""
Copyright (c) 2015, Jairus Martin.
Distributed under the terms of the MIT License.
The full license is in the file COPYING.txt, distributed with this software.
Created on Aug 24, 2015
"""
from atom.api import (
Bool,
Coerced,
Enum,
Event,
ForwardInstance,
Int,
Property,
Str,
Typed,
observe,
)
from enaml.core.declarative import d_
from enaml.icon import Icon
from enaml.layout.geometry import Size
from enaml.widgets.control import Control, ProxyControl
class ProxyAbstractWidgetItemGroup(ProxyControl):
#: Reference to the declaration
declaration = ForwardInstance(lambda: AbstractWidgetItemGroup)
def set_selectable(self, selectable):
pass
class ProxyAbstractWidgetItem(ProxyControl):
#: Reference to the declaration
declaration = ForwardInstance(lambda: AbstractWidgetItem)
def set_row(self, row):
pass
def set_column(self, column):
pass
def set_text(self, text):
pass
def set_text_alignment(self, text_alignment):
pass
def set_icon(self, icon):
pass
def set_icon_size(self, size):
pass
def set_editable(self, editable):
pass
def set_checkable(self, checkable):
pass
class AbstractWidgetItemGroup(Control):
#: Triggered when clicked
clicked = d_(Event(), writable=False)
#: Triggered when double clicked
double_clicked = d_(Event(), writable=False)
#: Triggered when the row, column, or item is entered
entered = d_(Event(), writable=False)
#: Triggered when the row, column, or item is pressed
pressed = d_(Event(), writable=False)
#: Triggered when the row, column, or item's selection changes
selection_changed = d_(Event(bool), writable=False)
def _get_items(self):
return [c for c in self.children if isinstance(c, AbstractWidgetItem)]
#: Internal item reference
_items = Property(lambda self: self._get_items(), cached=True)
def child_added(self, child):
"""Reset the item cache when a child is added"""
super(AbstractWidgetItemGroup, self).child_added(child)
self.get_member("_items").reset(self)
def child_removed(self, child):
"""Reset the item cache when a child is removed"""
super(AbstractWidgetItemGroup, self).child_removed(child)
self.get_member("_items").reset(self)
class AbstractWidgetItem(AbstractWidgetItemGroup):
"""Item to be shared between table views and tree views"""
#: Model index or row within the view
row = d_(Int(), writable=False)
#: Column within the view
column = d_(Int(), writable=False)
#: Text to display within the cell
text = d_(Str())
#: Text alignment within the cell
text_alignment = d_(
Enum(
*[
(h, v)
for h in ("left", "right", "center", "justify")
for v in ("center", "top", "bottom")
]
)
)
#: Icon to display in the cell
icon = d_(Typed(Icon))
#: The size to use for the icon. The default is an invalid size
#: and indicates that an appropriate default should be used.
icon_size = d_(Coerced(Size, (-1, -1)))
#: Whether the item or group can be selected
selectable = d_(Bool(True))
#: Selection state of the item or group
selected = d_(Bool())
#: Whether the item or group can be checked
checkable = d_(Bool())
#: Checked state of the item or group
checked = d_(Bool())
#: Whether the item or group can be edited
editable = d_(Bool())
#: Triggered when the item's contents change
changed = d_(Event(), writable=False)
#: Triggered when the checkbox state changes
toggled = d_(Event(bool), writable=False)
@observe(
"row",
"column",
"text",
"text_alignment",
"icon",
"icon_size",
"selectable",
"selected",
"checkable",
"checked",
"editable",
)
def _update_proxy(self, change):
"""An observer which sends state change to the proxy."""
if change["name"] in ["row", "column"]:
super(AbstractWidgetItem, self)._update_proxy(change)
else:
self.proxy.data_changed(change)
|