dockwidgettab.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 or (at your option) any later version.
  19. ** The licenses are as published by the Free Software Foundation
  20. ** and appearing in the file LICENSE.LGPLv21 included in the packaging
  21. ** of this file. Please review the following information to ensure
  22. ** the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 3 or (at your option) any later version
  28. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  29. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  30. ** included in the packaging of this file. Please review the following
  31. ** information to ensure the GNU General Public License requirements will
  32. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  33. **
  34. ****************************************************************************/
  35. #include "dockwidgettab.h"
  36. #include "ads_globals.h"
  37. #include "dockareawidget.h"
  38. #include "dockmanager.h"
  39. #include "dockoverlay.h"
  40. #include "dockwidget.h"
  41. #include "elidinglabel.h"
  42. #include "floatingdockcontainer.h"
  43. #include "floatingdragpreview.h"
  44. #include "iconprovider.h"
  45. #include <QApplication>
  46. #include <QBoxLayout>
  47. #include <QLabel>
  48. #include <QLoggingCategory>
  49. #include <QMenu>
  50. #include <QMouseEvent>
  51. #include <QPushButton>
  52. #include <QSplitter>
  53. #include <QStyle>
  54. #include <QToolButton>
  55. #include <iostream>
  56. static Q_LOGGING_CATEGORY(adsLog, "qtc.qmldesigner.advanceddockingsystem", QtDebugMsg)
  57. namespace ADS
  58. {
  59. using TabLabelType = ElidingLabel;
  60. /**
  61. * Private data class of DockWidgetTab class (pimpl)
  62. */
  63. struct DockWidgetTabPrivate
  64. {
  65. DockWidgetTab *q;
  66. DockWidget *m_dockWidget;
  67. QLabel *m_iconLabel = nullptr;
  68. TabLabelType *m_titleLabel;
  69. QPoint m_globalDragStartMousePosition;
  70. QPoint m_dragStartMousePosition;
  71. bool m_isActiveTab = false;
  72. DockAreaWidget *m_dockArea = nullptr;
  73. eDragState m_dragState = DraggingInactive;
  74. AbstractFloatingWidget *m_floatingWidget = nullptr;
  75. QIcon m_icon;
  76. QAbstractButton *m_closeButton = nullptr;
  77. QSpacerItem *m_iconTextSpacer;
  78. QPoint m_tabDragStartPosition;
  79. /**
  80. * Private data constructor
  81. */
  82. DockWidgetTabPrivate(DockWidgetTab *parent);
  83. /**
  84. * Creates the complete layout including all controls
  85. */
  86. void createLayout();
  87. /**
  88. * Moves the tab depending on the position in the given mouse event
  89. */
  90. void moveTab(QMouseEvent *event);
  91. /**
  92. * Test function for current drag state
  93. */
  94. bool isDraggingState(eDragState dragState) const { return this->m_dragState == dragState; }
  95. /**
  96. * Starts floating of the dock widget that belongs to this title bar
  97. * Returns true, if floating has been started and false if floating
  98. * is not possible for any reason
  99. */
  100. bool startFloating(eDragState draggingState = DraggingFloatingWidget);
  101. /**
  102. * Returns true if the given config flag is set
  103. */
  104. bool testConfigFlag(DockManager::eConfigFlag flag) const
  105. {
  106. return DockManager::configFlags().testFlag(flag);
  107. }
  108. /**
  109. * Creates the close button as QPushButton or as QToolButton
  110. */
  111. QAbstractButton *createCloseButton() const
  112. {
  113. if (testConfigFlag(DockManager::TabCloseButtonIsToolButton)) {
  114. auto button = new QToolButton();
  115. button->setAutoRaise(true);
  116. return button;
  117. } else {
  118. return new QPushButton();
  119. }
  120. }
  121. template<typename T>
  122. AbstractFloatingWidget *createFloatingWidget(T *widget, bool opaqueUndocking)
  123. {
  124. if (opaqueUndocking) {
  125. return new FloatingDockContainer(widget);
  126. } else {
  127. auto w = new FloatingDragPreview(widget);
  128. QObject::connect(w, &FloatingDragPreview::draggingCanceled, q, [=]() {
  129. m_dragState = DraggingInactive;
  130. });
  131. return w;
  132. }
  133. }
  134. /**
  135. * Saves the drag start position in global and local coordinates
  136. */
  137. void saveDragStartMousePosition(const QPoint &globalPos)
  138. {
  139. m_globalDragStartMousePosition = globalPos;
  140. m_dragStartMousePosition = q->mapFromGlobal(globalPos);
  141. }
  142. };
  143. // struct DockWidgetTabPrivate
  144. DockWidgetTabPrivate::DockWidgetTabPrivate(DockWidgetTab *parent)
  145. : q(parent)
  146. {}
  147. void DockWidgetTabPrivate::createLayout()
  148. {
  149. m_titleLabel = new TabLabelType();
  150. m_titleLabel->setElideMode(Qt::ElideRight);
  151. m_titleLabel->setText(m_dockWidget->windowTitle());
  152. m_titleLabel->setObjectName("dockWidgetTabLabel");
  153. m_titleLabel->setAlignment(Qt::AlignCenter);
  154. QObject::connect(m_titleLabel, &ElidingLabel::elidedChanged, q, &DockWidgetTab::elidedChanged);
  155. m_closeButton = createCloseButton();
  156. m_closeButton->setObjectName("tabCloseButton");
  157. internal::setButtonIcon(m_closeButton, QStyle::SP_TitleBarCloseButton, TabCloseIcon);
  158. m_closeButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  159. q->onDockWidgetFeaturesChanged();
  160. internal::setToolTip(m_closeButton, QObject::tr("Close Tab"));
  161. QObject::connect(m_closeButton,
  162. &QAbstractButton::clicked,
  163. q,
  164. &DockWidgetTab::closeRequested);
  165. QFontMetrics fontMetrics(m_titleLabel->font());
  166. int spacing = qRound(fontMetrics.height() / 4.0);
  167. // Fill the layout
  168. QBoxLayout *boxLayout = new QBoxLayout(QBoxLayout::LeftToRight);
  169. boxLayout->setContentsMargins(2 * spacing, 0, 0, 0);
  170. boxLayout->setSpacing(0);
  171. q->setLayout(boxLayout);
  172. boxLayout->addWidget(m_titleLabel, 1);
  173. boxLayout->addSpacing(spacing);
  174. boxLayout->addWidget(m_closeButton);
  175. boxLayout->addSpacing(qRound(spacing * 4.0 / 3.0));
  176. boxLayout->setAlignment(Qt::AlignCenter);
  177. m_titleLabel->setVisible(true);
  178. }
  179. void DockWidgetTabPrivate::moveTab(QMouseEvent *event)
  180. {
  181. event->accept();
  182. QPoint distance = event->globalPos() - m_globalDragStartMousePosition;
  183. distance.setY(0);
  184. auto targetPos = distance + m_tabDragStartPosition;
  185. targetPos.rx() = qMax(targetPos.x(), 0);
  186. targetPos.rx() = qMin(q->parentWidget()->rect().right() - q->width() + 1, targetPos.rx());
  187. q->move(targetPos);
  188. q->raise();
  189. }
  190. bool DockWidgetTabPrivate::startFloating(eDragState draggingState)
  191. {
  192. auto dockContainer = m_dockWidget->dockContainer();
  193. qCInfo(adsLog) << "isFloating " << dockContainer->isFloating();
  194. qCInfo(adsLog) << "areaCount " << dockContainer->dockAreaCount();
  195. qCInfo(adsLog) << "widgetCount " << m_dockWidget->dockAreaWidget()->dockWidgetsCount();
  196. // if this is the last dock widget inside of this floating widget,
  197. // then it does not make any sense, to make it floating because
  198. // it is already floating
  199. if (dockContainer->isFloating() && (dockContainer->visibleDockAreaCount() == 1)
  200. && (m_dockWidget->dockAreaWidget()->dockWidgetsCount() == 1)) {
  201. return false;
  202. }
  203. qCInfo(adsLog) << "startFloating";
  204. m_dragState = draggingState;
  205. QSize size = m_dockArea->size();
  206. AbstractFloatingWidget *floatingWidget = nullptr;
  207. bool opaqueUndocking = DockManager::configFlags().testFlag(DockManager::OpaqueUndocking)
  208. || (DraggingFloatingWidget != draggingState);
  209. // If section widget has multiple tabs, we take only one tab
  210. // If it has only one single tab, we can move the complete
  211. // dock area into floating widget
  212. if (m_dockArea->dockWidgetsCount() > 1) {
  213. floatingWidget = createFloatingWidget(m_dockWidget, opaqueUndocking);
  214. } else {
  215. floatingWidget = createFloatingWidget(m_dockArea, opaqueUndocking);
  216. }
  217. if (DraggingFloatingWidget == draggingState) {
  218. floatingWidget->startFloating(m_dragStartMousePosition, size, DraggingFloatingWidget, q);
  219. auto Overlay = m_dockWidget->dockManager()->containerOverlay();
  220. Overlay->setAllowedAreas(OuterDockAreas);
  221. this->m_floatingWidget = floatingWidget;
  222. } else {
  223. floatingWidget->startFloating(m_dragStartMousePosition, size, DraggingInactive, nullptr);
  224. }
  225. return true;
  226. }
  227. DockWidgetTab::DockWidgetTab(DockWidget *dockWidget, QWidget *parent)
  228. : QFrame(parent)
  229. , d(new DockWidgetTabPrivate(this))
  230. {
  231. setAttribute(Qt::WA_NoMousePropagation, true);
  232. d->m_dockWidget = dockWidget;
  233. d->createLayout();
  234. }
  235. DockWidgetTab::~DockWidgetTab()
  236. {
  237. qCInfo(adsLog) << Q_FUNC_INFO;
  238. delete d;
  239. }
  240. void DockWidgetTab::mousePressEvent(QMouseEvent *event)
  241. {
  242. if (event->button() == Qt::LeftButton) {
  243. event->accept();
  244. d->saveDragStartMousePosition(event->globalPos());
  245. d->m_dragState = DraggingMousePressed;
  246. emit clicked();
  247. return;
  248. }
  249. Super::mousePressEvent(event);
  250. }
  251. void DockWidgetTab::mouseReleaseEvent(QMouseEvent *event)
  252. {
  253. if (event->button() == Qt::LeftButton) {
  254. auto currentDragState = d->m_dragState;
  255. d->m_globalDragStartMousePosition = QPoint();
  256. d->m_dragStartMousePosition = QPoint();
  257. d->m_dragState = DraggingInactive;
  258. switch (currentDragState) {
  259. case DraggingTab:
  260. // End of tab moving, emit signal
  261. if (d->m_dockArea) {
  262. emit moved(event->globalPos());
  263. }
  264. break;
  265. case DraggingFloatingWidget:
  266. d->m_floatingWidget->finishDragging();
  267. break;
  268. default:; // do nothing
  269. }
  270. }
  271. Super::mouseReleaseEvent(event);
  272. }
  273. void DockWidgetTab::mouseMoveEvent(QMouseEvent *event)
  274. {
  275. if (!(event->buttons() & Qt::LeftButton) || d->isDraggingState(DraggingInactive)) {
  276. d->m_dragState = DraggingInactive;
  277. Super::mouseMoveEvent(event);
  278. return;
  279. }
  280. // move floating window
  281. if (d->isDraggingState(DraggingFloatingWidget)) {
  282. d->m_floatingWidget->moveFloating();
  283. Super::mouseMoveEvent(event);
  284. return;
  285. }
  286. // move tab
  287. if (d->isDraggingState(DraggingTab)) {
  288. // Moving the tab is always allowed because it does not mean moving the
  289. // dock widget around
  290. d->moveTab(event);
  291. }
  292. auto mappedPos = mapToParent(event->pos());
  293. bool mouseOutsideBar = (mappedPos.x() < 0) || (mappedPos.x() > parentWidget()->rect().right());
  294. // Maybe a fixed drag distance is better here ?
  295. int dragDistanceY = qAbs(d->m_globalDragStartMousePosition.y() - event->globalPos().y());
  296. if (dragDistanceY >= DockManager::startDragDistance() || mouseOutsideBar) {
  297. // If this is the last dock area in a dock container with only
  298. // one single dock widget it does not make sense to move it to a new
  299. // floating widget and leave this one empty
  300. if (d->m_dockArea->dockContainer()->isFloating()
  301. && d->m_dockArea->openDockWidgetsCount() == 1
  302. && d->m_dockArea->dockContainer()->visibleDockAreaCount() == 1) {
  303. return;
  304. }
  305. // Floating is only allowed for widgets that are floatable
  306. // If we do non opaque undocking, then can create the drag preview
  307. // if the widget is movable.
  308. auto features = d->m_dockWidget->features();
  309. if (features.testFlag(DockWidget::DockWidgetFloatable)
  310. || (features.testFlag(DockWidget::DockWidgetMovable)
  311. && !DockManager::testConfigFlag(DockManager::OpaqueUndocking))) {
  312. // If we undock, we need to restore the initial position of this
  313. // tab because it looks strange if it remains on its dragged position
  314. if (d->isDraggingState(DraggingTab)
  315. && !DockManager::configFlags().testFlag(DockManager::OpaqueUndocking)) {
  316. parentWidget()->layout()->update();
  317. }
  318. d->startFloating();
  319. }
  320. return;
  321. } else if (d->m_dockArea->openDockWidgetsCount() > 1
  322. && (event->globalPos() - d->m_globalDragStartMousePosition).manhattanLength()
  323. >= QApplication::startDragDistance()) // Wait a few pixels before start moving
  324. {
  325. // If we start dragging the tab, we save its initial position to
  326. // restore it later
  327. if (DraggingTab != d->m_dragState) {
  328. d->m_tabDragStartPosition = this->pos();
  329. }
  330. d->m_dragState = DraggingTab;
  331. return;
  332. }
  333. Super::mouseMoveEvent(event);
  334. }
  335. void DockWidgetTab::contextMenuEvent(QContextMenuEvent *event)
  336. {
  337. event->accept();
  338. if (d->isDraggingState(DraggingFloatingWidget)) {
  339. return;
  340. }
  341. d->saveDragStartMousePosition(event->globalPos());
  342. QMenu menu(this);
  343. const bool isFloatable = d->m_dockWidget->features().testFlag(DockWidget::DockWidgetFloatable);
  344. const bool isNotOnlyTabInContainer = !d->m_dockArea->dockContainer()->hasTopLevelDockWidget();
  345. const bool isDetachable = isFloatable && isNotOnlyTabInContainer;
  346. auto action = menu.addAction(tr("Detach"), this, &DockWidgetTab::detachDockWidget);
  347. action->setEnabled(isDetachable);
  348. menu.addSeparator();
  349. action = menu.addAction(tr("Close"), this, &DockWidgetTab::closeRequested);
  350. action->setEnabled(isClosable());
  351. menu.addAction(tr("Close Others"), this, &DockWidgetTab::closeOtherTabsRequested);
  352. menu.exec(event->globalPos());
  353. }
  354. bool DockWidgetTab::isActiveTab() const { return d->m_isActiveTab; }
  355. void DockWidgetTab::setActiveTab(bool active)
  356. {
  357. bool dockWidgetClosable = d->m_dockWidget->features().testFlag(
  358. DockWidget::DockWidgetClosable);
  359. bool activeTabHasCloseButton = d->testConfigFlag(DockManager::ActiveTabHasCloseButton);
  360. bool allTabsHaveCloseButton = d->testConfigFlag(DockManager::AllTabsHaveCloseButton);
  361. bool tabHasCloseButton = (activeTabHasCloseButton && active) | allTabsHaveCloseButton;
  362. d->m_closeButton->setVisible(dockWidgetClosable && tabHasCloseButton);
  363. if (d->m_isActiveTab == active) {
  364. return;
  365. }
  366. d->m_isActiveTab = active;
  367. style()->unpolish(this);
  368. style()->polish(this);
  369. d->m_titleLabel->style()->unpolish(d->m_titleLabel);
  370. d->m_titleLabel->style()->polish(d->m_titleLabel);
  371. update();
  372. updateGeometry();
  373. emit activeTabChanged();
  374. }
  375. DockWidget *DockWidgetTab::dockWidget() const { return d->m_dockWidget; }
  376. void DockWidgetTab::setDockAreaWidget(DockAreaWidget *dockArea) { d->m_dockArea = dockArea; }
  377. DockAreaWidget *DockWidgetTab::dockAreaWidget() const { return d->m_dockArea; }
  378. void DockWidgetTab::setIcon(const QIcon &icon)
  379. {
  380. QBoxLayout *boxLayout = qobject_cast<QBoxLayout *>(layout());
  381. if (!d->m_iconLabel && icon.isNull()) {
  382. return;
  383. }
  384. if (!d->m_iconLabel) {
  385. d->m_iconLabel = new QLabel();
  386. d->m_iconLabel->setAlignment(Qt::AlignVCenter);
  387. d->m_iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
  388. internal::setToolTip(d->m_iconLabel, d->m_titleLabel->toolTip());
  389. boxLayout->insertWidget(0, d->m_iconLabel, Qt::AlignVCenter);
  390. boxLayout->insertSpacing(1, qRound(1.5 * boxLayout->contentsMargins().left() / 2.0));
  391. } else if (icon.isNull()) {
  392. // Remove icon label and spacer item
  393. boxLayout->removeWidget(d->m_iconLabel);
  394. boxLayout->removeItem(boxLayout->itemAt(0));
  395. delete d->m_iconLabel;
  396. d->m_iconLabel = nullptr;
  397. }
  398. d->m_icon = icon;
  399. if (d->m_iconLabel) {
  400. d->m_iconLabel->setPixmap(
  401. icon.pixmap(style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this)));
  402. d->m_iconLabel->setVisible(true);
  403. }
  404. }
  405. const QIcon &DockWidgetTab::icon() const { return d->m_icon; }
  406. QString DockWidgetTab::text() const { return d->m_titleLabel->text(); }
  407. void DockWidgetTab::mouseDoubleClickEvent(QMouseEvent *event)
  408. {
  409. // If this is the last dock area in a dock container it does not make
  410. // sense to move it to a new floating widget and leave this one empty
  411. if ((!d->m_dockArea->dockContainer()->isFloating() || d->m_dockArea->dockWidgetsCount() > 1)
  412. && d->m_dockWidget->features().testFlag(DockWidget::DockWidgetFloatable)) {
  413. d->saveDragStartMousePosition(event->globalPos());
  414. d->startFloating(DraggingInactive);
  415. }
  416. Super::mouseDoubleClickEvent(event);
  417. }
  418. void DockWidgetTab::setVisible(bool visible)
  419. {
  420. // Just here for debugging to insert debug output
  421. Super::setVisible(visible);
  422. }
  423. void DockWidgetTab::setText(const QString &title) { d->m_titleLabel->setText(title); }
  424. bool DockWidgetTab::isTitleElided() const { return d->m_titleLabel->isElided(); }
  425. bool DockWidgetTab::isClosable() const
  426. {
  427. return d->m_dockWidget
  428. && d->m_dockWidget->features().testFlag(DockWidget::DockWidgetClosable);
  429. }
  430. void DockWidgetTab::detachDockWidget()
  431. {
  432. if (!d->m_dockWidget->features().testFlag(DockWidget::DockWidgetFloatable)) {
  433. return;
  434. }
  435. d->saveDragStartMousePosition(QCursor::pos());
  436. d->startFloating(DraggingInactive);
  437. }
  438. bool DockWidgetTab::event(QEvent *event)
  439. {
  440. #ifndef QT_NO_TOOLTIP
  441. if (event->type() == QEvent::ToolTipChange) {
  442. const auto text = toolTip();
  443. d->m_titleLabel->setToolTip(text);
  444. }
  445. #endif
  446. return Super::event(event);
  447. }
  448. void DockWidgetTab::onDockWidgetFeaturesChanged()
  449. {
  450. auto features = d->m_dockWidget->features();
  451. auto sizePolicy = d->m_closeButton->sizePolicy();
  452. sizePolicy.setRetainSizeWhenHidden(
  453. features.testFlag(DockWidget::DockWidgetClosable)
  454. && d->testConfigFlag(DockManager::RetainTabSizeWhenCloseButtonHidden));
  455. d->m_closeButton->setSizePolicy(sizePolicy);
  456. }
  457. } // namespace ADS