diff --git a/src/DetachableTabBar.cpp b/src/DetachableTabBar.cpp index acc16e30..4ffd7819 100644 --- a/src/DetachableTabBar.cpp +++ b/src/DetachableTabBar.cpp @@ -1,225 +1,225 @@ /* 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 namespace Konsole { DetachableTabBar::DetachableTabBar(QWidget *parent) : QTabBar(parent), dragType(DragType::NONE), _originalCursor(cursor()), tabId(-1) { setAcceptDrops(true); setElideMode(Qt::TextElideMode::ElideMiddle); KAcceleratorManager::setNoAccel(this); connect(this, &QTabBar::tabMoved, this, &DetachableTabBar::moveColor); } void DetachableTabBar::setColor(int idx, const QColor &color) { std::map::const_iterator itColor = tabColors.find(idx); if (itColor != tabColors.end()) { tabColors[idx] = color; } else { tabColors.insert({idx, color}); } update(); } void DetachableTabBar::moveColor(int from, int to) { std::map::const_iterator itFromColor = tabColors.find(from); std::map::const_iterator itToColor = tabColors.find(to); if (itFromColor != tabColors.end() && itToColor != tabColors.end()) { // 'from' and 'to' exists color QColor toColor = tabColors[to]; // save temporary 'to' color tabColors[to] = tabColors[from]; // set new 'to' color tabColors[from] = toColor; // set new 'from' color } else { if (itFromColor != tabColors.end() && itToColor == tabColors.end()) { // 'from' exists color tabColors[to] = tabColors[from]; tabColors.erase(itFromColor); } if (itFromColor == tabColors.end() && itToColor != tabColors.end()) { // 'to' exists color tabColors[from] = tabColors[to]; tabColors.erase(itToColor); } } update(); } void DetachableTabBar::removeColor(int idx) { std::map::const_iterator itColor = tabColors.find(idx); if (itColor != tabColors.end()) { tabColors.erase(itColor); } using pair_type = decltype(tabColors)::value_type; auto pr = std::max_element( std::begin(tabColors), std::end(tabColors), [] (const pair_type &p1, const pair_type &p2) { return p1.first < p2.first; } ); if (pr->first >= idx) { for (int tabIndex = idx; tabIndex < count(); tabIndex++) { moveColor(tabIndex +1, tabIndex); } } update(); } void DetachableTabBar::middleMouseButtonClickAt(const QPoint& pos) { tabId = tabAt(pos); if (tabId != -1) { emit closeTab(tabId); } } void DetachableTabBar::mousePressEvent(QMouseEvent *event) { QTabBar::mousePressEvent(event); _containers = window()->findChildren(); } void DetachableTabBar::mouseMoveEvent(QMouseEvent *event) { QTabBar::mouseMoveEvent(event); auto widgetAtPos = qApp->topLevelAt(event->globalPos()); if (widgetAtPos != nullptr) { if (window() == widgetAtPos->window()) { if (dragType != DragType::NONE) { dragType = DragType::NONE; setCursor(_originalCursor); } } else { if (dragType != DragType::WINDOW) { dragType = DragType::WINDOW; setCursor(QCursor(Qt::DragMoveCursor)); } } } else if (!contentsRect().adjusted(-30,-30,30,30).contains(event->pos())) { // Don't let it detach the last tab. if (count() == 1) { return; } if (dragType != DragType::OUTSIDE) { dragType = DragType::OUTSIDE; setCursor(QCursor(Qt::DragCopyCursor)); } } } 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; case Qt::LeftButton: _containers = window()->findChildren(); break; default: break; } setCursor(_originalCursor); if (contentsRect().adjusted(-30,-30,30,30).contains(event->pos())) { return; } auto widgetAtPos = qApp->topLevelAt(event->globalPos()); if (widgetAtPos == nullptr) { if (count() != 1) { emit detachTab(currentIndex()); } } else if (window() != widgetAtPos->window()) { if (_containers.size() == 1 || count() > 1) { emit moveTabToWindow(currentIndex(), widgetAtPos); } } } 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); } } void DetachableTabBar::paintEvent(QPaintEvent *event) { QTabBar::paintEvent(event); QPainter painter(this); painter.setPen(Qt::NoPen); for (const auto &map_colors : tabColors) { const auto rect = tabRect(map_colors.first); QColor color = map_colors.second; - color.setAlpha(125); + color.setAlpha(map_colors.first == currentIndex() ? 180 : 125); if (!color.isValid()) { continue; } painter.setBrush(color); painter.drawRect(rect); } } }