1 /**
2 * \file
3 *
4 * \author Mattia Basaglia
5 *
6 * \copyright Copyright (C) 2013-2020 Mattia Basaglia
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22 #include "QtColorWidgets/color_list_widget.hpp"
23 #include "QtColorWidgets/color_selector.hpp"
24
25 namespace color_widgets {
26
27 class ColorListWidget::Private
28 {
29 public:
30 QList<QColor> colors;
31 QSignalMapper mapper;
32 ColorWheel::ShapeEnum wheel_shape = ColorWheel::ShapeTriangle;
33 ColorWheel::ColorSpaceEnum color_space = ColorWheel::ColorHSV;
34 bool wheel_rotating = true;
35 };
36
ColorListWidget(QWidget * parent)37 ColorListWidget::ColorListWidget(QWidget *parent)
38 : AbstractWidgetList(parent), p(new Private)
39 {
40 connect(this, &AbstractWidgetList::removed, this, &ColorListWidget::handle_removed);
41 connect(&p->mapper, SIGNAL(mapped(int)), SLOT(color_changed(int)));
42 }
43
~ColorListWidget()44 ColorListWidget::~ColorListWidget()
45 {
46 delete p;
47 }
48
colors() const49 QList<QColor> ColorListWidget::colors() const
50 {
51 return p->colors;
52 }
53
setColors(const QList<QColor> & colors)54 void ColorListWidget::setColors(const QList<QColor> &colors)
55 {
56 clear();
57 p->colors = colors;
58 for(int i = 0;i < colors.size();i++ )
59 append_widget(i);
60 Q_EMIT colorsChanged(colors);
61 }
62
swap(int a,int b)63 void ColorListWidget::swap(int a, int b)
64 {
65 ColorSelector* sa = widget_cast<ColorSelector>(a);
66 ColorSelector* sb = widget_cast<ColorSelector>(b);
67 if ( sa && sb )
68 {
69 QColor ca = sa->color();
70 sa->setColor(sb->color());
71 sb->setColor(ca);
72 Q_EMIT colorsChanged(p->colors);
73 }
74 }
75
append()76 void ColorListWidget::append()
77 {
78 p->colors.push_back(Qt::black);
79 append_widget(p->colors.size()-1);
80 Q_EMIT colorsChanged(p->colors);
81 }
82
emit_changed()83 void ColorListWidget::emit_changed()
84 {
85 Q_EMIT colorsChanged(p->colors);
86 }
87
handle_removed(int i)88 void ColorListWidget::handle_removed(int i)
89 {
90 p->colors.removeAt(i);
91 Q_EMIT colorsChanged(p->colors);
92 }
93
color_changed(int row)94 void ColorListWidget::color_changed(int row)
95 {
96 ColorSelector *cs = widget_cast<ColorSelector>(row);
97 if ( cs )
98 {
99 p->colors[row] = cs->color();
100 Q_EMIT colorsChanged(p->colors);
101 }
102 }
103
append_widget(int col)104 void ColorListWidget::append_widget(int col)
105 {
106 ColorSelector* cbs = new ColorSelector;
107 cbs->setDisplayMode(ColorPreview::AllAlpha);
108 cbs->setColor(p->colors[col]);
109 //connect(cbs,SIGNAL(colorChanged(QColor)),SLOT(emit_changed()));
110 p->mapper.setMapping(cbs,col);
111 connect(cbs,SIGNAL(colorChanged(QColor)),&p->mapper,SLOT(map()));
112 connect(this, &ColorListWidget::wheelRotatingChanged, cbs, &ColorSelector::setWheelRotating);
113 connect(this, &ColorListWidget::wheelShapeChanged, cbs, &ColorSelector::setWheelShape);
114 connect(this, &ColorListWidget::colorSpaceChanged, cbs, &ColorSelector::setColorSpace);
115 appendWidget(cbs);
116 setRowHeight(count()-1,22);
117 }
118
setWheelShape(ColorWheel::ShapeEnum shape)119 void ColorListWidget::setWheelShape(ColorWheel::ShapeEnum shape)
120 {
121 Q_EMIT wheelShapeChanged(p->wheel_shape = shape);
122 }
123
wheelShape() const124 ColorWheel::ShapeEnum ColorListWidget::wheelShape() const
125 {
126 return p->wheel_shape;
127 }
128
setColorSpace(ColorWheel::ColorSpaceEnum space)129 void ColorListWidget::setColorSpace(ColorWheel::ColorSpaceEnum space)
130 {
131 Q_EMIT colorSpaceChanged(p->color_space = space);
132 }
133
colorSpace() const134 ColorWheel::ColorSpaceEnum ColorListWidget::colorSpace() const
135 {
136 return p->color_space;
137 }
138
setWheelRotating(bool rotating)139 void ColorListWidget::setWheelRotating(bool rotating)
140 {
141 Q_EMIT wheelRotatingChanged(p->wheel_rotating = rotating);
142 }
143
wheelRotating() const144 bool ColorListWidget::wheelRotating() const
145 {
146 return p->wheel_rotating;
147 }
148
149 } // namespace color_widgets
150