2007-06-01 00:27:33 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Matt Broadstone <mbroadst@gmail.com>
|
|
|
|
* Copyright (C) 2007 Aaron Seigo <aseigo@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 version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* 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 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 "corona.h"
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
#include <QGraphicsSceneDragDropEvent>
|
|
|
|
#include <QMimeData>
|
2007-06-06 05:25:04 +02:00
|
|
|
#include <QUrl>
|
2007-06-04 02:25:44 +02:00
|
|
|
#include <QGraphicsView>
|
2007-06-20 10:11:59 +02:00
|
|
|
#include <QStringList>
|
2007-06-01 00:27:33 +02:00
|
|
|
|
2007-06-21 20:25:09 +02:00
|
|
|
#include <KAuthorized>
|
|
|
|
#include <KDebug>
|
2007-06-01 00:27:33 +02:00
|
|
|
#include <KLocale>
|
|
|
|
#include <KMenu>
|
2007-06-21 20:25:09 +02:00
|
|
|
#include <KMimeType>
|
2007-06-03 21:59:24 +02:00
|
|
|
#include <KRun>
|
2007-06-01 00:27:33 +02:00
|
|
|
#include <KWindowSystem>
|
|
|
|
|
|
|
|
#include "applet.h"
|
|
|
|
#include "dataengine.h"
|
2007-06-10 08:24:42 +02:00
|
|
|
#include "karambamanager.h"
|
|
|
|
#include "phase.h"
|
2007-06-01 00:27:33 +02:00
|
|
|
#include "widgets/vboxlayout.h"
|
2007-06-06 05:25:04 +02:00
|
|
|
#include "widgets/icon.h"
|
2007-06-01 00:27:33 +02:00
|
|
|
|
|
|
|
using namespace Plasma;
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class Corona::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
: formFactor(Planar),
|
|
|
|
location(Floating),
|
|
|
|
layout(0),
|
2007-06-17 22:05:43 +02:00
|
|
|
engineExplorerAction(0)
|
2007-06-01 00:27:33 +02:00
|
|
|
{
|
|
|
|
}
|
2007-06-07 22:57:18 +02:00
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
delete layout;
|
2007-06-08 07:25:49 +02:00
|
|
|
qDeleteAll(applets);
|
2007-06-07 22:57:18 +02:00
|
|
|
}
|
|
|
|
|
2007-06-04 02:25:44 +02:00
|
|
|
bool immutable;
|
2007-06-01 00:27:33 +02:00
|
|
|
Applet::List applets;
|
|
|
|
FormFactor formFactor;
|
|
|
|
Location location;
|
|
|
|
Layout* layout;
|
|
|
|
QAction *engineExplorerAction;
|
|
|
|
};
|
|
|
|
|
|
|
|
Corona::Corona(QObject * parent)
|
|
|
|
: QGraphicsScene(parent),
|
|
|
|
d(new Private)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Corona::Corona(const QRectF & sceneRect, QObject * parent )
|
|
|
|
: QGraphicsScene(sceneRect, parent),
|
|
|
|
d(new Private)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Corona::Corona(qreal x, qreal y, qreal width, qreal height, QObject * parent)
|
|
|
|
: QGraphicsScene(x, y, width, height, parent),
|
|
|
|
d(new Private)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::init()
|
|
|
|
{
|
|
|
|
/* setBackgroundMode(Qt::NoBackground);*/
|
|
|
|
|
|
|
|
/* QPalette pal = palette();
|
|
|
|
pal.setBrush(QPalette::Base, Qt::transparent);
|
|
|
|
setPalette(pal);*/
|
|
|
|
//setViewport(new QGLWidget ( QGLFormat(QGL::StencilBuffer | QGL::AlphaChannel) ));
|
|
|
|
|
|
|
|
/*
|
|
|
|
KPluginInfo::List applets = Applet::knownApplets();
|
|
|
|
kDebug() << "======= Applets =========" << endl;
|
|
|
|
foreach (KPluginInfo* info, applets) {
|
|
|
|
kDebug() << info->pluginName() << ": " << info->name() << endl;
|
|
|
|
}
|
|
|
|
kDebug() << "=========================" << endl;
|
|
|
|
*/
|
|
|
|
|
|
|
|
// connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(displayContextMenu(QPoint)));
|
|
|
|
d->engineExplorerAction = new QAction(i18n("Engine Explorer"), this);
|
2007-06-02 09:51:11 +02:00
|
|
|
connect(d->engineExplorerAction, SIGNAL(triggered(bool)), this, SLOT(launchExplorer()));
|
2007-06-04 02:25:44 +02:00
|
|
|
d->immutable = false;
|
2007-06-01 00:27:33 +02:00
|
|
|
// setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
}
|
|
|
|
|
|
|
|
Corona::~Corona()
|
|
|
|
{
|
2007-06-07 22:57:18 +02:00
|
|
|
delete d;
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Location Corona::location() const
|
|
|
|
{
|
|
|
|
return d->location;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::setLocation(Location location)
|
|
|
|
{
|
2007-06-02 00:59:58 +02:00
|
|
|
if (d->location == location) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-01 00:27:33 +02:00
|
|
|
d->location = location;
|
2007-06-02 00:59:58 +02:00
|
|
|
|
|
|
|
foreach (Applet* applet, d->applets) {
|
|
|
|
applet->constraintsUpdated();
|
|
|
|
}
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FormFactor Corona::formFactor() const
|
|
|
|
{
|
|
|
|
return d->formFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::setFormFactor(FormFactor formFactor)
|
|
|
|
{
|
|
|
|
if (d->formFactor == formFactor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-02 00:59:58 +02:00
|
|
|
//kDebug() << "switching FF to " << formFactor << endl;
|
2007-06-01 00:27:33 +02:00
|
|
|
d->formFactor = formFactor;
|
|
|
|
delete d->layout;
|
|
|
|
d->layout = 0;
|
|
|
|
|
|
|
|
switch (d->formFactor) {
|
|
|
|
case Planar:
|
|
|
|
break;
|
|
|
|
case Horizontal:
|
|
|
|
//d->layout = new HBoxLayout;
|
|
|
|
break;
|
|
|
|
case Vertical:
|
|
|
|
d->layout = new VBoxLayout;
|
|
|
|
break;
|
|
|
|
case MediaCenter:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
kDebug() << "This can't be happening!" << endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (Applet* applet, d->applets) {
|
|
|
|
applet->constraintsUpdated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-02 06:47:09 +02:00
|
|
|
QRectF Corona::maxSizeHint() const
|
|
|
|
{
|
|
|
|
//FIXME: this is a bit of a naive implementation, do you think? =)
|
|
|
|
// we should factor in how much space we actually have left!
|
|
|
|
return sceneRect();
|
|
|
|
}
|
2007-06-01 00:27:33 +02:00
|
|
|
|
2007-06-20 10:11:59 +02:00
|
|
|
Applet* Corona::addPlasmoid(const QString& name, const QStringList& args)
|
2007-06-01 00:27:33 +02:00
|
|
|
{
|
2007-06-20 10:11:59 +02:00
|
|
|
Applet* applet = Applet::loadApplet(name, 0, args);
|
2007-06-01 00:27:33 +02:00
|
|
|
if (applet) {
|
|
|
|
addItem(applet);
|
2007-06-01 00:40:38 +02:00
|
|
|
//applet->constraintsUpdated();
|
2007-06-01 00:27:33 +02:00
|
|
|
d->applets << applet;
|
2007-06-02 09:51:11 +02:00
|
|
|
connect(applet, SIGNAL(destroyed(QObject*)),
|
2007-06-02 09:30:16 +02:00
|
|
|
this, SLOT(appletDestroyed(QObject*)));
|
2007-06-17 22:05:43 +02:00
|
|
|
Phase::self()->animate(applet, Phase::Appear);
|
2007-06-01 00:27:33 +02:00
|
|
|
} else {
|
|
|
|
kDebug() << "Plasmoid " << name << " could not be loaded." << endl;
|
|
|
|
}
|
2007-06-20 10:11:59 +02:00
|
|
|
|
|
|
|
return applet;
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
2007-06-01 23:32:03 +02:00
|
|
|
void Corona::addKaramba(const KUrl& path)
|
|
|
|
{
|
|
|
|
QGraphicsItemGroup* karamba = KarambaManager::loadKaramba(path, this);
|
|
|
|
if (karamba) {
|
|
|
|
addItem(karamba);
|
2007-06-17 22:05:43 +02:00
|
|
|
Phase::self()->animate(karamba, Phase::Appear);
|
2007-06-01 23:32:03 +02:00
|
|
|
} else {
|
|
|
|
kDebug() << "Karamba " << path << " could not be loaded." << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-01 00:27:33 +02:00
|
|
|
void Corona::dragEnterEvent( QGraphicsSceneDragDropEvent *event)
|
|
|
|
{
|
|
|
|
kDebug() << "Corona::dragEnterEvent(QGraphicsSceneDragDropEvent* event)" << endl;
|
2007-06-14 00:11:49 +02:00
|
|
|
if (event->mimeData()->hasFormat("text/x-plasmoidservicename") ||
|
|
|
|
KUrl::List::canDecode(event->mimeData())) {
|
2007-06-01 00:27:33 +02:00
|
|
|
event->acceptProposedAction();
|
|
|
|
//TODO Create the applet, move to mouse position then send the
|
|
|
|
// following event to lock it to the mouse
|
|
|
|
//QMouseEvent event(QEvent::MouseButtonPress, event->pos(), Qt::LeftButton, event->mouseButtons(), 0);
|
|
|
|
//QApplication::sendEvent(this, &event);
|
|
|
|
}
|
|
|
|
//TODO Allow dragging an applet from another Corona into this one while
|
|
|
|
// keeping its settings etc.
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
kDebug() << "Corona::dragLeaveEvent(QGraphicsSceneDragDropEvent* event)" << endl;
|
|
|
|
//TODO If an established Applet is dragged out of the Corona, remove it and
|
|
|
|
// create a QDrag type thing to keep the Applet's settings
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event);
|
|
|
|
kDebug() << "Corona::dragMoveEvent(QDragMoveEvent* event)" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::dropEvent(QGraphicsSceneDragDropEvent *event)
|
|
|
|
{
|
|
|
|
kDebug() << "Corona::dropEvent(QDropEvent* event)" << endl;
|
|
|
|
if (event->mimeData()->hasFormat("text/x-plasmoidservicename")) {
|
|
|
|
//TODO This will pretty much move into dragEnterEvent()
|
|
|
|
QString plasmoidName;
|
|
|
|
plasmoidName = event->mimeData()->data("text/x-plasmoidservicename");
|
|
|
|
addPlasmoid(plasmoidName);
|
|
|
|
d->applets.last()->setPos(event->pos());
|
|
|
|
|
|
|
|
event->acceptProposedAction();
|
2007-06-06 07:12:51 +02:00
|
|
|
} else if (KUrl::List::canDecode(event->mimeData())) {
|
|
|
|
KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
|
|
|
|
foreach (const KUrl& url, urls) {
|
2007-06-20 10:11:59 +02:00
|
|
|
QStringList args;
|
|
|
|
args << url.url();
|
|
|
|
Applet* button = addPlasmoid("url", args);
|
|
|
|
if (button) {
|
|
|
|
//button->setSize(128,128);
|
|
|
|
button->setPos(event->scenePos() - QPoint(button->boundingRect().width()/2,
|
|
|
|
button->boundingRect().height()/2));
|
|
|
|
}
|
|
|
|
addItem(button);
|
2007-06-06 07:12:51 +02:00
|
|
|
}
|
2007-06-06 05:25:04 +02:00
|
|
|
event->acceptProposedAction();
|
2007-06-06 07:12:51 +02:00
|
|
|
}
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent)
|
|
|
|
{
|
2007-06-21 20:25:09 +02:00
|
|
|
if (!KAuthorized::authorizeKAction("desktop_contextmenu")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-01 00:27:33 +02:00
|
|
|
QPointF point = contextMenuEvent->scenePos();
|
|
|
|
/*
|
|
|
|
* example for displaying the SuperKaramba context menu
|
|
|
|
QGraphicsItem *item = itemAt(point);
|
|
|
|
if(item) {
|
|
|
|
QObject *object = dynamic_cast<QObject*>(item->parentItem());
|
|
|
|
if(object && object->objectName().startsWith("karamba")) {
|
|
|
|
QContextMenuEvent event(QContextMenuEvent::Mouse, point);
|
|
|
|
contextMenuEvent(&event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2007-06-03 21:59:24 +02:00
|
|
|
contextMenuEvent->accept();
|
2007-06-21 20:25:09 +02:00
|
|
|
QGraphicsItem* item = itemAt(point);
|
|
|
|
Applet* applet = 0;
|
|
|
|
|
|
|
|
while (item) {
|
|
|
|
applet = qgraphicsitem_cast<Applet*>(item);
|
|
|
|
if (applet) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
item = item->parentItem();
|
|
|
|
}
|
|
|
|
|
2007-06-01 00:27:33 +02:00
|
|
|
KMenu desktopMenu;
|
2007-06-21 20:25:09 +02:00
|
|
|
//kDebug() << "context menu event " << d->immutable << endl;
|
2007-06-07 21:40:16 +02:00
|
|
|
if (!applet) {
|
|
|
|
if (d->immutable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-17 00:35:31 +02:00
|
|
|
desktopMenu.addTitle("Plasma");
|
2007-06-01 00:27:33 +02:00
|
|
|
desktopMenu.addAction(d->engineExplorerAction);
|
2007-06-21 20:25:09 +02:00
|
|
|
} else if (applet->immutable()) {
|
|
|
|
return;
|
2007-06-01 00:27:33 +02:00
|
|
|
} else {
|
2007-06-05 21:31:48 +02:00
|
|
|
desktopMenu.addTitle(applet->name());
|
2007-06-01 00:27:33 +02:00
|
|
|
desktopMenu.addSeparator();
|
2007-06-07 21:40:16 +02:00
|
|
|
|
|
|
|
QAction* configureApplet = new QAction(i18n("Configure Plasmoid..."), this);
|
|
|
|
connect(configureApplet, SIGNAL(triggered(bool)),
|
|
|
|
applet, SLOT(configureDialog())); //This isn't implemented in Applet yet...
|
|
|
|
desktopMenu.addAction(configureApplet);
|
|
|
|
if (!d->immutable) {
|
|
|
|
QAction* closeApplet = new QAction(i18n("Close Plasmoid"), this);
|
|
|
|
connect(closeApplet, SIGNAL(triggered(bool)),
|
|
|
|
applet, SLOT(deleteLater()));
|
|
|
|
desktopMenu.addAction(closeApplet);
|
|
|
|
}
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
desktopMenu.exec(point.toPoint());
|
|
|
|
}
|
|
|
|
|
2007-06-02 09:51:11 +02:00
|
|
|
void Corona::launchExplorer()
|
2007-06-01 00:27:33 +02:00
|
|
|
{
|
2007-06-03 21:59:24 +02:00
|
|
|
KRun::run("plasmaengineexplorer", KUrl::List(), 0);
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
2007-06-02 09:51:11 +02:00
|
|
|
void Corona::appletDestroyed(QObject* object)
|
|
|
|
{
|
|
|
|
// we do a static_cast here since it really isn't an Applet by this
|
|
|
|
// point anymore since we are in the qobject dtor. we don't actually
|
|
|
|
// try and do anything with it, we just need the value of the pointer
|
|
|
|
// so this unsafe looking code is actually just fine.
|
|
|
|
Applet* applet = static_cast<Plasma::Applet*>(object);
|
|
|
|
int index = d->applets.indexOf(applet);
|
|
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
d->applets.removeAt(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-24 08:35:49 +02:00
|
|
|
bool Corona::immutable() const
|
2007-06-04 02:25:44 +02:00
|
|
|
{
|
|
|
|
return d->immutable;
|
|
|
|
}
|
|
|
|
|
2007-06-07 21:40:16 +02:00
|
|
|
void Corona::setImmutable(bool immutable)
|
2007-06-04 02:25:44 +02:00
|
|
|
{
|
2007-06-07 21:40:16 +02:00
|
|
|
if (d->immutable == immutable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->immutable = immutable;
|
|
|
|
foreach (QGraphicsItem* item, items()) {
|
|
|
|
QGraphicsItem::GraphicsItemFlags flags = item->flags();
|
|
|
|
if (immutable) {
|
|
|
|
flags ^= QGraphicsItem::ItemIsMovable;
|
|
|
|
} else {
|
|
|
|
flags |= QGraphicsItem::ItemIsMovable;
|
|
|
|
}
|
|
|
|
item->setFlags(flags);
|
2007-06-04 02:25:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-01 00:27:33 +02:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "corona.moc"
|
|
|
|
|