00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 import math
00021 from ost import gui
00022 from ost import gfx
00023 try:
00024 from ost import img
00025 _img_present=True
00026 except ImportError:
00027 _img_present=False
00028 pass
00029 from PyQt4 import QtCore, QtGui
00030
00031 from preset_widget import PresetWidget
00032
00033 class AdditionalSettingsWidget(QtGui.QStackedWidget):
00034 def __init__(self, parent=None):
00035 QtGui.QStackedWidget.__init__(self, parent)
00036 self.map_widget_ = MapLevelWidget(self)
00037 self.preset_widget_ = PresetWidget(self)
00038 self.addWidget(self.preset_widget_);
00039 self.addWidget(self.map_widget_);
00040 self.setContentsMargins(0,0,0,0)
00041 self.setMinimumSize(self.preset_widget_.minimumSize())
00042
00043 def Update(self):
00044 self.setEnabled(True)
00045 scene_selection = gui.SceneSelection.Instance()
00046 all_img = True
00047 all_entity = True
00048 for i in range(0,scene_selection.GetActiveNodeCount()):
00049 node = scene_selection.GetActiveNode(i)
00050 if not (isinstance(node, gfx.Entity) or isinstance(node, gfx.Surface)):
00051 all_entity = False
00052 if (not _img_present) or (not isinstance(node, gfx.MapIso)):
00053 all_img = False
00054 if all_img:
00055 self.map_widget_.Update()
00056 self.setMinimumSize(self.map_widget_.minimumSize())
00057 self.resize(self.map_widget_.minimumSize())
00058 self.setMinimumSize(self.map_widget_.minimumSize())
00059 self.setCurrentWidget(self.map_widget_)
00060 elif all_entity:
00061 self.preset_widget_.Update()
00062 self.setMinimumSize(self.preset_widget_.minimumSize())
00063 self.resize(self.preset_widget_.minimumSize())
00064 self.setMinimumSize(self.preset_widget_.minimumSize())
00065 self.setCurrentWidget(self.preset_widget_)
00066 else:
00067 self.setEnabled(False)
00068
00069
00070 class MapLevelWidget(QtGui.QWidget):
00071 def __init__(self, parent=None):
00072 QtGui.QWidget.__init__(self, parent)
00073
00074
00075 self.map_level_label_ = QtGui.QLabel("Map Contour Level")
00076 self.font = self.map_level_label_.font()
00077 self.font.setBold(True)
00078
00079 self.level_preview_ = LevelPreview()
00080
00081 self.level_spinbox_ = QtGui.QDoubleSpinBox()
00082 self.level_spinbox_.setDecimals(3)
00083 self.level_spinbox_.setSingleStep(0.05)
00084
00085 grid = QtGui.QGridLayout()
00086 grid.setContentsMargins(0,5,0,0)
00087 grid.addWidget(self.level_preview_, 0, 0, 1, 4)
00088 grid.addWidget(self.map_level_label_, 1, 0, 1, 3)
00089 grid.addWidget(self.level_spinbox_,1,3,1,1)
00090 grid.setRowStretch(3, 1)
00091 self.setLayout(grid)
00092
00093 QtCore.QObject.connect(self.level_preview_, QtCore.SIGNAL("levelUpdated"), self.UpdateLevel)
00094 QtCore.QObject.connect(self.level_preview_, QtCore.SIGNAL("levelModified"), self.ModifySpinBox)
00095 QtCore.QObject.connect(self.level_spinbox_, QtCore.SIGNAL("valueChanged(double)"), self.UpdateLevel)
00096
00097 self.setMinimumSize(250,200)
00098
00099 def Update(self):
00100 scene_selection = gui.SceneSelection.Instance()
00101 if(scene_selection.GetActiveNodeCount()==1):
00102 node = scene_selection.GetActiveNode(0)
00103 if _img_present and isinstance(node, gfx.MapIso):
00104 try:
00105 self.level_preview_.SetBins(node.GetHistogram())
00106 self.level_preview_.SetMinimum(node.GetMinLevel())
00107 self.level_spinbox_.setMinimum(node.GetMinLevel())
00108 self.level_preview_.SetMaximum(node.GetMaxLevel())
00109 self.level_spinbox_.setMaximum(node.GetMaxLevel())
00110 self.level_preview_.SetLevel(node.GetLevel())
00111 self.level_spinbox_.show()
00112 self.font.setBold(True)
00113 self.map_level_label_.setText("Map Contour Level")
00114 self.setEnabled(True)
00115 except UserWarning:
00116 self.font.setBold(False)
00117 self.map_level_label_.setText("Map uniformly filled with level %s"%node.GetLevel())
00118 self.level_spinbox_.hide()
00119 self.setEnabled(False)
00120 else:
00121 self.setEnabled(False)
00122 else:
00123 self.setEnabled(False)
00124
00125 def UpdateLevel(self, level):
00126 scene_selection = gui.SceneSelection.Instance()
00127 if(scene_selection.GetActiveNodeCount()==1):
00128 node = scene_selection.GetActiveNode(0)
00129 node.SetLevel(level)
00130
00131 def ModifySpinBox(self, level):
00132 QtCore.QObject.disconnect(self.level_spinbox_, QtCore.SIGNAL("valueChanged(double)"), self.UpdateLevel)
00133 self.level_spinbox_.setValue(level)
00134 QtCore.QObject.connect(self.level_spinbox_, QtCore.SIGNAL("valueChanged(double)"), self.UpdateLevel)
00135
00136
00137 class LevelPreview(QtGui.QWidget):
00138 def __init__(self, parent=None):
00139 QtGui.QWidget.__init__(self, parent)
00140
00141
00142 self.border_offset_ = 3
00143 self.preview_height_ = 150
00144 QtGui.QWidget.__init__(self, parent)
00145
00146
00147 self.setMinimumSize(0, self.preview_height_ + 4)
00148
00149 self.bins_ = None
00150 self.level_ = 0
00151 self.minimum_ = 0
00152 self.maximum_ = 0
00153
00154 self.paint_mouse_=False
00155
00156 def SetBins(self, bins):
00157 self.bins_ = bins
00158 self.update()
00159
00160 def SetMaximum(self, max):
00161 self.maximum_ = max
00162
00163 def SetMinimum(self, min):
00164 self.minimum_ = min
00165
00166 def SetLevel(self, level):
00167 self.level_ = level
00168
00169 def GetLevel(self):
00170 return self.level_
00171
00172 def paintEvent(self, event):
00173 if self.isEnabled() and self.bins_ is not None:
00174 painter = QtGui.QPainter()
00175 if painter.begin(self):
00176 self.PaintBackground(painter)
00177 self.PaintBins(painter)
00178 self.PaintLevel(painter)
00179 if(self.paint_mouse_):
00180 self.PaintMouse(painter)
00181 painter.end()
00182
00183 def PaintBackground(self,painter):
00184 size = self.size()
00185 painter.setBrush(QtCore.Qt.white)
00186 painter.setPen(QtCore.Qt.white)
00187 painter.drawRect(self.border_offset_,
00188 self.border_offset_,
00189 size.width() - 2 * self.border_offset_,
00190 self.preview_height_)
00191
00192 def PaintBins(self,painter):
00193 size = self.size()
00194 bin_cnt = len(self.bins_)
00195 bin_width = (size.width()-2* self.border_offset_) / float(bin_cnt)
00196 max=0
00197 for b in self.bins_:
00198 if(b>max):
00199 max = b
00200 max = math.log(max)
00201 if(max > 0):
00202 painter.setBrush(QtCore.Qt.black)
00203 painter.setPen(QtCore.Qt.black)
00204 for i in range(0,bin_cnt):
00205 bin_height = self.bins_[i]
00206 if(bin_height>0):
00207 bin_height = math.floor((math.log(bin_height)/max)*(self.preview_height_-2*(self.border_offset_)))
00208 painter.drawRect(self.border_offset_ + (i*bin_width),
00209 self.preview_height_ - bin_height,
00210 bin_width,
00211 bin_height)
00212
00213 def PaintLevel(self,painter):
00214 size = self.size()
00215 width = size.width()-(2* self.border_offset_)
00216 tot_len = self.maximum_-self.minimum_
00217 if(tot_len>0):
00218 cur_len = self.level_-self.minimum_
00219 painter.setBrush(QtCore.Qt.red)
00220 painter.setPen(QtCore.Qt.red)
00221 painter.drawRect((width / tot_len) * cur_len,
00222 self.border_offset_,
00223 1,
00224 self.preview_height_)
00225
00226 def PaintMouse(self,painter):
00227 size = self.size()
00228 width = size.width()-(2* self.border_offset_)
00229 painter.setBrush(QtCore.Qt.gray)
00230 painter.setPen(QtCore.Qt.gray)
00231 pos=self.mapFromGlobal(QtGui.QCursor.pos())
00232 painter.drawRect(pos.x(),
00233 self.border_offset_,
00234 1,
00235 self.preview_height_)
00236
00237 def mouseReleaseEvent(self, event):
00238 self.paint_mouse_=False
00239 size = self.size()
00240 width = size.width()-(2* self.border_offset_)
00241 tot_len = self.maximum_-self.minimum_
00242 self.level_ = self.minimum_ + float(event.x())/width * tot_len
00243 self.update()
00244 self.emit(QtCore.SIGNAL("levelUpdated"),(self.level_))
00245
00246 def mousePressEvent(self,event):
00247 self.paint_mouse_=True
00248
00249 def mouseMoveEvent(self, event):
00250 size = self.size()
00251 width = size.width()-(2* self.border_offset_)
00252 tot_len = self.maximum_-self.minimum_
00253 level = self.minimum_ + float(event.x())/width * tot_len
00254 self.emit(QtCore.SIGNAL("levelModified"),(level))
00255 self.update()