OpenStructure
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-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 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 PyQt5 import QtCore, QtWidgets
30 
31 from .preset_widget import PresetWidget
32 
33 class AdditionalSettingsWidget(QtWidgets.QStackedWidget):
34  def __init__(self, parent=None):
35  QtWidgets.QStackedWidget.__init__(self, parent)
36  self.map_widget_map_widget_ = MapLevelWidget(self)
37  self.preset_widget_preset_widget_ = PresetWidget(self)
38  self.addWidget(self.preset_widget_preset_widget_);
39  self.addWidget(self.map_widget_map_widget_);
40  self.setContentsMargins(0,0,0,0)
41  self.setMinimumSize(self.preset_widget_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_map_widget_.Update()
56  self.setMinimumSize(self.map_widget_map_widget_.minimumSize())
57  self.resize(self.map_widget_map_widget_.minimumSize())
58  self.setMinimumSize(self.map_widget_map_widget_.minimumSize())
59  self.setCurrentWidget(self.map_widget_map_widget_)
60  elif all_entity:
61  self.preset_widget_preset_widget_.Update()
62  self.setMinimumSize(self.preset_widget_preset_widget_.minimumSize())
63  self.resize(self.preset_widget_preset_widget_.minimumSize())
64  self.setMinimumSize(self.preset_widget_preset_widget_.minimumSize())
65  self.setCurrentWidget(self.preset_widget_preset_widget_)
66  else:
67  self.setEnabled(False)
68 
69 #Map Level Widget
70 class MapLevelWidget(QtWidgets.QWidget):
71  def __init__(self, parent=None):
72  QtWidgets.QWidget.__init__(self, parent)
73 
74  #Create Ui elements
75  self.map_level_label_map_level_label_ = QtWidgets.QLabel("Map Contour Level")
76  self.fontfont = self.map_level_label_map_level_label_.font()
77  self.fontfont.setBold(True)
78 
79  self.level_preview_level_preview_ = LevelPreview()
80 
81  self.level_spinbox_level_spinbox_ = QtWidgets.QDoubleSpinBox()
82  self.level_spinbox_level_spinbox_.setDecimals(3)
83  self.level_spinbox_level_spinbox_.setSingleStep(0.05)
84 
85  grid = QtWidgets.QGridLayout()
86  grid.setContentsMargins(0,5,0,0)
87  grid.addWidget(self.level_preview_level_preview_, 0, 0, 1, 4)
88  grid.addWidget(self.map_level_label_map_level_label_, 1, 0, 1, 3)
89  grid.addWidget(self.level_spinbox_level_spinbox_,1,3,1,1)
90  grid.setRowStretch(3, 1)
91  self.setLayout(grid)
92 
93  self.level_preview_level_preview_.levelUpdated.connect(self.UpdateLevelUpdateLevel)
94  self.level_preview_level_preview_.levelModified.connect(self.ModifySpinBoxModifySpinBox)
95  self.level_spinbox_level_spinbox_.valueChanged.connect(self.UpdateLevelUpdateLevel)
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_level_preview_.SetBins(node.GetHistogram())
106  self.level_preview_level_preview_.SetMinimum(node.GetMinLevel())
107  self.level_spinbox_level_spinbox_.setMinimum(node.GetMinLevel())
108  self.level_preview_level_preview_.SetMaximum(node.GetMaxLevel())
109  self.level_spinbox_level_spinbox_.setMaximum(node.GetMaxLevel())
110  self.level_preview_level_preview_.SetLevel(node.GetLevel())
111  self.level_spinbox_level_spinbox_.show()
112  self.fontfont.setBold(True)
113  self.map_level_label_map_level_label_.setText("Map Contour Level")
114  self.setEnabled(True)
115  except UserWarning:
116  self.fontfont.setBold(False)
117  self.map_level_label_map_level_label_.setText("Map uniformly filled with level %s"%node.GetLevel())
118  self.level_spinbox_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  self.level_spinbox_level_spinbox_.valueChanged.disconnect(self.UpdateLevelUpdateLevel)
133  self.level_spinbox_level_spinbox_.setValue(level)
134  self.level_spinbox_level_spinbox_.valueChanged.connect(self.UpdateLevelUpdateLevel)
135 
136 #Level Preview
137 class LevelPreview(QtWidgets.QWidget):
138 
139  levelUpdated = QtCore.pyqtSignal(int, name="levelUpdated")
140  levelModified = QtCore.pyqtSignal(int, name="levelModified")
141 
142  def __init__(self, parent=None):
143  QtWidgets.QWidget.__init__(self, parent)
144 
145  #Defaults
146  self.border_offset_border_offset_ = 3
147  self.preview_height_preview_height_ = 150
148  QtWidgets.QWidget.__init__(self, parent)
149 
150  #Ui
151  self.setMinimumSize(0, self.preview_height_preview_height_ + 4)
152 
153  self.bins_bins_ = None
154  self.level_level_ = 0
155  self.minimum_minimum_ = 0
156  self.maximum_maximum_ = 0
157 
158  self.paint_mouse_paint_mouse_=False
159 
160  def SetBins(self, bins):
161  self.bins_bins_ = bins
162  self.update()
163 
164  def SetMaximum(self, max):
165  self.maximum_maximum_ = max
166 
167  def SetMinimum(self, min):
168  self.minimum_minimum_ = min
169 
170  def SetLevel(self, level):
171  self.level_level_ = level
172 
173  def GetLevel(self):
174  return self.level_level_
175 
176  def paintEvent(self, event):
177  if self.isEnabled() and self.bins_bins_ is not None:
178  painter = QtWidgets.QPainter()
179  if painter.begin(self):
180  self.PaintBackgroundPaintBackground(painter)
181  self.PaintBinsPaintBins(painter)
182  self.PaintLevelPaintLevel(painter)
183  if(self.paint_mouse_paint_mouse_):
184  self.PaintMousePaintMouse(painter)
185  painter.end()
186 
187  def PaintBackground(self,painter):
188  size = self.size()
189  painter.setBrush(QtCore.Qt.white)
190  painter.setPen(QtCore.Qt.white)
191  painter.drawRect(self.border_offset_border_offset_,
192  self.border_offset_border_offset_,
193  size.width() - 2 * self.border_offset_border_offset_,
194  self.preview_height_preview_height_)
195 
196  def PaintBins(self,painter):
197  size = self.size()
198  bin_cnt = len(self.bins_bins_)
199  bin_width = (size.width()-2* self.border_offset_border_offset_) / float(bin_cnt)
200  max=0
201  for b in self.bins_bins_:
202  if(b>max):
203  max = b
204  max = math.log(max)
205  if(max > 0):
206  painter.setBrush(QtCore.Qt.black)
207  painter.setPen(QtCore.Qt.black)
208  for i in range(0,bin_cnt):
209  bin_height = self.bins_bins_[i]
210  if(bin_height>0):
211  bin_height = math.floor((math.log(bin_height)/max)*(self.preview_height_preview_height_-2*(self.border_offset_border_offset_)))
212  painter.drawRect(self.border_offset_border_offset_ + (i*bin_width),
213  self.preview_height_preview_height_ - bin_height,
214  bin_width,
215  bin_height)
216 
217  def PaintLevel(self,painter):
218  size = self.size()
219  width = size.width()-(2* self.border_offset_border_offset_)
220  tot_len = self.maximum_maximum_-self.minimum_minimum_
221  if(tot_len>0):
222  cur_len = self.level_level_-self.minimum_minimum_
223  painter.setBrush(QtCore.Qt.red)
224  painter.setPen(QtCore.Qt.red)
225  painter.drawRect((width / tot_len) * cur_len,
226  self.border_offset_border_offset_,
227  1,
228  self.preview_height_preview_height_)
229 
230  def PaintMouse(self,painter):
231  size = self.size()
232  width = size.width()-(2* self.border_offset_border_offset_)
233  painter.setBrush(QtCore.Qt.gray)
234  painter.setPen(QtCore.Qt.gray)
235  pos=self.mapFromGlobal(QtWidgets.QCursor.pos())
236  painter.drawRect(pos.x(),
237  self.border_offset_border_offset_,
238  1,
239  self.preview_height_preview_height_)
240 
241  def mouseReleaseEvent(self, event):
242  self.paint_mouse_paint_mouse_=False
243  size = self.size()
244  width = size.width()-(2* self.border_offset_border_offset_)
245  tot_len = self.maximum_maximum_-self.minimum_minimum_
246  self.level_level_ = self.minimum_minimum_ + float(event.x())/width * tot_len
247  self.update()
248  self.levelUpdatedlevelUpdated.emit(self.level_level_)
249 
250  def mousePressEvent(self,event):
251  self.paint_mouse_paint_mouse_=True
252 
253  def mouseMoveEvent(self, event):
254  size = self.size()
255  width = size.width()-(2* self.border_offset_border_offset_)
256  tot_len = self.maximum_maximum_-self.minimum_minimum_
257  level = self.minimum_minimum_ + float(event.x())/width * tot_len
258  self.levelModifiedlevelModified.emit(level)
259  self.update()
graphical rendering of mol::EntityHandle entites
Definition: entity.hh:60
isocontour rendering for 3D image data
Definition: map_iso.hh:52