summaryrefslogtreecommitdiffstats
path: root/application/groupview/VisualGroup.cpp
blob: 940c7a8b45bdc208115fa89309b31bd645fa7e5a (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* Copyright 2013-2018 MultiMC Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "VisualGroup.h"

#include <QModelIndex>
#include <QPainter>
#include <QtMath>
#include <QApplication>
#include <QDebug>

#include "GroupView.h"

VisualGroup::VisualGroup(const QString &text, GroupView *view) : view(view), text(text), collapsed(false)
{
}

VisualGroup::VisualGroup(const VisualGroup *other)
	: view(other->view), text(other->text), collapsed(other->collapsed)
{
}

void VisualGroup::update()
{
	auto temp_items = items();
	auto itemsPerRow = view->itemsPerRow();

	int numRows = qMax(1, qCeil((qreal)temp_items.size() / (qreal)itemsPerRow));
	rows = QVector<VisualRow>(numRows);

	int maxRowHeight = 0;
	int positionInRow = 0;
	int currentRow = 0;
	int offsetFromTop = 0;
	for (auto item: temp_items)
	{
		if(positionInRow == itemsPerRow)
		{
			rows[currentRow].height = maxRowHeight;
			rows[currentRow].top = offsetFromTop;
			currentRow ++;
			offsetFromTop += maxRowHeight + 5;
			positionInRow = 0;
			maxRowHeight = 0;
		}
		auto itemHeight = view->itemDelegate()->sizeHint(view->viewOptions(), item).height();
		if(itemHeight > maxRowHeight)
		{
			maxRowHeight = itemHeight;
		}
		rows[currentRow].items.append(item);
		positionInRow++;
	}
	rows[currentRow].height = maxRowHeight;
	rows[currentRow].top = offsetFromTop;
}

QPair<int, int> VisualGroup::positionOf(const QModelIndex &index) const
{
	int y = 0;
	for (auto & row: rows)
	{
		for(auto x = 0; x < row.items.size(); x++)
		{
			if(row.items[x] == index)
			{
				return qMakePair(x,y);
			}
		}
		y++;
	}
	qWarning() << "Item" << index.row() << index.data(Qt::DisplayRole).toString() << "not found in visual group" << text;
	return qMakePair(0, 0);
}

int VisualGroup::rowTopOf(const QModelIndex &index) const
{
	auto position = positionOf(index);
	return rows[position.second].top;
}

int VisualGroup::rowHeightOf(const QModelIndex &index) const
{
	auto position = positionOf(index);
	return rows[position.second].height;
}

VisualGroup::HitResults VisualGroup::hitScan(const QPoint &pos) const
{
	VisualGroup::HitResults results = VisualGroup::NoHit;
	int y_start = verticalPosition();
	int body_start = y_start + headerHeight();
	int body_end = body_start + contentHeight() + 5; // FIXME: wtf is this 5?
	int y = pos.y();
	// int x = pos.x();
	if (y < y_start)
	{
		results = VisualGroup::NoHit;
	}
	else if (y < body_start)
	{
		results = VisualGroup::HeaderHit;
		int collapseSize = headerHeight() - 4;

		// the icon
		QRect iconRect = QRect(view->m_leftMargin + 2, 2 + y_start, collapseSize, collapseSize);
		if (iconRect.contains(pos))
		{
			results |= VisualGroup::CheckboxHit;
		}
	}
	else if (y < body_end)
	{
		results |= VisualGroup::BodyHit;
	}
	return results;
}

void VisualGroup::drawHeader(QPainter *painter, const QStyleOptionViewItem &option)
{
	painter->setRenderHint(QPainter::Antialiasing);

	const QRect optRect = option.rect;
	QFont font(QApplication::font());
	font.setBold(true);
	const QFontMetrics fontMetrics = QFontMetrics(font);

	QColor outlineColor = option.palette.text().color();
	outlineColor.setAlphaF(0.35);

	//BEGIN: top left corner
	{
		painter->save();
		painter->setPen(outlineColor);
		const QPointF topLeft(optRect.topLeft());
		QRectF arc(topLeft, QSizeF(4, 4));
		arc.translate(0.5, 0.5);
		painter->drawArc(arc, 1440, 1440);
		painter->restore();
	}
	//END: top left corner

	//BEGIN: left vertical line
	{
		QPoint start(optRect.topLeft());
		start.ry() += 3;
		QPoint verticalGradBottom(optRect.topLeft());
		verticalGradBottom.ry() += fontMetrics.height() + 5;
		QLinearGradient gradient(start, verticalGradBottom);
		gradient.setColorAt(0, outlineColor);
		gradient.setColorAt(1, Qt::transparent);
		painter->fillRect(QRect(start, QSize(1, fontMetrics.height() + 5)), gradient);
	}
	//END: left vertical line

	//BEGIN: horizontal line
	{
		QPoint start(optRect.topLeft());
		start.rx() += 3;
		QPoint horizontalGradTop(optRect.topLeft());
		horizontalGradTop.rx() += optRect.width() - 6;
		painter->fillRect(QRect(start, QSize(optRect.width() - 6, 1)), outlineColor);
	}
	//END: horizontal line

	//BEGIN: top right corner
	{
		painter->save();
		painter->setPen(outlineColor);
		QPointF topRight(optRect.topRight());
		topRight.rx() -= 4;
		QRectF arc(topRight, QSizeF(4, 4));
		arc.translate(0.5, 0.5);
		painter->drawArc(arc, 0, 1440);
		painter->restore();
	}
	//END: top right corner

	//BEGIN: right vertical line
	{
		QPoint start(optRect.topRight());
		start.ry() += 3;
		QPoint verticalGradBottom(optRect.topRight());
		verticalGradBottom.ry() += fontMetrics.height() + 5;
		QLinearGradient gradient(start, verticalGradBottom);
		gradient.setColorAt(0, outlineColor);
		gradient.setColorAt(1, Qt::transparent);
		painter->fillRect(QRect(start, QSize(1, fontMetrics.height() + 5)), gradient);
	}
	//END: right vertical line

	//BEGIN: checkboxy thing
	{
		painter->save();
		painter->setRenderHint(QPainter::Antialiasing, false);
		painter->setFont(font);
		QColor penColor(option.palette.text().color());
		penColor.setAlphaF(0.6);
		painter->setPen(penColor);
		QRect iconSubRect(option.rect);
		iconSubRect.setTop(iconSubRect.top() + 7);
		iconSubRect.setLeft(iconSubRect.left() + 7);

		int sizing = fontMetrics.height();
		int even = ( (sizing - 1) % 2 );

		iconSubRect.setHeight(sizing - even);
		iconSubRect.setWidth(sizing - even);
		painter->drawRect(iconSubRect);


		/*
		if(collapsed)
			painter->drawText(iconSubRect, Qt::AlignHCenter | Qt::AlignVCenter, "+");
		else
			painter->drawText(iconSubRect, Qt::AlignHCenter | Qt::AlignVCenter, "-");
		*/
		painter->setBrush(option.palette.text());
		painter->fillRect(iconSubRect.x(), iconSubRect.y() + iconSubRect.height() / 2,
						  iconSubRect.width(), 2, penColor);
		if (collapsed)
		{
			painter->fillRect(iconSubRect.x() + iconSubRect.width() / 2, iconSubRect.y(), 2,
							  iconSubRect.height(), penColor);
		}

		painter->restore();
	}
	//END: checkboxy thing

	//BEGIN: text
	{
		QRect textRect(option.rect);
		textRect.setTop(textRect.top() + 7);
		textRect.setLeft(textRect.left() + 7 + fontMetrics.height() + 7);
		textRect.setHeight(fontMetrics.height());
		textRect.setRight(textRect.right() - 7);

		painter->save();
		painter->setFont(font);
		QColor penColor(option.palette.text().color());
		penColor.setAlphaF(0.6);
		painter->setPen(penColor);
		painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
		painter->restore();
	}
	//END: text
}

int VisualGroup::totalHeight() const
{
	return headerHeight() + 5 + contentHeight(); // FIXME: wtf is that '5'?
}

int VisualGroup::headerHeight() const
{
	QFont font(QApplication::font());
    font.setBold(true);
    QFontMetrics fontMetrics(font);

    const int height = fontMetrics.height() + 1 /* 1 pixel-width gradient */
                                            + 11 /* top and bottom separation */;
    return height;
	/*
	int raw = view->viewport()->fontMetrics().height() + 4;
	// add english. maybe. depends on font height.
	if (raw % 2 == 0)
		raw++;
	return std::min(raw, 25);
	*/
}

int VisualGroup::contentHeight() const
{
	if (collapsed)
	{
		return 0;
	}
	auto last = rows[numRows() - 1];
	return last.top + last.height;
}

int VisualGroup::numRows() const
{
	return rows.size();
}

int VisualGroup::verticalPosition() const
{
	return m_verticalPosition;
}

QList<QModelIndex> VisualGroup::items() const
{
	QList<QModelIndex> indices;
	for (int i = 0; i < view->model()->rowCount(); ++i)
	{
		const QModelIndex index = view->model()->index(i, 0);
		if (index.data(GroupViewRoles::GroupRole).toString() == text)
		{
			indices.append(index);
		}
	}
	return indices;
}