Skip to content

Commit

Permalink
[#45] update loading gif
Browse files Browse the repository at this point in the history
  • Loading branch information
jeaunrg committed Jun 14, 2021
1 parent 03da28a commit 5103430
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 18 deletions.
11 changes: 3 additions & 8 deletions src/presenter/presenter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from PyQt5 import uic
from src import UI_DIR, CONFIG_DIR
import json
from PyQt5 import QtWidgets, QtCore
from PyQt5 import QtWidgets
import os
import nibabel as nib
from src import DATA_DIR, OUT_DIR
Expand All @@ -20,15 +20,15 @@ class Presenter:
"""

def __init__(self, view, model=None):
def __init__(self, view, model=None, threading_enabled=True):
self._view = view
self._model = model
self.modules = json.load(open(os.path.join(CONFIG_DIR, "modules.json"), "rb"))
self._view.initMenu(self.modules)
self._view.graph.nodeClicked.connect(self.activate_node)
self._view.presenter = self

self.threading_enabled = False
self.threading_enabled = threading_enabled
self._view.action_thread.toggled.connect(self.enable_threading)

def enable_threading(self, boolean):
Expand All @@ -53,11 +53,6 @@ def activate_node(self, node):
widget.node = node
node.parameters.addWidget(widget)

# add loading gif to widget
node.gif = ui.Gif()
node.parameters.addWidget(node.gif)
node.parameters.setAlignment(node.gif, QtCore.Qt.AlignHCenter)

activation_function = eval("self."+parameters['function'])
widget.apply.clicked.connect(lambda: activation_function(widget))

Expand Down
12 changes: 6 additions & 6 deletions src/presenter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def run(self):

def view_manager(threadable=True):
"""
this decorator manage the loading gif and threading
this decorator manage the progress bar and threading
Parameters
----------
Expand All @@ -39,18 +39,18 @@ def view_manager(threadable=True):
"""
def decorator(foo):
def inner(presenter, widget):
# start gif animation
widget.node.gif.start()

# start progress bar
widget.node.setState("loading")
function, args = foo(presenter, widget)

def updateView(output):
widget.node.progress.setMaximum(1)
if isinstance(output, Exception):
widget.node.gif.fail("[{0}] {1}".format(type(output).__name__, output))
widget.node.setState("fail", "[{0}] {1}".format(type(output).__name__, output))
else:
store_image(output, widget.node.name)
widget.node.updateSnap()
widget.node.gif.stop()
widget.node.setState("stop")

# start the process inside a QThread
if threadable and presenter.threading_enabled:
Expand Down
21 changes: 21 additions & 0 deletions src/view/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,27 @@ def getScaledColorTable(self, im):
self.ctable = [QtGui.qRgb(i, i, i) for i in values]
return self.ctable

def setState(self, state, msg=""):
"""
set state of progress
Parameters
----------
state: {"loading", "fail", "stop"}
msg: str,
tooltip added to the node to see errors
"""
if state == "loading":
self.progress.setMaximum(0)
self.progress.setValue(-1)
else:
self.progress.setMaximum(1)
if state == "fail":
self.progress.setValue(0)
elif state == "stop":
self.progress.setValue(1)
self.button.setToolTip(msg)

def updateSnap(self, sync=True):
"""
update image slice in snap view
Expand Down
3 changes: 3 additions & 0 deletions src/view/ui/Home.ui
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="text">
<string>process in background</string>
</property>
Expand Down
41 changes: 38 additions & 3 deletions src/view/ui/Node.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>221</width>
<height>90</height>
<width>256</width>
<height>50</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -45,7 +45,42 @@
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="parameters"/>
<widget class="QProgressBar" name="progress">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>2</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QProgressBar::chunk {background-color: qlineargradient(spread:pad, x1:0, x2:1, stop:0 transparent, stop:0.5 green, stop:1 transparent);}</string>
</property>
<property name="maximum">
<number>1</number>
</property>
<property name="value">
<number>0</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="invertedAppearance">
<bool>false</bool>
</property>
<property name="textDirection">
<enum>QProgressBar::TopToBottom</enum>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="parameters">
<property name="spacing">
<number>7</number>
</property>
</layout>
</item>
<item alignment="Qt::AlignTop">
<widget class="QLabel" name="snap">
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_image_write_path():
def francis():
# Launch Francis
view = View()
Presenter(view)
Presenter(view, threading_enabled=False)
return view


Expand Down

0 comments on commit 5103430

Please sign in to comment.