File indexing completed on 2022-11-22 15:05:57

0001 /*
0002     SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef GENERICLAYOUT_H
0007 #define GENERICLAYOUT_H
0008 
0009 // local
0010 #include <coretypes.h>
0011 #include "abstractlayout.h"
0012 #include "../data/errordata.h"
0013 #include "../data/viewdata.h"
0014 #include "../data/viewstable.h"
0015 
0016 // Qt
0017 #include <QObject>
0018 #include <QQuickView>
0019 #include <QPointer>
0020 #include <QScreen>
0021 
0022 // Plasma
0023 #include <Plasma>
0024 
0025 namespace Plasma {
0026 class Applet;
0027 class Containment;
0028 class Types;
0029 }
0030 
0031 namespace Latte {
0032 class Corona;
0033 class ScreenPool;
0034 class View;
0035 }
0036 
0037 namespace Latte {
0038 namespace Layout {
0039 
0040 //! This is  views map in the following structure:
0041 //! SCREEN_NAME -> EDGE -> VIEWID
0042 typedef QHash<QString, QHash<Plasma::Types::Location, QList<uint>>> ViewsMap;
0043 
0044 class GenericLayout : public AbstractLayout
0045 {
0046     Q_OBJECT
0047     Q_PROPERTY(int viewsCount READ viewsCount NOTIFY viewsCountChanged)
0048 
0049 public:   
0050     GenericLayout(QObject *parent, QString layoutFile, QString assignedName = QString());
0051     ~GenericLayout() override;
0052 
0053     QString background() const override;
0054     QString textColor() const override;
0055 
0056     virtual const QStringList appliedActivities() = 0; // to move at an interface
0057 
0058     virtual bool initCorona();
0059     void importToCorona();
0060     bool initContainments();
0061     void setCorona(Latte::Corona *corona);
0062 
0063     bool isActive() const; //! is loaded and running
0064     virtual bool isCurrent();
0065     bool isWritable() const;
0066     bool hasCorona() const;
0067 
0068     virtual int viewsCount(int screen) const;
0069     virtual int viewsCount(QScreen *screen) const;
0070     virtual int viewsCount() const;
0071 
0072     Type type() const override;
0073 
0074     Latte::Corona *corona() const;
0075 
0076     QStringList unloadedContainmentsIds();
0077 
0078     virtual Types::ViewType latteViewType(uint containmentId) const;
0079     const QList<Plasma::Containment *> *containments() const;
0080 
0081     bool contains(Plasma::Containment *containment) const;
0082     bool containsView(const int &containmentId) const;
0083     int screenForContainment(Plasma::Containment *containment);
0084 
0085     Latte::View *highestPriorityView();
0086     Latte::View *viewForContainment(uint id) const;
0087     Latte::View *viewForContainment(Plasma::Containment *containment) const;
0088     Plasma::Containment *containmentForId(uint id) const;
0089     QList<Plasma::Containment *> subContainmentsOf(uint id) const;
0090 
0091     static bool viewAtLowerScreenPriority(Latte::View *test, Latte::View *base, QScreen *primaryScreen);
0092     static bool viewAtLowerEdgePriority(Latte::View *test, Latte::View *base);
0093     static QList<Latte::View *> sortedLatteViews(QList<Latte::View *> views, QScreen *primaryScreen);
0094 
0095     QList<Latte::View *> sortedLatteViews();
0096     virtual QList<Latte::View *> viewsWithPlasmaShortcuts();
0097     virtual QList<Latte::View *> latteViews();
0098     virtual QList<Latte::View *> onlyOriginalViews();
0099     ViewsMap validViewsMap();
0100     virtual void syncLatteViewsToScreens();
0101 
0102     void syncToLayoutFile(bool removeLayoutId = false);
0103 
0104     void lock(); //! make it only read-only
0105     void renameLayout(QString newName);
0106     virtual void unloadContainments();
0107     void unloadLatteViews();
0108     void unlock(); //! make it writable which it should be the default
0109 
0110     virtual void setLastConfigViewFor(Latte::View *view);
0111     virtual Latte::View *lastConfigViewFor();
0112 
0113     //! this function needs the layout to have first set the corona through setCorona() function
0114     virtual void addView(Plasma::Containment *containment);
0115     void recreateView(Plasma::Containment *containment, bool delayed = true);
0116     bool hasLatteView(Plasma::Containment *containment);
0117 
0118     bool newView(const QString &templateName);
0119     Data::View newView(const Latte::Data::View &nextViewData);
0120     void removeView(const Latte::Data::View &viewData);
0121     void updateView(const Latte::Data::View &viewData);    
0122     QString storedView(const int &containmentId); //returns temp filepath containing all view data
0123     void removeOrphanedSubContainment(const int &containmentId);
0124 
0125     //! Available edges for specific view in that screen
0126     virtual QList<Plasma::Types::Location> availableEdgesForView(QScreen *scr, Latte::View *forView) const;
0127     //! All free edges in that screen
0128     virtual QList<Plasma::Types::Location> freeEdges(QScreen *scr) const;
0129     virtual QList<Plasma::Types::Location> freeEdges(int screen) const;
0130 
0131     //! Bind this latteView and its relevant containments(including subcontainments)
0132     //! to this layout. It is used for moving a Latte::View from layout to layout)
0133     void assignToLayout(Latte::View *latteView, QList<Plasma::Containment *> containments);
0134     //! Unassign that latteView from this layout (this is used for moving a latteView
0135     //! from layout to layout) and returns all the containments relevant to
0136     //! that latteView
0137     QList<Plasma::Containment *> unassignFromLayout(Plasma::Containment *latteContainment);
0138 
0139     QList<int> viewsExplicitScreens();
0140 
0141     Latte::Data::ViewsTable viewsTable() const;
0142 
0143     //! errors/warnings
0144     Data::ErrorsList errors() const;
0145     Data::WarningsList warnings() const;
0146 
0147 public slots:
0148     Q_INVOKABLE int viewsWithTasks() const;
0149     virtual Q_INVOKABLE QList<int> qmlFreeEdges(int screen) const;  //change <Plasma::Types::Location> to <int> types
0150 
0151     void toggleHiddenState(QString viewName, QString screenName, Plasma::Types::Location edge);
0152 
0153 signals:
0154     void activitiesChanged(); // to move at an interface
0155     void viewsCountChanged();
0156     void viewEdgeChanged();
0157 
0158     //! used from ConfigView(s) in order to be informed which is one should be shown
0159     void lastConfigViewForChanged(Latte::View *view);
0160 
0161     //! used from LatteView(s) in order to exist only one each time that has the highest priority
0162     //! to use the global shortcuts activations
0163     void preferredViewForShortcutsChanged(Latte::View *view);
0164 
0165 protected:
0166     void updateLastUsedActivity();
0167 
0168 protected:
0169     Latte::Corona *m_corona{nullptr};
0170 
0171     QList<Plasma::Containment *> m_containments;
0172 
0173     QHash<const Plasma::Containment *, Latte::View *> m_latteViews;
0174     QHash<const Plasma::Containment *, Latte::View *> m_waitingLatteViews;
0175 
0176 private slots:
0177     void addContainment(Plasma::Containment *containment);
0178     void appletCreated(Plasma::Applet *applet);
0179     void destroyedChanged(bool destroyed);
0180     void containmentDestroyed(QObject *cont);
0181     void onLastConfigViewChangedFrom(Latte::View *view);
0182 
0183 private:
0184     //! It can be used in order for LatteViews to not be created automatically when
0185     //! their corresponding containments are created e.g. copyView functionality
0186     bool blockAutomaticLatteViewCreation() const;
0187     void setBlockAutomaticLatteViewCreation(bool block);
0188 
0189     bool explicitDockOccupyEdge(int screen, Plasma::Types::Location location) const;
0190     bool primaryDockOccupyEdge(Plasma::Types::Location location) const;
0191 
0192     bool viewDataAtLowerEdgePriority(const Latte::Data::View &test, const Latte::Data::View &base) const;
0193     bool viewDataAtLowerScreenPriority(const Latte::Data::View &test, const Latte::Data::View &base) const;
0194     bool viewDataAtLowerStatePriority(const Latte::Data::View &test, const Latte::Data::View &base) const;
0195 
0196     bool mapContainsId(const ViewsMap *map, uint viewId) const;
0197     QString mapScreenName(const ViewsMap *map, uint viewId) const;
0198 
0199     QList<int> subContainmentsOf(Plasma::Containment *containment) const;
0200 
0201     QList<Latte::Data::View> sortedViewsData(const QList<Latte::Data::View> &viewsData);
0202 
0203     void destroyContainment(Plasma::Containment *containment);
0204 
0205 private:
0206     bool m_blockAutomaticLatteViewCreation{false};
0207     bool m_hasInitializedContainments{false};
0208     QPointer<Latte::View> m_lastConfigViewFor;
0209 
0210     QStringList m_unloadedContainmentsIds;
0211 
0212     //! try to avoid crashes from recreating the same views all the time
0213     QList<const Plasma::Containment *> m_viewsToRecreate;
0214 
0215     //! Containments that are pending screen/state updates
0216     Latte::Data::ViewsTable m_pendingContainmentUpdates;
0217 
0218     friend class Latte::View;
0219 };
0220 
0221 }
0222 }
0223 
0224 #endif