OpenStructure
toolbar_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, QtGui
25 
26 class ToolBarOptionsWidget(QtWidgets.QWidget):
27  """QWidget with a ToolBar and a show area.
28 
29  This abstract QWidget has a toolbar and a show area. Whenever a button of the tool bar is pressed, the Widget corresponding to the button's value is shown in the show area.
30  """
31  def __init__(self, parent=None):
32  QtWidgets.QWidget.__init__(self, parent)
33 
34  #Setup ui_
35  self.parent_parent_=parent
36  self.resize(400, 300)
37  self.setMinimumSize(QtCore.QSize(250, 200))
38  self.gridLayoutgridLayout = QtWidgets.QGridLayout(self)
39  self.gridLayoutgridLayout.setHorizontalSpacing(0)
40  self.gridLayoutgridLayout.setVerticalSpacing(0)
41  self.gridLayoutgridLayout.setContentsMargins(0,0,0,0)
42  self.gridLayoutgridLayout.setSpacing(0)
43  self.tool_bar_tool_bar_ = QtWidgets.QToolBar(self)
44  self.tool_bar_tool_bar_.setIconSize(QtCore.QSize(16, 16))
45  self.gridLayoutgridLayout.addWidget(self.tool_bar_tool_bar_, 0, 0, 1, 1)
46  self.stackedWidgetstackedWidget = QtWidgets.QStackedWidget(self)
47  self.gridLayoutgridLayout.addWidget(self.stackedWidgetstackedWidget, 1, 0, 1, 1)
48 
49  self.current_action_current_action_ = None
50  self.actions_actions_ = list()
51 
52  self.tool_bar_tool_bar_.actionTriggered.connect(self.ChangeSelectedItemChangeSelectedItem)
53 
54  self.setEnabledsetEnabled(False)
55 
56  self.UpdateUpdate()
57 
58  def Update(self):
59  """Updates the active widget of the show area.
60 
61  This method calls the Update method of the active widget.
62  """
63  self.setEnabledsetEnabled(True)
64  widget = self.__GetCurrentWidget__GetCurrentWidget()
65  if hasattr(widget, "Update"):
66  widget.Update()
67 
68 
69  def AddWidget(self, ident, widget, text=None):
70  """Adds a Widget to this Options Widget.
71 
72  The Widget must have a identifier. If another Widget has the same identifier,
73  the old widget will be removed and the new widget gets the identifier.
74  Returns True, if widget is added. Otherwise it returns False
75  """
76  if isinstance(widget, QtWidgets.QWidget) and ident is not None:
77  if text is not None:
78  string = text
79  elif hasattr(widget, "GetText"):
80  string = widget.GetText()
81  else:
82  string = ident
83 
84  self.stackedWidgetstackedWidget.addWidget(widget)
85  action = self.tool_bar_tool_bar_.addAction(ident)
86  action.setIcon(QtGui.QIcon(ident))
87  action.setToolTip(string)
88  pair = ident, widget
89  action.setData(QtCore.QVariant(pair))
90  action.setCheckable(True);
91  if(len(self.actions_actions_) == 0):
92  self.ChangeSelectedItemChangeSelectedItem(action)
93  self.actions_actions_.append(action)
94  return True
95  return False
96 
97  def OnComboChange(self, item):
98  """This abstract method is called whenever the View is updated.
99 
100  This abstract method must be implemented by all subclasses.
101  It can be used to do something ;-) whenever the combobox changes its value.
102  """
103  raise NotImplementedError("Subclasses must define OnComboChange()")
104 
105  def DoResize(self):
106  item = self.__GetCurrentWidget__GetCurrentWidget()
107  width = 0
108  height = 0
109  if(hasattr(item,"minimumHeight")):
110  height=item.minimumHeight()
111  if(hasattr(item,"minimumWidth")):
112  width=item.minimumWidth()
113  self.setMinimumSize(width,self.tool_bar_tool_bar_.height()+height)
114  if(hasattr(self.parent_parent_,"DoResize")):
115  self.parent_parent_.DoResize()
116 
117  def ChangeSelectedItem(self, action):
118  """Change Current Selected Item.
119 
120  Shows the widget which corresponds to the action in the show area.
121  """
122  if(self.current_action_current_action_ != None):
123  self.current_action_current_action_.setChecked(False)
124  else:
125  self.current_action_current_action_ = action
126  widget = action.data()[1]
127  self.stackedWidgetstackedWidget.setCurrentWidget(widget)
128  if hasattr(widget, "Update"):
129  widget.Update()
130  if(self.current_action_current_action_ == action):
131  self.current_action_current_action_.setChecked(True)
132  else:
133  self.current_action_current_action_=action
134  self.OnComboChangeOnComboChange(widget)
135  #Private Methods
136  def __GetCurrentWidget(self):
137  return self.stackedWidgetstackedWidget.currentWidget();
138 
139  #Overwritten Methods
140  def setEnabled(self, bool):
141  QtWidgets.QWidget.setEnabled(self, bool)
142