floatingwidgettitlebar.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2020 Uwe Kindler
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and The Qt Company. For licensing terms
  13. ** and conditions see https://www.qt.io/terms-conditions. For further
  14. ** information use the contact form at https://www.qt.io/contact-us.
  15. **
  16. ** GNU General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU
  18. ** General Public License version 3 as published by the Free Software
  19. ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
  20. ** included in the packaging of this file. Please review the following
  21. ** information to ensure the GNU General Public License requirements will
  22. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  23. **
  24. ****************************************************************************/
  25. #include "floatingwidgettitlebar.h"
  26. #include "ads_globals.h"
  27. #include "elidinglabel.h"
  28. #include "floatingdockcontainer.h"
  29. #include <QHBoxLayout>
  30. #include <QMouseEvent>
  31. #include <QPixmap>
  32. #include <QPushButton>
  33. #include <QStyle>
  34. #include <QToolButton>
  35. #include <iostream>
  36. namespace ADS {
  37. using TabLabelType = ElidingLabel;
  38. using tCloseButton = QPushButton;
  39. /**
  40. * @brief Private data class of public interface CFloatingWidgetTitleBar
  41. */
  42. struct FloatingWidgetTitleBarPrivate
  43. {
  44. FloatingWidgetTitleBar *q; ///< public interface class
  45. QLabel *m_iconLabel = nullptr;
  46. TabLabelType *m_titleLabel;
  47. tCloseButton *m_closeButton = nullptr;
  48. FloatingDockContainer *m_floatingWidget = nullptr;
  49. eDragState m_dragState = DraggingInactive;
  50. FloatingWidgetTitleBarPrivate(FloatingWidgetTitleBar *parent)
  51. : q(parent)
  52. {}
  53. /**
  54. * Creates the complete layout including all controls
  55. */
  56. void createLayout();
  57. };
  58. void FloatingWidgetTitleBarPrivate::createLayout()
  59. {
  60. m_titleLabel = new TabLabelType();
  61. m_titleLabel->setElideMode(Qt::ElideRight);
  62. m_titleLabel->setText("DockWidget->windowTitle()");
  63. m_titleLabel->setObjectName("floatingTitleLabel");
  64. m_titleLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  65. m_closeButton = new tCloseButton();
  66. m_closeButton->setObjectName("floatingTitleCloseButton");
  67. m_closeButton->setFlat(true);
  68. // The standard icons do does not look good on high DPI screens
  69. QIcon closeIcon;
  70. QPixmap normalPixmap = q->style()->standardPixmap(QStyle::SP_TitleBarCloseButton,
  71. nullptr,
  72. m_closeButton);
  73. closeIcon.addPixmap(normalPixmap, QIcon::Normal);
  74. closeIcon.addPixmap(internal::createTransparentPixmap(normalPixmap, 0.25), QIcon::Disabled);
  75. m_closeButton->setIcon(q->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
  76. m_closeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  77. m_closeButton->setVisible(true);
  78. m_closeButton->setFocusPolicy(Qt::NoFocus);
  79. q->connect(m_closeButton, &QPushButton::clicked, q, &FloatingWidgetTitleBar::closeRequested);
  80. QFontMetrics fontMetrics(m_titleLabel->font());
  81. int spacing = qRound(fontMetrics.height() / 4.0);
  82. // Fill the layout
  83. QBoxLayout *layout = new QBoxLayout(QBoxLayout::LeftToRight);
  84. layout->setContentsMargins(6, 0, 0, 0);
  85. layout->setSpacing(0);
  86. q->setLayout(layout);
  87. layout->addWidget(m_titleLabel, 1);
  88. layout->addSpacing(spacing);
  89. layout->addWidget(m_closeButton);
  90. layout->setAlignment(Qt::AlignCenter);
  91. m_titleLabel->setVisible(true);
  92. }
  93. FloatingWidgetTitleBar::FloatingWidgetTitleBar(FloatingDockContainer *parent)
  94. : QWidget(parent)
  95. , d(new FloatingWidgetTitleBarPrivate(this))
  96. {
  97. d->m_floatingWidget = parent;
  98. d->createLayout();
  99. }
  100. FloatingWidgetTitleBar::~FloatingWidgetTitleBar()
  101. {
  102. delete d;
  103. }
  104. void FloatingWidgetTitleBar::mousePressEvent(QMouseEvent *event)
  105. {
  106. if (event->button() == Qt::LeftButton) {
  107. d->m_dragState = DraggingFloatingWidget;
  108. d->m_floatingWidget->startDragging(event->pos(), d->m_floatingWidget->size(), this);
  109. return;
  110. }
  111. Super::mousePressEvent(event);
  112. }
  113. void FloatingWidgetTitleBar::mouseReleaseEvent(QMouseEvent *event)
  114. {
  115. d->m_dragState = DraggingInactive;
  116. if (d->m_floatingWidget) {
  117. d->m_floatingWidget->finishDragging();
  118. }
  119. Super::mouseReleaseEvent(event);
  120. }
  121. void FloatingWidgetTitleBar::mouseMoveEvent(QMouseEvent *event)
  122. {
  123. if (!(event->buttons() & Qt::LeftButton) || DraggingInactive == d->m_dragState) {
  124. d->m_dragState = DraggingInactive;
  125. Super::mouseMoveEvent(event);
  126. return;
  127. }
  128. // move floating window
  129. if (DraggingFloatingWidget == d->m_dragState) {
  130. d->m_floatingWidget->moveFloating();
  131. Super::mouseMoveEvent(event);
  132. return;
  133. }
  134. Super::mouseMoveEvent(event);
  135. }
  136. void FloatingWidgetTitleBar::enableCloseButton(bool enable)
  137. {
  138. d->m_closeButton->setEnabled(enable);
  139. }
  140. void FloatingWidgetTitleBar::setTitle(const QString &text)
  141. {
  142. d->m_titleLabel->setText(text);
  143. }
  144. } // namespace ADS