vtoolbar.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "vtoolbar.h"
  2. #include <QDebug>
  3. #include <QMouseEvent>
  4. #include <QCoreApplication>
  5. #include <QToolButton>
  6. using namespace vnotex;
  7. VToolBar::VToolBar(QWidget *p_parent)
  8. : QToolBar(p_parent),
  9. m_window(p_parent)
  10. {
  11. setupUI();
  12. m_window->installEventFilter(this);
  13. }
  14. VToolBar::VToolBar(const QString &p_title, QWidget *p_parent)
  15. : QToolBar(p_title, p_parent),
  16. m_window(p_parent)
  17. {
  18. setupUI();
  19. m_window->installEventFilter(this);
  20. }
  21. void VToolBar::setupUI()
  22. {
  23. }
  24. void VToolBar::mousePressEvent(QMouseEvent *p_event)
  25. {
  26. QToolBar::mousePressEvent(p_event);
  27. m_lastPos = p_event->pos();
  28. }
  29. void VToolBar::mouseDoubleClickEvent(QMouseEvent *p_event)
  30. {
  31. QToolBar::mouseDoubleClickEvent(p_event);
  32. m_ignoreNextMove = true;
  33. maximizeRestoreWindow();
  34. }
  35. void VToolBar::maximizeRestoreWindow()
  36. {
  37. m_window->isMaximized() ? m_window->showNormal() : m_window->showMaximized();
  38. }
  39. void VToolBar::mouseMoveEvent(QMouseEvent *p_event)
  40. {
  41. auto delta = p_event->pos() - m_lastPos;
  42. if (!m_ignoreNextMove && !m_lastPos.isNull() && (qAbs(delta.x()) > 10 || qAbs(delta.y()) > 10)) {
  43. if (m_window->isMaximized()) {
  44. m_window->showNormal();
  45. } else {
  46. m_window->move(p_event->globalPos() - m_lastPos);
  47. }
  48. }
  49. QToolBar::mouseMoveEvent(p_event);
  50. }
  51. void VToolBar::mouseReleaseEvent(QMouseEvent *p_event)
  52. {
  53. QToolBar::mouseReleaseEvent(p_event);
  54. m_ignoreNextMove = false;
  55. m_lastPos = QPoint();
  56. }
  57. void VToolBar::addTitleBarIcons(const QIcon &p_minimizeIcon,
  58. const QIcon &p_maximizeIcon,
  59. const QIcon &p_restoreIcon,
  60. const QIcon &p_closeIcon)
  61. {
  62. addSeparator();
  63. addAction(p_minimizeIcon, tr("Minimize"),
  64. this, [this]() {
  65. m_window->showMinimized();
  66. });
  67. m_maximizeIcon = p_maximizeIcon;
  68. m_restoreIcon = p_restoreIcon;
  69. m_maximizeAct = addAction(p_maximizeIcon, tr("Maximize"),
  70. this, [this]() {
  71. maximizeRestoreWindow();
  72. });
  73. {
  74. auto closeAct = addAction(p_closeIcon, tr("Close"),
  75. this, [this]() {
  76. m_window->close();
  77. });
  78. auto btn = static_cast<QToolButton *>(widgetForAction(closeAct));
  79. btn->setProperty("DangerousButton", true);
  80. }
  81. updateMaximizeAct();
  82. }
  83. bool VToolBar::eventFilter(QObject *p_obj, QEvent *p_event)
  84. {
  85. if (p_obj == m_window) {
  86. if (p_event->type() == QEvent::WindowStateChange) {
  87. updateMaximizeAct();
  88. }
  89. }
  90. return QToolBar::eventFilter(p_obj, p_event);
  91. }
  92. void VToolBar::updateMaximizeAct()
  93. {
  94. if (m_window->isMaximized()) {
  95. m_maximizeAct->setIcon(m_restoreIcon);
  96. m_maximizeAct->setText(tr("Restore Down"));
  97. } else {
  98. m_maximizeAct->setIcon(m_maximizeIcon);
  99. m_maximizeAct->setText(tr("Maximize"));
  100. }
  101. }