Exemple #1
0
    def __onProject(self, ev=None):
        """Called when an item in the the *Project* drop down box is selected.
        Clears the browser tree, and creates a new root node for the newly
        selected project.
        """

        project = self.__project.GetSelection()

        if project == wx.NOT_FOUND:
            return

        project = self.__project.GetString(project).strip()
        label   = LABELS['project']

        self.__browser.DeleteAllItems()

        # For each element in the tree, the xnat
        # object, and the name of its level in
        # the XNAT hierarchy (e.g 'project',
        # 'experiment') is stored by the tree
        # browser.
        data = [self.__session.projects[project], 'project']

        if fw.wxversion() == fw.WX_PYTHON:
            data = wx.TreeItemData(data)

        root = self.__browser.AddRoot(
            '{} {}'.format(label, project),
            data=data,
            image=self.__unloadedFolderImageId)

        self.__onTreeSelect(item=root)
Exemple #2
0
    def __init__(self, parent, size=None, colour=None):
        """Create a ``ColourButton``.

        :arg parent: A :mod:`wx` parent window.

        :arg size:   A tuple containing the ``(width, height)`` of the
                     colour bitmap in pixels. Defaults to ``(32, 32)``.

        :arg colour: Initial colour. Defaults to black.
        """

        if size is None: size = (32, 32)
        if colour is None: colour = (0, 0, 0, 255)

        style = wx.BU_EXACTFIT | wx.BU_NOTEXT

        wx.Button.__init__(self, parent, style=style)

        # Under wxPython-phoenix, setting
        # label='' results in "Button".
        if fw.wxversion() == fw.WX_PHOENIX:
            self.SetLabel(' ')

        self.__size = size
        self.__bmp = None

        self.Bind(wx.EVT_BUTTON, self.__onClick)

        self.SetValue(colour)
Exemple #3
0
def getTreeData(tree, item):
    """Returns the data in the given ``wx.TreeCtrl`` associated with the
    given ``item``. This is done differently depending on whether wxPython
    or wxPhoenix is used.
    """
    data = tree.GetItemData(item)
    if fw.wxversion() == fw.WX_PHOENIX: return data
    else:                               return data.GetData()
Exemple #4
0
    def __expandTreeItem(self, obj, level, treeItem):
        """Expands the contents of the given ``treeItem`` in the tree browser.
        For each child level of the item's level in the XNAT hierarchy, any
        child objects are retrieved from the XNAT repository and added as items
        in the tree browser.

        :arg obj:      XNAT object

        :arg level:    Level of ``obj`` in the XNAT hierarchy

        :arg treeItem: ``wx.TreeItemId`` corresponding to ``obj``

        :returns:      A mapping of the form: ``{ xnat_id : (xnat_obj,
                       wx.TreeItemId) }``, containing the newly created
                       ``wx.TreeItemId`` objects orresponding to the
                       children of ``obj``.
        """

        childItems = {}

        if level != 'file':
            self.__browser.SetItemImage(treeItem, self.__loadedFolderImageId)

        for catt in XNAT_HIERARCHY[level]:

            children = getattr(obj, catt, None)
            catt     = catt[:-1]

            if children is None:
                continue

            for child in children.listing:

                name  = getattr(child, XNAT_NAME_ATT[catt])
                label = LABELS[catt]

                if self.__filterItem(catt, name):
                    continue

                if catt == 'file': image = self.__fileImageId
                else:              image = self.__unloadedFolderImageId

                data = [child, catt]

                if fw.wxversion() == fw.WX_PYTHON:
                    data = wx.TreeItemData(data)

                childItem = self.__browser.AppendItem(
                    treeItem,
                    '{} {}'.format(label, name),
                    image=image,
                    data=data)

                childItems[child.id] = (child, childItem)

        return childItems
Exemple #5
0
    def cbarUpdate(*a):
        w, h = cbpanel.GetSize().Get()

        if w < 20 or h < 20:
            return

        bmp = colourbar.colourBar(w, h)

        if bmp is None:
            return

        if fwidgets.wxversion() == fwidgets.WX_PHOENIX:
            bmp = wx.Bitmap.FromBufferRGBA(w, h, bmp.transpose(1, 0, 2))
        else:
            bmp = wx.BitmapFromBufferRGBA(w, h, bmp.transpose(1, 0, 2))

        cbpanel.SetImage(bmp.ConvertToImage())
