File indexing completed on 2024-05-05 17:44:51

0001 /*
0002     SPDX-FileCopyrightText: 2016 Eike Hein <hein@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractProxyModel>
0010 
0011 #include "abstracttasksmodeliface.h"
0012 #include "tasksmodel.h"
0013 
0014 #include "taskmanager_export.h"
0015 
0016 namespace TaskManager
0017 {
0018 /**
0019  * @short A proxy tasks model for grouping tasks, forming a tree.
0020  *
0021  * This proxy model groups tasks in its source tasks model, forming a tree
0022  * of tasks. Gouping behavior is influenced by various properties set on
0023  * the proxy model instance.
0024  *
0025  * @author Eike Hein <hein@kde.org>
0026  **/
0027 
0028 class TASKMANAGER_EXPORT TaskGroupingProxyModel : public QAbstractProxyModel, public AbstractTasksModelIface
0029 {
0030     Q_OBJECT
0031 
0032     Q_PROPERTY(TasksModel::GroupMode groupMode READ groupMode WRITE setGroupMode NOTIFY groupModeChanged)
0033     Q_PROPERTY(bool groupDemandingAttention READ groupDemandingAttention WRITE setGroupDemandingAttention NOTIFY groupDemandingAttentionChanged)
0034     Q_PROPERTY(int windowTasksThreshold READ windowTasksThreshold WRITE setWindowTasksThreshold NOTIFY windowTasksThresholdChanged)
0035     Q_PROPERTY(QStringList blacklistedAppIds READ blacklistedAppIds WRITE setBlacklistedAppIds NOTIFY blacklistedAppIdsChanged)
0036     Q_PROPERTY(QStringList blacklistedLauncherUrls READ blacklistedLauncherUrls WRITE setBlacklistedLauncherUrls NOTIFY blacklistedLauncherUrlsChanged)
0037 
0038 public:
0039     explicit TaskGroupingProxyModel(QObject *parent = nullptr);
0040     ~TaskGroupingProxyModel() override;
0041 
0042     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0043     QModelIndex parent(const QModelIndex &child) const override;
0044 
0045     QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override;
0046     QModelIndex mapToSource(const QModelIndex &proxyIndex) const override;
0047 
0048     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0049     bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
0050     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0051 
0052     QVariant data(const QModelIndex &proxyIndex, int role) const override;
0053 
0054     void setSourceModel(QAbstractItemModel *sourceModel) override;
0055 
0056     /**
0057      * Returns the current group mode, i.e. the criteria by which tasks should
0058      * be grouped.
0059      *
0060      * Defaults to TasksModel::GroupApplication, which groups tasks backed by
0061      * the same application.
0062      *
0063      * If the group mode is TasksModel::GroupDisabled, no grouping is done.
0064      *
0065      * @see TasksModel
0066      * @see setGroupMode
0067      * @returns the active group mode.
0068      **/
0069     TasksModel::GroupMode groupMode() const;
0070 
0071     /**
0072      * Sets the group mode, i.e. the criteria by which tasks should be grouped.
0073      *
0074      * The group mode can be set to TasksModel::GroupDisabled to disable grouping
0075      * entirely, breaking apart any existing groups.
0076      *
0077      * @see TasksModel
0078      * @see groupMode
0079      * @param mode A TasksModel group mode.
0080      **/
0081     void setGroupMode(TasksModel::GroupMode mode);
0082 
0083     /**
0084      * Whether new tasks which demand attention
0085      * (AbstractTasksModel::IsDemandingAttention) should be grouped immediately,
0086      * or only once they have stopped demanding attention. Defaults to @c false.
0087      *
0088      * @see setGroupDemandingAttention
0089      * @returns whether tasks which demand attention are grouped immediately.
0090      **/
0091     bool groupDemandingAttention() const;
0092 
0093     /**
0094      * Sets whether new tasks which demand attention
0095      * (AbstractTasksModel::IsDemandingAttention) should be grouped immediately,
0096      * or only once they have stopped demanding attention.
0097      *
0098      * @see groupDemandingAttention
0099      * @param group Whether tasks with demand attention should be grouped immediately.
0100      **/
0101     void setGroupDemandingAttention(bool group);
0102 
0103     /**
0104      * As window tasks (AbstractTasksModel::IsWindow) come and go in the source
0105      * model, groups will be formed when this threshold value is exceeded, and
0106      * broken apart when it matches or falls below.
0107      *
0108      * Defaults to @c -1, which means grouping is done regardless of the number
0109      * of window tasks in the source model.
0110      *
0111      * @see setWindowTasksThreshold
0112      * @return the threshold number of source window tasks used in grouping
0113      * decisions.
0114      **/
0115     int windowTasksThreshold() const;
0116 
0117     /**
0118      * Sets the number of source model window tasks (AbstractTasksModel::IsWindow)
0119      * above which groups will be formed, and at or below which groups will be broken
0120      * apart.
0121      *
0122      * If set to -1, grouping will be done regardless of the number of window tasks
0123      * in the source model.
0124      *
0125      * @see windowTasksThreshold
0126      * @param threshold A threshold number of source window tasks used in grouping
0127      * decisions.
0128      **/
0129     void setWindowTasksThreshold(int threshold);
0130 
0131     /**
0132      * A blacklist of app ids (AbstractTasksModel::AppId) that is consulted before
0133      * grouping a task. If a task's app id is found on the blacklist, it is not
0134      * grouped.
0135      *
0136      * The default app id blacklist is empty.
0137      *
0138      * @see setBlacklistedAppIds
0139      * @returns the blacklist of app ids consulted before grouping a task.
0140      **/
0141     QStringList blacklistedAppIds() const;
0142 
0143     /**
0144      * Sets the blacklist of app ids (AbstractTasksModel::AppId) that is consulted
0145      * before grouping a task. If a task's app id is found on the blacklist, it is
0146      * not grouped.
0147      *
0148      * When set, groups will be formed and broken apart as necessary.
0149      *
0150      * @see blacklistedAppIds
0151      * @param list a blacklist of app ids to be consulted before grouping a task.
0152      **/
0153     void setBlacklistedAppIds(const QStringList &list);
0154 
0155     /**
0156      * A blacklist of launcher URLs (AbstractTasksModel::LauncherUrl) that is
0157      * consulted before grouping a task. If a task's launcher URL is found on the
0158      * blacklist, it is not grouped.
0159      *
0160      * The default launcher URL blacklist is empty.
0161      *
0162      * @see setBlacklistedLauncherUrls
0163      * @returns the blacklist of launcher URLs consulted before grouping a task.
0164      **/
0165     QStringList blacklistedLauncherUrls() const;
0166 
0167     /**
0168      * Sets the blacklist of launcher URLs (AbstractTasksModel::LauncherUrl) that
0169      * is consulted before grouping a task. If a task's launcher URL is found on
0170      * the blacklist, it is not grouped.
0171      *
0172      * When set, groups will be formed and broken apart as necessary.
0173      *
0174      * @see blacklistedLauncherUrls
0175      * @param list a blacklist of launcher URLs to be consulted before grouping a task.
0176      **/
0177     void setBlacklistedLauncherUrls(const QStringList &list);
0178 
0179     /**
0180      * Request activation of the task at the given index. Derived classes are
0181      * free to interpret the meaning of "activate" themselves depending on
0182      * the nature and state of the task, e.g. launch or raise a window task.
0183      *
0184      * @param index An index in this tasks model.
0185      **/
0186     void requestActivate(const QModelIndex &index) override;
0187 
0188     /**
0189      * Request an additional instance of the application backing the task
0190      * at the given index.
0191      *
0192      * @param index An index in this tasks model.
0193      **/
0194     void requestNewInstance(const QModelIndex &index) override;
0195 
0196     /**
0197      * Requests to open the given URLs with the application backing the task
0198      * at the given index.
0199      *
0200      * @param index An index in this tasks model.
0201      * @param urls The URLs to be passed to the application.
0202      **/
0203     void requestOpenUrls(const QModelIndex &index, const QList<QUrl> &urls) override;
0204 
0205     /**
0206      * Request the task at the given index be closed.
0207      *
0208      * @param index An index in this tasks model.
0209      **/
0210     void requestClose(const QModelIndex &index) override;
0211 
0212     /**
0213      * Request starting an interactive move for the task at the given index.
0214      *
0215      * This is meant for tasks that have an associated window, and may be
0216      * a no-op when there is no window.
0217      *
0218      * This base implementation does nothing.
0219      *
0220      * @param index An index in this tasks model.
0221      **/
0222     void requestMove(const QModelIndex &index) override;
0223 
0224     /**
0225      * Request starting an interactive resize for the task at the given index.
0226      *
0227      * This is meant for tasks that have an associated window, and may be a
0228      * no-op when there is no window.
0229      *
0230      * @param index An index in this tasks model.
0231      **/
0232     void requestResize(const QModelIndex &index) override;
0233 
0234     /**
0235      * Request toggling the minimized state of the task at the given index.
0236      *
0237      * This is meant for tasks that have an associated window, and may be
0238      * a no-op when there is no window.
0239      *
0240      * This base implementation does nothing.
0241      *
0242      * @param index An index in this tasks model.
0243      **/
0244     void requestToggleMinimized(const QModelIndex &index) override;
0245 
0246     /**
0247      * Request toggling the maximized state of the task at the given index.
0248      *
0249      * This is meant for tasks that have an associated window, and may be
0250      * a no-op when there is no window.
0251      *
0252      * @param index An index in this tasks model.
0253      **/
0254     void requestToggleMaximized(const QModelIndex &index) override;
0255 
0256     /**
0257      * Request toggling the keep-above state of the task at the given index.
0258      *
0259      * This is meant for tasks that have an associated window, and may be
0260      * a no-op when there is no window.
0261      *
0262      * @param index An index in this tasks model.
0263      **/
0264     void requestToggleKeepAbove(const QModelIndex &index) override;
0265 
0266     /**
0267      * Request toggling the keep-below state of the task at the given index.
0268      *
0269      * This is meant for tasks that have an associated window, and may be
0270      * a no-op when there is no window.
0271      *
0272      * @param index An index in this tasks model.
0273      **/
0274     void requestToggleKeepBelow(const QModelIndex &index) override;
0275 
0276     /**
0277      * Request toggling the fullscreen state of the task at the given index.
0278      *
0279      * This is meant for tasks that have an associated window, and may be
0280      * a no-op when there is no window.
0281      *
0282      * This base implementation does nothing.
0283      *
0284      * @param index An index in this tasks model.
0285      **/
0286     void requestToggleFullScreen(const QModelIndex &index) override;
0287 
0288     /**
0289      * Request toggling the shaded state of the task at the given index.
0290      *
0291      * This is meant for tasks that have an associated window, and may be
0292      * a no-op when there is no window.
0293      *
0294      * @param index An index in this tasks model.
0295      **/
0296     void requestToggleShaded(const QModelIndex &index) override;
0297 
0298     /**
0299      * Request entering the window at the given index on the specified virtual desktops,
0300      * leaving any other desktops.
0301      *
0302      * On Wayland, virtual desktop ids are QStrings. On X11, they are uint >0.
0303      *
0304      * An empty list has a special meaning: The window is entered on all virtual desktops
0305      * in the session.
0306      *
0307      * On X11, a window can only be on one or all virtual desktops. Therefore, only the
0308      * first list entry is actually used.
0309      *
0310      * On X11, the id 0 has a special meaning: The window is entered on all virtual
0311      * desktops in the session.
0312      *
0313      * @param index An index in this window tasks model.
0314      * @param desktops A list of virtual desktop ids.
0315      **/
0316     void requestVirtualDesktops(const QModelIndex &index, const QVariantList &desktops) override;
0317 
0318     /**
0319      * Request entering the window at the given index on a new virtual desktop,
0320      * which is created in response to this request.
0321      *
0322      * @param index An index in this window tasks model.
0323      **/
0324     void requestNewVirtualDesktop(const QModelIndex &index) override;
0325 
0326     /**
0327      * Request moving the task at the given index to the specified activities.
0328      *
0329      * This is meant for tasks that have an associated window, and may be
0330      * a no-op when there is no window.
0331      *
0332      * This base implementation does nothing.
0333      *
0334      * @param index An index in this tasks model.
0335      * @param activities The new list of activities.
0336      **/
0337     void requestActivities(const QModelIndex &index, const QStringList &activities) override;
0338 
0339     /**
0340      * Request informing the window manager of new geometry for a visual
0341      * delegate for the task at the given index. The geometry should be in
0342      * screen coordinates.
0343      *
0344      * If the task at the given index is a group parent, the geometry is
0345      * set for all of its children. If the task at the given index is a
0346      * group member, the geometry is set for all of its siblings.
0347      *
0348      * @param index An index in this tasks model.
0349      * @param geometry Visual delegate geometry in screen coordinates.
0350      * @param delegate The delegate. Implementations are on their own with
0351      * regard to extracting information from this, and should take care to
0352      * reject invalid objects.
0353      **/
0354     void requestPublishDelegateGeometry(const QModelIndex &index, const QRect &geometry, QObject *delegate = nullptr) override;
0355 
0356     /**
0357      * Request toggling whether the task at the given index, along with any
0358      * tasks matching its kind, should be grouped or not. Task groups will be
0359      * formed or broken apart as needed, along with affecting future grouping
0360      * decisions as new tasks appear in the source model.
0361      *
0362      * As grouping is toggled for a task, updates are made to the blacklisted*
0363      * properties of the model instance.
0364      *
0365      * @see blacklistedAppIds
0366      * @see blacklistedLauncherUrls
0367      *
0368      * @param index An index in this tasks model.
0369      **/
0370     void requestToggleGrouping(const QModelIndex &index);
0371 
0372 Q_SIGNALS:
0373     void groupModeChanged() const;
0374     void groupDemandingAttentionChanged() const;
0375     void windowTasksThresholdChanged() const;
0376     void blacklistedAppIdsChanged() const;
0377     void blacklistedLauncherUrlsChanged() const;
0378 
0379 private:
0380     class Private;
0381     QScopedPointer<Private> d;
0382 
0383     Q_PRIVATE_SLOT(d, void sourceRowsAboutToBeInserted(const QModelIndex &parent, int first, int last))
0384     Q_PRIVATE_SLOT(d, void sourceRowsInserted(const QModelIndex &parent, int start, int end))
0385     Q_PRIVATE_SLOT(d, void sourceRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last))
0386     Q_PRIVATE_SLOT(d, void sourceRowsRemoved(const QModelIndex &parent, int start, int end))
0387     Q_PRIVATE_SLOT(d, void sourceModelAboutToBeReset())
0388     Q_PRIVATE_SLOT(d, void sourceModelReset())
0389     Q_PRIVATE_SLOT(d, void sourceDataChanged(QModelIndex, QModelIndex, QVector<int>))
0390 };
0391 
0392 }