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