2007-06-01 00:27:33 +02:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2007 Matt Broadstone <mbroadst@gmail.com>
|
|
|
|
* Copyright 2007 Aaron Seigo <aseigo@kde.org>
|
|
|
|
* Copyright 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
|
2007-09-14 21:06:18 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
2007-06-01 00:27:33 +02:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
#include "containment.h"
|
2007-06-01 00:27:33 +02:00
|
|
|
#include "dataengine.h"
|
2007-06-10 08:24:42 +02:00
|
|
|
#include "phase.h"
|
2007-09-13 16:59:29 +02:00
|
|
|
#include "widgets/freelayout.h"
|
2007-09-01 14:34:22 +02:00
|
|
|
#include "widgets/boxlayout.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),
|
2007-07-17 23:13:41 +02:00
|
|
|
mimetype("text/x-plasmoidservicename")
|
2007-06-01 00:27:33 +02:00
|
|
|
{
|
|
|
|
}
|
2007-06-07 22:57:18 +02:00
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-06-04 02:25:44 +02:00
|
|
|
bool immutable;
|
2007-07-17 23:13:41 +02:00
|
|
|
QString mimetype;
|
2007-09-18 23:25:15 +02:00
|
|
|
QList<Containment*> containments;
|
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-09-18 23:25:15 +02:00
|
|
|
//d->init(this);
|
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-09-18 23:25:15 +02:00
|
|
|
//d->init(this);
|
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-09-18 23:25:15 +02:00
|
|
|
//d->init(this);
|
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
|
|
|
}
|
|
|
|
|
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-09-18 23:25:15 +02:00
|
|
|
KConfig cg(config);
|
|
|
|
foreach (const QString& group, cg.groupList()) {
|
|
|
|
cg.deleteGroup(group);
|
2007-08-06 00:04:26 +02:00
|
|
|
}
|
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
QStringList containmentIds;
|
|
|
|
foreach (const Containment *containment, d->containments) {
|
|
|
|
QString cid = QString::number(containment->id());
|
|
|
|
KConfigGroup containmentConfig(&cg, cid.append("-containment"));
|
|
|
|
containment->saveConstraints(&containmentConfig);
|
|
|
|
containment->save(&containmentConfig);
|
|
|
|
foreach (const Applet* applet, containment->applets()) {
|
|
|
|
KConfigGroup appletConfig(&cg, QString::number(applet->id()).append("-applet"));
|
|
|
|
applet->save(&appletConfig);
|
|
|
|
}
|
2007-08-05 15:03:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::saveApplets() const
|
|
|
|
{
|
|
|
|
saveApplets("plasma-appletsrc");
|
|
|
|
}
|
|
|
|
|
2007-10-08 04:22:29 +02:00
|
|
|
void Corona::loadApplets(const QString& configname)
|
2007-08-05 15:03:54 +02:00
|
|
|
{
|
2007-08-08 19:32:32 +02:00
|
|
|
clearApplets();
|
2007-08-29 04:33:22 +02:00
|
|
|
|
2007-10-08 04:22:29 +02:00
|
|
|
KConfig config(configname, KConfig::SimpleConfig);
|
2007-09-18 23:25:15 +02:00
|
|
|
|
|
|
|
QList<KConfigGroup> applets;
|
|
|
|
QHash<int, Containment*> containments;
|
2007-10-08 04:22:29 +02:00
|
|
|
foreach (const QString& group, config.groupList()) {
|
|
|
|
KConfigGroup appletConfig(&config, group);
|
2007-09-18 23:25:15 +02:00
|
|
|
if (group.contains("containment")) {
|
|
|
|
int cid = group.left(group.indexOf('-')).toUInt();
|
|
|
|
Containment *c = addContainment(appletConfig.readEntry("plugin", QString()), QVariantList(),
|
|
|
|
cid, true);
|
|
|
|
if (c) {
|
|
|
|
containments.insert(c->id(), c);
|
|
|
|
c->initConstraints(&appletConfig);
|
2007-09-30 20:06:08 +02:00
|
|
|
kDebug() << "Containment" << c->id() << "geometry is" << c->geometry();
|
2007-09-18 23:25:15 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// it's an applet, let's grab the containment association
|
2007-09-30 20:06:08 +02:00
|
|
|
kDebug() << "insert multi" << group;
|
2007-09-18 23:25:15 +02:00
|
|
|
applets.append(appletConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//int maxContainment = containments.size();
|
|
|
|
//kDebug() << "number of applets?" << applets.count();
|
|
|
|
foreach (KConfigGroup cg, applets) {
|
|
|
|
int cid = cg.readEntry("containment", 0);
|
2007-10-08 04:22:29 +02:00
|
|
|
kDebug() << "trying to load applet " << cg.name() << " in containment " << cid;
|
2007-09-18 23:25:15 +02:00
|
|
|
|
|
|
|
Containment* c = containments.value(cid, 0);
|
|
|
|
|
|
|
|
if (!c) {
|
2007-10-08 04:22:29 +02:00
|
|
|
kDebug() << "couldn't find containment " << cid << ", skipping this applet";
|
2007-09-18 23:25:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-10-09 18:32:34 +02:00
|
|
|
kDebug() << "creating applet " << cg.name() << "in containment" << cid;
|
2007-10-06 00:21:25 +02:00
|
|
|
int appId = cg.name().left(cg.name().indexOf('-')).toUInt();
|
2007-09-18 23:25:15 +02:00
|
|
|
c->addApplet(cg.readEntry("plugin", QString()), QVariantList(),
|
2007-10-11 21:16:01 +02:00
|
|
|
appId, cg.readEntry("geometry", QRectF()), true);
|
2007-09-13 19:03:13 +02:00
|
|
|
}
|
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
foreach (Containment* c, containments) {
|
|
|
|
QString cid = QString::number(c->id());
|
2007-10-08 04:22:29 +02:00
|
|
|
KConfigGroup containmentConfig(&config, cid.append("-containment"));
|
2007-09-18 23:25:15 +02:00
|
|
|
c->setImmutable(containmentConfig.isImmutable());
|
2007-08-05 15:03:54 +02:00
|
|
|
}
|
2007-09-18 23:25:15 +02:00
|
|
|
|
|
|
|
if (d->containments.count() < 1) {
|
|
|
|
loadDefaultSetup();
|
2007-10-09 18:32:34 +02:00
|
|
|
} else {
|
|
|
|
foreach (Containment* containment, d->containments) {
|
|
|
|
containment->init();
|
2007-10-12 01:18:33 +02:00
|
|
|
addItem(containment);
|
2007-09-18 23:25:15 +02:00
|
|
|
|
2007-10-09 18:32:34 +02:00
|
|
|
foreach(Applet* applet, containment->applets()) {
|
|
|
|
applet->init();
|
|
|
|
}
|
2007-09-18 23:25:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-08 04:22:29 +02:00
|
|
|
setImmutable(config.isImmutable());
|
2007-08-05 15:03:54 +02:00
|
|
|
}
|
|
|
|
|
2007-08-06 00:44:07 +02:00
|
|
|
void Corona::loadApplets()
|
|
|
|
{
|
|
|
|
loadApplets("plasma-appletsrc");
|
|
|
|
}
|
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
void Corona::loadDefaultSetup()
|
2007-08-08 19:32:32 +02:00
|
|
|
{
|
2007-09-18 23:25:15 +02:00
|
|
|
//FIXME: implement support for system-wide defaults
|
|
|
|
QDesktopWidget desktop;
|
|
|
|
int numScreens = desktop.numScreens();
|
|
|
|
// create a containment for each screen
|
|
|
|
for (int i = 0; i < numScreens; ++i) {
|
2007-10-03 17:45:31 +02:00
|
|
|
Containment* c = addContainment("desktop");
|
2007-09-18 23:25:15 +02:00
|
|
|
c->setScreen(i);
|
|
|
|
c->setFormFactor(Plasma::Planar);
|
|
|
|
}
|
2007-09-30 20:06:08 +02:00
|
|
|
|
|
|
|
// make a panel at the bottom
|
|
|
|
Containment* panel = addContainment("panel", (QVariantList() << (int)Plasma::BottomEdge));
|
|
|
|
|
|
|
|
// some default applets to get a usable UI
|
|
|
|
QList<Plasma::Applet*> applets;
|
|
|
|
Plasma::Applet *tasksApplet = panel->addApplet("tasks");
|
|
|
|
Plasma::Applet *systemTrayApplet = panel->addApplet("systemtray");
|
|
|
|
Plasma::Applet *clockApplet = panel->addApplet("digital-clock");
|
|
|
|
|
|
|
|
applets << tasksApplet << systemTrayApplet << clockApplet;
|
2007-08-08 19:32:32 +02:00
|
|
|
}
|
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
Containment* Corona::containmentForScreen(int screen) const
|
2007-08-05 15:03:54 +02:00
|
|
|
{
|
2007-09-18 23:25:15 +02:00
|
|
|
foreach (Containment* containment, d->containments) {
|
|
|
|
if (containment->screen() == screen) {
|
|
|
|
return containment;
|
|
|
|
}
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
2007-06-20 10:11:59 +02:00
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2007-09-30 20:06:08 +02:00
|
|
|
QList<Containment*> Corona::containments() const
|
|
|
|
{
|
|
|
|
return d->containments;
|
|
|
|
}
|
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
void Corona::clearApplets()
|
|
|
|
{
|
|
|
|
foreach (Containment* containment, d->containments) {
|
|
|
|
containment->clearApplets();
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
2007-09-18 23:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Containment* Corona::addContainment(const QString& name, const QVariantList& args, uint id, bool delayedInit)
|
|
|
|
{
|
2007-10-07 23:53:47 +02:00
|
|
|
QString pluginName = name;
|
2007-09-18 23:25:15 +02:00
|
|
|
Containment* containment = 0;
|
|
|
|
Applet* applet = 0;
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2007-09-30 20:06:08 +02:00
|
|
|
kDebug() << "Loading" << name << args << id;
|
|
|
|
|
2007-10-07 23:53:47 +02:00
|
|
|
if (pluginName.isEmpty()) {
|
|
|
|
// default to the desktop containment
|
|
|
|
pluginName = "desktop";
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
|
|
|
|
2007-10-07 23:53:47 +02:00
|
|
|
applet = Applet::loadApplet(pluginName, id, args);
|
|
|
|
containment = dynamic_cast<Containment*>(applet);
|
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
if (!containment) {
|
2007-10-08 04:22:29 +02:00
|
|
|
kDebug() << "loading failed.";
|
2007-09-18 23:25:15 +02:00
|
|
|
delete applet; // in case we got a non-Containment from Applet::loadApplet
|
|
|
|
containment = new Containment;
|
|
|
|
containment->setFailedToLaunch(false); // we want to provide something and don't care about the failure to launch
|
2007-08-05 15:03:54 +02:00
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
if (!delayedInit) {
|
|
|
|
containment->init();
|
2007-10-12 01:18:33 +02:00
|
|
|
addItem(containment);
|
2007-09-13 19:03:13 +02:00
|
|
|
}
|
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
d->containments.append(containment);
|
|
|
|
connect(containment, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(containmentDestroyed(QObject*)));
|
|
|
|
|
|
|
|
return containment;
|
|
|
|
}
|
|
|
|
|
|
|
|
Applet* Corona::addApplet(const QString& name, const QVariantList& args, uint id, const QRectF& geometry)
|
|
|
|
{
|
|
|
|
if (d->containments.size() < 1) {
|
|
|
|
kDebug() << "No containments to add an applet to!" << endl;
|
|
|
|
//FIXME create a containment if one doesn't exist ... ?
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return d->containments[0]->addApplet(name, args, id, geometry);
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Corona::dragEnterEvent( QGraphicsSceneDragDropEvent *event)
|
|
|
|
{
|
2007-09-18 23:25:15 +02:00
|
|
|
// kDebug() << "Corona::dragEnterEvent(QGraphicsSceneDragDropEvent* event)";
|
2007-08-06 00:04:26 +02:00
|
|
|
if (event->mimeData()->hasFormat(d->mimetype) ||
|
2007-06-14 00:11:49 +02:00
|
|
|
KUrl::List::canDecode(event->mimeData())) {
|
2007-06-01 00:27:33 +02:00
|
|
|
event->acceptProposedAction();
|
2007-10-03 17:45:31 +02:00
|
|
|
//TODO Create the applet, move to mouse position then send the
|
2007-06-01 00:27:33 +02:00
|
|
|
// 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
|
2007-08-16 07:32:54 +02:00
|
|
|
|
|
|
|
QGraphicsScene::dragLeaveEvent(event);
|
2007-06-01 00:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-09-18 23:25:15 +02:00
|
|
|
void Corona::containmentDestroyed(QObject* obj)
|
2007-06-02 09:51:11 +02:00
|
|
|
{
|
2007-09-18 23:25:15 +02:00
|
|
|
// we do a static_cast here since it really isn't an Containment by this
|
2007-06-02 09:51:11 +02:00
|
|
|
// 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.
|
2007-09-18 23:25:15 +02:00
|
|
|
Containment* containment = static_cast<Plasma::Containment*>(obj);
|
|
|
|
int index = d->containments.indexOf(containment);
|
2007-06-02 09:51:11 +02:00
|
|
|
|
|
|
|
if (index > -1) {
|
2007-09-18 23:25:15 +02:00
|
|
|
d->containments.removeAt(index);
|
2007-06-02 09:51:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()) {
|
2007-09-18 23:25:15 +02:00
|
|
|
item->setFlag(QGraphicsItem::ItemIsMovable, immutable);
|
2007-06-04 02:25:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-01 00:27:33 +02:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "corona.moc"
|
|
|
|
|