|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2018 Teemu Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include "colorbutton.h" |
|
20 #include "../dialogs/colorselector.h" |
|
21 |
|
22 /* |
|
23 * Initializes the color button and sets the default color |
|
24 */ |
|
25 ColorButton::ColorButton(LDColor color, QWidget* parent) : |
|
26 ColorButton (parent) |
|
27 { |
|
28 setColor(color); |
|
29 } |
|
30 |
|
31 /* |
|
32 * Initializes the color button |
|
33 */ |
|
34 ColorButton::ColorButton(QWidget* parent) : |
|
35 QPushButton (parent) |
|
36 { |
|
37 connect( |
|
38 this, |
|
39 &QPushButton::clicked, |
|
40 [&]() |
|
41 { |
|
42 // The dialog must not have the button as the parent or the dialog inherits the stylesheet. |
|
43 ColorSelector dialog {qobject_cast<QWidget*>(this->parent()), _color}; |
|
44 |
|
45 if (dialog.exec() and dialog.selectedColor().isValid()) |
|
46 setColor(dialog.selectedColor()); |
|
47 } |
|
48 ); |
|
49 |
|
50 connect( |
|
51 this, |
|
52 &ColorButton::colorChanged, |
|
53 [&](LDColor) |
|
54 { |
|
55 if (_color.isValid()) |
|
56 { |
|
57 setFlat(true); |
|
58 setText(_color.name()); |
|
59 setStyleSheet(format( |
|
60 "background-color: %1; color: %2; border:none;", |
|
61 _color.hexcode(), |
|
62 _color.edgeColor().name() |
|
63 )); |
|
64 } |
|
65 else |
|
66 { |
|
67 setFlat(false); |
|
68 setText(""); |
|
69 setStyleSheet(""); |
|
70 } |
|
71 } |
|
72 ); |
|
73 } |
|
74 |
|
75 /* |
|
76 * Returns the currently selected color. |
|
77 */ |
|
78 LDColor ColorButton::color() const |
|
79 { |
|
80 return _color; |
|
81 } |
|
82 |
|
83 /* |
|
84 * Sets the currently selected color. |
|
85 */ |
|
86 void ColorButton::setColor(LDColor color) |
|
87 { |
|
88 _color = color; |
|
89 emit colorChanged(color); |
|
90 } |