-
Notifications
You must be signed in to change notification settings - Fork 19
/
CColorProperty.cpp
146 lines (104 loc) · 3.07 KB
/
CColorProperty.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "CColorProperty.h"
#include <QColorDialog>
#include <QLineEdit>
CColorProperty::CColorProperty(const QByteArray& id, const QString &name, const QColor& color):
CBaseProperty(id, name),
m_color(color),
m_listColorsOnly(false)
{
setColor(m_color);
}
CColorProperty::CColorProperty(CBaseProperty *top, const QByteArray& id, const QString &name, const QColor& color):
CBaseProperty(top, id, name),
m_color(color),
m_listColorsOnly(false)
{
setColor(m_color);
}
void CColorProperty::setColor(const QColor &color)
{
m_color = color;
CBaseProperty::setValue();
}
const QColor &CColorProperty::getColor() const
{
return m_color;
}
void CColorProperty::setColorsList(const QStringList &colorNames)
{
m_colorEditor.setColorsList(colorNames);
CBaseProperty::setValue();
}
void CColorProperty::allowListColorsOnly(bool on)
{
m_listColorsOnly = on;
CBaseProperty::setValue();
}
QVariant CColorProperty::getVariantValue() const
{
return m_color;
}
void CColorProperty::validateValue()
{
if (!m_color.isValid())
{
m_color = QColor(Qt::black);
}
if (m_listColorsOnly && m_colorEditor.count())
{
if (m_colorEditor.findData(m_color) < 0)
m_color = QColor(m_colorEditor.itemText(0));
}
}
void CColorProperty::displayValue()
{
if (treeWidget())
treeWidget()->blockSignals(true);
QString colorNameExt = QString("HEX: %1\nRGB: %2,%3,%4")
.arg(m_color.name())
.arg(m_color.red()).arg(m_color.green()).arg(m_color.blue());
setText(1, m_colorEditor.colorName(m_color));
setIcon(1, QColorComboBox::colorIcon(m_color));
setToolTip(1, colorNameExt);
if (treeWidget())
treeWidget()->blockSignals(false);
}
QWidget *CColorProperty::createEditor() const
{
CColorButtonEditor* hostEditor = new CColorButtonEditor(&m_colorEditor, const_cast<CColorProperty*>(this));
hostEditor->enableButton(!m_listColorsOnly);
return hostEditor;
}
void CColorProperty::valueToEditor()
{
if (m_colorEditor.isVisible())
{
m_colorEditor.allowListColorsOnly(m_listColorsOnly);
m_colorEditor.setCurrentColor(m_color);
m_colorEditor.lineEdit()->selectAll();
}
}
void CColorProperty::valueFromEditor()
{
QColor col = m_colorEditor.currentColor();
if (col.isValid() && col != m_color)
{
setColor(col);
emitValueChanged();
}
}
// CColorButtonEditor
CColorProperty::CColorButtonEditor::CColorButtonEditor(QColorComboBox *colorComboEditor, CColorProperty *property)
: TButtonBasedEditor<QColorComboBox>(colorComboEditor)
{
m_property = property;
}
void CColorProperty::CColorButtonEditor::onEditButtonActivated()
{
QColorDialog colorDialog(getEditor()->currentColor());
if (colorDialog.exec() == QDialog::Accepted)
{
getEditor()->setCurrentColor(colorDialog.selectedColor());
m_property->finishEdit();
}
}