introducing the concept of associated application:

an an application and/or an url list can be associated with an aplet
and a context menu entry and an applet handle button will be added to
launch that application.
the applet will be considered a preview of something, where the
application its full view, for example the picture frame can open
gwenview

svn path=/trunk/KDE/kdelibs/; revision=1024487
This commit is contained in:
Marco Martin 2009-09-16 17:08:45 +00:00
parent 6a9a5ec1a8
commit 08d49e6fd0
9 changed files with 357 additions and 2 deletions

View File

@ -63,6 +63,7 @@ set(plasma_LIB_SRCS
plasma.cpp plasma.cpp
popupapplet.cpp popupapplet.cpp
private/applethandle.cpp private/applethandle.cpp
private/associatedapplicationmanager.cpp
private/datacontainer_p.cpp private/datacontainer_p.cpp
private/dataengineconsumer.cpp private/dataengineconsumer.cpp
private/dataengineservice.cpp private/dataengineservice.cpp

View File

@ -60,6 +60,10 @@
#include <kshortcut.h> #include <kshortcut.h>
#include <kwindowsystem.h> #include <kwindowsystem.h>
#include <kpushbutton.h> #include <kpushbutton.h>
#include <krun.h>
#include "kio/jobclasses.h" // for KIO::JobFlags
#include "kio/job.h"
#include <solid/powermanagement.h> #include <solid/powermanagement.h>
@ -99,6 +103,7 @@
#include "private/service_p.h" #include "private/service_p.h"
#include "private/remotedataengine.h" #include "private/remotedataengine.h"
#include "private/toolbox_p.h" #include "private/toolbox_p.h"
#include "private/associatedapplicationmanager_p.h"
#include "ui_publish.h" #include "ui_publish.h"
#include "config-plasma.h" #include "config-plasma.h"
@ -1163,6 +1168,11 @@ void Applet::flushPendingConstraintsEvents()
configAction->setEnabled(canConfig); configAction->setEnabled(canConfig);
} }
QAction *runAssociatedApplication = d->actions->action("run associated application");
if (runAssociatedApplication) {
connect(runAssociatedApplication, SIGNAL(triggered(bool)), this, SLOT(runAssociatedApplication()));
}
d->updateShortcuts(); d->updateShortcuts();
Corona * corona = qobject_cast<Corona*>(scene()); Corona * corona = qobject_cast<Corona*>(scene());
if (corona) { if (corona) {
@ -1569,6 +1579,14 @@ KActionCollection* AppletPrivate::defaultActions(QObject *parent)
closeApplet->setIcon(KIcon("edit-delete")); closeApplet->setIcon(KIcon("edit-delete"));
closeApplet->setShortcut(KShortcut("alt+d, r")); closeApplet->setShortcut(KShortcut("alt+d, r"));
KAction *runAssociatedApplication = actions->addAction("run associated application");
runAssociatedApplication->setAutoRepeat(false);
runAssociatedApplication->setText(i18n("Run the associated aplication"));
runAssociatedApplication->setIcon(KIcon("system-run"));
runAssociatedApplication->setShortcut(KShortcut("alt+d, t"));
runAssociatedApplication->setVisible(false);
runAssociatedApplication->setEnabled(false);
return actions; return actions;
} }
@ -1921,6 +1939,50 @@ bool Applet::hasAuthorization(const QString &constraint) const
return constraintGroup.readEntry(constraint, true); return constraintGroup.readEntry(constraint, true);
} }
void Applet::setAssociatedApplication(const QString &string)
{
AssociatedApplicationManager::self()->setApplication(this, string);
QAction *runAssociatedApplication = d->actions->action("run associated application");
if (runAssociatedApplication) {
bool valid = AssociatedApplicationManager::self()->appletHasValidAssociatedApplication(this);
runAssociatedApplication->setVisible(valid);
runAssociatedApplication->setEnabled(valid);
}
}
void Applet::setAssociatedApplicationUrls(const KUrl::List &urls)
{
AssociatedApplicationManager::self()->setUrls(this, urls);
QAction *runAssociatedApplication = d->actions->action("run associated application");
if (runAssociatedApplication) {
bool valid = AssociatedApplicationManager::self()->appletHasValidAssociatedApplication(this);
runAssociatedApplication->setVisible(valid);
runAssociatedApplication->setEnabled(valid);
}
}
QString Applet::associatedApplication() const
{
return AssociatedApplicationManager::self()->application(this);
}
KUrl::List Applet::associatedApplicationUrls() const
{
return AssociatedApplicationManager::self()->urls(this);
}
void Applet::runAssociatedApplication()
{
AssociatedApplicationManager::self()->run(this);
}
bool Applet::hasValidAssociatedApplication() const
{
return AssociatedApplicationManager::self()->appletHasValidAssociatedApplication(this);
}
KPluginInfo::List Applet::listAppletInfo(const QString &category, KPluginInfo::List Applet::listAppletInfo(const QString &category,
const QString &parentApp) const QString &parentApp)
{ {

View File

@ -628,6 +628,46 @@ class PLASMA_EXPORT Applet : public QGraphicsWidget
*/ */
bool hasAuthorization(const QString &constraint) const; bool hasAuthorization(const QString &constraint) const;
/**
* Sets an application associated to this applet, that will be
* regarded as a full view of what is represented in the applet
*
* @param string the name of the application. it can be
* \li a name understood by KService::serviceByDesktopName
* (e.g. "konqueror")
* \li a command in $PATH
* \li or an absolute path to an executable
* @since 4.4
*/
void setAssociatedApplication(const QString &string);
/**
* Sets a list of urls associated to this application,
* they will be used as parameters for the associated application
* @see setAssociatedApplication()
*
* @param urls
*/
void setAssociatedApplicationUrls(const KUrl::List &urls);
/**
* @return the application associated to this applet
* @since 4.4
*/
QString associatedApplication() const;
/**
* @return the urls associated to this applet
* @since 4.4
*/
KUrl::List associatedApplicationUrls() const;
/**
* @return true if the applet has a valid associated application or urls
* @since 4.4
*/
bool hasValidAssociatedApplication() const;
Q_SIGNALS: Q_SIGNALS:
/** /**
* This signal indicates that an application launch, window * This signal indicates that an application launch, window
@ -800,6 +840,16 @@ class PLASMA_EXPORT Applet : public QGraphicsWidget
bool isPublished() const; bool isPublished() const;
/**
* Open the application associated to this applet, if it's not set
* but some urls are, open those urls with the proper application
* for their mimetype
* @see setAssociatedApplication()
* @see setAssociatedApplicationUrls()
* @since 4.4
*/
void runAssociatedApplication();
protected: protected:
/** /**
* This constructor is to be used with the plugin loading systems * This constructor is to be used with the plugin loading systems
@ -1018,6 +1068,7 @@ class PLASMA_EXPORT Applet : public QGraphicsWidget
friend class AccessAppletJobPrivate; friend class AccessAppletJobPrivate;
friend class PopupApplet; friend class PopupApplet;
friend class PopupAppletPrivate; friend class PopupAppletPrivate;
friend class AssociatedApplicationManager;
friend class Extender; friend class Extender;
friend class ExtenderGroup; friend class ExtenderGroup;

View File

@ -655,6 +655,11 @@ void ContainmentPrivate::appletActions(KMenu &desktopMenu, Applet *applet, bool
desktopMenu.addAction(configureApplet); desktopMenu.addAction(configureApplet);
} }
QAction *runAssociatedApplication = applet->d->actions->action("run associated application");
if (runAssociatedApplication && runAssociatedApplication->isEnabled()) {
desktopMenu.addAction(runAssociatedApplication);
}
KMenu *containmentMenu = new KMenu(i18nc("%1 is the name of the containment", "%1 Options", q->name()), &desktopMenu); KMenu *containmentMenu = new KMenu(i18nc("%1 is the name of the containment", "%1 Options", q->name()), &desktopMenu);
containmentActions(*containmentMenu); containmentActions(*containmentMenu);
if (!containmentMenu->isEmpty()) { if (!containmentMenu->isEmpty()) {

View File

@ -32,6 +32,11 @@
class KKeySequenceWidget; class KKeySequenceWidget;
namespace KIO
{
class Job;
}
namespace Plasma namespace Plasma
{ {

View File

@ -206,7 +206,7 @@ void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
int iconMargin = m_iconSize / 2; int iconMargin = m_iconSize / 2;
const QSize pixmapSize(int(m_decorationRect.width()), const QSize pixmapSize(int(m_decorationRect.width()),
int(m_decorationRect.height()) + m_iconSize * 4 + 1); int(m_decorationRect.height()) + m_iconSize * 5 + 1);
const QSize iconSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall); const QSize iconSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall);
//regenerate our buffer? //regenerate our buffer?
@ -270,6 +270,12 @@ void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
iconRect.translate(0, m_iconSize); iconRect.translate(0, m_iconSize);
m_configureIcons->paint(&buffPainter, iconRect, "configure"); m_configureIcons->paint(&buffPainter, iconRect, "configure");
} }
if (m_applet && m_applet->hasValidAssociatedApplication()) {
iconRect.translate(0, m_iconSize);
m_configureIcons->paint(&buffPainter, iconRect, "maximize");
}
iconRect.translate(0, m_iconSize); iconRect.translate(0, m_iconSize);
m_configureIcons->paint(&buffPainter, iconRect, "close"); m_configureIcons->paint(&buffPainter, iconRect, "close");
@ -294,6 +300,7 @@ void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
QPointF shiftD; QPointF shiftD;
QPointF shiftR; QPointF shiftR;
QPointF shiftM; QPointF shiftM;
QPointF shiftMx;
switch(m_pressedButton) switch(m_pressedButton)
{ {
@ -309,6 +316,9 @@ void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
case ResizeButton: case ResizeButton:
shiftM = QPointF(2, 2); shiftM = QPointF(2, 2);
break; break;
case MaximizeButton:
shiftMx = QPointF(2, 2);
break;
default: default:
break; break;
} }
@ -332,6 +342,7 @@ void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
sourceIconRect.translate(0, m_iconSize); sourceIconRect.translate(0, m_iconSize);
painter->drawPixmap(QRectF(basePoint + shiftR, iconSize), *m_backgroundBuffer, sourceIconRect); painter->drawPixmap(QRectF(basePoint + shiftR, iconSize), *m_backgroundBuffer, sourceIconRect);
//configure
if (m_applet && m_applet->hasConfigurationInterface()) { if (m_applet && m_applet->hasConfigurationInterface()) {
basePoint += step; basePoint += step;
sourceIconRect.translate(0, m_iconSize); sourceIconRect.translate(0, m_iconSize);
@ -339,6 +350,14 @@ void AppletHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
QRectF(basePoint + shiftC, iconSize), *m_backgroundBuffer, sourceIconRect); QRectF(basePoint + shiftC, iconSize), *m_backgroundBuffer, sourceIconRect);
} }
//maximize
if (m_applet && m_applet->hasValidAssociatedApplication()) {
basePoint += step;
sourceIconRect.translate(0, m_iconSize);
painter->drawPixmap(
QRectF(basePoint + shiftMx, iconSize), *m_backgroundBuffer, sourceIconRect);
}
//close //close
basePoint = m_rect.bottomLeft() + QPointF(HANDLE_MARGIN, 0) - step; basePoint = m_rect.bottomLeft() + QPointF(HANDLE_MARGIN, 0) - step;
sourceIconRect.translate(0, m_iconSize); sourceIconRect.translate(0, m_iconSize);
@ -379,6 +398,13 @@ AppletHandle::ButtonType AppletHandle::mapToButton(const QPointF &point) const
} }
} }
if (m_applet && m_applet->hasValidAssociatedApplication()) {
activeArea.translate(step);
if (activeArea.contains(point)) {
return MaximizeButton;
}
}
activeArea.moveTop(m_rect.bottom() - activeArea.height() - iconMargin); activeArea.moveTop(m_rect.bottom() - activeArea.height() - iconMargin);
if (activeArea.contains(point)) { if (activeArea.contains(point)) {
return RemoveButton; return RemoveButton;
@ -512,6 +538,11 @@ void AppletHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
} }
break; break;
} }
case MaximizeButton:
if (m_applet) {
m_applet->runAssociatedApplication();
}
break;
default: default:
break; break;
} }

View File

@ -50,7 +50,8 @@ class AppletHandle : public QObject, public QGraphicsItem
RotateButton, RotateButton,
ConfigureButton, ConfigureButton,
RemoveButton, RemoveButton,
ResizeButton ResizeButton,
MaximizeButton
}; };
AppletHandle(Containment *parent, Applet *applet, const QPointF &hoverPos); AppletHandle(Containment *parent, Applet *applet, const QPointF &hoverPos);

View File

@ -0,0 +1,132 @@
/*
* Copyright 2009 Marco Martin <notmart@gmail.com>
*
* 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 "associatedapplicationmanager_p.h"
#include <QHash>
#include <QFile>
#include <kstandarddirs.h>
#include <krun.h>
#include <kicon.h>
#include "plasma/applet.h"
namespace Plasma
{
class AssociatedApplicationManagerPrivate
{
public:
AssociatedApplicationManagerPrivate()
{
}
~AssociatedApplicationManagerPrivate()
{
}
void cleanupApplet(QObject *obj)
{
Plasma::Applet *applet = static_cast<Plasma::Applet *>(obj);
applicationNames.remove(applet);
urlLists.remove(applet);
}
QHash<const Plasma::Applet *, QString> applicationNames;
QHash<const Plasma::Applet *, KUrl::List> urlLists;
};
class AssociatedApplicationManagerSingleton
{
public:
AssociatedApplicationManager self;
};
K_GLOBAL_STATIC(AssociatedApplicationManagerSingleton, privateAssociatedApplicationManagerSelf)
AssociatedApplicationManager::AssociatedApplicationManager(QObject *parent)
: QObject(parent),
d(new AssociatedApplicationManagerPrivate())
{
}
AssociatedApplicationManager::~AssociatedApplicationManager()
{
delete d;
}
AssociatedApplicationManager *AssociatedApplicationManager::self()
{
return &privateAssociatedApplicationManagerSelf->self;
}
void AssociatedApplicationManager::setApplication(Plasma::Applet *applet, const QString &application)
{
KService::Ptr service = KService::serviceByDesktopName(application);
if (service || !KStandardDirs::findExe(application).isNull() || QFile::exists(application)) {
d->applicationNames[applet] = application;
if (!d->urlLists.contains(applet)) {
connect(applet, SIGNAL(destroyed(QObject *)), this, SLOT(cleanupApplet(QObject *)));
}
}
}
QString AssociatedApplicationManager::application(const Plasma::Applet *applet) const
{
return d->applicationNames.value(applet);
if (!d->applicationNames.contains(applet)) {
connect(applet, SIGNAL(destroyed(QObject *)), this, SLOT(cleanupApplet(QObject *)));
}
}
void AssociatedApplicationManager::setUrls(Plasma::Applet *applet, const KUrl::List &urls)
{
d->urlLists[applet] = urls;
}
KUrl::List AssociatedApplicationManager::urls(const Plasma::Applet *applet) const
{
return d->urlLists.value(applet);
}
void AssociatedApplicationManager::run(Plasma::Applet *applet)
{
if (d->applicationNames.contains(applet)) {
bool success = KRun::run(d->applicationNames.value(applet), d->urlLists.value(applet), 0);
if (!success) {
applet->showMessage(KIcon("application-exit"), i18n("There was an error attempting to exec the associated application with this widget."), ButtonOk);
}
} else if (d->urlLists.contains(applet)) {
KRun *krun = new KRun(d->urlLists.value(applet).first(), 0);
krun->setAutoDelete(true);
}
}
bool AssociatedApplicationManager::appletHasValidAssociatedApplication(const Plasma::Applet *applet) const
{
return (d->applicationNames.contains(applet) || d->urlLists.contains(applet));
}
} // namespace Plasma
#include <moc_associatedapplicationmanager_p.cpp>

View File

@ -0,0 +1,67 @@
/*
* Copyright 2009 Marco Martin <notmart@gmail.com>
*
* 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.
*/
#ifndef PLASMA_ASSOCIATEDAPPLICATIONMANAGER_P_H
#define PLASMA_ASSOCIATEDAPPLICATIONMANAGER_P_H
#include <QObject>
#include <kurl.h>
namespace Plasma
{
class Applet;
class AssociatedApplicationManagerPrivate;
class AssociatedApplicationManager : public QObject
{
Q_OBJECT
public:
static AssociatedApplicationManager *self();
//set an application name for an applet
void setApplication(Plasma::Applet *applet, const QString &application);
//returns the application name associated to an applet
QString application(const Plasma::Applet *applet) const;
//sets the urls associated to an applet
void setUrls(Plasma::Applet *applet, const KUrl::List &urls);
//returns the urls associated to an applet
KUrl::List urls(const Plasma::Applet *applet) const;
//run the associated application or the urls if no app is associated
void run(Plasma::Applet *applet);
//returns true if the applet has a valid associated application or urls
bool appletHasValidAssociatedApplication(const Plasma::Applet *applet) const;
private:
AssociatedApplicationManager(QObject *parent = 0);
~AssociatedApplicationManager();
AssociatedApplicationManagerPrivate *const d;
friend class AssociatedApplicationManagerSingleton;
Q_PRIVATE_SLOT(d, void cleanupApplet(QObject *obj))
};
} // namespace Plasma
#endif // multiple inclusion guard