summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/launcher/ui/widgets/ProjectItem.cpp
blob: 073d9515337972c2c56840f692bda9fb9951f51b (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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
 *  ProjT Launcher - Minecraft Launcher
 *  Copyright (C) 2026 Project Tick
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, version 3.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include "ProjectItem.h"

#include <QApplication>

#include <QDebug>
#include <QIcon>
#include <QPainter>
#include "Common.h"

ProjectItemDelegate::ProjectItemDelegate(QWidget* parent) : QStyledItemDelegate(parent)
{}

void ProjectItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	painter->save();

	QStyleOptionViewItem opt(option);
	initStyleOption(&opt, index);

	auto isInstalled = index.data(UserDataTypes::INSTALLED).toBool();
	auto isChecked	 = opt.checkState == Qt::Checked;
	auto isSelected	 = option.state & QStyle::State_Selected;

	const QStyle* style = opt.widget == nullptr ? QApplication::style() : opt.widget->style();

	auto rect = opt.rect;

	style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);

	if (isSelected && style->objectName() != "windowsvista")
		painter->setPen(opt.palette.highlightedText().color());

	if (opt.features & QStyleOptionViewItem::HasCheckIndicator)
	{
		QStyleOptionViewItem checkboxOpt = makeCheckboxStyleOption(opt, style);
		style->drawPrimitive(QStyle::PE_IndicatorItemViewItemCheck, &checkboxOpt, painter, opt.widget);

		rect.setX(checkboxOpt.rect.right());
	}

	if (!isSelected && !isChecked && isInstalled)
	{
		painter->setOpacity(0.4); // Fade out the entire item
	}
	// The default icon size will be a square (and height is usually the lower value).
	auto icon_width = rect.height(), icon_height = rect.height();
	int icon_x_margin = (rect.height() - icon_width) / 2;
	int icon_y_margin = (rect.height() - icon_height) / 2;

	if (!opt.icon.isNull())
	{ // Icon painting
		{
			auto icon_size = opt.decorationSize;
			icon_width	   = icon_size.width();
			icon_height	   = icon_size.height();

			icon_y_margin = (rect.height() - icon_height) / 2;
			icon_x_margin = icon_y_margin; // use same margins for consistency
		}

		// Centralize icon with a margin to separate from the other elements
		int x = rect.x() + icon_x_margin;
		int y = rect.y() + icon_y_margin;

		if (opt.features & QStyleOptionViewItem::HasCheckIndicator)
			rect.translate(icon_x_margin / 2, 0);

		// Prevent 'scaling null pixmap' warnings
		if (icon_width > 0 && icon_height > 0)
			opt.icon.paint(painter, x, y, icon_width, icon_height);
	}

	// Change the rect so that funther painting is easier
	auto remaining_width = rect.width() - icon_width - 2 * icon_x_margin;
	rect.setRect(rect.x() + icon_width + 2 * icon_x_margin, rect.y(), remaining_width, rect.height());

	int title_height = 0;

	{ // Title painting
		auto title = index.data(UserDataTypes::TITLE).toString();

		painter->save();

		auto font = opt.font;
		if (isChecked)
		{
			font.setBold(true);
		}
		if (isInstalled)
		{
			title = tr("%1 [installed]").arg(title);
		}

		font.setPointSize(font.pointSize() + 2);
		painter->setFont(font);

		title_height = QFontMetrics(font).height();

		// On the top, aligned to the left after the icon
		painter->drawText(rect.x(), rect.y() + title_height, title);

		painter->restore();
	}

	{ // Description painting
		auto description = index.data(UserDataTypes::DESCRIPTION).toString().simplified();

		QTextLayout text_layout(description, opt.font);

		qreal height  = 0;
		auto cut_text = viewItemTextLayout(text_layout, remaining_width, height);

		// Get first line unconditionally
		description	   = cut_text.first().second;
		auto num_lines = 1;

		// Get second line, elided if needed
		if (cut_text.size() > 1)
		{
			// 2.5x so because there should be some margin left from the 2x so things don't get too squishy.
			if (rect.height() - title_height <= 2.5 * opt.fontMetrics.height())
			{
				// If there's not enough space, show only a single line, elided.
				description = opt.fontMetrics.elidedText(description, opt.textElideMode, cut_text.at(0).first);
			}
			else
			{
				if (cut_text.size() > 2)
				{
					description +=
						opt.fontMetrics.elidedText(cut_text.at(1).second, opt.textElideMode, cut_text.at(1).first);
				}
				else
				{
					description += cut_text.at(1).second;
				}
				num_lines += 1;
			}
		}

		int description_x = rect.x();

		// Have the y-value be set based on the number of lines in the description, to centralize the
		// description text with the space between the base and the title.
		int description_y = rect.y() + title_height + (rect.height() - title_height) / 2;
		if (num_lines == 1)
			description_y -= opt.fontMetrics.height() / 2;
		else
			description_y -= opt.fontMetrics.height();

		// On the bottom, aligned to the left after the icon, and featuring at most two lines of text (with some margin
		// space to spare)
		painter->drawText(description_x,
						  description_y,
						  remaining_width,
						  cut_text.size() * opt.fontMetrics.height(),
						  Qt::TextWordWrap,
						  description);
	}

	painter->restore();
}

bool ProjectItemDelegate::editorEvent(QEvent* event,
									  QAbstractItemModel* model,
									  const QStyleOptionViewItem& option,
									  const QModelIndex& index)
{
	if (!(event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::MouseButtonPress
		  || event->type() == QEvent::MouseButtonDblClick))
		return false;

	auto mouseEvent = (QMouseEvent*)event;

	if (mouseEvent->button() != Qt::LeftButton)
		return false;

	QStyleOptionViewItem opt(option);
	initStyleOption(&opt, index);

	const QStyle* style = opt.widget == nullptr ? QApplication::style() : opt.widget->style();

	const QStyleOptionViewItem checkboxOpt = makeCheckboxStyleOption(opt, style);

	if (!checkboxOpt.rect.contains(mouseEvent->pos().x(), mouseEvent->pos().y()))
		return false;

	// swallow other events
	// (prevents item being selected or double click action triggering)
	if (event->type() != QEvent::MouseButtonRelease)
		return true;

	emit checkboxClicked(index);
	return true;
}

QStyleOptionViewItem ProjectItemDelegate::makeCheckboxStyleOption(const QStyleOptionViewItem& opt,
																  const QStyle* style) const
{
	QStyleOptionViewItem checkboxOpt = opt;

	checkboxOpt.state &= ~QStyle::State_HasFocus;

	if (checkboxOpt.checkState == Qt::Checked)
		checkboxOpt.state |= QStyle::State_On;
	else
		checkboxOpt.state |= QStyle::State_Off;

	QRect checkboxRect = style->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, &checkboxOpt, opt.widget);
	// 5px is the typical top margin for image
	// we don't want the checkboxes to be all over the place :)
	checkboxOpt.rect = QRect(opt.rect.x() + 5,
							 opt.rect.y() + (opt.rect.height() / 2 - checkboxRect.height() / 2),
							 checkboxRect.width(),
							 checkboxRect.height());

	return checkboxOpt;
}