File indexing completed on 2024-06-09 17:02:21

0001 /*
0002 *  Copyright 2019  Michail Vourlakos <mvourlakos@gmail.com>
0003 *
0004 *  This file is part of Latte-Dock
0005 *
0006 *  Latte-Dock is free software; you can redistribute it and/or
0007 *  modify it under the terms of the GNU General Public License as
0008 *  published by the Free Software Foundation; either version 2 of
0009 *  the License, or (at your option) any later version.
0010 *
0011 *  Latte-Dock is distributed in the hope that it will be useful,
0012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 *  GNU General Public License for more details.
0015 *
0016 *  You should have received a copy of the GNU General Public License
0017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #ifndef GENERICLAYOUT_H
0021 #define GENERICLAYOUT_H
0022 
0023 // local
0024 #include "abstractlayout.h"
0025 #include "../../liblatte2/types.h"
0026 
0027 // Qt
0028 #include <QObject>
0029 #include <QQuickView>
0030 #include <QPointer>
0031 #include <QScreen>
0032 
0033 // Plasma
0034 #include <Plasma>
0035 
0036 namespace Plasma {
0037 class Applet;
0038 class Containment;
0039 class Types;
0040 }
0041 
0042 namespace Latte {
0043 class Corona;
0044 class ScreenPool;
0045 class View;
0046 }
0047 
0048 namespace Latte {
0049 namespace Layout {
0050 class Storage;
0051 }
0052 }
0053 
0054 namespace Latte {
0055 namespace Layout {
0056 
0057 struct ViewData
0058 {
0059     int id; //view id
0060     bool active; //is active
0061     bool onPrimary; //on primary
0062     int screenId; //explicit screen id
0063     int location; //edge location
0064     QList<int> systrays;
0065 };
0066 
0067 //! This is  views map in the following structure:
0068 //! SCREEN_NAME -> EDGE -> VIEWID
0069 typedef QHash<QString, QHash<Plasma::Types::Location, uint>> ViewsMap;
0070 
0071 class GenericLayout : public AbstractLayout
0072 {
0073     Q_OBJECT
0074     Q_PROPERTY(int viewsCount READ viewsCount NOTIFY viewsCountChanged)
0075 
0076 public:   
0077     GenericLayout(QObject *parent, QString layoutFile, QString assignedName = QString());
0078     ~GenericLayout() override;
0079 
0080     virtual const QStringList appliedActivities() = 0; // to move at an interface
0081 
0082     void importToCorona();
0083     bool initToCorona(Latte::Corona *corona);
0084 
0085     bool isActive() const; //! is loaded and running
0086     virtual bool isCurrent() const;
0087     bool isWritable() const;
0088     bool layoutIsBroken() const;
0089 
0090     bool isInternalContainment(Plasma::Applet *applet) const;
0091     Plasma::Containment *internalContainmentOf(Plasma::Applet *applet) const;
0092 
0093     virtual bool configViewIsShown() const;
0094 
0095     virtual int viewsCount(int screen) const;
0096     virtual int viewsCount(QScreen *screen) const;
0097     virtual int viewsCount() const;
0098 
0099     Type type() const override;
0100 
0101     Latte::Corona *corona();
0102 
0103     QStringList unloadedContainmentsIds();
0104 
0105     virtual Types::ViewType latteViewType(int containmentId) const;
0106     const QList<Plasma::Containment *> *containments();
0107 
0108     Latte::View *highestPriorityView();
0109     Latte::View *viewForContainment(Plasma::Containment *containment);
0110     virtual QList<Latte::View *> sortedLatteViews(QList<Latte::View *> views = QList<Latte::View *>());
0111     virtual QList<Latte::View *> viewsWithPlasmaShortcuts();
0112     virtual QList<Latte::View *> latteViews();
0113     ViewsMap validViewsMap(ViewsMap *occupiedMap = nullptr);
0114     virtual void syncLatteViewsToScreens(Layout::ViewsMap *occupiedMap = nullptr);
0115 
0116     void syncToLayoutFile(bool removeLayoutId = false);
0117 
0118     void lock(); //! make it only read-only
0119     void renameLayout(QString newName);
0120     virtual void unloadContainments();
0121     void unloadLatteViews();
0122     void unlock(); //! make it writable which it should be the default
0123 
0124     virtual void setLastConfigViewFor(Latte::View *view);
0125     virtual Latte::View *lastConfigViewFor();
0126 
0127     //! this function needs the layout to have first set the corona through initToCorona() function
0128     virtual void addView(Plasma::Containment *containment, bool forceOnPrimary = false, int explicitScreen = -1, Layout::ViewsMap *occupied = nullptr);
0129     void copyView(Plasma::Containment *containment);
0130     void recreateView(Plasma::Containment *containment, bool delayed = true);
0131     bool latteViewExists(Plasma::Containment *containment);
0132 
0133     //! Available edges for specific view in that screen
0134     virtual QList<Plasma::Types::Location> availableEdgesForView(QScreen *scr, Latte::View *forView) const;
0135     //! All free edges in that screen
0136     virtual QList<Plasma::Types::Location> freeEdges(QScreen *scr) const;
0137     virtual QList<Plasma::Types::Location> freeEdges(int screen) const;
0138 
0139     //! Bind this latteView and its relevant containments(including systrays)
0140     //! to this layout. It is used for moving a Latte::View from layout to layout)
0141     void assignToLayout(Latte::View *latteView, QList<Plasma::Containment *> containments);
0142     //! Unassign that latteView from this layout (this is used for moving a latteView
0143     //! from layout to layout) and returns all the containments relevant to
0144     //! that latteView
0145     QList<Plasma::Containment *> unassignFromLayout(Latte::View *latteView);
0146 
0147     QString reportHtml(const ScreenPool *screenPool);
0148     QList<int> viewsScreens();
0149 
0150 public slots:
0151     Q_INVOKABLE void addNewView();
0152     Q_INVOKABLE int viewsWithTasks() const;
0153     virtual Q_INVOKABLE QList<int> qmlFreeEdges(int screen) const;  //change <Plasma::Types::Location> to <int> types
0154 
0155 signals:
0156     void activitiesChanged(); // to move at an interface
0157     void viewsCountChanged();
0158     void viewEdgeChanged();
0159 
0160     //! used from ConfigView(s) in order to be informed which is one should be shown
0161     void lastConfigViewForChanged(Latte::View *view);
0162 
0163     //! used from LatteView(s) in order to exist only one each time that has the highest priority
0164     //! to use the global shortcuts activations
0165     void preferredViewForShortcutsChanged(Latte::View *view);
0166 
0167 protected:
0168     void updateLastUsedActivity();
0169 
0170 protected:
0171     Latte::Corona *m_corona{nullptr};
0172 
0173     QList<Plasma::Containment *> m_containments;
0174 
0175     QHash<const Plasma::Containment *, Latte::View *> m_latteViews;
0176     QHash<const Plasma::Containment *, Latte::View *> m_waitingLatteViews;
0177 
0178 private slots:
0179     void addContainment(Plasma::Containment *containment);
0180     void appletCreated(Plasma::Applet *applet);
0181     void destroyedChanged(bool destroyed);
0182     void containmentDestroyed(QObject *cont);
0183 
0184 private:
0185     //! It can be used in order for LatteViews to not be created automatically when
0186     //! their corresponding containments are created e.g. copyView functionality
0187     bool blockAutomaticLatteViewCreation() const;
0188     void setBlockAutomaticLatteViewCreation(bool block);
0189 
0190     bool explicitDockOccupyEdge(int screen, Plasma::Types::Location location) const;
0191     bool primaryDockOccupyEdge(Plasma::Types::Location location) const;
0192 
0193     bool viewAtLowerScreenPriority(Latte::View *test, Latte::View *base);
0194     bool viewAtLowerEdgePriority(Latte::View *test, Latte::View *base);
0195 
0196     bool viewDataAtLowerEdgePriority(const ViewData &test, const ViewData &base) const;
0197     bool viewDataAtLowerScreenPriority(const ViewData &test, const ViewData &base) const;
0198     bool viewDataAtLowerStatePriority(const ViewData &test, const ViewData &base) const;
0199 
0200     bool mapContainsId(const ViewsMap *map, uint viewId) const;
0201 
0202     QList<int> containmentSystrays(Plasma::Containment *containment) const;
0203 
0204     QList<ViewData> sortedViewsData(const QList<ViewData> &viewsData);
0205 
0206 private:
0207     bool m_blockAutomaticLatteViewCreation{false};
0208 
0209     QPointer<Latte::View> m_lastConfigViewFor;
0210 
0211     QStringList m_unloadedContainmentsIds;
0212 
0213     QPointer<Storage> m_storage;
0214 
0215     //! try to avoid crashes from recreating the same views all the time
0216     QList<const Plasma::Containment *> m_viewsToRecreate;
0217 
0218     friend class Storage;
0219     friend class Latte::View;
0220 };
0221 
0222 }
0223 }
0224 
0225 #endif