Exemple #6
0
    def __updateBitmap(self, colour):
        """Called when the colour is changed. Updates the bitmap shown
        on the button.
        """

        import numpy as np

        w, h = self.__size
        data = np.zeros((w, h, 4), dtype=np.uint8)

        data[:, :] = colour

        if fw.wxversion() == fw.WX_PHOENIX:
            self.__bmp = wx.Bitmap.FromBufferRGBA(w, h, data)
        else:
            self.__bmp = wx.BitmapFromBufferRGBA(w, h, data)

        self.SetBitmapLabel(self.__bmp)
Exemple #7
0
def loadBitmap(iconb64):
    """Convert the given ``base64``-encoded byte string to a ``wx.Bitmap``
    object.
    """

    iconbytes = b64.b64decode(iconb64)
    success = False

    if fwidgets.wxversion() == fwidgets.WX_PHOENIX:
        image = wx.Image()
        success = wx.Image.LoadFile(image, BytesIO(iconbytes))
    else:
        image = wx.EmptyImage()
        success = wx.Image.LoadStream(image, StringIO(iconbytes))

    if not success:
        raise IconError('Error loading icon')

    return image.ConvertToBitmap()
def test_wxversion():
    fw.wxversion()
    with run_without_wx():
        assert fw.wxversion() is None
Exemple #9
0
#!/usr/bin/env python
#
# textpanel.py - A panel for displaying horizontal or vertical text.
#
# Author: Paul McCarthy <*****@*****.**>
#
"""This module provides the :class:`TextPanel` class, for displaying
some text, oriented either horizontally or vertically.
"""

import wx

import fsleyes_widgets as fw

if fw.wxversion() == fw.WX_PHOENIX: TextPanelBase = wx.Panel
else: TextPanelBase = wx.PyPanel


class TextPanel(TextPanelBase):
    """A :class:`wx.PyPanel` which may be used to display a string of
    text, oriented either horizotnally or vertically.
    """
    def __init__(self, parent, text=None, orient=wx.HORIZONTAL):
        """Create a ``TextPanel``.

        :arg parent: The :mod:`wx` parent object.

        :arg text:   The text to display. This can be changed via
                     :meth:`SetText`.

        :arg orient: Text orientation - either ``wx.HORIZONTAL`` (the

import re
import time
import logging

import                    wx
import wx.lib.newevent as wxevent

import fsleyes_widgets as fw


log = logging.getLogger(__name__)


if fw.wxversion() == fw.WX_PHOENIX: FloatSpinBase = wx.Panel
else:                               FloatSpinBase = wx.PyPanel


class FloatSpinCtrl(FloatSpinBase):
    """A ``FloatSpinCtrl`` is a :class:`wx.Panel` which contains a
    :class:`wx.TextCtrl` and a :class:`wx.SpinButton`, allowing the user to
    modify a floating point (or integer) value.

    The ``FloatSpinCtrl`` is an alternative to :class:`wx.SpinCtrl`,
    :class:`wx.SpinCtrlDouble`, and :class:`wx.lib.agw.floatspin.FloatSpin`.

     - :class:`wx.SpinCtrlDouble`: Under Linux/GTK, this widget does not allow
        the user to enter values that are not a multiple of the increment.

     - :class:`wx.lib.agw.floatspin.FloatSpin`. Differs from the
