widgets.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. New Widgets
  2. ===========
  3. KCG is written in Python using PyQt4. Therefore all Widgets are and have to be written in Python with PyQt4 for the
  4. Gui toolkit.
  5. Creating Widgets
  6. ----------------
  7. The following things are mandatory:
  8. * A widget has to be defined in it's own class in it's own seperate module (file).
  9. * The class for the widget has to be derived from base.kcgwidget.KCGWidgets.
  10. * The __init__ method has to set self.par from `parent` parameter and self.id from `unique_id` parameter. It also has to call the KCGWidgets __init__ method. See :ref:`here <exampleInit>`.
  11. * The module has to contain the variable __widget_id__ that is initially set to `None`
  12. * The module has to contain a function to add the widget to the MultiWidget area as described :ref:`here <add_widget_function>`.
  13. * The class has to override the method closeEvent(...). See :ref:`here <closeEvent>`.
  14. * The module has to ``import base.kcgwidget as kcgw``
  15. .. note:: To activate a widget it has to be placed into the widget subfolder and the module name has to be added to the __all__
  16. variable in the file ``__init__.py`` in the widget folder.
  17. To register a widget this line is needed:
  18. .. code:: python
  19. kcgw.register_widget(QtGui.QIcon("path/to/icon"), "Name of Widget", addWidgetFunction, "Ctrl+e")
  20. Substitute ``"path/to/icon"``, ``"Name of Widget"``, ``addWidgetFunction`` (see :ref:`add_widget_function`) and ``"Ctrl+e"`` with
  21. appropriate values. For ``kcgw.register_widget()`` refer to the :ref:`kcgwidget module <kcgwidget_register_widget>`.
  22. .. note:: Collision of keyboard shortcuts is not handled by the gui.
  23. If a widget is activated and registered it will automatically be imported and the correct steps to integrate the widget are performed.
  24. .. hint:: Because every widget is derived from base.kcgwidget.KCGWidgets the helper methods defined :ref:`here <kcgwidgets_helper_methods>`
  25. can be used.
  26. .. _exampleInit:
  27. Minimal __init__
  28. ~~~~~~~~~~~~~~~~
  29. Assume: Widget class is called `ExampleWidget`
  30. .. code:: python
  31. def __init__(self, unique_id, parent):
  32. super(ExampleWidget, self).__init__()
  33. self.id = unique_id
  34. self.par = parent
  35. .. _add_widget_function:
  36. Function to add a widget
  37. ~~~~~~~~~~~~~~~~~~~~~~~~
  38. Assume: base.kcgwiget is imported as kcgw
  39. .. code:: python
  40. def addNewWidget():
  41. global __widget_id__
  42. if __widget_id__:
  43. kcgw.area.widgets[__widget_id__].setFocus()
  44. else:
  45. nid = kcgw.idg.genid()
  46. __widget_id__ = nid
  47. w = acquireSettings(nid, kcgw.area)
  48. kcgw.area.newWidget(w, "NewWidgetName", nid, widget_type=NewWidgetType_as_int, minSize=True)
  49. .. note:: It is highly recommended to use this function as is (with adjustments to the function name, ``NewWidgetName``
  50. and ``NewWidgetType_as_int``). For ``newWidget(..)`` see :ref:`here <newWidget>`.
  51. .. _closeEvent:
  52. closeEvent method
  53. ~~~~~~~~~~~~~~~~~
  54. .. code:: python
  55. def closeEvent(self, event):
  56. global __widget_id__
  57. __widget_id__ = None
  58. del self.par.widgets[self.id]
  59. This is a minimal construct that has to be present in a widget class.
  60. Interface to the GUI
  61. --------------------
  62. Sometimes elements (e.g. Buttons) have to be disabled at certain points. To group elements you can make use of the
  63. groupelements module.
  64. See :ref:`groupedelements`.
  65. Following is a list of used groups that will be activated and disabled at certain points in runtime:
  66. * **after_start**: Elements in this group get enabled after board is started
  67. * **continuous_read**: Elements in this group get enabled when a continuous read is possible
  68. * **synchronize**: Elements in this group get enabled when the board is calibrated
  69. * **set_defaults**: Elements in this group get enabled when the board is synchronized
  70. * **timing**: Elements in this group get enabled when defaults are set
  71. .. caution:: Be careful when using one of this groups, as the names may mislead their point of activation.
  72. Interface to the board
  73. ----------------------
  74. It is recommended to use the :ref:`backendinterface` to interface with the board
  75. Example Widget
  76. --------------
  77. This is a minimal example implementation of a widget.
  78. .. code:: python
  79. from PyQt4 import QtGui
  80. import base.kcgwidget as kcgw
  81. __widget_id__ = None
  82. class exampleWidget(kcgw.KCGWidgets):
  83. def __init__(self, unique_id, parent):
  84. super(exampleWidget, self).__init__()
  85. self.id = unique_id
  86. self.par = parent
  87. self.layout = QtGui.QHBoxLayout()
  88. self.setLayout(self.layout)
  89. self.button1 = self.createButton("Button 1", connect=self.pressed)
  90. self.button2 = self.createButton("Button 2", connect=self.pressed)
  91. self.layout.addWidget(self.button1)
  92. self.layout.addWidget(self.button2)
  93. self.setWindowTitle("Example Widget")
  94. def pressed(self):
  95. print 'Pressed'
  96. def closeEvent(self, event):
  97. global __widget_id__
  98. __widget_id__ = None
  99. del self.par.widgets[self.id]
  100. def addExampleWidget():
  101. global __widget_id__
  102. if __widget_id__:
  103. kcgw.area.widgets[__widget_id__].setFocus()
  104. else:
  105. nid = kcgw.idg.genid()
  106. __widget_id__ = nid
  107. w = exampleWidget(nid, kcgw.area)
  108. kcgw.area.newWidget(w, "Example Widget", nid, widget_type=4)
  109. kcgw.register_widget(QtGui.QIcon("icons/project.svg"), "Example Widget", addExampleWidget, "Ctrl+e")
  110. Screenshot:
  111. .. image:: _static/example_widget.png
  112. .. caution:: Be careful when refering to this as this is a screenshot made with kde5 and PyQt4.
  113. The design of the actual widget may vary between different desktop environments.