{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Algorithm cancel and progress widget\n\nThis scipt shows how the ``itom.progressObserver`` is used\nto observe and report the progress of functions.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itomUi import (\n ItomUi,\n) # import the base class ItomUi from the module itomUi in the itom-packages subfolder\nfrom itom import ui\nfrom itom import uiItem\nfrom itom import progressObserver\nfrom contextlib import contextmanager\nfrom typing import Dict\n\n\nclass AlgoCancelAndProgressWidget(ItomUi):\n def __init__(self): # constructor\n\n # call constructor of ItomUi like it would be the constructor of the class itom.ui:\n ItomUi.__init__(self, \"algoCancelAndProgressWidget.ui\", ui.TYPEWINDOW)\n\n self.observer = progressObserver(\n progressBar=self.gui.progressBar,\n label=self.gui.lblProgress,\n progressMinimum=0,\n progressMaximum=100,\n )\n\n self.gui.btnCancel[\"visible\"] = False\n self.gui.lblProgress[\"visible\"] = False\n self.gui.progressBar[\"visible\"] = False\n self.gui.btnCancel.invokeProgressObserverCancellation(\n \"clicked()\", self.observer\n )\n\n @ItomUi.autoslot(\"\")\n def on_btnStart_clicked(self):\n with self.disableGui(\n {\n self.gui.btnStart: False,\n self.gui.btnCancel: True,\n self.gui.lblProgress: True,\n self.gui.progressBar: True,\n }\n ):\n # the following filter must have the ability to provide status information (see information of filter)\n filter(\"demoCancellationFunction\", _observer=self.observer)\n\n @contextmanager\n def disableGui(self, widgets: Dict[uiItem, bool]):\n \"\"\"this is a smart helper method that can be used in a with context.\n It changes the visible property when entering the with context for\n all given uiItems to the given boolean value, then executes the content\n of the with statement, and finally switches the visible properties back\n to the origin.The switch back is executed even if an exception (cancellation\n of the algorithm etc.) occurred.\n \"\"\"\n for w in widgets:\n w[\"visible\"] = widgets[w]\n try:\n yield\n finally:\n for w in widgets:\n w[\"visible\"] = not widgets[w]\n\n\n# create a first instance of AlgoCancelAndProgressWidget and the gui\nwin1 = AlgoCancelAndProgressWidget()\nwin1.gui.show() # show the gui" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }