2007-06-01 00:27:33 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Matt Broadstone <mbroadst@gmail.com>
|
|
|
|
* Copyright (C) 2007 Aaron Seigo <aseigo@kde.org>
|
2007-07-31 12:52:05 +02:00
|
|
|
* Copyright (C) 2007 Riccardo Iaconelli <riccardo@kde.org>
|
2007-06-01 00:27:33 +02:00
|
|
|
*
|
|
|
|
* 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 <KDebug>
|
2007-06-01 00:27:33 +02:00
|
|
|
#include <KLocale>
|
2007-06-21 20:25:09 +02:00
|
|
|
#include <KMimeType>
|
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()
|
2007-07-17 19:55:05 +02:00
|
|
|
: immutable(false),
|
|
|
|
formFactor(Planar),
|
2007-06-01 00:27:33 +02:00
|
|
|
location(Floating),
|
2007-07-17 23:13:41 +02:00
|
|
|
layout(0),
|
|
|
|
mimetype("text/x-plasmoidservicename")
|
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;
|
2007-07-17 23:13:41 +02:00
|
|
|
QString mimetype;
|
2007-06-01 00:27:33 +02:00
|
|
|
};
|
|
|
|
|
2007-08-05 15:03:54 +02:00
|
|
|
Corona::Corona(QObject *parent)
|
2007-06-01 00:27:33 +02:00
|
|
|
: QGraphicsScene(parent),
|
|
|
|
d(new Private)
|
|
|
|
{
|
2007-08-05 15:03:54 +02:00
|
|
|
loadApplets("plasma-appletsrc");
|
2007-07-17 19:55:05 +02:00
|
|
|
//setViewport(new QGLWidget(QGLFormat(QGL::StencilBuffer | QGL::AlphaChannel)));
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Corona::Corona(const QRectF & sceneRect, QObject * parent )
|
|
|
|
: QGraphicsScene(sceneRect, parent),
|
|
|
|
d(new Private)
|
|
|
|
{
|
2007-08-05 15:03:54 +02:00
|
|
|
loadApplets("plasma-appletsrc");
|
2007-07-17 19:55:05 +02:00
|
|
|
//setViewport(new QGLWidget(QGLFormat(QGL::StencilBuffer | QGL::AlphaChannel)));
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Corona::Corona(qreal x, qreal y, qreal width, qreal height, QObject * parent)
|
|
|
|
: QGraphicsScene(x, y, width, height, parent),
|
|
|
|
d(new Private)
|
|
|
|
{
|
2007-08-05 15:03:54 +02:00
|
|
|
loadApplets("plasma-appletsrc");
|
2007-07-17 19:55:05 +02:00
|
|
|
//setViewport(new QGLWidget(QGLFormat(QGL::StencilBuffer | QGL::AlphaChannel)));
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2007-08-05 10:41:55 +02:00
|
|
|
applet->updateConstraints();
|
2007-06-02 00:59:58 +02:00
|
|
|
}
|
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-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "switching FF to " << formFactor;
|
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:
|
2007-08-02 15:12:59 +02:00
|
|
|
kDebug() << "This can't be happening!";
|
2007-06-01 00:27:33 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (Applet* applet, d->applets) {
|
2007-08-05 10:41:55 +02:00
|
|
|
applet->updateConstraints();
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-17 23:13:41 +02:00
|
|
|
void Corona::setAppletMimeType(const QString& type)
|
|
|
|
{
|
|
|
|
d->mimetype = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Corona::appletMimeType()
|
|
|
|
{
|
|
|
|
return d->mimetype;
|
|
|
|
}
|
|
|
|
|
2007-08-05 15:03:54 +02:00
|
|
|
void Corona::saveApplets(const QString &config) const
|
2007-06-01 00:27:33 +02:00
|
|
|
{
|
2007-08-05 15:03:54 +02:00
|
|
|
KConfig appletConfig(config);
|
|
|
|
foreach (Applet *applet, d->applets) {
|
|
|
|
KConfigGroup cg(&appletConfig, QString::number(applet->id()));
|
|
|
|
kDebug() << "saving applet " << applet->name();
|
|
|
|
cg.writeEntry("plugin", applet->pluginName());
|
|
|
|
cg.writeEntry("geometry", QRect(applet->pos().toPoint(), applet->boundingRect().size().toSize()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::saveApplets() const
|
|
|
|
{
|
|
|
|
saveApplets("plasma-appletsrc");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::loadApplets(const QString& config)
|
|
|
|
{
|
|
|
|
qDeleteAll(d->applets);
|
|
|
|
d->applets.clear();
|
|
|
|
|
|
|
|
KConfig appletConfig(config, KConfig::OnlyLocal);
|
|
|
|
foreach (const QString& group, appletConfig.groupList()) {
|
|
|
|
KConfigGroup cg(&appletConfig, group);
|
|
|
|
addApplet(cg.readEntry("plugin", QString()), QStringList(),
|
|
|
|
group.toUInt(), cg.readEntry("geometry", QRectF()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Applet* Corona::addApplet(const QString& name, const QStringList& args, uint id, const QRectF& geometry)
|
|
|
|
{
|
|
|
|
Applet* applet = Applet::loadApplet(name, id, args);
|
2007-07-12 20:37:13 +02:00
|
|
|
if (!applet) {
|
2007-08-02 15:12:59 +02:00
|
|
|
kDebug() << "Applet " << name << " could not be loaded.";
|
2007-07-12 20:37:13 +02:00
|
|
|
applet = new Applet;
|
|
|
|
applet->setFailedToLaunch(true);
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
2007-06-20 10:11:59 +02:00
|
|
|
|
2007-07-18 08:18:47 +02:00
|
|
|
qreal appWidth = applet->boundingRect().width();
|
|
|
|
qreal appHeight = applet->boundingRect().height();
|
2007-08-05 15:03:54 +02:00
|
|
|
if (geometry.isValid()) {
|
|
|
|
applet->setGeometry(geometry);
|
|
|
|
} else {
|
|
|
|
//TODO: Make sure new applets don't overlap with existing ones
|
|
|
|
// Center exactly:
|
|
|
|
applet->setPos((width() / 2) - (appWidth / 2),(height() / 2) - (appHeight / 2));
|
|
|
|
}
|
2007-07-12 20:37:13 +02:00
|
|
|
addItem(applet);
|
2007-08-05 10:41:55 +02:00
|
|
|
applet->updateConstraints();
|
|
|
|
|
2007-07-12 20:37:13 +02:00
|
|
|
//applet->constraintsUpdated();
|
|
|
|
d->applets << applet;
|
|
|
|
connect(applet, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(appletDestroyed(QObject*)));
|
|
|
|
Phase::self()->animateItem(applet, Phase::Appear);
|
|
|
|
|
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-25 08:02:33 +02:00
|
|
|
Phase::self()->animateItem(karamba, Phase::Appear);
|
2007-06-01 23:32:03 +02:00
|
|
|
} else {
|
2007-08-02 15:12:59 +02:00
|
|
|
kDebug() << "Karamba " << path << " could not be loaded.";
|
2007-06-01 23:32:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-01 00:27:33 +02:00
|
|
|
void Corona::dragEnterEvent( QGraphicsSceneDragDropEvent *event)
|
|
|
|
{
|
2007-08-02 15:12:59 +02:00
|
|
|
kDebug() << "Corona::dragEnterEvent(QGraphicsSceneDragDropEvent* event)";
|
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);
|
|
|
|
}
|
2007-08-05 10:41:55 +02:00
|
|
|
|
2007-08-01 09:09:27 +02:00
|
|
|
event->accept();
|
2007-06-01 00:27:33 +02:00
|
|
|
//TODO Allow dragging an applet from another Corona into this one while
|
|
|
|
// keeping its settings etc.
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
|
|
|
|
{
|
2007-08-03 08:59:24 +02:00
|
|
|
// kDebug() << "Corona::dragLeaveEvent(QGraphicsSceneDragDropEvent* event)";
|
2007-06-01 00:27:33 +02:00
|
|
|
//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)
|
|
|
|
{
|
2007-08-01 09:09:27 +02:00
|
|
|
QGraphicsScene::dragMoveEvent(event);
|
|
|
|
|
|
|
|
event->accept();
|
2007-08-03 08:59:24 +02:00
|
|
|
//kDebug() << "Corona::dragMoveEvent(QDragMoveEvent* event)";
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::dropEvent(QGraphicsSceneDragDropEvent *event)
|
|
|
|
{
|
2007-08-03 08:59:24 +02:00
|
|
|
//kDebug() << "Corona::dropEvent(QDropEvent* event)";
|
2007-06-01 00:27:33 +02:00
|
|
|
if (event->mimeData()->hasFormat("text/x-plasmoidservicename")) {
|
|
|
|
//TODO This will pretty much move into dragEnterEvent()
|
|
|
|
QString plasmoidName;
|
|
|
|
plasmoidName = event->mimeData()->data("text/x-plasmoidservicename");
|
2007-07-12 20:37:13 +02:00
|
|
|
addApplet(plasmoidName);
|
2007-07-26 01:58:03 +02:00
|
|
|
Applet *applet = d->applets.last();
|
|
|
|
// TODO: should we place it centered on the mouse cursor? If so, the
|
|
|
|
// default "zoom in" animation needs changing to zoom in from the same
|
|
|
|
// point, or it just looks odd
|
|
|
|
applet->setPos(event->scenePos()); // -
|
|
|
|
// QPoint(applet->boundingRect().width()/2,applet->boundingRect().height()/2));
|
2007-06-01 00:27:33 +02:00
|
|
|
|
|
|
|
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-07-31 12:52:05 +02:00
|
|
|
KMimeType::Ptr mime = KMimeType::findByUrl(url);
|
|
|
|
QString mimeName = mime->name();
|
2007-08-02 15:12:59 +02:00
|
|
|
// kDebug() << mimeName;
|
2007-07-31 12:52:05 +02:00
|
|
|
KPluginInfo::List appletList = Applet::knownAppletsForMimetype(mimeName);
|
|
|
|
|
|
|
|
|
|
|
|
//FIXME: we should probably show a dialog here to choose which plasmoid load.
|
|
|
|
addApplet(appletList.first().pluginName());
|
|
|
|
|
2007-06-20 10:11:59 +02:00
|
|
|
QStringList args;
|
|
|
|
args << url.url();
|
2007-07-12 20:37:13 +02:00
|
|
|
Applet* button = addApplet("url", args);
|
2007-06-20 10:11:59 +02:00
|
|
|
if (button) {
|
2007-07-31 12:52:05 +02:00
|
|
|
// button->setSize(128,128);
|
2007-06-20 10:11:59 +02:00
|
|
|
button->setPos(event->scenePos() - QPoint(button->boundingRect().width()/2,
|
|
|
|
button->boundingRect().height()/2));
|
|
|
|
}
|
|
|
|
addItem(button);
|
2007-08-05 10:41:55 +02:00
|
|
|
button->updateConstraints();
|
2007-06-06 07:12:51 +02:00
|
|
|
}
|
2007-06-06 05:25:04 +02:00
|
|
|
event->acceptProposedAction();
|
2007-08-05 10:41:55 +02:00
|
|
|
} else {
|
2007-08-01 09:09:27 +02:00
|
|
|
QGraphicsScene::dropEvent(event);
|
2007-08-05 10:41:55 +02:00
|
|
|
}
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
2007-07-17 19:55:05 +02:00
|
|
|
// void Corona::contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent)
|
|
|
|
// {
|
|
|
|
// }
|
2007-07-17 18:55:27 +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-07-23 02:24:36 +02:00
|
|
|
bool Corona::isImmutable() 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"
|
|
|
|
|