{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dynamic form layout\n\nExample for dynamically changing the content of a form layout.\n\nThis example shows how new widgets are added or inserted to a form\nlayout in an user interface, how entire rows are removed or how\nthe existing widgets in a form layout are requested.\n\nMost functions used in this example are based on special methods\nof the form layout (Qt class: ``QFormLayout``), that are made accessible\nin itom via the ``uiItem.call('methodName', *args)`` method.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from itomUi import ItomUi\nfrom itom import ui\nfrom itom import uiItem\n\n\nclass DynamicFormLayout(ItomUi):\n \"\"\"Main class with the functionality of the user interface.\"\"\"\n\n def __init__(self):\n \"\"\"Constructor.\"\"\"\n # call constructor of ItomUi like it would be the constructor of the class itom.ui:\n ItomUi.__init__(self, \"dynamicFormLayout.ui\", ui.TYPEWINDOW)\n\n # the spacing between each item of the layout is a property\n self.layout[\"spacing\"] = 7\n\n self.gui.comboAddWidget.call(\"addItems\", ui.availableWidgets())\n\n self._update()\n\n @property\n def layout(self):\n \"\"\"The reference to the form layout.\"\"\"\n return self.gui.formLayout\n\n @property\n def rowCount(self):\n \"\"\"Returns number of rows in the form layout.\"\"\"\n return self.layout.call(\"rowCount\")\n\n @ItomUi.autoslot(\"\")\n def on_btnAddRowLineEdit_clicked(self):\n \"\"\"Adds a new row with a string label and\n a line edit widget (class name in Qt: QLineEdit.\"\"\"\n num = self.rowCount\n name = f\"LineEdit_{num+1}\"\n objName = name\n self.layout.call(\"addRow\", name, \"QLineEdit\", objName)\n self._update()\n\n @ItomUi.autoslot(\"\")\n def on_btnAddRowCheckBox_clicked(self):\n \"\"\"Adds a new row with a string label and\n a line edit widget (class name in Qt: QCheckBox.\"\"\"\n num = self.rowCount\n name = f\"CheckBox_{num+1}\"\n objName = name\n box = self.layout.call(\"addRow\", name, \"QCheckBox\", objName)\n box[\"checked\"] = True\n self._update()\n\n @ItomUi.autoslot(\"\")\n def on_btnInsertRowComboBox_clicked(self):\n row, valid = ui.getInt(\n \"Row index\",\n \"At which row should the combo box be inserted?\",\n 0,\n parent=self.gui,\n )\n\n if valid:\n name = f\"ComboBox_{self.rowCount+1}\"\n objName = name\n combo = self.layout.call(\"insertRow\", row, name, \"QComboBox\", objName)\n combo.call(\"addItems\", (\"Option 1\", \"Option 2\", \"Option 3\"))\n self._update()\n\n @ItomUi.autoslot(\"\")\n def on_btnSetWidget_clicked(self):\n\n rowIndex = self.gui.spinAddRowIndex[\"value\"]\n\n # 0: modify label, 1: modify field, 2: widget spans both columns\n role = self.gui.comboAddRole[\"currentIndex\"]\n\n className = self.gui.comboAddWidget[\"currentText\"]\n\n self.layout.call(\"setItem\", rowIndex, role, className, f\"item_{rowIndex}_{role}\")\n self._update()\n\n @ItomUi.autoslot(\"\")\n def on_btnRemoveRow_clicked(self):\n row, valid = ui.getInt(\n \"Row index\",\n \"Which row index should be removed?\",\n 0,\n min=0,\n max=self.rowCount - 1,\n parent=self.gui,\n )\n\n if valid:\n self.layout.call(\"removeRow\", row)\n self._update()\n\n @ItomUi.autoslot(\"\")\n def on_btnInfo_clicked(self):\n\n items = []\n label: uiItem\n field: uiItem\n\n for r in range(self.rowCount):\n try:\n label = self.layout.call(\"itemAtPosition\", r, 0) # 0 stands for label\n className = label.getClassName()\n\n if className == \"QLabel\":\n lblStr = label[\"text\"]\n else:\n lblStr = str(label)\n\n except RuntimeError:\n lblStr = \"\"\n\n try:\n field = self.layout.call(\"itemAtPosition\", r, 1) # 1 stands for field\n className = field.getClassName()\n\n if className == \"QLineEdit\":\n fieldStr = field[\"text\"]\n elif className == \"QCheckBox\":\n fieldStr = \"Check Box\"\n elif className == \"QComboBox\":\n fieldStr = field[\"currentText\"]\n else:\n fieldStr = str(field)\n except RuntimeError:\n fieldStr = \"\"\n\n items.append(f\"{r + 1}: {lblStr}: {fieldStr}\")\n\n text = (\n f\"Form Layout with {self.rowCount} rows:\\n\"\n + \"-----------------------------------------\\n\\n\"\n + \"\\n\".join(items)\n )\n\n ui.msgInformation(\"Info\", text, parent=self.gui)\n\n def _update(self):\n self.gui.btnRemoveRow[\"enabled\"] = self.rowCount > 0\n self.gui.lblCaption[\"text\"] = f\"Form Layout (Currently {self.rowCount} rows):\"\n\n\nif __name__ == \"__main__\":\n # create an object of DynamicFormLayout and shows it\n win1 = DynamicFormLayout()\n win1.gui.show()" ] }, { "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 }