2017-05-23 03:12:47 +05:30
|
|
|
/* Copyright 2013-2017 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.
|
|
|
|
*/
|
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
#include "VisualGroup.h"
|
2013-12-31 21:56:36 +05:30
|
|
|
|
|
|
|
#include <QModelIndex>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QtMath>
|
2014-02-09 02:16:29 +05:30
|
|
|
#include <QApplication>
|
2016-12-19 05:04:03 +05:30
|
|
|
#include <QDebug>
|
2013-12-31 21:56:36 +05:30
|
|
|
|
2014-02-01 03:21:45 +05:30
|
|
|
#include "GroupView.h"
|
2013-12-31 21:56:36 +05:30
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
VisualGroup::VisualGroup(const QString &text, GroupView *view) : view(view), text(text), collapsed(false)
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
|
|
|
}
|
2014-02-02 18:57:43 +05:30
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
VisualGroup::VisualGroup(const VisualGroup *other)
|
2014-02-02 18:57:43 +05:30
|
|
|
: view(other->view), text(other->text), collapsed(other->collapsed)
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
void VisualGroup::update()
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
2014-07-13 00:43:23 +05:30
|
|
|
auto temp_items = items();
|
|
|
|
auto itemsPerRow = view->itemsPerRow();
|
2013-12-31 21:56:36 +05:30
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
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)
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
2014-07-13 00:43:23 +05:30
|
|
|
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++;
|
2013-12-31 21:56:36 +05:30
|
|
|
}
|
2014-07-13 00:43:23 +05:30
|
|
|
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-19 05:04:03 +05:30
|
|
|
qWarning() << "Item" << index.row() << index.data(Qt::DisplayRole).toString() << "not found in visual group" << text;
|
|
|
|
return qMakePair(0, 0);
|
2014-07-13 00:43:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int VisualGroup::rowTopOf(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
auto position = positionOf(index);
|
|
|
|
return rows[position.second].top;
|
2013-12-31 21:56:36 +05:30
|
|
|
}
|
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
int VisualGroup::rowHeightOf(const QModelIndex &index) const
|
2014-02-02 18:57:43 +05:30
|
|
|
{
|
2014-07-13 00:43:23 +05:30
|
|
|
auto position = positionOf(index);
|
|
|
|
return rows[position.second].height;
|
|
|
|
}
|
|
|
|
|
|
|
|
VisualGroup::HitResults VisualGroup::hitScan(const QPoint &pos) const
|
|
|
|
{
|
|
|
|
VisualGroup::HitResults results = VisualGroup::NoHit;
|
2014-02-04 05:10:10 +05:30
|
|
|
int y_start = verticalPosition();
|
2014-02-02 18:57:43 +05:30
|
|
|
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 06:04:50 +05:30
|
|
|
if (y < y_start)
|
2014-02-02 18:57:43 +05:30
|
|
|
{
|
2014-07-13 00:43:23 +05:30
|
|
|
results = VisualGroup::NoHit;
|
2014-02-02 18:57:43 +05:30
|
|
|
}
|
2014-02-05 06:04:50 +05:30
|
|
|
else if (y < body_start)
|
2014-02-02 18:57:43 +05:30
|
|
|
{
|
2014-07-13 00:43:23 +05:30
|
|
|
results = VisualGroup::HeaderHit;
|
2014-02-02 18:57:43 +05:30
|
|
|
int collapseSize = headerHeight() - 4;
|
|
|
|
|
|
|
|
// the icon
|
|
|
|
QRect iconRect = QRect(view->m_leftMargin + 2, 2 + y_start, collapseSize, collapseSize);
|
2014-02-05 06:04:50 +05:30
|
|
|
if (iconRect.contains(pos))
|
2014-02-02 18:57:43 +05:30
|
|
|
{
|
2014-07-13 00:43:23 +05:30
|
|
|
results |= VisualGroup::CheckboxHit;
|
2014-02-02 18:57:43 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (y < body_end)
|
|
|
|
{
|
2014-07-13 00:43:23 +05:30
|
|
|
results |= VisualGroup::BodyHit;
|
2014-02-02 18:57:43 +05:30
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
void VisualGroup::drawHeader(QPainter *painter, const QStyleOptionViewItem &option)
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
2014-02-10 01:15:18 +05:30
|
|
|
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
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
2014-02-10 01:15:18 +05:30
|
|
|
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
|
2014-02-05 06:04:50 +05:30
|
|
|
|
2014-02-10 01:15:18 +05:30
|
|
|
//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);
|
2013-12-31 21:56:36 +05:30
|
|
|
}
|
2014-02-10 01:15:18 +05:30
|
|
|
//END: left vertical line
|
2013-12-31 21:56:36 +05:30
|
|
|
|
2014-02-10 01:15:18 +05:30
|
|
|
//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
|
2013-12-31 21:56:36 +05:30
|
|
|
|
2014-02-10 01:15:18 +05:30
|
|
|
//BEGIN: top right corner
|
2014-02-05 06:04:50 +05:30
|
|
|
{
|
2014-02-10 01:15:18 +05:30
|
|
|
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();
|
2014-02-05 06:04:50 +05:30
|
|
|
}
|
2014-02-10 01:15:18 +05:30
|
|
|
//END: top right corner
|
2013-12-31 21:56:36 +05:30
|
|
|
|
2014-02-10 01:15:18 +05:30
|
|
|
//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
|
2014-02-09 02:16:29 +05:30
|
|
|
|
2014-02-10 01:15:18 +05:30
|
|
|
//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
|
2013-12-31 21:56:36 +05:30
|
|
|
}
|
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
int VisualGroup::totalHeight() const
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
2014-02-04 05:10:10 +05:30
|
|
|
return headerHeight() + 5 + contentHeight(); // FIXME: wtf is that '5'?
|
2013-12-31 21:56:36 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
int VisualGroup::headerHeight() const
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
2014-02-10 01:15:18 +05:30
|
|
|
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 06:04:50 +05:30
|
|
|
int raw = view->viewport()->fontMetrics().height() + 4;
|
|
|
|
// add english. maybe. depends on font height.
|
2014-02-10 01:15:18 +05:30
|
|
|
if (raw % 2 == 0)
|
2014-02-05 06:04:50 +05:30
|
|
|
raw++;
|
2014-02-10 01:15:18 +05:30
|
|
|
return std::min(raw, 25);
|
|
|
|
*/
|
2013-12-31 21:56:36 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
int VisualGroup::contentHeight() const
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
|
|
|
if (collapsed)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-13 00:43:23 +05:30
|
|
|
auto last = rows[numRows() - 1];
|
|
|
|
return last.top + last.height;
|
2013-12-31 21:56:36 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
int VisualGroup::numRows() const
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
2014-07-13 00:43:23 +05:30
|
|
|
return rows.size();
|
2013-12-31 21:56:36 +05:30
|
|
|
}
|
2014-02-01 03:21:45 +05:30
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
int VisualGroup::verticalPosition() const
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
2014-02-09 02:16:29 +05:30
|
|
|
return m_verticalPosition;
|
2013-12-31 21:56:36 +05:30
|
|
|
}
|
|
|
|
|
2014-07-13 00:43:23 +05:30
|
|
|
QList<QModelIndex> VisualGroup::items() const
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
|
|
|
QList<QModelIndex> indices;
|
|
|
|
for (int i = 0; i < view->model()->rowCount(); ++i)
|
|
|
|
{
|
|
|
|
const QModelIndex index = view->model()->index(i, 0);
|
2014-02-04 05:10:10 +05:30
|
|
|
if (index.data(GroupViewRoles::GroupRole).toString() == text)
|
2013-12-31 21:56:36 +05:30
|
|
|
{
|
|
|
|
indices.append(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return indices;
|
|
|
|
}
|