{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Multiple plots in horizontal layout\n\nExample for dynamically changing the content of a form layout.\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 dataObject\n\n\n\nclass MultiPlotHorLayout(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, \"multiplePlotsInHorizontalLayout.ui\", ui.TYPEWINDOW)\n\n # the spacing between each item of the layout is a property\n self.layout[\"spacing\"] = 7\n\n # contents margins is left, top, right, bottom\n self.layout.call(\"setContentsMargins\", 30, 15, 20, 5)\n\n @property\n def layout(self):\n \"\"\"The reference to the horizontal layout.\"\"\"\n return self.gui.horLayout\n\n @property\n def numWidgets(self):\n \"\"\"Returns number of widgets in horLayout.\"\"\"\n return self.layout.call(\"count\")\n\n @ItomUi.autoslot(\"\")\n def on_btnInfo_clicked(self):\n text = f\"Num plots: {self.numWidgets}\"\n\n for idx in range(self.numWidgets):\n widget: uiItem = self.layout.call(\"itemAt\", idx)\n stretch: int = self.layout.call(\"stretch\", idx)\n text += f\"\\nWidget {idx+1}: {str(widget)}, stretch: {stretch}\"\n\n ui.msgInformation(\"Information\", text, parent=self.gui)\n\n @ItomUi.autoslot(\"\")\n def on_btnAddButton_clicked(self):\n className = \"QPushButton\"\n objectName = f\"Button_{self.numWidgets}\"\n obj: uiItem = self.layout.call(\"addItem\", className, objectName)\n obj[\"text\"] = objectName\n\n obj.connect(\"clicked()\", self._buttonClicked)\n\n self.gui.btnRemove[\"enabled\"] = self.numWidgets > 0\n self.gui.btnSetStretch[\"enabled\"] = self.numWidgets > 0\n\n @ItomUi.autoslot(\"\")\n def on_btnAddPlot_clicked(self):\n className = \"itom2dqwtplot\"\n objectName = f\"Plot_{self.numWidgets}\"\n obj: uiItem = self.layout.call(\"addItem\", className, objectName)\n obj[\"source\"] = dataObject.randN([30, 10])\n\n self.gui.btnRemove[\"enabled\"] = self.numWidgets > 0\n self.gui.btnSetStretch[\"enabled\"] = self.numWidgets > 0\n\n @ItomUi.autoslot(\"\")\n def on_btnInsertButton_clicked(self):\n\n idx, valid = ui.getInt(\n \"Position\",\n \"At which index should the button be inserted (-1: end)?\",\n 0,\n parent=self.gui,\n )\n\n if valid:\n className = \"QPushButton\"\n objectName = f\"Button_{self.numWidgets}\"\n obj: uiItem = self.layout.call(\"insertItem\", idx, className, objectName)\n obj[\"text\"] = objectName\n\n obj.connect(\"clicked()\", self._buttonClicked)\n\n self.gui.btnRemove[\"enabled\"] = self.numWidgets > 0\n self.gui.btnSetStretch[\"enabled\"] = self.numWidgets > 0\n\n @ItomUi.autoslot(\"\")\n def on_btnInsertFromUiFile_clicked(self):\n idx, valid = ui.getInt(\n \"Position\",\n \"At which index should the widget from the UI file 'container.ui' be inserted (-1: end)?\",\n 0,\n parent=self.gui,\n )\n\n if valid:\n obj: uiItem = self.layout.call(\n \"insertItemFromUiFile\",\n idx, # index\n \"container.ui\", # filename to ui file\n \"_%i\" % self.numWidgets, # prefix, added to the objectNames of all new widgets and layouts\n )\n\n self.gui.btnRemove[\"enabled\"] = self.numWidgets > 0\n self.gui.btnSetStretch[\"enabled\"] = self.numWidgets > 0\n\n @ItomUi.autoslot(\"\")\n def on_btnRemove_clicked(self):\n\n if self.numWidgets <= 0:\n return\n\n labels = [self.layout.call(\"itemAt\", idx)[\"objectName\"] for idx in range(self.numWidgets)]\n\n name, valid = ui.getItem(\n \"Widget to remove\",\n \"Select the widget to be removed\",\n labels,\n editable=False,\n )\n\n if valid:\n idx = labels.index(name)\n self.layout.call(\"removeItemAt\", idx)\n\n self.gui.btnRemove[\"enabled\"] = self.numWidgets > 0\n self.gui.btnSetStretch[\"enabled\"] = self.numWidgets > 0\n\n @ItomUi.autoslot(\"\")\n def on_btnSetStretch_clicked(self):\n\n if self.numWidgets <= 0:\n return\n\n stretchs = [str(self.layout.call(\"stretch\", idx)) for idx in range(self.numWidgets)]\n\n text, valid = ui.getText(\n \"Stretch\",\n f\"Indicate a comma-separated list of stretch \" f\"factors for up to {self.numWidgets} widgets\",\n \",\".join(stretchs),\n )\n\n if valid:\n stretchs = text.split(\",\")\n\n if len(stretchs) > self.numWidgets:\n ui.msgCritical(\n \"Wrong input\",\n f\"Stretchs must be a comma separated list of \" f\"integers (max. {self.numWidgets} entries)\",\n parent=self.gui,\n )\n return\n\n for idx in range(len(stretchs)):\n try:\n val = int(stretchs[idx])\n except ValueError:\n ui.msgCiritcal(\n \"Wrong input\",\n f\"Value '{stretchs[idx]}' is no integer number\",\n parent=self.gui,\n )\n return\n\n self.layout.call(\"setStretch\", idx, val)\n\n def _buttonClicked(self):\n # slot, called if any button is clicked\n ui.msgInformation(\"Button clicked\", \"The button has been clicked\", parent=self.gui)\n\n\n# create a first instance of AutoConnectExample and the gui\nwin1 = MultiPlotHorLayout()\nwin1.gui.show() # show the gui" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n" ] } ], "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 }