00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 import sys
00022 from ost import gui
00023 from ost import gfx
00024 from PyQt4 import QtCore, QtGui
00025
00026 class ToolBarOptionsWidget(QtGui.QWidget):
00027 """QWidget with a ToolBar and a show area.
00028
00029 This abstract QWidget has a toolbar and a show area. Whenever a button of the tool bar is pressed, the Widget corresponding to the button's value is shown in the show area.
00030 """
00031 def __init__(self, parent=None):
00032 QtGui.QWidget.__init__(self, parent)
00033
00034
00035 self.parent_=parent
00036 self.resize(400, 300)
00037 self.setMinimumSize(QtCore.QSize(250, 200))
00038 self.gridLayout = QtGui.QGridLayout(self)
00039 self.gridLayout.setHorizontalSpacing(0)
00040 self.gridLayout.setVerticalSpacing(0)
00041 self.gridLayout.setContentsMargins(0,0,0,0)
00042 self.gridLayout.setMargin(0)
00043 self.gridLayout.setSpacing(0)
00044 self.tool_bar_ = QtGui.QToolBar(self)
00045 self.tool_bar_.setIconSize(QtCore.QSize(16, 16))
00046 self.gridLayout.addWidget(self.tool_bar_, 0, 0, 1, 1)
00047 self.stackedWidget = QtGui.QStackedWidget(self)
00048 self.gridLayout.addWidget(self.stackedWidget, 1, 0, 1, 1)
00049
00050 self.current_action_ = None
00051 self.actions_ = list()
00052
00053 QtCore.QObject.connect(self.tool_bar_, QtCore.SIGNAL("actionTriggered(QAction*)"), self.ChangeSelectedItem)
00054
00055 self.setEnabled(False)
00056
00057 self.Update()
00058
00059 def Update(self):
00060 """Updates the active widget of the show area.
00061
00062 This method calls the Update method of the active widget.
00063 """
00064 self.setEnabled(True)
00065 widget = self.__GetCurrentWidget()
00066 if hasattr(widget, "Update"):
00067 widget.Update()
00068
00069
00070 def AddWidget(self, ident, widget, text=None):
00071 """Adds a Widget to this Options Widget.
00072
00073 The Widget must have a identifier. If another Widget has the same identifier,
00074 the old widget will be removed and the new widget gets the identifier.
00075 Returns True, if widget is added. Otherwise it returns False
00076 """
00077 if isinstance(widget, QtGui.QWidget) and ident is not None:
00078 if text is not None:
00079 string = QtCore.QString(text)
00080 elif hasattr(widget, "GetText"):
00081 string = QtCore.QString(widget.GetText())
00082 else:
00083 string = QtCore.QString(ident)
00084
00085 self.stackedWidget.addWidget(widget)
00086 action = self.tool_bar_.addAction(ident)
00087 action.setIcon(QtGui.QIcon(ident))
00088 action.setToolTip(string)
00089 pair = ident, widget
00090 action.setData(QtCore.QVariant(pair))
00091 action.setCheckable(True);
00092 if(len(self.actions_) == 0):
00093 self.ChangeSelectedItem(action)
00094 self.actions_.append(action)
00095 return True
00096 return False
00097
00098 def OnComboChange(self, item):
00099 """This abstract method is called whenever the View is updated.
00100
00101 This abstract method must be implemented by all subclasses.
00102 It can be used to do something ;-) whenever the combobox changes its value.
00103 """
00104 raise NotImplementedError, "Subclasses must define OnComboChange()"
00105
00106 def DoResize(self):
00107 item = self.__GetCurrentWidget()
00108 width = 0
00109 height = 0
00110 if(hasattr(item,"minimumHeight")):
00111 height=item.minimumHeight()
00112 if(hasattr(item,"minimumWidth")):
00113 width=item.minimumWidth()
00114 self.setMinimumSize(width,self.tool_bar_.height()+height)
00115 if(hasattr(self.parent_,"DoResize")):
00116 self.parent_.DoResize()
00117
00118 def ChangeSelectedItem(self, action):
00119 """Change Current Selected Item.
00120
00121 Shows the widget which corresponds to the action in the show area.
00122 """
00123 if(self.current_action_ != None):
00124 self.current_action_.setChecked(False)
00125 else:
00126 self.current_action_ = action
00127 widget = action.data().toPyObject()[1]
00128 self.stackedWidget.setCurrentWidget(widget)
00129 if hasattr(widget, "Update"):
00130 widget.Update()
00131 if(self.current_action_ == action):
00132 self.current_action_.setChecked(True)
00133 else:
00134 self.current_action_=action
00135 self.OnComboChange(widget)
00136
00137 def __GetCurrentWidget(self):
00138 return self.stackedWidget.currentWidget();
00139
00140
00141 def setEnabled(self, bool):
00142 QtGui.QWidget.setEnabled(self, bool)
00143