Group View: Use painting code from the previous group headers, small optimizations
This commit is contained in:
		@@ -3,6 +3,7 @@
 | 
			
		||||
#include <QModelIndex>
 | 
			
		||||
#include <QPainter>
 | 
			
		||||
#include <QtMath>
 | 
			
		||||
#include <QApplication>
 | 
			
		||||
 | 
			
		||||
#include "GroupView.h"
 | 
			
		||||
 | 
			
		||||
@@ -58,8 +59,9 @@ Group::HitResults Group::hitScan(const QPoint &pos) const
 | 
			
		||||
	return results;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Group::drawHeader(QPainter *painter, const QStyleOptionViewItem &option, const int y)
 | 
			
		||||
void Group::drawHeader(QPainter *painter, const QStyleOptionViewItem &option)
 | 
			
		||||
{
 | 
			
		||||
	/*
 | 
			
		||||
	QStyleOptionViewItemV4 opt = option;
 | 
			
		||||
	painter->save();
 | 
			
		||||
 | 
			
		||||
@@ -107,6 +109,95 @@ void Group::drawHeader(QPainter *painter, const QStyleOptionViewItem &option, co
 | 
			
		||||
					  midY + 1);
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int Group::totalHeight() const
 | 
			
		||||
@@ -144,17 +235,7 @@ int Group::numRows() const
 | 
			
		||||
 | 
			
		||||
int Group::verticalPosition() const
 | 
			
		||||
{
 | 
			
		||||
	int res = 0;
 | 
			
		||||
	const QList<Group *> cats = view->m_groups;
 | 
			
		||||
	for (int i = 0; i < cats.size(); ++i)
 | 
			
		||||
	{
 | 
			
		||||
		if (cats.at(i) == this)
 | 
			
		||||
		{
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
		res += cats.at(i)->totalHeight() + view->m_categoryMargin;
 | 
			
		||||
	}
 | 
			
		||||
	return res;
 | 
			
		||||
	return m_verticalPosition;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QList<QModelIndex> Group::items() const
 | 
			
		||||
 
 | 
			
		||||
@@ -16,18 +16,19 @@ struct Group
 | 
			
		||||
	Group(const Group *other);
 | 
			
		||||
 | 
			
		||||
/* data */
 | 
			
		||||
	GroupView *view;
 | 
			
		||||
	GroupView *view = nullptr;
 | 
			
		||||
	QString text;
 | 
			
		||||
	bool collapsed;
 | 
			
		||||
	bool collapsed = false;
 | 
			
		||||
	QVector<int> rowHeights;
 | 
			
		||||
	int firstItemIndex;
 | 
			
		||||
	int firstItemIndex = 0;
 | 
			
		||||
	int m_verticalPosition = 0;
 | 
			
		||||
 | 
			
		||||
/* logic */
 | 
			
		||||
	/// do stuff. and things. TODO: redo.
 | 
			
		||||
	void update();
 | 
			
		||||
 | 
			
		||||
	/// draw the header at y-position.
 | 
			
		||||
	void drawHeader(QPainter *painter, const QStyleOptionViewItem &option, const int y);
 | 
			
		||||
	void drawHeader(QPainter *painter, const QStyleOptionViewItem &option);
 | 
			
		||||
 | 
			
		||||
	/// height of the group, in total. includes a small bit of padding.
 | 
			
		||||
	int totalHeight() const;
 | 
			
		||||
 
 | 
			
		||||
@@ -114,12 +114,19 @@ void GroupView::updateGeometries()
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		int totalHeight = 0;
 | 
			
		||||
		// top margin
 | 
			
		||||
		totalHeight += m_categoryMargin;
 | 
			
		||||
		for (auto category : m_groups)
 | 
			
		||||
		{
 | 
			
		||||
			category->m_verticalPosition = totalHeight;
 | 
			
		||||
			totalHeight += category->totalHeight() + m_categoryMargin;
 | 
			
		||||
		}
 | 
			
		||||
		/*
 | 
			
		||||
		// remove the last margin (we don't want it)
 | 
			
		||||
		totalHeight -= m_categoryMargin;
 | 
			
		||||
		// add a margin on top...
 | 
			
		||||
		totalHeight += m_categoryMargin;
 | 
			
		||||
		*/
 | 
			
		||||
		totalHeight += m_bottomMargin;
 | 
			
		||||
		verticalScrollBar()->setRange(0, totalHeight - height());
 | 
			
		||||
	}
 | 
			
		||||
@@ -426,12 +433,22 @@ void GroupView::paintEvent(QPaintEvent *event)
 | 
			
		||||
	QStyleOptionViewItemV4 option(viewOptions());
 | 
			
		||||
	option.widget = this;
 | 
			
		||||
 | 
			
		||||
	int y = -verticalOffset();
 | 
			
		||||
	int wpWidth = viewport()->width();
 | 
			
		||||
	option.rect.setWidth(wpWidth);
 | 
			
		||||
	for (int i = 0; i < m_groups.size(); ++i)
 | 
			
		||||
	{
 | 
			
		||||
		Group *category = m_groups.at(i);
 | 
			
		||||
		category->drawHeader(&painter, option, y);
 | 
			
		||||
		int y = category->verticalPosition();
 | 
			
		||||
		y -= verticalOffset();
 | 
			
		||||
		QRect backup = option.rect;
 | 
			
		||||
		int height = category->totalHeight();
 | 
			
		||||
		option.rect.setTop(y);
 | 
			
		||||
		option.rect.setHeight(height);
 | 
			
		||||
		option.rect.setLeft(m_leftMargin);
 | 
			
		||||
		option.rect.setRight(wpWidth - m_rightMargin);
 | 
			
		||||
		category->drawHeader(&painter, option);
 | 
			
		||||
		y += category->totalHeight() + m_categoryMargin;
 | 
			
		||||
		option.rect = backup;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for (int i = 0; i < model()->rowCount(); ++i)
 | 
			
		||||
@@ -587,7 +604,6 @@ void GroupView::startDrag(Qt::DropActions supportedActions)
 | 
			
		||||
		QDrag *drag = new QDrag(this);
 | 
			
		||||
		drag->setPixmap(pixmap);
 | 
			
		||||
		drag->setMimeData(data);
 | 
			
		||||
		drag->setHotSpot(m_pressedPosition - rect.topLeft());
 | 
			
		||||
		Qt::DropAction defaultDropAction = Qt::IgnoreAction;
 | 
			
		||||
		if (this->defaultDropAction() != Qt::IgnoreAction &&
 | 
			
		||||
			(supportedActions & this->defaultDropAction()))
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user