Don't rely graphic objects in panel script engine

The basic design of Plasma is that scripts and and the shell (in theory)
manipulate a tree of basic applet geometry and configs.

Plasmashell then reacts to those changes and displays them visually with
a distinct separation between the layout and UI.

Panel's scriptengine seemed to do away with all, and try and manipulate
the graphic object directly..which might not exist and that leads to
complex code.

This changes it to read/write from the same config object as
PanelView will use. More akin to how the script engine for applet and
contiainment works.

If there's a view for this panel, we update immediately, otherwise it'll
just get loaded when it's needed. PanelView::reload() has the error
checking/bounds management so no point duplicating that.

BUG: 355918
REVIEW: 125921
wilder-5.14
David Edmundson 10 years ago committed by David Edmundson
parent 3ff5cdd3cf
commit 0dc7f6c3e5
  1. 47
      shell/panelview.cpp
  2. 3
      shell/panelview.h
  3. 366
      shell/scripting/panel.cpp
  4. 1
      shell/scripting/panel.h

@ -123,22 +123,27 @@ PanelView::~PanelView()
PanelShadows::self()->removeWindow(this); PanelShadows::self()->removeWindow(this);
} }
KConfigGroup PanelView::config() const KConfigGroup PanelView::panelConfig(ShellCorona *corona, Plasma::Containment *containment, QScreen *screen)
{ {
if (!containment()) { if (!containment || !screen) {
return KConfigGroup(); return KConfigGroup();
} }
KConfigGroup views(m_corona->applicationConfig(), "PlasmaViews"); KConfigGroup views(corona->applicationConfig(), "PlasmaViews");
views = KConfigGroup(&views, QStringLiteral("Panel %1").arg(containment()->id())); views = KConfigGroup(&views, QStringLiteral("Panel %1").arg(containment->id()));
if (formFactor() == Plasma::Types::Vertical) { if (containment->formFactor() == Plasma::Types::Vertical) {
return KConfigGroup(&views, "Vertical" + QString::number(screen()->size().height())); return KConfigGroup(&views, "Vertical" + QString::number(screen->size().height()));
//treat everything else as horizontal //treat everything else as horizontal
} else { } else {
return KConfigGroup(&views, "Horizontal" + QString::number(screen()->size().width())); return KConfigGroup(&views, "Horizontal" + QString::number(screen->size().width()));
} }
} }
KConfigGroup PanelView::config() const
{
return panelConfig(m_corona, containment(), screen());
}
void PanelView::maximize() void PanelView::maximize()
{ {
int length; int length;
@ -491,26 +496,12 @@ void PanelView::restore()
int defaultMaxLength = 0; int defaultMaxLength = 0;
int defaultMinLength = 0; int defaultMinLength = 0;
int defaultAlignment = Qt::AlignLeft; int defaultAlignment = Qt::AlignLeft;
QQuickItem *containmentItem = containment()->property("_plasma_graphicObject").value<QQuickItem *>();
if (containmentItem && containmentItem->property("_plasma_desktopscripting_alignment").canConvert<int>()) {
defaultAlignment = containmentItem->property("_plasma_desktopscripting_alignment").toInt();
}
setAlignment((Qt::Alignment)config().readEntry<int>("alignment", defaultAlignment)); setAlignment((Qt::Alignment)config().readEntry<int>("alignment", defaultAlignment));
if (containmentItem && containmentItem->property("_plasma_desktopscripting_offset").canConvert<int>()) {
defaultOffset = containmentItem->property("_plasma_desktopscripting_offset").toInt();
}
m_offset = config().readEntry<int>("offset", defaultOffset); m_offset = config().readEntry<int>("offset", defaultOffset);
if (m_alignment != Qt::AlignCenter) { if (m_alignment != Qt::AlignCenter) {
m_offset = qMax(0, m_offset); m_offset = qMax(0, m_offset);
} }
if (containmentItem && containmentItem->property("_plasma_desktopscripting_thickness").canConvert<int>()) {
defaultThickness = qMax(16, containmentItem->property("_plasma_desktopscripting_thickness").toInt());
}
setThickness(config().readEntry<int>("thickness", defaultThickness)); setThickness(config().readEntry<int>("thickness", defaultThickness));
setMinimumSize(QSize(-1, -1)); setMinimumSize(QSize(-1, -1));
@ -521,13 +512,6 @@ void PanelView::restore()
defaultMaxLength = screen()->size().height(); defaultMaxLength = screen()->size().height();
defaultMinLength = screen()->size().height(); defaultMinLength = screen()->size().height();
if (containmentItem && containmentItem->property("_plasma_desktopscripting_maxLength").canConvert<int>()) {
defaultMaxLength = containmentItem->property("_plasma_desktopscripting_maxLength").toInt();
}
if (containmentItem && containmentItem->property("_plasma_desktopscripting_minLength").canConvert<int>()) {
defaultMinLength = containmentItem->property("_plasma_desktopscripting_minLength").toInt();
}
m_maxLength = config().readEntry<int>("maxLength", defaultMaxLength); m_maxLength = config().readEntry<int>("maxLength", defaultMaxLength);
m_minLength = config().readEntry<int>("minLength", defaultMinLength); m_minLength = config().readEntry<int>("minLength", defaultMinLength);
@ -545,13 +529,6 @@ void PanelView::restore()
defaultMaxLength = screen()->size().width(); defaultMaxLength = screen()->size().width();
defaultMinLength = screen()->size().width(); defaultMinLength = screen()->size().width();
if (containmentItem && containmentItem->property("_plasma_desktopscripting_maxLength").canConvert<int>()) {
defaultMaxLength = containmentItem->property("_plasma_desktopscripting_maxLength").toInt();
}
if (containmentItem && containmentItem->property("_plasma_desktopscripting_minLength").canConvert<int>()) {
defaultMinLength = containmentItem->property("_plasma_desktopscripting_minLength").toInt();
}
m_maxLength = config().readEntry<int>("maxLength", defaultMaxLength); m_maxLength = config().readEntry<int>("maxLength", defaultMaxLength);
m_minLength = config().readEntry<int>("minLength", defaultMinLength); m_minLength = config().readEntry<int>("minLength", defaultMinLength);

@ -133,6 +133,9 @@ public:
*/ */
QRect geometryByDistance(int distance) const; QRect geometryByDistance(int distance) const;
/* Shared with script/panel.cpp */
static KConfigGroup panelConfig(ShellCorona *corona, Plasma::Containment *containment, QScreen *screen);
protected: protected:
void resizeEvent(QResizeEvent *ev); void resizeEvent(QResizeEvent *ev);
void showEvent(QShowEvent *event); void showEvent(QShowEvent *event);

@ -48,34 +48,34 @@ QString Panel::location() const
{ {
Plasma::Containment *c = containment(); Plasma::Containment *c = containment();
if (!c) { if (!c) {
return QStringLiteral("floating"); return "floating";
} }
switch (c->location()) { switch (c->location()) {
case Plasma::Types::Floating: case Plasma::Types::Floating:
return QStringLiteral("floating"); return "floating";
break; break;
case Plasma::Types::Desktop: case Plasma::Types::Desktop:
return QStringLiteral("desktop"); return "desktop";
break; break;
case Plasma::Types::FullScreen: case Plasma::Types::FullScreen:
return QStringLiteral("fullscreen"); return "fullscreen";
break; break;
case Plasma::Types::TopEdge: case Plasma::Types::TopEdge:
return QStringLiteral("top"); return "top";
break; break;
case Plasma::Types::BottomEdge: case Plasma::Types::BottomEdge:
return QStringLiteral("bottom"); return "bottom";
break; break;
case Plasma::Types::LeftEdge: case Plasma::Types::LeftEdge:
return QStringLiteral("left"); return "left";
break; break;
case Plasma::Types::RightEdge: case Plasma::Types::RightEdge:
return QStringLiteral("right"); return "right";
break; break;
} }
return QStringLiteral("floating"); return "floating";
} }
void Panel::setLocation(const QString &locationString) void Panel::setLocation(const QString &locationString)
@ -88,20 +88,20 @@ void Panel::setLocation(const QString &locationString)
const QString lower = locationString.toLower(); const QString lower = locationString.toLower();
Plasma::Types::Location loc = Plasma::Types::Floating; Plasma::Types::Location loc = Plasma::Types::Floating;
Plasma::Types::FormFactor ff = Plasma::Types::Planar; Plasma::Types::FormFactor ff = Plasma::Types::Planar;
if (lower == QLatin1String("desktop")) { if (lower == "desktop") {
loc = Plasma::Types::Desktop; loc = Plasma::Types::Desktop;
} else if (lower == QLatin1String("fullscreen")) { } else if (lower == "fullscreen") {
loc = Plasma::Types::FullScreen; loc = Plasma::Types::FullScreen;
} else if (lower == QLatin1String("top")) { } else if (lower == "top") {
loc = Plasma::Types::TopEdge; loc = Plasma::Types::TopEdge;
ff = Plasma::Types::Horizontal; ff = Plasma::Types::Horizontal;
} else if (lower == QLatin1String("bottom")) { } else if (lower == "bottom") {
loc = Plasma::Types::BottomEdge; loc = Plasma::Types::BottomEdge;
ff = Plasma::Types::Horizontal; ff = Plasma::Types::Horizontal;
} else if (lower == QLatin1String("left")) { } else if (lower == "left") {
loc = Plasma::Types::LeftEdge; loc = Plasma::Types::LeftEdge;
ff = Plasma::Types::Vertical; ff = Plasma::Types::Vertical;
} else if (lower == QLatin1String("right")) { } else if (lower == "right") {
loc = Plasma::Types::RightEdge; loc = Plasma::Types::RightEdge;
ff = Plasma::Types::Vertical; ff = Plasma::Types::Vertical;
} }
@ -120,336 +120,148 @@ PanelView *Panel::panel() const
return m_corona->panelView(c); return m_corona->panelView(c);
} }
QString Panel::alignment() const
KConfigGroup Panel::panelConfig() const
{ {
PanelView *v = panel(); int screenNum = qMax(screen(), 0); //if we don't have a screen (-1) we'll be put on screen 0
if (!v) {
return QStringLiteral("left"); if (QGuiApplication::screens().size() < screenNum) {
return KConfigGroup();
} }
QScreen *s = QGuiApplication::screens().at(screenNum);
return PanelView::panelConfig(m_corona, containment(), s);
}
switch (v->alignment()) { QString Panel::alignment() const
{
switch (panelConfig().readEntry("alignment", 0)) {
case Qt::AlignRight: case Qt::AlignRight:
return QStringLiteral("right"); return "right";
break; break;
case Qt::AlignCenter: case Qt::AlignCenter:
return QStringLiteral("center"); return "center";
break; break;
default: default:
return QStringLiteral("left"); return "left";
break; break;
} }
return QStringLiteral("left"); return "left";
} }
void Panel::setAlignment(const QString &alignment) void Panel::setAlignment(const QString &alignment)
{ {
Qt::Alignment alignmentValue = Qt::AlignLeft; int a = Qt::AlignLeft;
if (alignment.compare("right", Qt::CaseInsensitive) == 0) {
bool success = false; a = Qt::AlignRight;
} else if (alignment.compare("center", Qt::CaseInsensitive) == 0) {
if (alignment.compare(QLatin1String("left"), Qt::CaseInsensitive) == 0) { a = Qt::AlignCenter;
success = true;
alignmentValue = Qt::AlignLeft;
} else if (alignment.compare(QLatin1String("right"), Qt::CaseInsensitive) == 0) {
success = true;
alignmentValue = Qt::AlignRight;
} else if (alignment.compare(QLatin1String("center"), Qt::CaseInsensitive) == 0) {
success = true;
alignmentValue = Qt::AlignCenter;
} }
if (!success) { panelConfig().writeEntry("alignment", a);
return; if (panel()) {
} QMetaObject::invokeMethod(panel(), "restore");
PanelView *v = panel();
if (v) {
v->setOffset(0);
v->setAlignment(alignmentValue);
} else {
QQuickItem *graphicObject = qobject_cast<QQuickItem *>(containment()->property("_plasma_graphicObject").value<QObject *>());
if (!graphicObject) {
return;
}
graphicObject->setProperty("_plasma_desktopscripting_alignment", (int)alignmentValue);
} }
} }
int Panel::offset() const int Panel::offset() const
{ {
PanelView *v = panel(); return panelConfig().readEntry("offset", 0);
if (v) {
return v->offset();
}
return 0;
} }
void Panel::setOffset(int pixels) void Panel::setOffset(int pixels)
{ {
Plasma::Containment *c = containment(); panelConfig().writeEntry("offset", pixels);
if (pixels < 0 || !c) { if (panel()) {
return; QMetaObject::invokeMethod(panel(), "restore");
} }
QQuickItem *graphicObject = qobject_cast<QQuickItem *>(c->property("_plasma_graphicObject").value<QObject *>());
if (!graphicObject) {
return;
}
PanelView *v = panel();
if (v) {
QRectF screen = v->screen()->geometry();
QSizeF size(graphicObject->width(), graphicObject->height());
if (c->formFactor() == Plasma::Types::Vertical) {
if (pixels > screen.height()) {
return;
}
if (size.height() + pixels > screen.height()) {
graphicObject->setWidth(size.width());
graphicObject->setHeight(screen.height() - pixels);
}
} else if (pixels > screen.width()) {
return;
} else if (size.width() + pixels > screen.width()) {
size.setWidth(screen.width() - pixels);
graphicObject->setWidth(size.width());
graphicObject->setHeight(size.height());
v->setMinimumSize(size.toSize());
v->setMaximumSize(size.toSize());
}
v->setOffset(pixels);
}
} }
int Panel::length() const int Panel::length() const
{ {
Plasma::Containment *c = containment(); return panelConfig().readEntry("length", 0);
if (!c) {
return 0;
}
QQuickItem *graphicObject = qobject_cast<QQuickItem *>(c->property("_plasma_graphicObject").value<QObject *>());
if (!graphicObject) {
return 0;
}
if (c->formFactor() == Plasma::Types::Vertical) {
return graphicObject->height();
} else {
return graphicObject->width();
}
} }
void Panel::setLength(int pixels) void Panel::setLength(int pixels)
{ {
PanelView *v = panel(); panelConfig().writeEntry("length", pixels);
Plasma::Containment *c = containment(); if (panel()) {
QMetaObject::invokeMethod(panel(), "restore");
if (!v || !c || pixels < 0 || pixels > v->maximumLength() || pixels < v->minimumLength()) { }
return;
}
QRectF screen = v->screen()->geometry();
if (c->formFactor() == Plasma::Types::Vertical
&& pixels > screen.height() - v->offset()) {
return;
} else if (pixels > screen.width() - v->offset()) {
return;
}
v->setLength(pixels);
} }
int Panel::minimumLength() const int Panel::minimumLength() const
{ {
PanelView *v = panel(); return panelConfig().readEntry("minLength", 0);
if (v) {
return v->minimumLength();
}
return 0;
} }
void Panel::setMinimumLength(int pixels) void Panel::setMinimumLength(int pixels)
{ {
PanelView *v = panel(); panelConfig().writeEntry("minLength", pixels);
Plasma::Containment *c = containment(); if (panel()) {
QMetaObject::invokeMethod(panel(), "restore");
if (!c || pixels < 0) { }
return;
}
//NOTE: due to dependency of class creation at startup, we can't in any way have
//the panel views already instantiated, so put the property in a placeholder dynamic property
if (!v) {
QQuickItem *graphicObject = qobject_cast<QQuickItem *>(c->property("_plasma_graphicObject").value<QObject *>());
if (!graphicObject) {
return;
}
graphicObject->setProperty("_plasma_desktopscripting_minLength", pixels);
return;
}
QRectF screen = v->screen()->geometry();
if (c->formFactor() == Plasma::Types::Vertical
&& pixels > screen.height() - v->offset()) {
return;
} else if (pixels > screen.width() - v->offset()) {
return;
}
v->setMinimumLength(pixels);
if (v->maximumLength() < pixels) {
v->setMaximumLength(pixels);
}
} }
int Panel::maximumLength() const int Panel::maximumLength() const
{ {
PanelView *v = panel(); return panelConfig().readEntry("maxLength", 0);
if (v) {
return v->maximumLength();
}
return 0;
} }
void Panel::setMaximumLength(int pixels) void Panel::setMaximumLength(int pixels)
{ {
PanelView *v = panel(); panelConfig().writeEntry("maxLength", pixels);
Plasma::Containment *c = containment(); if (panel()) {
QMetaObject::invokeMethod(panel(), "restore");
if (!c || pixels < 0) { }
return;
}
//NOTE: due to dependency of class creation at startup, we can't in any way have
//the panel views already instantiated, so put the property in a placeholder dynamic property
if (!v) {
QQuickItem *graphicObject = qobject_cast<QQuickItem *>(c->property("_plasma_graphicObject").value<QObject *>());
if (!graphicObject) {
return;
}
graphicObject->setProperty("_plasma_desktopscripting_maxLength", pixels);
return;
}
QRectF screen = v->screen()->geometry();
if (c->formFactor() == Plasma::Types::Vertical
&& pixels > screen.height() - v->offset()) {
return;
} else if (pixels > screen.width() - v->offset()) {
return;
}
v->setMaximumLength(pixels);
if (v->minimumLength() > pixels) {
v->setMinimumLength(pixels);
}
} }
int Panel::height() const int Panel::height() const
{ {
Plasma::Containment *c = containment(); return panelConfig().readEntry("thickness", 0);
if (!c) {
return 0;
}
QQuickItem *graphicObject = qobject_cast<QQuickItem *>(c->property("_plasma_graphicObject").value<QObject *>());
if (!graphicObject) {
return 0;
}
return c->formFactor() == Plasma::Types::Vertical ? graphicObject->width()
: graphicObject->height();
} }
void Panel::setHeight(int height) void Panel::setHeight(int height)
{ {
Plasma::Containment *c = containment(); panelConfig().writeEntry("thickness", height);
if (height < 16 || !c) { if (panel()) {
return; QMetaObject::invokeMethod(panel(), "restore");
}
QQuickItem *graphicObject = qobject_cast<QQuickItem *>(c->property("_plasma_graphicObject").value<QObject *>());
if (!graphicObject) {
return;
}
PanelView *v = panel();
if (v) {
QRect screen = v->screen()->geometry();
QSizeF size(graphicObject->width(), graphicObject->height());
const int max = (c->formFactor() == Plasma::Types::Vertical ? screen.width() : screen.height()) / 3;
height = qBound(16, height, max);
v->setThickness(height);
} else {
//NOTE: due to dependency of class creation at startup, we can't in any way have
//the panel views already instantiated, so put the property in a placeholder dynamic property
QQuickItem *graphicObject = qobject_cast<QQuickItem *>(c->property("_plasma_graphicObject").value<QObject *>());
if (!graphicObject) {
return;
}
graphicObject->setProperty("_plasma_desktopscripting_thickness", height);
} }
} }
QString Panel::hiding() const QString Panel::hiding() const
{ {
PanelView *v = panel(); switch (panelConfig().readEntry("panelVisibility", 0)) {
if (v) { case PanelView::NormalPanel:
switch (v->visibilityMode()) { return "none";
case PanelView::NormalPanel: break;
return QStringLiteral("none"); case PanelView::AutoHide:
break; return "autohide";
case PanelView::AutoHide: break;
return QStringLiteral("autohide"); case PanelView::LetWindowsCover:
break; return "windowscover";
case PanelView::LetWindowsCover: break;
return QStringLiteral("windowscover"); case PanelView::WindowsGoBelow:
break; return "windowsbelow";
case PanelView::WindowsGoBelow: break;
return QStringLiteral("windowsbelow");
break;
}
} }
return "none";
return QStringLiteral("none");
} }
void Panel::setHiding(const QString &mode) void Panel::setHiding(const QString &mode)
{ {
PanelView *v = panel(); PanelView::VisibilityMode visibilityMode = PanelView::NormalPanel;
if (v) { if (mode.compare("autohide", Qt::CaseInsensitive) == 0) {
if (mode.compare(QLatin1String("autohide"), Qt::CaseInsensitive) == 0) { visibilityMode = PanelView::AutoHide;
v->setVisibilityMode(PanelView::AutoHide); } else if (mode.compare("windowscover", Qt::CaseInsensitive) == 0) {
} else if (mode.compare(QLatin1String("windowscover"), Qt::CaseInsensitive) == 0) { visibilityMode = PanelView::LetWindowsCover;
v->setVisibilityMode(PanelView::LetWindowsCover); } else if (mode.compare("windowsbelow", Qt::CaseInsensitive) == 0) {
} else if (mode.compare(QLatin1String("windowsbelow"), Qt::CaseInsensitive) == 0) { visibilityMode = PanelView::WindowsGoBelow;
v->setVisibilityMode(PanelView::WindowsGoBelow); }
} else {
v->setVisibilityMode(PanelView::NormalPanel); panelConfig().writeEntry("panelVisibility", (int)visibilityMode);
} if (panel()) {
QMetaObject::invokeMethod(panel(), "restore");
} }
} }

@ -100,6 +100,7 @@ public Q_SLOTS:
private: private:
PanelView *panel() const; PanelView *panel() const;
KConfigGroup panelConfig() const;
ShellCorona *m_corona; ShellCorona *m_corona;
}; };

Loading…
Cancel
Save