OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
combo_options_widget.py
Go to the documentation of this file.
1 #------------------------------------------------------------------------------
2 # This file is part of the OpenStructure project <www.openstructure.org>
3 #
4 # Copyright (C) 2008-2011 by the OpenStructure authors
5 #
6 # This library is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation; either version 3.0 of the License, or (at your option)
9 # any later version.
10 # This library is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this library; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #------------------------------------------------------------------------------
19 # -*- coding: utf-8 -*-
20 
21 import sys
22 from ost import gui
23 from ost import gfx
24 from PyQt4 import QtCore, QtGui
25 
26 class ComboOptionsWidget(QtGui.QWidget):
27  """QWidget with a Combobox and a show area.
28 
29  This abstract QWidget has a Combobox and a show area. Whenever the value of
30  the Combobox changes, the Widget corresponding to the Combobox's value is
31  shown in the show area.
32  """
33  def __init__(self, parent=None):
34  QtGui.QWidget.__init__(self, parent)
35  #Setup ui_
36  self.parent_ = parent
37  self.grid_layout_ = QtGui.QGridLayout(self)
38  self.grid_layout_.setHorizontalSpacing(0)
39  self.grid_layout_.setVerticalSpacing(0)
40  self.grid_layout_.setContentsMargins(0,0,0,0)
41  self.combo_box_ = QtGui.QComboBox(self)
42  self.grid_layout_.addWidget(self.combo_box_, 0, 0, 1, 1)
43  self.stacked_widget_ = QtGui.QStackedWidget(self)
44  self.grid_layout_.addWidget(self.stacked_widget_, 1, 0, 1, 1)
45 
46  self.__UpdateView(self.combo_box_.currentIndex())
47 
48  QtCore.QObject.connect(self.combo_box_, QtCore.SIGNAL("activated(int)"),
49  self.__UpdateView)
50 
51  self.setEnabled(False)
52 
53  self.Update()
54 
55  def Update(self):
56  """Updates the ComboOptionsWidget and the active widget of the show area.
57 
58  This method calls the Update method of the active widget.
59  """
60  self.__GetCurrentPair()[1].Update()
61 
62  def AddWidget(self, ident, widget):
63  """Adds a Widget to this Options Widget.
64 
65  The Widget must have a identifier. If another Widget has the same identifier,
66  the old widget will be removed and the new widget gets the identifier.
67  Returns True, if widget is added. Otherwise it returns False
68  """
69  if isinstance(widget, QtGui.QWidget) and ident is not None:
70  if hasattr(widget, "GetText"):
71  string = QtCore.QString(widget.GetText())
72  else:
73  string = QtCore.QString(ident)
74 
75  self.RemoveWidget(ident)
76  self.stacked_widget_.addWidget(widget)
77  qpair = ident, widget
78  self.combo_box_.addItem(string, QtCore.QVariant(qpair))
79  return True
80  return False
81 
82  def RemoveWidget(self,ident):
83  index = self.__GetIndex(ident)
84  if(index >= 0):
85  self.stacked_widget_.removeWidget(self.combo_box_.itemData(index).toPyObject()[1])
86  self.combo_box_.removeItem(index)
87 
88  def OnComboChange(self, item):
89  """This abstract method is called whenever the View is updated.
90 
91  This abstract method must be implemented by all subclasses.
92  It can be used to do something ;-) whenever the combobox changes its value.
93  """
94  raise NotImplementedError, "Subclasses must define OnComboChange()"
95 
96  def OnActivate(self, item):
97  return self.OnComboChange(self, item)
98 
99  def ChangeSelectedItem(self, ident):
100  """
101  Change Current Selected Item.
102 
103  Shows the widget which corresponds to the ident in the show area. If ident
104  is not valid, nothing happens.
105  """
106  i = self.__GetIndex(ident)
107  if(i>=0) and self.combo_box_.currentIndex() != i:
108  self.combo_box_.setCurrentIndex(i)
109  if (self.combo_box_.count() > 0):
110  pair = self.__GetCurrentPair()
111  self.stacked_widget_.setCurrentWidget(pair[1])
112  self.OnActivate(pair[1])
113 
114  def GetCurrentWidget(self):
115  if(self.combo_box_.currentIndex() >= 0):
116  return self.__GetCurrentPair()[1]
117  return None
118 
119  def DoResize(self):
120  item = self.GetCurrentWidget()
121  width = 0
122  height = 0
123  if(hasattr(item,"minimumHeight")):
124  height=item.minimumHeight()
125  if(hasattr(item,"minimumWidth")):
126  width=item.minimumWidth()
127  self.setMinimumSize(width,40+height)
128  if(hasattr(self.parent_,"DoResize")):
129  self.parent_.DoResize()
130 
131  #Private Methods
132  def __UpdateView(self, item):
133  if (self.combo_box_.count() > 0):
134  pair = self.__GetCurrentPair()
135  self.stacked_widget_.setCurrentWidget(pair[1])
136  self.OnComboChange(pair[1])
137 
138  def __GetIndex(self, ident):
139  for i in range(self.combo_box_.count()):
140  pair = self.combo_box_.itemData(i).toPyObject()
141  if ident == pair[0]:
142  return i
143  return -1
144 
145  def __GetCurrentPair(self):
146  current_index = self.combo_box_.currentIndex()
147  return self.combo_box_.itemData(current_index).toPyObject()
148 
149  #Overwritten Methods
150  def setEnabled(self, bool):
151  QtGui.QWidget.setEnabled(self, bool)
152  for i in range(self.combo_box_.count()):
153  pair = self.combo_box_.itemData(i).toPyObject()
154  pair[1].setEnabled(bool)