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