r24658 by jghali - Refactor ScTreeWidgetDelegate::paint() to exit early when possible

scribus-commit scribus-commit at lists.scribus.net
Fri Jun 4 21:33:05 UTC 2021


Author: jghali
Date: Fri Jun  4 21:33:05 2021
New Revision: 24658

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=24658
Log:
Refactor ScTreeWidgetDelegate::paint() to exit early when possible

Modified:
    trunk/Scribus/scribus/ui/sctreewidget.cpp

Modified: trunk/Scribus/scribus/ui/sctreewidget.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=24658&path=/trunk/Scribus/scribus/ui/sctreewidget.cpp
==============================================================================
--- trunk/Scribus/scribus/ui/sctreewidget.cpp	(original)
+++ trunk/Scribus/scribus/ui/sctreewidget.cpp	Fri Jun  4 21:33:05 2021
@@ -38,52 +38,54 @@
 {
 	const QAbstractItemModel *model = index.model();
 	Q_ASSERT(model);
-	if (!model->parent(index).isValid())
-	{
-		// this is a top-level item.
-		QStyleOptionButton buttonOption;
-		buttonOption.state = option.state;
+
+	if (model->parent(index).isValid())
+	{
+		QItemDelegate::paint(painter, option, index);
+		return;
+	}
+
+	// This is a top-level item.
+	QStyleOptionButton buttonOption;
+	buttonOption.state = option.state;
 #if defined(Q_OS_MAC) || defined(Q_OS_WIN)
-		buttonOption.state |= QStyle::State_Raised;
+	buttonOption.state |= QStyle::State_Raised;
 #endif
-		buttonOption.state &= ~QStyle::State_HasFocus;
-		buttonOption.rect = option.rect;
-		buttonOption.palette = option.palette;
-		m_view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, m_view);
-
-		// Draw arrow
-		static const int i = 9; // ### hardcoded in qcommonstyle.cpp
-		QRect r = option.rect;
-		QPalette::ColorGroup arrowColorGroup = (option.state & QStyle::State_Enabled) ? QPalette::Active : QPalette::Disabled;
-		QColor arrowColor = option.palette.color(arrowColorGroup, QPalette::ButtonText);
-		painter->save();
-		painter->setBrush(arrowColor);
-		painter->setPen(Qt::NoPen);
-		QRect rect = QRect(r.left() + 6, r.top() + 6, r.height() - 12, r.height() - 12);
-		QPolygon pa(3);
-		if (m_view->isExpanded(index))
-		{
-			pa.setPoint(0, rect.left(), rect.top());
-			pa.setPoint(1, rect.right(), rect.top());
-			pa.setPoint(2, rect.center().x(), rect.bottom());
-		}
-		else
-		{
-			pa.setPoint(0, rect.left(), rect.top());
-			pa.setPoint(1, rect.left(), rect.bottom());
-			pa.setPoint(2, rect.right(), rect.center().y());
-		}
-		painter->setRenderHint(QPainter::Antialiasing, true);
-		painter->drawPolygon(pa);
-		painter->restore();
-
-		// Draw text
-		QRect textrect = QRect(r.left() + i * 2, r.top(), r.width() - ((5 * i) / 2), r.height());
-		QString text = option.fontMetrics.elidedText(model->data(index, Qt::DisplayRole).toString(), Qt::ElideMiddle, textrect.width(), Qt::TextShowMnemonic);
-		m_view->style()->drawItemText(painter, textrect, Qt::AlignCenter | Qt::TextShowMnemonic, option.palette, (option.state & QStyle::State_Enabled), text, QPalette::Text);
-	}
-	else
-		QItemDelegate::paint(painter, option, index);
+	buttonOption.state &= ~QStyle::State_HasFocus;
+	buttonOption.rect = option.rect;
+	buttonOption.palette = option.palette;
+	m_view->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, m_view);
+
+	// Draw arrow
+	static const int i = 9; // ### hardcoded in qcommonstyle.cpp
+	QRect r = option.rect;
+	QPalette::ColorGroup arrowColorGroup = (option.state & QStyle::State_Enabled) ? QPalette::Active : QPalette::Disabled;
+	QColor arrowColor = option.palette.color(arrowColorGroup, QPalette::ButtonText);
+	painter->save();
+	painter->setBrush(arrowColor);
+	painter->setPen(Qt::NoPen);
+	QRect rect = QRect(r.left() + 6, r.top() + 6, r.height() - 12, r.height() - 12);
+	QPolygon pa(3);
+	if (m_view->isExpanded(index))
+	{
+		pa.setPoint(0, rect.left(), rect.top());
+		pa.setPoint(1, rect.right(), rect.top());
+		pa.setPoint(2, rect.center().x(), rect.bottom());
+	}
+	else
+	{
+		pa.setPoint(0, rect.left(), rect.top());
+		pa.setPoint(1, rect.left(), rect.bottom());
+		pa.setPoint(2, rect.right(), rect.center().y());
+	}
+	painter->setRenderHint(QPainter::Antialiasing, true);
+	painter->drawPolygon(pa);
+	painter->restore();
+
+	// Draw text
+	QRect textrect = QRect(r.left() + i * 2, r.top(), r.width() - ((5 * i) / 2), r.height());
+	QString text = option.fontMetrics.elidedText(model->data(index, Qt::DisplayRole).toString(), Qt::ElideMiddle, textrect.width(), Qt::TextShowMnemonic);
+	m_view->style()->drawItemText(painter, textrect, Qt::AlignCenter | Qt::TextShowMnemonic, option.palette, (option.state & QStyle::State_Enabled), text, QPalette::Text);
 }
 
 QSize ScTreeWidgetDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const




More information about the scribus-commit mailing list