NavigationPanel.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #ifndef NAVIGATIONPANEL_H
  2. #define NAVIGATIONPANEL_H
  3. #include "NavigationWidget.h"
  4. #include <QBoxLayout>
  5. #include <QFrame>
  6. #include <QPropertyAnimation>
  7. class ScrollArea;
  8. class NavigationToolButton;
  9. class NavigationHistory;
  10. class NavigationWidget;
  11. enum NavigationDisplayMode
  12. {
  13. MINIMAL = 0,
  14. COMPACT = 1,
  15. EXPAND = 2,
  16. MENU = 3
  17. };
  18. enum NavigationItemPosition
  19. {
  20. TOP = 0,
  21. SCROLL = 1,
  22. BOTTOM = 2
  23. };
  24. class NavigationItemLayout : public QVBoxLayout
  25. {
  26. Q_OBJECT
  27. public:
  28. explicit NavigationItemLayout(QWidget *parent = nullptr) : QVBoxLayout(parent){};
  29. // QLayoutItem interface
  30. public:
  31. void setGeometry(const QRect &rect) override
  32. {
  33. QVBoxLayout::setGeometry(rect);
  34. for (int i = 0; i < this->count(); ++i) {
  35. QLayoutItem *item = this->itemAt(i);
  36. NavigationSeparator *ns = dynamic_cast<NavigationSeparator *>(item->widget());
  37. if (ns) {
  38. QRect geo = item->geometry();
  39. item->widget()->setGeometry(0, geo.y(), geo.width(), geo.height());
  40. }
  41. }
  42. }
  43. };
  44. class NavigationHistory : public QObject
  45. {
  46. Q_OBJECT
  47. public:
  48. explicit NavigationHistory(const QHash<QString, NavigationWidget *> &items, QObject *parent = nullptr);
  49. QString defaultRouteKey() const;
  50. void setDefaultRouteKey(const QString &key);
  51. void push(const QString &routeKey);
  52. void pop();
  53. void remove(const QString &routeKey, bool all = false);
  54. protected:
  55. void navigate();
  56. signals:
  57. void emptyChanged(bool);
  58. private:
  59. QString m_defaultRouteKey;
  60. QStringList m_history;
  61. QHash<QString, NavigationWidget *> m_items;
  62. friend class NavigationPanel;
  63. };
  64. class NavigationPanel : public QFrame
  65. {
  66. Q_OBJECT
  67. public:
  68. explicit NavigationPanel(bool minimalEnabled = false, QWidget *parent = nullptr);
  69. /**
  70. * @brief add navigation item
  71. * @param routeKey: the unique name of item
  72. * @param icon: the icon of navigation item
  73. * @param text: the text of navigation item
  74. * @param onClick: the slot connected to item clicked signal
  75. * @param selectable: whether the item is selectable
  76. * @param position: where the button is added
  77. */
  78. void addItem(const QString &routeKey, FluentIconBase *icon, const QString &text, const QObject *receiver,
  79. const char *onClick, bool selectable = true,
  80. NavigationItemPosition position = NavigationItemPosition::TOP);
  81. /**
  82. * @brief add custom widget
  83. * @param routeKey: the unique name of item
  84. * @param widget: the custom widget to be added
  85. * @param onClick: the slot connected to item clicked signal
  86. * @param position: where the button is added
  87. */
  88. void addWidget(const QString &routeKey, NavigationWidget *widget, const QObject *receiver, const char *onClick,
  89. NavigationItemPosition position = NavigationItemPosition::TOP);
  90. void removeWidget(const QString &routeKey);
  91. void setMenuButtonVisible(bool visible);
  92. void setReturnButtonVisible(bool visible);
  93. void setExpandWidth(int width);
  94. void expand();
  95. void collapse();
  96. void addSeparator(NavigationItemPosition position = NavigationItemPosition::TOP);
  97. void setCurrentItem(const QString &routeKey);
  98. int layoutMinHeight();
  99. void setDefaultRouteKey(const QString &routeKey);
  100. virtual bool eventFilter(QObject *watched, QEvent *event) override;
  101. virtual void resizeEvent(QResizeEvent *event) override;
  102. public:
  103. ScrollArea *scrollArea;
  104. QWidget *scrollWidget;
  105. NavigationToolButton *menuButton;
  106. NavigationToolButton *returnButton;
  107. NavigationItemLayout *vBoxLayout;
  108. NavigationItemLayout *topLayout;
  109. NavigationItemLayout *bottomLayout;
  110. NavigationItemLayout *scrollLayout;
  111. NavigationHistory *history;
  112. bool isMinimalEnabled;
  113. NavigationDisplayMode displayMode;
  114. public slots:
  115. void toggle();
  116. signals:
  117. void displayModeChanged(NavigationDisplayMode);
  118. private:
  119. void initWidget();
  120. void initLayout();
  121. void addWidgetToLayout(NavigationWidget *widget, NavigationItemPosition position);
  122. void setWidgetCompacted(bool compacted);
  123. private slots:
  124. void onWidgetClicked();
  125. void onExpandAniFinished();
  126. private:
  127. QWidget *m_parent;
  128. bool m_isMenuButtonVisible;
  129. bool m_isReturnButtonVisible;
  130. // QHash<QString, NavigationWidget *> m_items;
  131. QPropertyAnimation *m_expandAni;
  132. int m_expandWidth;
  133. };
  134. #endif // NAVIGATIONPANEL_H