File indexing completed on 2024-05-19 15:46:52

0001 /*
0002     SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "expandingtree.h"
0008 
0009 #include <QAbstractProxyModel>
0010 #include <QAbstractTextDocumentLayout>
0011 #include <QPainter>
0012 #include "expandingwidgetmodel.h"
0013 
0014 #include <util/path.h>
0015 #include <util/widgetcolorizer.h>
0016 
0017 using namespace KDevelop;
0018 
0019 ExpandingTree::ExpandingTree(QWidget* parent) : QTreeView(parent)
0020 {
0021     m_drawText.documentLayout()->setPaintDevice(this);
0022     setUniformRowHeights(false);
0023 }
0024 
0025 void ExpandingTree::setModel(QAbstractItemModel* model)
0026 {
0027     Q_ASSERT(!model || qobject_cast<const ExpandingWidgetModel*>(
0028                  qobject_cast<const QAbstractProxyModel*>(model)->sourceModel())
0029              );
0030     QTreeView::setModel(model);
0031 }
0032 
0033 void ExpandingTree::drawRow(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
0034 {
0035     QTreeView::drawRow(painter, option, index);
0036 
0037     const auto* eModel = qobject_cast<const ExpandingWidgetModel*>(qobject_cast<const QAbstractProxyModel*>(model())->sourceModel());
0038     Q_ASSERT(eModel);
0039     const QModelIndex sourceIndex = eModel->mapToSource(index);
0040     if (eModel->isPartiallyExpanded(sourceIndex) != ExpandingWidgetModel::ExpansionType::NotExpanded) {
0041         QRect rect = eModel->partialExpandRect(sourceIndex);
0042         if (rect.isValid()) {
0043             QStyleOption opt;
0044             QAbstractTextDocumentLayout::PaintContext ctx;
0045 
0046             opt.rect = rect;
0047             style()->drawPrimitive(QStyle::PE_FrameLineEdit, &opt, painter);
0048 
0049             ctx.clip = QRectF(0, 0, rect.width(), rect.height());
0050             painter->setViewTransformEnabled(true);
0051             painter->translate(rect.left(), rect.top());
0052 
0053             m_drawText.setHtml(eModel->partialExpandText(sourceIndex));
0054             WidgetColorizer::convertDocumentToDarkTheme(&m_drawText);
0055             m_drawText.setPageSize(QSizeF(rect.width(), rect.height()));
0056             m_drawText.documentLayout()->draw(painter, ctx);
0057 
0058             painter->translate(-rect.left(), -rect.top());
0059         }
0060     }
0061 }
0062 
0063 int ExpandingTree::sizeHintForColumn(int column) const
0064 {
0065     return columnWidth(column);
0066 }
0067 
0068 void ExpandingTree::drawBranches(QPainter* painter, const QRect& rect, const QModelIndex& index) const
0069 {
0070     const auto& path = index.data(ProjectPathRole).value<Path>();
0071     if (path.isValid()) {
0072         const auto color = WidgetColorizer::colorForId(qHash(path), palette(), true);
0073         WidgetColorizer::drawBranches(this, painter, rect, index, color);
0074     }
0075     QTreeView::drawBranches(painter, rect, index);
0076 }
0077 
0078 #include "moc_expandingtree.cpp"