Skip to content

Commit

Permalink
cxx-qt-lib: add QGuiApplication and use it in example
Browse files Browse the repository at this point in the history
Related to KDAB#291
  • Loading branch information
ahayzen-kdab authored and przempore committed Mar 15, 2023
1 parent 132758a commit be41e68
Show file tree
Hide file tree
Showing 17 changed files with 486 additions and 55 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Added

- Support for container types: `QSet<T>`, `QHash<K, V>`, `QList<T>`, `QMap<K, V>`, `QVector<T>`
- Support for further types: `QByteArray`, `QModelIndex`, `QPersistentModelIndex`, `QStringList`, `QVector2D`, `QVector3D`, `QVector4D`
- Support for further types: `QByteArray`, `QGuiApplication`, `QModelIndex`, `QPersistentModelIndex`, `QStringList`, `QVector2D`, `QVector3D`, `QVector4D`
- Support for nesting objects in properties, invokables, and signals with `*mut T`

### Changed
Expand Down
57 changes: 57 additions & 0 deletions crates/cxx-qt-lib-headers/include/gui/qguiapplication.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// clang-format off
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// clang-format on
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#pragma once

#ifdef CXX_QT_GUI_FEATURE
#include <cstdint>
#include <memory>

#include <QtCore/QByteArray>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVector>
#include <QtGui/QGuiApplication>

#include "rust/cxx.h"

namespace rust {
namespace cxxqtlib1 {

::std::unique_ptr<QGuiApplication>
qguiapplicationNew(const QVector<QByteArray>& args);

void
qguiapplicationAddLibraryPath(QGuiApplication& app, const QString& path);
QString
qguiapplicationApplicationName(const QGuiApplication& app);
QString
qguiapplicationApplicationVersion(const QGuiApplication& app);
::std::int32_t
qguiapplicationExec(QGuiApplication& app);
QStringList
qguiapplicationLibraryPaths(const QGuiApplication& app);
QString
qguiapplicationOrganizationDomain(const QGuiApplication& app);
QString
qguiapplicationOrganizationName(const QGuiApplication& app);
void
qguiapplicationSetApplicationName(QGuiApplication& app, const QString& name);
void
qguiapplicationSetApplicationVersion(QGuiApplication& app,
const QString& version);
void
qguiapplicationSetLibraryPaths(QGuiApplication& app, const QStringList& paths);
void
qguiapplicationSetOrganizationDomain(QGuiApplication& app,
const QString& domain);
void
qguiapplicationSetOrganizationName(QGuiApplication& app, const QString& name);

}
}

#endif
5 changes: 5 additions & 0 deletions crates/cxx-qt-lib-headers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ pub fn write_headers(directory: impl AsRef<Path>) {
#[cfg(feature = "qt_gui")]
(include_str!("../include/gui/qcolor.h"), "qcolor.h"),
#[cfg(feature = "qt_gui")]
(
include_str!("../include/gui/qguiapplication.h"),
"qguiapplication.h",
),
#[cfg(feature = "qt_gui")]
(include_str!("../include/gui/qvector2d.h"), "qvector2d.h"),
#[cfg(feature = "qt_gui")]
(include_str!("../include/gui/qvector3d.h"), "qvector3d.h"),
Expand Down
2 changes: 2 additions & 0 deletions crates/cxx-qt-lib/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ fn main() {
"core/qvariant/qvariant_qcolor",
"core/qvector/qvector_qcolor",
"gui/qcolor",
"gui/qguiapplication",
"gui/qvector2d",
"gui/qvector3d",
"gui/qvector4d",
Expand Down Expand Up @@ -184,6 +185,7 @@ fn main() {
if feature_qt_gui_enabled {
cpp_files.extend([
"gui/qcolor",
"gui/qguiapplication",
"gui/qvector2d",
"gui/qvector3d",
"gui/qvector4d",
Expand Down
3 changes: 3 additions & 0 deletions crates/cxx-qt-lib/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
mod qcolor;
pub use qcolor::QColor;

mod qguiapplication;
pub use qguiapplication::QGuiApplication;

mod qvector2d;
pub use qvector2d::QVector2D;

Expand Down
138 changes: 138 additions & 0 deletions crates/cxx-qt-lib/src/gui/qguiapplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// clang-format off
// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
// clang-format on
// SPDX-FileContributor: Andrew Hayzen <[email protected]>
// SPDX-FileContributor: Leon Matthes <[email protected]>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

#ifdef CXX_QT_GUI_FEATURE
#include "cxx-qt-lib/qguiapplication.h"

#include <vector>

#include <QtCore/QObject>

namespace {

class ArgsData : public QObject
{
public:
explicit ArgsData(const QVector<QByteArray>& args, QObject* parent = nullptr)
: QObject(parent)
, m_ownedVector(args)
{
// Construct our vector of char*
for (auto& bytes : m_ownedVector) {
m_vector.emplace_back(bytes.data());
m_size += 1;
}
}

char** data() { return m_vector.data(); }
int& size() { return m_size; }

private:
QVector<QByteArray> m_ownedVector;
int m_size = 0;
std::vector<char*> m_vector;
};

}

namespace rust {
namespace cxxqtlib1 {

::std::unique_ptr<QGuiApplication>
qguiapplicationNew(const QVector<QByteArray>& args)
{
// Ensure that our QVector has the same lifetime as the QGuiApplication
// by storing it inside a QObject that has QGuiApplication as it's parent
auto argsData = new ArgsData(args);
// Note that QGuiApplication uses a reference to an int for the size here
// so we need to ensure that reference remains valid
auto ptr =
::std::make_unique<QGuiApplication>(argsData->size(), argsData->data());
Q_ASSERT(ptr != nullptr);
argsData->setParent(ptr.get());

return ptr;
}

void
qguiapplicationAddLibraryPath(QGuiApplication& app, const QString& path)
{
app.addLibraryPath(path);
}

QString
qguiapplicationApplicationName(const QGuiApplication& app)
{
return app.applicationName();
}

QString
qguiapplicationApplicationVersion(const QGuiApplication& app)
{
return app.applicationVersion();
}

::std::int32_t
qguiapplicationExec(QGuiApplication& app)
{
return static_cast<::std::int32_t>(app.exec());
}

QStringList
qguiapplicationLibraryPaths(const QGuiApplication& app)
{
return app.libraryPaths();
}

QString
qguiapplicationOrganizationDomain(const QGuiApplication& app)
{
return app.organizationDomain();
}

QString
qguiapplicationOrganizationName(const QGuiApplication& app)
{
return app.organizationName();
}

void
qguiapplicationSetApplicationName(QGuiApplication& app, const QString& name)
{
app.setApplicationName(name);
}

void
qguiapplicationSetApplicationVersion(QGuiApplication& app,
const QString& version)
{
app.setApplicationVersion(version);
}

void
qguiapplicationSetLibraryPaths(QGuiApplication& app, const QStringList& paths)
{
app.setLibraryPaths(paths);
}

void
qguiapplicationSetOrganizationDomain(QGuiApplication& app,
const QString& domain)
{
app.setOrganizationDomain(domain);
}

void
qguiapplicationSetOrganizationName(QGuiApplication& app, const QString& name)
{
app.setOrganizationName(name);
}

}
}
#endif
Loading

0 comments on commit be41e68

Please sign in to comment.