1
|
|
|
import os |
|
|
|
|
2
|
|
|
|
3
|
|
|
from PyQt4.QtGui import QListWidget, QIcon, QSizePolicy |
|
|
|
|
4
|
|
|
|
5
|
|
|
from Orange.widgets import gui |
6
|
|
|
from Orange.widgets.settings import ContextSetting |
7
|
|
|
from Orange.widgets.widget import OWWidget |
8
|
|
|
from Orange.widgets.utils import vartype |
9
|
|
|
|
10
|
|
|
ICON_UP = gui.resource_filename("icons/Dlg_up3.png") |
11
|
|
|
ICON_DOWN = gui.resource_filename("icons/Dlg_down3.png") |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class OWVisWidget(OWWidget): |
15
|
|
|
_shown_attributes = ContextSetting(default=[], required=ContextSetting.REQUIRED, |
16
|
|
|
selected='selected_shown', reservoir="_hidden_attributes") |
17
|
|
|
# Setting above will override these fields |
18
|
|
|
_hidden_attributes = () |
19
|
|
|
selected_shown = () |
20
|
|
|
selected_hidden = () |
21
|
|
|
|
22
|
|
|
@property |
23
|
|
|
def shown_attributes(self): |
24
|
|
|
return [a[0] for a in self._shown_attributes] |
25
|
|
|
|
26
|
|
|
@shown_attributes.setter |
27
|
|
|
def shown_attributes(self, value): |
28
|
|
|
shown = [] |
29
|
|
|
hidden = [] |
30
|
|
|
|
31
|
|
|
domain = self.get_data_domain() |
32
|
|
|
attr_info = lambda a: (a.name, vartype(a)) |
33
|
|
|
if domain: |
34
|
|
|
if value: |
35
|
|
|
shown = value if isinstance(value[0], tuple) else [attr_info(domain[a]) for a in value] |
36
|
|
|
hidden = [x for x in [attr_info(domain[a]) for a in domain.attributes] if x not in shown] |
37
|
|
|
else: |
38
|
|
|
shown = [attr_info(a) for a in domain.attributes] |
39
|
|
|
if not self.show_all_attributes: |
40
|
|
|
hidden = shown[10:] |
41
|
|
|
shown = shown[:10] |
42
|
|
|
|
43
|
|
|
if domain.class_var and attr_info(domain.class_var) not in shown: |
44
|
|
|
hidden += [attr_info(domain.class_var)] |
45
|
|
|
|
46
|
|
|
self._shown_attributes = shown |
47
|
|
|
self._hidden_attributes = hidden |
48
|
|
|
self.selected_hidden = [] |
49
|
|
|
self.selected_shown = [] |
50
|
|
|
|
51
|
|
|
self.trigger_attributes_changed() |
52
|
|
|
|
53
|
|
|
@property |
54
|
|
|
def hidden_attributes(self): |
55
|
|
|
return [a[0] for a in self._hidden_attributes] |
56
|
|
|
|
57
|
|
|
__attribute_selection_area_initialized = False |
58
|
|
|
|
59
|
|
|
#noinspection PyAttributeOutsideInit |
60
|
|
|
def add_attribute_selection_area(self, parent): |
61
|
|
|
self.add_shown_attributes(parent) |
62
|
|
|
self.add_hidden_attributes(parent) |
63
|
|
|
self.__attribute_selection_area_initialized = True |
64
|
|
|
|
65
|
|
|
self.trigger_attributes_changed() |
66
|
|
|
|
67
|
|
|
#noinspection PyAttributeOutsideInit |
68
|
|
|
def add_shown_attributes(self, parent): |
69
|
|
|
self.shown_attributes_area = gui.widgetBox(parent, " Shown attributes ") |
|
|
|
|
70
|
|
|
self.shown_attributes_listbox = gui.listBox( |
|
|
|
|
71
|
|
|
self.shown_attributes_area, self, "selected_shown", "_shown_attributes", |
72
|
|
|
dragDropCallback=self.trigger_attributes_changed, |
73
|
|
|
enableDragDrop=True, selectionMode=QListWidget.ExtendedSelection) |
74
|
|
|
|
75
|
|
|
#noinspection PyAttributeOutsideInit |
76
|
|
|
def add_hidden_attributes(self, parent): |
77
|
|
|
self.hidden_attributes_area = gui.widgetBox(parent, " Hidden attributes ") |
|
|
|
|
78
|
|
|
self.hidden_attributes_listbox = gui.listBox(self.hidden_attributes_area, self, "selected_hidden", |
|
|
|
|
79
|
|
|
"_hidden_attributes", |
80
|
|
|
dragDropCallback=self.trigger_attributes_changed, |
81
|
|
|
enableDragDrop=True, selectionMode=QListWidget.ExtendedSelection) |
82
|
|
|
|
83
|
|
|
def get_data_domain(self): |
84
|
|
|
if hasattr(self, "data") and self.data: |
85
|
|
|
return self.data.domain |
86
|
|
|
else: |
87
|
|
|
return None |
88
|
|
|
|
89
|
|
|
def trigger_attributes_changed(self): |
90
|
|
|
if not self.__attribute_selection_area_initialized: |
91
|
|
|
# Some components trigger this event during the initialization. |
92
|
|
|
# We ignore those requests, a separate event will be triggered |
93
|
|
|
# manually when everything is initialized. |
94
|
|
|
return |
95
|
|
|
|
96
|
|
|
self.attributes_changed() |
97
|
|
|
|
98
|
|
|
def closeContext(self): |
99
|
|
|
super().closeContext() |
100
|
|
|
|
101
|
|
|
self.data = None |
|
|
|
|
102
|
|
|
self.shown_attributes = None |
103
|
|
|
|
104
|
|
|
# "Events" |
105
|
|
|
def attributes_changed(self): |
106
|
|
|
pass |
107
|
|
|
|