123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- """
- This is the container widget for multiple subwindows
- """
- from PyQt4 import QtGui, QtCore
- from . import kcgwidget as kcgw
- from . import LeftBar
- from .globals import glob as global_objects
- from .. import config
- tr = kcgw.tr
- class WidgetTypeError(Exception):
- """
- Simple error that describes when a wrong window type gets added
- """
- pass
- class MDIArea(kcgw.KCGWidgets):
- """
- The MDI Area used by Multiwidget
- """
- def __init__(self, parent):
- super(MDIArea, self).__init__("MDIArea")
- self.parent = parent
- self.layout = QtGui.QHBoxLayout()
- self.layout.setContentsMargins(0, 0, 0, 0)
- self.setLayout(self.layout)
- self.widgets = {}
- self.area = QtGui.QMdiArea()
- self.area.setFrameStyle(self.area.StyledPanel | self.area.Sunken)
- self.layout.addWidget(self.area)
- brush = QtGui.QBrush(QtGui.QColor('white'))
- self.area.setBackground(brush)
- def newWidget(self, widget, name, unique_id, widget_type, minSize=False):
- """
- Add a new Widget to the MDIArea
- :param widget: (subclass of QMdiSubWindow) The widget to show
- :param name: (str) name of the window
- :param unique_id: (int) unique id of the window
- :param widget_type: (int) the type of this window
- :param minSize: (bool) whether to shrink the window to minimum size upon creation
- :return: -
- """
- if not isinstance(widget, kcgw.KCGWidgets):
- raise WidgetTypeError("Newly Created Widget is of type " +
- type(widget).__name__ +
- ". Expected class derived of " +
- kcgw.KCGWidgets.__name__ + ".")
- subWidget = kcgw.KCGSubWidget(name=name, unique_id=unique_id, typ=widget_type, minSize=minSize)
- subWidget.setAttribute(QtCore.Qt.WA_DeleteOnClose)
- widget.setParent(subWidget)
- if minSize:
- subWidget.resize(widget.minimumSizeHint()*1.2)
- widget.closeSignal.connect(subWidget.close)
- subWidget.setWidget(widget)
- self.area.addSubWindow(subWidget)
- subWidget.show()
- self.widgets[unique_id] = widget
- # 888b d888 888 888 d8b 888 888 d8b 888 888
- # 8888b d8888 888 888 Y8P 888 o 888 Y8P 888 888
- # 88888b.d88888 888 888 888 d8b 888 888 888
- # 888Y88888P888 888 888 888 888888 888 888 d888b 888 888 .d88888 .d88b. .d88b. 888888
- # 888 Y888P 888 888 888 888 888 888 888d88888b888 888 d88" 888 d88P"88b d8P Y8b 888
- # 888 Y8P 888 888 888 888 888 888 88888P Y88888 888 888 888 888 888 88888888 888
- # 888 " 888 Y88b 888 888 Y88b. 888 8888P Y8888 888 Y88b 888 Y88b 888 Y8b. Y88b.
- # 888 888 "Y88888 888 "Y888 888 888P Y888 888 "Y88888 "Y88888 "Y8888 "Y888
- # 888
- # Y8b d88P
- # "Y88P"
- class MultiWidget(QtGui.QWidget):
- """
- The Widget used as Multiwidget. This is the main View during operation with KCG.
- """
- def __init__(self):
- super(MultiWidget, self).__init__()
- self.layout = QtGui.QVBoxLayout()
- self.setLayout(self.layout)
- self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
- self.leftBar = LeftBar.LeftBar(self)
- self.area = MDIArea(self)
- self.area.setObjectName("MDIArea")
- self.toolbar = QtGui.QToolBar()
- global_objects.set_global('area', self.area)
- # --------[ ToolBar ]--------
- self.new_live_action = QtGui.QAction(QtGui.QIcon(config.icon_path(config.newPlotLiveIcon)), tr("Button", "New Live Plot"), self)
- self.new_live_action.setShortcut("Ctrl+L")
- self.toolbar.addAction(self.new_live_action)
- self.new_live_action.triggered.connect(lambda: self.leftBar.add_plot(LeftBar.LIVE))
- self.new_live_action.setToolTip(tr("Button", "New Live Plot") + "\n(Ctrl+L)")
- self.new_data_action = QtGui.QAction(QtGui.QIcon(config.icon_path(config.newPlotDataIcon)), tr("Button", "New Data Plot"), self)
- self.new_data_action.setShortcuts(["Ctrl+D", "Ctrl+O"])
- self.toolbar.addAction(self.new_data_action)
- self.new_data_action.triggered.connect(lambda: self.leftBar.add_plot(LeftBar.FILE))
- self.new_data_action.setToolTip(tr("Button", "New Data Plot") + "\n(Ctrl+D/O)")
- self.layout.addWidget(self.toolbar)
- # ------[ End Tool Bar ]-----
- self.splitter.addWidget(self.leftBar)
- self.splitter.addWidget(self.area)
- self.layout.addWidget(self.splitter)
- self.evaluate_registered_widgets()
- self.evaluate_registered_widget_functions()
- #Automatically open a Live Plot on load complete
- #This is purely for user convenience
- self.leftBar.add_plot(LeftBar.LIVE)
- def addToolbarButton(self, icon, text, target, shortcut=None):
- """
- Add a toolbar button.
- :param icon: (QIcon) The icon to show on the toolbar button
- :param text: (str) tooltip for this button
- :param target: (callable) The function to call upon press on button
- :param shortcut: (str) Keyboard shortcut to call target
- :return: -
- """
- na = QtGui.QAction(icon, text, self)
- if shortcut:
- na.setShortcut(shortcut)
- na.setToolTip(text + "\n("+shortcut+")")
- self.toolbar.addAction(na)
- na.triggered.connect(target)
- def evaluate_registered_widgets(self):
- """
- Evaluate all the registered widgets and add a toolbar button for those
- :return:
- """
- for f in kcgw.get_registered_widgets():
- self.addToolbarButton(*f)
- def evaluate_registered_widget_functions(self):
- """
- Evaluate the functions that are registered
- Those are in general functions that have to be called after widgets are created
- :return:
- """
- for f in kcgw.get_registered_widget_functions():
- f()
|