TitleBar.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "TitleBar.h"
  2. #include "TitleBarButton.h"
  3. #include <QEvent>
  4. #include <QMouseEvent>
  5. #include <QDebug>
  6. #if defined(Q_OS_WIN32)
  7. #include "utils/Win32Utils.h"
  8. #elif defined(Q_OS_OSX)
  9. #include "utils/macUtils.h"
  10. #else
  11. #endif
  12. TitleBar::TitleBar(QWidget *parent) : QWidget(parent)
  13. {
  14. Q_INIT_RESOURCE(qframelesswindow);
  15. minBtn = new MinimizeButton(this);
  16. closeBtn = new CloseButton(this);
  17. maxBtn = new MaximizeButton(this);
  18. isDoubleClickEnabled = true;
  19. // QHBoxLayout *
  20. hBoxLayout = new QHBoxLayout;
  21. setLayout(hBoxLayout);
  22. hBoxLayout->setSpacing(0);
  23. hBoxLayout->setMargin(0);
  24. hBoxLayout->setAlignment(Qt::AlignCenter | Qt::AlignLeft);
  25. hBoxLayout->addStretch(1);
  26. hBoxLayout->addWidget(minBtn, 0, Qt::AlignRight);
  27. hBoxLayout->addWidget(maxBtn, 0, Qt::AlignRight);
  28. hBoxLayout->addWidget(closeBtn, 0, Qt::AlignRight);
  29. resize(200, 32);
  30. setFixedHeight(32);
  31. connect(minBtn, &MinimizeButton::clicked, this->window(), &QWidget::showMinimized);
  32. connect(maxBtn, &MaximizeButton::clicked, this, &TitleBar::onToggleMaxState);
  33. connect(closeBtn, &CloseButton::clicked, this->window(), &QWidget::close);
  34. this->window()->installEventFilter(this);
  35. }
  36. MinimizeButton *TitleBar::minimizeButton() const
  37. {
  38. return minBtn;
  39. }
  40. CloseButton *TitleBar::closeButton() const
  41. {
  42. return closeBtn;
  43. }
  44. MaximizeButton *TitleBar::maximizeButton() const
  45. {
  46. return maxBtn;
  47. }
  48. void TitleBar::onToggleMaxState()
  49. {
  50. if (this->window()->isMaximized()) {
  51. this->window()->showNormal();
  52. } else {
  53. this->window()->showMaximized();
  54. }
  55. }
  56. bool TitleBar::eventFilter(QObject *watched, QEvent *event)
  57. {
  58. if (watched == this->window()) {
  59. if (event->type() == QEvent::WindowStateChange) {
  60. maxBtn->setMaxState(this->window()->isMaximized());
  61. return false;
  62. }
  63. }
  64. return QWidget::eventFilter(watched, event);
  65. }
  66. void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
  67. {
  68. if ((event->button() != Qt::LeftButton) || !isDoubleClickEnabled) {
  69. return;
  70. }
  71. onToggleMaxState();
  72. }
  73. ///
  74. /// @attention 原本的逻辑和原文不一样,改成了一样的
  75. void TitleBar::mouseMoveEvent(QMouseEvent *event)
  76. {
  77. #if defined(Q_OS_WIN32)
  78. if (canDrag(event->pos())) {
  79. Win32Utils::WindowsMoveResize::startSystemMove(this->window(), event->globalPos());
  80. }
  81. #elif defined(Q_OS_OSX)
  82. #include "utils/macUtils.h"
  83. #else
  84. #endif
  85. }
  86. ///
  87. /// @attention 原本的逻辑和原文不一样,改成了一样的
  88. void TitleBar::mousePressEvent(QMouseEvent *event)
  89. {
  90. #if !defined(Q_OS_WIN32)
  91. if (canDrag(event->pos())) {
  92. Win32Utils::WindowsMoveResize::startSystemMove(this->window(), event->globalPos());
  93. }
  94. #else
  95. #endif
  96. }
  97. void TitleBar::setDoubleClickEnabled(bool isEnabled)
  98. {
  99. isDoubleClickEnabled = isEnabled;
  100. }
  101. bool TitleBar::canDrag(QPoint pos)
  102. {
  103. return isDragRegion(pos) && (!hasButtonPressed());
  104. }
  105. bool TitleBar::isDragRegion(QPoint pos)
  106. {
  107. int width = 0;
  108. for (auto button : this->findChildren<TitleBarButton *>()) {
  109. if (button->isVisible())
  110. width += button->width();
  111. }
  112. return (pos.x() > 0) && (pos.x() < this->width() - width);
  113. }
  114. bool TitleBar::hasButtonPressed()
  115. {
  116. for (auto button : this->findChildren<TitleBarButton *>()) {
  117. if (button->isPressed())
  118. return true;
  119. }
  120. return false;
  121. }
  122. StandardTitleBar::StandardTitleBar(QWidget *parent) : TitleBar(parent)
  123. {
  124. // add window icon
  125. iconLabel = new QLabel(this);
  126. iconLabel->setFixedSize(20, 20);
  127. hBoxLayout->insertSpacing(0, 10);
  128. hBoxLayout->insertWidget(1, iconLabel, 0, Qt::AlignLeft);
  129. // add title label
  130. titleLabel = new QLabel(this);
  131. hBoxLayout->insertWidget(2, titleLabel, 0, Qt::AlignLeft);
  132. titleLabel->setStyleSheet("QLabel{"
  133. "background: transparent;"
  134. "font: 13px 'Segoe UI';"
  135. "padding: 0 4px}");
  136. connect(this->window(), &QWidget::windowTitleChanged, this, &StandardTitleBar::setTitle);
  137. }
  138. /// set the title of title bar
  139. void StandardTitleBar::setTitle(const QString &title)
  140. {
  141. titleLabel->setText(title);
  142. titleLabel->adjustSize();
  143. }
  144. /// set the icon of title bar
  145. void StandardTitleBar::setIcon(const QIcon &icon)
  146. {
  147. iconLabel->setPixmap(icon.pixmap(20, 20));
  148. }