pollymc/Group.cpp

170 lines
3.7 KiB
C++
Raw Normal View History

2014-02-01 03:21:45 +05:30
#include "Group.h"
2013-12-31 21:56:36 +05:30
#include <QModelIndex>
#include <QPainter>
#include <QtMath>
2014-02-01 03:21:45 +05:30
#include "GroupView.h"
2013-12-31 21:56:36 +05:30
2014-02-01 03:28:57 +05:30
Group::Group(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-02-01 03:21:45 +05:30
Group::Group(const Group *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-02-01 03:21:45 +05:30
void Group::update()
2013-12-31 21:56:36 +05:30
{
firstItemIndex = firstItem().row();
2013-12-31 21:56:36 +05:30
rowHeights = QVector<int>(numRows());
for (int i = 0; i < numRows(); ++i)
{
2014-02-01 03:21:45 +05:30
rowHeights[i] = view->categoryRowHeight(
view->model()->index(i * view->itemsPerRow() + firstItemIndex, 0));
2013-12-31 21:56:36 +05:30
}
}
Group::HitResults Group::hitScan(const QPoint &pos) const
2014-02-02 18:57:43 +05:30
{
Group::HitResults results = Group::NoHit;
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();
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-02-01 03:21:45 +05:30
void Group::drawHeader(QPainter *painter, const int y)
2013-12-31 21:56:36 +05:30
{
painter->save();
int height = headerHeight() - 4;
int collapseSize = height;
// the icon
2014-02-02 18:57:43 +05:30
QRect iconRect = QRect(view->m_leftMargin + 2, 2 + y, collapseSize, collapseSize);
2013-12-31 21:56:36 +05:30
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 18:57:43 +05:30
QRect textRect = QRect(iconRect.right() + 4, y, textWidth, headerHeight());
2013-12-31 21:56:36 +05:30
painter->setBrush(view->viewOptions().palette.text());
2014-02-01 03:21:45 +05:30
view->style()->drawItemText(painter, textRect, Qt::AlignHCenter | Qt::AlignVCenter,
view->viewport()->palette(), true, text);
2013-12-31 21:56:36 +05:30
// the line
2014-02-01 03:21:45 +05:30
painter->drawLine(textRect.right() + 4, y + headerHeight() / 2,
view->contentWidth() - view->m_rightMargin, y + headerHeight() / 2);
2013-12-31 21:56:36 +05:30
painter->restore();
}
2014-02-01 03:21:45 +05:30
int Group::totalHeight() const
2013-12-31 21:56:36 +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
int Group::headerHeight() const
2013-12-31 21:56:36 +05:30
{
return view->viewport()->fontMetrics().height() + 4;
}
2014-02-01 03:21:45 +05:30
int Group::contentHeight() const
2013-12-31 21:56:36 +05:30
{
if (collapsed)
{
return 0;
}
int result = 0;
for (int i = 0; i < rowHeights.size(); ++i)
{
result += rowHeights[i];
}
return result;
}
2014-02-01 03:21:45 +05:30
int Group::numRows() const
2013-12-31 21:56:36 +05:30
{
return qMax(1, qCeil((qreal)numItems() / (qreal)view->itemsPerRow()));
}
2014-02-01 03:21:45 +05:30
int Group::verticalPosition() const
2013-12-31 21:56:36 +05:30
{
int res = 0;
const QList<Group *> cats = view->m_groups;
2013-12-31 21:56:36 +05:30
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-02-01 03:21:45 +05:30
QList<QModelIndex> Group::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);
if (index.data(GroupViewRoles::GroupRole).toString() == text)
2013-12-31 21:56:36 +05:30
{
indices.append(index);
}
}
return indices;
}
2014-02-01 03:21:45 +05:30
int Group::numItems() const
2013-12-31 21:56:36 +05:30
{
return items().size();
}
2014-02-01 03:21:45 +05:30
QModelIndex Group::firstItem() const
2013-12-31 21:56:36 +05:30
{
QList<QModelIndex> indices = items();
return indices.isEmpty() ? QModelIndex() : indices.first();
}
2014-02-01 03:21:45 +05:30
QModelIndex Group::lastItem() const
2013-12-31 21:56:36 +05:30
{
QList<QModelIndex> indices = items();
return indices.isEmpty() ? QModelIndex() : indices.last();
}