Fixed ki18n. Was broken previously.

No longer returning KLocalizedContex when initializing from QQmlEngine.
Unique Pointer was causing errors.
This commit is contained in:
Ayush Singh 2022-02-04 13:56:36 +05:30
parent a63078b951
commit 16c65fc226
4 changed files with 21 additions and 12 deletions

View file

@ -2,7 +2,7 @@
name = "ki18n" name = "ki18n"
description = "A crate to use KF5I18n from rust." description = "A crate to use KF5I18n from rust."
author = ["Ayush Singh <ayushdevel1325@gmail.com>"] author = ["Ayush Singh <ayushdevel1325@gmail.com>"]
version = "2.0.1" version = "1.2.0"
edition = "2021" edition = "2021"
links = "KF5I18n" links = "KF5I18n"
license = "MIT" license = "MIT"

View file

@ -3,7 +3,7 @@ use qttypes::QString;
use std::ffi::c_void; use std::ffi::c_void;
#[cfg(feature = "qmetaobject")] #[cfg(feature = "qmetaobject")]
use qmetaobject::QmlEngine; use qmetaobject::{QObject, QObjectPinned, QmlEngine};
cpp! {{ cpp! {{
#include <KLocalizedContext> #include <KLocalizedContext>
@ -12,18 +12,29 @@ cpp! {{
#include <QtQuick/QtQuick> #include <QtQuick/QtQuick>
struct KLocalizedContextHolder { struct KLocalizedContextHolder {
std::unique_ptr<KLocalizedContext> klocalized; std::shared_ptr<KLocalizedContext> klocalized;
KLocalizedContextHolder() : klocalized(new KLocalizedContext) {}
KLocalizedContextHolder(QObject *parent) : klocalized(new KLocalizedContext(parent)) {} KLocalizedContextHolder(QObject *parent) : klocalized(new KLocalizedContext(parent)) {}
}; };
}} }}
cpp_class!( cpp_class!(
/// Struct representing [KLocalizedContext](https://api.kde.org/frameworks/ki18n/html/classKLocalizedContext.html). Mainly used with QML. /// Struct representing [KLocalizedContext](https://api.kde.org/frameworks/ki18n/html/classKLocalizedContext.html). Mainly used with QML.
#[derive(Default)]
pub unsafe struct KLocalizedContext as "KLocalizedContextHolder" pub unsafe struct KLocalizedContext as "KLocalizedContextHolder"
); );
impl KLocalizedContext { impl KLocalizedContext {
#[cfg(feature = "qmetaobject")]
/// Construct a new KLocalizedContext from a QObject.
pub fn new<T: QObject + Sized>(obj: QObjectPinned<T>) -> 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")] #[cfg(feature = "qmetaobject")]
/// Initialize KLocalizedContext from Engine. /// Initialize KLocalizedContext from Engine.
/// **Feature** `qmetaobject` needs to be enabled for this function. /// **Feature** `qmetaobject` needs to be enabled for this function.
@ -35,12 +46,10 @@ impl KLocalizedContext {
/// let engine = QmlEngine::new(); /// let engine = QmlEngine::new();
/// KLocalizedContext::init_from_engine(&engine); /// 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(); let engine_ptr = engine.cpp_ptr();
cpp!(unsafe [engine_ptr as "QQmlEngine*"] -> KLocalizedContext as "KLocalizedContextHolder" { cpp!(unsafe [engine_ptr as "QQmlEngine*"]{
auto klocalizedholder = KLocalizedContextHolder(engine_ptr); engine_ptr->rootContext()->setContextObject(new KLocalizedContext(engine_ptr));
engine_ptr->rootContext()->setContextObject(klocalizedholder.klocalized.get());
return klocalizedholder;
}) })
} }
@ -62,7 +71,7 @@ use ki18n::klocalizedcontext::KLocalizedContext;
use qmetaobject::QmlEngine; use qmetaobject::QmlEngine;
let engine = QmlEngine::new(); let engine = QmlEngine::new();
let mut context = KLocalizedContext::init_from_engine(&engine); let mut context = KLocalizedContext::default();
context.set_translation_domain("Test Domain".into()); context.set_translation_domain("Test Domain".into());
``` ```
"## "##

View file

@ -30,7 +30,7 @@
//! - `qmetaobject` : Enables some methods that require qmetaobject. Most people will need this. //! - `qmetaobject` : Enables some methods that require qmetaobject. Most people will need this.
//! //!
//! # Example //! # Example
//! ```no_run //! ```no-run
//! use cstr::cstr; //! use cstr::cstr;
//! use qmetaobject::prelude::*; //! use qmetaobject::prelude::*;
//! use ki18n::klocalizedcontext::KLocalizedContext; //! use ki18n::klocalizedcontext::KLocalizedContext;

View file

@ -12,7 +12,7 @@ fn cpp_ptr() {
let _lock = common::lock_for_test(); let _lock = common::lock_for_test();
let engine = QmlEngine::new(); let engine = QmlEngine::new();
let context = KLocalizedContext::init_from_engine(&engine); let context = KLocalizedContext::default();
let context_ptr = context.cpp_ptr(); let context_ptr = context.cpp_ptr();
assert_ne!(std::ptr::null(), context_ptr); assert_ne!(std::ptr::null(), context_ptr);
@ -24,7 +24,7 @@ fn translation_domain() {
let _lock = common::lock_for_test(); let _lock = common::lock_for_test();
let engine = QmlEngine::new(); let engine = QmlEngine::new();
let mut context = KLocalizedContext::init_from_engine(&engine); let mut context = KLocalizedContext::default();
const TRANSLATION_DOMAIN: &str = "Test Domain"; const TRANSLATION_DOMAIN: &str = "Test Domain";
context.set_translation_domain(TRANSLATION_DOMAIN.into()); context.set_translation_domain(TRANSLATION_DOMAIN.into());