multiWidget.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """
  2. This is the container widget for multiple subwindows
  3. """
  4. from PyQt4 import QtGui, QtCore
  5. import kcgwidget as kcgw
  6. from leftbar import LeftBar
  7. import leftbar
  8. from globals import glob as global_objects
  9. from .. import config
  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. class MultiWidget(QtGui.QWidget):
  58. """
  59. The Widget used as Multiwidget. This is the main View during operation with KCG.
  60. """
  61. def __init__(self):
  62. super(MultiWidget, self).__init__()
  63. self.layout = QtGui.QVBoxLayout()
  64. self.setLayout(self.layout)
  65. self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
  66. self.leftBar = LeftBar(self)
  67. self.area = MDIArea(self)
  68. self.area.setObjectName("MDIArea")
  69. self.toolbar = QtGui.QToolBar()
  70. global_objects.set_global('area', self.area)
  71. # --------[ ToolBar ]--------
  72. self.new_live_action = QtGui.QAction(QtGui.QIcon(config.install_path + config.newPlotLiveIcon), tr("Button", "New Live Plot"), self)
  73. self.new_live_action.setShortcut("Ctrl+L")
  74. self.toolbar.addAction(self.new_live_action)
  75. self.new_live_action.triggered.connect(lambda: self.leftBar.add_plot(leftbar.LIVE))
  76. self.new_live_action.setToolTip(tr("Button", "New Live Plot") + "\n(Ctrl+L)")
  77. self.new_data_action = QtGui.QAction(QtGui.QIcon(config.install_path + config.newPlotDataIcon), tr("Button", "New Data Plot"), self)
  78. self.new_data_action.setShortcuts(["Ctrl+D", "Ctrl+O"])
  79. self.toolbar.addAction(self.new_data_action)
  80. self.new_data_action.triggered.connect(lambda: self.leftBar.add_plot(leftbar.FILE))
  81. self.new_data_action.setToolTip(tr("Button", "New Data Plot") + "\n(Ctrl+D/O)")
  82. self.layout.addWidget(self.toolbar)
  83. # ------[ End Tool Bar ]-----
  84. self.splitter.addWidget(self.leftBar)
  85. self.splitter.addWidget(self.area)
  86. self.layout.addWidget(self.splitter)
  87. self.evaluate_registered_widgets()
  88. self.evaluate_registered_widget_functions()
  89. def addToolbarButton(self, icon, text, target, shortcut=None):
  90. """
  91. Add a toolbar button.
  92. :param icon: (QIcon) The icon to show on the toolbar button
  93. :param text: (str) tooltip for this button
  94. :param target: (callable) The function to call upon press on button
  95. :param shortcut: (str) Keyboard shortcut to call target
  96. :return: -
  97. """
  98. na = QtGui.QAction(icon, text, self)
  99. if shortcut:
  100. na.setShortcut(shortcut)
  101. na.setToolTip(text + "\n("+shortcut+")")
  102. self.toolbar.addAction(na)
  103. na.triggered.connect(target)
  104. def evaluate_registered_widgets(self):
  105. """
  106. Evaluate all the registered widgets and add a toolbar button for those
  107. :return:
  108. """
  109. for f in kcgw.get_registered_widgets():
  110. self.addToolbarButton(*f)
  111. def evaluate_registered_widget_functions(self):
  112. """
  113. Evaluate the functions that are registered
  114. Those are in general functions that have to be called after widgets are created
  115. :return:
  116. """
  117. for f in kcgw.get_registered_widget_functions():
  118. f()