widgets.py 7.76 KB
Newer Older
Taylor Hanayik's avatar
Taylor Hanayik committed
1
2
3
4
"""
the widges module provides common widgets that are intended
to be embedded within other FSL guis.  
"""
Taylor Hanayik's avatar
Taylor Hanayik committed
5
import os
6
import subprocess
Taylor Hanayik's avatar
Taylor Hanayik committed
7

Taylor Hanayik's avatar
Taylor Hanayik committed
8
import wx
Taylor Hanayik's avatar
Taylor Hanayik committed
9
import fsleyes
Taylor Hanayik's avatar
Taylor Hanayik committed
10
11
12
13
14
import fsleyes.overlay as fsloverlay
import fsleyes.displaycontext as fsldc
import fsleyes.views.orthopanel as orthopanel
import fsleyes.profiles as profiles
import fsleyes.profiles.profilemap as profilemap
Taylor Hanayik's avatar
Taylor Hanayik committed
15
import fsleyes.colourmaps as colourmaps
16
import fsleyes_props as props
17
from fsleyes.main import embed
Taylor Hanayik's avatar
Taylor Hanayik committed
18

19
20
from fsl.utils.platform import platform as fslplatform
from fsl.utils import idle
Taylor Hanayik's avatar
Taylor Hanayik committed
21
import fsl.data.image as fslimage
22

Taylor Hanayik's avatar
Taylor Hanayik committed
23
import fsl.gui.icons as fslicons
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
import fsl.gui.exceptions as fslerrs
props.initGUI()

class column(wx.Panel):
    """
    a wx.Panel with a vertical sizer
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.SetSizer(wx.BoxSizer(wx.VERTICAL))
        # self.SetBackgroundColour((0, 255, 0))

class row(wx.Panel):
    """
    a wx.Panel with a horizontal sizer
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.SetSizer(wx.BoxSizer(wx.HORIZONTAL))
        # self.SetBackgroundColour((255, 0, 0))

def button(parent, propobj, **kwargs):
    if kwargs.get('label') == None:
        raise fslerrs.MissingRequiredKey("A 'label' key is required")
    if kwargs.get('propName') == None:
        raise fslerrs.MissingRequiredKey("A 'propName' key is required")
    btn = props.build._createButton(parent, getattr(propobj, kwargs['propName']), propobj, parent)
    return btn


def group(parent, label):
    """
    Uses a wx.StaticBoxSizer to group items visually
    """
    panel = wx.Panel(parent)
    bsizer = wx.StaticBoxSizer(wx.VERTICAL, panel, label)
    panel.SetSizer(bsizer)
    return panel

Taylor Hanayik's avatar
Taylor Hanayik committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def notebook(parent):
    """
    noetbook
    """
    notebook = wx.Notebook(parent)
    return notebook

def page(parent):
    """
    page
    """
    panel = wx.Panel(parent)
    sizer = wx.BoxSizer(wx.VERTICAL)
    panel.SetSizer(sizer)
    panel.SetBackgroundColour(wx.NullColour)
    return panel

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
def filepath(parent, propobj, **kwargs):
    if kwargs.get('label') == None:
        raise fslerrs.MissingRequiredKey("A 'label' key must be provided to a FilePath")
    panel = wx.Panel(parent)
    panel.SetSizer(wx.BoxSizer(wx.HORIZONTAL))
    st = wx.StaticText(panel, label=kwargs['label'])
    panel.GetSizer().Add(st, proportion=0, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=2)
    f = props.widgets.makeWidget(panel, propobj, kwargs['propName'])
    panel.GetSizer().Add(f, proportion=1, flag=wx.ALL | wx.EXPAND, border=5)
    return panel


