diff --git a/src/SessionGroup.cpp b/src/SessionGroup.cpp index bd4899fd..1df1dd46 100644 --- a/src/SessionGroup.cpp +++ b/src/SessionGroup.cpp @@ -1,122 +1,122 @@ /* This file is part of Konsole, an X terminal. Copyright 2007-2008 by Robert Knight Copyright 1997,1998 by Lars Doelle Copyright 2009 by Thomas Dreibholz 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. */ // Konsole #include "Session.h" #include "SessionGroup.h" #include "Emulation.h" #include "TerminalDisplay.h" namespace Konsole { SessionGroup::SessionGroup(QObject* parent) : QObject(parent) { } SessionGroup::~SessionGroup() = default; QList SessionGroup::sessions() const { return _sessions.keys(); } void SessionGroup::addSession(Session* session, int group) { connect(session, &Konsole::Session::finished, this, &Konsole::SessionGroup::sessionFinished); _sessions.insert(session, {group, false}); } void SessionGroup::removeSession(Session* session) { disconnect(session, &Konsole::Session::finished, this, &Konsole::SessionGroup::sessionFinished); _sessions.remove(session); } void SessionGroup::clearSessions() { _sessions.clear(); } void SessionGroup::sessionFinished() { auto* session = qobject_cast(sender()); Q_ASSERT(session); removeSession(session); } void SessionGroup::setActiveSession(Session *session) { QList sessionsKeys = _sessions.keys(); for (Session *other : sessionsKeys) { if (_sessions[other].second) { _sessions[other].second = false; disconnect(other, &Konsole::Session::sendData, this, &Konsole::SessionGroup::forwardData); } if (other == session) { _sessions[other].second = true; connect(session, &Konsole::Session::sendData, this, &Konsole::SessionGroup::forwardData); return; } } } void SessionGroup::forwardData(const QByteArray& data) { const Session *session = qobject_cast(sender()); static bool _inForwardData = false; if (_inForwardData) { // Avoid recursive calls among session groups! // A recursive call happens when a master in group A calls forwardData() // in group B. If one of the destination sessions in group B is also a // master of a group including the master session of group A, this would // again call forwardData() in group A, and so on. return; } _inForwardData = true; const QList terminals = session->views(); for (const TerminalDisplay *terminalDisplay : terminals) { const int currentGroup = terminalDisplay->headerBar()->currentGroup(); const QList sessionsKeys = _sessions.keys(); - if (currentGroup == 99) { + if (currentGroup == 99) { // 99 -> Broadcast to all. // Broadcast to all sessions for (Session *other : sessionsKeys) { if (!_sessions[other].second && other != session) { other->emulation()->sendString(data); } } } else if (currentGroup > 0) { for (Session *other : sessionsKeys) { if (_sessions[other].first == currentGroup && !_sessions[other].second && other != session) { other->emulation()->sendString(data); } } } } _inForwardData = false; } } // namespace konsole diff --git a/src/TerminalHeaderBar.cpp b/src/TerminalHeaderBar.cpp index a3a86c0c..9261f5ca 100644 --- a/src/TerminalHeaderBar.cpp +++ b/src/TerminalHeaderBar.cpp @@ -1,311 +1,311 @@ /* * This file is part of Konsole, a terminal emulator for KDE. * * Copyright 2019 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 "TerminalHeaderBar.h" #include "TerminalDisplay.h" #include "SessionController.h" #include "ViewProperties.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Konsole { TerminalHeaderBar::TerminalHeaderBar(QWidget *parent) : QWidget(parent) { m_currentGroup = 0; m_boxLayout = new QBoxLayout(QBoxLayout::LeftToRight); m_boxLayout->setSpacing(0); m_boxLayout->setContentsMargins(0, 0, 0, 0); // Session icon m_terminalIcon = new QLabel(this); m_terminalIcon->setAlignment(Qt::AlignCenter); m_terminalIcon->setFixedSize(20, 20); m_boxLayout->addWidget(m_terminalIcon); // Status icons QLabel ** statusIcons[] = {&m_statusIconReadOnly, &m_statusIconCopyInput, &m_statusIconSilence, &m_statusIconActivity, &m_statusIconBell}; for (auto **statusIcon: statusIcons) { *statusIcon = new QLabel(this); (*statusIcon)->setAlignment(Qt::AlignCenter); (*statusIcon)->setFixedSize(20, 20); (*statusIcon)->setVisible(false); m_boxLayout->addWidget(*statusIcon); } m_statusIconReadOnly->setPixmap(QIcon::fromTheme(QStringLiteral("object-locked")).pixmap(QSize(16,16))); m_statusIconCopyInput->setPixmap(QIcon::fromTheme(QStringLiteral("irc-voice")).pixmap(QSize(16,16))); m_statusIconSilence->setPixmap(QIcon::fromTheme(QStringLiteral("system-suspend")).pixmap(QSize(16,16))); m_statusIconActivity->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-information")).pixmap(QSize(16,16))); m_statusIconBell->setPixmap(QIcon::fromTheme(QStringLiteral("notifications")).pixmap(QSize(16,16))); // Title m_terminalTitle = new QLabel(this); m_terminalTitle->setFont(QApplication::font()); m_boxLayout->addStretch(); m_boxLayout->addWidget(m_terminalTitle); m_boxLayout->addStretch(); // Broadcast group button m_broadcastGroup = new QToolButton(this); m_broadcastGroup->setIcon(QIcon::fromTheme(QStringLiteral("mail-attachment"))); m_broadcastGroup->setAutoRaise(true); m_broadcastGroup->setToolTip(i18nc("@info:tooltip", "Broadcast group")); m_broadcastGroup->setPopupMode(QToolButton::MenuButtonPopup); m_boxLayout->addWidget(m_broadcastGroup); // Expand button m_toggleExpandedMode = new QToolButton(this); m_toggleExpandedMode->setIcon(QIcon::fromTheme(QStringLiteral("view-fullscreen"))); // fake 'expand' icon. VDG input? m_toggleExpandedMode->setAutoRaise(true); m_toggleExpandedMode->setCheckable(true); m_toggleExpandedMode->setToolTip(i18nc("@info:tooltip", "Maximize terminal")); connect(m_toggleExpandedMode, &QToolButton::clicked, this, &TerminalHeaderBar::requestToggleExpansion); m_boxLayout->addWidget(m_toggleExpandedMode); // Close button m_closeBtn = new QToolButton(this); m_closeBtn->setIcon(QIcon::fromTheme(QStringLiteral("tab-close"))); m_closeBtn->setToolTip(i18nc("@info:tooltip", "Close terminal")); m_closeBtn->setText(i18nc("@info:tooltip", "Close terminal")); m_closeBtn->setObjectName(QStringLiteral("close-terminal-button")); m_closeBtn->setAutoRaise(true); m_boxLayout->addWidget(m_closeBtn); // The widget itself setLayout(m_boxLayout); setAutoFillBackground(true); setFocusIndicatorState(false); // Create menu and setup group actions m_groupMenu = new QMenu(this); QImage img(QSize(16, 16), QImage::Format_RGB32); QPainter painter(&img); // Setup actions for (int index = 0; index < MAX_GROUPS; index++) { painter.setBrush(QBrush(groupColor(index))); painter.drawRect(0, 0, 16, 16); QString actionString; if (index == 0) { actionString = i18n("No broadcast group"); } else if (index == MAX_GROUPS -1) { actionString = i18n("Broadcast to all"); } else { actionString = i18n("Group %1", index); } m_actionGroups[index] = new QAction(QPixmap::fromImage(img), actionString, this); connect(m_actionGroups[index], &QAction::triggered, this, [=](){ if (index == 0) { m_broadcastGroup->setIcon(QIcon::fromTheme(QStringLiteral("mail-attachment"))); } else { m_broadcastGroup->setIcon(QPixmap::fromImage(img)); } if (index == MAX_GROUPS -1) { - m_currentGroup = 99; + m_currentGroup = 99; // 99 -> Broadcast to all. } else { m_currentGroup = index; } emit groupChanged(); }); m_groupMenu->addAction(m_actionGroups[index]); } m_broadcastGroup->setMenu(m_groupMenu); } void TerminalHeaderBar::mouseDoubleClickEvent(QMouseEvent *ev) { if (ev->button() != Qt::LeftButton) { return; } m_toggleExpandedMode->click(); } // Hack untill I can detangle the creation of the TerminalViews void TerminalHeaderBar::finishHeaderSetup(ViewProperties *properties) { auto controller = dynamic_cast(properties); connect(properties, &Konsole::ViewProperties::titleChanged, this, [this, properties]{ m_terminalTitle->setText(properties->title()); }); m_terminalTitle->setText(properties->title()); connect(properties, &Konsole::ViewProperties::iconChanged, this, [this, properties] { m_terminalIcon->setPixmap(properties->icon().pixmap(QSize(22,22))); }); m_terminalIcon->setPixmap(properties->icon().pixmap(QSize(22,22))); connect(properties, &Konsole::ViewProperties::notificationChanged, this, &Konsole::TerminalHeaderBar::updateNotification); connect(properties, &Konsole::ViewProperties::readOnlyChanged, this, &Konsole::TerminalHeaderBar::updateSpecialState); connect(properties, &Konsole::ViewProperties::copyGroupChanged, this, &Konsole::TerminalHeaderBar::updateSpecialState); connect(m_closeBtn, &QToolButton::clicked, controller, &SessionController::closeSession); connect(this, &TerminalHeaderBar::groupChanged, controller, &SessionController::groupChanged); } void TerminalHeaderBar::setFocusIndicatorState(bool focused) { m_terminalIsFocused = focused; update(); } void TerminalHeaderBar::updateNotification(ViewProperties *item, Session::Notification notification, bool enabled) { Q_UNUSED(item) switch(notification) { case Session::Notification::Silence: m_statusIconSilence->setVisible(enabled); break; case Session::Notification::Activity: m_statusIconActivity->setVisible(enabled); break; case Session::Notification::Bell: m_statusIconBell->setVisible(enabled); break; default: break; } } void TerminalHeaderBar::updateSpecialState(ViewProperties *item) { auto controller = dynamic_cast(item); m_statusIconReadOnly->setVisible(controller->isReadOnly()); m_statusIconCopyInput->setVisible(controller->isCopyInputActive()); } void TerminalHeaderBar::paintEvent(QPaintEvent *paintEvent) { /* Try to get the widget that's 10px above this one. * If the widget is something else than a TerminalWidget, a TabBar or a QSplitter, * draw a 1px line to separate it from the others. */ const auto globalPos = parentWidget()->mapToGlobal(pos()); auto *widget = qApp->widgetAt(globalPos.x() + 10, globalPos.y() - 10); const bool isTabbar = qobject_cast(widget) != nullptr; const bool isTerminalWidget = qobject_cast(widget) != nullptr; const bool isSplitter = (qobject_cast(widget) != nullptr) || (qobject_cast(widget) != nullptr); if ((widget != nullptr) && !isTabbar && !isTerminalWidget && !isSplitter) { QStyleOptionTabBarBase optTabBase; QStylePainter p(this); optTabBase.init(this); optTabBase.shape = QTabBar::Shape::RoundedSouth; optTabBase.documentMode = false; p.drawPrimitive(QStyle::PE_FrameTabBarBase, optTabBase); } QWidget::paintEvent(paintEvent); if (!m_terminalIsFocused) { auto p = qApp->palette(); auto shadowColor = p.color(QPalette::ColorRole::Shadow); shadowColor.setAlphaF( qreal(0.2) * shadowColor.alphaF() ); // same as breeze. QPainter painter(this); painter.setPen(Qt::NoPen); painter.setBrush(shadowColor); painter.drawRect(rect()); } } void TerminalHeaderBar::mouseMoveEvent(QMouseEvent* ev) { if (m_toggleExpandedMode->isChecked()) { return; } auto point = ev->pos() - m_startDrag; if (point.manhattanLength() > 10) { auto drag = new QDrag(parent()); auto mimeData = new QMimeData(); QByteArray payload; payload.setNum(qApp->applicationPid()); mimeData->setData(QStringLiteral("konsole/terminal_display"), payload); drag->setMimeData(mimeData); drag->exec(); } } void TerminalHeaderBar::mousePressEvent(QMouseEvent* ev) { m_startDrag = ev->pos(); } void TerminalHeaderBar::mouseReleaseEvent(QMouseEvent* ev) { Q_UNUSED(ev) } int TerminalHeaderBar::currentGroup() { return m_currentGroup; } } QSize Konsole::TerminalHeaderBar::minimumSizeHint() const { auto height = sizeHint().height(); return {height, height}; }