File: combo_options_widget.py

package info (click to toggle)
openstructure 2.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 205,228 kB
  • sloc: cpp: 188,129; python: 35,361; ansic: 34,298; fortran: 3,275; sh: 286; xml: 146; makefile: 29
file content (153 lines) | stat: -rw-r--r-- 5,421 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#------------------------------------------------------------------------------
# This file is part of the OpenStructure project <www.openstructure.org>
#
# Copyright (C) 2008-2020 by the OpenStructure authors
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3.0 of the License, or (at your option)
# any later version.
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#------------------------------------------------------------------------------
# -*- coding: utf-8 -*-

import sys
from ost import gui
from ost import gfx
from PyQt5 import QtCore, QtWidgets

class ComboOptionsWidget(QtWidgets.QWidget):
  """QWidget with a Combobox and a show area.
    
   This abstract QWidget has a Combobox and a show area. Whenever the value of 
   the Combobox changes, the Widget corresponding to the Combobox's value is 
   shown in the show area.  
  """
  def __init__(self, parent=None):
    QtWidgets.QWidget.__init__(self, parent)
    #Setup ui_
    self.parent_ = parent
    self.grid_layout_ = QtWidgets.QGridLayout(self)
    self.grid_layout_.setHorizontalSpacing(0)
    self.grid_layout_.setVerticalSpacing(0)
    self.grid_layout_.setContentsMargins(0,0,0,0)
    self.combo_box_ = QtWidgets.QComboBox(self)
    self.grid_layout_.addWidget(self.combo_box_, 0, 0, 1, 1)
    self.stacked_widget_ = QtWidgets.QStackedWidget(self)
    self.grid_layout_.addWidget(self.stacked_widget_, 1, 0, 1, 1)

    self.__UpdateView(self.combo_box_.currentIndex())
    
    self.combo_box_.activated.connect(self.__UpdateView)
    
    self.setEnabled(False)
       
    self.Update()
    
  def Update(self):
    """Updates the ComboOptionsWidget and the active widget of the show area.
    
     This method calls the Update method of the active widget. 
    """
    self.__GetCurrentPair()[1].Update()
    
  def AddWidget(self, ident, widget):
    """Adds a Widget to this Options Widget.
    
     The Widget must have a identifier. If another Widget has the same identifier,
     the old widget will be removed and the new widget gets the identifier.
     Returns True, if widget is added. Otherwise it returns False
    """
    if isinstance(widget, QtWidgets.QWidget) and ident is not None:
      if hasattr(widget, "GetText"):
        string = widget.GetText()
      else:
        string = ident
      
      self.RemoveWidget(ident)
      self.stacked_widget_.addWidget(widget)
      qpair = ident, widget
      self.combo_box_.addItem(string, QtCore.QVariant(qpair))
      return True
    return False
  
  def RemoveWidget(self,ident):
    index = self.__GetIndex(ident)
    if(index >= 0):
      self.stacked_widget_.removeWidget(self.combo_box_.itemData(index)[1])
      self.combo_box_.removeItem(index)
  
  def OnComboChange(self, item):
    """This abstract method is called whenever the View is updated.
    
     This abstract method must be implemented by all subclasses. 
     It can be used to do something ;-) whenever the combobox changes its value.
    """
    raise NotImplementedError("Subclasses must define OnComboChange()")

  def OnActivate(self, item):
    return self.OnComboChange(self, item)
    
  def ChangeSelectedItem(self, ident):
    """
    Change Current Selected Item.
    
    Shows the widget which corresponds to the ident in the show area. If ident 
    is not valid, nothing happens.
    """
    i = self.__GetIndex(ident)
    if(i>=0) and self.combo_box_.currentIndex() != i:
      self.combo_box_.setCurrentIndex(i)
      if (self.combo_box_.count() > 0):
        pair = self.__GetCurrentPair()
        self.stacked_widget_.setCurrentWidget(pair[1])
        self.OnActivate(pair[1])
  
  def GetCurrentWidget(self):
    if(self.combo_box_.currentIndex() >= 0):
      return self.__GetCurrentPair()[1]
    return None
  
  def DoResize(self):
    item = self.GetCurrentWidget()
    width = 0
    height = 0
    if(hasattr(item,"minimumHeight")):
      height=item.minimumHeight()
    if(hasattr(item,"minimumWidth")):
      width=item.minimumWidth()
    self.setMinimumSize(width,40+height)
    if(hasattr(self.parent_,"DoResize")):
      self.parent_.DoResize()
  
  #Private Methods
  def __UpdateView(self, item):
    if (self.combo_box_.count() > 0):
      pair = self.__GetCurrentPair()
      self.stacked_widget_.setCurrentWidget(pair[1])
      self.OnComboChange(pair[1])

  def __GetIndex(self, ident):
    for i in range(self.combo_box_.count()):
      pair = self.combo_box_.itemData(i)
      if ident == pair[0]:
        return i
    return -1
  
  def __GetCurrentPair(self):
      current_index = self.combo_box_.currentIndex()
      return self.combo_box_.itemData(current_index)

  #Overwritten Methods
  def setEnabled(self, bool):
    QtWidgets.QWidget.setEnabled(self, bool)
    for i in range(self.combo_box_.count()):
        pair = self.combo_box_.itemData(i)
        pair[1].setEnabled(bool)