Refactored KLocalizedContext
Extracted everything from lib.rs Also extracted all doc tests to integration tests.
This commit is contained in:
parent
0470f05c07
commit
38ebb314b8
6 changed files with 114 additions and 96 deletions
102
src/lib.rs
102
src/lib.rs
|
|
@ -1,6 +1,11 @@
|
|||
//! # KI18n-rs
|
||||
//! KI18n is a cross-platform internationalization framework used by KDE applications. This crate is meant to allow using KI18n with
|
||||
//! Rust and [qmetaobject-rs](https://github.com/woboq/qmetaobject-rs) crate.
|
||||
//!
|
||||
//! # Custom Location for KF5I18n
|
||||
//! The crate searches from KF5I18n using either the environment variables (KF5_I18n_INCLUDE_PATH and KF5_I18n_LIBRARY_PATH) if they are set or just
|
||||
//! searches at /usr/include /usr/library
|
||||
//!
|
||||
//! # Example
|
||||
//! ```ignore
|
||||
//! use cstr::cstr;
|
||||
|
|
@ -39,99 +44,6 @@
|
|||
//! engine.exec();
|
||||
//! }
|
||||
//! ```
|
||||
use cpp::{cpp, cpp_class};
|
||||
use qmetaobject::prelude::*;
|
||||
use std::ffi::c_void;
|
||||
|
||||
cpp! {{
|
||||
#include <KLocalizedContext>
|
||||
#include <QtCore/QObject>
|
||||
#include <QtQml/QQmlEngine>
|
||||
#include <QtQuick/QtQuick>
|
||||
|
||||
struct KLocalizedContextHolder {
|
||||
std::unique_ptr<KLocalizedContext> klocalized;
|
||||
|
||||
KLocalizedContextHolder(QObject *parent) : klocalized(new KLocalizedContext(parent)) {}
|
||||
};
|
||||
}}
|
||||
|
||||
cpp_class!(
|
||||
/// Struct representing KLocalizedContext. Mainly used with QML.
|
||||
pub unsafe struct KLocalizedContext as "KLocalizedContextHolder"
|
||||
);
|
||||
|
||||
impl KLocalizedContext {
|
||||
/// Initialize KLocalizedContext from Engine.
|
||||
/// ```rust
|
||||
/// use qmetaobject::prelude::*;
|
||||
/// use ki18n_rs::KLocalizedContext;
|
||||
///
|
||||
/// let mut engine = QmlEngine::new();
|
||||
/// let context = KLocalizedContext::init_from_engine(&engine);
|
||||
/// ```
|
||||
pub fn init_from_engine(engine: &QmlEngine) -> Self {
|
||||
let engine_ptr = engine.cpp_ptr();
|
||||
cpp!(unsafe [engine_ptr as "QQmlEngine*"] -> KLocalizedContext as "KLocalizedContextHolder" {
|
||||
auto klocalized = new KLocalizedContext(engine_ptr);
|
||||
auto klocalizedholder = KLocalizedContextHolder(klocalized);
|
||||
engine_ptr->rootContext()->setContextObject(klocalized);
|
||||
return klocalizedholder;
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns a pointer to the C++ object. The pointer is of the type `KLocalizedContext *` in C++.
|
||||
/// ```rust
|
||||
/// use qmetaobject::prelude::*;
|
||||
/// use ki18n_rs::KLocalizedContext;
|
||||
///
|
||||
/// let mut engine = QmlEngine::new();
|
||||
/// let context = KLocalizedContext::init_from_engine(&engine);
|
||||
///
|
||||
/// let context_ptr = context.cpp_ptr();
|
||||
/// ```
|
||||
pub fn cpp_ptr(&self) -> *mut c_void {
|
||||
cpp!(unsafe [self as "KLocalizedContextHolder *"] -> *mut c_void as "KLocalizedContext *" {
|
||||
return self->klocalized.get();
|
||||
})
|
||||
}
|
||||
|
||||
/// Set Translation Domain for current KLocalizedContext.
|
||||
/// ```rust
|
||||
/// use qmetaobject::prelude::*;
|
||||
/// use ki18n_rs::KLocalizedContext;
|
||||
///
|
||||
/// let mut engine = QmlEngine::new();
|
||||
/// let mut context = KLocalizedContext::init_from_engine(&engine);
|
||||
///
|
||||
/// const TRANSLATION_DOMAIN: &str = "Test Domain";
|
||||
/// context.set_translation_domain(TRANSLATION_DOMAIN.into());
|
||||
///
|
||||
/// let domain = context.translation_domain();
|
||||
///
|
||||
/// assert_eq!(domain, QString::from(TRANSLATION_DOMAIN));
|
||||
/// ```
|
||||
pub fn set_translation_domain(&mut self, domain: QString) {
|
||||
cpp!(unsafe [self as "KLocalizedContextHolder *", domain as "QString"] {
|
||||
self->klocalized->setTranslationDomain(domain);
|
||||
})
|
||||
}
|
||||
|
||||
/// Retrns the current Translation Domain.
|
||||
/// ```rust
|
||||
/// use qmetaobject::prelude::*;
|
||||
/// use ki18n_rs::KLocalizedContext;
|
||||
///
|
||||
/// let mut engine = QmlEngine::new();
|
||||
/// let mut context = KLocalizedContext::init_from_engine(&engine);
|
||||
///
|
||||
/// let domain = context.translation_domain();
|
||||
///
|
||||
/// assert_eq!(domain, QString::from(""));
|
||||
/// ```
|
||||
pub fn translation_domain(&self) -> QString {
|
||||
cpp!(unsafe [self as "KLocalizedContextHolder *"] -> QString as "QString" {
|
||||
return self->klocalized->translationDomain();
|
||||
})
|
||||
}
|
||||
}
|
||||
mod klocalizedcontext;
|
||||
pub use klocalizedcontext::KLocalizedContext;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue