Changeset View
Changeset View
Standalone View
Standalone View
plugins/python/mixer_slider_docker/slider_line.py
- This file was added.
1 | ''' | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2019 Tusooa Zhu <tusooa@vista.aero> | ||||
3 | | ||||
4 | This file is part of Krita-docker-color-slider. | ||||
5 | | ||||
6 | Krita-docker-color-slider is free software: you can redistribute it and/or modify | ||||
7 | it under the terms of the GNU General Public License as published by | ||||
8 | the Free Software Foundation, either version 3 of the License, or | ||||
9 | (at your option) any later version. | ||||
10 | | ||||
11 | Krita-docker-color-slider is distributed in the hope that it will be useful, | ||||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
14 | GNU General Public License for more details. | ||||
15 | | ||||
16 | You should have received a copy of the GNU General Public License | ||||
17 | along with Krita-docker-color-slider. If not, see <https://www.gnu.org/licenses/>. | ||||
18 | ''' | ||||
19 | from PyQt5.QtWidgets import QHBoxLayout, QWidget | ||||
20 | from PyQt5.QtGui import QPixmap, QPainter | ||||
21 | from PyQt5.Qt import pyqtSlot, pyqtSignal | ||||
22 | | ||||
23 | from .color_slider import ColorSlider | ||||
24 | | ||||
25 | | ||||
26 | class SliderBtn(QWidget): | ||||
27 | clicked = pyqtSignal() | ||||
28 | | ||||
29 | def __init__(self, parent=None): | ||||
30 | super(SliderBtn, self).__init__(parent) | ||||
31 | | ||||
32 | def set_color(self, qcolor): | ||||
33 | self.color = qcolor | ||||
34 | self.update() | ||||
35 | | ||||
36 | def update_color(self): | ||||
37 | color_sq = QPixmap(self.width(), self.height()) | ||||
38 | color_sq.fill(self.color) | ||||
39 | image = color_sq.toImage() | ||||
40 | | ||||
41 | painter = QPainter(self) | ||||
42 | painter.drawImage(0, 0, image) | ||||
43 | | ||||
44 | def paintEvent(self, event): | ||||
45 | self.update_color() | ||||
46 | | ||||
47 | def mouseReleaseEvent(self, event): | ||||
48 | self.clicked.emit() | ||||
49 | | ||||
50 | | ||||
51 | class SliderLine(QWidget): | ||||
52 | def __init__(self, left_color, right_color, docker, parent=None): | ||||
53 | super(SliderLine, self).__init__(parent) | ||||
54 | self.left_button = SliderBtn() | ||||
55 | self.right_button = SliderBtn() | ||||
56 | self.docker = docker | ||||
57 | self.color_slider = ColorSlider(docker) | ||||
58 | self.layout = QHBoxLayout() | ||||
59 | self.layout.setContentsMargins(2, 2, 2, 2) | ||||
60 | self.setLayout(self.layout) | ||||
61 | self.layout.addWidget(self.left_button) | ||||
62 | self.layout.addWidget(self.color_slider) | ||||
63 | self.layout.addWidget(self.right_button) | ||||
64 | self.left_button.clicked.connect(self.slot_update_left_color) | ||||
65 | self.right_button.clicked.connect(self.slot_update_right_color) | ||||
66 | self.set_color('left', left_color) | ||||
67 | self.set_color('right', right_color) | ||||
68 | self.left_button.setMinimumSize(30, 30) | ||||
69 | self.left_button.setMaximumSize(30, 30) | ||||
70 | self.right_button.setMinimumSize(30, 30) | ||||
71 | self.right_button.setMaximumSize(30, 30) | ||||
72 | self.color_slider.setMaximumHeight(30) | ||||
73 | | ||||
74 | def set_color(self, pos, color): | ||||
75 | button_to_set = None | ||||
76 | if pos == 'left': | ||||
77 | self.left = color | ||||
78 | button_to_set = self.left_button | ||||
79 | else: | ||||
80 | self.right = color | ||||
81 | button_to_set = self.right_button | ||||
82 | | ||||
83 | self.color_slider.set_color(pos, color) | ||||
84 | | ||||
85 | button_to_set.set_color(self.docker.managedcolor_to_qcolor(color)) | ||||
86 | | ||||
87 | @pyqtSlot() | ||||
88 | def slot_update_left_color(self): | ||||
89 | if self.docker.canvas() is not None: | ||||
90 | if self.docker.canvas().view() is not None: | ||||
91 | self.set_color('left', self.docker.canvas().view().foregroundColor()) | ||||
92 | self.color_slider.value_x = 0 # set the cursor to the left-most | ||||
93 | self.color_slider.update() | ||||
94 | self.docker.write_settings() | ||||
95 | | ||||
96 | @pyqtSlot() | ||||
97 | def slot_update_right_color(self): | ||||
98 | if self.docker.canvas() is not None: | ||||
99 | if self.docker.canvas().view() is not None: | ||||
100 | self.set_color('right', self.docker.canvas().view().foregroundColor()) | ||||
101 | self.color_slider.value_x = self.color_slider.width() - 1 | ||||
102 | self.color_slider.update() | ||||
103 | self.docker.write_settings() |