OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
color_select_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 PyQt4 import QtCore, QtGui
24 
25 #Gradient Stop
26 class ColorSelectWidget(QtGui.QWidget):
27  def __init__(self, width, height, color, parent=None):
28  QtGui.QWidget.__init__(self, parent)
29 
30  #Membervars
31  self.width_ = width
32  self.height_ = height
33 
34  if(color is None):
35  self.color_ = QtGui.QColor("White")
36  else:
37  self.color_ = color
38 
39  self.show()
40 
41  #ContextMenu
42  self.change_color_ = QtGui.QAction('ChangeColor', self)
43 
44  QtCore.QObject.connect(self.change_color_, QtCore.SIGNAL('triggered()'), self.ChangeColor)
45 
46  self.addAction(self.change_color_)
47  self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
48 
49  self.Resize()
50 
51  def ChangeColor(self):
52  color = QtGui.QColorDialog.getColor(self.color_, self)
53 
54  if(color != self.color_ and color.isValid()):
55  self.color_ = color
56  self.emit(QtCore.SIGNAL("colorChanged"))
57  self.update()
58 
59  def GetColor(self):
60  return self.color_
61 
62  def GetGfxColor(self):
63  color = self.GetColor()
64  return gfx.RGB(color.redF(), color.greenF(), color.blueF())
65 
66  def SetColor(self, color):
67  if(self.color_ != color):
68  self.color_ = color
69  self.emit(QtCore.SIGNAL("colorChanged"))
70  self.update()
71 
72  def SetGfxColor(self, color):
73  qcolor= QtGui.QColor(color.Red()*255,color.Green()*255,color.Blue()*255,color.Alpha()*255)
74  self.SetColor(qcolor)
75 
76  def paintEvent(self, event):
77  if self.isEnabled():
78  size = self.size()
79  paint = QtGui.QPainter()
80  if paint.begin(self):
81  brush = QtGui.QBrush(self.color_)
82  paint.setBrush(brush)
83  paint.drawRect(0,
84  0,
85  self.width() - 1,
86  self.height() - 1)
87  paint.end()
88 
89  def mouseDoubleClickEvent(self, event):
90  self.ChangeColor()
91 
92  def SetSize(self, width, height):
93  self.width_ = width
94  self.height_ = height
95  self.Resize()
96 
97  def Resize(self):
98  self.setMinimumSize(self.width_, self.height_)
99  self.setMaximumSize(self.width_, self.height_)
100  self.resize(self.width_, self.height_)