#include #include "sys_net.h" #include "allocate.h" #include "marshal.h" #define dprintf(fmt, args...) IOStream_print(iostdout, "[DEBUG] %s" fmt, __FUNCTION__, ##args) #define wprintf(fmt, args...) IOStream_print(iostderr, "[WARN] %s" fmt, __FUNCTION__, ##args) #define iprintf(fmt, args...) IOStream_print(iostdout, "[INFO] %s" fmt, __FUNCTION__, ##args) #define eprintf(fmt, args...) IOStream_print(iostderr, "[ERROR] %s" fmt, __FUNCTION__, ##args) #define ARRAY_SIZE(ary) (sizeof(ary)/sizeof((ary)[0])) /* Messages are coded as msgid followed by message fields. * Initial message on any channel is hello - so can check version * compatibility. * * char* -> uint16_t:n * ints/uints go as suitable number of bytes (e.g. uint16_t is 2 bytes). * optional fields go as '1' or '0' (the 0/1 is 1 byte). * lists go as ('1' )* '0' */ int marshal_flush(IOStream *io){ int err = 0; err = IOStream_flush(io); return err; } int marshal_bytes(IOStream *io, void *s, uint32_t s_n){ int err = 0; int n; n = IOStream_write(io, s, s_n); if(n < 0){ err = n; } else if (n < s_n){ wprintf("> Wanted %d, got %d\n", s_n, n); err = -EIO; } return err; } int unmarshal_bytes(IOStream *io, void *s, uint32_t s_n){ int err = 0; int n; //dprintf("> s_n=%d\n", s_n); n = IOStream_read(io, s, s_n); //dprintf("> n=%d\n", n); if(n < 0){ err = n; } else if(n < s_n){ wprintf("> Wanted %d, got %d\n", s_n, n); err = -EIO; } //dprintf("< err=%d\n", err); return err; } int marshal_uint8(IOStream *io, uint8_t x){ return marshal_bytes(io, &x, sizeof(x)); } int unmarshal_uint8(IOStream *io, uint8_t *x){ return unmarshal_bytes(io, x, sizeof(*x)); } int marshal_uint16(IOStream *io, uint16_t x){ x = htons(x); return marshal_bytes(io, &x, sizeof(x)); } int unmarshal_uint16(IOStream *io, uint16_t *x){ int err = 0; err = unmarshal_bytes(io, x, sizeof(*x)); *x = ntohs(*x); return err; } int marshal_int32(IOStream *io, int32_t x){ int err = 0; //dprintf("> x=%d\n", x); x = htonl(x); err = marshal_bytes(io, &x, sizeof(x)); //dprintf("< err=%d\n", err); return err; } int unmarshal_int32(IOStream *io, int32_t *x){ int err = 0; //dprintf(">\n"); err = unmarshal_bytes(io, x, sizeof(*x)); *x = ntohl(*x); //dprintf("< err=%d x=%d\n", err, *x); return err; } int marshal_uint32(IOStream *io, uint32_t x){ int err = 0; //dprintf("> x=%u\n", x); x = htonl(x); err = marshal_bytes(io, &x, sizeof(x)); //dprintf("< err=%d\n", err); return err; } int unmarshal_uint32(IOStream *io, uint32_t *x){ int err = 0; //dprintf(">\n"); err = unmarshal_bytes(io, x, sizeof(*x)); *x = ntohl(*x); //dprintf("< err=%d x=%u\n", err, *x); return err; } int marshal_uint64(IOStream *io, uint64_t x){ int err; err = marshal_uint32(io, (uint32_t) ((x >> 32) & 0xffffffff)); if(err) goto exit; err = marshal_uint32(io, (uint32_t) ( x & 0xffffffff)); exit: return err; } int unmarshal_uint64(IOStream *io, uint64_t *x){ int err = 0; uint32_t hi, lo; err = unmarshal_uint32(io, &hi); if(err) goto exit; err = unmarshal_uint32(io, &lo); *x = (((uint64_t) hi) << 32) | lo; exit: return err; } int marshal_net16(IOStream *io, net16_t x){ return marshal_bytes(io, &x, sizeof(x)); } int unmarshal_net16(IOStream *io, net16_t *x){ int err = 0; err = unmarshal_bytes(io, x, sizeof(*x)); return err; } int marshal_net32(IOStream *io, net32_t x){ return marshal_bytes(io, &x, sizeof(x)); } int unmarshal_net32(IOStream *io, net32_t *x){ int err = 0; err = unmarshal_bytes(io, x, sizeof(*x)); return err; } int marshal_string(IOStream *io, char *s, uint32_t s_n){ int err; //dprintf("> s=%s\n", s); err = marshal_uint32(io, s_n); if(err) goto exit; err = marshal_bytes(io, s, s_n); exit: //dprintf("< err=%d\n", err); return err; } int unmarshal_string(IOStream *io, char *s, uint32_t s_n){ int err = 0, val_n = 0; //dprintf(">\n"); err = unmarshal_uint32(io, &val_n); if(err) goto exit; if(val_n >= s_n){ err = -EINVAL; goto exit; } err = unmarshal_bytes(io, s, val_n); if(err) goto exit; s[val_n] = '\0'; exit: //dprintf("< err=%d s=%s\n", err, s); return err; } int unmarshal_new_string(IOStream *io, char **s, uint32_t *s_n){ int err = 0, val_n = 0; char *val = NULL; //dprintf(">\n"); err = unmarshal_uint32(io, &val_n); if(err) goto exit; val = allocate(val_n + 1); if(!val){ err = -ENOMEM; goto exit; } err = unmarshal_bytes(io, val, val_n); if(err) goto exit; val[val_n] = '\0'; exit: if(err){ if(val) deallocate(val); val = NULL; val_n = 0; } *s = val; if(s_n) *s_n = val_n; //dprintf("< err=%d s=%s\n", err, *s); return err; } href='#n42'>42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of a Qt Solutions component.
**
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
**     the names of its contributors may be used to endorse or promote
**     products derived from this software without specific prior written
**     permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
****************************************************************************/

