Add ResourceInstance from mobilecomponents

This commit is contained in:
Sebastian Kügler 2012-03-15 22:04:34 +01:00
parent ac99feb490
commit 3f241aace9
4 changed files with 279 additions and 1 deletions

View File

@ -1,7 +1,12 @@
project(plasmaextracomponents) project(plasmaextracomponents)
find_package(KActivities REQUIRED)
#include(KDE4Defaults)
set(plasmaextracomponents_SRCS set(plasmaextracomponents_SRCS
appbackgroundprovider.cpp appbackgroundprovider.cpp
resourceinstance.cpp
plasmaextracomponentsplugin.cpp plasmaextracomponentsplugin.cpp
) )
@ -15,7 +20,10 @@ qt4_automoc(${plasmaextracomponents_SRCS})
add_library(plasmaextracomponentsplugin SHARED ${plasmaextracomponents_SRCS}) add_library(plasmaextracomponentsplugin SHARED ${plasmaextracomponents_SRCS})
target_link_libraries(plasmaextracomponentsplugin ${QT_QTCORE_LIBRARY} ${QT_QTDECLARATIVE_LIBRARY} ${QT_QTGUI_LIBRARY} ${KDE4_PLASMA_LIBS}) target_link_libraries(plasmaextracomponentsplugin ${QT_QTCORE_LIBRARY} ${QT_QTDECLARATIVE_LIBRARY} ${QT_QTGUI_LIBRARY} ${KDE4_PLASMA_LIBS} ${KACTIVITIES_LIBRARY} )
#target_link_libraries(plasmaextracomponentsplugin ${QT_QTCORE_LIBRARY} ${QT_QTDBUS_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTSCRIPT_LIBRARY} ${QT_QTDECLARATIVE_LIBRARY} ${KDE4_KDECORE_LIBRARY} ${KDE4_PLASMA_LIBS} ${KACTIVITIES_LIBRARY})
install(TARGETS plasmaextracomponentsplugin DESTINATION ${IMPORTS_INSTALL_DIR}/org/kde/plasma/extras) install(TARGETS plasmaextracomponentsplugin DESTINATION ${IMPORTS_INSTALL_DIR}/org/kde/plasma/extras)

View File

@ -18,7 +18,9 @@
*/ */
#include "plasmaextracomponentsplugin.h" #include "plasmaextracomponentsplugin.h"
#include "appbackgroundprovider_p.h" #include "appbackgroundprovider_p.h"
#include "resourceinstance.h"
#include <QtDeclarative/qdeclarative.h> #include <QtDeclarative/qdeclarative.h>
#include <QtDeclarative/QDeclarativeEngine> #include <QtDeclarative/QDeclarativeEngine>
@ -45,6 +47,9 @@ void PlasmaExtraComponentsPlugin::registerTypes(const char *uri)
} }
*/ */
// Register additional types here... // Register additional types here...
qmlRegisterType<ResourceInstance>(uri, 0, 1, "ResourceInstance");
} }

View File

