OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
map_level_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 import math
21 from ost import gui
22 from ost import gfx
23 try:
24  from ost import img
25  _img_present=True
26 except ImportError:
27  _img_present=False
28  pass
29 from PyQt4 import QtCore, QtGui
30 
31 from preset_widget import PresetWidget
32 
33 class AdditionalSettingsWidget(QtGui.QStackedWidget):
34  def __init__(self, parent=None):
35  QtGui.QStackedWidget.__init__(self, parent)
37  self.preset_widget_ = PresetWidget(self)
38  self.addWidget(self.preset_widget_);
39  self.addWidget(self.map_widget_);
40  self.setContentsMargins(0,0,0,0)
41  self.setMinimumSize(self.preset_widget_.minimumSize())
42 
43  def Update(self):
44  self.setEnabled(True)
45  scene_selection = gui.SceneSelection.Instance()
46  all_img = True
47  all_entity = True
48  for i in range(0,scene_selection.GetActiveNodeCount()):
49  node = scene_selection.GetActiveNode(i)
50  if not (isinstance(node, gfx.Entity) or isinstance(node, gfx.Surface)):
51  all_entity = False
52  if (not _img_present) or (not isinstance(node, gfx.MapIso)):
53  all_img = False
54  if all_img:
55  self.map_widget_.Update()
56  self.setMinimumSize(self.map_widget_.minimumSize())
57  self.resize(self.map_widget_.minimumSize())
58  self.setMinimumSize(self.map_widget_.minimumSize())
59  self.setCurrentWidget(self.map_widget_)
60  elif all_entity:
61  self.preset_widget_.Update()
62  self.setMinimumSize(self.preset_widget_.minimumSize())
63  self.resize(self.preset_widget_.minimumSize())
64  self.setMinimumSize(self.preset_widget_.minimumSize())
65  self.setCurrentWidget(self.preset_widget_)
66  else:
67  self.setEnabled(False)
68 
69 #Map Level Widget
70 class MapLevelWidget(QtGui.QWidget):
71  def __init__(self, parent=None):
72  QtGui.QWidget.__init__(self, parent)
73 
74  #Create Ui elements
75  self.map_level_label_ = QtGui.QLabel("Map Contour Level")
76  self.font = self.map_level_label_.font()
77  self.font.setBold(True)
78 
80 
81  self.level_spinbox_ = QtGui.QDoubleSpinBox()
82  self.level_spinbox_.setDecimals(3)
83  self.level_spinbox_.setSingleStep(0.05)
84 
85  grid = QtGui.QGridLayout()
86  grid.setContentsMargins(0,5,0,0)
87  grid.addWidget(self.level_preview_, 0, 0, 1, 4)
88  grid.addWidget(self.map_level_label_, 1, 0, 1, 3)
89  grid.addWidget(self.level_spinbox_,1,3,1,1)
90  grid.setRowStretch(3, 1)
91  self.setLayout(grid)
92 
93  QtCore.QObject.connect(self.level_preview_, QtCore.SIGNAL("levelUpdated"), self.UpdateLevel)
94  QtCore.QObject.connect(self.level_preview_, QtCore.SIGNAL("levelModified"), self.ModifySpinBox)
95  QtCore.QObject.connect(self.level_spinbox_, QtCore.SIGNAL("valueChanged(double)"), self.UpdateLevel)
96 
97  self.setMinimumSize(250,200)
98 
99  def Update(self):
100  scene_selection = gui.SceneSelection.Instance()
101  if(scene_selection.GetActiveNodeCount()==1):
102  node = scene_selection.GetActiveNode(0)
103  if _img_present and isinstance(node, gfx.MapIso):
104  try:
105  self.level_preview_.SetBins(node.GetHistogram())
106  self.level_preview_.SetMinimum(node.GetMinLevel())
107  self.level_spinbox_.setMinimum(node.GetMinLevel())
108  self.level_preview_.SetMaximum(node.GetMaxLevel())
109  self.level_spinbox_.setMaximum(node.GetMaxLevel())
110  self.level_preview_.SetLevel(node.GetLevel())
111  self.level_spinbox_.show()
112  self.font.setBold(True)
113  self.map_level_label_.setText("Map Contour Level")
114  self.setEnabled(True)
115  except UserWarning:
116  self.font.setBold(False)
117  self.map_level_label_.setText("Map uniformly filled with level %s"%node.GetLevel())
118  self.level_spinbox_.hide()
119  self.setEnabled(False)
120  else:
121  self.setEnabled(False)
122  else:
123  self.setEnabled(False)
124 
125  def UpdateLevel(self, level):
126  scene_selection = gui.SceneSelection.Instance()
127  if(scene_selection.GetActiveNodeCount()==1):
128  node = scene_selection.GetActiveNode(0)
129  node.SetLevel(level)
130 
131  def ModifySpinBox(self, level):
132  QtCore.QObject.disconnect(self.level_spinbox_, QtCore.SIGNAL("valueChanged(double)"), self.UpdateLevel)
133  self.level_spinbox_.setValue(level)
134  QtCore.QObject.connect(self.level_spinbox_, QtCore.SIGNAL("valueChanged(double)"), self.UpdateLevel)
135 
136 #Level Preview
137 class LevelPreview(QtGui.QWidget):
138  def __init__(self, parent=None):
139  QtGui.QWidget.__init__(self, parent)
140 
141  #Defaults
142  self.border_offset_ = 3
143  self.preview_height_ = 150
144  QtGui.QWidget.__init__(self, parent)
145 
146  #Ui
147  self.setMinimumSize(0, self.preview_height_ + 4)
148 
149  self.bins_ = None
150  self.level_ = 0
151  self.minimum_ = 0
152  self.maximum_ = 0
153 
154  self.paint_mouse_=False
155 
156  def SetBins(self, bins):
157  self.bins_ = bins
158  self.update()
159 
160  def SetMaximum(self, max):
161  self.maximum_ = max
162 
163  def SetMinimum(self, min):
164  self.minimum_ = min
165 
166  def SetLevel(self, level):
167  self.level_ = level
168 
169  def GetLevel(self):
170  return self.level_
171 
172  def paintEvent(self, event):
173  if self.isEnabled() and self.bins_ is not None:
174  painter = QtGui.QPainter()
175  if painter.begin(self):
176  self.PaintBackground(painter)
177  self.PaintBins(painter)
178  self.PaintLevel(painter)
179  if(self.paint_mouse_):
180  self.PaintMouse(painter)
181  painter.end()
182 
183  def PaintBackground(self,painter):
184  size = self.size()
185  painter.setBrush(QtCore.Qt.white)
186  painter.setPen(QtCore.Qt.white)
187  painter.drawRect(self.border_offset_,
188  self.border_offset_,
189  size.width() - 2 * self.border_offset_,
190  self.preview_height_)
191 
192  def PaintBins(self,painter):
193  size = self.size()
194  bin_cnt = len(self.bins_)
195  bin_width = (size.width()-2* self.border_offset_) / float(bin_cnt)
196  max=0
197  for b in self.bins_:
198  if(b>max):
199  max = b
200  max = math.log(max)
201  if(max > 0):
202  painter.setBrush(QtCore.Qt.black)
203  painter.setPen(QtCore.Qt.black)
204  for i in range(0,bin_cnt):
205  bin_height = self.bins_[i]
206  if(bin_height>0):
207  bin_height = math.floor((math.log(bin_height)/max)*(self.preview_height_-2*(self.border_offset_)))
208  painter.drawRect(self.border_offset_ + (i*bin_width),
209  self.preview_height_ - bin_height,
210  bin_width,
211  bin_height)
212 
213  def PaintLevel(self,painter):
214  size = self.size()
215  width = size.width()-(2* self.border_offset_)
216  tot_len = self.maximum_-self.minimum_
217  if(tot_len>0):
218  cur_len = self.level_-self.minimum_
219  painter.setBrush(QtCore.Qt.red)
220  painter.setPen(QtCore.Qt.red)
221  painter.drawRect((width / tot_len) * cur_len,
222  self.border_offset_,
223  1,
224  self.preview_height_)
225 
226  def PaintMouse(self,painter):
227  size = self.size()
228  width = size.width()-(2* self.border_offset_)
229  painter.setBrush(QtCore.Qt.gray)
230  painter.setPen(QtCore.Qt.gray)
231  pos=self.mapFromGlobal(QtGui.QCursor.pos())
232  painter.drawRect(pos.x(),
233  self.border_offset_,
234  1,
235  self.preview_height_)
236 
237  def mouseReleaseEvent(self, event):
238  self.paint_mouse_=False
239  size = self.size()
240  width = size.width()-(2* self.border_offset_)
241  tot_len = self.maximum_-self.minimum_
242  self.level_ = self.minimum_ + float(event.x())/width * tot_len
243  self.update()
244  self.emit(QtCore.SIGNAL("levelUpdated"),(self.level_))
245 
246  def mousePressEvent(self,event):
247  self.paint_mouse_=True
248 
249  def mouseMoveEvent(self, event):
250  size = self.size()
251  width = size.width()-(2* self.border_offset_)
252  tot_len = self.maximum_-self.minimum_
253  level = self.minimum_ + float(event.x())/width * tot_len
254  self.emit(QtCore.SIGNAL("levelModified"),(level))
255  self.update()
graphical rendering of mol::EntityHandle entites
Definition: entity.hh:63
isocontour rendering for 3D image data
Definition: map_iso.hh:52