API review stuff

-have one virtual QEvent function
-move eventToString to ContextAction
-remove needsConfig reason
-remove configurationAction
-remove dataengine stuff

svn path=/trunk/KDE/kdelibs/; revision=1012642
This commit is contained in:
Chani Armitage 2009-08-17 22:30:58 +00:00
parent d82fe71821
commit 464df207d1
4 changed files with 80 additions and 143 deletions

View File

@ -26,7 +26,6 @@
#include <QFile>
#include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsView>
#include <QMetaEnum>
#include <QMimeData>
#include <QPainter>
#include <QStyleOptionGraphicsItem>
@ -526,7 +525,7 @@ void Containment::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
event->ignore();
QString trigger = eventToString(event);
QString trigger = ContextAction::eventToString(event);
//FIXME what if someone changes the modifiers before the mouseup?
if (d->contextActions.contains(trigger)) {
@ -556,7 +555,7 @@ void Containment::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
event->ignore();
QString trigger = eventToString(event);
QString trigger = ContextAction::eventToString(event);
if (d->contextActions.contains(trigger)) {
if (d->prepareContextAction(trigger, event->screenPos())) {
@ -622,9 +621,11 @@ void ContainmentPrivate::containmentActions(KMenu &desktopMenu)
QString trigger = "RightButton;NoModifier";
//get base context actions
if (ContextAction *cAction = contextActions.value(trigger)) {
if (QAction *a = cAction->configurationAction()) {
if (cAction->configurationRequired()) {
//it needs configuring
desktopMenu.addAction(a);
//FIXME the text could be better.
desktopMenu.addTitle(i18n("This plugin needs to be configured"));
desktopMenu.addAction(q->action("configure"));
} else {
QList<QAction*> actions = cAction->contextualActions();
if (actions.isEmpty()) {
@ -1433,11 +1434,11 @@ void Containment::keyPressEvent(QKeyEvent *event)
void Containment::wheelEvent(QGraphicsSceneWheelEvent *event)
{
QString trigger = eventToString(event);
QString trigger = ContextAction::eventToString(event);
if (d->contextActions.contains(trigger)) {
if (d->prepareContextAction(trigger, event->screenPos())) {
d->contextActions.value(trigger)->wheelEvent(event);
d->contextActions.value(trigger)->contextEvent(event);
}
event->accept();
return;
@ -2274,74 +2275,16 @@ bool ContainmentPrivate::prepareContextAction(const QString &trigger, const QPoi
action->restore(actionConfig);
}
if (QAction *a = action->configurationAction()) {
if (action->configurationRequired()) {
KMenu menu;
menu.addAction(a);
menu.addTitle(i18n("This plugin needs to be configured"));
menu.addAction(q->action("configure"));
menu.exec(screenPos);
return false;
}
return true;
}
QString Containment::eventToString(QEvent *event)
{
QString trigger;
Qt::KeyboardModifiers modifiers;
//strict typing sucks sometimes.
switch (event->type()) {
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
{
QMouseEvent *e = dynamic_cast<QMouseEvent*>(event);
int m = QObject::staticQtMetaObject.indexOfEnumerator("MouseButtons");
QMetaEnum mouse = QObject::staticQtMetaObject.enumerator(m);
trigger += mouse.valueToKey(e->button());
modifiers = e->modifiers();
break;
}
case QEvent::GraphicsSceneMousePress:
case QEvent::GraphicsSceneMouseRelease:
{
QGraphicsSceneMouseEvent *e = dynamic_cast<QGraphicsSceneMouseEvent*>(event);
int m = QObject::staticQtMetaObject.indexOfEnumerator("MouseButtons");
QMetaEnum mouse = QObject::staticQtMetaObject.enumerator(m);
trigger += mouse.valueToKey(e->button());
modifiers = e->modifiers();
break;
}
case QEvent::Wheel:
{
QWheelEvent *e = dynamic_cast<QWheelEvent*>(event);
int o = QObject::staticQtMetaObject.indexOfEnumerator("Orientations");
QMetaEnum orient = QObject::staticQtMetaObject.enumerator(o);
trigger = "wheel:";
trigger += orient.valueToKey(e->orientation());
modifiers = e->modifiers();
break;
}
case QEvent::GraphicsSceneWheel:
{
QGraphicsSceneWheelEvent *e = dynamic_cast<QGraphicsSceneWheelEvent*>(event);
int o = QObject::staticQtMetaObject.indexOfEnumerator("Orientations");
QMetaEnum orient = QObject::staticQtMetaObject.enumerator(o);
trigger = "wheel:";
trigger += orient.valueToKey(e->orientation());
modifiers = e->modifiers();
break;
}
default:
return QString();
}
int k = QObject::staticQtMetaObject.indexOfEnumerator("KeyboardModifiers");
QMetaEnum kbd = QObject::staticQtMetaObject.enumerator(k);
trigger += ";";
trigger += kbd.valueToKeys(modifiers);
return trigger;
}
} // Plasma namespace

View File

@ -386,13 +386,6 @@ class PLASMA_EXPORT Containment : public Applet
*/
ContextAction *contextAction(QString trigger);
/**
* Turns a mouse or wheel event into a string suitable for a ContextAction
* @return the string representation of the event
* @since 4.3
*/
static QString eventToString(QEvent *event);
Q_SIGNALS:
/**
* This signal is emitted when a new applet is created by the containment

View File

@ -21,7 +21,11 @@
#include "contextaction.h"
#include "containment.h"
#include <QAction>
#include <QMetaEnum>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneWheelEvent>
#include <kdebug.h>
#include <kglobal.h>
@ -30,7 +34,6 @@
#include <version.h>
#include "plasma/private/dataengineconsumer_p.h"
#include "plasma/private/packages_p.h"
#include "plasma/private/contextaction_p.h"
@ -187,12 +190,7 @@ void ContextAction::configurationAccepted()
//do nothing by default
}
void ContextAction::contextEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event)
}
void ContextAction::wheelEvent(QGraphicsSceneWheelEvent *event)
void ContextAction::contextEvent(QEvent *event)
{
Q_UNUSED(event)
}
@ -203,39 +201,74 @@ QList<QAction*> ContextAction::contextualActions()
return QList<QAction*>();
}
DataEngine *ContextAction::dataEngine(const QString &name) const
{
return d->dataEngine(name);
}
bool ContextAction::configurationRequired() const
{
return d->needsConfig;
}
QAction *ContextAction::configurationAction()
void ContextAction::setConfigurationRequired(bool needsConfig)
{
if (d->needsConfig) {
//create the "I need configuring" action
QAction *action = new QAction(i18n("This plugin needs to be configured"), this);
//TODO name/reason?
//TODO connect it to something
return action;
}
return NULL;
//TODO: reason?
d->needsConfig = needsConfig;
}
void ContextAction::setConfigurationRequired(bool needsConfig, const QString &reason)
QString ContextAction::eventToString(QEvent *event)
{
//TODO: implement something for reason. first, we need to decide where/how
// to communicate it to the user
Q_UNUSED(reason)
QString trigger;
Qt::KeyboardModifiers modifiers;
if (d->needsConfig == needsConfig) {
return;
//strict typing sucks sometimes.
switch (event->type()) {
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
{
QMouseEvent *e = dynamic_cast<QMouseEvent*>(event);
int m = QObject::staticQtMetaObject.indexOfEnumerator("MouseButtons");
QMetaEnum mouse = QObject::staticQtMetaObject.enumerator(m);
trigger += mouse.valueToKey(e->button());
modifiers = e->modifiers();
break;
}
case QEvent::GraphicsSceneMousePress:
case QEvent::GraphicsSceneMouseRelease:
{
QGraphicsSceneMouseEvent *e = dynamic_cast<QGraphicsSceneMouseEvent*>(event);
int m = QObject::staticQtMetaObject.indexOfEnumerator("MouseButtons");
QMetaEnum mouse = QObject::staticQtMetaObject.enumerator(m);
trigger += mouse.valueToKey(e->button());
modifiers = e->modifiers();
break;
}
case QEvent::Wheel:
{
QWheelEvent *e = dynamic_cast<QWheelEvent*>(event);
int o = QObject::staticQtMetaObject.indexOfEnumerator("Orientations");
QMetaEnum orient = QObject::staticQtMetaObject.enumerator(o);
trigger = "wheel:";
trigger += orient.valueToKey(e->orientation());
modifiers = e->modifiers();
break;
}
case QEvent::GraphicsSceneWheel:
{
QGraphicsSceneWheelEvent *e = dynamic_cast<QGraphicsSceneWheelEvent*>(event);
int o = QObject::staticQtMetaObject.indexOfEnumerator("Orientations");
QMetaEnum orient = QObject::staticQtMetaObject.enumerator(o);
trigger = "wheel:";
trigger += orient.valueToKey(e->orientation());
modifiers = e->modifiers();
break;
}
default:
return QString();
}
d->needsConfig = needsConfig;
int k = QObject::staticQtMetaObject.indexOfEnumerator("KeyboardModifiers");
QMetaEnum kbd = QObject::staticQtMetaObject.enumerator(k);
trigger += ";";
trigger += kbd.valueToKeys(modifiers);
return trigger;
}
} // Plasma namespace

View File

@ -34,7 +34,6 @@ class QAction;
namespace Plasma
{
class DataEngine;
class Containment;
class ContextActionPrivate;
@ -159,20 +158,12 @@ class PLASMA_EXPORT ContextAction : public QObject
virtual void configurationAccepted();
/**
* Implement this to respond to a mouse button event.
* Implement this to respond to events.
* The user can configure whatever button and modifier they like, so please don't look at
* those parameters.
* @param event the mouse event object
* So far the event could be a QGraphicsSceneMouseEvent or a QGraphicsSceneWheelEvent.
*/
virtual void contextEvent(QGraphicsSceneMouseEvent *event);
/**
* Implement this to respond to a wheel event.
* The user can configure which wheel and whatever modifier they like, so please don't look at
* those parameters.
* @param event the mousewheel event object
*/
virtual void wheelEvent(QGraphicsSceneWheelEvent *event);
virtual void contextEvent(QEvent *event);
/**
* Implement this to provide a list of actions that can be added to another menu
@ -181,26 +172,6 @@ class PLASMA_EXPORT ContextAction : public QObject
*/
virtual QList<QAction*> contextualActions();
/**
* Loads the given DataEngine
*
* Tries to load the data engine given by @p name. Each engine is
* only loaded once, and that instance is re-used on all subsequent
* requests.
*
* If the data engine was not found, an invalid data engine is returned
* (see DataEngine::isValid()).
*
* Note that you should <em>not</em> delete the returned engine.
*
* @param name Name of the data engine to load
* @return pointer to the data engine if it was loaded,
* or an invalid data engine if the requested engine
* could not be loaded
*
*/
Q_INVOKABLE DataEngine *dataEngine(const QString &name) const;
/**
* @return true if the contextaction currently needs to be configured,
* otherwise, false
@ -208,10 +179,10 @@ class PLASMA_EXPORT ContextAction : public QObject
bool configurationRequired() const;
/**
* @return a config action if the contextaction currently needs to be configured,
* otherwise, null
* Turns a mouse or wheel event into a string suitable for a ContextAction
* @return the string representation of the event
*/
QAction *configurationAction();
static QString eventToString(QEvent *event);
protected:
/**
@ -239,11 +210,8 @@ class PLASMA_EXPORT ContextAction : public QObject
*
* @param needsConfiguring true if the applet needs to be configured,
* or false if it doesn't
* @param reason a translated message for the user explaining that the
* applet needs configuring; this should note what needs
* to be configured
*/
void setConfigurationRequired(bool needsConfiguring, const QString &reason = QString());
void setConfigurationRequired(bool needsConfiguring);
/**
* return the containment the plugin is associated with, if any.