Exemple #11
0
    def __init__(self,
                 parent,
                 title=None,
                 message=None,
                 cbMessages=None,
                 cbStates=None,
                 yesText=None,
                 noText=None,
                 cancelText=None,
                 hintText=None,
                 focus=None,
                 icon=None,
                 style=None):
        """Create a ``CheckBoxMessageDialog``.

        :arg parent:        A ``wx`` parent object.

        :arg title:         The dialog frame title.

        :arg message:       Message to show on the dialog.

        :arg cbMessages:    A list of labels, one for each ``wx.CheckBox``.

        :arg cbStates:      A list of initial states (boolean values) for
                            each ``wx.CheckBox``.

        :arg yesText:       Text to show on the *yes*/confirm button. Defaults
                            to *OK*.

        :arg noText:        Text to show on the *no* button. If not provided,
                            there will be no *no* button.

        :arg cancelText:    Text to show on the *cancel* button. If not
                            provided, there will be no cancel button.

        :arg hintText:      If provided, shown as a "hint", in a slightly
                            faded font, between the checkboxes and the buttons.

        :arg focus:         One of ``'yes'``, ``'no'```, or ``'cancel'``,
                            specifying which button should be given initial
                            focus.

        :arg icon:          A ``wx`` icon identifier (e.g.
                            ``wx.ICON_EXCLAMATION``).

        :arg style:         Passed through to the ``wx.Dialog.__init__``
                            method. Defaults to ``wx.DEFAULT_DIALOG_STYLE``.
        """

        if style      is None: style      = wx.DEFAULT_DIALOG_STYLE
        if title      is None: title      = ''
        if message    is None: message    = ''
        if cbMessages is None: cbMessages = ['']
        if cbStates   is None: cbStates   = [False] * len(cbMessages)
        if yesText    is None: yesText    = 'OK'

        wx.Dialog.__init__(self, parent, title=title, style=style)

        if icon is not None:
            icon = wx.ArtProvider.GetMessageBoxIcon(icon)
            self.__icon = wx.StaticBitmap(self)

            if fw.wxversion() == fw.WX_PHOENIX:
                bmp = wx.Bitmap()
            else:
                bmp = wx.EmptyBitmap(icon.GetWidth(), icon.GetHeight())

            bmp.CopyFromIcon(icon)
            self.__icon.SetBitmap(bmp)
        else:
            self.__icon = (1, 1)

        self.__checkboxes = []
        for msg, state in zip(cbMessages, cbStates):
            cb = wx.CheckBox(self, label=msg)
            cb.SetValue(state)
            self.__checkboxes.append(cb)

        self.__message   = wx.StaticText(self, label=message)
        self.__yesButton = wx.Button(    self, label=yesText, id=wx.ID_YES)

        self.__yesButton.Bind(wx.EVT_BUTTON, self.__onYesButton)

        if noText is not None:
            self.__noButton = wx.Button(self, label=noText, id=wx.ID_NO)
            self.__noButton.Bind(wx.EVT_BUTTON, self.__onNoButton)

        else:
            self.__noButton = None

        if cancelText is not None:
            self.__cancelButton = wx.Button(self,
                                            label=cancelText,
                                            id=wx.ID_CANCEL)
            self.__cancelButton.Bind(wx.EVT_BUTTON, self.__onCancelButton)
        else:
            self.__cancelButton = None

        if hintText is not None:
            self.__hint = wx.StaticText(self, label=hintText)
            self.__hint.SetForegroundColour('#888888')
        else:
            self.__hint = None

        self.__mainSizer    = wx.BoxSizer(wx.HORIZONTAL)
        self.__contentSizer = wx.BoxSizer(wx.VERTICAL)
        self.__btnSizer     = wx.BoxSizer(wx.HORIZONTAL)

        self.__contentSizer.Add(self.__message,  flag=wx.EXPAND, proportion=1)
        self.__contentSizer.Add((1, 20), flag=wx.EXPAND)
        for cb in self.__checkboxes:
            self.__contentSizer.Add(cb, flag=wx.EXPAND)

        if self.__hint is not None:
            self.__contentSizer.Add((1, 20), flag=wx.EXPAND)
            self.__contentSizer.Add(self.__hint, flag=wx.EXPAND)

        self.__contentSizer.Add((1, 20), flag=wx.EXPAND)
        self.__btnSizer.Add((1, 1), flag=wx.EXPAND, proportion=1)

        buttons = [self.__yesButton, self.__noButton, self.__cancelButton]
        buttons = [b for b in buttons if b is not None]

        for i, b in enumerate(buttons):
            self.__btnSizer.Add(b)
            if i != len(buttons) - 1:
                self.__btnSizer.Add((5, 1), flag=wx.EXPAND)

        self.__contentSizer.Add(self.__btnSizer, flag=wx.EXPAND)

        self.__mainSizer.Add(self.__icon,
                             flag=wx.ALL | wx.CENTRE,
                             border=20)
        self.__mainSizer.Add(self.__contentSizer,
                             flag=wx.EXPAND | wx.ALL,
                             proportion=1,
                             border=20)

        self.__message.Wrap(self.GetSize().GetWidth())

        yes  = self.__yesButton
        no   = self.__noButton
        cncl = self.__cancelButton

        if   focus == 'yes':                         yes .SetDefault()
        elif focus == 'no'     and no   is not None: no  .SetDefault()
        elif focus == 'cancel' and cncl is not None: cncl.SetDefault()

        self.SetSizer(self.__mainSizer)
        self.Layout()
        self.Fit()
        self.CentreOnParent()
