2007-09-18 23:16:17 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2007 by 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 as
|
|
|
|
* published by the Free Software Foundation; either version 2, 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 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 "containment.h"
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QGraphicsSceneContextMenuEvent>
|
2007-09-23 20:03:37 +02:00
|
|
|
#include <QMimeData>
|
2007-09-18 23:16:17 +02:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
|
2007-09-20 20:23:28 +02:00
|
|
|
#include <KApplication>
|
2007-09-18 23:16:17 +02:00
|
|
|
#include <KAuthorized>
|
2007-09-20 20:23:28 +02:00
|
|
|
#include <KIcon>
|
2007-09-18 23:16:17 +02:00
|
|
|
#include <KMenu>
|
2007-09-23 20:03:37 +02:00
|
|
|
#include <KMimeType>
|
2007-09-18 23:16:17 +02:00
|
|
|
#include <KRun>
|
2007-09-20 20:30:40 +02:00
|
|
|
#include <KServiceTypeTrader>
|
2007-10-25 13:20:23 +02:00
|
|
|
#include <KStandardDirs>
|
2007-09-18 23:16:17 +02:00
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
#include "applethandle_p.h"
|
2007-09-18 23:16:17 +02:00
|
|
|
#include "corona.h"
|
|
|
|
#include "phase.h"
|
2007-11-19 02:19:58 +01:00
|
|
|
#include "toolbox_p.h"
|
2007-09-18 23:16:17 +02:00
|
|
|
#include "svg.h"
|
|
|
|
|
2007-11-19 23:42:56 +01:00
|
|
|
#include "layouts/freelayout.h"
|
|
|
|
#include "layouts/boxlayout.h"
|
2007-11-19 02:19:58 +01:00
|
|
|
#include "widgets/pushbutton.h"
|
2007-09-18 23:16:17 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-10-23 17:56:40 +02:00
|
|
|
static const int INTER_CONTAINMENT_MARGIN = 6;
|
|
|
|
|
2007-09-18 23:16:17 +02:00
|
|
|
class Containment::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
: formFactor(Planar),
|
|
|
|
location(Floating),
|
2007-11-27 00:28:03 +01:00
|
|
|
screen(-1),
|
2007-11-19 02:19:58 +01:00
|
|
|
immutable(false),
|
2007-11-28 22:46:17 +01:00
|
|
|
toolbox(0),
|
2007-11-29 01:20:37 +01:00
|
|
|
type(Containment::NoContainmentType)
|
2007-09-18 23:16:17 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
|
|
|
qDeleteAll(applets);
|
|
|
|
applets.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
FormFactor formFactor;
|
|
|
|
Location location;
|
|
|
|
Applet::List applets;
|
2007-11-12 20:27:21 +01:00
|
|
|
QMap<Applet*, AppletHandle*> handles;
|
2007-09-18 23:16:17 +02:00
|
|
|
int screen;
|
|
|
|
bool immutable;
|
2007-11-19 02:19:58 +01:00
|
|
|
DesktopToolbox *toolbox;
|
2007-11-28 22:46:17 +01:00
|
|
|
Containment::Type type;
|
2007-09-18 23:16:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Containment::Containment(QGraphicsItem* parent,
|
|
|
|
const QString& serviceId,
|
|
|
|
uint containmentId)
|
|
|
|
: Applet(parent, serviceId, containmentId),
|
|
|
|
d(new Private)
|
|
|
|
{
|
2007-11-27 07:08:44 +01:00
|
|
|
// WARNING: do not access config() OR globalConfig() in this method!
|
|
|
|
// that requires a scene, which is not available at this point
|
2007-11-26 19:32:25 +01:00
|
|
|
setDrawStandardBackground(false);
|
2007-11-29 01:20:37 +01:00
|
|
|
setContainmentType(CustomContainment);
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Containment::Containment(QObject* parent, const QVariantList& args)
|
|
|
|
: Applet(parent, args),
|
|
|
|
d(new Private)
|
|
|
|
{
|
2007-11-27 07:08:44 +01:00
|
|
|
// WARNING: do not access config() OR globalConfig() in this method!
|
|
|
|
// that requires a scene, which is not available at this point
|
2007-11-17 04:31:25 +01:00
|
|
|
setDrawStandardBackground(false);
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Containment::~Containment()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Containment::init()
|
|
|
|
{
|
2007-10-25 13:20:23 +02:00
|
|
|
setCachePaintMode(NoCacheMode);
|
2007-09-18 23:16:17 +02:00
|
|
|
setFlag(QGraphicsItem::ItemIsMovable, false);
|
2007-10-17 19:35:48 +02:00
|
|
|
setFlag(QGraphicsItem::ItemClipsChildrenToShape, false);
|
2007-09-23 20:03:37 +02:00
|
|
|
setAcceptDrops(true);
|
2007-11-09 01:44:35 +01:00
|
|
|
setAcceptsHoverEvents(true);
|
2007-10-09 18:33:52 +02:00
|
|
|
|
2007-10-24 04:10:10 +02:00
|
|
|
//TODO: would be nice to not do this on init, as it causes Phase to init
|
|
|
|
connect(Phase::self(), SIGNAL(animationComplete(QGraphicsItem*,Plasma::Phase::Animation)),
|
2007-11-12 20:27:21 +01:00
|
|
|
this, SLOT(appletAnimationComplete(QGraphicsItem*,Plasma::Phase::Animation)));
|
2007-11-19 03:14:18 +01:00
|
|
|
|
2007-11-29 01:20:37 +01:00
|
|
|
if (d->type == NoContainmentType) {
|
|
|
|
setContainmentType(DesktopContainment);
|
2007-11-19 02:19:58 +01:00
|
|
|
}
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
2007-11-17 04:31:25 +01:00
|
|
|
void Containment::loadConstraints(KConfigGroup* group)
|
2007-09-18 23:16:17 +02:00
|
|
|
{
|
2007-11-19 03:14:18 +01:00
|
|
|
/*kDebug() << "!!!!!!!!!!!!initConstraints" << group->name() << containmentType();
|
2007-11-11 00:19:05 +01:00
|
|
|
kDebug() << " location:" << group->readEntry("location", (int)d->location);
|
|
|
|
kDebug() << " geom:" << group->readEntry("geometry", geometry());
|
|
|
|
kDebug() << " formfactor:" << group->readEntry("formfactor", (int)d->formFactor);
|
|
|
|
kDebug() << " screen:" << group->readEntry("screen", d->screen);*/
|
2007-10-26 03:11:27 +02:00
|
|
|
setGeometry(group->readEntry("geometry", geometry()));
|
2007-11-11 00:19:05 +01:00
|
|
|
setLocation((Plasma::Location)group->readEntry("location", (int)d->location));
|
2007-10-26 03:11:27 +02:00
|
|
|
setFormFactor((Plasma::FormFactor)group->readEntry("formfactor", (int)d->formFactor));
|
|
|
|
setScreen(group->readEntry("screen", d->screen));
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Containment::saveConstraints(KConfigGroup* group) const
|
|
|
|
{
|
|
|
|
group->writeEntry("screen", d->screen);
|
|
|
|
group->writeEntry("formfactor", (int)d->formFactor);
|
|
|
|
group->writeEntry("location", (int)d->location);
|
|
|
|
}
|
|
|
|
|
2007-11-19 02:19:58 +01:00
|
|
|
void Containment::containmentConstraintsUpdated(Plasma::Constraints constraints)
|
|
|
|
{
|
|
|
|
if (d->toolbox && constraints & Plasma::ScreenConstraint) {
|
|
|
|
d->toolbox->setPos(geometry().width() - d->toolbox->boundingRect().width(), 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-19 02:35:10 +01:00
|
|
|
Containment::Type Containment::containmentType() const
|
2007-10-09 18:33:52 +02:00
|
|
|
{
|
2007-11-28 22:46:17 +01:00
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Containment::setContainmentType(Containment::Type type)
|
|
|
|
{
|
|
|
|
d->type = type;
|
2007-11-29 01:20:37 +01:00
|
|
|
|
|
|
|
if (isContainment() && type == DesktopContainment) {
|
|
|
|
if (!d->toolbox) {
|
|
|
|
Plasma::PushButton *tool = new Plasma::PushButton(i18n("Add Widgets"));
|
|
|
|
tool->resize(tool->sizeHint());
|
|
|
|
addToolBoxTool(tool);
|
|
|
|
connect(tool, SIGNAL(clicked()), this, SIGNAL(showAddWidgets()));
|
|
|
|
|
|
|
|
tool = new Plasma::PushButton(i18n("Zoom In"));
|
|
|
|
connect(tool, SIGNAL(clicked()), this, SIGNAL(zoomIn()));
|
|
|
|
tool->resize(tool->sizeHint());
|
|
|
|
addToolBoxTool(tool);
|
|
|
|
|
|
|
|
tool = new Plasma::PushButton(i18n("Zoom Out"));
|
|
|
|
connect(tool, SIGNAL(clicked()), this, SIGNAL(zoomOut()));
|
|
|
|
tool->resize(tool->sizeHint());
|
|
|
|
addToolBoxTool(tool);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
delete d->toolbox;
|
|
|
|
d->toolbox = 0;
|
|
|
|
}
|
2007-10-09 18:33:52 +02:00
|
|
|
}
|
|
|
|
|
2007-11-09 01:44:35 +01:00
|
|
|
Corona* Containment::corona() const
|
|
|
|
{
|
|
|
|
return dynamic_cast<Corona*>(scene());
|
|
|
|
}
|
|
|
|
|
2007-09-18 23:16:17 +02:00
|
|
|
void Containment::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
|
|
|
|
{
|
2007-10-11 21:16:01 +02:00
|
|
|
//kDebug() << "let's see if we manage to get a context menu here, huh";
|
2007-09-18 23:16:17 +02:00
|
|
|
if (!scene() || !KAuthorized::authorizeKAction("desktop_contextmenu")) {
|
|
|
|
QGraphicsItem::contextMenuEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF point = event->scenePos();
|
|
|
|
QGraphicsItem* item = scene()->itemAt(point);
|
|
|
|
if (item == this) {
|
|
|
|
item = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Applet* applet = 0;
|
|
|
|
|
|
|
|
while (item) {
|
|
|
|
applet = qgraphicsitem_cast<Applet*>(item);
|
|
|
|
if (applet) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
item = item->parentItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
KMenu desktopMenu;
|
2007-11-18 23:02:01 +01:00
|
|
|
//kDebug() << "context menu event " << (QObject*)applet;
|
2007-09-18 23:16:17 +02:00
|
|
|
if (!applet) {
|
|
|
|
if (!scene() || static_cast<Corona*>(scene())->isImmutable()) {
|
2007-11-17 21:50:57 +01:00
|
|
|
//kDebug() << "immutability";
|
2007-09-18 23:16:17 +02:00
|
|
|
QGraphicsItem::contextMenuEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//FIXME: change this to show this only in debug mode (or not at all?)
|
|
|
|
// before final release
|
|
|
|
QList<QAction*> actions = contextActions();
|
2007-10-07 23:53:20 +02:00
|
|
|
|
|
|
|
if (actions.count() < 1) {
|
2007-11-17 21:50:57 +01:00
|
|
|
//kDebug() << "no applet, but no actions";
|
2007-10-07 23:53:20 +02:00
|
|
|
QGraphicsItem::contextMenuEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-18 23:16:17 +02:00
|
|
|
foreach(QAction* action, actions) {
|
|
|
|
desktopMenu.addAction(action);
|
|
|
|
}
|
2007-11-12 20:27:21 +01:00
|
|
|
} else {
|
2007-11-22 06:11:06 +01:00
|
|
|
bool hasEntries = false;
|
|
|
|
if (applet->hasConfigurationInterface()) {
|
|
|
|
QAction* configureApplet = new QAction(i18n("%1 Settings...", applet->name()), &desktopMenu);
|
|
|
|
connect(configureApplet, SIGNAL(triggered(bool)),
|
|
|
|
applet, SLOT(showConfigurationInterface()));
|
|
|
|
desktopMenu.addAction(configureApplet);
|
|
|
|
hasEntries = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scene() && !static_cast<Corona*>(scene())->isImmutable()) {
|
|
|
|
QAction* closeApplet = new QAction(i18n("Remove this %1", applet->name()), &desktopMenu);
|
|
|
|
QVariant appletV;
|
|
|
|
appletV.setValue((QObject*)applet);
|
|
|
|
closeApplet->setData(appletV);
|
|
|
|
connect(closeApplet, SIGNAL(triggered(bool)),
|
|
|
|
this, SLOT(destroyApplet()));
|
|
|
|
desktopMenu.addAction(closeApplet);
|
|
|
|
hasEntries = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QAction*> actions = applet->contextActions();
|
|
|
|
if (!actions.isEmpty()) {
|
|
|
|
desktopMenu.addSeparator();
|
|
|
|
foreach(QAction* action, actions) {
|
|
|
|
desktopMenu.addAction(action);
|
|
|
|
}
|
|
|
|
hasEntries = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasEntries) {
|
|
|
|
QGraphicsItem::contextMenuEvent(event);
|
|
|
|
kDebug() << "no entries";
|
|
|
|
return;
|
2007-11-18 23:02:01 +01:00
|
|
|
}
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();
|
2007-11-17 21:50:57 +01:00
|
|
|
//kDebug() << "executing at" << event->screenPos();
|
2007-10-18 11:52:59 +02:00
|
|
|
desktopMenu.exec(event->screenPos());
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
2007-11-22 06:11:06 +01:00
|
|
|
void Containment::destroyApplet()
|
|
|
|
{
|
|
|
|
QAction *action = qobject_cast<QAction*>(sender());
|
|
|
|
|
|
|
|
if (!action) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Applet *applet = qobject_cast<Applet*>(action->data().value<QObject*>());
|
|
|
|
Phase::self()->animateItem(applet, Phase::Disappear);
|
|
|
|
}
|
|
|
|
|
2007-09-18 23:16:17 +02:00
|
|
|
void Containment::setFormFactor(FormFactor formFactor)
|
|
|
|
{
|
2007-11-11 00:19:05 +01:00
|
|
|
if (d->formFactor == formFactor && layout()) {
|
2007-09-18 23:16:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-17 21:50:57 +01:00
|
|
|
//kDebug() << "switching FF to " << formFactor;
|
2007-09-18 23:16:17 +02:00
|
|
|
d->formFactor = formFactor;
|
2007-11-11 00:19:05 +01:00
|
|
|
Layout *lay = layout();
|
|
|
|
setLayout(0);
|
|
|
|
delete lay;
|
|
|
|
lay = 0;
|
2007-09-18 23:16:17 +02:00
|
|
|
|
|
|
|
switch (d->formFactor) {
|
|
|
|
case Planar:
|
2007-11-11 00:19:05 +01:00
|
|
|
lay = new FreeLayout(this);
|
2007-09-18 23:16:17 +02:00
|
|
|
break;
|
|
|
|
case Horizontal:
|
2007-11-11 00:19:05 +01:00
|
|
|
lay = new BoxLayout(BoxLayout::LeftToRight, this);
|
|
|
|
lay->setMargin(0);
|
2007-11-20 01:44:29 +01:00
|
|
|
lay->setSpacing(4);
|
2007-09-18 23:16:17 +02:00
|
|
|
break;
|
|
|
|
case Vertical:
|
2007-11-11 00:19:05 +01:00
|
|
|
lay = new BoxLayout(BoxLayout::TopToBottom, this);
|
|
|
|
lay->setMargin(0);
|
2007-11-20 01:44:29 +01:00
|
|
|
lay->setSpacing(4);
|
2007-09-18 23:16:17 +02:00
|
|
|
break;
|
|
|
|
case MediaCenter:
|
|
|
|
//FIXME: need a layout type here!
|
|
|
|
break;
|
|
|
|
default:
|
2007-11-29 01:20:37 +01:00
|
|
|
kDebug() << "This can't be happening! Or... can it? ;)" << d->formFactor;
|
2007-09-18 23:16:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-11-11 00:19:05 +01:00
|
|
|
if (lay) {
|
|
|
|
foreach (Applet* applet, d->applets) {
|
|
|
|
lay->addItem(applet);
|
|
|
|
applet->updateConstraints(Plasma::FormFactorConstraint);
|
|
|
|
}
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
2007-10-02 01:24:38 +02:00
|
|
|
|
|
|
|
updateConstraints(Plasma::FormFactorConstraint);
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FormFactor Containment::formFactor() const
|
|
|
|
{
|
|
|
|
return d->formFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Containment::setLocation(Location location)
|
|
|
|
{
|
|
|
|
if (d->location == location) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->location = location;
|
|
|
|
|
|
|
|
foreach (Applet* applet, d->applets) {
|
2007-10-02 01:24:38 +02:00
|
|
|
applet->updateConstraints(Plasma::LocationConstraint);
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
2007-10-02 01:24:38 +02:00
|
|
|
|
2007-11-09 01:44:35 +01:00
|
|
|
setScreen(screen());
|
2007-10-02 01:24:38 +02:00
|
|
|
updateConstraints(Plasma::LocationConstraint);
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Location Containment::location() const
|
|
|
|
{
|
|
|
|
return d->location;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Containment::clearApplets()
|
|
|
|
{
|
|
|
|
qDeleteAll(d->applets);
|
|
|
|
d->applets.clear();
|
|
|
|
}
|
|
|
|
|
2007-11-11 00:19:05 +01:00
|
|
|
Applet* Containment::addApplet(const QString& name, const QVariantList& args, uint id, const QRectF& appletGeometry, bool delayInit)
|
2007-09-18 23:16:17 +02:00
|
|
|
{
|
|
|
|
Applet* applet = Applet::loadApplet(name, id, args);
|
|
|
|
if (!applet) {
|
|
|
|
kDebug() << "Applet" << name << "could not be loaded.";
|
|
|
|
applet = new Applet;
|
|
|
|
}
|
|
|
|
|
2007-10-11 10:41:10 +02:00
|
|
|
//panels don't want backgrounds, which is important when setting geometry
|
2007-11-19 03:14:18 +01:00
|
|
|
if (containmentType() == PanelContainment) {
|
2007-10-11 10:41:10 +02:00
|
|
|
applet->setDrawStandardBackground(false);
|
|
|
|
}
|
2007-11-11 00:19:05 +01:00
|
|
|
|
2007-11-26 22:57:50 +01:00
|
|
|
addApplet(applet);
|
|
|
|
|
2007-10-07 09:28:24 +02:00
|
|
|
//the applet needs to be given constraints before it can set its geometry
|
|
|
|
applet->updateConstraints(Plasma::AllConstraints);
|
|
|
|
|
2007-11-15 12:06:33 +01:00
|
|
|
//kDebug() << "adding applet" << applet->name() << "with a default geometry of" << appletGeometry << appletGeometry.isValid();
|
2007-11-11 00:19:05 +01:00
|
|
|
if (appletGeometry.isValid()) {
|
|
|
|
applet->setGeometry(appletGeometry);
|
|
|
|
} else if (appletGeometry.x() != -1 && appletGeometry.y() != -1) {
|
2007-09-18 23:16:17 +02:00
|
|
|
// yes, this means we can't have items start -1, -1
|
2007-11-11 00:19:05 +01:00
|
|
|
applet->setGeometry(QRectF(appletGeometry.topLeft(),
|
2007-09-18 23:16:17 +02:00
|
|
|
applet->sizeHint()));
|
2007-11-11 00:19:05 +01:00
|
|
|
} else if (geometry().isValid()) {
|
2007-09-18 23:16:17 +02:00
|
|
|
//TODO: Make sure new applets don't overlap with existing ones
|
|
|
|
// Center exactly:
|
|
|
|
QSizeF size = applet->sizeHint();
|
|
|
|
qreal appletWidth = size.width();
|
|
|
|
qreal appletHeight = size.height();
|
2007-11-11 00:19:05 +01:00
|
|
|
qreal width = geometry().width();
|
|
|
|
qreal height = geometry().height();
|
2007-09-18 23:16:17 +02:00
|
|
|
//kDebug() << "measuring geometry with" << appletWidth << appletHeight << width << height;
|
|
|
|
applet->setGeometry(QRectF(QPointF((width / 2) - (appletWidth / 2), (height / 2) - (appletHeight / 2)), size));
|
|
|
|
}
|
|
|
|
|
2007-11-29 02:05:16 +01:00
|
|
|
//kDebug() << applet->name() << "sizehint:" << applet->sizeHint() << "geometry:" << applet->geometry();
|
2007-09-30 20:06:08 +02:00
|
|
|
|
2007-11-28 00:19:02 +01:00
|
|
|
if (delayInit) {
|
2007-11-28 00:36:10 +01:00
|
|
|
if (containmentType() == DesktopContainment) {
|
2007-11-28 00:23:38 +01:00
|
|
|
applet->installSceneEventFilter(this);
|
|
|
|
}
|
2007-11-28 00:19:02 +01:00
|
|
|
} else {
|
2007-09-18 23:16:17 +02:00
|
|
|
applet->init();
|
2007-11-28 00:19:02 +01:00
|
|
|
Phase::self()->animateItem(applet, Phase::Appear);
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
2007-11-09 19:47:35 +01:00
|
|
|
emit appletAdded(applet);
|
2007-09-18 23:16:17 +02:00
|
|
|
return applet;
|
|
|
|
}
|
|
|
|
|
2007-11-27 07:08:44 +01:00
|
|
|
void Containment::addApplet(Applet *applet)
|
2007-11-26 22:57:50 +01:00
|
|
|
{
|
2007-11-27 07:08:44 +01:00
|
|
|
Containment *currentContainment = applet->containment();
|
|
|
|
if (currentContainment && currentContainment != this) {
|
|
|
|
applet->removeSceneEventFilter(currentContainment);
|
|
|
|
applet->resetConfigurationObject();
|
|
|
|
currentContainment->d->applets.removeAll(applet);
|
|
|
|
}
|
|
|
|
|
2007-11-26 22:57:50 +01:00
|
|
|
d->applets << applet;
|
|
|
|
addChild(applet);
|
2007-11-27 07:08:44 +01:00
|
|
|
|
|
|
|
if (currentContainment) {
|
|
|
|
applet->installSceneEventFilter(this);
|
|
|
|
}
|
|
|
|
|
2007-11-26 22:57:50 +01:00
|
|
|
connect(applet, SIGNAL(destroyed(QObject*)),
|
|
|
|
this, SLOT(appletDestroyed(QObject*)));
|
|
|
|
}
|
|
|
|
|
2007-09-18 23:16:17 +02:00
|
|
|
void Containment::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);
|
2007-11-27 07:08:44 +01:00
|
|
|
d->applets.removeAll(applet);
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
void Containment::appletAnimationComplete(QGraphicsItem *item, Plasma::Phase::Animation anim)
|
2007-10-24 04:10:10 +02:00
|
|
|
{
|
|
|
|
if (anim == Phase::Disappear) {
|
2007-11-22 06:11:06 +01:00
|
|
|
QGraphicsItem *parent = item->parentItem();
|
2007-10-24 04:10:10 +02:00
|
|
|
|
2007-11-22 06:11:06 +01:00
|
|
|
while (parent) {
|
|
|
|
if (parent == this) {
|
|
|
|
Applet *applet = qgraphicsitem_cast<Applet*>(item);
|
|
|
|
|
|
|
|
if (applet) {
|
|
|
|
applet->destroy();
|
|
|
|
}
|
2007-11-27 07:08:44 +01:00
|
|
|
|
|
|
|
break;
|
2007-10-24 04:10:10 +02:00
|
|
|
}
|
2007-11-22 06:11:06 +01:00
|
|
|
|
|
|
|
parent = parent->parentItem();
|
2007-10-24 04:10:10 +02:00
|
|
|
}
|
2007-11-12 20:27:21 +01:00
|
|
|
} else if (anim == Phase::Appear) {
|
2007-11-22 08:37:57 +01:00
|
|
|
if (containmentType() == DesktopContainment &&
|
|
|
|
item->parentItem() == this &&
|
2007-11-22 09:00:35 +01:00
|
|
|
qgraphicsitem_cast<Applet*>(item)) {
|
2007-11-22 08:37:57 +01:00
|
|
|
item->installSceneEventFilter(this);
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
2007-10-24 04:10:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-18 23:16:17 +02:00
|
|
|
Applet::List Containment::applets() const
|
|
|
|
{
|
|
|
|
return d->applets;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Containment::setScreen(int screen)
|
|
|
|
{
|
2007-10-26 01:09:02 +02:00
|
|
|
// screen of -1 means no associated screen.
|
2007-11-17 21:50:57 +01:00
|
|
|
// sanity check to make sure someone else doesn't have this screen already!
|
2007-11-27 00:28:03 +01:00
|
|
|
if (screen > -1 && containmentType() == DesktopContainment && corona()) {
|
2007-11-17 21:50:57 +01:00
|
|
|
Containment* currently = corona()->containmentForScreen(screen);
|
|
|
|
if (currently && currently != this) {
|
|
|
|
//kDebug() << "currently is on screen" << currently->screen() << "and is" << currently->name() << (QObject*)currently << (QObject*)this;
|
|
|
|
currently->setScreen(-1);
|
|
|
|
}
|
|
|
|
}
|
2007-10-26 01:09:02 +02:00
|
|
|
|
2007-11-29 02:05:16 +01:00
|
|
|
//kDebug() << "setting screen to" << screen << "and we are a" << containmentType();
|
2007-11-09 01:44:35 +01:00
|
|
|
QDesktopWidget *desktop = QApplication::desktop();
|
|
|
|
int numScreens = desktop->numScreens();
|
2007-11-27 09:47:46 +01:00
|
|
|
if (screen < -1) {
|
2007-09-18 23:16:17 +02:00
|
|
|
screen = -1;
|
|
|
|
}
|
|
|
|
|
2007-11-19 03:14:18 +01:00
|
|
|
//kDebug() << "setting screen to " << screen << "and type is" << containmentType();
|
2007-11-27 09:47:46 +01:00
|
|
|
if (screen < numScreens && screen > -1) {
|
2007-11-09 01:44:35 +01:00
|
|
|
QRect r = desktop->screenGeometry(screen);
|
2007-10-26 01:31:20 +02:00
|
|
|
|
2007-11-19 03:14:18 +01:00
|
|
|
if (containmentType() == DesktopContainment) {
|
2007-10-30 00:32:55 +01:00
|
|
|
// we need to find how many screens are to our top and left
|
|
|
|
// to calculate the proper offsets for the margins.
|
|
|
|
int x = r.x();
|
|
|
|
int y = r.y();
|
|
|
|
int screensLeft = 0;
|
|
|
|
int screensAbove = 0;
|
|
|
|
for (int i = 0; i < numScreens; ++i) {
|
2007-11-09 01:44:35 +01:00
|
|
|
QRect otherScreen = desktop->screenGeometry(screen);
|
2007-10-30 00:32:55 +01:00
|
|
|
if (x > otherScreen.x()) {
|
|
|
|
++screensLeft;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y > otherScreen.y()) {
|
|
|
|
++screensAbove;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.moveLeft(r.x() + INTER_CONTAINMENT_MARGIN * screensLeft);
|
|
|
|
r.moveTop(r.y() + INTER_CONTAINMENT_MARGIN * screensAbove);
|
2007-11-17 21:50:57 +01:00
|
|
|
|
|
|
|
// FIXME: positioning at this x,y will break if we switch between containments for a
|
|
|
|
// given screen! we should change the pos() on new containment setup.
|
2007-10-26 01:31:20 +02:00
|
|
|
setGeometry(r);
|
2007-11-09 01:44:35 +01:00
|
|
|
//kDebug() << "setting geometry to" << desktop->screenGeometry(screen) << r << geometry();
|
2007-11-19 03:14:18 +01:00
|
|
|
} else if (containmentType() == PanelContainment) {
|
2007-11-09 01:44:35 +01:00
|
|
|
QRect r = desktop->screenGeometry(screen);
|
2007-11-04 06:38:16 +01:00
|
|
|
//kDebug() << "we are a panel on" << r << ", let's move ourselves to a negative coordinate system" << -r.height() - INTER_CONTAINMENT_MARGIN;
|
2007-11-09 01:44:35 +01:00
|
|
|
int vertOffset = r.height() + INTER_CONTAINMENT_MARGIN;
|
|
|
|
translate(0, -vertOffset);
|
2007-10-26 01:09:02 +02:00
|
|
|
}
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d->screen = screen;
|
2007-10-02 01:24:38 +02:00
|
|
|
updateConstraints(Plasma::ScreenConstraint);
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int Containment::screen() const
|
|
|
|
{
|
|
|
|
return d->screen;
|
|
|
|
}
|
|
|
|
|
2007-09-20 20:30:40 +02:00
|
|
|
KPluginInfo::List Containment::knownContainments(const QString &category,
|
|
|
|
const QString &parentApp)
|
|
|
|
{
|
|
|
|
QString constraint;
|
|
|
|
|
|
|
|
if (parentApp.isEmpty()) {
|
|
|
|
constraint.append("not exist [X-KDE-ParentApp]");
|
|
|
|
} else {
|
|
|
|
constraint.append("[X-KDE-ParentApp] == '").append(parentApp).append("'");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!category.isEmpty()) {
|
|
|
|
if (!constraint.isEmpty()) {
|
|
|
|
constraint.append(" and ");
|
|
|
|
}
|
|
|
|
|
|
|
|
constraint.append("[X-KDE-PluginInfo-Category] == '").append(category).append("'");
|
|
|
|
if (category == "Miscellaneous") {
|
|
|
|
constraint.append(" or (not exist [X-KDE-PluginInfo-Category] or [X-KDE-PluginInfo-Category] == '')");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/Containment", constraint);
|
|
|
|
//kDebug() << "constraint was" << constraint << "which got us" << offers.count() << "matches";
|
|
|
|
return KPluginInfo::fromServices(offers);
|
|
|
|
}
|
|
|
|
|
|
|
|
KPluginInfo::List Containment::knownContainmentsForMimetype(const QString &mimetype)
|
|
|
|
{
|
|
|
|
QString constraint = QString("'%1' in MimeTypes").arg(mimetype);
|
|
|
|
//kDebug() << "knownContainmentsForMimetype with" << mimetype << constraint;
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/Containment", constraint);
|
|
|
|
return KPluginInfo::fromServices(offers);
|
|
|
|
}
|
|
|
|
|
2007-09-23 20:03:37 +02:00
|
|
|
void Containment::dropEvent(QGraphicsSceneDragDropEvent *event)
|
|
|
|
{
|
|
|
|
//kDebug() << "drop event:" << event->mimeData()->text();
|
|
|
|
|
|
|
|
QString mimetype(static_cast<Corona*>(scene())->appletMimeType());
|
|
|
|
|
|
|
|
if (event->mimeData()->hasFormat(mimetype) && scene()) {
|
|
|
|
QString plasmoidName;
|
|
|
|
plasmoidName = event->mimeData()->data(mimetype);
|
2007-10-27 14:37:05 +02:00
|
|
|
QRectF geom(mapFromScene(event->scenePos()), QSize(0, 0));
|
2007-09-23 20:03:37 +02:00
|
|
|
addApplet(plasmoidName, QVariantList(), 0, geom);
|
|
|
|
event->acceptProposedAction();
|
|
|
|
} else if (KUrl::List::canDecode(event->mimeData())) {
|
2007-10-03 17:46:40 +02:00
|
|
|
KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
|
2007-09-23 20:03:37 +02:00
|
|
|
foreach (const KUrl& url, urls) {
|
|
|
|
KMimeType::Ptr mime = KMimeType::findByUrl(url);
|
|
|
|
QString mimeName = mime->name();
|
|
|
|
QRectF geom(event->scenePos(), QSize(0, 0));
|
|
|
|
QVariantList args;
|
|
|
|
args << url.url();
|
2007-10-30 00:32:55 +01:00
|
|
|
// kDebug() << mimeName;
|
2007-09-23 20:03:37 +02:00
|
|
|
KPluginInfo::List appletList = Applet::knownAppletsForMimetype(mimeName);
|
|
|
|
|
|
|
|
if (appletList.isEmpty()) {
|
2007-10-03 17:46:40 +02:00
|
|
|
// no special applet associated with this mimetype, let's
|
2007-09-23 20:03:37 +02:00
|
|
|
addApplet("url", args, 0, geom);
|
|
|
|
} else {
|
|
|
|
//TODO: should we show a dialog here to choose which plasmoid load if
|
|
|
|
//appletList.count() > 0?
|
|
|
|
addApplet(appletList.first().pluginName(), args, 0, geom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-05 19:40:45 +01:00
|
|
|
void Containment::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
//FIXME Qt4.4 check to see if this is still necessary to avoid unecessary repaints
|
|
|
|
// check with QT_FLUSH_PAINT=1 and mouse through applets that accept hover,
|
|
|
|
// applets that don't and system windows
|
|
|
|
if (event->spontaneous()) {
|
|
|
|
Applet::hoverEnterEvent(event);
|
|
|
|
}
|
|
|
|
Q_UNUSED(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
void Containment::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
//FIXME Qt4.4 check to see if this is still necessary to avoid unecessary repaints
|
|
|
|
// check with QT_FLUSH_PAINT=1 and mouse through applets that accept hover,
|
|
|
|
// applets that don't and system windows
|
|
|
|
// Applet::hoverLeaveEvent(event);
|
|
|
|
Q_UNUSED(event)
|
|
|
|
}
|
|
|
|
|
2007-11-12 20:27:21 +01:00
|
|
|
bool Containment::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
Applet *applet = qgraphicsitem_cast<Applet*>(watched);
|
|
|
|
//QEvent::GraphicsSceneHoverEnter
|
|
|
|
|
|
|
|
// Otherwise we're watching something we shouldn't be...
|
2007-11-27 07:08:44 +01:00
|
|
|
//kDebug() << "got sceneEvent";
|
2007-11-12 20:27:21 +01:00
|
|
|
Q_ASSERT(applet!=0);
|
2007-11-13 16:49:28 +01:00
|
|
|
if (!d->applets.contains(applet)) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-11-12 20:27:21 +01:00
|
|
|
|
2007-11-13 16:49:28 +01:00
|
|
|
switch (event->type()) {
|
2007-11-12 20:27:21 +01:00
|
|
|
case QEvent::GraphicsSceneHoverEnter:
|
2007-11-27 07:08:44 +01:00
|
|
|
//kDebug() << "got hoverenterEvent" << d->immutable << " " << applet->isImmutable();
|
2007-11-18 23:02:01 +01:00
|
|
|
if (!d->immutable && !applet->isImmutable() && !d->handles.contains(applet)) {
|
2007-11-27 07:08:44 +01:00
|
|
|
//kDebug() << "generated applet handle";
|
2007-11-12 20:27:21 +01:00
|
|
|
AppletHandle *handle = new AppletHandle(this, applet);
|
|
|
|
d->handles[applet] = handle;
|
|
|
|
connect(handle, SIGNAL(disappearDone(AppletHandle*)),
|
|
|
|
this, SLOT(handleDisappeared(AppletHandle*)));
|
2007-11-23 09:01:29 +01:00
|
|
|
connect(applet, SIGNAL(geometryChanged()),
|
|
|
|
handle, SLOT(appletResized()));
|
2007-11-12 20:27:21 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Containment::handleDisappeared(AppletHandle *handle)
|
|
|
|
{
|
|
|
|
d->handles.remove(handle->applet());
|
|
|
|
handle->deleteLater();
|
|
|
|
}
|
|
|
|
|
2007-11-15 12:06:33 +01:00
|
|
|
void Containment::emitLaunchActivated()
|
|
|
|
{
|
|
|
|
kDebug();
|
|
|
|
emit launchActivated();
|
|
|
|
}
|
|
|
|
|
2007-11-19 02:19:58 +01:00
|
|
|
void Containment::addToolBoxTool(QGraphicsItem *tool)
|
|
|
|
{
|
|
|
|
if (!d->toolbox) {
|
|
|
|
d->toolbox = new DesktopToolbox(this);
|
|
|
|
d->toolbox->setPos(geometry().width() - d->toolbox->boundingRect().width(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
d->toolbox->addTool(tool);
|
2007-09-18 23:16:17 +02:00
|
|
|
}
|
|
|
|
|
2007-11-19 02:19:58 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|
2007-09-18 23:16:17 +02:00
|
|
|
#include "containment.moc"
|
2007-11-19 02:19:58 +01:00
|
|
|
|