OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
immutable_gradient_info_handler.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 info
24 from PyQt4 import QtCore, QtGui
25 
26 #Gradient Editor
28  def __init__(self, fileName):
29 
30  self.FILE_NAME = fileName
31  self.GRADIENTS_GROUP_NAME = "Gradients"
32  self.GRADIENT_GROUP_NAME = "Gradient"
33  self.NAME_ATTRIBUTE_NAME = "Name"
34 
35  self.handle_ = info.LoadOrCreateInfo(self.FILE_NAME)
36 
37  self.root_ = self.handle_.Root()
38 
39  self.gradients_ = self.root_.RetrieveGroup(self.GRADIENTS_GROUP_NAME)
40 
41 
42  def GetGfxGradient(self, name):
43  group = self.gradients_.GetGroup(name)
44  gfx_gradient = gfx.Gradient.GradientFromInfo(group)
45  return gfx_gradient
46 
47  def GetQGradient(self, name):
48  gfx_gradient = self.GetGfxGradient(name)
49  return ImmutableGradientInfoHandler.ConvertToQGradient(gfx_gradient)
50 
51  def GetQGradients(self):
52  group_list = self.gradients_.GetGroups(self.GRADIENT_GROUP_NAME)
53  q_gradients = dict()
54 
55  for group in group_list:
56  if group.HasAttribute(self.NAME_ATTRIBUTE_NAME):
57  gfx_gradient = gfx.Gradient.GradientFromInfo(group)
58  q_gradients[group.GetAttribute(self.NAME_ATTRIBUTE_NAME)] = (ImmutableGradientInfoHandler.ConvertToQGradient(gfx_gradient))
59 
60  return q_gradients
61 
62  @staticmethod
63  def ConvertToQGradient(gfx_gradient):
64  stops = gfx_gradient.GetStops()
65  qgrad = QtGui.QLinearGradient()
66  for stop in stops:
67  color = stop.GetColor()
68  qcolor = QtGui.QColor(color.Red()*255,
69  color.Green()*255,
70  color.Blue()*255,
71  color.Alpha()*255)
72  qgrad.setColorAt(stop.GetRel(), qcolor)
73  return qgrad
74 
75  @staticmethod
76  def ConvertToGfxGradient(gradient):
77  gfx_gradient = gfx.Gradient()
78  for s in gradient.stops():
79  rel=s[0]
80  color=s[1]
81  gfx_gradient.SetColorAt(s[0], gfx.RGB(s[1].redF(), s[1].greenF(), s[1].blueF()));
82  return gfx_gradient;