File indexing completed on 2024-05-12 17:10:23

0001 /*
0002     SPDX-FileCopyrightText: 2009 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "panel.h"
0008 
0009 #include <QAction>
0010 #include <QQuickItem>
0011 
0012 #include <Plasma/Containment>
0013 #include <Plasma/Corona>
0014 #include <QScreen>
0015 
0016 #include "panelview.h"
0017 #include "scriptengine.h"
0018 #include "shellcorona.h"
0019 #include "widget.h"
0020 
0021 namespace WorkspaceScripting
0022 {
0023 Panel::Panel(Plasma::Containment *containment, ScriptEngine *engine)
0024     : Containment(containment, engine)
0025 {
0026 }
0027 
0028 Panel::~Panel()
0029 {
0030 }
0031 
0032 QString Panel::location() const
0033 {
0034     Plasma::Containment *c = containment();
0035     if (!c) {
0036         return "floating";
0037     }
0038 
0039     switch (c->location()) {
0040     case Plasma::Types::Floating:
0041         return "floating";
0042     case Plasma::Types::Desktop:
0043         return "desktop";
0044     case Plasma::Types::FullScreen:
0045         return "fullscreen";
0046     case Plasma::Types::TopEdge:
0047         return "top";
0048     case Plasma::Types::BottomEdge:
0049         return "bottom";
0050     case Plasma::Types::LeftEdge:
0051         return "left";
0052     case Plasma::Types::RightEdge:
0053         return "right";
0054     }
0055 
0056     return "floating";
0057 }
0058 
0059 void Panel::setLocation(const QString &locationString)
0060 {
0061     Plasma::Containment *c = containment();
0062     if (!c) {
0063         return;
0064     }
0065 
0066     const QString lower = locationString.toLower();
0067     Plasma::Types::Location loc = Plasma::Types::Floating;
0068     Plasma::Types::FormFactor ff = Plasma::Types::Planar;
0069     if (lower == "desktop") {
0070         loc = Plasma::Types::Desktop;
0071     } else if (lower == "fullscreen") {
0072         loc = Plasma::Types::FullScreen;
0073     } else if (lower == "top") {
0074         loc = Plasma::Types::TopEdge;
0075         ff = Plasma::Types::Horizontal;
0076     } else if (lower == "bottom") {
0077         loc = Plasma::Types::BottomEdge;
0078         ff = Plasma::Types::Horizontal;
0079     } else if (lower == "left") {
0080         loc = Plasma::Types::LeftEdge;
0081         ff = Plasma::Types::Vertical;
0082     } else if (lower == "right") {
0083         loc = Plasma::Types::RightEdge;
0084         ff = Plasma::Types::Vertical;
0085     }
0086 
0087     c->setLocation(loc);
0088     c->setFormFactor(ff);
0089 
0090     /*
0091      * After location is set, one layout script will usually start adding widgets.
0092      * It's required to emit formFactorChanged() to update plasmoid.formFactor bindings
0093      * in QML side to avoid Layout issues.
0094      *
0095      * @see isHorizontal in plasma-desktop/containments/panel/contents/ui/main.qml
0096      */
0097     c->flushPendingConstraintsEvents();
0098 }
0099 
0100 PanelView *Panel::panel() const
0101 {
0102     Plasma::Containment *c = containment();
0103     if (!c || !corona()) {
0104         return nullptr;
0105     }
0106 
0107     return corona()->panelView(c);
0108 }
0109 
0110 // NOTE: this is used *only* for alignment and visibility
0111 KConfigGroup Panel::panelConfig() const
0112 {
0113     int screenNum = qMax(screen(), 0); // if we don't have a screen (-1) we'll be put on screen 0
0114 
0115     if (QGuiApplication::screens().size() < screenNum) {
0116         return KConfigGroup();
0117     }
0118     QScreen *s = QGuiApplication::screens().at(screenNum);
0119     return PanelView::panelConfig(corona(), containment(), s);
0120 }
0121 
0122 // NOTE: when we don't have a view we should write only to the defaults group as we don't know yet during startup if we are on the "final" screen resolution yet
0123 KConfigGroup Panel::panelConfigDefaults() const
0124 {
0125     int screenNum = qMax(screen(), 0); // if we don't have a screen (-1) we'll be put on screen 0
0126 
0127     if (QGuiApplication::screens().size() < screenNum) {
0128         return KConfigGroup();
0129     }
0130     QScreen *s = QGuiApplication::screens().at(screenNum);
0131     return PanelView::panelConfigDefaults(corona(), containment(), s);
0132 }
0133 
0134 // NOTE: Alignment is the only one that reads and writes directly from panelconfig()
0135 QString Panel::alignment() const
0136 {
0137     int alignment;
0138     if (panel()) {
0139         alignment = panel()->alignment();
0140     } else {
0141         alignment = panelConfig().readEntry("alignment", 0);
0142     }
0143 
0144     switch (alignment) {
0145     case Qt::AlignRight:
0146         return "right";
0147     case Qt::AlignCenter:
0148         return "center";
0149     default:
0150         return "left";
0151     }
0152 
0153     return "left";
0154 }
0155 
0156 // NOTE: Alignment is the only one that reads and writes directly from panelconfig()
0157 void Panel::setAlignment(const QString &alignment)
0158 {
0159     int a = Qt::AlignLeft;
0160     if (alignment.compare("right", Qt::CaseInsensitive) == 0) {
0161         a = Qt::AlignRight;
0162     } else if (alignment.compare("center", Qt::CaseInsensitive) == 0) {
0163         a = Qt::AlignCenter;
0164     }
0165 
0166     // Always prefer the view, if available
0167     if (panel()) {
0168         panel()->setAlignment(Qt::Alignment(a));
0169     } else {
0170         panelConfig().writeEntry("alignment", a);
0171     }
0172 }
0173 
0174 // From now on only panelConfigDefaults should be used
0175 int Panel::offset() const
0176 {
0177     if (panel()) {
0178         return panel()->offset();
0179     } else {
0180         return panelConfigDefaults().readEntry("offset", 0);
0181     }
0182 }
0183 
0184 void Panel::setOffset(int pixels)
0185 {
0186     panelConfigDefaults().writeEntry("offset", pixels);
0187     if (panel()) {
0188         panel()->setOffset(pixels);
0189     } else {
0190         panelConfigDefaults().readEntry("offset", pixels);
0191     }
0192 }
0193 
0194 int Panel::length() const
0195 {
0196     if (panel()) {
0197         return panel()->length();
0198     } else {
0199         return panelConfigDefaults().readEntry("length", 0);
0200     }
0201 }
0202 
0203 void Panel::setLength(int pixels)
0204 {
0205     if (panel()) {
0206         panel()->setLength(pixels);
0207     } else {
0208         panelConfigDefaults().writeEntry("length", pixels);
0209     }
0210 }
0211 
0212 int Panel::minimumLength() const
0213 {
0214     if (panel()) {
0215         return panel()->minimumLength();
0216     } else {
0217         return panelConfigDefaults().readEntry("minLength", 0);
0218     }
0219 }
0220 
0221 void Panel::setMinimumLength(int pixels)
0222 {
0223     if (panel()) {
0224         panel()->setMinimumLength(pixels);
0225     } else {
0226         panelConfigDefaults().writeEntry("minLength", pixels);
0227     }
0228 }
0229 
0230 int Panel::maximumLength() const
0231 {
0232     if (panel()) {
0233         return panel()->maximumLength();
0234     } else {
0235         return panelConfigDefaults().readEntry("maxLength", 0);
0236     }
0237 }
0238 
0239 void Panel::setMaximumLength(int pixels)
0240 {
0241     if (panel()) {
0242         panel()->setMaximumLength(pixels);
0243     } else {
0244         panelConfigDefaults().writeEntry("maxLength", pixels);
0245     }
0246 }
0247 
0248 int Panel::height() const
0249 {
0250     if (panel()) {
0251         return panel()->thickness();
0252     } else {
0253         return panelConfigDefaults().readEntry("thickness", 0);
0254     }
0255 }
0256 
0257 void Panel::setHeight(int height)
0258 {
0259     if (panel()) {
0260         panel()->setThickness(height);
0261     } else {
0262         panelConfigDefaults().writeEntry("thickness", height);
0263     }
0264 }
0265 
0266 QString Panel::hiding() const
0267 {
0268     int visibility;
0269     if (panel()) {
0270         visibility = panel()->visibilityMode();
0271     } else {
0272         visibility = panelConfig().readEntry("panelVisibility", 0);
0273     }
0274 
0275     switch (visibility) {
0276     case PanelView::NormalPanel:
0277         return "none";
0278     case PanelView::AutoHide:
0279         return "autohide";
0280     case PanelView::LetWindowsCover:
0281         return "windowscover";
0282     case PanelView::WindowsGoBelow:
0283         return "windowsbelow";
0284     }
0285     return "none";
0286 }
0287 
0288 void Panel::setHiding(const QString &mode)
0289 {
0290     PanelView::VisibilityMode visibilityMode = PanelView::NormalPanel;
0291     if (mode.compare("autohide", Qt::CaseInsensitive) == 0) {
0292         visibilityMode = PanelView::AutoHide;
0293     } else if (mode.compare("windowscover", Qt::CaseInsensitive) == 0) {
0294         visibilityMode = PanelView::LetWindowsCover;
0295     } else if (mode.compare("windowsbelow", Qt::CaseInsensitive) == 0) {
0296         visibilityMode = PanelView::WindowsGoBelow;
0297     }
0298 
0299     if (panel()) {
0300         panel()->setVisibilityMode(visibilityMode);
0301     } else {
0302         panelConfig().writeEntry("panelVisibility", (int)visibilityMode);
0303     }
0304 }
0305 
0306 }