openlp/openlp/core/widgets/buttons.py

82 lines
3.1 KiB
Python
Raw Normal View History

2016-04-17 19:09:46 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-01 10:10:57 +00:00
# Copyright (c) 2008-2022 OpenLP Developers #
2019-04-13 13:00:22 +00:00
# ---------------------------------------------------------------------- #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
2016-04-17 19:09:46 +00:00
"""
Provide a custom widget based on QPushButton for the selection of colors
"""
from PyQt5 import QtCore, QtGui, QtWidgets
2017-10-07 07:05:07 +00:00
from openlp.core.common.i18n import translate
2016-04-17 19:09:46 +00:00
class ColorButton(QtWidgets.QPushButton):
"""
Subclasses QPushbutton to create a "Color Chooser" button
"""
colorChanged = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
"""
Initialise the ColorButton
"""
super().__init__(parent)
2016-04-17 19:09:46 +00:00
self.parent = parent
self.change_color('#ffffff')
self.setToolTip(translate('OpenLP.ColorButton', 'Click to select a color.'))
self.clicked.connect(self.on_clicked)
def change_color(self, color):
"""
Sets the _color variable and the background color.
2018-04-13 16:57:22 +00:00
:param color: String representation of a hexadecimal color
2016-04-17 19:09:46 +00:00
"""
self._color = color
self.setStyleSheet('background-color: %s' % color)
@property
def color(self):
"""
Property method to return the color variable
2018-04-13 16:57:22 +00:00
:return: String representation of a hexadecimal color
2016-04-17 19:09:46 +00:00
"""
return self._color
@color.setter
def color(self, color):
"""
Property setter to change the instance color
2018-04-13 16:57:22 +00:00
:param color: String representation of a hexadecimal color
2016-04-17 19:09:46 +00:00
"""
self.change_color(color)
def on_clicked(self):
"""
Handle the PushButton clicked signal, showing the ColorDialog and validating the input
"""
new_color = QtWidgets.QColorDialog.getColor(QtGui.QColor(self._color), self.parent)
if new_color.isValid() and self._color != new_color.name():
self.change_color(new_color.name())
self.colorChanged.emit(new_color.name())