Exemple #12
0
    def __init__(self, parent, toolName, osxHint):
        """Create a ``FSLDirDialog``.

        :arg parent:   The :mod:`wx` parent object.

        :arg toolName: The name of the tool which is running.

        :arg osxHint:  If ``True``, an OSX-specific hint is added to the
                       dialog.
        """

        wx.Dialog.__init__(self, parent, title='$FSLDIR is not set')

        self.__fsldir  = None
        self.__icon    = wx.StaticBitmap(self)
        self.__message = wx.StaticText(  self)
        self.__locate  = wx.Button(      self, id=wx.ID_OK)
        self.__skip    = wx.Button(      self, id=wx.ID_CANCEL)

        icon = wx.ArtProvider.GetMessageBoxIcon(wx.ICON_EXCLAMATION)

        if fw.wxversion() == fw.WX_PHOENIX:
            bmp = wx.Bitmap()
        else:
            bmp  = wx.EmptyBitmap(icon.GetWidth(), icon.GetHeight())

        bmp.CopyFromIcon(icon)

        self.__icon.SetBitmap(bmp)
        self.__message.SetLabel(
            'The $FSLDIR environment variable is not set - {} '
            'may not behave correctly.'.format(toolName))
        self.__locate .SetLabel('Locate $FSLDIR')
        self.__skip   .SetLabel('Skip')

        self.__skip  .Bind(wx.EVT_BUTTON, self.__onSkip)
        self.__locate.Bind(wx.EVT_BUTTON, self.__onLocate)

        self.__mainSizer    = wx.BoxSizer(wx.HORIZONTAL)
        self.__contentSizer = wx.BoxSizer(wx.VERTICAL)
        self.__buttonSizer  = wx.BoxSizer(wx.HORIZONTAL)

        self.__buttonSizer.Add((1, 1), flag=wx.EXPAND, proportion=1)
        self.__buttonSizer.Add(self.__locate)
        self.__buttonSizer.Add((20, 1))
        self.__buttonSizer.Add(self.__skip)

        self.__contentSizer.Add(self.__message, flag=wx.EXPAND, proportion=1)
        self.__contentSizer.Add((1, 20))
        self.__contentSizer.Add(self.__buttonSizer, flag=wx.EXPAND)

        # If running on OSX, add a message
        # telling the user about the
        # cmd+shift+g shortcut
        if osxHint:

            self.__hint = wx.StaticText(
                self,
                label=six.u('Hint: Press \u2318+\u21e7+G in the file '
                            'dialog to manually type in a location.'))

            self.__hint.SetForegroundColour('#888888')

            self.__contentSizer.Insert(2, self.__hint, flag=wx.EXPAND)
            self.__contentSizer.Insert(3, (1, 20))

        else:
            self.__hint = None

        self.__mainSizer.Add(self.__icon,
                             flag=wx.ALL | wx.CENTRE,
                             border=20)
        self.__mainSizer.Add(self.__contentSizer,
                             flag=wx.EXPAND | wx.ALL,
                             proportion=1,
                             border=20)

        self.__message.Wrap(self.GetSize().GetWidth())

        self.SetSizer(self.__mainSizer)
        self.__mainSizer.Layout()
        self.__mainSizer.Fit(self)

        self.CentreOnParent()
