File indexing completed on 2024-12-22 04:16:27

0001 '''
0002     SPDX-FileCopyrightText: 2019 Tusooa Zhu <tusooa@vista.aero>
0003 
0004     This file is part of Krita-docker-color-slider.
0005 
0006     SPDX-License-Identifier: GPL-3.0-or-later
0007 '''
0008 from PyQt5.QtWidgets import QHBoxLayout, QWidget
0009 from PyQt5.QtGui import QPixmap, QPainter
0010 from PyQt5.Qt import pyqtSlot, pyqtSignal
0011 
0012 from .color_slider import ColorSlider
0013 
0014 
0015 class SliderBtn(QWidget):
0016     clicked = pyqtSignal()
0017 
0018     def __init__(self, parent=None):
0019         super(SliderBtn, self).__init__(parent)
0020 
0021     def set_color(self, qcolor):
0022         self.color = qcolor
0023         self.update()
0024 
0025     def update_color(self):
0026         color_sq = QPixmap(self.width(), self.height())
0027         color_sq.fill(self.color)
0028         image = color_sq.toImage()
0029 
0030         painter = QPainter(self)
0031         painter.drawImage(0, 0, image)
0032 
0033     def paintEvent(self, event):
0034         self.update_color()
0035 
0036     def mouseReleaseEvent(self, event):
0037         self.clicked.emit()
0038 
0039 
0040 class SliderLine(QWidget):
0041     def __init__(self, left_color, right_color, docker, parent=None):
0042         super(SliderLine, self).__init__(parent)
0043         self.left_button = SliderBtn()
0044         self.right_button = SliderBtn()
0045         self.docker = docker
0046         self.color_slider = ColorSlider(docker)
0047         self.layout = QHBoxLayout()
0048         self.layout.setContentsMargins(2, 2, 2, 2)
0049         self.setLayout(self.layout)
0050         self.layout.addWidget(self.left_button)
0051         self.layout.addWidget(self.color_slider)
0052         self.layout.addWidget(self.right_button)
0053         self.left_button.clicked.connect(self.slot_update_left_color)
0054         self.right_button.clicked.connect(self.slot_update_right_color)
0055         self.set_color('left', left_color)
0056         self.set_color('right', right_color)
0057         self.left_button.setMinimumSize(30, 30)
0058         self.left_button.setMaximumSize(30, 30)
0059         self.right_button.setMinimumSize(30, 30)
0060         self.right_button.setMaximumSize(30, 30)
0061         self.color_slider.setMaximumHeight(30)
0062 
0063     def set_color(self, pos, color):
0064         button_to_set = None
0065         if pos == 'left':
0066             self.left = color
0067             button_to_set = self.left_button
0068         else:
0069             self.right = color
0070             button_to_set = self.right_button
0071 
0072         self.color_slider.set_color(pos, color)
0073 
0074         button_to_set.set_color(self.docker.managedcolor_to_qcolor(color))
0075 
0076     @pyqtSlot()
0077     def slot_update_left_color(self):
0078         if self.docker.canvas() is not None:
0079             if self.docker.canvas().view() is not None:
0080                 self.set_color('left', self.docker.canvas().view().foregroundColor())
0081         self.color_slider.value_x = 0  # set the cursor to the left-most
0082         self.color_slider.update()
0083         self.docker.write_settings()
0084 
0085     @pyqtSlot()
0086     def slot_update_right_color(self):
0087         if self.docker.canvas() is not None:
0088             if self.docker.canvas().view() is not None:
0089                 self.set_color('right', self.docker.canvas().view().foregroundColor())
0090         self.color_slider.value_x = self.color_slider.width() - 1
0091         self.color_slider.update()
0092         self.docker.write_settings()