All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.qt.core.QBindable Maven / Gradle / Ivy

/****************************************************************************
**
** Copyright (C) 2009-2024 Dr. Peter Droste, Omix Visualization GmbH & Co. KG. All rights reserved.
**
** This file is part of Qt Jambi.
**
** ** $BEGIN_LICENSE$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** 
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
** $END_LICENSE$
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
package io.qt.core;

import io.qt.*;

/**
 * 

Java wrapper for Qt class QBindable

* Use QBindable to make a {@link QObject.QProperty} field available as bindable {@link QMetaProperty}. *

Example:

* * public class MyObject extends QObject{
*     private final QProperty<QColor> color = new QProperty<>();
*     
*     public QColor color(){
*         return color.value();
*     }
*     
*     public void setColor(QColor color){
*         color.setValue(color);
*     }
*     
*     public QBindable<QColor> bindableColor(){
*         return new QBindable<>(color);
*     }
* } *
*

To let QtJambi's metaobject system identify a method as bindable getter * the method meets following signature and naming convention:

*
    *
  1. returns {@link QUntypedBindable} or a subtype of QUntypedBindable
  2. *
  3. no parameters
  4. *
  5. method name is "bindableFoo" for an available property "foo" * whereas "foo" can be any name
  6. *
*

If the bindable getter's name does not meet the upper naming convention * use the {@link io.qt.QtPropertyBindable} annotation instead and specify the * property's name by the annotation's {@link io.qt.QtPropertyBindable#name()} attribute.

*

Alternatively, it is possible to declare a {@link QObject.QProperty} field public * or by using the {@link io.qt.QtPropertyMember} annotation. * In this case, QBindable is supplied automatically:

* * public class MyObject extends QObject{
*     @QtPropertyMember
*     private final QProperty<QColor> color = new QProperty<>();
* } *
*

For primitive-typed implementations of {@link QBindable} see:

*
    *
  • {@link QBooleanBindable}
  • *
  • {@link QByteBindable}
  • *
  • {@link QShortBindable}
  • *
  • {@link QIntBindable}
  • *
  • {@link QLongBindable}
  • *
  • {@link QFloatBindable}
  • *
  • {@link QDoubleBindable}
  • *
  • {@link QCharBindable}
  • *
* * @see io.qt.QtPropertyBindable * @see io.qt.core.QObject.QProperty * @see QMetaProperty#isBindable() * @see QMetaProperty#bindable(QObject) */ public final class QBindable extends QUntypedBindable { /** * Creates a new Boolean-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Boolean> fromProperty(@StrictNonNull QBooleanProperty property){ return new QBindable<>(property, QBooleanBindable.bindableInterface(0)); } /** * Creates a new Byte-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Byte> fromProperty(@StrictNonNull QByteProperty property){ return new QBindable<>(property, QByteBindable.bindableInterface(0)); } /** * Creates a new Short-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Short> fromProperty(@StrictNonNull QShortProperty property){ return new QBindable<>(property, QShortBindable.bindableInterface(0)); } /** * Creates a new Integer-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Integer> fromProperty(@StrictNonNull QIntProperty property){ return new QBindable<>(property, QIntBindable.bindableInterface(0)); } /** * Creates a new Long-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Long> fromProperty(@StrictNonNull QLongProperty property){ return new QBindable<>(property, QLongBindable.bindableInterface(0)); } /** * Creates a new Float-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Float> fromProperty(@StrictNonNull QFloatProperty property){ return new QBindable<>(property, QFloatBindable.bindableInterface(0)); } /** * Creates a new Double-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Double> fromProperty(@StrictNonNull QDoubleProperty property){ return new QBindable<>(property, QDoubleBindable.bindableInterface(0)); } /** * Creates a new Character-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Character> fromProperty(@StrictNonNull QCharProperty property){ return new QBindable<>(property, QCharBindable.bindableInterface(0)); } /** * Creates a new Boolean-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Boolean> fromProperty(@StrictNonNull QObject.QBooleanProperty property){ return new QBindable<>(property, QBooleanBindable.bindableInterface(1)); } /** * Creates a new Byte-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Byte> fromProperty(@StrictNonNull QObject.QByteProperty property){ return new QBindable<>(property, QByteBindable.bindableInterface(1)); } /** * Creates a new Short-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Short> fromProperty(@StrictNonNull QObject.QShortProperty property){ return new QBindable<>(property, QShortBindable.bindableInterface(1)); } /** * Creates a new Integer-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Integer> fromProperty(@StrictNonNull QObject.QIntProperty property){ return new QBindable<>(property, QIntBindable.bindableInterface(1)); } /** * Creates a new Long-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Long> fromProperty(@StrictNonNull QObject.QLongProperty property){ return new QBindable<>(property, QLongBindable.bindableInterface(1)); } /** * Creates a new Float-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Float> fromProperty(@StrictNonNull QObject.QFloatProperty property){ return new QBindable<>(property, QFloatBindable.bindableInterface(1)); } /** * Creates a new Double-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Double> fromProperty(@StrictNonNull QObject.QDoubleProperty property){ return new QBindable<>(property, QDoubleBindable.bindableInterface(1)); } /** * Creates a new Character-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Character> fromProperty(@StrictNonNull QObject.QCharProperty property){ return new QBindable<>(property, QCharBindable.bindableInterface(1)); } /** * Creates a new Boolean-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Boolean> fromProperty(@StrictNonNull QObject.QComputedBooleanProperty property){ return new QBindable<>(property, QBooleanBindable.bindableInterface(2)); } /** * Creates a new Byte-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Byte> fromProperty(@StrictNonNull QObject.QComputedByteProperty property){ return new QBindable<>(property, QByteBindable.bindableInterface(2)); } /** * Creates a new Short-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Short> fromProperty(@StrictNonNull QObject.QComputedShortProperty property){ return new QBindable<>(property, QShortBindable.bindableInterface(2)); } /** * Creates a new Integer-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Integer> fromProperty(@StrictNonNull QObject.QComputedIntProperty property){ return new QBindable<>(property, QIntBindable.bindableInterface(2)); } /** * Creates a new Long-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Long> fromProperty(@StrictNonNull QObject.QComputedLongProperty property){ return new QBindable<>(property, QLongBindable.bindableInterface(2)); } /** * Creates a new Float-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Float> fromProperty(@StrictNonNull QObject.QComputedFloatProperty property){ return new QBindable<>(property, QFloatBindable.bindableInterface(2)); } /** * Creates a new Double-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Double> fromProperty(@StrictNonNull QObject.QComputedDoubleProperty property){ return new QBindable<>(property, QDoubleBindable.bindableInterface(2)); } /** * Creates a new Character-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ public static @NonNull QBindable<@QtPrimitiveType Character> fromProperty(@StrictNonNull QObject.QComputedCharProperty property){ return new QBindable<>(property, QCharBindable.bindableInterface(2)); } /** * Creates a new Boolean-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ @Deprecated(forRemoval=true, since="Qt 6.6") @SuppressWarnings("removal") public static @NonNull QBindable<@QtPrimitiveType Boolean> fromProperty(@StrictNonNull QBooleanPropertyAlias property){ return new QBindable<>(property.aliasedProperty(), property.iface); } /** * Creates a new Byte-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ @Deprecated(forRemoval=true, since="Qt 6.6") @SuppressWarnings("removal") public static @NonNull QBindable<@QtPrimitiveType Byte> fromProperty(@StrictNonNull QBytePropertyAlias property){ return new QBindable<>(property.aliasedProperty(), property.iface); } /** * Creates a new Short-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ @Deprecated(forRemoval=true, since="Qt 6.6") @SuppressWarnings("removal") public static @NonNull QBindable<@QtPrimitiveType Short> fromProperty(@StrictNonNull QShortPropertyAlias property){ return new QBindable<>(property.aliasedProperty(), property.iface); } /** * Creates a new Integer-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ @Deprecated(forRemoval=true, since="Qt 6.6") @SuppressWarnings("removal") public static @NonNull QBindable<@QtPrimitiveType Integer> fromProperty(@StrictNonNull QIntPropertyAlias property){ return new QBindable<>(property.aliasedProperty(), property.iface); } /** * Creates a new Long-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ @Deprecated(forRemoval=true, since="Qt 6.6") @SuppressWarnings("removal") public static @NonNull QBindable<@QtPrimitiveType Long> fromProperty(@StrictNonNull QLongPropertyAlias property){ return new QBindable<>(property.aliasedProperty(), property.iface); } /** * Creates a new Float-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ @Deprecated(forRemoval=true, since="Qt 6.6") @SuppressWarnings("removal") public static @NonNull QBindable<@QtPrimitiveType Float> fromProperty(@StrictNonNull QFloatPropertyAlias property){ return new QBindable<>(property.aliasedProperty(), property.iface); } /** * Creates a new Double-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ @Deprecated(forRemoval=true, since="Qt 6.6") @SuppressWarnings("removal") public static @NonNull QBindable<@QtPrimitiveType Double> fromProperty(@StrictNonNull QDoublePropertyAlias property){ return new QBindable<>(property.aliasedProperty(), property.iface); } /** * Creates a new Character-typed generic QBindable from primitive-typed property. * @param property * @return QBindable */ @Deprecated(forRemoval=true, since="Qt 6.6") @SuppressWarnings("removal") public static @NonNull QBindable<@QtPrimitiveType Character> fromProperty(@StrictNonNull QCharPropertyAlias property){ return new QBindable<>(property.aliasedProperty(), property.iface); } public static @NonNull QBindable fromProperty(QtUtilities.Supplier propertyGetter){ io.qt.internal.ClassAnalyzerUtility.LambdaInfo info = io.qt.internal.ClassAnalyzerUtility.lambdaInfo(propertyGetter); if(info!=null && info.qobject!=null && info.reflectiveMethod!=null && !info.reflectiveMethod.isSynthetic()) { QtPropertyReader pr = info.reflectiveMethod.getAnnotation(QtPropertyReader.class); if(pr!=null) { if(pr.enabled() && !pr.name().isEmpty()) { QMetaProperty prp = info.qobject.metaObject().property(pr.name()); if(prp!=null && prp.isValid()) { return new QBindable<>(info.qobject, prp); } } }else{ int[] lambdaMetaTypes = QtJambi_LibraryUtilities.internal.lambdaMetaTypes(QtUtilities.Supplier.class, propertyGetter); if(lambdaMetaTypes!=null && lambdaMetaTypes.length==1) { QMetaProperty prp = info.qobject.metaObject().property(info.reflectiveMethod.getName()); if(prp!=null && prp.isValid() && lambdaMetaTypes[0]==prp.typeId()) { return new QBindable<>(info.qobject, prp); } boolean isIs = false; if(info.reflectiveMethod.getName().startsWith("get") || info.reflectiveMethod.getName().startsWith("has") || (isIs = info.reflectiveMethod.getName().startsWith("is"))) { String name = info.reflectiveMethod.getName().substring(isIs ? 2 : 3); if(name.length()>1) { name = Character.toLowerCase(name.charAt(0)) + name.substring(1); prp = info.qobject.metaObject().property(name); if(prp!=null && prp.isValid() && lambdaMetaTypes[0]==prp.typeId()) { return new QBindable<>(info.qobject, prp); } } } } } throw new IllegalArgumentException(String.format("Unable to determine property from method %1$s.", info.reflectiveMethod.toGenericString())); }else { throw new IllegalArgumentException("Unable to determine property from given method."); } } private QBindable(QPrivateConstructor p) { super(p); } /** * Creates a new invalid QBindable. */ public QBindable() { super(null, null); } /** * Creates a new QBindable from provided property. * @param property */ public QBindable(@StrictNonNull QProperty property) { super(property.valueMetaType().isValid() ? property : null, property.valueMetaType().isValid() ? bindableInterface(property.valueMetaType().id(), 0) : null); } /** * Creates a new QBindable from provided property. * @param property */ public QBindable(QObject.@StrictNonNull QProperty property) { super(property.valueMetaType().isValid() ? property : null, property.valueMetaType().isValid() ? bindableInterface(property.valueMetaType().id(), 1) : null); } /** * Creates a new QBindable from provided property. * @param property */ public QBindable(QObject.@StrictNonNull QComputedProperty property) { super(property.valueMetaType().isValid() ? property : null, property.valueMetaType().isValid() ? bindableInterface(property.valueMetaType().id(), 2) : null); } /** * Creates a new QBindable from provided property. * @param property */ @Deprecated(forRemoval=true, since="Qt 6.6") @SuppressWarnings("removal") public QBindable(@StrictNonNull QPropertyAlias property) { super(property.aliasedProperty(), property.iface); } /** * Creates a new bindable from provided meta property of given object. * If the type of the property is not short an {@link IllegalArgumentException} is thrown. * @param object * @param property */ public QBindable(@StrictNonNull QObject object, @StrictNonNull String property, @StrictNonNull Class type, @StrictNonNull QMetaType @StrictNonNull... instantiations) { this(object, object.metaObject().property(property), type, instantiations); } /** * Creates a new bindable from provided meta property of given object. * If the type of the property is not short an {@link IllegalArgumentException} is thrown. * @param object * @param property */ public QBindable(@StrictNonNull QObject object, @StrictNonNull QMetaProperty property, @StrictNonNull Class type, @StrictNonNull QMetaType @StrictNonNull... instantiations) { super(object, property, QBindable.bindableInterface(property, QMetaType.fromType(type, instantiations))); } private QBindable(@StrictNonNull QObject object, @StrictNonNull QMetaProperty property) { super(object, property, QBindable.bindableInterface(property)); } private static QBindableInterface bindableInterface(QMetaProperty property, QMetaType metaType) { QBindableInterface result = bindableInterface(property); if(!result.metaType().equals(metaType)) { throw new IllegalArgumentException(String.format("Cannot create bindable of type %1$s to property of type %2$s", metaType.name(), result.metaType().name())); } return result; } static QBindableInterface bindableInterface(QMetaProperty property) { if(property==null || !property.isValid()) throw new IllegalArgumentException("Given QMetaProperty is invalid."); return bindableInterface(property.typeId(), 3); } static native QBindableInterface bindableInterface(int metaType, int propertyType); QBindable(QUntypedPropertyData d, QBindableInterface i) { super(d, i); } /** * Creates a binding to the underlying property. */ @QtUninvokable @Override public @NonNull QPropertyBinding makeBinding() { return new QPropertyBinding<>(super.makeBinding()); } /** * Returns the binding expression that is associated with the underlying property. * A default constructed {@link QPropertyBinding} will be returned if no such association exists. * @return binding */ @QtUninvokable @Override public @NonNull QPropertyBinding binding() { return new QPropertyBinding<>(super.binding()); } /** *

Disassociates the binding expression from the underlying property and returns it.

*

After calling this function, the value of the property will only change if you assign a new value to it, or when a new binding is set.

* @return the removed binding */ @QtUninvokable public @NonNull QPropertyBinding takeBinding() { return setBinding(new QPropertyBinding<>()); } /** *

Associates the value of the underlying property with the provided newBinding expression and returns the previously associated binding.

*

The binding's value type ({@link QUntypedPropertyBinding#valueMetaType()}) has to be equals to the property's value type T, * otherwise the property remains unchanged.

*

The first time the property value is read, the binding is evaluated. * Whenever a dependency of the binding changes, the binding will be re-evaluated the next time the value of the underlying property is read.

* @param binding * @return oldBinding */ @SuppressWarnings("unchecked") @QtUninvokable public @NonNull QPropertyBinding setBinding(@NonNull QPropertyBinding binding) { if(iface()!=null && binding!=null && !io.qt.core.QProperty.checkType(iface().metaType(), binding.valueMetaType())) return new QPropertyBinding<>(); QUntypedPropertyBinding oldBinding = super.overrideBinding(binding); if(oldBinding instanceof QPropertyBinding) { return (QPropertyBinding)oldBinding; }else { return new QPropertyBinding<>(oldBinding); } } /** *

Associates the value of the underlying property with the provided functor and returns the previously associated binding.

*

The first time the property value is read, the binding is evaluated by invoking {@link java.util.function.Supplier#get()} of functor. * Whenever a dependency of the binding changes, the binding will be re-evaluated the next time the value of the underlying property is read.

* @param functor * @return oldBinding */ @QtUninvokable public @NonNull QPropertyBinding setBinding(QtUtilities.@StrictNonNull Supplier functor) { try { QPropertyBinding.setPendingMetaType(this::metaType); return setBinding(new QPropertyBinding<>(functor)); }finally { QPropertyBinding.setPendingMetaType(null); } } /** * Returns the value of the underlying property. This may evaluate a binding expression that is tied to the property, before returning the value. * @return value */ @io.qt.QtUninvokable public final T value() { return value(QtJambi_LibraryUtilities.internal.nativeId(this.iface()), QtJambi_LibraryUtilities.internal.nativeId(this.data()), 0); } /** *

Assigns value to the underlying property and removes the property's associated binding, if present.

* @param value */ @io.qt.QtUninvokable public final void setValue(T value) { setValue(QtJambi_LibraryUtilities.internal.nativeId(this.iface()), QtJambi_LibraryUtilities.internal.nativeId(this.data()), 0, value); } @io.qt.QtUninvokable static native T value(long ifaceId, long dataId, long metaTypeId); @io.qt.QtUninvokable static native boolean setValue(long ifaceId, long dataId, long metaTypeId, T value); QMetaType valueMetaType() { if(iface()!=null) return iface().metaType(); else return new QMetaType(); } @NativeAccess private static QUntypedBindable createBinding(QUntypedPropertyData property) { if(property instanceof QProperty) { return new QBindable<>((QProperty)property); }else if(property instanceof QObject.QProperty) { return new QBindable<>((QObject.QProperty)property); }else if(property instanceof QObject.QComputedProperty) { return new QBindable<>((QObject.QComputedProperty)property); }else if(property instanceof QBooleanProperty) { return new QBooleanBindable((QBooleanProperty)property); }else if(property instanceof QObject.QBooleanProperty) { return new QBooleanBindable((QObject.QBooleanProperty)property); }else if(property instanceof QObject.QComputedBooleanProperty) { return new QBooleanBindable((QObject.QComputedBooleanProperty)property); }else if(property instanceof QByteProperty) { return new QByteBindable((QByteProperty)property); }else if(property instanceof QObject.QByteProperty) { return new QByteBindable((QObject.QByteProperty)property); }else if(property instanceof QObject.QComputedByteProperty) { return new QByteBindable((QObject.QComputedByteProperty)property); }else if(property instanceof QShortProperty) { return new QShortBindable((QShortProperty)property); }else if(property instanceof QObject.QShortProperty) { return new QShortBindable((QObject.QShortProperty)property); }else if(property instanceof QObject.QComputedShortProperty) { return new QShortBindable((QObject.QComputedShortProperty)property); }else if(property instanceof QIntProperty) { return new QIntBindable((QIntProperty)property); }else if(property instanceof QObject.QIntProperty) { return new QIntBindable((QObject.QIntProperty)property); }else if(property instanceof QObject.QComputedIntProperty) { return new QIntBindable((QObject.QComputedIntProperty)property); }else if(property instanceof QLongProperty) { return new QLongBindable((QLongProperty)property); }else if(property instanceof QObject.QLongProperty) { return new QLongBindable((QObject.QLongProperty)property); }else if(property instanceof QObject.QComputedLongProperty) { return new QLongBindable((QObject.QComputedLongProperty)property); }else if(property instanceof QFloatProperty) { return new QFloatBindable((QFloatProperty)property); }else if(property instanceof QObject.QFloatProperty) { return new QFloatBindable((QObject.QFloatProperty)property); }else if(property instanceof QObject.QComputedFloatProperty) { return new QFloatBindable((QObject.QComputedFloatProperty)property); }else if(property instanceof QDoubleProperty) { return new QDoubleBindable((QDoubleProperty)property); }else if(property instanceof QObject.QDoubleProperty) { return new QDoubleBindable((QObject.QDoubleProperty)property); }else if(property instanceof QObject.QComputedDoubleProperty) { return new QDoubleBindable((QObject.QComputedDoubleProperty)property); }else if(property instanceof QCharProperty) { return new QCharBindable((QCharProperty)property); }else if(property instanceof QObject.QCharProperty) { return new QCharBindable((QObject.QCharProperty)property); }else if(property instanceof QObject.QComputedCharProperty) { return new QCharBindable((QObject.QComputedCharProperty)property); } return null; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy