Exemple #1
0
template <> QDate Conversion<QDate>::from(RubyValue x)
{
    int y = x.send(RUBYQML_INTERN("year")).to<int>();
    int m = x.send(RUBYQML_INTERN("month")).to<int>();
    int d = x.send(RUBYQML_INTERN("day")).to<int>();
    return QDate(y, m, d);
}
Exemple #2
0
template <> QRectF Conversion<QRectF>::from(RubyValue value)
{
    auto x = value.send(RUBYQML_INTERN("x")).to<double>();
    auto y = value.send(RUBYQML_INTERN("y")).to<double>();
    auto w = value.send(RUBYQML_INTERN("width")).to<double>();
    auto h = value.send(RUBYQML_INTERN("height")).to<double>();
    return QRectF(QPointF(x, y), QSizeF(w, h));
}
Exemple #3
0
template <> RubyValue Conversion<QRectF>::to(const QRectF &rect)
{
    static auto klass = RubyClass::fromPath("QML::Geometry::Rectangle");
    return klass.toValue().send(RUBYQML_INTERN("new"),
                                RubyValue::from(rect.x()), RubyValue::from(rect.y()),
                                RubyValue::from(rect.width()), RubyValue::from(rect.height()));
}
Exemple #4
0
template <> QDateTime Conversion<QDateTime>::from(RubyValue time)
{
    int offset;

    if (time.isKindOf(rb_cTime)) {
        offset = time.send(RUBYQML_INTERN("gmt_offset")).to<int>();
    } else { // DateTime
        VALUE dayOffset = time.send(RUBYQML_INTERN("offset"));
        offset = RubyValue(RRATIONAL(dayOffset)->num).to<int>() * 24 * 60 * 60 / RubyValue(RRATIONAL(dayOffset)->den).to<int>();
        time = time.send(RUBYQML_INTERN("to_time"));
    }
    timeval at = rb_time_timeval(time);

    QDateTime dateTime;
    dateTime.setOffsetFromUtc(offset);
    dateTime.setMSecsSinceEpoch(at.tv_sec * 1000 + at.tv_usec / 1000);
    return dateTime;
}
WeakValueReference::WeakValueReference(RubyValue value) :
    d(std::make_shared<Data>())
{
    d->mHasValue = true;
    d->mValue = value;
    static auto objspace = RubyModule::fromPath("ObjectSpace");
    protect([&] {
        auto proc = rb_proc_new((VALUE (*)(...))&Data::finalize, Ext_AnyWrapper::create(QVariant::fromValue(d)));
        VALUE args[] = { value };
        rb_funcall_with_block(objspace, RUBYQML_INTERN("define_finalizer"), 1, args, proc);
    });
}
Exemple #6
0
int ListModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) {
        return 0;
    }
    int count;
    withGvl([&] {
        rescueNotify([&] {
            count = NUM2INT(rb_funcall(mRubyModel, RUBYQML_INTERN("count"), 0));
        });
    });
    return count;
}
Exemple #7
0
QVariant ListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    QVariant result;
    withGvl([&] {
        RubyValue value;
        rescueNotify([&] {
            auto subscribe = RUBYQML_INTERN("[]");
            auto record = rb_funcall(mRubyModel, subscribe, 1, INT2NUM(index.row()));
            value = rb_funcall(record, subscribe, 1, ID2SYM(mColumnIDs[role]));
        });
        result = value.toVariant();
    });
    return result;
}
Exemple #8
0
template <> RubyValue Conversion<QSizeF>::to(const QSizeF &size)
{
    static auto klass = RubyClass::fromPath("QML::Geometry::Size");
    return klass.toValue().send(RUBYQML_INTERN("new"), RubyValue::from(size.width()), RubyValue::from(size.height()));
}
Exemple #9
0
template <> QSizeF Conversion<QSizeF>::from(RubyValue value)
{
    auto width = value.send(RUBYQML_INTERN("width")).to<double>();
    auto height = value.send(RUBYQML_INTERN("height")).to<double>();
    return QSizeF(width, height);
}
Exemple #10
0
template <> RubyValue Conversion<QPointF>::to(const QPointF &point)
{
    static auto klass = RubyClass::fromPath("QML::Geometry::Point");
    return klass.toValue().send(RUBYQML_INTERN("new"), RubyValue::from(point.x()), RubyValue::from(point.y()));
}
Exemple #11
0
template <> QPointF Conversion<QPointF>::from(RubyValue value)
{
    auto x = value.send(RUBYQML_INTERN("x")).to<double>();
    auto y = value.send(RUBYQML_INTERN("y")).to<double>();
    return QPointF(x, y);
}
Exemple #12
0
template <> RubyValue Conversion<QDate>::to(const QDate &date)
{
    static auto klass = RubyClass::fromPath("Date");
    return klass.toValue().send(RUBYQML_INTERN("new"), RubyValue::from(date.year()), RubyValue::from(date.month()), RubyValue::from(date.day()));
}
Exemple #13
0
template <> RubyValue Conversion<QDateTime>::to(const QDateTime &dateTime)
{
    RubyValue sec = rb_rational_new(RubyValue::from(dateTime.toMSecsSinceEpoch()), RubyValue::from(1000));
    RubyValue time = rb_time_num_new(sec, RubyValue::from(dateTime.offsetFromUtc()));
    return time.send(RUBYQML_INTERN("to_datetime"));
}