summaryrefslogtreecommitdiff
path: root/plugins/warp/ui/shared/constraint.cpp
blob: 4a4aa4e272b46d0871f5a239f27575d6e209a946 (plain)
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
#include "constraint.h"

#include <QGridLayout>
#include <QHeaderView>

#include "theme.h"

WarpConstraintItem::WarpConstraintItem(const Warp::Constraint& constraint) : m_constraint(constraint)
{
	QString guidStr = QString::fromStdString(constraint.guid.ToString());
	if (const auto offset = constraint.offset; offset)
		guidStr += QString(" @ %1").arg(*offset, 0, 16);
	setText(guidStr);
}

WarpConstraintItemModel::WarpConstraintItemModel(const QStringList& labels, QObject* parent) :
	QStandardItemModel(parent)
{
	this->setHorizontalHeaderLabels(labels);
}

void WarpConstraintItemModel::AddItem(WarpConstraintItem* item)
{
	QList<QStandardItem*> row = {};
	row.insert(COL_CONSTRAINT_ITEM, item);
	appendRow(row);
}

WarpConstraintItem* WarpConstraintItemModel::GetItem(const QModelIndex& index) const
{
	if (!index.isValid())
		return nullptr;
	return dynamic_cast<WarpConstraintItem*>(item(index.row(), COL_CONSTRAINT_ITEM));
}

QVariant WarpConstraintItemModel::data(const QModelIndex& index, int role) const
{
	// Highlight constraints that are found in analysis.
	if (role == Qt::BackgroundRole)
	{
		if (const auto item = GetItem(index); item)
		{
			auto itemConstraint = item->GetConstraint();
			// TODO: We really should store the guid in a hashmap or something instead of looping over it for every
			// item.
			for (const auto& constraint : m_matchedConstraints)
			{
				if (constraint.guid == itemConstraint.guid)
				{
					static QColor matchedColor = getThemeColor(GreenStandardHighlightColor);
					matchedColor.setAlpha(100);
					if (constraint.offset == itemConstraint.offset)
						matchedColor.setAlpha(128);
					return matchedColor;
				}
			}
		}
	}

	return QStandardItemModel::data(index, role);
}

WarpConstraintTableWidget::WarpConstraintTableWidget(QWidget* parent)
{
	QGridLayout* layout = new QGridLayout(this);
	layout->setContentsMargins(2, 2, 2, 2);
	layout->setVerticalSpacing(4);

	m_table = new QTableView(this);
	m_model = new WarpConstraintItemModel({"Constraint"}, this);
	m_proxyModel = new GenericTextFilterModel(this);
	m_proxyModel->setSourceModel(m_model);
	m_table->setModel(m_proxyModel);

	m_filterEdit = new FilterEdit(this);
	m_filterView = new FilteredView(this, m_table, this, m_filterEdit);
	m_filterView->setFilterPlaceholderText("Search constraints");

	layout->addWidget(m_filterEdit, 0, 0, 1, 5);
	layout->addWidget(m_table, 1, 0, 1, 5);

	// Make the table look nice.
	m_table->horizontalHeader()->setStretchLastSection(true);
	m_table->verticalHeader()->hide();
	m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
	m_table->setSelectionMode(QAbstractItemView::SingleSelection);
	m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
	m_table->setFocusPolicy(Qt::NoFocus);
	m_table->setShowGrid(false);
	m_table->setAlternatingRowColors(false);
	m_table->setSortingEnabled(true);
	// NOTE: We only have a single column right now, so disable the header.
	m_table->horizontalHeader()->hide();
	// Decrease row height to make it look nice.
	m_table->verticalHeader()->setDefaultSectionSize(30);

	// Make the highlight less bright.
	QPalette palette = m_table->palette();
	palette.setColor(QPalette::Highlight, getThemeColor(SelectionColor));
	m_table->setPalette(palette);
}

void WarpConstraintTableWidget::SetConstraints(QVector<WarpConstraintItem*> constraints)
{
	// Clear matches as they are no longer valid.
	m_model->clear();
	m_model->setRowCount(0);

	// Temporarily disable sorting so we can add rows faster
	m_table->setModel(m_model);
	m_table->setSortingEnabled(false);
	m_table->setEnabled(false);

	for (const auto& constraint : constraints)
		m_model->AddItem(constraint);

	// We are done, re-enable table.
	m_table->setEnabled(true);
	m_table->setModel(m_proxyModel);
	m_table->setSortingEnabled(true);
}

void WarpConstraintTableWidget::SetMatchedConstraints(const std::vector<Warp::Constraint>& analysisConstraints)
{
	m_model->SetMatchedConstraints(analysisConstraints);
}

void WarpConstraintTableWidget::setFilter(const std::string& filter, FilterOptions options)
{
	m_proxyModel->setFilterFixedString(QString::fromStdString(filter));
	m_proxyModel->setFilterCaseSensitivity(
		options.testFlag(CaseSensitiveOption) ? Qt::CaseSensitive : Qt::CaseInsensitive);
	m_filterView->showFilter(QString::fromStdString(filter));
}