CycleListWidget.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef CYCLELISTWIDGET_H
  2. #define CYCLELISTWIDGET_H
  3. #include <QListWidget>
  4. #include <QToolButton>
  5. #include <Common/Icon.h>
  6. class SmoothScrollBar;
  7. class ScrollIcon : public FluentIconBase
  8. {
  9. Q_OBJECT
  10. public:
  11. enum IconType
  12. {
  13. UP = 0,
  14. DOWN
  15. };
  16. Q_ENUM(IconType)
  17. // const QString UP = "UP";
  18. // const QString DOWN = "DOWN";
  19. static QString iconName(IconType type);
  20. ScrollIcon(IconType type, Qfw::Theme t = Qfw::DARK);
  21. ~ScrollIcon();
  22. QString iconPath();
  23. QIcon icon() override;
  24. QString typeName() const override;
  25. QString enumName() const override;
  26. FluentIconBase *clone() override;
  27. Qfw::Theme theme() const;
  28. void setTheme(const Qfw::Theme &theme) override;
  29. private:
  30. Qfw::Theme m_theme;
  31. IconType m_type;
  32. };
  33. class ScrollButton : public QToolButton
  34. {
  35. Q_OBJECT
  36. public:
  37. explicit ScrollButton(FluentIconBase *icon, QWidget *parent = nullptr);
  38. virtual bool eventFilter(QObject *watched, QEvent *event) override;
  39. bool isPressed;
  40. protected:
  41. virtual void paintEvent(QPaintEvent *event) override;
  42. private:
  43. QScopedPointer<FluentIconBase> m_icon;
  44. };
  45. class CycleListWidget : public QListWidget
  46. {
  47. Q_OBJECT
  48. public:
  49. explicit CycleListWidget(const QVariantList &items, const QSize &itemSize,
  50. Qt::AlignmentFlag align = Qt::AlignCenter, QWidget *parent = nullptr);
  51. void setItems(const QVariantList &items);
  52. int currentIndex() const;
  53. void setCurrentIndex(int index);
  54. QListWidgetItem *currentItem() const;
  55. void scrollToItem(QListWidgetItem *item, QListWidget::ScrollHint hint = PositionAtCenter);
  56. void setSelectedItem(const QString &text);
  57. QSize itemSize() const;
  58. public:
  59. SmoothScrollBar *vScrollBar;
  60. protected:
  61. bool eventFilter(QObject *watched, QEvent *event) override;
  62. void wheelEvent(QWheelEvent *event) override;
  63. void leaveEvent(QEvent *event) override;
  64. void resizeEvent(QResizeEvent *event) override;
  65. void enterEvent(QEvent *event) override;
  66. signals:
  67. void currentItemChanged(QListWidgetItem *item);
  68. private:
  69. void createItems(const QVariantList &items);
  70. void addColumnItems(const QVariantList &items, bool disabled = false);
  71. private slots:
  72. void scrollUp();
  73. void scrollDown();
  74. void onItemClicked(QListWidgetItem *item);
  75. private:
  76. QSize m_itemSize;
  77. Qt::AlignmentFlag m_align;
  78. ScrollButton *m_upButton;
  79. ScrollButton *m_downButton;
  80. int m_scrollDuration;
  81. QVariantList m_originItems;
  82. int m_visibleNumber;
  83. bool m_isCycle;
  84. int m_currentIndex;
  85. };
  86. #endif // CYCLELISTWIDGET_H