OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
uniform_color_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 from ost import gui
22 from ost import gfx
23 from ost import mol
24 try:
25  from ost import img
26  _img_present=True
27 except ImportError:
28  _img_present=False
29  pass
30 from PyQt4 import QtCore, QtGui
31 from color_select_widget import ColorSelectWidget
32 
33 #Uniform Color Widget
34 class UniformColorWidget(QtGui.QWidget):
35  def __init__(self, parent=None):
36  QtGui.QWidget.__init__(self, parent)
37  self.parent_ = parent
38 
39  self.text_ = "Uniform Color"
40 
41  #Create Ui elements
42  uniform_label = QtGui.QLabel(self.text_)
43  font = uniform_label.font()
44  font.setBold(True)
45 
46  self.color_select_widget_ = ColorSelectWidget(1,1,QtGui.QColor("White"))
47 
48  top_layout = QtGui.QVBoxLayout()
49 
50  grid = QtGui.QGridLayout()
51  grid.addWidget(self.color_select_widget_, 2, 1, 1, 1)
52  grid.setRowStretch(1, 1)
53  grid.setRowStretch(3, 1)
54  grid.setColumnStretch(0,1)
55  grid.setColumnStretch(2,1)
56 
57  top_layout.addWidget(uniform_label)
58  top_layout.addLayout(grid)
59  self.setLayout(top_layout)
60 
61  QtCore.QObject.connect(self.color_select_widget_, QtCore.SIGNAL("colorChanged"), self.ChangeColors)
62 
63  self.setMinimumSize(250,150)
64 
65  def Update(self):
66  scene_selection = gui.SceneSelection.Instance()
67  for i in range(0,scene_selection.GetActiveNodeCount()):
68  node = scene_selection.GetActiveNode(i)
69  if _img_present and isinstance(node, gfx.MapIso):
70  if self.color_select_widget_.GetGfxColor() != node.GetColor():
71  self.color_select_widget_.SetGfxColor(node.GetColor())
72  else:
73  self.ChangeColors()
74 
75  def ChangeColors(self):
76  scene_selection = gui.SceneSelection.Instance()
77  for i in range(0,scene_selection.GetActiveNodeCount()):
78  node = scene_selection.GetActiveNode(i)
79  self.ChangeColor(node)
80 
81  if(scene_selection.GetActiveViewCount() > 0):
82  entity = scene_selection.GetViewEntity()
83  view = scene_selection.GetViewUnion()
84  self.ChangeViewColor(entity,view)
85 
86  def ChangeColor(self, node):
87  gfx_color = self.color_select_widget_.GetGfxColor()
88  if isinstance(node, gfx.Entity) or isinstance(node, gfx.Surface):
89  node.CleanColorOps()
90  if self.parent_.GetCarbonsOnly():
91  node.SetColor(gfx_color,"ele=C")
92  else:
93  node.SetColor(gfx_color,"")
94  elif _img_present and isinstance(node, gfx.MapIso):
95  node.SetColor(gfx_color)
96 
97  def ChangeViewColor(self, entity, view):
98  if isinstance(entity, gfx.Entity) and isinstance(view, mol.EntityView):
99  gfx_color = self.color_select_widget_.GetGfxColor()
100  if self.parent_.GetCarbonsOnly():
101  ufco=gfx.UniformColorOp(mol.QueryViewWrapper(mol.Query("ele=C"), view),gfx_color)
102  else:
103  ufco=gfx.UniformColorOp(mol.QueryViewWrapper(view),gfx_color)
104  entity.Apply(ufco)
105 
106  def resizeEvent(self, event):
107  self.color_select_widget_.SetSize(self.width()/2,self.height()/2)
108 
109  def GetText(self):
110  return self.text_