Skip to content

Add QtQuickControls module to cxx-qt-lib, expose QQuickStyle #966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `CxxQtThread` is now marked as `Sync` so that it can be used by reference
- Add cxx-qt-lib-extras crate which contains: `QCommandLineOption`, `QCommandLineParser`, `QElapsedTimer`, `QApplication`
- Serde support for `QString` (requires "serde" feature on cxx-qt-lib)
- A new QuickControls module, which exposes `QQuickStyle`. This module is enabled by default and is behind the `qt_quickcontrols` feature.

### Changed

Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ add_compile_definitions(
# QMAKE environment variable is needed by qt-build-utils to ensure that Cargo
# uses the same installation of Qt as CMake does.
if(NOT USE_QT5)
find_package(Qt6 COMPONENTS Core Gui Test)
find_package(Qt6 COMPONENTS Core Gui Test QuickControls2)
endif()
if(NOT Qt6_FOUND)
find_package(Qt5 5.15 COMPONENTS Core Gui Test REQUIRED)
find_package(Qt5 5.15 COMPONENTS Core Gui Test QuickControls2 REQUIRED)
endif()

if (NOT Qt5_FOUND)
Expand Down
1 change: 1 addition & 0 deletions crates/cxx-qt-lib-headers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ repository.workspace = true
default = []
qt_gui = []
qt_qml = []
qt_quickcontrols = []

[dependencies]
cxx-qt-build.workspace = true
30 changes: 30 additions & 0 deletions crates/cxx-qt-lib-headers/include/quickcontrols/qquickstyle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// clang-format off
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// clang-format on
// SPDX-FileContributor: Joshua Goins <joshua.goins@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#pragma once

#ifdef CXX_QT_QUICKCONTROLS_FEATURE

#include <memory>

#include <QtQuickControls2/QQuickStyle>

namespace rust {
namespace cxxqtlib1 {

QString
qquickstyleName();

void
qquickstyleSetFallbackStyle(const QString& style);

void
qquickstyleSetStyle(const QString& style);

}
}

#endif
12 changes: 12 additions & 0 deletions crates/cxx-qt-lib-headers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ pub fn build_opts() -> cxx_qt_build::CxxQtBuildersOpts {
),
#[cfg(feature = "qt_qml")]
(include_str!("../include/qml/qqmlengine.h"), "qqmlengine.h"),
#[cfg(feature = "qt_quickcontrols")]
(
include_str!("../include/quickcontrols/qquickstyle.h"),
"qquickstyle.h",
),
(include_str!("../include/common.h"), "common.h"),
] {
opts = opts.header(file_contents, "cxx-qt-lib", file_name);
Expand All @@ -115,5 +120,12 @@ pub fn build_opts() -> cxx_qt_build::CxxQtBuildersOpts {
opts = opts.define("CXX_QT_QML_FEATURE").qt_module("Qml");
}

#[cfg(feature = "qt_quickcontrols")]
{
opts = opts
.define("CXX_QT_QUICKCONTROLS_FEATURE")
.qt_module("QuickControls2");
}

opts
}
3 changes: 2 additions & 1 deletion crates/cxx-qt-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ cxx-qt-build.workspace = true
cxx-qt-lib-headers.workspace = true