Exemple #13
0
    def __init__(self,
                 parent,
                 title='',
                 message='',
                 text='',
                 icon=None,
                 style=None):
        """Create a ``TextEditDialog``.

        :arg parent:  The :mod:`wx` parent object.

        :arg title:   Dialog title.

        :arg message: Dialog message.

        :arg text:    String  to display in the text field.

        :arg icon:    A :mod:`wx` icon identifier, such as
                      :data:`wx.ICON_INFORMATION` or :data:`wx.ICON_WARNING`.

        :arg style:   A combination of :data:`TED_READONLY`,
                      :data:`TED_MULTILINE`, :data:`TED_OK`,
                      :data:`TED_CANCEL`, :data:`TED_OK_CANCEL`,
                      :data:`TED_COPY` and :data:`TED_COPY_MESSAGE` . Defaults
                      to :data:`TED_OK`.
        """

        if style is None:
            style = TED_OK

        wx.Dialog.__init__(self,
                           parent,
                           title=title,
                           style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)

        textStyle = 0
        if style & TED_READONLY:  textStyle |= wx.TE_READONLY
        if style & TED_MULTILINE: textStyle |= wx.TE_MULTILINE

        self.__message  = wx.StaticText(self)
        self.__textEdit = wx.TextCtrl(  self, style=textStyle)

        self.__message .SetLabel(message)
        self.__textEdit.SetValue(text)

        self.__showCopyMessage = style & TED_COPY_MESSAGE

        # set the min size of the text
        # ctrl so it can fit a few lines
        self.__textEdit.SetMinSize((-1, 120))
        self.__textEdit.SetMaxSize((600, -1))

        self.__ok      = (-1, -1)
        self.__copy    = (-1, -1)
        self.__cancel  = (-1, -1)
        self.__icon    = (-1, -1)
        self.__buttons = []

        if icon is not None:

            icon = wx.ArtProvider.GetMessageBoxIcon(icon)

            if fw.wxversion() == fw.WX_PHOENIX:
                bmp = wx.Bitmap()
            else:
                bmp = wx.EmptyBitmap(icon.GetWidth(), icon.GetHeight())

            bmp.CopyFromIcon(icon)
            self.__icon = wx.StaticBitmap(self)
            self.__icon.SetBitmap(bmp)

        if style & TED_OK:
            self.__ok = wx.Button(self, id=wx.ID_OK)
            self.__ok.Bind(wx.EVT_BUTTON, self.__onOk)
            self.__buttons.append(self.__ok)

        if style & TED_CANCEL:
            self.__cancel = wx.Button(self, id=wx.ID_CANCEL)
            self.__cancel.Bind(wx.EVT_BUTTON, self.__onCancel)
            self.__buttons.append(self.__cancel)

        if style & TED_COPY:
            self.__copy = wx.Button(self, label='Copy to clipboard')
            self.__copy.Bind(wx.EVT_BUTTON, self.__onCopy)
            self.__buttons.append(self.__copy)

        self.__textEdit.Bind(wx.EVT_CHAR_HOOK, self.__onCharHook)

        textSizer = wx.BoxSizer(wx.VERTICAL)
        iconSizer = wx.BoxSizer(wx.HORIZONTAL)
        btnSizer  = wx.BoxSizer(wx.HORIZONTAL)
        mainSizer = wx.BoxSizer(wx.VERTICAL)

        textSizer.Add(self.__message,
                      flag=wx.ALL | wx.CENTRE,
                      border=20)
        textSizer.Add(self.__textEdit,
                      flag=wx.ALL | wx.EXPAND,
                      border=20,
                      proportion=1)

        iconSizer.Add(self.__icon, flag=wx.ALL | wx.CENTRE, border=20)
        iconSizer.Add(textSizer, flag=wx.EXPAND, proportion=1)

        btnSizer.AddStretchSpacer()
        btnSizer.Add(self.__ok,
                     flag=wx.ALL | wx.CENTRE,
                     border=10)
        btnSizer.Add(self.__copy,
                     flag=wx.ALL | wx.CENTRE,
                     border=10)
        btnSizer.Add(self.__cancel,
                     flag=wx.ALL | wx.CENTRE,
                     border=10)
        btnSizer.Add((-1, 20))

        mainSizer.Add(iconSizer, flag=wx.EXPAND, proportion=1)
        mainSizer.Add(btnSizer,  flag=wx.EXPAND)

        self.SetSizer(mainSizer)
        self.Fit()