File indexing completed on 2024-12-22 05:13:40

0001 /*
0002     SPDX-FileCopyrightText: 2015 Ivan Cukic <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "window.h"
0008 
0009 #include "ui_window.h"
0010 
0011 #include <QItemDelegate>
0012 #include <QPainter>
0013 
0014 #include <KX11Extras>
0015 
0016 class Delegate : public QItemDelegate
0017 {
0018 public:
0019     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
0020     {
0021         painter->save();
0022 
0023         const QString title = index.data().toString();
0024 
0025         QRect titleRect = painter->fontMetrics().boundingRect(title);
0026         // unused int lineHeight = titleRect.height();
0027 
0028         // Header background
0029         auto rect = option.rect;
0030         rect.setHeight(64);
0031         titleRect.moveTop(option.rect.top());
0032         titleRect.setWidth(option.rect.width());
0033 
0034         if (index.data(KActivities::ActivitiesModel::ActivityIsCurrent).toBool()) {
0035             painter->fillRect(rect, QColor(64, 64, 64));
0036         } else {
0037             painter->fillRect(rect, QColor(32, 32, 32));
0038         }
0039 
0040         // Painting the title
0041         painter->setPen(QColor(255, 255, 255));
0042 
0043         titleRect.moveTop(titleRect.top() + 8);
0044         titleRect.setLeft(64 + 8);
0045         titleRect.setWidth(titleRect.width() - 64 - 8);
0046         painter->drawText(titleRect, title);
0047 
0048         titleRect.moveTop(titleRect.bottom() + 16);
0049 
0050         const QString description = index.data(KActivities::ActivitiesModel::ActivityDescription).toString();
0051 
0052         if (!description.isEmpty()) {
0053             painter->drawText(titleRect, index.data(KActivities::ActivitiesModel::ActivityDescription).toString());
0054         } else {
0055             painter->setPen(QColor(128, 128, 128));
0056             painter->drawText(titleRect, index.data(KActivities::ActivitiesModel::ActivityId).toString());
0057         }
0058 
0059         const QString iconName = index.data(KActivities::ActivitiesModel::ActivityIconSource).toString();
0060 
0061         if (!iconName.isEmpty()) {
0062             painter->drawPixmap(option.rect.x(), option.rect.y(), QIcon::fromTheme(iconName).pixmap(64, 64));
0063         }
0064 
0065         painter->restore();
0066     }
0067 
0068     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
0069     {
0070         Q_UNUSED(option);
0071         Q_UNUSED(index);
0072         return QSize(0, 70);
0073     }
0074 };
0075 
0076 Window::Window()
0077     : ui(new Ui::MainWindow())
0078     , activities(new KActivities::Consumer(this))
0079     , modelRunningActivities(new KActivities::ActivitiesModel({KActivities::Info::Running, KActivities::Info::Stopping}, this))
0080     , modelStoppedActivities(new KActivities::ActivitiesModel({KActivities::Info::Stopped, KActivities::Info::Starting}, this))
0081 {
0082     ui->setupUi(this);
0083 
0084     modelRunningActivities->setObjectName(QStringLiteral("RUNNING"));
0085     ui->listRunningActivities->setModel(modelRunningActivities);
0086     ui->listRunningActivities->setItemDelegate(new Delegate());
0087 
0088     modelStoppedActivities->setObjectName(QStringLiteral("STOPPED"));
0089     ui->listStoppedActivities->setModel(modelStoppedActivities);
0090     ui->listStoppedActivities->setItemDelegate(new Delegate());
0091 
0092     qDebug() << connect(activities, &KActivities::Consumer::runningActivitiesChanged, this, [](const QStringList &running) {
0093         qDebug() << running;
0094     });
0095 }
0096 
0097 void Window::showEvent(QShowEvent *event)
0098 {
0099     Q_UNUSED(event);
0100     KX11Extras::self()->setOnActivities(effectiveWinId(), QStringList());
0101     KX11Extras::self()->setOnAllDesktops(effectiveWinId(), true);
0102 }
0103 
0104 Window::~Window()
0105 {
0106     delete ui;
0107 }
0108 
0109 #include "moc_window.cpp"