File indexing completed on 2024-12-22 05:15:15

0001 /*
0002     SPDX-FileCopyrightText: 2016 Chinmoy Ranjan Pradhan <chinmoyrp65@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <KWindowSystem>
0010 #include <QAbstractListModel>
0011 #include <QAction>
0012 #include <QPointer>
0013 #include <QRect>
0014 #include <QStringList>
0015 #include <tasksmodel.h>
0016 
0017 #include <Plasma/Containment>
0018 
0019 class QMenu;
0020 class QModelIndex;
0021 class QDBusServiceWatcher;
0022 class KDBusMenuImporter;
0023 
0024 class AppMenuModel : public QAbstractListModel
0025 {
0026     Q_OBJECT
0027 
0028     Q_PROPERTY(bool menuAvailable READ menuAvailable WRITE setMenuAvailable NOTIFY menuAvailableChanged)
0029     Q_PROPERTY(bool visible READ visible NOTIFY visibleChanged)
0030 
0031     Q_PROPERTY(Plasma::Types::ItemStatus containmentStatus MEMBER m_containmentStatus NOTIFY containmentStatusChanged)
0032     Q_PROPERTY(QRect screenGeometry READ screenGeometry WRITE setScreenGeometry NOTIFY screenGeometryChanged)
0033 
0034 public:
0035     explicit AppMenuModel(QObject *parent = nullptr);
0036     ~AppMenuModel() override;
0037 
0038     enum AppMenuRole {
0039         MenuRole = Qt::UserRole + 1, // TODO this should be Qt::DisplayRole
0040         ActionRole,
0041     };
0042 
0043     QVariant data(const QModelIndex &index, int role) const override;
0044     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0045     QHash<int, QByteArray> roleNames() const override;
0046 
0047     void updateApplicationMenu(const QString &serviceName, const QString &menuObjectPath);
0048 
0049     bool menuAvailable() const;
0050     void setMenuAvailable(bool set);
0051 
0052     bool visible() const;
0053 
0054     QRect screenGeometry() const;
0055     void setScreenGeometry(QRect geometry);
0056     QList<QAction *> flatActionList();
0057 
0058 Q_SIGNALS:
0059     void requestActivateIndex(int index);
0060     void bringToFocus(int index);
0061 
0062 private Q_SLOTS:
0063     void onActiveWindowChanged();
0064     void setVisible(bool visible);
0065     void update();
0066 
0067 Q_SIGNALS:
0068     void menuAvailableChanged();
0069     void modelNeedsUpdate();
0070     void containmentStatusChanged();
0071     void screenGeometryChanged();
0072     void visibleChanged();
0073 
0074 private:
0075     bool m_menuAvailable;
0076     bool m_updatePending = false;
0077     bool m_visible = true;
0078 
0079     Plasma::Types::ItemStatus m_containmentStatus = Plasma::Types::PassiveStatus;
0080     TaskManager::TasksModel *m_tasksModel;
0081 
0082     //! current active window used
0083     WId m_currentWindowId = 0;
0084     //! window that its menu initialization may be delayed
0085     WId m_delayedMenuWindowId = 0;
0086 
0087     std::unique_ptr<QMenu> m_searchMenu;
0088     QPointer<QMenu> m_menu;
0089     QPointer<QAction> m_searchAction;
0090     QList<QAction *> m_currentSearchActions;
0091 
0092     void removeSearchActionsFromMenu();
0093     void insertSearchActionsIntoMenu(const QString &filter = QString());
0094 
0095     QDBusServiceWatcher *m_serviceWatcher;
0096     QString m_serviceName;
0097     QString m_menuObjectPath;
0098 
0099     QPointer<KDBusMenuImporter> m_importer;
0100 };