File indexing completed on 2024-06-16 14:04:30

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 #include "sharedlayout.h"
0021 
0022 // local
0023 #include "centrallayout.h"
0024 #include "../lattecorona.h"
0025 #include "../screenpool.h"
0026 #include "../layouts/manager.h"
0027 #include "../view/view.h"
0028 
0029 namespace Latte {
0030 
0031 SharedLayout::SharedLayout(CentralLayout *assigned, QObject *parent, QString layoutFile, QString layoutName)
0032     : Layout::GenericLayout (parent, layoutFile, layoutName)
0033 {
0034     initToCorona(assigned->corona());
0035 
0036     connect(m_corona->layoutsManager(), &Layouts::Manager::currentLayoutNameChanged, this, &SharedLayout::updateLastUsedCentralLayout);
0037 
0038     addCentralLayout(assigned);
0039     updateLastUsedCentralLayout();
0040 }
0041 
0042 
0043 SharedLayout::~SharedLayout()
0044 {
0045     qDebug() << " SHARED LAYOUT destroying ::: " << name();
0046 }
0047 
0048 bool SharedLayout::isCurrent() const
0049 {
0050     for (const auto  &layout : m_centralLayouts) {
0051         if (layout->isCurrent()) {
0052             return true;
0053         }
0054     }
0055 
0056     return false;
0057 }
0058 
0059 Layout::Type SharedLayout::type() const
0060 {
0061     return Layout::Type::Shared;
0062 }
0063 
0064 
0065 const QStringList SharedLayout::appliedActivities()
0066 {
0067     if (!m_corona) {
0068         return {};
0069     }
0070 
0071     QStringList activities;
0072 
0073     for (const auto  &layout : m_centralLayouts) {
0074         activities << layout->appliedActivities();
0075     }
0076 
0077     return activities;
0078 }
0079 
0080 void SharedLayout::updateLastUsedCentralLayout()
0081 {
0082     for (const auto  &layout : m_centralLayouts) {
0083         if (layout->isCurrent()) {
0084             m_lastUsedCentralLayout = layout->name();
0085             break;
0086         }
0087     }
0088 }
0089 
0090 CentralLayout *SharedLayout::currentCentralLayout() const
0091 {
0092     //! first the current active one
0093     for (const auto  &layout : m_centralLayouts) {
0094         if (layout->isCurrent()) {
0095             return layout;
0096         }
0097     }
0098 
0099     //! the last used
0100     for (const auto  &layout : m_centralLayouts) {
0101         if (layout->name() == m_lastUsedCentralLayout) {
0102             return layout;
0103         }
0104     }
0105 
0106     return nullptr;
0107 }
0108 
0109 void SharedLayout::addCentralLayout(CentralLayout *layout)
0110 {
0111     if (layout != nullptr && !m_centralLayouts.contains(layout)) {
0112         m_centralLayouts.append(layout);
0113 
0114         qDebug() << " ADDING Central : " << layout->name() << " at Shared: " << name();
0115         connect(layout, &GenericLayout::activitiesChanged, this, &GenericLayout::activitiesChanged);
0116         emit activitiesChanged();
0117         emit viewsCountChanged();
0118 
0119         updateLastUsedActivity();
0120     }
0121 }
0122 
0123 void SharedLayout::removeCentralLayout(CentralLayout *layout)
0124 {
0125     if (m_centralLayouts.contains(layout)) {
0126         qDebug() << "SHAREDLAYOUT <" << name() << "> : Removing active layout, " << layout->name();
0127         m_centralLayouts.removeAll(layout);
0128 
0129         disconnect(layout, &GenericLayout::activitiesChanged, this, &GenericLayout::activitiesChanged);
0130 
0131         if (m_centralLayouts.count() > 0) {
0132             emit activitiesChanged();
0133         } else {
0134             //! all assigned layouts have been unloaded so the shared layout should be destroyed also
0135             emit layoutDestroyed(this);
0136         }
0137 
0138         //! viewsCount signal is not needed to be trigerred here because
0139         //! in such case the views number has not been changed for the rest
0140         //! active layouts
0141     }
0142 }
0143 
0144 //! OVERRIDE
0145 int SharedLayout::viewsCount(int screen) const
0146 {
0147     if (!m_corona) {
0148         return 0;
0149     }
0150 
0151     CentralLayout *current = currentCentralLayout();
0152 
0153     if (current) {
0154         return current->viewsCount(screen);
0155     }
0156 
0157     return Layout::GenericLayout::viewsCount(screen);
0158 }
0159 
0160 int SharedLayout::viewsCount(QScreen *screen) const
0161 {
0162     if (!m_corona) {
0163         return 0;
0164     }
0165 
0166     CentralLayout *current = currentCentralLayout();
0167 
0168     if (current) {
0169         return current->viewsCount(screen);
0170     }
0171 
0172     return Layout::GenericLayout::viewsCount(screen);;
0173 }
0174 
0175 int SharedLayout::viewsCount() const
0176 {
0177     if (!m_corona) {
0178         return 0;
0179     }
0180     
0181     CentralLayout *current = currentCentralLayout();
0182 
0183     if (current) {
0184         return current->viewsCount();
0185     }
0186 
0187     return Layout::GenericLayout::viewsCount();
0188 }
0189 
0190 QList<Plasma::Types::Location> SharedLayout::availableEdgesForView(QScreen *scr, Latte::View *forView) const
0191 {
0192  /*   using Plasma::Types;
0193     QList<Types::Location> edges{Types::BottomEdge, Types::LeftEdge,
0194                 Types::TopEdge, Types::RightEdge};
0195 
0196     if (!m_corona) {
0197         return edges;
0198     }*/
0199 
0200     return Layout::GenericLayout::availableEdgesForView(scr, forView);
0201 }
0202 
0203 QList<Plasma::Types::Location> SharedLayout::freeEdges(QScreen *scr) const
0204 {
0205  /*   using Plasma::Types;
0206     QList<Types::Location> edges{Types::BottomEdge, Types::LeftEdge,
0207                 Types::TopEdge, Types::RightEdge};
0208 
0209     if (!m_corona) {
0210         return edges;
0211     }
0212 
0213     CentralLayout *current = currentCentralLayout();
0214 
0215     if (current) {
0216         return current->freeEdges(scr);
0217     }*/
0218 
0219     return Layout::GenericLayout::freeEdges(scr);
0220 }
0221 
0222 QList<Plasma::Types::Location> SharedLayout::freeEdges(int screen) const
0223 {
0224   /*  using Plasma::Types;
0225     QList<Types::Location> edges{Types::BottomEdge, Types::LeftEdge,
0226                 Types::TopEdge, Types::RightEdge};
0227 
0228     if (!m_corona) {
0229         return edges;
0230     }
0231 
0232     CentralLayout *current = currentCentralLayout();
0233 
0234     if (current) {
0235         return current->freeEdges(screen);
0236     }*/
0237 
0238     return Layout::GenericLayout::freeEdges(screen);
0239 }
0240 
0241 void SharedLayout::setLastConfigViewFor(Latte::View *view)
0242 {
0243     CentralLayout *current = currentCentralLayout();
0244 
0245     if (current) {
0246         return current->setLastConfigViewFor(view);
0247     }
0248 
0249     return GenericLayout::setLastConfigViewFor(view);
0250 }
0251 
0252 Latte::View *SharedLayout::lastConfigViewFor()
0253 {
0254     CentralLayout *current = currentCentralLayout();
0255 
0256     if (current) {
0257         return current->lastConfigViewFor();
0258     }
0259 
0260     return GenericLayout::lastConfigViewFor();
0261 }
0262 
0263 QList<Latte::View *> SharedLayout::sortedLatteViews(QList<Latte::View *> views)
0264 {
0265     CentralLayout *current = currentCentralLayout();
0266 
0267     if (current) {
0268         return current->sortedLatteViews();
0269     }
0270 
0271     return Layout::GenericLayout::sortedLatteViews();
0272 }
0273 
0274 }