00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 from ost import gui
00022 from ost import gfx
00023 from ost import mol
00024 from PyQt4 import QtCore, QtGui
00025 from color_select_widget import ColorSelectWidget
00026 from gradient_preset_widget import GradientPresetWidget
00027
00028
00029 class GradientEditor(QtGui.QWidget):
00030 def __init__(self, parent=None):
00031 QtGui.QWidget.__init__(self, parent)
00032 self.parent_ = parent
00033
00034
00035 gradient_label = QtGui.QLabel("Gradient Editor")
00036 font = gradient_label.font()
00037 font.setBold(True)
00038
00039 self.prop_combo_box_ = QtGui.QComboBox()
00040 self.gradient_preview_ = GradientPreview()
00041 self.gradient_edit_ = GradientEdit(self.gradient_preview_,self)
00042 self.gradient_preset_ = GradientPresetWidget(self.gradient_edit_)
00043
00044 self.prop_combo_box_.addItem("atom B-factor")
00045 self.prop_combo_box_.addItem("average residue B-factor")
00046 self.prop_combo_box_.addItem("X-Coordinate")
00047 self.prop_combo_box_.addItem("Y-Coordinate")
00048 self.prop_combo_box_.addItem("Z-Coordinate")
00049 self.prop_combo_box_.addItem("Residue Number")
00050 self.prop_combo_box_.addItem("Atom Charge")
00051
00052 self.props=["abfac","rbfac","x","y","z","rnum","acharge"]
00053
00054 grid = QtGui.QGridLayout()
00055 grid.setContentsMargins(0,5,0,0)
00056 grid.addWidget(gradient_label, 0, 0, 1, 1)
00057 grid.addWidget(self.prop_combo_box_, 1, 0, 1, 1)
00058 grid.addWidget(self.gradient_preview_, 2, 0, 1, 1)
00059 grid.addWidget(self.gradient_edit_, 3, 0, 1, 1)
00060 grid.setRowStretch(4, 1)
00061 grid.addWidget(self.gradient_preset_, 5, 0, 1, 1)
00062 self.setLayout(grid)
00063
00064 QtCore.QObject.connect(self.prop_combo_box_, QtCore.SIGNAL("currentIndexChanged(int)"), self.Update)
00065 QtCore.QObject.connect(self.gradient_edit_, QtCore.SIGNAL("gradientUpdated"), self.Update)
00066 QtCore.QObject.connect(self.gradient_preset_, QtCore.SIGNAL("gradientSelected"), self.gradient_edit_.LoadGradient)
00067
00068 self.setMinimumSize(250,300)
00069
00070 def Update(self):
00071 scene_selection = gui.SceneSelection.Instance()
00072 for i in range(0,scene_selection.GetActiveNodeCount()):
00073 node = scene_selection.GetActiveNode(i)
00074 self.ChangeColor(node)
00075
00076 if(scene_selection.GetActiveViewCount() > 0):
00077 entity = scene_selection.GetViewEntity()
00078 view = scene_selection.GetViewUnion()
00079 self.ChangeViewColor(entity,view)
00080
00081 def ChangeColor(self,node):
00082 if isinstance(node, gfx.Entity) or isinstance(node, gfx.Surface):
00083 node.CleanColorOps()
00084 if self.parent_.GetCarbonsOnly():
00085 node.ColorBy(self.props[self.prop_combo_box_.currentIndex()],
00086 self.gradient_edit_.GetGfxGradient(), "ele=C")
00087 else:
00088 node.ColorBy(self.props[self.prop_combo_box_.currentIndex()],
00089 self.gradient_edit_.GetGfxGradient())
00090
00091 def ChangeViewColor(self, entity, view):
00092 if isinstance(entity, gfx.Entity) and isinstance(view, mol.EntityView):
00093 if self.parent_.GetCarbonsOnly():
00094 glco=gfx.GradientLevelColorOp(mol.QueryViewWrapper(mol.Query("ele=C"),view),self.props[self.prop_combo_box_.currentIndex()],self.gradient_edit_.GetGfxGradient(),mol.Prop.Level.UNSPECIFIED)
00095 else:
00096 glco=gfx.GradientLevelColorOp(mol.QueryViewWrapper(view),self.props[self.prop_combo_box_.currentIndex()],self.gradient_edit_.GetGfxGradient(),mol.Prop.Level.UNSPECIFIED)
00097 entity.Apply(glco)
00098
00099
00100 class GradientPreview(QtGui.QWidget):
00101 def __init__(self, parent=None):
00102 QtGui.QWidget.__init__(self, parent)
00103
00104
00105 self.border_offset_ = 3
00106 self.preview_height_ = 25
00107 QtGui.QWidget.__init__(self, parent)
00108
00109 self.gradient_ = QtGui.QLinearGradient()
00110
00111
00112 self.setMinimumSize(0, self.preview_height_ + 4)
00113
00114 def SetGradient(self, gradient):
00115 self.gradient_ = gradient
00116 self.update()
00117
00118 def paintEvent(self, event):
00119 if self.isEnabled() and self.gradient_ is not None:
00120 size = self.size()
00121 paint = QtGui.QPainter()
00122 if paint.begin(self):
00123 brush = QtGui.QBrush(self.gradient_)
00124 paint.setBrush(brush)
00125 paint.drawRect(self.border_offset_,
00126 self.border_offset_,
00127 size.width() - 2 * self.border_offset_,
00128 self.preview_height_)
00129 paint.end()
00130
00131
00132 class GradientEdit(QtGui.QWidget):
00133 def __init__(self, gradient_preview, parent=None):
00134 QtGui.QWidget.__init__(self, parent)
00135
00136
00137 self.gradient_preview_ = gradient_preview
00138 self.border_offset_ = self.gradient_preview_.border_offset_
00139 self.edit_height_ = 20
00140
00141
00142 self.stops = list()
00143
00144
00145
00146 self.add_ = QtGui.QAction("Add", self)
00147 QtCore.QObject.connect(self.add_, QtCore.SIGNAL("triggered()"), self.Add)
00148 self.addAction(self.add_)
00149 self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
00150
00151
00152 pal = QtGui.QPalette(self.palette())
00153 pal.setColor(QtGui.QPalette.Window, QtGui.QColor(255, 255, 255, 128))
00154 self.setPalette(pal)
00155 self.setAutoFillBackground(True)
00156 self.gradient_ = QtGui.QLinearGradient()
00157
00158 self.setMinimumSize(0, self.edit_height_)
00159
00160
00161 self.AddStop(self.border_offset_+3, QtGui.QColor("Red"))
00162 self.AddStop(self.width()-(self.border_offset_+3), QtGui.QColor("Green"))
00163
00164
00165 self.width_ = self.width()
00166
00167 def RemoveStop(self, stop):
00168 self.stops.remove(stop)
00169 stop.hide()
00170 del(stop)
00171
00172 self.UpdateGradient()
00173 self.emit(QtCore.SIGNAL("gradientUpdated"),(self))
00174
00175 def RemoveStopGui(self, stop):
00176 if(len(self.stops)>2):
00177 self.RemoveStop(stop)
00178 else:
00179 QtGui.QMessageBox.question(self, "Information", "Please keep in mind, at least two stops are needed for a gradient!")
00180
00181 def AddStop(self, pos, color=None):
00182 stop = MyGradientStop(pos, self.border_offset_, self, color)
00183 QtCore.QObject.connect(stop, QtCore.SIGNAL("gradientChanged"), self.UpdateGradient)
00184 QtCore.QObject.connect(stop, QtCore.SIGNAL("colorChanged"), self.UpdateGradient)
00185 QtCore.QObject.connect(stop, QtCore.SIGNAL("colorChanged"), self.parent().Update)
00186 QtCore.QObject.connect(stop, QtCore.SIGNAL("gradientUpdated"), self.parent().Update)
00187 QtCore.QObject.connect(stop, QtCore.SIGNAL("removeStop"), self.RemoveStopGui)
00188
00189 self.stops.append(stop)
00190 self.UpdateGradient()
00191 self.emit(QtCore.SIGNAL("gradientUpdated"),(self))
00192
00193 def Add(self):
00194 self.AddStop(QtGui.QCursor.pos().x() - self.mapToGlobal(QtCore.QPoint(0, 0)).x())
00195
00196 def UpdateGradient(self):
00197 self.gradient_preview_.SetGradient(self.GetGradient())
00198
00199 def GetGfxGradient(self):
00200 gradient = gfx.Gradient()
00201 for s in self.stops:
00202 c=s.GetColor();
00203 gradient.SetColorAt(s.GetRel(), gfx.RGB(c.redF(), c.greenF(), c.blueF()));
00204 return gradient;
00205
00206 def GetGradient(self):
00207 gradient = QtGui.QLinearGradient(self.border_offset_,
00208 0,
00209 self.size().width() - 2 * self.border_offset_,
00210 0)
00211 for s in self.stops:
00212 gradient.setColorAt(s.GetRel(), s.GetColor())
00213 return gradient
00214
00215 def LoadGradient(self, gradient):
00216 stopcpy = self.stops[:]
00217 for stop in stopcpy:
00218 self.RemoveStop(stop)
00219
00220 gradStops = gradient.stops()
00221 for stop in gradStops:
00222 self.AddStop((self.width()-(2*self.border_offset_))*stop[0], stop[1])
00223
00224
00225 def mouseDoubleClickEvent(self, event):
00226 self.AddStop(event.x())
00227
00228 def resizeEvent(self, event):
00229 factor = 1/(float(self.width_)/event.size().width())
00230 if(factor >=0):
00231 for s in self.stops:
00232 s.SetPos(s.GetPos() * factor)
00233 self.UpdateGradient()
00234 self.width_ = event.size().width()
00235
00236
00237 class MyGradientStop(ColorSelectWidget):
00238 def __init__(self, pos, offset, parent, color=None):
00239
00240 self.length_ = 20
00241 self.halflength_ = self.length_ / 2
00242
00243 self.pos_ = pos
00244 self.offset_ = offset
00245
00246 ColorSelectWidget.__init__(self, self.length_,self.length_, color, parent)
00247
00248 self.MoveToNewPos()
00249
00250 if(color is None):
00251 self.color_ = QtGui.QColor("White")
00252 self.ChangeColor()
00253 else:
00254 self.color_ = color
00255
00256
00257 self.remove_ = QtGui.QAction("Remove", self)
00258 QtCore.QObject.connect(self.remove_, QtCore.SIGNAL("triggered()"), self.Remove)
00259 self.addAction(self.remove_)
00260
00261 def Remove(self):
00262 self.emit(QtCore.SIGNAL("removeStop"),(self))
00263
00264 def GetPos(self):
00265 return self.pos_
00266
00267 def SetPos(self, pos):
00268 self.pos_ = pos
00269 self.MoveToNewPos()
00270
00271 def GetRel(self):
00272 rel = self.pos_ / float(self.parent().width() - 2 * self.offset_)
00273 if rel > 1:
00274 return 1
00275 elif rel < 0:
00276 return 0
00277 return rel
00278
00279 def MoveToNewPos(self):
00280 self.move(self.pos_ - self.halflength_, 0)
00281 self.update()
00282 self.emit(QtCore.SIGNAL("gradientChanged"))
00283
00284 def mouseMoveEvent(self, event):
00285 self.pos_ += event.pos().x() - self.halflength_
00286 if self.pos_ <= self.offset_:
00287 self.pos_ = self.offset_ + 1
00288 elif self.pos_ >= self.parent().width() - 2*self.offset_:
00289 self.pos_ = self.parent().width() - (2*self.offset_ + 1)
00290 self.MoveToNewPos()
00291
00292 def mouseDoubleClickEvent(self, event):
00293 self.ChangeColor()
00294
00295 def mouseReleaseEvent(self, entity):
00296 if entity.button() == QtCore.Qt.LeftButton:
00297 self.emit(QtCore.SIGNAL("gradientUpdated"))