OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
color_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 mol
23 from ost import gui
24 from ost import gfx
25 try:
26  from ost import img
27  _img_present=True
28 except ImportError:
29  _img_present=False
30  pass
31 
32 from PyQt4 import QtCore, QtGui
33 from scene_selection_helper import SelHelper
34 from gradient_editor_widget import GradientEditor
35 from uniform_color_widget import UniformColorWidget
36 from combo_options_widget import ComboOptionsWidget
37 
38 class ColorOptionsWidget(ComboOptionsWidget):
39  def __init__(self, parent=None):
40  ComboOptionsWidget.__init__(self, parent)
41 
42  #Title
43  self.text_ = "Color Options"
44  conly_label_ = QtGui.QLabel('carbons only')
45  self.conly_box_ = QtGui.QCheckBox()
46 
47  #Add options to menu
48  self.entity_widgets_ = list()
49  self.entity_widgets_.append(["Color by Element", ByElementWidget("Color by Element", self)])
50  self.entity_widgets_.append(["Color by Chain", ByChainWidget("Color by Chain", self)])
51  self.entity_widgets_.append(["Color by Entity", ByEntityWidget("Color by Entity", self)])
52  self.entity_widgets_.append(["Color by Property", GradientEditor(self)])
53  self.entity_widgets_.append(["Uniform",UniformColorWidget(self)])
54 
55  self.img_widgets_ = list()
56  self.img_widgets_.append(["Uniform",UniformColorWidget()])
57 
58  qw = QtGui.QWidget(self)
59  gl = QtGui.QGridLayout(qw)
60  gl.addWidget(self.conly_box_, 0, 0, 1, 1)
61  gl.addWidget(conly_label_, 0, 1, 1, 4)
62  self.grid_layout_.addWidget(qw, 2, 0, 1, 1)
63 
64  self.setMinimumSize(250,200)
65 
66  def OnComboChange(self, item):
67  scene_selection = gui.SceneSelection.Instance()
68  if hasattr(item,"PrepareColoring"):
69  item.PrepareColoring()
70  for i in range(0,scene_selection.GetActiveNodeCount()):
71  node = scene_selection.GetActiveNode(i)
72  item.ChangeColor(node)
73 
74  if(scene_selection.GetActiveViewCount() > 0):
75  entity = scene_selection.GetViewEntity()
76  view = scene_selection.GetViewUnion()
77  item.ChangeViewColor(entity,view)
78 
79  self.DoResize()
80 
81 
82  def Update(self):
83 
84  ComboOptionsWidget.setEnabled(self,True)
85 
86  if SelHelper().CheckAllFlags(SelHelper.NO_SELECTION):
87  ComboOptionsWidget.setEnabled(self,False)
88  return
89 
90  for w in self.entity_widgets_:
91  self.RemoveWidget(w[0])
92  for w in self.img_widgets_:
93  self.RemoveWidget(w[0])
94 
95 
96  if SelHelper().CheckFlags(SelHelper.HAS_IMG | SelHelper.IS_ONE_TYPE):
97  for w in self.img_widgets_:
98  self.AddWidget(w[0], w[1])
99  elif SelHelper().CheckMinOneFlag(SelHelper.HAS_ENTITY| SelHelper.HAS_VIEW| SelHelper.HAS_SURFACE) and SelHelper().CheckNotFlags(SelHelper.HAS_IMG):
100  for w in self.entity_widgets_:
101  self.AddWidget(w[0], w[1])
102  else:
103  ComboOptionsWidget.setEnabled(self,False)
104  return
105 
106  self.GetCurrentWidget().Update()
107 
108  def GetText(self):
109  return self.text_
110 
111  def GetCarbonsOnly(self):
112  return self.conly_box_.isChecked()
113 
114 
115 class ByElementWidget(QtGui.QWidget):
116  def __init__(self, text, parent=None):
117  QtGui.QWidget.__init__(self, parent)
118  self.parent_ = parent
119 
120 
121  #Title
122  self.text_ = text
123 
124  #UI
125  text_label = QtGui.QLabel(text)
126  font = text_label.font()
127  font.setBold(True)
128 
129  grid = QtGui.QGridLayout()
130  grid.addWidget(text_label,0,0,1,1)
131  grid.addWidget(QtGui.QLabel("No Settings available"), 1, 0, 1, 3)
132  grid.setRowStretch(2,1)
133  self.setLayout(grid)
134  self.setMinimumSize(250,60)
135 
136  def Update(self):
137  pass #Do Nothing
138 
139  def ChangeColor(self, node):
140  if isinstance(node, gfx.Entity):
141  node.CleanColorOps()
142  if self.parent_.GetCarbonsOnly():
143  node.ColorByElement("ele=C")
144  else:
145  node.ColorByElement()
146 
147  def ChangeViewColor(self, entity, view):
148  if isinstance(entity, gfx.Entity) and isinstance(view, mol.EntityView):
149  if self.parent_.GetCarbonsOnly():
151  else:
153  entity.Apply(beco)
154 
155  def GetText(self):
156  return self.text_
157 
158 
159 class ByChainWidget(QtGui.QWidget):
160  def __init__(self, text, parent=None):
161  QtGui.QWidget.__init__(self, parent)
162  self.parent_ = parent
163 
164  #Title
165  self.text_ = text
166 
167  #UI
168  text_label = QtGui.QLabel(text)
169  font = text_label.font()
170  font.setBold(True)
171 
172  grid = QtGui.QGridLayout()
173  grid.addWidget(text_label,0,0,1,1)
174  grid.setRowStretch(2,1)
175  self.setLayout(grid)
176  self.setMinimumSize(250,60)
177 
178  def Update(self):
179  pass #Do Nothing
180 
181  def ChangeColor(self, node):
182  if isinstance(node, gfx.Entity):
183  node.CleanColorOps()
184  if self.parent_.GetCarbonsOnly():
185  node.ColorByChain('ele=C')
186  else:
187  node.ColorByChain()
188 
189  def ChangeViewColor(self, entity, view):
190  if isinstance(entity, gfx.Entity) and isinstance(view, mol.EntityView):
191  if self.parent_.GetCarbonsOnly():
193  else:
195  entity.Apply(bco)
196 
197  def GetText(self):
198  return self.text_
199 
200 class ByEntityWidget(QtGui.QWidget):
201  def __init__(self, text, parent=None):
202  QtGui.QWidget.__init__(self, parent)
203  self.parent_ = parent
204 
205  #Title
206  self.text_ = text
207 
208  #UI
209  text_label = QtGui.QLabel(text)
210  font = text_label.font()
211  font.setBold(True)
212 
213  grid = QtGui.QGridLayout()
214  grid.addWidget(text_label,0,0,1,1)
215  grid.setRowStretch(2,1)
216  self.setLayout(grid)
217  self.setMinimumSize(250,60)
218 
219  self.gradient_ = gfx.Gradient("RAINBOW")
220 
221  def Update(self):
222  pass #Do Nothing
223 
224  def PrepareColoring(self):
225  scene_selection = gui.SceneSelection.Instance()
226  entity_count = scene_selection.GetActiveNodeCount()
227  for i in range(0,scene_selection.GetActiveNodeCount()):
228  if i<=0:
229  color=self.gradient_.GetColorAt(0.0)
230  else:
231  color=self.gradient_.GetColorAt(float(i) / entity_count)
232  node = scene_selection.GetActiveNode(i)
233  if self.parent_.GetCarbonsOnly():
234  node.SetColor(color, 'ele=C')
235  else:
236  node.SetColor(color)
237 
238  def ChangeColor(self, node):
239  pass
240 
241  def ChangeViewColor(self, entity, view):
242  if isinstance(entity, gfx.Entity) and isinstance(view, mol.EntityView):
243  if self.parent_.GetCarbonsOnly():
245  else:
247  entity.Apply(bco)
248 
249  def GetText(self):
250  return self.text_
graphical rendering of mol::EntityHandle entites
Definition: entity.hh:63
Selection Query.
Definition: query.hh:74
definition of EntityView
Definition: entity_view.hh:86
color gradient
Definition: gradient.hh:59