@@ -0,0 +1,29 | |||
|
1 | #ifndef SCIQLOP_DRAGDROPTABSWITCHER_H | |
|
2 | #define SCIQLOP_DRAGDROPTABSWITCHER_H | |
|
3 | ||
|
4 | #include <Common/spimpl.h> | |
|
5 | ||
|
6 | #include <QLoggingCategory> | |
|
7 | #include <QTabBar> | |
|
8 | ||
|
9 | Q_DECLARE_LOGGING_CATEGORY(LOG_DragDropTabSwitcher) | |
|
10 | ||
|
11 | class DragDropTabSwitcher : public QObject { | |
|
12 | Q_OBJECT | |
|
13 | ||
|
14 | public: | |
|
15 | DragDropTabSwitcher(QObject *parent = nullptr); | |
|
16 | ||
|
17 | void addTabBar(QTabBar *tabBar); | |
|
18 | void removeTabBar(QTabBar *tabBar); | |
|
19 | ||
|
20 | protected: | |
|
21 | bool eventFilter(QObject *obj, QEvent *event); | |
|
22 | ||
|
23 | private: | |
|
24 | class DragDropTabSwitcherPrivate; | |
|
25 | spimpl::unique_impl_ptr<DragDropTabSwitcherPrivate> impl; | |
|
26 | }; | |
|
27 | ||
|
28 | ||
|
29 | #endif // SCIQLOP_DRAGDROPTABSWITCHER_H |
@@ -0,0 +1,180 | |||
|
1 | #include "DragAndDrop/DragDropTabSwitcher.h" | |
|
2 | ||
|
3 | #include <QAbstractButton> | |
|
4 | #include <QDragEnterEvent> | |
|
5 | #include <QDragMoveEvent> | |
|
6 | #include <QTimer> | |
|
7 | ||
|
8 | #include "SqpApplication.h" | |
|
9 | ||
|
10 | Q_LOGGING_CATEGORY(LOG_DragDropTabSwitcher, "DragDropTabSwitcher") | |
|
11 | ||
|
12 | const int CHANGE_TAB_INTERVAL = 400; // time necessary over a tab to accept the switch | |
|
13 | const int SCROLL_BUTTON_AUTO_CLICK_INTERVAL | |
|
14 | = 500; // time between 2 auto clicks on a scroll button of the tab bar | |
|
15 | ||
|
16 | struct DragDropTabSwitcher::DragDropTabSwitcherPrivate { | |
|
17 | ||
|
18 | QList<QTabBar *> m_TabBarList; | |
|
19 | QTabBar *m_CurrentTabBar = nullptr; | |
|
20 | ||
|
21 | int m_HoveredTabIndex = -1; | |
|
22 | std::unique_ptr<QTimer> m_TabSwitchTimer = nullptr; | |
|
23 | ||
|
24 | QAbstractButton *m_HoveredScrollButton = nullptr; | |
|
25 | std::unique_ptr<QTimer> m_ScrollButtonsTimer = nullptr; | |
|
26 | ||
|
27 | explicit DragDropTabSwitcherPrivate() | |
|
28 | : m_TabSwitchTimer{std::make_unique<QTimer>()}, | |
|
29 | m_ScrollButtonsTimer{std::make_unique<QTimer>()} | |
|
30 | { | |
|
31 | m_TabSwitchTimer->setSingleShot(true); | |
|
32 | m_TabSwitchTimer->setInterval(CHANGE_TAB_INTERVAL); | |
|
33 | QObject::connect(m_TabSwitchTimer.get(), &QTimer::timeout, [this]() { | |
|
34 | if (m_CurrentTabBar) { | |
|
35 | m_CurrentTabBar->setCurrentIndex(m_HoveredTabIndex); | |
|
36 | } | |
|
37 | else { | |
|
38 | qCWarning(LOG_DragDropTabSwitcher()) << "DragDropTabSwitcherPrivate::timeout: " | |
|
39 | "Cannot select a new tab: unknown current " | |
|
40 | "tab bar."; | |
|
41 | } | |
|
42 | }); | |
|
43 | ||
|
44 | m_ScrollButtonsTimer->setInterval(SCROLL_BUTTON_AUTO_CLICK_INTERVAL); | |
|
45 | QObject::connect(m_ScrollButtonsTimer.get(), &QTimer::timeout, [this]() { | |
|
46 | if (m_HoveredScrollButton) { | |
|
47 | m_HoveredScrollButton->animateClick(); | |
|
48 | } | |
|
49 | else { | |
|
50 | qCWarning(LOG_DragDropTabSwitcher()) | |
|
51 | << "DragDropTabSwitcherPrivate::timeoutScroll: " | |
|
52 | "Unknown scroll button"; | |
|
53 | } | |
|
54 | }); | |
|
55 | } | |
|
56 | ||
|
57 | bool isScrollTabButton(QAbstractButton *button, QTabBar *tabBar) | |
|
58 | { | |
|
59 | auto isNextOrPreviousTabButton = true; | |
|
60 | ||
|
61 | if (tabBar->isAncestorOf(button)) { | |
|
62 | for (auto i = 0; i < tabBar->count(); ++i) { | |
|
63 | if (tabBar->tabButton(i, QTabBar::RightSide) == button | |
|
64 | || tabBar->tabButton(i, QTabBar::LeftSide) == button) { | |
|
65 | isNextOrPreviousTabButton = false; | |
|
66 | break; | |
|
67 | } | |
|
68 | } | |
|
69 | } | |
|
70 | else { | |
|
71 | isNextOrPreviousTabButton = false; | |
|
72 | } | |
|
73 | ||
|
74 | return isNextOrPreviousTabButton; | |
|
75 | } | |
|
76 | ||
|
77 | QAbstractButton *tabScrollButtonAt(const QPoint &pos, QTabBar *tabBar) | |
|
78 | { | |
|
79 | ||
|
80 | auto globalPos = tabBar->mapToGlobal(pos); | |
|
81 | ||
|
82 | auto widgetUnderMouse = sqpApp->widgetAt(globalPos); | |
|
83 | if (auto btn = qobject_cast<QAbstractButton *>(widgetUnderMouse)) { | |
|
84 | ||
|
85 | if (isScrollTabButton(btn, tabBar)) { | |
|
86 | return btn; | |
|
87 | } | |
|
88 | } | |
|
89 | ||
|
90 | return nullptr; | |
|
91 | } | |
|
92 | }; | |
|
93 | ||
|
94 | DragDropTabSwitcher::DragDropTabSwitcher(QObject *parent) | |
|
95 | : QObject(parent), impl{spimpl::make_unique_impl<DragDropTabSwitcherPrivate>()} | |
|
96 | { | |
|
97 | } | |
|
98 | ||
|
99 | void DragDropTabSwitcher::addTabBar(QTabBar *tabBar) | |
|
100 | { | |
|
101 | impl->m_TabBarList << tabBar; | |
|
102 | tabBar->setAcceptDrops(true); | |
|
103 | } | |
|
104 | ||
|
105 | void DragDropTabSwitcher::removeTabBar(QTabBar *tabBar) | |
|
106 | { | |
|
107 | impl->m_TabBarList.removeAll(tabBar); | |
|
108 | tabBar->setAcceptDrops(false); | |
|
109 | } | |
|
110 | ||
|
111 | bool DragDropTabSwitcher::eventFilter(QObject *obj, QEvent *event) | |
|
112 | { | |
|
113 | if (event->type() == QEvent::DragMove) { | |
|
114 | ||
|
115 | if (impl->m_CurrentTabBar) { | |
|
116 | ||
|
117 | QWidget *w = static_cast<QWidget *>(obj); | |
|
118 | if (!impl->m_CurrentTabBar->isAncestorOf(w)) { | |
|
119 | return false; | |
|
120 | } | |
|
121 | ||
|
122 | auto moveEvent = static_cast<QDragMoveEvent *>(event); | |
|
123 | ||
|
124 | auto scrollButton = impl->tabScrollButtonAt(moveEvent->pos(), impl->m_CurrentTabBar); | |
|
125 | ||
|
126 | if (!scrollButton) { | |
|
127 | ||
|
128 | auto tabIndex = impl->m_CurrentTabBar->tabAt(moveEvent->pos()); | |
|
129 | if (tabIndex >= 0 && tabIndex != impl->m_CurrentTabBar->currentIndex()) { | |
|
130 | // The mouse is over an unselected tab | |
|
131 | if (!impl->m_TabSwitchTimer->isActive() | |
|
132 | || tabIndex != impl->m_HoveredTabIndex) { | |
|
133 | impl->m_HoveredTabIndex = tabIndex; | |
|
134 | impl->m_TabSwitchTimer->start(); | |
|
135 | } | |
|
136 | else { | |
|
137 | // do nothing, timer already running | |
|
138 | } | |
|
139 | } | |
|
140 | else { | |
|
141 | impl->m_TabSwitchTimer->stop(); | |
|
142 | } | |
|
143 | ||
|
144 | impl->m_ScrollButtonsTimer->stop(); | |
|
145 | } | |
|
146 | else { | |
|
147 | // The mouse is over a scroll button | |
|
148 | // click it in a loop with a timer | |
|
149 | if (!impl->m_ScrollButtonsTimer->isActive() | |
|
150 | || impl->m_HoveredScrollButton != scrollButton) { | |
|
151 | impl->m_HoveredScrollButton = scrollButton; | |
|
152 | impl->m_ScrollButtonsTimer->start(); | |
|
153 | } | |
|
154 | } | |
|
155 | } | |
|
156 | } | |
|
157 | else if (event->type() == QEvent::DragEnter) { | |
|
158 | QWidget *w = static_cast<QWidget *>(obj); | |
|
159 | ||
|
160 | for (auto tabBar : impl->m_TabBarList) { | |
|
161 | if (w == tabBar) { | |
|
162 | auto enterEvent = static_cast<QDragEnterEvent *>(event); | |
|
163 | enterEvent->acceptProposedAction(); | |
|
164 | enterEvent->setDropAction(Qt::IgnoreAction); | |
|
165 | impl->m_CurrentTabBar = tabBar; | |
|
166 | break; | |
|
167 | } | |
|
168 | } | |
|
169 | } | |
|
170 | else if (event->type() == QEvent::DragLeave || event->type() == QEvent::Drop) { | |
|
171 | if (impl->m_CurrentTabBar) { | |
|
172 | impl->m_HoveredTabIndex = -1; | |
|
173 | impl->m_TabSwitchTimer->stop(); | |
|
174 | impl->m_CurrentTabBar = nullptr; | |
|
175 | impl->m_ScrollButtonsTimer->stop(); | |
|
176 | } | |
|
177 | } | |
|
178 | ||
|
179 | return false; | |
|
180 | } |
@@ -7,6 +7,7 | |||
|
7 | 7 | |
|
8 | 8 | class QVBoxLayout; |
|
9 | 9 | class QScrollArea; |
|
10 | class QTabBar; | |
|
10 | 11 | class VisualizationDragWidget; |
|
11 | 12 | class VisualizationDragDropContainer; |
|
12 | 13 | class QMimeData; |
@@ -54,6 +55,9 public: | |||
|
54 | 55 | void addDragDropScrollArea(QScrollArea *scrollArea); |
|
55 | 56 | void removeDragDropScrollArea(QScrollArea *scrollArea); |
|
56 | 57 | |
|
58 | void addDragDropTabBar(QTabBar *tabBar); | |
|
59 | void removeDragDropTabBar(QTabBar *tabBar); | |
|
60 | ||
|
57 | 61 | QUrl imageTemporaryUrl(const QImage &image) const; |
|
58 | 62 | |
|
59 | 63 | void setHightlightedDragWidget(VisualizationDragWidget *dragWidget); |
@@ -8,6 +8,7 gui_moc_headers = [ | |||
|
8 | 8 | 'include/SqpApplication.h', |
|
9 | 9 | 'include/DragAndDrop/DragDropHelper.h', |
|
10 | 10 | 'include/DragAndDrop/DragDropScroller.h', |
|
11 | 'include/DragAndDrop/DragDropTabSwitcher.h', | |
|
11 | 12 | 'include/TimeWidget/TimeWidget.h', |
|
12 | 13 | 'include/Variable/VariableInspectorWidget.h', |
|
13 | 14 | 'include/Variable/VariableInspectorTableView.h', |
@@ -46,6 +47,7 gui_sources = [ | |||
|
46 | 47 | 'src/SqpApplication.cpp', |
|
47 | 48 | 'src/DragAndDrop/DragDropHelper.cpp', |
|
48 | 49 | 'src/DragAndDrop/DragDropScroller.cpp', |
|
50 | 'src/DragAndDrop/DragDropTabSwitcher.cpp', | |
|
49 | 51 | 'src/Common/ColorUtils.cpp', |
|
50 | 52 | 'src/Common/VisualizationDef.cpp', |
|
51 | 53 | 'src/DataSource/DataSourceTreeWidgetItem.cpp', |
@@ -1,5 +1,6 | |||
|
1 | 1 | #include "DragAndDrop/DragDropHelper.h" |
|
2 | 2 | #include "DragAndDrop/DragDropScroller.h" |
|
3 | #include "DragAndDrop/DragDropTabSwitcher.h" | |
|
3 | 4 | #include "SqpApplication.h" |
|
4 | 5 | #include "Visualization/VisualizationDragDropContainer.h" |
|
5 | 6 | #include "Visualization/VisualizationDragWidget.h" |
@@ -18,7 +19,7 | |||
|
18 | 19 | #include <QVBoxLayout> |
|
19 | 20 | |
|
20 | 21 | |
|
21 |
Q_LOGGING_CATEGORY(LOG_DragDropHelper, "DragDrop |
|
|
22 | Q_LOGGING_CATEGORY(LOG_DragDropHelper, "DragDropHelper") | |
|
22 | 23 | |
|
23 | 24 | |
|
24 | 25 | struct DragDropHelper::DragDropHelperPrivate { |
@@ -28,6 +29,7 struct DragDropHelper::DragDropHelperPrivate { | |||
|
28 | 29 | QLabel *m_PlaceHolderLabel; |
|
29 | 30 | QWidget *m_PlaceBackground; |
|
30 | 31 | std::unique_ptr<DragDropScroller> m_DragDropScroller = nullptr; |
|
32 | std::unique_ptr<DragDropTabSwitcher> m_DragDropTabSwitcher = nullptr; | |
|
31 | 33 | QString m_ImageTempUrl; // Temporary file for image url generated by the drag & drop. Not using |
|
32 | 34 | // QTemporaryFile to have a name which is not generated. |
|
33 | 35 | |
@@ -38,7 +40,8 struct DragDropHelper::DragDropHelperPrivate { | |||
|
38 | 40 | |
|
39 | 41 | explicit DragDropHelperPrivate() |
|
40 | 42 | : m_PlaceHolder{std::make_unique<QWidget>()}, |
|
41 | m_DragDropScroller{std::make_unique<DragDropScroller>()} | |
|
43 | m_DragDropScroller{std::make_unique<DragDropScroller>()}, | |
|
44 | m_DragDropTabSwitcher{std::make_unique<DragDropTabSwitcher>()} | |
|
42 | 45 | { |
|
43 | 46 | |
|
44 | 47 | auto layout = new QVBoxLayout{m_PlaceHolder.get()}; |
@@ -54,6 +57,7 struct DragDropHelper::DragDropHelperPrivate { | |||
|
54 | 57 | layout->addWidget(m_PlaceBackground); |
|
55 | 58 | |
|
56 | 59 | sqpApp->installEventFilter(m_DragDropScroller.get()); |
|
60 | sqpApp->installEventFilter(m_DragDropTabSwitcher.get()); | |
|
57 | 61 | |
|
58 | 62 | m_ImageTempUrl = QDir::temp().absoluteFilePath("Sciqlop_graph.png"); |
|
59 | 63 | } |
@@ -165,6 +169,16 void DragDropHelper::removeDragDropScrollArea(QScrollArea *scrollArea) | |||
|
165 | 169 | impl->m_DragDropScroller->removeScrollArea(scrollArea); |
|
166 | 170 | } |
|
167 | 171 | |
|
172 | void DragDropHelper::addDragDropTabBar(QTabBar *tabBar) | |
|
173 | { | |
|
174 | impl->m_DragDropTabSwitcher->addTabBar(tabBar); | |
|
175 | } | |
|
176 | ||
|
177 | void DragDropHelper::removeDragDropTabBar(QTabBar *tabBar) | |
|
178 | { | |
|
179 | impl->m_DragDropTabSwitcher->removeTabBar(tabBar); | |
|
180 | } | |
|
181 | ||
|
168 | 182 | QUrl DragDropHelper::imageTemporaryUrl(const QImage &image) const |
|
169 | 183 | { |
|
170 | 184 | image.save(impl->m_ImageTempUrl); |
@@ -11,6 +11,9 | |||
|
11 | 11 | |
|
12 | 12 | #include "ui_VisualizationWidget.h" |
|
13 | 13 | |
|
14 | #include "DragAndDrop/DragDropHelper.h" | |
|
15 | #include "SqpApplication.h" | |
|
16 | ||
|
14 | 17 | #include <QToolButton> |
|
15 | 18 | |
|
16 | 19 | Q_LOGGING_CATEGORY(LOG_VisualizationWidget, "VisualizationWidget") |
@@ -67,12 +70,15 VisualizationWidget::VisualizationWidget(QWidget *parent) | |||
|
67 | 70 | connect(addTabViewButton, &QToolButton::clicked, addTabView); |
|
68 | 71 | connect(ui->tabWidget, &QTabWidget::tabCloseRequested, removeTabView); |
|
69 | 72 | |
|
73 | sqpApp->dragDropHelper().addDragDropTabBar(ui->tabWidget->tabBar()); | |
|
74 | ||
|
70 | 75 | // Adds default tab |
|
71 | 76 | addTabView(); |
|
72 | 77 | } |
|
73 | 78 | |
|
74 | 79 | VisualizationWidget::~VisualizationWidget() |
|
75 | 80 | { |
|
81 | sqpApp->dragDropHelper().removeDragDropTabBar(ui->tabWidget->tabBar()); | |
|
76 | 82 | delete ui; |
|
77 | 83 | } |
|
78 | 84 |
General Comments 0
You need to be logged in to leave comments.
Login now