Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cxx-qt-lib: Add binding for QQmlApplicationEngine::addImageProvider #1142

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- QObject subclasses can now inherit from other CXX-Qt generated QObject classes
- `BUILD_WASM` CMake option to support WebAssembly builds and a book page for building for WASM
- Add support for cxx_name and rust_name on qproperty attributes which applies to the QProperty generated as well as functions
- Add support for adding custom image providers for QML.

### Changed

Expand Down
31 changes: 31 additions & 0 deletions crates/cxx-qt-lib/src/qml/qqmlapplicationengine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ mod ffi {
type QStringList = crate::QStringList;
include!("cxx-qt-lib/qurl.h");
type QUrl = crate::QUrl;
include!(<QQmlImageProviderBase>);
type QQmlImageProviderBase;
include!(<QtQuick/QQuickImageProvider>);
type QQuickImageProvider;
include!(<QtQuick/QQuickAsyncImageProvider>);
type QQuickAsyncImageProvider;

include!("cxx-qt-lib/qqmlapplicationengine.h");
type QQmlApplicationEngine;

#[doc(hidden)]
#[rust_name = "add_image_provider_internal"]
unsafe fn addImageProvider(self: Pin<&mut QQmlApplicationEngine>, provider_id: &QString, provider: *mut QQmlImageProviderBase);

/// Adds path as a directory where the engine searches for installed modules in a URL-based directory structure.
#[rust_name = "add_import_path"]
fn addImportPath(self: Pin<&mut QQmlApplicationEngine>, path: &QString);
Expand Down Expand Up @@ -90,6 +100,14 @@ use crate::QQmlEngine;
use core::pin::Pin;

pub use ffi::QQmlApplicationEngine;
use crate::QString;
use ffi::QQmlImageProviderBase;

#[allow(dead_code)]
pub enum QQmlImageProviderBasePointer {
QQuickImageProvider(*mut ffi::QQuickImageProvider),
QQuickAsyncImageProvider(*mut ffi::QQuickAsyncImageProvider)
}

impl QQmlApplicationEngine {
/// Convert the existing [QQmlApplicationEngine] to a [QQmlEngine]
Expand All @@ -101,4 +119,17 @@ impl QQmlApplicationEngine {
pub fn new() -> cxx::UniquePtr<Self> {
ffi::qqmlapplicationengine_new()
}

/// Sets the provider to use for images requested via the image: url scheme, with host proveri_id. The QQmlEngine takes ownership of provider.
pub unsafe fn add_image_provider(self: Pin<&mut QQmlApplicationEngine>, provider_id: &QString, provider: QQmlImageProviderBasePointer) {
let ptr = match provider {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is another case where having up/down casting would help out #562 (something we hope for this cycle). As then the method could just support the QQmlImageProviderBase* and in Rust you do upcast_ptr() when calling the method.

QQmlImageProviderBasePointer::QQuickAsyncImageProvider(ptr) => {
ptr as *mut QQmlImageProviderBase
},
QQmlImageProviderBasePointer::QQuickImageProvider(ptr) => {
ptr as *mut QQmlImageProviderBase
}
};
self.add_image_provider_internal(provider_id, ptr);
}
}
Loading