Permalink
Browse files

Added Preloader, needs commenting and tests

  • Loading branch information...
karandeepSJ committed Jul 25, 2018
1 parent b55303c commit 0240ba3e301fa6a3407d5e434ac4141efc0c311c
Showing with 132 additions and 0 deletions.
  1. +132 −0 dipy/viz/ui.py
@@ -3303,3 +3303,135 @@ def left_button_clicked(self, i_ren, obj, list_box_item):
self.list_box.select(self, multiselect, range_select)
i_ren.force_render()
i_ren.event.abort() # Stop propagating the event.


class Preloader(UI):
""" A animated component to give the effect of loading.
Uses three concentric arcs rotating about their centres.
"""

def __init__(self, outer_radius, width=10, center=(0, 0)):
""" Initializes a Preloader.
Parameters
----------
outer_radius : int
Radius of the outermost arc.
width: int, optional
Distance between two consecutive arcs in pixels.
center : (float, float), optional
Coordinates (x, y) of the center of the concentric arcs.
"""
super(Preloader, self).__init__()
self.outer_radius = outer_radius
self.width = width
self.position = center
self.outer_arc.GetProperty().SetColor(0,0,1)
self.middle_arc.GetProperty().SetColor(1,0,0)
self.middle_arc.RotateZ(30)
self.inner_arc.GetProperty().SetColor(1,1,0)
self.inner_arc.RotateZ(60)

def _setup(self):
""" Setup this UI component.
"""
# Setting up arc actors.
self._arc1 = vtk.vtkArcSource()
self._arc1.UseNormalAndAngleOn()
self._arc1.SetResolution(50)
self._arc1.SetAngle(60)

self._arc2 = vtk.vtkArcSource()
self._arc2.UseNormalAndAngleOn()
self._arc2.SetResolution(50)
self._arc2.SetAngle(50)

self._arc3 = vtk.vtkArcSource()
self._arc3.UseNormalAndAngleOn()
self._arc3.SetResolution(50)
self._arc3.SetAngle(40)

# Mappers
mapper1 = vtk.vtkPolyDataMapper()
mapper1 = set_input(mapper1, self._arc1.GetOutputPort())
mapper2 = vtk.vtkPolyDataMapper()
mapper2 = set_input(mapper2, self._arc2.GetOutputPort())
mapper3 = vtk.vtkPolyDataMapper()
mapper3 = set_input(mapper3, self._arc3.GetOutputPort())

# Actors
self.outer_arc = vtk.vtkActor()
self.outer_arc.SetMapper(mapper1)
self.middle_arc = vtk.vtkActor()
self.middle_arc.SetMapper(mapper2)
self.inner_arc = vtk.vtkActor()
self.inner_arc.SetMapper(mapper3)

def _get_actors(self):
""" Get the actors composing this UI component.
"""
return [self.inner_arc,self.middle_arc,self.outer_arc]

def _add_to_renderer(self, ren):
""" Add all subcomponents or VTK props that compose this UI component.
Parameters
----------
ren : renderer
"""
ren.add(self.inner_arc)
ren.add(self.middle_arc)
ren.add(self.outer_arc)

def _get_size(self):
diameter = 2 * self.outer_radius
size = (diameter, diameter)
return size

def _set_position(self, coords):
""" Position the center of the arcs.
Parameters
----------
coords: (float, float)
Absolute pixel coordinates (x, y).
"""
self._arc1.SetCenter(coords[0],coords[1],0)
self._arc2.SetCenter(coords[0],coords[1],0)
self._arc3.SetCenter(coords[0],coords[1],0)

@property
def width(self):
return self._arc1.GetPolarVector()[0] - self._arc2.GetPolarVector()[0]

@width.setter
def width(self, width):
self._arc1.SetPolarVector(self.outer_radius,0,0)
self._arc2.SetPolarVector(self.outer_radius - width,0,0)
self._arc3.SetPolarVector(self.outer_radius - 2 * width,0,0)

@property
def outer_radius(self):
return self._arc1.GetPolarVector()[0]

@outer_radius.setter
def outer_radius(self, radius):
self._arc1.SetPolarVector(radius,0,0)
self._arc2.SetPolarVector(radius - self.width,0,0)
self._arc3.SetPolarVector(radius - 2 * self.width,0,0)

def rotate(self, style, iren, preloader):
""" Rotation of the arcs invoked by timer event.
Parameters
----------
style : :class:`CustomInteractorStyle`
iren : :class:`vtkRenderWindowInteractor`
slider : :class:`Preloader`
"""
self.outer_arc.RotateZ(2)
self.middle_arc.RotateZ(2)
self.inner_arc.RotateZ(2)
style.force_render()

0 comments on commit 0240ba3

Please sign in to comment.