PrismLauncher/Group.cpp

139 lines
3.1 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-01-31 21:51:45 +00:00
Group::Group(const Group *other)
: view(other->view), text(other->text), collapsed(other->collapsed),
iconRect(other->iconRect), textRect(other->textRect)
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
{
firstRow = firstItem().row();
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() + firstRow, 0));
2013-12-31 16:26:36 +00:00
}
}
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
iconRect = QRect(view->m_rightMargin + 2, 2 + y, collapseSize, collapseSize);
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);
textRect = QRect(iconRect.right() + 4, y, textWidth, headerHeight());
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();
}
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::top() const
2013-12-31 16:26:36 +00:00
{
int res = 0;
2014-01-31 21:51:45 +00:00
const QList<Group *> cats = view->m_categories;
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(CategorizedViewRoles::CategoryRole).toString() == text)
{
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();
}