MultiWidget.py 6.1 KB

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