@ -0,0 +1,171 @@
/***************************************************************************
* Copyright 2011 Marco Martin <mart@kde.org> *
* *
* This program 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 program 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 program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#include "resourceinstance.h"
#include <QApplication>
#include <QGraphicsObject>
#include <QGraphicsView>
#include <QTimer>
#include <KDE/KActivities/ResourceInstance>
#include <KDebug>
ResourceInstance::ResourceInstance(QDeclarativeItem *parent)
: QDeclarativeItem(parent)
{
m_syncTimer = new QTimer(this);
m_syncTimer->setSingleShot(true);
connect(m_syncTimer, SIGNAL(timeout()), this, SLOT(syncWid()));
}
ResourceInstance::~ResourceInstance()
{
}
QGraphicsView *ResourceInstance::view() const
{
// It's assumed that we won't be visible on more than one view here.
// Anything that actually needs view() should only really care about
// one of them anyway though.
if (!scene()) {
return 0;
}
QGraphicsView *found = 0;
QGraphicsView *possibleFind = 0;
//kDebug() << "looking through" << scene()->views().count() << "views";
foreach (QGraphicsView *view, scene()->views()) {
//kDebug() << " checking" << view << view->sceneRect()
// << "against" << sceneBoundingRect() << scenePos();
if (view->sceneRect().intersects(sceneBoundingRect()) ||
view->sceneRect().contains(scenePos())) {
//kDebug() << " found something!" << view->isActiveWindow();
if (view->isActiveWindow()) {
found = view;
} else {
possibleFind = view;
}
}
}
return found ? found : possibleFind;
}
void ResourceInstance::syncWid()
{
QGraphicsView *v = view();
if (!v) {
return;
}
WId wid = v->topLevelWidget()->effectiveWinId();
if (!m_resourceInstance || m_resourceInstance->winId() != wid) {
delete m_resourceInstance;
kDebug() << "Creating a new instance of the resource" << m_uri << "window id" << wid;
m_resourceInstance = new KActivities::ResourceInstance(wid, m_uri, m_mimetype, m_title);
} else {
if (m_uri.scheme().startsWith("http") && !m_uri.hasQuery() && m_uri.path().endsWith('/')) {
const QString & oldPath = m_uri.path();
m_uri.setPath(oldPath.left(oldPath.length() - 1));
kDebug() << "Old and new path" << oldPath << m_uri;
} else {
m_resourceInstance->setUri(m_uri);
}
kDebug() << "Setting" << m_uri << m_mimetype << "to window" << wid;
m_resourceInstance->setMimetype(m_mimetype);
m_resourceInstance->setTitle(m_title);
}
}
QUrl ResourceInstance::uri() const
{
return m_uri;
}
void ResourceInstance::setUri(const QUrl &uri)
{
if (m_uri == uri) {
return;
}
m_uri = uri;
m_syncTimer->start(100);
}
QString ResourceInstance::mimetype() const
{
return m_mimetype;
}
void ResourceInstance::setMimetype(const QString &mimetype)
{
if (m_mimetype == mimetype) {
return;
}
m_mimetype = mimetype;
m_syncTimer->start(100);
}
QString ResourceInstance::title() const
{
return m_title;
}
void ResourceInstance::setTitle(const QString &title)
{
if (m_title == title) {
return;
}
m_title = title;
m_syncTimer->start(100);
}
void ResourceInstance::notifyModified()
{
//ensure the resource instance exists
syncWid();
m_resourceInstance->notifyModified();
}
void ResourceInstance::notifyFocusedIn()
{
//ensure the resource instance exists
syncWid();
m_resourceInstance->notifyFocusedIn();
}
void ResourceInstance::notifyFocusedOut()
{
//ensure the resource instance exists
syncWid();
m_resourceInstance->notifyFocusedOut();
}
#include "resourceinstance.moc"

View File

@ -0,0 +1,94 @@
/***************************************************************************
* Copyright 2011 Marco Martin <mart@kde.org> *
* *
* This program 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 program 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 program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#ifndef RESOURCEINSTANCE_H
#define RESOURCEINSTANCE_H
#include <QDeclarativeItem>
#include <QUrl>
namespace KActivities {
class ResourceInstance;
}
class QTimer;
class QGraphicsView;
class ResourceInstance : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY(QUrl uri READ uri WRITE setUri NOTIFY uriChanged)
Q_PROPERTY(QString mimetype READ mimetype WRITE setMimetype NOTIFY mimetypeChanged)
Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
//Q_PROPERTY(OpenReason openReason READ openReason)
public:
ResourceInstance(QDeclarativeItem *parent = 0);
~ResourceInstance();
QUrl uri() const;
void setUri(const QUrl &uri);
QString mimetype() const;
void setMimetype(const QString &mimetype);
QString title() const;
void setTitle(const QString &title);
protected:
QGraphicsView *view() const;
protected Q_SLOTS:
void syncWid();
Q_SIGNALS:
void uriChanged();
void mimetypeChanged();
void titleChanged();
public Q_SLOTS:
/**
* Call this method to notify the system that you modified
* (the contents of) the resource
*/
void notifyModified();
/**
* Call this method to notify the system that the resource
* has the focus in your application
* @note You only need to call this in MDI applications
*/
void notifyFocusedIn();
/**
* Call this method to notify the system that the resource
* lost the focus in your application
* @note You only need to call this in MDI applications
*/
void notifyFocusedOut();
private:
KActivities::ResourceInstance *m_resourceInstance;
QUrl m_uri;
QString m_mimetype;
QString m_title;
QTimer *m_syncTimer;
};
#endif