PrismLauncher/Group.cpp

170 lines
3.7 KiB
C++
Raw Normal View History

2014-01-31 21:51:45 +00:00
#include "Group.h"
2013-12-31 16:26:36 +00:00
#include <QModelIndex>
#include <QPainter>
#include <QtMath>
2014-01-31 21:51:45 +00:00
#include "GroupView.h"
2013-12-31 16:26:36 +00:00
2014-01-31 21:58:57 +00:00
Group::Group(const QString &text, GroupView *view) : view(view), text(text), collapsed(false)
2013-12-31 16:26:36 +00:00
{
}
2014-02-02 13:27:43 +00:00
2014-01-31 21:51:45 +00:00
Group::Group(const Group *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-01-31 21:51:45 +00:00
void Group::update()
2013-12-31 16:26:36 +00:00
{
firstItemIndex = firstItem().row();
2013-12-31 16:26:36 +00:00
rowHeights = QVector<int>(numRows());
for (int i = 0; i < numRows(); ++i)
{
2014-01-31 21:51:45 +00:00
rowHeights[i] = view->categoryRowHeight(
view->model()->index(i * view->itemsPerRow() + firstItemIndex, 0));
2013-12-31 16:26:36 +00:00
}
}
Group::HitResults Group::hitScan(const QPoint &pos) const
2014-02-02 13:27:43 +00:00
{
Group::HitResults results = Group::NoHit;
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();
if(y < y_start)
{
results = Group::NoHit;
}
else if(y < body_start)
{
results = Group::HeaderHit;
int collapseSize = headerHeight() - 4;
// the icon
QRect iconRect = QRect(view->m_leftMargin + 2, 2 + y_start, collapseSize, collapseSize);
if(iconRect.contains(pos))
{
results |= Group::CheckboxHit;
}
}
else if (y < body_end)
{
results |= Group::BodyHit;
}
return results;
}
2014-01-31 21:51:45 +00:00
void Group::drawHeader(QPainter *painter, const int y)
2013-12-31 16:26:36 +00:00
{
painter->save();
int height = headerHeight() - 4;
int collapseSize = height;
// the icon
2014-02-02 13:27:43 +00:00
QRect iconRect = QRect(view->m_leftMargin + 2, 2 + y, collapseSize, collapseSize);
2013-12-31 16:26:36 +00:00
painter->setPen(QPen(Qt::black, 1));
painter->drawRect(iconRect);
static const int margin = 2;
QRect iconSubrect = iconRect.adjusted(margin, margin, -margin, -margin);
int midX = iconSubrect.center().x();
int midY = iconSubrect.center().y();
if (collapsed)
{
painter->drawLine(midX, iconSubrect.top(), midX, iconSubrect.bottom());
}
painter->drawLine(iconSubrect.left(), midY, iconSubrect.right(), midY);
// the text
int textWidth = painter->fontMetrics().width(text);
2014-02-02 13:27:43 +00:00
QRect textRect = QRect(iconRect.right() + 4, y, textWidth, headerHeight());
2013-12-31 16:26:36 +00:00
painter->setBrush(view->viewOptions().palette.text());
2014-01-31 21:51:45 +00:00
view->style()->drawItemText(painter, textRect, Qt::AlignHCenter | Qt::AlignVCenter,
view->viewport()->palette(), true, text);
2013-12-31 16:26:36 +00:00
// the line
2014-01-31 21:51:45 +00:00
painter->drawLine(textRect.right() + 4, y + headerHeight() / 2,
view->contentWidth() - view->m_rightMargin, y + headerHeight() / 2);
2013-12-31 16:26:36 +00:00
painter->restore();
}
2014-01-31 21:51:45 +00:00
int Group::totalHeight() const
2013-12-31 16:26:36 +00:00
{
return headerHeight() + 5 + contentHeight(); // FIXME: wtf is that '5'?
2013-12-31 16:26:36 +00:00
}
2014-01-31 21:51:45 +00:00
int Group::headerHeight() const
2013-12-31 16:26:36 +00:00
{
return view->viewport()->fontMetrics().height() + 4;
}
2014-01-31 21:51:45 +00:00
int Group::contentHeight() const
2013-12-31 16:26:36 +00:00
{
if (collapsed)
{
return 0;
}
int result = 0;
for (int i = 0; i < rowHeights.size(); ++i)
{
result += rowHeights[i];
}
return result;
}
2014-01-31 21:51:45 +00:00
int Group::numRows() const
2013-12-31 16:26:36 +00:00
{
return qMax(1, qCeil((qreal)numItems() / (qreal)view->itemsPerRow()));
}
2014-01-31 21:51:45 +00:00
int Group::verticalPosition() const
2013-12-31 16:26:36 +00:00
{
int res = 0;
const QList<Group *> cats = view->m_groups;
2013-12-31 16:26:36 +00:00
for (int i = 0; i < cats.size(); ++i)
{
if (cats.at(i) == this)
{
break;
}
res += cats.at(i)->totalHeight() + view->m_categoryMargin;
}
return res;
}
2014-01-31 21:51:45 +00:00
QList<QModelIndex> Group::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);
if (index.data(GroupViewRoles::GroupRole).toString() == text)
2013-12-31 16:26:36 +00:00
{
indices.append(index);
}
}
return indices;
}
2014-01-31 21:51:45 +00:00
int Group::numItems() const
2013-12-31 16:26:36 +00:00
{
return items().size();
}
2014-01-31 21:51:45 +00:00
QModelIndex Group::firstItem() const
2013-12-31 16:26:36 +00:00
{
QList<QModelIndex> indices = items();
return indices.isEmpty() ? QModelIndex() : indices.first();
}
2014-01-31 21:51:45 +00:00
QModelIndex Group::lastItem() const
2013-12-31 16:26:36 +00:00
{
QList<QModelIndex> indices = items();
return indices.isEmpty() ? QModelIndex() : indices.last();
}