[features]
default = ["qt_gui", "qt_qml"]
default = ["qt_gui", "qt_qml", "qt_quickcontrols"]
bytes = ["dep:bytes"]
chrono = ["dep:chrono"]
http = ["dep:http"]
rgb = ["dep:rgb"]
qt_gui = ["cxx-qt-lib-headers/qt_gui"]
qt_qml = ["cxx-qt-lib-headers/qt_qml"]
qt_quickcontrols = ["cxx-qt-lib-headers/qt_quickcontrols"]
time = ["dep:time"]
url = ["dep:url"]
serde = ["dep:serde"]
Expand Down
9 changes: 9 additions & 0 deletions crates/cxx-qt-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cxx_qt_build::CxxQtBuilder;
fn main() {
let feature_qt_gui_enabled = std::env::var("CARGO_FEATURE_QT_GUI").is_ok();
let feature_qt_qml_enabled = std::env::var("CARGO_FEATURE_QT_QML").is_ok();
let feature_qt_quickcontrols_enabled = std::env::var("CARGO_FEATURE_QT_QUICKCONTROLS").is_ok();
let emscripten_targeted = match std::env::var("CARGO_CFG_TARGET_OS") {
Ok(val) => val == "emscripten",
Err(_) => false,
Expand Down Expand Up @@ -159,6 +160,10 @@ fn main() {
rust_bridges.extend(["qml/qqmlapplicationengine", "qml/qqmlengine"]);
}

if feature_qt_quickcontrols_enabled {
rust_bridges.extend(["quickcontrols/qquickstyle"]);
}

if !emscripten_targeted {
rust_bridges.extend([
"core/qdatetime",
Expand Down Expand Up @@ -220,6 +225,10 @@ fn main() {
cpp_files.extend(["qml/qqmlapplicationengine", "qml/qqmlengine"]);
}

if feature_qt_quickcontrols_enabled {
cpp_files.extend(["quickcontrols/qquickstyle"]);
}

if !emscripten_targeted {
cpp_files.extend(["core/qdatetime", "core/qtimezone"]);
}
Expand Down
5 changes: 5 additions & 0 deletions crates/cxx-qt-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ pub use crate::gui::*;
mod qml;
#[cfg(feature = "qt_qml")]
pub use crate::qml::*;

#[cfg(feature = "qt_quickcontrols")]
mod quickcontrols;
#[cfg(feature = "qt_quickcontrols")]
pub use crate::quickcontrols::*;
7 changes: 7 additions & 0 deletions crates/cxx-qt-lib/src/quickcontrols/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Joshua Goins <joshua.goins@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

mod qquickstyle;
pub use qquickstyle::QQuickStyle;
32 changes: 32 additions & 0 deletions crates/cxx-qt-lib/src/quickcontrols/qquickstyle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// clang-format off
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// clang-format on
// SPDX-FileContributor: Joshua Goins <joshua.goins@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#include "cxx-qt-lib/qquickstyle.h"

namespace rust {
namespace cxxqtlib1 {

QString
qquickstyleName()
{
return QQuickStyle::name();
}

void
qquickstyleSetFallbackStyle(const QString& style)
{
QQuickStyle::setFallbackStyle(style);
}

void
qquickstyleSetStyle(const QString& style)
{
QQuickStyle::setStyle(style);
}

}
}
48 changes: 48 additions & 0 deletions crates/cxx-qt-lib/src/quickcontrols/qquickstyle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Joshua Goins <joshua.goins@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#[cxx_qt::bridge(cxx_file_stem = "qquickstyle")]
mod ffi {
unsafe extern "C++Qt" {
include!("cxx-qt-lib/qquickstyle.h");
#[qobject]
type QQuickStyle;

include!("cxx-qt-lib/qstring.h");
type QString = crate::QString;
}

#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
#[doc(hidden)]
#[rust_name = "qquickstyle_name"]
fn qquickstyleName() -> QString;

#[doc(hidden)]
#[rust_name = "qquickstyle_set_fallback_style"]
fn qquickstyleSetFallbackStyle(style: &QString);

#[doc(hidden)]
#[rust_name = "qquickstyle_set_style"]
fn qquickstyleSetStyle(style: &QString);
}
}

use crate::QString;
pub use ffi::QQuickStyle;

impl QQuickStyle {
pub fn name() -> QString {
ffi::qquickstyle_name()
}

pub fn set_fallback_style(style: &QString) {
ffi::qquickstyle_set_fallback_style(style)
}

pub fn set_style(style: &QString) {
ffi::qquickstyle_set_style(style)
}
}
1 change: 1 addition & 0 deletions tests/basic_cxx_only/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ target_link_libraries(${CRATE} INTERFACE
Qt::Core
Qt::Gui
Qt::Qml
Qt::QuickControls2
)

add_executable(${APP_NAME}
Expand Down
1 change: 1 addition & 0 deletions tests/basic_cxx_qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ target_link_libraries(${CRATE} INTERFACE
Qt::Core
Qt::Gui
Qt::Qml
Qt::QuickControls2
)

add_executable(${APP_NAME} cpp/main.cpp)
Expand Down
1 change: 1 addition & 0 deletions tests/qt_types_standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ target_link_libraries(${CRATE} INTERFACE
Qt::Core
Qt::Gui
Qt::Qml
Qt::QuickControls2
)

add_executable(${APP_NAME}
Expand Down
Loading