Skip to content
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

Add QJSValue, QJSEngine, and related types to cxx-qt-lib-extras with QJSValue serde support #1147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion crates/cxx-qt-lib-extras/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ rust-version.workspace = true
cxx.workspace = true
cxx-qt.workspace = true
cxx-qt-lib = { workspace = true, features = ["qt_full"] }
serde = { version = "1", features=["derive"], optional = true }

[build-dependencies]
cxx-qt-build.workspace = true

[features]
default = []
default = ["serde"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I'm unsure if serde should be a default here

serde = ["dep:serde"]
link_qt_object_files = ["cxx-qt-build/link_qt_object_files"]

[lints]
Expand Down
9 changes: 9 additions & 0 deletions crates/cxx-qt-lib-extras/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn write_headers() {

write_headers_in("core");
write_headers_in("gui");
write_headers_in("qml");
}

fn main() {
Expand All @@ -57,6 +58,10 @@ fn main() {
"core/qcommandlineoption",
"core/qcommandlineparser",
"gui/qapplication",
"qml/qjsengine",
"qml/qjsvalue",
"qml/qjsvalueiterator",
"qml/qjsvaluelist",
];

for rust_source in &rust_bridges {
Expand All @@ -68,6 +73,10 @@ fn main() {
"core/qcommandlineoption",
"core/qcommandlineparser",
"gui/qapplication",
"qml/qjsengine",
"qml/qjsvalue",
"qml/qjsvalueiterator",
"qml/qjsvaluelist",
];

builder = builder.cc_builder(move |cc| {
Expand Down
81 changes: 81 additions & 0 deletions crates/cxx-qt-lib-extras/include/qml/qjsengine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#pragma once

#include <memory>

#include <QtQml/QJSEngine>

#include "rust/cxx.h"

namespace rust
{
namespace cxxqtlib2
{
::std::unique_ptr<QJSEngine>
qjsengineNew();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to replace this with our make_unique template from cxx-qt-lib.

Take a look at the qpainter_init_default() function in qpainter.rs for how to use this.


template <typename T>
::std::unique_ptr<QJSValue>
jsengineNewArray(T &engine, quint32 length)
{
auto ptr = ::std::make_unique<QJSValue>(engine.newArray(length));
Q_ASSERT(ptr != nullptr);

return ptr;
}

template <typename T>
::std::unique_ptr<QJSValue>
jsengineNewObject(T &engine)
{
auto ptr = ::std::make_unique<QJSValue>(engine.newObject());
Q_ASSERT(ptr != nullptr);

return ptr;
}

template <typename T>
::std::unique_ptr<QJSValue>
jsengineNewQObject(T &engine, QObject &object)
{
auto ptr = ::std::make_unique<QJSValue>(engine.newQObject(&object));
Q_ASSERT(ptr != nullptr);

return ptr;
}

template <typename T>
::std::unique_ptr<QJSValue>
jsengineEvaluate(
T &engine,
const QString &program,
const QString &fileName,
int lineNumber)
{
auto ptr = ::std::make_unique<QJSValue>(
engine.evaluate(program, fileName, lineNumber, nullptr));
Q_ASSERT(ptr != nullptr);

return ptr;
}

template <typename T>
::std::unique_ptr<QJSValue>
jsengineImportModule(T &engine, const QString &fileName)
{
auto ptr = ::std::make_unique<QJSValue>(engine.importModule(fileName));
Q_ASSERT(ptr != nullptr);

return ptr;
}

template <typename T>
::std::unique_ptr<QJSValue>
jsengineGlobalObject(T &engine)
{
auto ptr = ::std::make_unique<QJSValue>(engine.globalObject());
Q_ASSERT(ptr != nullptr);

return ptr;
}
}
}
36 changes: 36 additions & 0 deletions crates/cxx-qt-lib-extras/include/qml/qjsvalue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <QtQml/QJSValue>
#include <QtCore/QVariant>

namespace rust
{
namespace cxxqtlib2
{

::std::unique_ptr<QJSValue> qjsvalue_new();
::std::unique_ptr<QJSValue> qjsvalue_new_null();
::std::unique_ptr<QJSValue> qjsvalue_new_bool(bool value);
::std::unique_ptr<QJSValue> qjsvalue_new_int(int value);
::std::unique_ptr<QJSValue> qjsvalue_new_uint(uint value);
::std::unique_ptr<QJSValue> qjsvalue_new_double(double value);
::std::unique_ptr<QJSValue> qjsvalue_new_qstring(const QString &value);

::std::unique_ptr<QJSValue> qjsvalue_from_jsvalue(const QJSValue &value);

QString qjsvalue_to_string(const QJSValue &value);

::std::unique_ptr<QJSValue> qjsvalue_property(
const QJSValue &value,
const QString &name);
::std::unique_ptr<QJSValue> qjsvalue_element(
const QJSValue &value,
quint32 index);

QVariant qjsvalue_to_qvariant(const QJSValue &value);
QObject *qjsvalue_to_qobject(QJSValue &value);

bool qvariantCanConvertQJSValue(const QVariant &variant);
::std::unique_ptr<QJSValue> qjsvalueFromQVariant(const QVariant &variant) noexcept;
}
}
15 changes: 15 additions & 0 deletions crates/cxx-qt-lib-extras/include/qml/qjsvalueiterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <QtQml/QJSValue>
#include <QtQml/QJSValueIterator>

namespace rust
{
namespace cxxqtlib2
{

::std::unique_ptr<QJSValueIterator> qjsvalueiterator_new(const QJSValue &value);

::std::unique_ptr<QJSValue> qjsvalueiterator_value(const QJSValueIterator &value);
}
}
32 changes: 32 additions & 0 deletions crates/cxx-qt-lib-extras/include/qml/qjsvaluelist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <QtCore/QList>
#include <QtQml/QJSValue>

// Define a proper operator== for QJSValue
#ifndef QJSVALUE_OPERATOR_EQ_DEFINED
#define QJSVALUE_OPERATOR_EQ_DEFINED

inline bool operator==(const QJSValue& lhs, const QJSValue& rhs)
{
return lhs.strictlyEquals(rhs); // Example using strictlyEquals method
}

#endif // QJSVALUE_OPERATOR_EQ_DEFINED

namespace rust
{
namespace cxxqtlib2
{

class QJSValueList : public QList<QJSValue>
{
public:
QJSValueList() : QList<QJSValue>() {}
~QJSValueList() {}
};

::std::unique_ptr<QJSValueList> qjsvaluelistNew();
::std::unique_ptr<QJSValueList> qjsvaluelistClone(const QJSValueList &list);
}
}
3 changes: 3 additions & 0 deletions crates/cxx-qt-lib-extras/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ pub use crate::core::*;

mod gui;
pub use crate::gui::*;

mod qml;
pub use crate::qml::*;
Loading
Loading