MultiWidget.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. """
  2. This is the container widget for multiple subwindows
  3. """
  4. from PyQt4 import QtGui, QtCore
  5. from . import kcgwidget as kcgw
  6. from . import LeftBar
  7. from .globals import glob as global_objects
  8. from .. import config
  9. from . import ModeButtonsToolbar
  10. tr = kcgw.tr
  11. class WidgetTypeError(Exception):
  12. """
  13. Simple error that describes when a wrong window type gets added
  14. """
  15. pass
  16. class MDIArea(kcgw.KCGWidgets):
  17. """
  18. The MDI Area used by Multiwidget
  19. """
  20. def __init__(self, parent):
  21. super(MDIArea, self).__init__("MDIArea")
  22. self.parent = parent
  23. self.layout = QtGui.QHBoxLayout()
  24. self.layout.setContentsMargins(0, 0, 0, 0)
  25. self.setLayout(self.layout)
  26. self.widgets = {}
  27. self.area = QtGui.QMdiArea()
  28. self.area.setFrameStyle(self.area.StyledPanel | self.area.Sunken)
  29. self.layout.addWidget(self.area)
  30. brush = QtGui.QBrush(QtGui.QColor('white'))
  31. self.area.setBackground(brush)
  32. def newWidget(self, widget, name, unique_id, widget_type, minSize=False):
  33. """
  34. Add a new Widget to the MDIArea
  35. :param widget: (subclass of QMdiSubWindow) The widget to show
  36. :param name: (str) name of the window
  37. :param unique_id: (int) unique id of the window
  38. :param widget_type: (int) the type of this window
  39. :param minSize: (bool) whether to shrink the window to minimum size upon creation
  40. :return: -
  41. """
  42. if not isinstance(widget, kcgw.KCGWidgets):
  43. raise WidgetTypeError("Newly Created Widget is of type " +
  44. type(widget).__name__ +
  45. ". Expected class derived of " +
  46. kcgw.KCGWidgets.__name__ + ".")
  47. subWidget = kcgw.KCGSubWidget(name=name, unique_id=unique_id, typ=widget_type, minSize=minSize)
  48. subWidget.setAttribute(QtCore.Qt.WA_DeleteOnClose)
  49. widget.setParent(subWidget)
  50. if minSize:
  51. subWidget.resize(widget.minimumSizeHint()*1.2)
  52. widget.closeSignal.connect(subWidget.close)
  53. subWidget.setWidget(widget)
  54. self.area.addSubWindow(subWidget)
  55. subWidget.show()
  56. self.widgets[unique_id] = widget
  57. # 888b d888 888 888 d8b 888 888 d8b 888 888
  58. # 8888b d8888 888 888 Y8P 888 o 888 Y8P 888 888
  59. # 88888b.d88888 888 888 888 d8b 888 888 888
  60. # 888Y88888P888 888 888 888 888888 888 888 d888b 888 888 .d88888 .d88b. .d88b. 888888
  61. # 888 Y888P 888 888 888 888 888 888 888d88888b888 888 d88" 888 d88P"88b d8P Y8b 888
  62. # 888 Y8P 888 888 888 888 888 888 88888P Y88888 888 888 888 888 888 88888888 888
  63. # 888 " 888 Y88b 888 888 Y88b. 888 8888P Y8888 888 Y88b 888 Y88b 888 Y8b. Y88b.
  64. # 888 888 "Y88888 888 "Y888 888 888P Y888 888 "Y88888 "Y88888 "Y8888 "Y888
  65. # 888
  66. # Y8b d88P
  67. # "Y88P"
  68. class MultiWidget(QtGui.QWidget):
  69. """
  70. The Widget used as Multiwidget. This is the main View during operation with KCG.
  71. """
  72. def __init__(self):
  73. super(MultiWidget, self).__init__()
  74. self.layout = QtGui.QVBoxLayout()
  75. self.setLayout(self.layout)
  76. self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
  77. self.leftBar = LeftBar.LeftBar(self)
  78. self.area = MDIArea(self)
  79. self.area.setObjectName("MDIArea")
  80. self.toolbar = QtGui.QToolBar()
  81. global_objects.set_global('area', self.area)
  82. # --------[ ToolBar ]--------
  83. self.new_live_action = QtGui.QAction(QtGui.QIcon(config.icon_path(config.newPlotLiveIcon)), tr("Button", "New Live Plot"), self)
  84. self.new_live_action.setShortcut("Ctrl+L")
  85. self.toolbar.addAction(self.new_live_action)
  86. self.new_live_action.triggered.connect(lambda: self.leftBar.add_plot(LeftBar.LIVE))
  87. self.new_live_action.setToolTip(tr("Button", "New Live Plot") + "\n(Ctrl+L)")
  88. self.new_data_action = QtGui.QAction(QtGui.QIcon(config.icon_path(config.newPlotDataIcon)), tr("Button", "New Data Plot"), self)
  89. self.new_data_action.setShortcuts(["Ctrl+D", "Ctrl+O"])
  90. self.toolbar.addAction(self.new_data_action)
  91. self.new_data_action.triggered.connect(lambda: self.leftBar.add_plot(LeftBar.FILE))
  92. self.new_data_action.setToolTip(tr("Button", "New Data Plot") + "\n(Ctrl+D/O)")
  93. #By Timo on 26.11.2021
  94. #Michele wants buttons for operation modes in the toolbar
  95. #so here we go.
  96. #Since most of these buttons will directly communicate with the board,
  97. #the creation and functionality setup of these buttons does not belong
  98. #here, where we're only doing layout. I will move the creation of the
  99. #Mode Toolbar to it's own function in a separate file, to better
  100. #seperate these concerns
  101. self.modeToolbar = ModeButtonsToolbar.ModeSelectToolbar(self)
  102. self.toolbarHBox = QtGui.QHBoxLayout()
  103. self.toolbarHBox.addWidget(self.toolbar)
  104. self.toolbarHBox.addWidget(self.modeToolbar)
  105. self.layout.addLayout(self.toolbarHBox)
  106. # ------[ End Tool Bar ]-----
  107. self.splitter.addWidget(self.leftBar)
  108. self.splitter.addWidget(self.area)
  109. self.layout.addWidget(self.splitter)
  110. self.evaluate_registered_widgets()
  111. self.evaluate_registered_widget_functions()
  112. #Automatically open a Live Plot on load complete
  113. #This is purely for user convenience
  114. self.leftBar.add_plot(LeftBar.LIVE)
  115. def addToolbarButton(self, icon, text, target, shortcut=None):
  116. """
  117. Add a toolbar button.
  118. :param icon: (QIcon) The icon to show on the toolbar button
  119. :param text: (str) tooltip for this button
  120. :param target: (callable) The function to call upon press on button
  121. :param shortcut: (str) Keyboard shortcut to call target
  122. :return: -
  123. """
  124. na = QtGui.QAction(icon, text, self)
  125. if shortcut:
  126. na.setShortcut(shortcut)
  127. na.setToolTip(text + "\n("+shortcut+")")
  128. self.toolbar.addAction(na)
  129. na.triggered.connect(target)
  130. def evaluate_registered_widgets(self):
  131. """
  132. Evaluate all the registered widgets and add a toolbar button for those
  133. :return:
  134. """
  135. for f in kcgw.get_registered_widgets():
  136. self.addToolbarButton(*f)
  137. def evaluate_registered_widget_functions(self):
  138. """
  139. Evaluate the functions that are registered
  140. Those are in general functions that have to be called after widgets are created
  141. :return:
  142. """
  143. for f in kcgw.get_registered_widget_functions():
  144. f()