diff --git a/src/DetachableTabBar.cpp b/src/DetachableTabBar.cpp index 23885916..6e1dc8ce 100644 --- a/src/DetachableTabBar.cpp +++ b/src/DetachableTabBar.cpp @@ -1,144 +1,142 @@ /* Copyright 2018 by Tomaz Canabrava This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "DetachableTabBar.h" #include "KonsoleSettings.h" #include "ViewContainer.h" #include #include #include #include #include #include #include #include namespace Konsole { DetachableTabBar::DetachableTabBar(QWidget *parent) : QTabBar(parent), tabId(-1) { setAcceptDrops(true); setElideMode(Qt::TextElideMode::ElideMiddle); KAcceleratorManager::setNoAccel(this); } void DetachableTabBar::middleMouseButtonClickAt(const QPoint& pos) { tabId = tabAt(pos); if (tabId != -1) { emit closeTab(tabId); } } void DetachableTabBar::mousePressEvent(QMouseEvent *event) { QTabBar::mousePressEvent(event); m_dragStartPosition = event->pos(); } void DetachableTabBar::handleDrop(QDrag *drag) { /* Three possibilities: * The user could have dropped the tab in this window, just ignore. * The user could have dropped the tab in another window, move the tab there * The user could have dropped the tab "nowhere", create a new window. */ if (!drag->target()) { if (count() != 1) { emit detachTab(currentIndex()); } return; } /* dropped in the same window, just bail out. */ for(auto tabBar : drag->target()->findChildren()) { if (tabBar == this) { QTimer::singleShot(10, this, [this]{ QMouseEvent ev(QEvent::Type::MouseButtonPress, m_dragStartPosition, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QTabBar::mousePressEvent(&ev); }); return; } } - if (count() != 1) { - emit moveTabToWindow(currentIndex(), qobject_cast(drag->target())); - } + emit moveTabToWindow(currentIndex(), qobject_cast(drag->target())); } void DetachableTabBar::mouseMoveEvent(QMouseEvent *event) { QTabBar::mouseMoveEvent(event); if (!(event->buttons() & Qt::LeftButton)) return; if (event->pos().y() > -10 && event->pos().y() < height() + 10) return; QScopedPointer drag(new QDrag(this)); QMimeData *mimeData = new QMimeData; mimeData->setData(QStringLiteral("konsole/tab"), QByteArray::number(currentIndex())); drag->setMimeData(mimeData); drag->exec(Qt::MoveAction); handleDrop(drag.get()); } void DetachableTabBar::mouseReleaseEvent(QMouseEvent *event) { QTabBar::mouseReleaseEvent(event); switch(event->button()) { case Qt::MiddleButton : if (KonsoleSettings::closeTabOnMiddleMouseButton()) { middleMouseButtonClickAt(event->pos()); } tabId = tabAt(event->pos()); if (tabId == -1) { emit newTabRequest(); } break; default: break; } } void DetachableTabBar::dragEnterEvent(QDragEnterEvent* event) { const auto dragId = QStringLiteral("konsole/terminal_display"); if (event->mimeData()->hasFormat(dragId)) { auto other_pid = event->mimeData()->data(dragId).toInt(); // don't accept the drop if it's another instance of konsole if (qApp->applicationPid() != other_pid) { return; } event->accept(); } } void DetachableTabBar::dragMoveEvent(QDragMoveEvent* event) { int tabIdx = tabAt(event->pos()); if (tabIdx != -1) { setCurrentIndex(tabIdx); } } }