2022-05-26 22:18:54 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2017-05-22 22:42:47 +01:00
|
|
|
*
|
2022-05-26 22:18:54 +01:00
|
|
|
* 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.
|
2017-05-22 22:42:47 +01:00
|
|
|
*
|
2022-05-26 22:18:54 +01:00
|
|
|
* 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.
|
2017-05-22 22:42:47 +01:00
|
|
|
*
|
2022-05-26 22:18:54 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 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.
|
2017-05-22 22:42:47 +01:00
|
|
|
*/
|
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
#include "VisualGroup.h"
|
2013-12-31 16:26:36 +00:00
|
|
|
|
|
|
|
#include <QModelIndex>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QtMath>
|
2014-02-08 20:46:29 +00:00
|
|
|
#include <QApplication>
|
2016-12-18 23:34:03 +00:00
|
|
|
#include <QDebug>
|
2013-12-31 16:26:36 +00:00
|
|
|
|
2021-10-25 22:51:42 +01:00
|
|
|
#include "InstanceView.h"
|
2013-12-31 16:26:36 +00:00
|
|
|
|
2021-10-25 22:51:42 +01:00
|
|
|
VisualGroup::VisualGroup(const QString &text, InstanceView *view) : view(view), text(text), collapsed(false)
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
|
|
|
}
|
2014-02-02 13:27:43 +00:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
VisualGroup::VisualGroup(const VisualGroup *other)
|
2014-02-02 13:27:43 +00:00
|
|
|
: view(other->view), text(other->text), collapsed(other->collapsed)
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
void VisualGroup::update()
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
auto temp_items = items();
|
|
|
|
auto itemsPerRow = view->itemsPerRow();
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
int numRows = qMax(1, qCeil((qreal)temp_items.size() / (qreal)itemsPerRow));
|
|
|
|
rows = QVector<VisualRow>(numRows);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
int maxRowHeight = 0;
|
|
|
|
int positionInRow = 0;
|
|
|
|
int currentRow = 0;
|
|
|
|
int offsetFromTop = 0;
|
|
|
|
for (auto item: temp_items)
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
if(positionInRow == itemsPerRow)
|
|
|
|
{
|
|
|
|
rows[currentRow].height = maxRowHeight;
|
|
|
|
rows[currentRow].top = offsetFromTop;
|
|
|
|
currentRow ++;
|
|
|
|
offsetFromTop += maxRowHeight + 5;
|
|
|
|
positionInRow = 0;
|
|
|
|
maxRowHeight = 0;
|
|
|
|
}
|
2022-05-02 20:34:55 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
QStyleOptionViewItem viewItemOption;
|
|
|
|
view->initViewItemOption(&viewItemOption);
|
|
|
|
#else
|
|
|
|
QStyleOptionViewItem viewItemOption = view->viewOptions();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
auto itemHeight = view->itemDelegate()->sizeHint(viewItemOption, item).height();
|
2014-07-12 20:13:23 +01:00
|
|
|
if(itemHeight > maxRowHeight)
|
|
|
|
{
|
|
|
|
maxRowHeight = itemHeight;
|
|
|
|
}
|
|
|
|
rows[currentRow].items.append(item);
|
|
|
|
positionInRow++;
|
2013-12-31 16:26:36 +00:00
|
|
|
}
|
2014-07-12 20:13:23 +01:00
|
|
|
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++;
|
|
|
|
}
|
2016-12-18 23:34:03 +00:00
|
|
|
qWarning() << "Item" << index.row() << index.data(Qt::DisplayRole).toString() << "not found in visual group" << text;
|
|
|
|
return qMakePair(0, 0);
|
2014-07-12 20:13:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int VisualGroup::rowTopOf(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
auto position = positionOf(index);
|
|
|
|
return rows[position.second].top;
|
2013-12-31 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
int VisualGroup::rowHeightOf(const QModelIndex &index) const
|
2014-02-02 13:27:43 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
auto position = positionOf(index);
|
|
|
|
return rows[position.second].height;
|
|
|
|
}
|
|
|
|
|
|
|
|
VisualGroup::HitResults VisualGroup::hitScan(const QPoint &pos) const
|
|
|
|
{
|
|
|
|
VisualGroup::HitResults results = VisualGroup::NoHit;
|
2014-02-03 23:40:10 +00:00
|
|
|
int y_start = verticalPosition();
|
2014-02-02 13:27:43 +00:00
|
|
|
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();
|
2014-02-05 00:34:50 +00:00
|
|
|
if (y < y_start)
|
2014-02-02 13:27:43 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
results = VisualGroup::NoHit;
|
2014-02-02 13:27:43 +00:00
|
|
|
}
|
2014-02-05 00:34:50 +00:00
|
|
|
else if (y < body_start)
|
2014-02-02 13:27:43 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
results = VisualGroup::HeaderHit;
|
2014-02-02 13:27:43 +00:00
|
|
|
int collapseSize = headerHeight() - 4;
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2014-02-02 13:27:43 +00:00
|
|
|
// the icon
|
|
|
|
QRect iconRect = QRect(view->m_leftMargin + 2, 2 + y_start, collapseSize, collapseSize);
|
2014-02-05 00:34:50 +00:00
|
|
|
if (iconRect.contains(pos))
|
2014-02-02 13:27:43 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
results |= VisualGroup::CheckboxHit;
|
2014-02-02 13:27:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (y < body_end)
|
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
results |= VisualGroup::BodyHit;
|
2014-02-02 13:27:43 +00:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
void VisualGroup::drawHeader(QPainter *painter, const QStyleOptionViewItem &option)
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
2023-06-28 17:42:49 +01:00
|
|
|
QRect optRect = option.rect;
|
|
|
|
optRect.setTop(optRect.top() + 7);
|
2014-02-09 19:45:18 +00:00
|
|
|
QFont font(QApplication::font());
|
|
|
|
font.setBold(true);
|
|
|
|
const QFontMetrics fontMetrics = QFontMetrics(font);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-06-28 17:42:49 +01:00
|
|
|
int centerHeight = optRect.top() + fontMetrics.height()/2;
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-06-28 17:00:40 +01:00
|
|
|
QPen pen;
|
|
|
|
pen.setWidth(2);
|
|
|
|
QColor penColor = option.palette.text().color();
|
|
|
|
penColor.setAlphaF(0.6);
|
|
|
|
pen.setColor(penColor);
|
|
|
|
painter->setPen(pen);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-06-28 17:42:49 +01:00
|
|
|
int arrowOffsetLeft = fontMetrics.height()/2 + 7;
|
|
|
|
int textOffsetLeft = arrowOffsetLeft *2;
|
|
|
|
int arrowSize = 6;
|
|
|
|
|
2023-06-28 17:00:40 +01:00
|
|
|
//BEGIN: arrow
|
2014-02-09 19:45:18 +00:00
|
|
|
{
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing, false);
|
2023-06-28 17:00:40 +01:00
|
|
|
painter->save();
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-06-28 17:00:40 +01:00
|
|
|
QPolygon polygon;
|
|
|
|
if (collapsed) {
|
2023-06-28 17:42:49 +01:00
|
|
|
polygon << QPoint(arrowOffsetLeft - arrowSize/2, centerHeight - arrowSize) << QPoint(arrowOffsetLeft + arrowSize/2, centerHeight) << QPoint(arrowOffsetLeft - arrowSize/2, centerHeight + arrowSize);
|
2023-06-28 17:00:40 +01:00
|
|
|
painter->drawPolyline(polygon);
|
|
|
|
} else {
|
2023-06-28 17:42:49 +01:00
|
|
|
polygon << QPoint(arrowOffsetLeft - arrowSize, centerHeight - arrowSize/2) << QPoint(arrowOffsetLeft, centerHeight + arrowSize/2) << QPoint(arrowOffsetLeft + arrowSize, centerHeight - arrowSize/2);
|
2023-06-28 17:00:40 +01:00
|
|
|
painter->drawPolyline(polygon);
|
2014-02-09 19:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-28 17:00:40 +01:00
|
|
|
//END: arrow
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2014-02-09 19:45:18 +00:00
|
|
|
//BEGIN: text
|
|
|
|
{
|
2023-06-28 17:00:40 +01:00
|
|
|
painter->setRenderHint(QPainter::Antialiasing);
|
2023-06-28 17:42:49 +01:00
|
|
|
QRect textRect(optRect);
|
2023-06-28 17:00:40 +01:00
|
|
|
textRect.setTop(textRect.top());
|
2023-06-28 17:42:49 +01:00
|
|
|
textRect.setLeft(textOffsetLeft);
|
2014-02-09 19:45:18 +00:00
|
|
|
textRect.setHeight(fontMetrics.height());
|
|
|
|
textRect.setRight(textRect.right() - 7);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2014-02-09 19:45:18 +00:00
|
|
|
painter->save();
|
|
|
|
painter->setFont(font);
|
|
|
|
painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
|
|
|
|
}
|
|
|
|
//END: text
|
2013-12-31 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
int VisualGroup::totalHeight() const
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
2023-06-28 17:42:49 +01:00
|
|
|
return headerHeight() + contentHeight();
|
2013-12-31 16:26:36 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
int VisualGroup::headerHeight() const
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
2014-02-09 19:45:18 +00:00
|
|
|
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;
|
|
|
|
/*
|
2014-02-05 00:34:50 +00:00
|
|
|
int raw = view->viewport()->fontMetrics().height() + 4;
|
|
|
|
// add english. maybe. depends on font height.
|
2014-02-09 19:45:18 +00:00
|
|
|
if (raw % 2 == 0)
|
2014-02-05 00:34:50 +00:00
|
|
|
raw++;
|
2014-02-09 19:45:18 +00:00
|
|
|
return std::min(raw, 25);
|
|
|
|
*/
|
2013-12-31 16:26:36 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
int VisualGroup::contentHeight() const
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
|
|
|
if (collapsed)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-12 20:13:23 +01:00
|
|
|
auto last = rows[numRows() - 1];
|
|
|
|
return last.top + last.height;
|
2013-12-31 16:26:36 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
int VisualGroup::numRows() const
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
2014-07-12 20:13:23 +01:00
|
|
|
return rows.size();
|
2013-12-31 16:26:36 +00:00
|
|
|
}
|
2014-01-31 21:51:45 +00:00
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
int VisualGroup::verticalPosition() const
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
2014-02-08 20:46:29 +00:00
|
|
|
return m_verticalPosition;
|
2013-12-31 16:26:36 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 20:13:23 +01:00
|
|
|
QList<QModelIndex> VisualGroup::items() const
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
|
|
|
QList<QModelIndex> indices;
|
|
|
|
for (int i = 0; i < view->model()->rowCount(); ++i)
|
|
|
|
{
|
|
|
|
const QModelIndex index = view->model()->index(i, 0);
|
2021-10-25 22:51:42 +01:00
|
|
|
if (index.data(InstanceViewRoles::GroupRole).toString() == text)
|
2013-12-31 16:26:36 +00:00
|
|
|
{
|
|
|
|
indices.append(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return indices;
|
|
|
|
}
|