File indexing completed on 2024-05-19 15:19:04

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 #ifndef EXPANDING_WIDGET_MODEL_H
0008 #define EXPANDING_WIDGET_MODEL_H
0009 
0010 #include <QAbstractTableModel>
0011 #include <QIcon>
0012 #include <QPointer>
0013 
0014 class QTreeView;
0015 
0016 /**
0017  * Cares about expanding/un-expanding items in a tree-view together with ExpandingDelegate
0018  */
0019 class ExpandingWidgetModel : public QAbstractItemModel
0020 {
0021     Q_OBJECT
0022 public:
0023     explicit ExpandingWidgetModel(QWidget *parent);
0024     ~ExpandingWidgetModel() override;
0025 
0026     enum ExpandingType { NotExpandable = 0, Expandable, Expanded };
0027 
0028     /// Unexpand all rows and clear all cached information about them(this includes deleting the expanding-widgets)
0029     void clearExpanding();
0030 
0031     ///@return whether the row given through index is expandable
0032     bool isExpandable(const QModelIndex &index) const;
0033 
0034     ///@return whether row is currently expanded
0035     bool isExpanded(const QModelIndex &row) const;
0036     /// Change the expand-state of the row given through index. The display will be updated.
0037     void setExpanded(QModelIndex index, bool expanded);
0038 
0039     ///@return the expanding-widget for the given row, if available. Expanding-widgets are in best case available for all expanded rows.
0040     /// This does not return the partially-expand widget.
0041     QWidget *expandingWidget(const QModelIndex &row) const;
0042 
0043     /// Places and shows the expanding-widget for the given row, if it should be visible and is valid.
0044     /// Also shows the partial-expanding-widget when it should be visible.
0045     void placeExpandingWidget(const QModelIndex &row);
0046 
0047     virtual QTreeView *treeView() const = 0;
0048 
0049     /// Should return true if the given row should be painted like a contained item(as opposed to label-rows etc.)
0050     virtual bool indexIsItem(const QModelIndex &index) const = 0;
0051 
0052     /// Does not request data from index, this only returns local data like highlighting for expanded rows and similar
0053     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0054 
0055     /// Returns the match-color for the given index, or zero if match-quality could not be computed.
0056     uint matchColor(const QModelIndex &index) const;
0057 
0058 public Q_SLOTS:
0059     /// Place or hides all expanding-widgets to the correct positions. Should be called after the view was scrolled.
0060     void placeExpandingWidgets();
0061 
0062 protected:
0063     /**
0064      * @return the context-match quality from 0 to 10 if it could be determined, else -1
0065      * */
0066     virtual int contextMatchQuality(const QModelIndex &index) const = 0;
0067 
0068     // Makes sure m_expandedIcon and m_collapsedIcon are loaded
0069     void cacheIcons() const;
0070 
0071     mutable QIcon m_expandedIcon;
0072     mutable QIcon m_collapsedIcon;
0073 
0074     // Finds out the basic height of the row represented by the given index. Basic means without respecting any expansion.
0075     int basicRowHeight(const QModelIndex &index) const;
0076 
0077 private:
0078     //     QMap<QModelIndex, ExpansionType> m_partiallyExpanded;
0079     // Store expanding-widgets and cache whether items can be expanded
0080     mutable QMap<QModelIndex, ExpandingType> m_expandState;
0081     QMap<QModelIndex, QPointer<QWidget>> m_expandingWidgets; // Map rows to their expanding-widgets
0082 };
0083 
0084 /**
0085  * Helper-function to merge custom-highlighting variant-lists.
0086  *
0087  * @param strings A list of strings that should be merged
0088  * @param highlights One variant-list for highlighting, as described in the kde header ktextedtor/codecompletionmodel.h
0089  * @param gapBetweenStrings How many signs are inserted between 2 strings?
0090  * */
0091 QList<QVariant> mergeCustomHighlighting(QStringList strings, QList<QVariantList> highlights, int gapBetweenStrings = 0);
0092 #endif