Changeset View
Changeset View
Standalone View
Standalone View
src/DetachableTabBar.cpp
Show All 12 Lines | 1 | /* | |||
---|---|---|---|---|---|
13 | 13 | | |||
14 | You should have received a copy of the GNU General Public License | 14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software | 15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301 USA. | 17 | 02110-1301 USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "DetachableTabBar.h" | 20 | #include "DetachableTabBar.h" | ||
21 | #include "ViewContainer.h" | ||||
22 | | ||||
21 | #include <QMouseEvent> | 23 | #include <QMouseEvent> | ||
24 | #include <QApplication> | ||||
25 | | ||||
26 | namespace Konsole { | ||||
22 | 27 | | |||
23 | DetachableTabBar::DetachableTabBar(QWidget *parent) : | 28 | DetachableTabBar::DetachableTabBar(QWidget *parent) : | ||
24 | QTabBar(parent), | 29 | QTabBar(parent), | ||
25 | _draggingOutside(false) {} | 30 | dragType(DragType::NONE), | ||
31 | _originalCursor(cursor()) | ||||
32 | {} | ||||
33 | | ||||
34 | bool DetachableTabBar::droppedContainerIsNotThis(const QPoint& currentPos) const | ||||
35 | { | ||||
36 | for(const auto dropWidget : _containers) { | ||||
37 | if (dropWidget->rect().contains(dropWidget->mapFromGlobal(currentPos))) { | ||||
38 | if (dropWidget != parent()) { | ||||
39 | return true; | ||||
40 | } | ||||
41 | } | ||||
42 | } | ||||
43 | return false; | ||||
44 | } | ||||
45 | | ||||
46 | void DetachableTabBar::mousePressEvent(QMouseEvent *event) | ||||
47 | { | ||||
48 | _containers = window()->findChildren<Konsole::TabbedViewContainer*>(); | ||||
49 | } | ||||
26 | 50 | | |||
27 | void DetachableTabBar::mouseMoveEvent(QMouseEvent *event) | 51 | void DetachableTabBar::mouseMoveEvent(QMouseEvent *event) | ||
28 | { | 52 | { | ||
29 | QTabBar::mouseMoveEvent(event); | 53 | QTabBar::mouseMoveEvent(event); | ||
30 | if (!contentsRect().adjusted(-30,-30,30,30).contains(event->pos())) { | 54 | auto widgetAtPos = qApp->topLevelAt(event->globalPos()); | ||
31 | if (!_draggingOutside) { | 55 | if (widgetAtPos){ | ||
32 | _draggingOutside = true; | 56 | if (window() == widgetAtPos->window()) { | ||
33 | _originalCursor = cursor(); | 57 | if (droppedContainerIsNotThis(event->globalPos())) { | ||
34 | setCursor(QCursor(Qt::DragCopyCursor)); | 58 | if (dragType != DragType::WINDOW) { | ||
59 | dragType = DragType::WINDOW; | ||||
60 | setCursor(QCursor(Qt::DragMoveCursor)); | ||||
35 | } | 61 | } | ||
36 | } else if (_draggingOutside) { | 62 | } else { | ||
37 | _draggingOutside = false; | 63 | if (dragType != DragType::NONE) { | ||
64 | dragType = DragType::NONE; | ||||
38 | setCursor(_originalCursor); | 65 | setCursor(_originalCursor); | ||
39 | } | 66 | } | ||
40 | } | 67 | } | ||
68 | } else { | ||||
69 | if (dragType != DragType::WINDOW) { | ||||
70 | dragType = DragType::WINDOW; | ||||
71 | setCursor(QCursor(Qt::DragMoveCursor)); | ||||
72 | } | ||||
73 | } | ||||
74 | } else if (!contentsRect().adjusted(-30,-30,30,30).contains(event->pos())) { | ||||
75 | // Don't let it detach the last tab. | ||||
76 | if (count() == 1) | ||||
77 | return; | ||||
78 | if (dragType != DragType::OUTSIDE) { | ||||
79 | dragType = DragType::OUTSIDE; | ||||
80 | setCursor(QCursor(Qt::DragCopyCursor)); | ||||
81 | } | ||||
82 | } | ||||
83 | } | ||||
41 | 84 | | |||
42 | void DetachableTabBar::mouseReleaseEvent(QMouseEvent *event) | 85 | void DetachableTabBar::mouseReleaseEvent(QMouseEvent *event) | ||
43 | { | 86 | { | ||
44 | QTabBar::mouseReleaseEvent(event); | 87 | QTabBar::mouseReleaseEvent(event); | ||
45 | if (!contentsRect().adjusted(-30,-30,30,30).contains(event->pos())) { | | |||
46 | setCursor(_originalCursor); | 88 | setCursor(_originalCursor); | ||
89 | | ||||
90 | if (contentsRect().adjusted(-30,-30,30,30).contains(event->pos())) { | ||||
91 | return; | ||||
92 | } | ||||
93 | | ||||
94 | auto widgetAtPos = qApp->topLevelAt(event->globalPos()); | ||||
95 | if (!widgetAtPos) { | ||||
96 | if (count() != 1) | ||||
47 | emit detachTab(currentIndex()); | 97 | emit detachTab(currentIndex()); | ||
98 | } else if (window() != widgetAtPos->window()) { | ||||
99 | // splits have a tendency to break, forbid to detach if splitted and it's the last tab. | ||||
100 | if (_containers.size() != 1 && count() > 1) | ||||
101 | emit moveTabToWindow(currentIndex(), widgetAtPos); | ||||
102 | } else if (droppedContainerIsNotThis(event->globalPos())){ | ||||
103 | if (count() != 1) | ||||
104 | emit moveTabToWindow(currentIndex(), widgetAtPos); | ||||
48 | } | 105 | } | ||
49 | } | 106 | } | ||
107 | | ||||
108 | } |