Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Notation & Composition
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "docktabbar.h"
24 :
25 : #include "kddockwidgets/src/core/TabBar.h"
26 : #include "kddockwidgets/src/core/DockWidget.h"
27 :
28 : #include "log.h"
29 :
30 : using namespace muse::dock;
31 : using namespace KDDockWidgets;
32 :
33 0 : DockTabBar::DockTabBar(Core::TabBar* controller, QQuickItem* parent)
34 0 : : KDDockWidgets::QtQuick::TabBar(controller, parent)
35 : {
36 0 : }
37 :
38 0 : bool DockTabBar::event(QEvent* event)
39 : {
40 0 : switch (event->type()) {
41 0 : case QEvent::MouseButtonDblClick: {
42 0 : QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
43 0 : doubleClicked(mouseEvent->pos());
44 0 : return true;
45 : }
46 0 : case QEvent::MouseButtonPress:
47 : case QEvent::MouseButtonRelease: {
48 0 : QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
49 0 : onMousePressRelease(mouseEvent);
50 0 : return true;
51 : }
52 0 : default:
53 0 : break;
54 : }
55 :
56 0 : return KDDockWidgets::QtQuick::TabBar::event(event);
57 : }
58 :
59 0 : void DockTabBar::onMousePressRelease(const QMouseEvent* mouseEvent)
60 : {
61 0 : QQuickItem* tabBarItem = tabBarQmlItem();
62 0 : if (!mouseEvent || !tabBarItem) {
63 0 : return;
64 : }
65 :
66 0 : const QPoint localPos = mouseEvent->pos();
67 :
68 0 : int tabIndex = tabAt(localPos);
69 0 : if (tabIndex < 0) {
70 0 : return;
71 : }
72 :
73 0 : switch (mouseEvent->type()) {
74 0 : case QEvent::MouseButtonPress: {
75 0 : m_indexOfPressedTab = tabIndex;
76 0 : m_tabChangedOnClick = false;
77 0 : auto* tabBarCtrl = static_cast<Core::TabBar*>(controller());
78 0 : tabBarCtrl->onMousePress(KDDockWidgets::Point(localPos.x(), localPos.y()));
79 0 : break;
80 : }
81 0 : case QEvent::MouseButtonRelease: {
82 0 : const int currentTabIndex = tabBarItem->property("currentIndex").toInt();
83 0 : if (tabIndex != currentTabIndex && tabIndex == m_indexOfPressedTab) {
84 0 : tabBarItem->setProperty("currentIndex", tabIndex);
85 0 : m_tabChangedOnClick = true;
86 : }
87 0 : m_indexOfPressedTab = -1;
88 0 : break;
89 : }
90 0 : default: UNREACHABLE;
91 : }
92 : }
93 :
94 0 : void DockTabBar::doubleClicked(QPoint pos) const
95 : {
96 0 : auto* tabBarCtrl = static_cast<Core::TabBar*>(controller());
97 0 : if (Core::DockWidget* dw = tabBarCtrl->dockWidgetAt(KDDockWidgets::Point(pos.x(), pos.y()))) {
98 0 : dw->setFloating(!dw->isFloating());
99 : }
100 0 : }
101 :
102 0 : void DockTabBar::setDraggableMouseArea(QQuickItem* mouseArea)
103 : {
104 0 : if (m_draggableMouseArea == mouseArea) {
105 0 : return;
106 : }
107 :
108 0 : m_draggableMouseArea = mouseArea;
109 0 : redirectMouseEvents(mouseArea);
110 : }
|