OpenStructure
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-2020 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 PyQt5 import QtCore, QtWidgets
25 
26 class ComboOptionsWidget(QtWidgets.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  QtWidgets.QWidget.__init__(self, parent)
35  #Setup ui_
36  self.parent_parent_ = parent
37  self.grid_layout_grid_layout_ = QtWidgets.QGridLayout(self)
38  self.grid_layout_grid_layout_.setHorizontalSpacing(0)
39  self.grid_layout_grid_layout_.setVerticalSpacing(0)
40  self.grid_layout_grid_layout_.setContentsMargins(0,0,0,0)
41  self.combo_box_combo_box_ = QtWidgets.QComboBox(self)
42  self.grid_layout_grid_layout_.addWidget(self.combo_box_combo_box_, 0, 0, 1, 1)
43  self.stacked_widget_stacked_widget_ = QtWidgets.QStackedWidget(self)
44  self.grid_layout_grid_layout_.addWidget(self.stacked_widget_stacked_widget_, 1, 0, 1, 1)
45 
46  self.__UpdateView__UpdateView(self.combo_box_combo_box_.currentIndex())
47 
48  self.combo_box_combo_box_.activated.connect(self.__UpdateView__UpdateView)
49 
50  self.setEnabledsetEnabled(False)
51 
52  self.UpdateUpdate()
53 
54  def Update(self):
55  """Updates the ComboOptionsWidget and the active widget of the show area.
56 
57  This method calls the Update method of the active widget.
58  """
59  self.__GetCurrentPair__GetCurrentPair()[1].Update()
60 
61  def AddWidget(self, ident, widget):
62  """Adds a Widget to this Options Widget.
63 
64  The Widget must have a identifier. If another Widget has the same identifier,
65  the old widget will be removed and the new widget gets the identifier.
66  Returns True, if widget is added. Otherwise it returns False
67  """
68  if isinstance(widget, QtWidgets.QWidget) and ident is not None:
69  if hasattr(widget, "GetText"):
70  string = widget.GetText()
71  else:
72  string = ident
73 
74  self.RemoveWidgetRemoveWidget(ident)
75  self.stacked_widget_stacked_widget_.addWidget(widget)
76  qpair = ident, widget
77  self.combo_box_combo_box_.addItem(string, QtCore.QVariant(qpair))
78  return True
79  return False
80 
81  def RemoveWidget(self,ident):
82  index = self.__GetIndex__GetIndex(ident)
83  if(index >= 0):
84  self.stacked_widget_stacked_widget_.removeWidget(self.combo_box_combo_box_.itemData(index)[1])
85  self.combo_box_combo_box_.removeItem(index)
86 
87  def OnComboChange(self, item):
88  """This abstract method is called whenever the View is updated.
89 
90  This abstract method must be implemented by all subclasses.
91  It can be used to do something ;-) whenever the combobox changes its value.
92  """
93  raise NotImplementedError("Subclasses must define OnComboChange()")
94 
95  def OnActivate(self, item):
96  return self.OnComboChangeOnComboChange(self, item)
97 
98  def ChangeSelectedItem(self, ident):
99  """
100  Change Current Selected Item.
101 
102  Shows the widget which corresponds to the ident in the show area. If ident
103  is not valid, nothing happens.
104  """
105  i = self.__GetIndex__GetIndex(ident)
106  if(i>=0) and self.combo_box_combo_box_.currentIndex() != i:
107  self.combo_box_combo_box_.setCurrentIndex(i)
108  if (self.combo_box_combo_box_.count() > 0):
109  pair = self.__GetCurrentPair__GetCurrentPair()
110  self.stacked_widget_stacked_widget_.setCurrentWidget(pair[1])
111  self.OnActivateOnActivate(pair[1])
112 
113  def GetCurrentWidget(self):
114  if(self.combo_box_combo_box_.currentIndex() >= 0):
115  return self.__GetCurrentPair__GetCurrentPair()[1]
116  return None
117 
118  def DoResize(self):
119  item = self.GetCurrentWidgetGetCurrentWidget()
120  width = 0
121  height = 0
122  if(hasattr(item,"minimumHeight")):
123  height=item.minimumHeight()
124  if(hasattr(item,"minimumWidth")):
125  width=item.minimumWidth()
126  self.setMinimumSize(width,40+height)
127  if(hasattr(self.parent_parent_,"DoResize")):
128  self.parent_parent_.DoResize()
129 
130  #Private Methods
131  def __UpdateView(self, item):
132  if (self.combo_box_combo_box_.count() > 0):
133  pair = self.__GetCurrentPair__GetCurrentPair()
134  self.stacked_widget_stacked_widget_.setCurrentWidget(pair[1])
135  self.OnComboChangeOnComboChange(pair[1])
136 
137  def __GetIndex(self, ident):
138  for i in range(self.combo_box_combo_box_.count()):
139  pair = self.combo_box_combo_box_.itemData(i)
140  if ident == pair[0]:
141  return i
142  return -1
143 
144  def __GetCurrentPair(self):
145  current_index = self.combo_box_combo_box_.currentIndex()
146  return self.combo_box_combo_box_.itemData(current_index)
147 
148  #Overwritten Methods
149  def setEnabled(self, bool):
150  QtWidgets.QWidget.setEnabled(self, bool)
151  for i in range(self.combo_box_combo_box_.count()):
152  pair = self.combo_box_combo_box_.itemData(i)
153  pair[1].setEnabled(bool)