OpenStructure
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-2020 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 PyQt5 import QtCore, QtWidgets, QtGui
24 
25 #Gradient Stop
26 class ColorSelectWidget(QtWidgets.QWidget):
27 
28  colorChanged = QtCore.pyqtSignal()
29 
30  def __init__(self, width, height, color, parent=None):
31  QtWidgets.QWidget.__init__(self, parent)
32 
33  #Membervars
34  self.width_width_ = width
35  self.height_height_ = height
36 
37  if(color is None):
38  self.color_color_ = QtWidgets.QColor("White")
39  else:
40  self.color_color_ = color
41 
42  self.show()
43 
44  #ContextMenu
45  self.change_color_change_color_ = QtWidgets.QAction('ChangeColor', self)
46 
47  self.change_color_change_color_.triggered.connect(self.ChangeColorChangeColor)
48 
49  self.addAction(self.change_color_change_color_)
50  self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
51 
52  self.ResizeResize()
53 
54  def ChangeColor(self):
55  color = QtWidgets.QColorDialog.getColor(initial = self.color_color_, parent = self,
56  title = "Select Color")
57 
58  if(color != self.color_color_ and color.isValid()):
59  self.color_color_ = color
60  self.colorChangedcolorChanged.emit()
61  self.update()
62 
63  def GetColor(self):
64  return self.color_color_
65 
66  def GetGfxColor(self):
67  color = self.GetColorGetColor()
68  return gfx.RGB(color.redF(), color.greenF(), color.blueF())
69 
70  def SetColor(self, color):
71  if(self.color_color_ != color):
72  self.color_color_ = color
73  self.colorChangedcolorChanged.emit()
74  self.update()
75 
76  def SetGfxColor(self, color):
77  qcolor= QtWidgets.QColor(color.Red()*255,color.Green()*255,color.Blue()*255,color.Alpha()*255)
78  self.SetColorSetColor(qcolor)
79 
80  def paintEvent(self, event):
81  if self.isEnabled():
82  size = self.size()
83  paint = QtGui.QPainter()
84  if paint.begin(self):
85  brush = QtGui.QBrush(self.color_color_)
86  paint.setBrush(brush)
87  paint.drawRect(0,
88  0,
89  self.width() - 1,
90  self.height() - 1)
91  paint.end()
92 
93  def mouseDoubleClickEvent(self, event):
94  self.ChangeColorChangeColor()
95 
96  def SetSize(self, width, height):
97  self.width_width_ = width
98  self.height_height_ = height
99  self.ResizeResize()
100 
101  def Resize(self):
102  self.setMinimumSize(self.width_width_, self.height_height_)
103  self.setMaximumSize(self.width_width_, self.height_height_)
104  self.resize(self.width_width_, self.height_height_)
def __init__(self, width, height, color, parent=None)