summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/citizenquartz/propertyaddstate.cpp
blob: dd2394896de92255963f7123fe24124551fbe7b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "PropertyAddState.h"
#include "timeperiod.h"

#include <QDateTime>
#include <QDebug>

PropertyAddState::PropertyAddState(QState *parent)
    : QState(parent)
{
}

void PropertyAddState::addToProperty(QObject *object, const char *propertyName, 
                                      const QVariant &valueToAdd)
{
    m_propertyAdditions.append(PropertyAdder(object, propertyName, valueToAdd));
}

QVariant PropertyAddState::addProperties(const QVariant &current, const QVariant &toAdd) const
{
    QVariant result;
    switch (current.type()) {
    case QVariant::DateTime:
        result = current.toDateTime() + qvariant_cast<TimePeriod>(toAdd);
        break;                                    
    case QVariant::Time:
        result = current.toTime() + qvariant_cast<TimePeriod>(toAdd);
        break;
    default:
        qWarning("PropertyAddState::addProperties: QVariant type '%s' not supported", 
                 current.typeName());
    };

    return result;
}

void PropertyAddState::onEntry()
{
    foreach (PropertyAdder propertyAdder, m_propertyAdditions) {
        QObject *object = propertyAdder.object;
        QByteArray propertyName = propertyAdder.propertyName;
        QVariant toAdd = propertyAdder.valueToAdd;
        QVariant current = object->property(propertyName);

        object->setProperty(propertyName, addProperties(current, toAdd));
    }
}