diff --git a/scriptengines/javascript/CMakeLists.txt b/scriptengines/javascript/CMakeLists.txt index d259b7fbf..fe0658223 100644 --- a/scriptengines/javascript/CMakeLists.txt +++ b/scriptengines/javascript/CMakeLists.txt @@ -12,6 +12,7 @@ set(simple_javascript_engine_SRCS common/extension_io.cpp common/guiscriptenv.cpp common/javascriptaddonpackagestructure.cpp + declarative/appletcontainer.cpp plasmoid/abstractjsappletscript.cpp plasmoid/appletauthorization.cpp plasmoid/jsappletinterface.cpp @@ -53,6 +54,7 @@ target_link_libraries(plasma_appletscript_simple_javascript ${KDE4_KDECORE_LIBS} ${KDE4_KIO_LIBS} ${KDE4_PLASMA_LIBS} + ${QT_QTDECLARATIVE_LIBRARY} ${QT_QTSCRIPT_LIBRARY} ${QT_QTUITOOLS_LIBRARY} ${QT_QTXML_LIBRARY} @@ -129,6 +131,7 @@ set(declarative_appletscript_SRCS common/extension_io.cpp common/javascriptaddonpackagestructure.cpp common/declarativescriptenv.cpp + declarative/appletcontainer.cpp declarative/declarativeitemcontainer.cpp declarative/packageaccessmanager.cpp declarative/packageaccessmanagerfactory.cpp diff --git a/scriptengines/javascript/declarative/appletcontainer.cpp b/scriptengines/javascript/declarative/appletcontainer.cpp new file mode 100644 index 000000000..2dc540920 --- /dev/null +++ b/scriptengines/javascript/declarative/appletcontainer.cpp @@ -0,0 +1,204 @@ +/* + Copyright 2011 Marco Martin + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "appletcontainer.h" + +#include + +#include + +#include + +AppletContainer::AppletContainer(QDeclarativeItem *parent) + : QDeclarativeItem(parent) +{ + setFlag(QGraphicsItem::ItemHasNoContents, true); + + //the virtual geometryChanged is *NOT* called in case of change by the anchors + connect(this, SIGNAL(widthChanged()), this, SLOT(afterWidthChanged()), Qt::QueuedConnection); + connect(this, SIGNAL(heightChanged()), this, SLOT(afterHeightChanged()), Qt::QueuedConnection); +} + +AppletContainer::~AppletContainer() +{ +} + +QGraphicsWidget *AppletContainer::applet() const +{ + return m_applet.data(); +} + +void AppletContainer::setApplet(QGraphicsWidget *widget) +{ + Plasma::Applet *applet = qobject_cast(widget); + if (!applet || applet == m_applet.data()) { + return; + } + + if (m_applet) { + disconnect(m_applet.data(), 0, this, 0); + m_applet.data()->setParentItem(parentItem()); + } + + m_applet = applet; + + connect(applet, SIGNAL(sizeHintChanged(Qt::SizeHint)), this, SLOT(sizeHintChanged(Qt::SizeHint))); + connect(applet, SIGNAL(newStatus(Plasma::ItemStatus)), this, SIGNAL(statusChanged())); + + applet->setParentItem(this); + applet->setGeometry(0, 0, qMax((qreal)16, width()), qMax((qreal)16, height())); + applet->setFlag(QGraphicsItem::ItemIsMovable, false); + + emit appletChanged(widget); + emit statusChanged(); +} + +void AppletContainer::sizeHintChanged(Qt::SizeHint which) +{ + switch (which) { + case Qt::MinimumSize: + emit minimumWidthChanged(minimumWidth()); + emit minimumHeightChanged(minimumHeight()); + break; + case Qt::PreferredSize: + emit preferredWidthChanged(preferredWidth()); + emit preferredHeightChanged(preferredHeight()); + break; + case Qt::MaximumSize: + emit maximumWidthChanged(maximumWidth()); + emit maximumHeightChanged(maximumHeight()); + break; + default: + break; + } +} + +int AppletContainer::minimumWidth() const +{ + if (!m_applet) { + return -1; + } + + return m_applet.data()->effectiveSizeHint(Qt::MinimumSize).width(); +} + +int AppletContainer::minimumHeight() const +{ + if (!m_applet) { + return -1; + } + + return m_applet.data()->effectiveSizeHint(Qt::MinimumSize).height(); +} + + +int AppletContainer::preferredWidth() const +{ + if (!m_applet) { + return -1; + } + + return m_applet.data()->effectiveSizeHint(Qt::PreferredSize).width(); +} + +int AppletContainer::preferredHeight() const +{ + if (!m_applet) { + return -1; + } + + return m_applet.data()->effectiveSizeHint(Qt::PreferredSize).height(); +} + + +int AppletContainer::maximumWidth() const +{ + if (!m_applet) { + return -1; + } + + return m_applet.data()->effectiveSizeHint(Qt::MaximumSize).width(); +} + +int AppletContainer::maximumHeight() const +{ + if (!m_applet) { + return -1; + } + + return m_applet.data()->effectiveSizeHint(Qt::MaximumSize).height(); +} + +void AppletContainer::setStatus(const AppletContainer::ItemStatus status) +{ + if (!m_applet) { + return; + } + + m_applet.data()->setStatus((Plasma::ItemStatus)status); +} + +AppletContainer::ItemStatus AppletContainer::status() const +{ + if (!m_applet) { + return UnknownStatus; + } + + return (AppletContainer::ItemStatus)((int)(m_applet.data()->status())); +} + +void AppletContainer::afterWidthChanged() +{ + if (!m_applet) { + return; + } + + m_applet.data()->resize(width(), height()); + m_applet.data()->setPos(width()/2 - m_applet.data()->size().width()/2, + height()/2 - m_applet.data()->size().height()/2); + emit minimumWidthChanged(minimumWidth()); + emit preferredWidthChanged(preferredWidth()); + emit maximumWidthChanged(maximumWidth()); + + emit minimumHeightChanged(minimumHeight()); + emit preferredHeightChanged(preferredHeight()); + emit maximumHeightChanged(maximumHeight()); +} + +void AppletContainer::afterHeightChanged() +{ + if (!m_applet) { + return; + } + + m_applet.data()->resize(width(), height()); + m_applet.data()->setPos(width()/2 - m_applet.data()->size().width()/2, + height()/2 - m_applet.data()->size().height()/2); + + emit minimumWidthChanged(minimumWidth()); + emit preferredWidthChanged(preferredWidth()); + emit maximumWidthChanged(maximumWidth()); + + emit minimumHeightChanged(minimumHeight()); + emit preferredHeightChanged(preferredHeight()); + emit maximumHeightChanged(maximumHeight()); +} + + +#include "appletcontainer.moc" diff --git a/scriptengines/javascript/declarative/appletcontainer.h b/scriptengines/javascript/declarative/appletcontainer.h new file mode 100644 index 000000000..a877585a3 --- /dev/null +++ b/scriptengines/javascript/declarative/appletcontainer.h @@ -0,0 +1,97 @@ +/* + Copyright 2011 Marco Martin + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef APPLETCONTAINER_H +#define APPLETCONTAINER_H + +#include + +namespace Plasma { + class Applet; +} + +class AppletContainer : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(QGraphicsWidget *applet READ applet WRITE setApplet NOTIFY appletChanged) + + Q_PROPERTY(int minimumWidth READ minimumWidth NOTIFY minimumWidthChanged) + Q_PROPERTY(int minimumHeight READ minimumHeight NOTIFY minimumHeightChanged) + + Q_PROPERTY(int preferredWidth READ preferredWidth NOTIFY preferredWidthChanged) + Q_PROPERTY(int preferredHeight READ preferredHeight NOTIFY preferredHeightChanged) + + Q_PROPERTY(int maximumWidth READ maximumWidth NOTIFY maximumWidthChanged) + Q_PROPERTY(int maximumHeight READ maximumHeight NOTIFY maximumHeightChanged) + + Q_PROPERTY(ItemStatus status READ status WRITE setStatus NOTIFY statusChanged) + Q_ENUMS(ItemStatus) + +public: + enum ItemStatus { + UnknownStatus = 0, /**< The status is unknown **/ + PassiveStatus = 1, /**< The Item is passive **/ + ActiveStatus = 2, /**< The Item is active **/ + NeedsAttentionStatus = 3, /**< The Item needs attention **/ + AcceptingInputStatus = 4 /**< The Item is accepting input **/ + }; + + AppletContainer(QDeclarativeItem *parent = 0); + ~AppletContainer(); + + QGraphicsWidget *applet() const; + void setApplet(QGraphicsWidget *applet); + + int minimumWidth() const; + int minimumHeight() const; + + int preferredWidth() const; + int preferredHeight() const; + + int maximumWidth() const; + int maximumHeight() const; + + void setStatus(const ItemStatus status); + ItemStatus status() const; + +Q_SIGNALS: + void appletChanged(QGraphicsWidget *applet); + + void minimumWidthChanged(int); + void minimumHeightChanged(int); + + void preferredWidthChanged(int); + void preferredHeightChanged(int); + + void maximumWidthChanged(int); + void maximumHeightChanged(int); + + void statusChanged(); + + +protected Q_SLOTS: + void sizeHintChanged(Qt::SizeHint which); + void afterWidthChanged(); + void afterHeightChanged(); + +private: + QWeakPointer m_applet; +}; + +#endif diff --git a/scriptengines/javascript/plasmoid/appletinterface.cpp b/scriptengines/javascript/plasmoid/appletinterface.cpp index 46a644a6e..1f685ffb9 100644 --- a/scriptengines/javascript/plasmoid/appletinterface.cpp +++ b/scriptengines/javascript/plasmoid/appletinterface.cpp @@ -21,6 +21,8 @@ #include "appletinterface.h" +#include "../declarative/appletcontainer.h" + #include #include #include @@ -609,6 +611,9 @@ ContainmentInterface::ContainmentInterface(AbstractJsAppletScript *parent) connect(containment()->corona(), SIGNAL(availableScreenRegionChanged()), this, SIGNAL(availableScreenRegionChanged())); } + + qmlRegisterType("org.kde.plasma.containments", 0, 1, "AppletContainer"); + } QScriptValue ContainmentInterface::applets()