src/widgets/colorbutton.cpp

Sat, 04 Aug 2018 21:46:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 04 Aug 2018 21:46:58 +0300
changeset 1428
ece049033adc
parent 1396
3442a59ab05c
permissions
-rw-r--r--

fixed a crash when trying to open a document for the 3rd time after closing it 2 times

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

mercurial