File indexing completed on 2025-01-19 04:56:42

0001 /*
0002  * SPDX-FileCopyrightText: 2016 David Faure <faure@kde.org>
0003  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004  */
0005 
0006 #include "runningtaskwidget.h"
0007 #include "runningtaskmodelinterface.h"
0008 
0009 #include <QApplication>
0010 #include <QHBoxLayout>
0011 #include <QLabel>
0012 #include <QPushButton>
0013 #include <QScreen>
0014 
0015 #include <KLocalizedString>
0016 #include <KWindowSystem>
0017 #include <KX11Extras>
0018 
0019 using namespace Widgets;
0020 
0021 RunningTaskWidget::RunningTaskWidget(QWidget *parent)
0022     : QWidget(parent),
0023       m_model(nullptr),
0024       m_layout(new QHBoxLayout(this)),
0025       m_titleLabel(new QLabel(this)),
0026       m_stopButton(new QPushButton(this)),
0027       m_doneButton(new QPushButton(this)),
0028       m_collapsed(false)
0029 {
0030     // BypassWindowManagerHint allows to prevent the window from showing up in Alt-Tab
0031     // This means no way to focus it with the keyboard, though, obviously.
0032 
0033     setWindowFlags(Qt::Window | Qt::BypassWindowManagerHint | Qt::FramelessWindowHint);
0034     if (KWindowSystem::isPlatformX11()) {
0035         KX11Extras::setOnAllDesktops(winId(), true);
0036         KX11Extras::setState(winId(), NET::KeepAbove | NET::SkipTaskbar | NET::SkipPager);
0037     }
0038 
0039     setWindowTitle(i18nc("@title:window", "Zanshin Running Task Banner"));
0040 
0041     // Current idea for a good background color:
0042     // the selection color, i.e. usually blue. Arguable ;)
0043     QPalette pal;
0044     pal.setBrush(QPalette::Window, pal.brush(QPalette::Highlight));
0045     setPalette(pal);
0046     setAutoFillBackground(true);
0047 
0048     m_stopButton->setObjectName(QLatin1StringView("stopButton"));
0049     m_stopButton->setText(i18n("Stop"));
0050     connect(m_stopButton, &QAbstractButton::clicked, this, &RunningTaskWidget::onTaskRunStopped);
0051 
0052     m_doneButton->setObjectName(QLatin1StringView("doneButton"));
0053     m_doneButton->setText(i18n("Done"));
0054     connect(m_doneButton, &QAbstractButton::clicked, this, &RunningTaskWidget::onTaskRunDone);
0055 
0056     m_layout->setContentsMargins(0, 0, 0, 0);
0057     m_layout->addWidget(m_stopButton);
0058     m_layout->addWidget(m_titleLabel, 1, Qt::AlignCenter);
0059     m_layout->addWidget(m_doneButton);
0060 
0061     setCollapsed(true);
0062 }
0063 
0064 void RunningTaskWidget::setModel(Presentation::RunningTaskModelInterface *model)
0065 {
0066     if (m_model) {
0067         disconnect(m_model, nullptr, this, nullptr);
0068     }
0069 
0070     m_model = model;
0071 
0072     if (m_model) {
0073         connect(m_model, &Presentation::RunningTaskModelInterface::runningTaskChanged,
0074                 this, &RunningTaskWidget::onRunningTaskChanged);
0075     }
0076 }
0077 
0078 void RunningTaskWidget::setCollapsed(bool b)
0079 {
0080     if (m_collapsed == b) {
0081         return;
0082     }
0083     m_collapsed = b;
0084     m_stopButton->setVisible(!b);
0085     m_titleLabel->setVisible(!b);
0086     m_doneButton->setVisible(!b);
0087     m_layout->activate();
0088     resize();
0089 }
0090 
0091 void RunningTaskWidget::enterEvent(QEnterEvent *)
0092 {
0093     setCollapsed(false);
0094 }
0095 
0096 void RunningTaskWidget::leaveEvent(QEvent *)
0097 {
0098     setCollapsed(true);
0099 }
0100 
0101 void RunningTaskWidget::onRunningTaskChanged(const Domain::Task::Ptr &task)
0102 {
0103     if (task) {
0104         m_titleLabel->setText(task->title());
0105         resize();
0106         show();
0107     } else {
0108         hide();
0109     }
0110 }
0111 
0112 void RunningTaskWidget::onTaskRunStopped()
0113 {
0114     m_model->stopTask();
0115 }
0116 
0117 void RunningTaskWidget::onTaskRunDone()
0118 {
0119     m_model->doneTask();
0120 }
0121 
0122 void RunningTaskWidget::resize()
0123 {
0124     const auto screenGeometry = screen()->availableGeometry();
0125     const int screenWidth = screenGeometry.width();
0126     const int height = m_collapsed ? 5 : sizeHint().height();
0127     setGeometry(QRect(screenGeometry.left(), screenGeometry.top(), screenWidth, height));
0128 }
0129 
0130 Presentation::RunningTaskModelInterface *RunningTaskWidget::model() const
0131 {
0132     return m_model;
0133 }
0134 
0135 QString RunningTaskWidget::currentText() const
0136 {
0137     return m_titleLabel->text();
0138 }
0139 
0140 #include "moc_runningtaskwidget.cpp"