Made qmetaobject optional dep.

This commit is contained in:
Ayush Singh 2021-12-23 17:35:16 +05:30
parent bb82d5fdfc
commit 1b8354d05b
4 changed files with 39 additions and 18 deletions

View file

@ -13,7 +13,7 @@ build = "build.rs"
[dependencies] [dependencies]
cpp = "0.5" cpp = "0.5"
qmetaobject = "0.2" qmetaobject = { version = "0.2", optional = true }
qttypes = "0.2" qttypes = "0.2"
# qmetaobject = { path="../qmetaobject-rs/qmetaobject" } # qmetaobject = { path="../qmetaobject-rs/qmetaobject" }
# qttypes = { path="../qmetaobject-rs/qttypes" } # qttypes = { path="../qmetaobject-rs/qttypes" }
@ -26,4 +26,8 @@ semver = "1.0"
lazy_static = "1.4" lazy_static = "1.4"
[package.metadata.docs.rs] [package.metadata.docs.rs]
dependencies = ["qtbase5-dev", "qtdeclarative5-dev", "libqt5svg5-dev", "qtquickcontrols2-5-dev", "qml-module-org-kde-kirigami2", "kirigami2-dev", "libkf5i18n-dev", "gettext", "libkf5coreaddons-dev", "qml-module-qtquick-layouts"] dependencies = ["qtbase5-dev", "qtdeclarative5-dev", "libkf5i18n-dev"]
all-features = true
[features]
all = ["qmetaobject"]

View file

@ -1,7 +1,10 @@
use cpp::{cpp, cpp_class}; use cpp::{cpp, cpp_class};
use qmetaobject::prelude::*; use qttypes::QString;
use std::ffi::c_void; use std::ffi::c_void;
#[cfg(feature = "qmetaobject")]
use qmetaobject::QmlEngine;
cpp! {{ cpp! {{
#include <KLocalizedContext> #include <KLocalizedContext>
#include <QtCore/QObject> #include <QtCore/QObject>
@ -21,11 +24,13 @@ cpp_class!(
); );
impl KLocalizedContext { impl KLocalizedContext {
#[cfg(feature = "qmetaobject")]
/// Initialize KLocalizedContext from Engine. /// Initialize KLocalizedContext from Engine.
/// **Feature** `qmetaobject` needs to be enabled for this function.
/// # Example /// # Example
/// ``` /// ```
/// use ki18n::klocalizedcontext::KLocalizedContext; /// use ki18n::klocalizedcontext::KLocalizedContext;
/// use qmetaobject::prelude::*; /// use qmetaobject::QmlEngine;
/// ///
/// let engine = QmlEngine::new(); /// let engine = QmlEngine::new();
/// KLocalizedContext::init_from_engine(&engine); /// KLocalizedContext::init_from_engine(&engine);
@ -48,22 +53,28 @@ impl KLocalizedContext {
} }
/// Set Translation Domain for current KLocalizedContext. /// Set Translation Domain for current KLocalizedContext.
/// # Example #[cfg_attr(
/// ``` feature = "qmetaobject",
/// use ki18n::klocalizedcontext::KLocalizedContext; doc = r##"
/// use qmetaobject::prelude::*; # Example
///
/// let engine = QmlEngine::new(); ```
/// let mut context = KLocalizedContext::init_from_engine(&engine); use ki18n::klocalizedcontext::KLocalizedContext;
/// context.set_translation_domain("Test Domain".into()); use qmetaobject::QmlEngine;
/// ```
let engine = QmlEngine::new();
let mut context = KLocalizedContext::init_from_engine(&engine);
context.set_translation_domain("Test Domain".into());
```
"##
)]
pub fn set_translation_domain(&mut self, domain: QString) { pub fn set_translation_domain(&mut self, domain: QString) {
cpp!(unsafe [self as "KLocalizedContextHolder *", domain as "QString"] { cpp!(unsafe [self as "KLocalizedContextHolder *", domain as "QString"] {
self->klocalized->setTranslationDomain(domain); self->klocalized->setTranslationDomain(domain);
}) })
} }
/// Retrns the current Translation Domain. /// Returns the current Translation Domain.
pub fn translation_domain(&self) -> QString { pub fn translation_domain(&self) -> QString {
cpp!(unsafe [self as "KLocalizedContextHolder *"] -> QString as "QString" { cpp!(unsafe [self as "KLocalizedContextHolder *"] -> QString as "QString" {
return self->klocalized->translationDomain(); return self->klocalized->translationDomain();

View file

@ -1,5 +1,5 @@
use cpp::{cpp, cpp_class}; use cpp::{cpp, cpp_class};
use qmetaobject::{QByteArray, QString, QStringList}; use qttypes::{QByteArray, QString, QStringList};
use std::ffi::CStr; use std::ffi::CStr;
cpp! {{ cpp! {{
@ -116,7 +116,7 @@ impl KLocalizedString {
/// TODO: Add Test /// TODO: Add Test
pub fn with_languages(&self, languages: &QStringList) -> KLocalizedString { pub fn with_languages(&self, languages: &QStringList) -> KLocalizedString {
cpp!(unsafe [self as "KLocalizedString *", languages as "QStringList"] -> KLocalizedString as "KLocalizedString" { cpp!(unsafe [self as "KLocalizedString *", languages as "QStringList"] -> KLocalizedString as "KLocalizedString" {
return self->withLangauages(languages); return self->withLanguages(languages);
}) })
} }
} }

View file

@ -1,8 +1,13 @@
use ki18n::klocalizedcontext::KLocalizedContext; use ki18n::klocalizedcontext::KLocalizedContext;
use qmetaobject::prelude::*; use qttypes::QString;
#[cfg(feature = "qmetaobject")]
use qmetaobject::QmlEngine;
mod common; mod common;
#[test] #[test]
#[cfg(feature = "qmetaobject")]
fn cpp_ptr() { fn cpp_ptr() {
let _lock = common::lock_for_test(); let _lock = common::lock_for_test();
@ -14,6 +19,7 @@ fn cpp_ptr() {
} }
#[test] #[test]
#[cfg(feature = "qmetaobject")]
fn translation_domain() { fn translation_domain() {
let _lock = common::lock_for_test(); let _lock = common::lock_for_test();