#include <QApplication>
#include "qtvariantproperty.h"
#include "qteditorfactory.h"
#include "qttreepropertybrowser.h"

class VariantManager : public QtVariantPropertyManager
{
    Q_OBJECT
public:
    VariantManager(QObject *parent = 0);
    ~VariantManager();

    virtual QVariant value(const QtProperty *property) const;
    virtual int valueType(int propertyType) const;
    virtual bool isPropertyTypeSupported(int propertyType) const;

    QString valueText(const QtProperty *property) const;

public slots:
    virtual void setValue(QtProperty *property, const QVariant &val);
protected:
    virtual void initializeProperty(QtProperty *property);
    virtual void uninitializeProperty(QtProperty *property);
private slots:
    void slotValueChanged(QtProperty *property, const QVariant &value);
    void slotPropertyDestroyed(QtProperty *property);
private:
    struct Data {
        QVariant value;
        QtVariantProperty *x;
        QtVariantProperty *y;
    };
    QMap<const QtProperty *, Data> propertyToData;
    QMap<const QtProperty *, QtProperty *> xToProperty;
    QMap<const QtProperty *, QtProperty *> yToProperty;
};

VariantManager::VariantManager(QObject *parent)
    : QtVariantPropertyManager(parent)
{
    connect(this, SIGNAL(valueChanged(QtProperty *, const QVariant &)),
                this, SLOT(slotValueChanged(QtProperty *, const QVariant &)));
    connect(this, SIGNAL(propertyDestroyed(QtProperty *)),
                this, SLOT(slotPropertyDestroyed(QtProperty *)));
}

VariantManager::~VariantManager()
{

}

void VariantManager::slotValueChanged(QtProperty *property, const QVariant &value)
{
    if (xToProperty.contains(property)) {
        QtProperty *pointProperty = xToProperty[property];
        QVariant v = this->value(pointProperty);
        QPointF p = v.value<QPointF>();
        p.setX(value.value<double>());
        setValue(pointProperty, p);
    } else if (yToProperty.contains(property)) {
        QtProperty *pointProperty = yToProperty[property];
        QVariant v = this->value(pointProperty);
        QPointF p = v.value<QPointF>();
        p.setY(value.value<double>());
        setValue(pointProperty, p);
    }
}

