diff --git a/Cargo.toml b/Cargo.toml index 593f9ac..51dc11b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "ki18n" description = "A crate to use KF5I18n from rust." author = ["Ayush Singh "] -version = "2.0.1" +version = "1.2.0" edition = "2021" links = "KF5I18n" license = "MIT" diff --git a/src/klocalizedcontext.rs b/src/klocalizedcontext.rs index 6e0865b..8f930bb 100644 --- a/src/klocalizedcontext.rs +++ b/src/klocalizedcontext.rs @@ -3,7 +3,7 @@ use qttypes::QString; use std::ffi::c_void; #[cfg(feature = "qmetaobject")] -use qmetaobject::QmlEngine; +use qmetaobject::{QObject, QObjectPinned, QmlEngine}; cpp! {{ #include @@ -12,18 +12,29 @@ cpp! {{ #include struct KLocalizedContextHolder { - std::unique_ptr klocalized; + std::shared_ptr klocalized; + KLocalizedContextHolder() : klocalized(new KLocalizedContext) {} KLocalizedContextHolder(QObject *parent) : klocalized(new KLocalizedContext(parent)) {} }; }} cpp_class!( /// Struct representing [KLocalizedContext](https://api.kde.org/frameworks/ki18n/html/classKLocalizedContext.html). Mainly used with QML. + #[derive(Default)] pub unsafe struct KLocalizedContext as "KLocalizedContextHolder" ); impl KLocalizedContext { + #[cfg(feature = "qmetaobject")] + /// Construct a new KLocalizedContext from a QObject. + pub fn new(obj: QObjectPinned) -> Self { + let obj_ptr = obj.get_or_create_cpp_object(); + cpp!(unsafe [obj_ptr as "QObject *"] -> KLocalizedContext as "KLocalizedContextHolder" { + return KLocalizedContextHolder(obj_ptr); + }) + } + #[cfg(feature = "qmetaobject")] /// Initialize KLocalizedContext from Engine. /// **Feature** `qmetaobject` needs to be enabled for this function. @@ -35,12 +46,10 @@ impl KLocalizedContext { /// let engine = QmlEngine::new(); /// KLocalizedContext::init_from_engine(&engine); /// ``` - pub fn init_from_engine(engine: &QmlEngine) -> Self { + pub fn init_from_engine(engine: &QmlEngine) { let engine_ptr = engine.cpp_ptr(); - cpp!(unsafe [engine_ptr as "QQmlEngine*"] -> KLocalizedContext as "KLocalizedContextHolder" { - auto klocalizedholder = KLocalizedContextHolder(engine_ptr); - engine_ptr->rootContext()->setContextObject(klocalizedholder.klocalized.get()); - return klocalizedholder; + cpp!(unsafe [engine_ptr as "QQmlEngine*"]{ + engine_ptr->rootContext()->setContextObject(new KLocalizedContext(engine_ptr)); }) } @@ -62,7 +71,7 @@ use ki18n::klocalizedcontext::KLocalizedContext; use qmetaobject::QmlEngine; let engine = QmlEngine::new(); -let mut context = KLocalizedContext::init_from_engine(&engine); +let mut context = KLocalizedContext::default(); context.set_translation_domain("Test Domain".into()); ``` "## diff --git a/src/lib.rs b/src/lib.rs index da80fa8..a576956 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,7 +30,7 @@ //! - `qmetaobject` : Enables some methods that require qmetaobject. Most people will need this. //! //! # Example -//! ```no_run +//! ```no-run //! use cstr::cstr; //! use qmetaobject::prelude::*; //! use ki18n::klocalizedcontext::KLocalizedContext; diff --git a/tests/klocalizedcontext_tests.rs b/tests/klocalizedcontext_tests.rs index 9bc163f..59a858e 100644 --- a/tests/klocalizedcontext_tests.rs +++ b/tests/klocalizedcontext_tests.rs @@ -12,7 +12,7 @@ fn cpp_ptr() { let _lock = common::lock_for_test(); let engine = QmlEngine::new(); - let context = KLocalizedContext::init_from_engine(&engine); + let context = KLocalizedContext::default(); let context_ptr = context.cpp_ptr(); assert_ne!(std::ptr::null(), context_ptr); @@ -24,7 +24,7 @@ fn translation_domain() { let _lock = common::lock_for_test(); let engine = QmlEngine::new(); - let mut context = KLocalizedContext::init_from_engine(&engine); + let mut context = KLocalizedContext::default(); const TRANSLATION_DOMAIN: &str = "Test Domain"; context.set_translation_domain(TRANSLATION_DOMAIN.into());