StelActionMgr.cpp   StelActionMgr.cpp 
skipping to change at line 36 skipping to change at line 36
#include <QMetaProperty> #include <QMetaProperty>
#include <QStringList> #include <QStringList>
#include <QSettings> #include <QSettings>
#ifndef USE_QUICKVIEW #ifndef USE_QUICKVIEW
#include <QAction> #include <QAction>
#include "StelMainView.hpp" #include "StelMainView.hpp"
#endif #endif
StelAction::StelAction(const QString& actionId, StelAction::StelAction(const QString& actionId,
const QString& groupId, const QString& groupId,
const QString& text, const QString& text,
const QString& primaryKey, const QString& primaryKey,
const QString& altKey, const QString& altKey,
bool global): bool global):
QObject(StelApp::getInstance().getStelActionManager()), QObject(StelApp::getInstance().getStelActionManager()),
checkable(false),
checked(false),
group(groupId), group(groupId),
text(text), text(text),
global(global), global(global),
keySequence(primaryKey), keySequence(primaryKey),
altKeySequence(altKey), altKeySequence(altKey),
defaultKeySequence(primaryKey), defaultKeySequence(primaryKey),
defaultAltKeySequence(altKey), defaultAltKeySequence(altKey),
target(NULL), target(NULL),
property(NULL) boolProperty(NULL)
#ifndef USE_QUICKVIEW #ifndef USE_QUICKVIEW
,qAction(NULL) ,qAction(NULL)
#endif #endif
{ {
setObjectName(actionId); setObjectName(actionId);
// Check the global conf for custom shortcuts. // Check the global conf for custom shortcuts.
QSettings* conf = StelApp::getInstance().getSettings(); QSettings* conf = StelApp::getInstance().getSettings();
QString confShortcut = conf->value("shortcuts/" + actionId).toString (); QString confShortcut = conf->value("shortcuts/" + actionId).toString ();
if (!confShortcut.isEmpty()) if (!confShortcut.isEmpty())
{ {
QStringList shortcuts = confShortcut.split(" "); QStringList shortcuts = confShortcut.split(" ");
if (shortcuts.size() > 2) if (shortcuts.size() > 2)
qWarning() << actionId << ": does not support more t han two shortcuts per action"; qWarning() << actionId << ": does not support more t han two shortcuts per action";
skipping to change at line 107 skipping to change at line 105
emit changed(); emit changed();
} }
QString StelAction::getText() const QString StelAction::getText() const
{ {
return q_(text); return q_(text);
} }
void StelAction::setChecked(bool value) void StelAction::setChecked(bool value)
{ {
Q_ASSERT(checkable); Q_ASSERT(boolProperty);
if (value == checked) if (value == isChecked()) //dont do anything if value would not chan
ge
return; return;
checked = value;
if (target) boolProperty->setValue(value);
target->setProperty(property, checked); if(!boolProperty->canNotify())
emit toggled(checked); emit toggled(value); //if connected to a property without NO
TIFY, we have to toggle the event ourselves
} }
void StelAction::toggle() void StelAction::toggle()
{ {
setChecked(!checked); setChecked(!isChecked());
} }
void StelAction::trigger() void StelAction::trigger()
{ {
if (checkable) if (isCheckable())
toggle(); toggle();
else else
{
//We do not call the slot here, but let Qt handle it through
a
//connection established in ::connectToObject from the trigg
ered() signal
//to the slot
//This should still call the target slot first before all ot
her registered slots
//(because it is registered first, see https://doc.qt.io/qt-
4.8/signalsandslots.html#signals).
//This enables the slot to find out the StelAction that was
triggered using sender(),
//and enables use of QSignalMapper or similar constructs
emit triggered(); emit triggered();
}
} }
void StelAction::connectToObject(QObject* obj, const char* slot) void StelAction::connectToObject(QObject* obj, const char* slot)
{ {
target = obj;
QVariant prop = obj->property(slot); QVariant prop = obj->property(slot);
if (prop.isValid()) // Connect to a property. if (prop.isValid()) // Connect to a bool property, use a StelPropert y if possible
{ {
checkable = true; Q_ASSERT(prop.type() == QVariant::Bool);
target = obj;
property = slot;
checked = prop.toBool();
// Listen to the property notified signal if there is one. // Listen to the property notified signal if there is one.
int propIndex = obj->metaObject()->indexOfProperty(slot); int propIndex = obj->metaObject()->indexOfProperty(slot);
QMetaProperty metaProp = obj->metaObject()->property(propInd ex); QMetaProperty metaProp = obj->metaObject()->property(propInd ex);
int slotIndex = metaObject()->indexOfMethod("propertyChanged
(bool)"); //can use a StelProperty for the connection, property name i
if (metaProp.hasNotifySignal()) s action name
connect(obj, metaProp.notifySignal(), this, metaObje boolProperty = StelApp::getInstance().getStelPropertyManager
ct()->method(slotIndex)); ()->registerProperty(objectName(),obj,slot);
StelPropertyBoolProxy* prox = new StelPropertyBoolProxy(bool
Property,this);
connect(prox,SIGNAL(propertyChanged(bool)),this,SLOT(propert
yChanged(bool)));
if(!metaProp.hasNotifySignal())
{
//warn about missing notify
qWarning()<<"[StelAction]"<<getId()<<"is connected t
o property"<<slot<<"of object"<<obj<<" without a NOTIFY signal";
}
return; return;
} }
//now, check if its a slot
int slotIndex = obj->metaObject()->indexOfMethod(slot); int slotIndex = obj->metaObject()->indexOfMethod(slot);
if (obj->metaObject()->method(slotIndex).parameterCount() == 1) #ifndef NDEBUG
{ if (slotIndex<0) qWarning()<<"[StelAction]"<<getId()<<"cannot connec
// connect to a boolean slot. t to slot"<<slot<<"of object"<<obj << "- EXIT!";
Q_ASSERT(obj->metaObject()->method(slotIndex).parameterType( #endif
0) == QMetaType::Bool); Q_ASSERT(slotIndex>=0);
checkable = true; // connect to a parameterless slot.
int signalIndex = metaObject()->indexOfMethod("toggled(bool) this->slot = obj->metaObject()->method(slotIndex);
"); Q_ASSERT(this->slot.parameterCount() == 0);
connect(this, metaObject()->method(signalIndex), obj, obj->m //let Qt handle invoking the slot when the StelAction is triggered
etaObject()->method(slotIndex)); int signalIndex = metaObject()->indexOfSignal("triggered()");
} connect(this,metaObject()->method(signalIndex),obj,this->slot);
else
{
// connect to a slot.
Q_ASSERT(obj->metaObject()->method(slotIndex).parameterCount
() == 0);
checkable = false;
int signalIndex = metaObject()->indexOfMethod("triggered()")
;
connect(this, metaObject()->method(signalIndex), obj, obj->m
etaObject()->method(slotIndex));
}
emit changed(); emit changed();
} }
void StelAction::propertyChanged(bool value) void StelAction::propertyChanged(bool value)
{ {
if (value == checked) emit toggled(value);
return;
checked = value;
emit toggled(checked);
} }
QKeySequence::SequenceMatch StelAction::matches(const QKeySequence& seq) co nst QKeySequence::SequenceMatch StelAction::matches(const QKeySequence& seq) co nst
{ {
Q_ASSERT(QKeySequence::PartialMatch > QKeySequence::NoMatch); Q_ASSERT(QKeySequence::PartialMatch > QKeySequence::NoMatch);
Q_ASSERT(QKeySequence::ExactMatch > QKeySequence::PartialMatch); Q_ASSERT(QKeySequence::ExactMatch > QKeySequence::PartialMatch);
return qMax((!keySequence.isEmpty() ? keySequence.matches(seq) : QKe ySequence::NoMatch), return qMax((!keySequence.isEmpty() ? keySequence.matches(seq) : QKe ySequence::NoMatch),
(!altKeySequence.isEmpty() ? altKeySequence. matches(seq) : QKeySequence::NoMatch)); (!altKeySequence.isEmpty() ? altKeySequence.matches(seq) : QKeySequence::NoMatch));
} }
StelActionMgr::StelActionMgr() : StelActionMgr::StelActionMgr() :
actionsEnabled(true) actionsEnabled(true)
{ {
} }
StelActionMgr::~StelActionMgr() StelActionMgr::~StelActionMgr()
{ {
} }
StelAction* StelActionMgr::addAction(const QString& id, const QString& grou pId, const QString& text, StelAction* StelActionMgr::addAction(const QString& id, const QString& grou pId, const QString& text,
QObject* target, const char* slot, QObject* target, const char* slot,
const QString& shortcut, const QString const QString& shortcut, const QString&
& altShortcut, altShortcut,
bool global) bool global)
{ {
StelAction* action = new StelAction(id, groupId, text, shortcut, alt Shortcut, global); StelAction* action = new StelAction(id, groupId, text, shortcut, alt Shortcut, global);
connect(action,SIGNAL(toggled(bool)),this,SLOT(onStelActionToggled(b ool)));
action->connectToObject(target, slot); action->connectToObject(target, slot);
return action; return action;
} }
StelAction* StelActionMgr::findAction(const QString& id) StelAction* StelActionMgr::findAction(const QString& id)
{ {
return findChild<StelAction*>(id); return findChild<StelAction*>(id);
} }
bool StelActionMgr::pushKey(int key, bool global) bool StelActionMgr::pushKey(int key, bool global)
{ {
if (!actionsEnabled) if (!actionsEnabled)
return false; return false;
keySequence << key; keySequence << key;
QKeySequence sequence(keySequence.size() > 0 ? keySequence[0] : 0, QKeySequence sequence(keySequence.size() > 0 ? keySequence[0] : 0,
keySequence.size() > 1 ? k keySequence.size() > 1 ? keySequence[1] : 0,
eySequence[1] : 0, keySequence.size() > 2 ? keySequence[2] : 0,
keySequence.size() > 2 ? k keySequence.size() > 3 ? keySequence[3] : 0);
eySequence[2] : 0,
keySequence.size() > 3 ? k
eySequence[3] : 0);
bool hasPartialMatch = false; bool hasPartialMatch = false;
foreach(StelAction* action, findChildren<StelAction*>()) foreach(StelAction* action, findChildren<StelAction*>())
{ {
if (global && !action->global) continue; if (global && !action->global) continue;
QKeySequence::SequenceMatch match = action->matches(sequence ); QKeySequence::SequenceMatch match = action->matches(sequence );
if (match == QKeySequence::ExactMatch) if (match == QKeySequence::ExactMatch)
{ {
keySequence.clear(); keySequence.clear();
action->trigger(); action->trigger();
return true; return true;
skipping to change at line 256 skipping to change at line 266
{ {
QList<StelAction*> ret; QList<StelAction*> ret;
foreach(StelAction* action, findChildren<StelAction*>()) foreach(StelAction* action, findChildren<StelAction*>())
{ {
if (action->group == group) if (action->group == group)
ret.append(action); ret.append(action);
} }
return ret; return ret;
} }
QList<StelAction*> StelActionMgr::getActionList() const
{
return findChildren<StelAction*>();
}
void StelActionMgr::saveShortcuts() void StelActionMgr::saveShortcuts()
{ {
QSettings* conf = StelApp::getInstance().getSettings(); QSettings* conf = StelApp::getInstance().getSettings();
conf->beginGroup("shortcuts"); conf->beginGroup("shortcuts");
conf->remove(""); conf->remove("");
foreach(StelAction* action, findChildren<StelAction*>()) foreach(StelAction* action, findChildren<StelAction*>())
{ {
if ( action->keySequence == action->defaultKeySequence && if ( action->keySequence == action->defaultKeySequence &&
action->altKeySequence == action->defaultAlt KeySequence) action->altKeySequence == action->defaultAlt KeySequence)
continue; continue;
skipping to change at line 284 skipping to change at line 299
void StelActionMgr::restoreDefaultShortcuts() void StelActionMgr::restoreDefaultShortcuts()
{ {
foreach(StelAction* action, findChildren<StelAction*>()) foreach(StelAction* action, findChildren<StelAction*>())
{ {
action->keySequence = action->defaultKeySequence; action->keySequence = action->defaultKeySequence;
action->altKeySequence = action->defaultAltKeySequence; action->altKeySequence = action->defaultAltKeySequence;
emit action->changed(); emit action->changed();
} }
saveShortcuts(); saveShortcuts();
} }
void StelActionMgr::onStelActionToggled(bool val)
{
StelAction* action = qobject_cast<StelAction*>(sender());
emit actionToggled(action->getId(),val);
}
 End of changes. 22 change blocks. 
66 lines changed or deleted 83 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/