TreeView.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef TREEVIEW_H
  2. #define TREEVIEW_H
  3. #include <QPainter>
  4. #include <QStyledItemDelegate>
  5. #include <QTreeWidget>
  6. #include "QFluentWidgets.h"
  7. class SmoothScroll;
  8. class TreeItemDelegate : public QStyledItemDelegate
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit TreeItemDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent){};
  13. // QAbstractItemDelegate interface
  14. public:
  15. virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
  16. {
  17. painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
  18. QStyledItemDelegate::paint(painter, option, index);
  19. if (!(option.state & (QStyle::State_Selected | QStyle::State_MouseOver))) {
  20. return;
  21. }
  22. painter->save();
  23. painter->setPen(Qt::NoPen);
  24. // draw background
  25. int h = option.rect.height() - 4;
  26. int c = QFWIns.isDarkTheme() ? 255 : 0;
  27. painter->setBrush(QColor(c, c, c, 9));
  28. QWidget *p = static_cast<QWidget *>(this->parent());
  29. painter->drawRoundedRect(4, option.rect.y() + 2, p->width() - 8, h, 4, 4);
  30. // draw indicator
  31. if (option.state & QStyle::State_Selected) {
  32. painter->setBrush(themeColor());
  33. painter->drawRoundedRect(4, 9 + option.rect.y(), 3, h - 13, 1.5, 1.5);
  34. }
  35. painter->restore();
  36. }
  37. };
  38. class TreeWidget : public QTreeWidget
  39. {
  40. Q_OBJECT
  41. public:
  42. explicit TreeWidget(QWidget *parent = nullptr);
  43. SmoothScroll *verticalSmoothScroll;
  44. SmoothScroll *horizonSmoothScroll;
  45. protected:
  46. virtual void drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const override;
  47. virtual void wheelEvent(QWheelEvent *event) override;
  48. };
  49. class TreeView : public QTreeView
  50. {
  51. Q_OBJECT
  52. public:
  53. explicit TreeView(QWidget *parent = nullptr);
  54. SmoothScroll *verticalSmoothScroll;
  55. SmoothScroll *horizonSmoothScroll;
  56. protected:
  57. virtual void drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const override;
  58. virtual void wheelEvent(QWheelEvent *event) override;
  59. };
  60. #endif // TREEVIEW_H