pollymc/gui/groupview/Group.cpp

271 lines
6.8 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>
#include <QApplication>
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-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();
2014-02-05 06:04:50 +05:30
if (y < y_start)
2014-02-02 18:57:43 +05:30
{
results = Group::NoHit;
}
2014-02-05 06:04:50 +05:30
else if (y < body_start)
2014-02-02 18:57:43 +05:30
{
results = Group::HeaderHit;
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
{
results |= Group::CheckboxHit;
}
}
else if (y < body_end)
{
results |= Group::BodyHit;
}
return results;
}
void Group::drawHeader(QPainter *painter, const QStyleOptionViewItem &option)
2013-12-31 21:56:36 +05:30
{
/*
2014-02-05 06:04:50 +05:30
QStyleOptionViewItemV4 opt = option;
2013-12-31 21:56:36 +05:30
painter->save();
static const int margin = 2;
2014-02-05 06:04:50 +05:30
static const int spacing = 10;
int height = headerHeight();
int text_height = height - 2 * margin;
// set the text colors
QPalette::ColorGroup cg = QPalette::Normal;
painter->setPen(opt.palette.color(cg, QPalette::Text));
// set up geometry
QRect iconRect = QRect(view->m_leftMargin + margin, y + margin, text_height - 1, text_height - 1);
2013-12-31 21:56:36 +05:30
QRect iconSubrect = iconRect.adjusted(margin, margin, -margin, -margin);
2014-02-05 06:04:50 +05:30
QRect smallRect = iconSubrect.adjusted(margin, margin, -margin, -margin);
2013-12-31 21:56:36 +05:30
int midX = iconSubrect.center().x();
int midY = iconSubrect.center().y();
2014-02-05 06:04:50 +05:30
// checkboxy thingy
2013-12-31 21:56:36 +05:30
{
2014-02-05 06:04:50 +05:30
painter->drawRect(iconSubrect);
painter->setBrush(opt.palette.text());
painter->drawRect(smallRect.x(), midY, smallRect.width(), 2);
if(collapsed)
{
painter->drawRect(midX, smallRect.y(), 2, smallRect.height());
}
2013-12-31 21:56:36 +05:30
}
2014-02-05 06:04:50 +05:30
int x_left = iconRect.right();
2013-12-31 21:56:36 +05:30
2014-02-05 06:04:50 +05:30
if(text.length())
{
// the text
int text_width = painter->fontMetrics().width(text);
QRect textRect = QRect(x_left + spacing, y + margin, text_width, text_height);
x_left = textRect.right();
view->style()->drawItemText(painter, textRect, Qt::AlignHCenter | Qt::AlignVCenter,
opt.palette, true, text);
}
2013-12-31 21:56:36 +05:30
// the line
2014-02-05 06:04:50 +05:30
painter->drawLine(x_left + spacing, midY + 1, view->contentWidth() - view->m_rightMargin,
midY + 1);
2013-12-31 21:56:36 +05:30
painter->restore();
*/
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
{
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
//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);
}
//END: left vertical line
//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
//BEGIN: top right corner
{
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();
}
//END: top right corner
//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
//BEGIN: text
{
QRect textRect(option.rect);
textRect.setTop(textRect.top() + 7);
textRect.setLeft(textRect.left() + 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-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
{
2014-02-05 06:04:50 +05:30
int raw = view->viewport()->fontMetrics().height() + 4;
// add english. maybe. depends on font height.
if(raw % 2 == 0)
raw++;
return std::min( raw , 25 );
2013-12-31 21:56:36 +05:30
}
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
{
return m_verticalPosition;
2013-12-31 21:56:36 +05:30
}
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();
}