Initial Commit.

This commit is contained in:
Ayush Singh 2021-11-01 15:01:02 +05:30
commit 2c6be797a3
5 changed files with 128 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

17
Cargo.toml Normal file
View file

@ -0,0 +1,17 @@
[package]
name = "ki18n-rs"
version = "0.1.0"
edition = "2018"
links = "KF5I18n"
[dependencies]
cpp = "0.5"
qmetaobject = { git = "https://github.com/woboq/qmetaobject-rs" }
qttypes = { git = "https://github.com/woboq/qmetaobject-rs", features = [
"qtquick",
] }
[build-dependencies]
cpp_build = "0.5"
semver = "1.0"
pkg-config = "0.3"

74
build.rs Normal file
View file

@ -0,0 +1,74 @@
/* Copyright (C) 2018 Olivier Goffart <ogoffart@woboq.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
use semver::Version;
fn main() {
eprintln!("cargo:warning={:?}", std::env::vars().collect::<Vec<_>>());
let mut config = cpp_build::Config::new();
let qt_version = qt_setup(&mut config);
ki18n_setup(&mut config);
config.build("src/lib.rs");
for minor in 7..=15 {
if qt_version >= Version::new(5, minor, 0) {
println!("cargo:rustc-cfg=qt_{}_{}", 5, minor);
}
}
let mut minor = 0;
while qt_version >= Version::new(6, minor, 0) {
println!("cargo:rustc-cfg=qt_{}_{}", 6, minor);
minor += 1;
}
}
fn qt_setup(config: &mut cpp_build::Config) -> Version {
let qt_include_path = std::env::var("DEP_QT_INCLUDE_PATH").unwrap();
let qt_library_path = std::env::var("DEP_QT_LIBRARY_PATH").unwrap();
let qt_version = std::env::var("DEP_QT_VERSION")
.unwrap()
.parse::<Version>()
.expect("Parsing Qt version failed");
if cfg!(target_os = "macos") {
config.flag("-F");
config.flag(&qt_library_path);
}
if qt_version >= Version::new(6, 0, 0) {
config.flag_if_supported("-std=c++17");
config.flag_if_supported("/std:c++17");
config.flag_if_supported("/Zc:__cplusplus");
}
config.include(&qt_include_path);
qt_version
}
fn ki18n_setup(config: &mut cpp_build::Config) {
let kf5_i18n_path = "/usr/include/KF5/KI18n";
let qt_core = "/usr/include/qt/QtCore";
config.include(kf5_i18n_path).include(qt_core);
println!("cargo:rustc-link-lib=KF5I18n");
}

27
src/lib.rs Normal file
View file

@ -0,0 +1,27 @@
use cpp::{cpp, cpp_class};
use qmetaobject::prelude::*;
use qmetaobject::QObjectPinned;
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!(pub unsafe struct KLocalizedContext as "KLocalizedContextHolder");
impl KLocalizedContext {
pub fn init_from_engine(engine: &QmlEngine) {
let engine_ptr = engine.cpp_ptr();
cpp!(unsafe [engine_ptr as "QQmlEngine*"] {
engine_ptr->rootContext()->setContextObject(new KLocalizedContext(engine_ptr));
});
}
}

8
tests/tests.rs Normal file
View file

@ -0,0 +1,8 @@
use ki18n_rs::*;
use qmetaobject::prelude::*;
#[test]
fn test_klocalized_init() {
let mut engine = QmlEngine::new();
KLocalizedContext::init_from_engine(&engine);
}