NavigationInterface.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "NavigationInterface.h"
  2. #include <QResizeEvent>
  3. NavigationInterface::NavigationInterface(bool showMenuButton, bool showReturnButton, QWidget *parent) : QWidget(parent)
  4. {
  5. panel = new NavigationPanel(false, this);
  6. panel->setMenuButtonVisible(showMenuButton);
  7. panel->setReturnButtonVisible(showReturnButton);
  8. panel->installEventFilter(this);
  9. connect(panel, &NavigationPanel::displayModeChanged, this, &NavigationInterface::displayModeChanged);
  10. resize(48, this->height());
  11. setMinimumWidth(48);
  12. setAttribute(Qt::WA_StyledBackground);
  13. FluentStyleSheet::apply("NAVIGATION_INTERFACE", this);
  14. }
  15. void NavigationInterface::addItem(const QString &routeKey, FluentIconBase *icon, const QString &text,
  16. const QObject *receiver, const char *onClick, bool selectable,
  17. NavigationItemPosition position)
  18. {
  19. panel->addItem(routeKey, icon, text, receiver, onClick, selectable, position);
  20. setMinimumHeight(panel->layoutMinHeight());
  21. }
  22. void NavigationInterface::addWidget(const QString &routeKey, NavigationWidget *widget, const QObject *receiver,
  23. const char *onClick, NavigationItemPosition position)
  24. {
  25. panel->addWidget(routeKey, widget, receiver, onClick, position);
  26. setMinimumHeight(panel->layoutMinHeight());
  27. }
  28. void NavigationInterface::addSeparator(NavigationItemPosition position)
  29. {
  30. panel->addSeparator(position);
  31. setMinimumHeight(panel->layoutMinHeight());
  32. }
  33. void NavigationInterface::removeWidget(const QString &routeKey)
  34. {
  35. panel->removeWidget(routeKey);
  36. }
  37. void NavigationInterface::setCurrentItem(const QString &name)
  38. {
  39. panel->setCurrentItem(name);
  40. }
  41. /// set the routing key to use when the navigation history is empty
  42. void NavigationInterface::setDefaultRouteKey(const QString &routeKey)
  43. {
  44. panel->setDefaultRouteKey(routeKey);
  45. }
  46. /// set the maximum width
  47. void NavigationInterface::setExpandWidth(int width)
  48. {
  49. panel->setExpandWidth(width);
  50. }
  51. bool NavigationInterface::eventFilter(QObject *watched, QEvent *event)
  52. {
  53. if (watched != panel || event->type() != QEvent::Resize) {
  54. return QWidget::eventFilter(watched, event);
  55. }
  56. if (panel->displayMode != NavigationDisplayMode::MENU) {
  57. QResizeEvent *resizeEvt = dynamic_cast<QResizeEvent *>(event);
  58. if (resizeEvt->oldSize().width() != resizeEvt->size().width()) {
  59. setFixedWidth(resizeEvt->size().width());
  60. }
  61. }
  62. return QWidget::eventFilter(watched, event);
  63. }
  64. void NavigationInterface::resizeEvent(QResizeEvent *event)
  65. {
  66. if (event->oldSize().height() != this->height()) {
  67. panel->setFixedHeight(this->height());
  68. }
  69. }