void VariantManager::slotPropertyDestroyed(QtProperty *property)
{
    if (xToProperty.contains(property)) {
        QtProperty *pointProperty = xToProperty[property];
        propertyToData[pointProperty].x = 0;
        xToProperty.remove(property);
    } else if (yToProperty.contains(property)) {
        QtProperty *pointProperty = yToProperty[property];
        propertyToData[pointProperty].y = 0;
        yToProperty.remove(property);
    }
}

bool VariantManager::isPropertyTypeSupported(int propertyType) const
{
    if (propertyType == QVariant::PointF)
        return true;
    return QtVariantPropertyManager::isPropertyTypeSupported(propertyType);
}

int VariantManager::valueType(int propertyType) const
{
    if (propertyType == QVariant::PointF)
        return QVariant::PointF;
    return QtVariantPropertyManager::valueType(propertyType);
}

QVariant VariantManager::value(const QtProperty *property) const
{
    if (propertyToData.contains(property))
        return propertyToData[property].value;
    return QtVariantPropertyManager::value(property);
}

QString VariantManager::valueText(const QtProperty *property) const
{
    if (propertyToData.contains(property)) {
        QVariant v = propertyToData[property].value;
        QPointF p = v.value<QPointF>();
        return QString(tr("(%1, %2)").arg(QString::number(p.x()))
                                 .arg(QString::number(p.y())));
    }
    return QtVariantPropertyManager::valueText(property);
}

void VariantManager::setValue(QtProperty *property, const QVariant &val)
{
    if (propertyToData.contains(property)) {
        if (val.type() != QVariant::PointF && !val.canConvert(QVariant::PointF))
            return;
        QPointF p = val.value<QPointF>();
        Data d = propertyToData[property];
        d.value = p;
        if (d.x)
            d.x->setValue(p.x());
        if (d.y)
            d.y->setValue(p.y());
        propertyToData[property] = d;
        emit propertyChanged(property);
        emit valueChanged(property, p);
        return;
    }
    QtVariantPropertyManager::setValue(property, val);
}

void VariantManager::initializeProperty(QtProperty *property)
{
    if (propertyType(property) == QVariant::PointF) {
        Data d;

        d.value = QPointF(0, 0);

        VariantManager *that = (VariantManager *)this;

        d.x = that->addProperty(QVariant::Double);
        d.x->setPropertyName(tr("Position X"));
        property->addSubProperty(d.x);
        xToProperty[d.x] = property;

        d.y = that->addProperty(QVariant::Double);
        d.y->setPropertyName(tr("Position Y"));
        property->addSubProperty(d.y);
        yToProperty[d.y] = property;

        propertyToData[property] = d;
    }
    QtVariantPropertyManager::initializeProperty(property);
}

void VariantManager::uninitializeProperty(QtProperty *property)
{
    if (propertyToData.contains(property)) {
        Data d = propertyToData[property];
        if (d.x)
            xToProperty.remove(d.x);
        if (d.y)
            yToProperty.remove(d.y);
        propertyToData.remove(property);
    }
    QtVariantPropertyManager::uninitializeProperty(property);
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    VariantManager *variantManager = new VariantManager();

    QtVariantProperty *item = variantManager->addProperty(QVariant::PointF,
                "PointF Property");
    item->setValue(QPointF(2.5, 13.13));

    QtVariantEditorFactory *variantFactory = new QtVariantEditorFactory();

    QtTreePropertyBrowser ed1;
    QtVariantPropertyManager *varMan = variantManager;
    ed1.setFactoryForManager(varMan, variantFactory);
    ed1.addProperty(item);


    ed1.show();

    int ret = app.exec();

    delete variantFactory;
    delete variantManager;

    return ret;
}

#include "main.moc"