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 ComboOptionsWidget(QtGui.QWidget):
00027 """QWidget with a Combobox and a show area.
00028
00029 This abstract QWidget has a Combobox and a show area. Whenever the value of
00030 the Combobox changes, the Widget corresponding to the Combobox's value is
00031 shown in the show area.
00032 """
00033 def __init__(self, parent=None):
00034 QtGui.QWidget.__init__(self, parent)
00035
00036 self.parent_ = parent
00037 self.grid_layout_ = QtGui.QGridLayout(self)
00038 self.grid_layout_.setHorizontalSpacing(0)
00039 self.grid_layout_.setVerticalSpacing(0)
00040 self.grid_layout_.setContentsMargins(0,0,0,0)
00041 self.combo_box_ = QtGui.QComboBox(self)
00042 self.grid_layout_.addWidget(self.combo_box_, 0, 0, 1, 1)
00043 self.stacked_widget_ = QtGui.QStackedWidget(self)
00044 self.grid_layout_.addWidget(self.stacked_widget_, 1, 0, 1, 1)
00045
00046 self.__UpdateView(self.combo_box_.currentIndex())
00047
00048 QtCore.QObject.connect(self.combo_box_, QtCore.SIGNAL("activated(int)"), self.__UpdateView)
00049
00050 self.setEnabled(False)
00051
00052 self.Update()
00053
00054 def Update(self):
00055 """Updates the ComboOptionsWidget and the active widget of the show area.
00056
00057 This method calls the Update method of the active widget.
00058 """
00059 self.__GetCurrentPair()[1].Update()
00060
00061 def AddWidget(self, ident, widget):
00062 """Adds a Widget to this Options Widget.
00063
00064 The Widget must have a identifier. If another Widget has the same identifier,
00065 the old widget will be removed and the new widget gets the identifier.
00066 Returns True, if widget is added. Otherwise it returns False
00067 """
00068 if isinstance(widget, QtGui.QWidget) and ident is not None:
00069 if hasattr(widget, "GetText"):
00070 string = QtCore.QString(widget.GetText())
00071 else:
00072 string = QtCore.QString(ident)
00073
00074 self.RemoveWidget(ident)
00075 self.stacked_widget_.addWidget(widget)
00076 qpair = ident, widget
00077 self.combo_box_.addItem(string, QtCore.QVariant(qpair))
00078 return True
00079 return False
00080
00081 def RemoveWidget(self,ident):
00082 index = self.__GetIndex(ident)
00083 if(index >= 0):
00084 self.stacked_widget_.removeWidget(self.combo_box_.itemData(index).toPyObject()[1])
00085 self.combo_box_.removeItem(index)
00086
00087 def DoSomething(self, item):
00088 """This abstract method is called whenever the View is updated.
00089
00090 This abstract method must be implemented by all subclasses.
00091 It can be used to do something ;-) whenever the combobox changes its value.
00092 """
00093 raise NotImplementedError, "Subclasses must define DoSomething()"
00094
00095 def ChangeSelectedItem(self, ident):
00096 """Change Current Selected Item.
00097
00098 Shows the widget which corresponds to the ident in the show area. If ident is not valid, nothing happens.
00099 """
00100 i = self.__GetIndex(ident)
00101 if(i>=0) and self.combo_box_.currentIndex() != i:
00102 self.combo_box_.setCurrentIndex(i)
00103 self.__UpdateView(None)
00104
00105 def GetCurrentWidget(self):
00106 if(self.combo_box_.currentIndex() >= 0):
00107 return self.__GetCurrentPair()[1]
00108 return None
00109
00110 def DoResize(self):
00111 item = self.GetCurrentWidget()
00112 width = 0
00113 height = 0
00114 if(hasattr(item,"minimumHeight")):
00115 height=item.minimumHeight()
00116 if(hasattr(item,"minimumWidth")):
00117 width=item.minimumWidth()
00118 self.setMinimumSize(width,40+height)
00119 if(hasattr(self.parent_,"DoResize")):
00120 self.parent_.DoResize()
00121
00122
00123 def __UpdateView(self, item):
00124 if (self.combo_box_.count() > 0):
00125 pair = self.__GetCurrentPair()
00126 self.stacked_widget_.setCurrentWidget(pair[1])
00127 self.DoSomething(pair[1])
00128
00129 def __GetIndex(self, ident):
00130 for i in range(self.combo_box_.count()):
00131 pair = self.combo_box_.itemData(i).toPyObject()
00132 if ident == pair[0]:
00133 return i
00134 return -1
00135
00136 def __GetCurrentPair(self):
00137 current_index = self.combo_box_.currentIndex()
00138 return self.combo_box_.itemData(current_index).toPyObject()
00139
00140
00141 def setEnabled(self, bool):
00142 QtGui.QWidget.setEnabled(self, bool)
00143 for i in range(self.combo_box_.count()):
00144 pair = self.combo_box_.itemData(i).toPyObject()
00145 pair[1].setEnabled(bool)