File: resource-table.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (166 lines) | stat: -rw-r--r-- 4,775 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
#include "resource-table.hpp"
#include "plugin-state-helpers.hpp"
#include "ui-helpers.hpp"

#include <QGridLayout>
#include <QHeaderView>
#include <QShortcut>

namespace advss {

ResourceTable::ResourceTable(QWidget *parent, const QString &help,
			     const QString &addToolTip,
			     const QString &removeToolTip,
			     const QStringList &headers,
			     const std::function<void()> &openSettings)
	: QWidget(parent),
	  _table(new QTableWidget()),
	  _add(new QToolButton()),
	  _remove(new QToolButton()),
	  _help(new QLabel(help))
{
	_add->setProperty("themeID",
			  QVariant(QString::fromUtf8("addIconSmall")));
	_add->setProperty("class", QVariant(QString::fromUtf8("icon-plus")));
	_add->setToolTip(addToolTip);

	_remove->setProperty("themeID",
			     QVariant(QString::fromUtf8("removeIconSmall")));
	_remove->setProperty("class",
			     QVariant(QString::fromUtf8("icon-trash")));
	_remove->setToolTip(removeToolTip);

	_help->setWordWrap(true);
	_help->setAlignment(Qt::AlignCenter);

	_table->setColumnCount(headers.size());
	_table->horizontalHeader()->setSectionResizeMode(
		QHeaderView::ResizeMode::Interactive);
	_table->setHorizontalHeaderLabels(headers);
	_table->verticalHeader()->hide();
	_table->setCornerButtonEnabled(false);
	_table->setShowGrid(false);
	_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
	_table->setSelectionBehavior(QAbstractItemView::SelectRows);

	auto helpAndTableLayout = new QGridLayout();
	helpAndTableLayout->setContentsMargins(0, 0, 0, 0);
	helpAndTableLayout->addWidget(_table, 0, 0);
	helpAndTableLayout->addWidget(_help, 0, 0, Qt::AlignCenter);

	auto controlLayout = new QHBoxLayout;
	controlLayout->setContentsMargins(0, 0, 0, 0);
	controlLayout->addWidget(_add);
	controlLayout->addWidget(_remove);
	controlLayout->addStretch();

	auto layout = new QVBoxLayout();
	layout->addLayout(helpAndTableLayout);
	layout->addLayout(controlLayout);
	setLayout(layout);

	QWidget::connect(_add, SIGNAL(clicked()), this, SLOT(Add()));
	QWidget::connect(_remove, SIGNAL(clicked()), this, SLOT(Remove()));
	QWidget::connect(_table, &QTableWidget::cellDoubleClicked,
			 [openSettings]() { openSettings(); });

	auto settingsShortcut = new QShortcut(QKeySequence("F2"), this);
	settingsShortcut->setContext(Qt::WidgetWithChildrenShortcut);
	QWidget::connect(settingsShortcut, &QShortcut::activated, this,
			 openSettings);
	auto removeShortcut = new QShortcut(QKeySequence("Del"), this);
	removeShortcut->setContext(Qt::WidgetWithChildrenShortcut);
	QWidget::connect(removeShortcut, &QShortcut::activated, this,
			 [this]() { Remove(); });

	auto newShortcut = new QShortcut(QKeySequence("Ctrl+N"), this);
	newShortcut->setContext(Qt::WidgetWithChildrenShortcut);
	QWidget::connect(newShortcut, &QShortcut::activated, this,
			 [this]() { Add(); });
}

void ResourceTable::SetHelpVisible(bool visible) const
{
	_help->setVisible(visible);
}

void ResourceTable::HighlightAddButton(bool enable)
{
	if (_highlightConnection) {
		_highlightConnection->deleteLater();
		_highlightConnection = nullptr;
	}

	if (enable && HighlightUIElementsEnabled()) {
		_highlightConnection = HighlightWidget(_add, QColor(Qt::green));
	}
}

void ResourceTable::resizeEvent(QResizeEvent *)
{
	const auto columnCount = _table->columnCount();
	const auto columnSize = (_table->width() - 1) / columnCount;
	for (int i = 0; i < columnCount; ++i) {
		_table->horizontalHeader()->resizeSection(i, columnSize);
	}
}

void AddItemTableRow(QTableWidget *table, const QStringList &cellLabels)
{
	int row = table->rowCount();
	table->setRowCount(row + 1);

	int col = 0;
	for (const auto &cellLabel : cellLabels) {
		auto *item = new QTableWidgetItem(cellLabel);
		item->setToolTip(cellLabel);
		table->setItem(row, col, item);
		col++;
	}

	table->sortByColumn(0, Qt::AscendingOrder);
}

void UpdateItemTableRow(QTableWidget *table, int row,
			const QStringList &cellLabels)
{
	int col = 1; // Skip the name cell
	for (const auto &cellLabel : cellLabels) {
		auto item = table->item(row, col);
		item->setText(cellLabel);
		item->setToolTip(cellLabel);
		col++;
	}
}

void RenameItemTableRow(QTableWidget *table, const QString &oldName,
			const QString &newName)
{
	for (int row = 0; row < table->rowCount(); row++) {
		auto item = table->item(row, 0);
		if (!item) {
			continue;
		}

		if (item->text() == oldName) {
			item->setText(newName);
			table->sortByColumn(0, Qt::AscendingOrder);
			return;
		}
	}
	assert(false);
}

void RemoveItemTableRow(QTableWidget *table, const QString &name)
{
	for (int row = 0; row < table->rowCount(); ++row) {
		auto item = table->item(row, 0);
		if (item && item->text() == name) {
			table->removeRow(row);
			return;
		}
	}
	table->sortByColumn(0, Qt::AscendingOrder);
}

} // namespace advss