File: lc_colorpicker.cpp

package info (click to toggle)
leocad 25.09-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 51,794; xml: 11,265; python: 81; sh: 52; makefile: 16
file content (204 lines) | stat: -rw-r--r-- 4,601 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "lc_global.h"
#include "lc_colorpicker.h"
#include "lc_colorlist.h"
#include "lc_colors.h"

lcColorPickerPopup::lcColorPickerPopup(QWidget* Parent, int ColorIndex, bool AllowNoColor)
	: QFrame(Parent, Qt::Popup)
{
	setFrameStyle(QFrame::StyledPanel);
	setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

	setFocusPolicy(Qt::StrongFocus);
	setMouseTracking(true);

	QGridLayout *layout = new QGridLayout(this);
	layout->setContentsMargins(0, 0, 0, 0);
	setLayout(layout);

	mColorList = new lcColorList(this, AllowNoColor);
	connect(mColorList, &lcColorList::ColorChanged, this, &lcColorPickerPopup::ColorChanged);
	connect(mColorList, &lcColorList::ColorSelected, this, &lcColorPickerPopup::ColorSelected);
	layout->addWidget(mColorList);

	mColorList->blockSignals(true);
	mColorList->SetCurrentColor(ColorIndex);
	mColorList->blockSignals(false);

	mEventLoop = nullptr;
}

lcColorPickerPopup::~lcColorPickerPopup()
{
	if (mEventLoop)
		mEventLoop->exit();
}

void lcColorPickerPopup::exec()
{
	show();

	QEventLoop EventLoop;
	mEventLoop = &EventLoop;
	(void) EventLoop.exec();
	mEventLoop = nullptr;
}

void lcColorPickerPopup::mouseReleaseEvent(QMouseEvent* MouseEvent)
{
	if (!rect().contains(MouseEvent->pos()))
		hide();
}

void lcColorPickerPopup::hideEvent(QHideEvent* HideEvent)
{
	if (mEventLoop)
		mEventLoop->exit();

	emit Hid();
	QFrame::hideEvent(HideEvent);
}

void lcColorPickerPopup::ColorChanged(int ColorIndex)
{
	emit Changed(ColorIndex);
}

void lcColorPickerPopup::ColorSelected(int ColorIndex)
{
	emit Selected(ColorIndex);
	hide();
}

void lcColorPickerPopup::showEvent(QShowEvent* ShowEvent)
{
	Q_UNUSED(ShowEvent);

	mColorList->setFocus();
}

lcColorPicker::lcColorPicker(QWidget* Parent, bool AllowNoColor)
	: QPushButton(Parent), mAllowNoColor(AllowNoColor)
{
	setFocusPolicy(Qt::StrongFocus);
	setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
	setAutoDefault(false);
	setCheckable(true);

	UpdateIcon();

	connect(this, &QPushButton::toggled, this, &lcColorPicker::ButtonPressed);
}

lcColorPicker::~lcColorPicker()
{
}

void lcColorPicker::SetCurrentColor(int ColorIndex)
{
	Selected(ColorIndex);
}

void lcColorPicker::SetCurrentColorCode(int ColorCode)
{
	SetCurrentColor(lcGetColorIndex(ColorCode));
}

int lcColorPicker::GetCurrentColor() const
{
	return mCurrentColorIndex;
}

int lcColorPicker::GetCurrentColorCode() const
{
	return gColorList[mCurrentColorIndex].Code;
}

void lcColorPicker::ButtonPressed(bool Toggled)
{
	if (!Toggled)
		return;

	lcColorPickerPopup* Popup = new lcColorPickerPopup(this, mCurrentColorIndex, mAllowNoColor);
	connect(Popup, &lcColorPickerPopup::Changed, this, &lcColorPicker::Changed);
	connect(Popup, &lcColorPickerPopup::Selected, this, &lcColorPicker::Selected);
	connect(Popup, &lcColorPickerPopup::Hid, this, &lcColorPicker::PopupClosed);
	Popup->setMinimumSize(300, 200);

#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
	QScreen* Screen = screen();
	const QRect Desktop = Screen ? Screen->geometry() : QRect();
#else
	const QRect Desktop = QApplication::desktop()->geometry();
#endif

	QPoint Pos = mapToGlobal(rect().bottomLeft());
	if (Pos.x() < Desktop.left())
		Pos.setX(Desktop.left());
	if (Pos.y() < Desktop.top())
		Pos.setY(Desktop.top());

	if ((Pos.x() + Popup->width()) > Desktop.width())
		Pos.setX(Desktop.width() - Popup->width());
	if ((Pos.y() + Popup->height()) > Desktop.bottom())
		Pos.setY(Desktop.bottom() - Popup->height());
	Popup->move(Pos);

	clearFocus();
	update();

	Popup->setFocus();
	Popup->show();
}

void lcColorPicker::UpdateIcon()
{
	const int IconSize = 14;//style()->pixelMetric(QStyle::PM_SmallIconSize);
	QPixmap Pixmap(IconSize, IconSize);

	QPainter Painter(&Pixmap);

	Painter.setPen(Qt::darkGray);

	const lcColor* Color = &gColorList[mCurrentColorIndex];

	if (Color->Code != LC_COLOR_NOCOLOR)
	{
		Painter.setBrush(QColor::fromRgbF(Color->Value[0], Color->Value[1], Color->Value[2]));
		Painter.drawRect(0, 0, Pixmap.width() - 1, Pixmap.height() - 1);
	}
	else
		lcDrawNoColorRect(Painter, QRect(0, 0, Pixmap.width() - 1, Pixmap.height() - 1));

	Painter.end();

	setIcon(QIcon(Pixmap));
}

void lcColorPicker::PopupClosed()
{
	if (mInitialColorIndex != mCurrentColorIndex)
		Changed(mInitialColorIndex);

	setChecked(false);
	setFocus();
}

void lcColorPicker::Changed(int ColorIndex)
{
	if (ColorIndex == mCurrentColorIndex)
		return;

	mCurrentColorIndex = ColorIndex;
	UpdateIcon();

	update();

	emit ColorChanged(mCurrentColorIndex);
}

void lcColorPicker::Selected(int ColorIndex)
{
	mInitialColorIndex = ColorIndex;
	Changed(ColorIndex);
}