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

qtypes: add support for Qt alias types that don't match #904

Open
wants to merge 3 commits 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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/KDAB/cxx-qt/compare/v0.7.0...HEAD)

### Added

- Support for further types: `qreal`, `qint64`, `qintptr`, `qsizetype`, `quint64`, `quintptr`
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

do we also want qlonglong, qulonglong, or qptrdiff ?

See https://doc.qt.io/qt-6/qttypes.html#types for full types in Qt


## [0.7.0](https://github.com/KDAB/cxx-qt/compare/v0.6.1...v0.7.0) - 2024-10-30

### Added
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 @@ -147,6 +147,7 @@ fn main() {
"core/qstringlist",
"core/qt",
"core/qtime",
"core/qtypes",
"core/qurl",
"core/qvariant/mod",
"core/qvariant/qvariant_bool",
Expand Down Expand Up @@ -271,6 +272,7 @@ fn main() {
"core/qstring",
"core/qstringlist",
"core/qtime",
"core/qtypes",
"core/qurl",
"core/qvariant/qvariant",
"core/qvector/qvector",
Expand Down
4 changes: 1 addition & 3 deletions crates/cxx-qt-lib/include/core/qdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
namespace rust {
namespace cxxqtlib1 {

QDate
qdateAddDays(const QDate& date, ::std::int64_t ndays);
QDate
qdateCurrentDate();
QDate
qdateFromString(const QString& string, const QString& format);
QDate
qdateFromString(const QString& string, Qt::DateFormat format);
// In Qt 5 d is const-ref, in Qt 6 it is value
::std::int64_t
qint64
qdateDaysTo(const QDate& date, QDate d);
bool
qdateIsLeapYear(::std::int32_t year);
Expand Down
28 changes: 4 additions & 24 deletions crates/cxx-qt-lib/include/core/qdatetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,24 @@ struct IsRelocatable<QDateTime> : ::std::true_type
namespace rust {
namespace cxxqtlib1 {

QDateTime
qdatetimeAddDays(const QDateTime& datetime, ::std::int64_t ndays);
QDateTime
qdatetimeAddMSecs(const QDateTime& datetime, ::std::int64_t msecs);
QDateTime
qdatetimeAddSecs(const QDateTime& datetime, ::std::int64_t secs);
QDateTime
qdatetimeCurrentDateTime();
QDateTime
qdatetimeCurrentDateTimeUtc();
::std::int64_t
qint64
qdatetimeCurrentMSecsSinceEpoch();
::std::int64_t
qint64
qdatetimeCurrentSecsSinceEpoch();
::std::int64_t
qdatetimeDaysTo(const QDateTime& datetime, const QDateTime& other);
QDateTime
qdatetimeFromMSecsSinceEpoch(::std::int64_t msecs, const QTimeZone& timeZone);
qdatetimeFromMSecsSinceEpoch(qint64 msecs, const QTimeZone& timeZone);
QDateTime
qdatetimeFromSecsSinceEpoch(::std::int64_t secs, const QTimeZone& timeZone);
::std::int64_t
qdatetimeMSecsTo(const QDateTime& datetime, const QDateTime& other);
::std::int64_t
qdatetimeSecsTo(const QDateTime& datetime, const QDateTime& other);
qdatetimeFromSecsSinceEpoch(qint64 secs, const QTimeZone& timeZone);
void
qdatetimeSetDate(QDateTime& datetime, QDate date);
void
qdatetimeSetMSecsSinceEpoch(QDateTime& datetime, ::std::int64_t msecs);
void
qdatetimeSetSecsSinceEpoch(QDateTime& datetime, ::std::int64_t secs);
void
qdatetimeSetTime(QDateTime& datetime, QTime time);
::std::unique_ptr<QTimeZone>
qdatetimeTimeZone(const QDateTime& datetime);
::std::int64_t
qdatetimeToMSecsSinceEpoch(const QDateTime& datetime);
::std::int64_t
qdatetimeToSecsSinceEpoch(const QDateTime& datetime);
void
qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone);
}
Expand Down
9 changes: 0 additions & 9 deletions crates/cxx-qt-lib/include/core/qmodelindex.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,3 @@
#include <cinttypes>

#include <QtCore/QModelIndex>

namespace rust {
namespace cxxqtlib1 {

::std::size_t
qmodelindexInternalId(const QModelIndex& index);

}
}
9 changes: 9 additions & 0 deletions crates/cxx-qt-lib/include/core/qtypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// clang-format off
// SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// clang-format on
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0
#pragma once

#include <QtCore/Qt>
3 changes: 3 additions & 0 deletions crates/cxx-qt-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pub use qt::{
mod qtime;
pub use qtime::QTime;

mod qtypes;
pub use qtypes::{qint64, qintptr, qreal, qsizetype, quint64, quintptr};

#[cfg(not(target_os = "emscripten"))]
mod qtimezone;
#[cfg(not(target_os = "emscripten"))]
Expand Down
10 changes: 2 additions & 8 deletions crates/cxx-qt-lib/src/core/qdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,17 @@ static_assert(::std::is_trivially_copyable<QDate>::value,
namespace rust {
namespace cxxqtlib1 {

QDate
qdateAddDays(const QDate& date, ::std::int64_t ndays)
{
return date.addDays(static_cast<qint64>(ndays));
}

QDate
qdateCurrentDate()
{
return QDate::currentDate();
}

::std::int64_t
qint64
qdateDaysTo(const QDate& date, QDate d)
{
// In Qt 5 d is const-ref, in Qt 6 it is value
return static_cast<::std::int64_t>(date.daysTo(d));
return date.daysTo(d);
}

QDate
Expand Down
32 changes: 16 additions & 16 deletions crates/cxx-qt-lib/src/core/qdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ mod ffi {
type DateFormat = crate::DateFormat;
}

unsafe extern "C++" {
include!("cxx-qt-lib/qtypes.h");
type qint64 = crate::qint64;
}

unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = crate::QString;

include!("cxx-qt-lib/qdate.h");
type QDate = super::QDate;

/// Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).
///
/// Returns a null date if the current date is invalid or the new date is out of range.
#[rust_name = "add_days"]
fn addDays(self: &QDate, ndays: qint64) -> QDate;

/// Returns a QDate object containing a date nmonths later than the date of this object (or earlier if nmonths is negative).
#[rust_name = "add_months"]
fn addMonths(self: &QDate, nmonths: i32) -> QDate;
Expand Down Expand Up @@ -76,17 +87,13 @@ mod ffi {

#[namespace = "rust::cxxqtlib1"]
unsafe extern "C++" {
#[doc(hidden)]
#[rust_name = "qdate_add_days"]
fn qdateAddDays(date: &QDate, ndays: i64) -> QDate;

#[doc(hidden)]
#[rust_name = "qdate_current_date"]
fn qdateCurrentDate() -> QDate;

#[doc(hidden)]
#[rust_name = "qdate_days_to"]
fn qdateDaysTo(date: &QDate, d: QDate) -> i64;
fn qdateDaysTo(date: &QDate, d: QDate) -> qint64;

#[doc(hidden)]
#[rust_name = "qdate_from_string"]
Expand Down Expand Up @@ -147,13 +154,6 @@ impl fmt::Debug for QDate {
}

impl QDate {
/// Returns a QDate object containing a date ndays later than the date of this object (or earlier if ndays is negative).
///
/// Returns a null date if the current date is invalid or the new date is out of range.
pub fn add_days(&self, ndays: i64) -> Self {
ffi::qdate_add_days(self, ndays)
}

// Returns the current date, as reported by the system clock.
pub fn current_date() -> Self {
ffi::qdate_current_date()
Expand All @@ -162,7 +162,7 @@ impl QDate {
/// Returns the number of days from this date to d (which is negative if d is earlier than this date).
///
/// Returns 0 if either date is invalid.
pub fn days_to(&self, date: Self) -> i64 {
pub fn days_to(&self, date: Self) -> ffi::qint64 {
ffi::qdate_days_to(self, date)
}

Expand Down Expand Up @@ -271,15 +271,15 @@ mod test {
#[test]
fn qdate_current_date() {
let date_a = QDate::current_date();
let date_b = date_a.add_days(100);
assert_eq!(date_a.days_to(date_b), 100);
let date_b = date_a.add_days(100.into());
assert_eq!(i64::from(date_a.days_to(date_b)), 100);
}

#[test]
fn qdate_julian_day() {
let date_a = QDate::from_julian_day(1000);
let date_b = QDate::from_julian_day(1010);
assert_eq!(date_a.days_to(date_b), 10);
assert_eq!(i64::from(date_a.days_to(date_b)), 10);
}

#[cfg(feature = "chrono")]
Expand Down
74 changes: 6 additions & 68 deletions crates/cxx-qt-lib/src/core/qdatetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,6 @@ static_assert(QTypeInfo<QDateTime>::isRelocatable);
namespace rust {
namespace cxxqtlib1 {

QDateTime
qdatetimeAddDays(const QDateTime& datetime, ::std::int64_t ndays)
{
return datetime.addDays(static_cast<qint64>(ndays));
}

QDateTime
qdatetimeAddMSecs(const QDateTime& datetime, ::std::int64_t msecs)
{
return datetime.addMSecs(static_cast<qint64>(msecs));
}

QDateTime
qdatetimeAddSecs(const QDateTime& datetime, ::std::int64_t secs)
{
return datetime.addSecs(static_cast<qint64>(secs));
}

QDateTime
qdatetimeCurrentDateTime()
{
Expand All @@ -57,48 +39,28 @@ qdatetimeCurrentDateTimeUtc()
return QDateTime::currentDateTimeUtc();
}

::std::int64_t
qint64
qdatetimeCurrentMSecsSinceEpoch()
{
return QDateTime::currentMSecsSinceEpoch();
}

::std::int64_t
qint64
qdatetimeCurrentSecsSinceEpoch()
{
return QDateTime::currentSecsSinceEpoch();
}

::std::int64_t
qdatetimeDaysTo(const QDateTime& datetime, const QDateTime& other)
{
return static_cast<::std::int64_t>(datetime.daysTo(other));
}

QDateTime
qdatetimeFromMSecsSinceEpoch(::std::int64_t msecs, const QTimeZone& timeZone)
qdatetimeFromMSecsSinceEpoch(qint64 msecs, const QTimeZone& timeZone)
{
return QDateTime::fromMSecsSinceEpoch(static_cast<qint64>(msecs), timeZone);
return QDateTime::fromMSecsSinceEpoch(msecs, timeZone);
}

QDateTime
qdatetimeFromSecsSinceEpoch(::std::int64_t secs, const QTimeZone& timeZone)
qdatetimeFromSecsSinceEpoch(qint64 secs, const QTimeZone& timeZone)
{
return QDateTime::fromSecsSinceEpoch(static_cast<qint64>(secs), timeZone);
}

::std::int64_t
qdatetimeMSecsTo(const QDateTime& datetime, const QDateTime& other)
{

return static_cast<::std::int64_t>(datetime.msecsTo(other));
}

::std::int64_t
qdatetimeSecsTo(const QDateTime& datetime, const QDateTime& other)
{

return static_cast<::std::int64_t>(datetime.secsTo(other));
return QDateTime::fromSecsSinceEpoch(secs, timeZone);
}

void
Expand All @@ -107,18 +69,6 @@ qdatetimeSetDate(QDateTime& datetime, QDate date)
datetime.setDate(date);
}

void
qdatetimeSetMSecsSinceEpoch(QDateTime& datetime, ::std::int64_t msecs)
{
datetime.setMSecsSinceEpoch(static_cast<qint64>(msecs));
}

void
qdatetimeSetSecsSinceEpoch(QDateTime& datetime, ::std::int64_t secs)
{
datetime.setSecsSinceEpoch(static_cast<qint64>(secs));
}

void
qdatetimeSetTime(QDateTime& datetime, QTime time)
{
Expand All @@ -131,18 +81,6 @@ qdatetimeTimeZone(const QDateTime& datetime)
return ::std::make_unique<QTimeZone>(datetime.timeZone());
}

::std::int64_t
qdatetimeToMSecsSinceEpoch(const QDateTime& datetime)
{
return static_cast<::std::int64_t>(datetime.toMSecsSinceEpoch());
}

::std::int64_t
qdatetimeToSecsSinceEpoch(const QDateTime& datetime)
{
return static_cast<::std::int64_t>(datetime.toSecsSinceEpoch());
}

void
qdatetimeSetTimeZone(QDateTime& datetime, const QTimeZone& timeZone)
{
Expand Down
Loading
Loading