File indexing completed on 2024-04-28 04:57:34

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2008 Javier Goday <jgoday@gmail.com>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 */
0010 
0011 #ifndef RANGETREEWIDGET_H
0012 #define RANGETREEWIDGET_H
0013 
0014 #include <QMap>
0015 #include <QSortFilterProxyModel>
0016 #include <QStyledItemDelegate>
0017 #include <QTreeView>
0018 
0019 class QStandardItem;
0020 class QStandardItemModel;
0021 class QSortFilterProxyModel;
0022 class QVariant;
0023 class RangeDelegate;
0024 
0025 /**
0026  * We need to override the qsortfilterproxymodel behavior
0027  * to avoid include the range rows in the search filter
0028  * the range rows are always showed
0029  **/
0030 class RangeSortFilterProxyModel : public QSortFilterProxyModel
0031 {
0032 public:
0033     RangeSortFilterProxyModel(QObject *parent = nullptr);
0034     ~RangeSortFilterProxyModel() override;
0035 
0036 protected:
0037     bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
0038 };
0039 
0040 class RangeTreeWidget : public QTreeView
0041 {
0042     Q_OBJECT
0043 public:
0044     RangeTreeWidget(QWidget *parent = nullptr);
0045     ~RangeTreeWidget() override;
0046 
0047     /**
0048      * Creates a range with a title between two values
0049      */
0050     int addRange(const QVariant &min, const QVariant &max, const QString &title);
0051     void clear();
0052 
0053     void add(const QVariant &data, const QString &column);
0054     void add(const QVariant &data, const QVariantList &columns);
0055     void addLabel(const QString &title);
0056     void setLabels(const QStringList &labels);
0057 
0058     /**
0059      * Set a delegate in case you want to create the ranges dynamically
0060      * Ej, the host ranges delegate
0061      */
0062     void setRangeDelegate(RangeDelegate *delegate);
0063 
0064     QList<QVariantList> data();
0065     QStandardItem *currentItem(int column = -1);
0066     QStandardItem *item(const QModelIndex &index = QModelIndex(), int column = 0);
0067 
0068 public Q_SLOTS:
0069     void removeRow(int row, const QModelIndex &parent = QModelIndex());
0070     /**
0071      * Filters the data throws the qsortfilterproxymodel
0072      */
0073     void setFilterRegExp(const QString &text);
0074 
0075 private:
0076     /**
0077      * Returns the selected range for a data.
0078      * If a rangedelegate is set, then gets the range from the delegate
0079      */
0080     QStandardItem *getRange(const QVariant &data);
0081 
0082 private:
0083     class Range;
0084 
0085     QStandardItemModel *m_model;
0086     RangeSortFilterProxyModel *m_proxyModel;
0087     QMap<int, QStandardItem *> m_data;
0088     QList<RangeTreeWidget::Range> m_ranges;
0089 
0090     RangeDelegate *m_rangeDelegate;
0091 };
0092 
0093 /**
0094  * Creates ranges dynamically, based on the item data
0095  */
0096 class RangeDelegate : public QObject
0097 {
0098     Q_OBJECT
0099 public:
0100     RangeDelegate(QObject *parent = nullptr);
0101     ~RangeDelegate() override;
0102 
0103     /**
0104      * Returns the current range of the incoming data
0105      */
0106     virtual QVariant getRangeData(const QVariant &data) = 0;
0107 };
0108 
0109 /**
0110  * Creates a ragen based on the host of the transfer
0111  */
0112 class HostRangeDelegate : public RangeDelegate
0113 {
0114     Q_OBJECT
0115 public:
0116     HostRangeDelegate(QObject *parent = nullptr);
0117     ~HostRangeDelegate() override;
0118 
0119     QVariant getRangeData(const QVariant &data) override;
0120 };
0121 
0122 class RangeTreeWidgetItemDelegate : public QStyledItemDelegate
0123 {
0124 public:
0125     RangeTreeWidgetItemDelegate(QAbstractItemView *parent);
0126 
0127     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0128     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0129 };
0130 #endif