def checkbox(parent, propobj, **kwargs):
    if kwargs.get('label') == None:
        raise fslerrs.MissingRequiredKey("A 'label' key must be provided to a Checkbox")
    panel = wx.Panel(parent)
    panel.SetSizer(wx.BoxSizer(wx.HORIZONTAL))
    st = wx.StaticText(panel, label=kwargs['label'])
    panel.GetSizer().Add(st, proportion=0, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=2)
    ch = props.widgets.makeWidget(panel, propobj, kwargs['propName'])
    panel.GetSizer().Add(ch, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
    return panel

def number(parent, propobj, **kwargs):
    if kwargs.get('label') == None:
        raise fslerrs.MissingRequiredKey("A 'label' key must be provided to a Checkbox")
    panel = wx.Panel(parent)
    panel.SetSizer(wx.BoxSizer(wx.HORIZONTAL))
Taylor Hanayik's avatar
Taylor Hanayik committed
108
    st = wx.StaticText(panel, label=kwargs.pop('label'))
109
    panel.GetSizer().Add(st, proportion=0, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=2)
Taylor Hanayik's avatar
Taylor Hanayik committed
110
    prp = props.widgets.makeWidget(panel, propobj, **kwargs)
Taylor Hanayik's avatar
Taylor Hanayik committed
111
112
113
114
115
116
117
118
119
120
121
    panel.GetSizer().Add(prp, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
    return panel

def point(parent, propobj, **kwargs):
    if kwargs.get('label') == None:
        raise fslerrs.MissingRequiredKey("A 'label' key must be provided to a Checkbox")
    panel = wx.Panel(parent)
    panel.SetSizer(wx.BoxSizer(wx.HORIZONTAL))
    st = wx.StaticText(panel, label=kwargs.pop('label'))
    panel.GetSizer().Add(st, proportion=0, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=2)
    prp = props.widgets.makeWidget(panel, propobj, **kwargs)
122
123
124
125
126
127
128
129
130
131
132
    panel.GetSizer().Add(prp, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
    return panel

def choice(parent, propobj, **kwargs):
    if kwargs.get('label') == None:
        raise fslerrs.MissingRequiredKey("A 'label' key must be provided to a Checkbox")
    panel = wx.Panel(parent)
    panel.SetSizer(wx.BoxSizer(wx.HORIZONTAL))
    st = wx.StaticText(panel, label=kwargs['label'])
    panel.GetSizer().Add(st, proportion=0, flag=wx.ALL | wx.ALIGN_CENTER_VERTICAL, border=2)
    prp = props.widgets.makeWidget(panel, propobj, kwargs['propName'])
Taylor Hanayik's avatar
Taylor Hanayik committed
133
134
135
136
137
138
139
140
    # prp = props.build.buildGUI(
    #     panel,
    #     propobj,
    #     view=props.Widget(
    #         kwargs['propName'],
    #         visibleWhen=lambda i: getattr(i, kwargs['dependsOn'])==kwargs['dependsOnVal']
    #     )
    # )
141
142
143
144
    panel.GetSizer().Add(prp, proportion=0, flag=wx.ALL | wx.EXPAND, border=5)
    return panel


Taylor Hanayik's avatar
Taylor Hanayik committed
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
class ViewProfile(profiles.Profile):
    def __init__(self, viewPanel, overlayList, displayCtx):
        profiles.Profile.__init__(self,
                                  viewPanel,
                                  overlayList,
                                  displayCtx,
                                  ['nav'])

    def getEventTargets(self):
        return [self.viewPanel.getXCanvas(),
                self.viewPanel.getYCanvas(),
                self.viewPanel.getZCanvas()]

    def _navModeLeftMouseDrag(self, ev, canvas, mousePos, canvasPos):
        if canvasPos is None:
            return False
        self.displayCtx.location = canvasPos
        return True


# This is the hacky bit - there is
# currently no formal way to register
# a custom profile class with a view.
# This might change in the future.

# The first profile in the profiles
# list is used as the default.
profilemap.profiles[orthopanel.OrthoPanel].insert(0, 'minview')
profilemap.profileHandlers[orthopanel.OrthoPanel, 'minview'] = ViewProfile

Taylor Hanayik's avatar
Taylor Hanayik committed
175
176
177
class FsleyesImage(wx.Panel):
    def __init__(self, parent, propobj, **kwargs):
        super().__init__(parent, **kwargs)
178
        self.overlayList, masterDisplayCtx, _ = embed(None, mkFrame=False)
Taylor Hanayik's avatar
Taylor Hanayik committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
        self.displayCtx = fsldc.DisplayContext(self.overlayList, parent=masterDisplayCtx)

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.op = orthopanel.OrthoPanel(
                self,
                self.overlayList,
                self.displayCtx,
                None)
        self.op.SetMinSize((-1, 300))
        self.op.Show()

        self.btn_fsleyes = wx.Button(self, label="Open in FSLeyes")
        sizer.Add(self.btn_fsleyes, proportion=0, flag=wx.ALL, border=5)
        sizer.Add(self.op, proportion=1, flag=wx.EXPAND | wx.ALL, border=5)
        self.SetSizer(sizer)

        # bind events
        self.btn_fsleyes.Bind(wx.EVT_BUTTON, self.launch_fsleyes)
Taylor Hanayik's avatar
Taylor Hanayik committed
197
        propobj.addListener('inputFile', 'FsleyesListener', self.add_image)
Taylor Hanayik's avatar
Taylor Hanayik committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    
    def _run_fsleyes(self):
        imgs = [img.dataSource for img in self.overlayList]
        # print([img.dataSource for img in self.overlayList])
        cmd = [
            os.path.join(fslplatform.fsldir, 'bin', 'fsleyes'),
            " ".join(imgs)
        ]
        subprocess.run(
            " ".join(cmd),
            shell=True, 
            check=True
        )


    def launch_fsleyes(self, event):
        thread_id = idle.run(self._run_fsleyes)

    def reset(self):
        self.overlayList.clear()

219
    def add_image(self, new_img, valid=None, context=None, name=None):
Taylor Hanayik's avatar
Taylor Hanayik committed
220
        self.reset()
Taylor Hanayik's avatar
Taylor Hanayik committed
221
222
223
        img = fslimage.Image(new_img)
        self.overlayList.append(img)

224
    def add_mask(self, new_img, valid=None, context=None, name=None):
Taylor Hanayik's avatar
Taylor Hanayik committed
225
226
        img = fslimage.Image(new_img)
        self.overlayList.append(img, cmap='red', alpha=30)