File indexing completed on 2024-05-12 04:40:56
0001 /* 0002 SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org> 0003 SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef KDEVPLATFORM_PLUGIN_EXPANDINGDELEGATE_H 0009 #define KDEVPLATFORM_PLUGIN_EXPANDINGDELEGATE_H 0010 0011 #include <QItemDelegate> 0012 #include <QTextLayout> 0013 #include <QModelIndex> 0014 0015 class KateRenderer; 0016 class KateCompletionWidget; 0017 class KateDocument; 0018 class KateTextLine; 0019 class ExpandingWidgetModel; 0020 class QVariant; 0021 class QStyleOptionViewItem; 0022 0023 /** 0024 * This is a delegate that cares, together with ExpandingWidgetModel, about embedded widgets in tree-view. 0025 * */ 0026 0027 class ExpandingDelegate 0028 : public QItemDelegate 0029 { 0030 Q_OBJECT 0031 public: 0032 explicit ExpandingDelegate(ExpandingWidgetModel* model, QObject* parent = nullptr); 0033 0034 // Overridden to create highlighting for current index 0035 void paint (QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override; 0036 0037 // Returns the basic size-hint as reported by QItemDelegate 0038 QSize basicSizeHint(const QModelIndex& index) const; 0039 0040 ExpandingWidgetModel* model() const; 0041 protected: 0042 //Called right before paint to allow last-minute changes to the style 0043 virtual void adjustStyle(const QModelIndex& index, QStyleOptionViewItem& option) const; 0044 void drawDisplay (QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect, const QString& text) const override; 0045 QSize sizeHint (const QStyleOptionViewItem& option, const QModelIndex& index) const override; 0046 bool editorEvent (QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) override; 0047 virtual void drawBackground (QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; 0048 void drawDecoration(QPainter* painter, const QStyleOptionViewItem& option, const QRect& rect, const QPixmap& pixmap) const override; 0049 //option can be changed 0050 virtual QVector<QTextLayout::FormatRange> createHighlighting(const QModelIndex& index, QStyleOptionViewItem& option) const; 0051 0052 void adjustRect(QRect& rect) const; 0053 0054 /** 0055 * Creates a list of FormatRanges as should be returned by createHighlighting from a list of QVariants as described in the kde header ktexteditor/codecompletionmodel.h 0056 * */ 0057 QVector<QTextLayout::FormatRange> highlightingFromVariantList(const QList<QVariant>& customHighlights) const; 0058 0059 //Called when an item was expanded/unexpanded and the height changed 0060 virtual void heightChanged() const; 0061 0062 //Initializes the style options from the index 0063 void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const; 0064 0065 mutable int m_currentColumnStart; //Text-offset for custom highlighting, will be applied to m_cachedHighlights(Only highlights starting after this will be used). Should be zero of the highlighting is not taken from kate. 0066 mutable QList<int> m_currentColumnStarts; 0067 mutable QVector<QTextLayout::FormatRange> m_cachedHighlights; 0068 0069 mutable Qt::Alignment m_cachedAlignment; 0070 mutable QColor m_backgroundColor; 0071 mutable QModelIndex m_currentIndex; 0072 private: 0073 0074 ExpandingWidgetModel* m_model; 0075 }; 0076 0077 #endif // KDEVPLATFORM_PLUGIN_EXPANDINGDELEGATE_H