File: lc_collapsiblewidget.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 (107 lines) | stat: -rw-r--r-- 2,524 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
#include "lc_global.h"
#include "lc_collapsiblewidget.h"

QImage lcCollapsibleWidgetButton::mExpandedIcon;
QImage lcCollapsibleWidgetButton::mCollapsedIcon;

lcCollapsibleWidgetButton::lcCollapsibleWidgetButton(const QString& Title, QWidget* Parent)
	: QToolButton(Parent)
{
	setText(Title);
	setAutoRaise(true);
	setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
	setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
	setFocusPolicy(Qt::NoFocus);

	UpdateIcon();

	connect(this, &QToolButton::clicked, this, &lcCollapsibleWidgetButton::Clicked);
}

void lcCollapsibleWidgetButton::Collapse()
{
	if (mExpanded)
		Clicked();
}

void lcCollapsibleWidgetButton::UpdateIcon()
{
	if (mExpanded)
	{
		if (mExpandedIcon.isNull())
		{
			QImage Image(16, 16, QImage::Format::Format_ARGB32);
			Image.fill(QColor(0, 0, 0, 0));
			uint Color = palette().color(QPalette::Text).rgba();

			for (int y = 0; y < 8; y++)
				for (int x = y; x < 8 - y; x++)
					Image.setPixel(x + 4, y + 6, Color);

			mExpandedIcon = Image;
		}

		setIcon(QPixmap::fromImage(mExpandedIcon));
	}
	else
	{
		if (mCollapsedIcon.isNull())
		{
			QImage Image(16, 16, QImage::Format::Format_ARGB32);
			Image.fill(QColor(0, 0, 0, 0));
			uint Color = palette().color(QPalette::Text).rgba();

			for (int y = 0; y < 8; y++)
				for (int x = y; x < 8 - y; x++)
					Image.setPixel(y + 6, x + 4, Color);

			mCollapsedIcon = Image;
		}

		setIcon(QPixmap::fromImage(mCollapsedIcon));
	}
}

void lcCollapsibleWidgetButton::Clicked()
{
	mExpanded = !mExpanded;
	UpdateIcon();

	emit StateChanged(mExpanded);
}

lcCollapsibleWidget::lcCollapsibleWidget(const QString& Title, QWidget* Parent)
	: QWidget(Parent)
{
	QVBoxLayout* Layout = new QVBoxLayout(this);
//	Layout->setSpacing(0);
	Layout->setContentsMargins(0, 0, 0, 0);

	QHBoxLayout* TitleLayout = new QHBoxLayout();
	TitleLayout->setContentsMargins(0, 0, 0, 0);
	Layout->addLayout(TitleLayout);

	mTitleButton = new lcCollapsibleWidgetButton(Title, this);
	TitleLayout->addWidget(mTitleButton);

	connect(mTitleButton, &lcCollapsibleWidgetButton::StateChanged, this, &lcCollapsibleWidget::ButtonStateChanged);

	mChildWidget = new QWidget(this);
	Layout->addWidget(mChildWidget);
}

void lcCollapsibleWidget::ButtonStateChanged(bool Expanded)
{
	mChildWidget->setVisible(Expanded);
}

void lcCollapsibleWidget::Collapse()
{
	mTitleButton->Collapse();
}

void lcCollapsibleWidget::SetChildLayout(QLayout* Layout)
{
	Layout->setContentsMargins(12, 0, 0, 0);
	mChildWidget->setLayout(Layout);
}