From 9b2203f0132460d530ab44061ccfe9b1194243d1 Mon Sep 17 00:00:00 2001 From: Ben Ford Date: Wed, 21 May 2025 14:25:56 +0100 Subject: [PATCH] Add _default constructors and rename constructors returning UniquePtr --- crates/cxx-qt-lib/src/core/qdate.rs | 12 +++++++++- crates/cxx-qt-lib/src/core/qdatetime.rs | 22 ++++++++++++------- crates/cxx-qt-lib/src/core/qtimezone.rs | 4 ++-- crates/cxx-qt-lib/src/gui/qimage.rs | 8 +++++++ .../qt_types_standalone/rust/src/qtimezone.rs | 2 +- .../qt_types_standalone/rust/src/qvariant.rs | 2 +- 6 files changed, 37 insertions(+), 13 deletions(-) diff --git a/crates/cxx-qt-lib/src/core/qdate.rs b/crates/cxx-qt-lib/src/core/qdate.rs index e1460dc62..d31782492 100644 --- a/crates/cxx-qt-lib/src/core/qdate.rs +++ b/crates/cxx-qt-lib/src/core/qdate.rs @@ -186,7 +186,12 @@ impl QDate { } } - /// Returns the time represented in the string as a QTime using the format given, or None if this is not possible. + /// Returns the QTime represented by the string, using the format given, or the Qt invalid date if not possible. + pub fn from_string_or_default(string: &ffi::QString, format: &ffi::QString) -> Self { + ffi::qdate_from_string(string, format) + } + + /// Returns the QTime represented by the string, using the format given in the enum, or None if this is not possible. pub fn from_string_enum(string: &ffi::QString, format: ffi::DateFormat) -> Option { let date = ffi::qdate_from_string_enum(string, format); if date.is_valid() { @@ -196,6 +201,11 @@ impl QDate { } } + /// Returns the QTime represented by the string, using the format given in the enum, or the Qt invalid date if not possible. + pub fn from_string_enum_or_default(string: &ffi::QString, format: ffi::DateFormat) -> Self { + ffi::qdate_from_string_enum(string, format) + } + /// Returns true if the specified year is a leap year in the Gregorian calendar; otherwise returns false. pub fn is_leap_year(year: i32) -> bool { ffi::qdate_is_leap_year(year) diff --git a/crates/cxx-qt-lib/src/core/qdatetime.rs b/crates/cxx-qt-lib/src/core/qdatetime.rs index 231f388d9..cdd260bf5 100644 --- a/crates/cxx-qt-lib/src/core/qdatetime.rs +++ b/crates/cxx-qt-lib/src/core/qdatetime.rs @@ -305,7 +305,7 @@ impl QDateTime { } /// Returns the datetime represented in the string as a QDateTime using the format given, or None if this is not possible. - pub fn from_string(string: &ffi::QString, format: ffi::DateFormat) -> Option { + pub fn from_string_enum(string: &ffi::QString, format: ffi::DateFormat) -> Option { let date = ffi::qdatetime_from_string(string, format); if date.is_valid() { Some(date) @@ -314,6 +314,11 @@ impl QDateTime { } } + /// Returns the QDateTime represented by the string, using the format given in the enum, or the Qt invalid date if not possible. + pub fn from_string_enum_or_default(string: &ffi::QString, format: ffi::DateFormat) -> Self { + ffi::qdatetime_from_string(string, format) + } + /// Sets the date part of this datetime to date. If no time is set yet, it is set to midnight. /// If date is invalid, this QDateTime becomes invalid. pub fn set_date(&mut self, date: QDate) { @@ -393,7 +398,8 @@ impl TryFrom> for QDateTime { type Error = &'static str; fn try_from(value: chrono::DateTime) -> Result { - let tz = crate::QTimeZone::from_offset_seconds(value.offset().fix().local_minus_utc()); + let tz = + crate::QTimeZone::owned_from_offset_seconds(value.offset().fix().local_minus_utc()); Ok(QDateTime::from_date_and_time_time_zone( &QDate::from(value.date_naive()), &QTime::try_from(value.time())?, @@ -439,7 +445,7 @@ impl TryFrom for chrono::DateTime { #[cfg(feature = "time")] impl From for QDateTime { fn from(value: time::OffsetDateTime) -> Self { - let tz = crate::QTimeZone::from_offset_seconds(value.offset().whole_seconds()); + let tz = crate::QTimeZone::owned_from_offset_seconds(value.offset().whole_seconds()); QDateTime::from_date_and_time_time_zone( &QDate::from(value.date()), &QTime::from(value.time()), @@ -539,7 +545,7 @@ mod test_chrono { &QDate::new(2023, 1, 1), // Chrono adds the offset to the given time, so add the offset here to match Chrono &QTime::new(1 + 1 /* plus the offset */, 2, 3, 4), - &ffi::QTimeZone::from_offset_seconds(60 * 60), + &ffi::QTimeZone::owned_from_offset_seconds(60 * 60), ); assert_eq!(QDateTime::try_from(datetime_east).unwrap(), qdatetime); } @@ -562,7 +568,7 @@ mod test_chrono { let qdatetime = QDateTime::from_date_and_time_time_zone( &QDate::new(2023, 1, 1), &QTime::new(1, 2, 3, 4), - &ffi::QTimeZone::from_offset_seconds(60 * 60), + &ffi::QTimeZone::owned_from_offset_seconds(60 * 60), ); assert_eq!( chrono::DateTime::::try_from(qdatetime).unwrap(), @@ -611,7 +617,7 @@ mod test_chrono { &QDate::new(2023, 1, 1), &QTime::new(1, 2, 3, 4), // Should cause one hour offset when in chrono::DateTime - &ffi::QTimeZone::from_offset_seconds(60 * 60), + &ffi::QTimeZone::owned_from_offset_seconds(60 * 60), ); assert_eq!( chrono::DateTime::::try_from(qdatetime).unwrap(), @@ -636,7 +642,7 @@ mod test_time { let qdatetime = QDateTime::from_date_and_time_time_zone( &QDate::new(2023, 1, 1), &QTime::new(1, 2, 3, 4), - &ffi::QTimeZone::from_offset_seconds(60 * 60), + &ffi::QTimeZone::owned_from_offset_seconds(60 * 60), ); assert_eq!( time::OffsetDateTime::try_from(qdatetime).unwrap(), @@ -673,7 +679,7 @@ mod test_time { let qdatetime = QDateTime::from_date_and_time_time_zone( &QDate::new(2023, 1, 1), &QTime::new(1, 2, 3, 4), - &ffi::QTimeZone::from_offset_seconds(60 * 60), + &ffi::QTimeZone::owned_from_offset_seconds(60 * 60), ); assert_eq!(QDateTime::from(time_offsetdatetime), qdatetime); } diff --git a/crates/cxx-qt-lib/src/core/qtimezone.rs b/crates/cxx-qt-lib/src/core/qtimezone.rs index 692b7e6de..04a71efee 100644 --- a/crates/cxx-qt-lib/src/core/qtimezone.rs +++ b/crates/cxx-qt-lib/src/core/qtimezone.rs @@ -187,12 +187,12 @@ impl QTimeZone { } /// Creates an instance of a time zone with the requested Offset from UTC of offsetSeconds. - pub fn from_offset_seconds(offset_seconds: i32) -> cxx::UniquePtr { + pub fn owned_from_offset_seconds(offset_seconds: i32) -> cxx::UniquePtr { ffi::qtimezone_from_offset_seconds(offset_seconds) } /// Creates an instance of the requested time zone ianaId. - pub fn from_iana(iana_id: &ffi::QByteArray) -> cxx::UniquePtr { + pub fn owned_from_iana(iana_id: &ffi::QByteArray) -> cxx::UniquePtr { ffi::qtimezone_from_iana(iana_id) } diff --git a/crates/cxx-qt-lib/src/gui/qimage.rs b/crates/cxx-qt-lib/src/gui/qimage.rs index d595b24f4..bc373e35a 100644 --- a/crates/cxx-qt-lib/src/gui/qimage.rs +++ b/crates/cxx-qt-lib/src/gui/qimage.rs @@ -385,6 +385,14 @@ impl QImage { } } + /// Convert raw image data to a [`QImage`]. + /// + /// If no `format` is provided, the format will be quessed from the image header, + /// and if the format still cannot be guessed, the invalid QImage will be returned + pub fn from_data_or_default(data: &[u8], format: Option<&str>) -> Self { + ffi::qimage_init_from_data(data, format.unwrap_or("")) + } + /// Constructs a QImage from an existing memory buffer. /// /// # Safety diff --git a/tests/qt_types_standalone/rust/src/qtimezone.rs b/tests/qt_types_standalone/rust/src/qtimezone.rs index 581188842..0fc235c2d 100644 --- a/tests/qt_types_standalone/rust/src/qtimezone.rs +++ b/tests/qt_types_standalone/rust/src/qtimezone.rs @@ -20,7 +20,7 @@ mod qtimezone_cxx { } fn construct_qtimezone() -> cxx::UniquePtr { - QTimeZone::from_iana(&QByteArray::from("Europe/London")) + QTimeZone::owned_from_iana(&QByteArray::from("Europe/London")) } fn read_qtimezone(t: &QTimeZone) -> bool { diff --git a/tests/qt_types_standalone/rust/src/qvariant.rs b/tests/qt_types_standalone/rust/src/qvariant.rs index 880c433ae..ca224ab80 100644 --- a/tests/qt_types_standalone/rust/src/qvariant.rs +++ b/tests/qt_types_standalone/rust/src/qvariant.rs @@ -64,7 +64,7 @@ fn construct_qvariant(test: VariantTest) -> QVariant { VariantTest::QDateTime => QVariant::from(&QDateTime::from_date_and_time_time_zone( &QDate::new(2022, 1, 1), &QTime::new(1, 2, 3, 4), - &QTimeZone::from_offset_seconds(0), + &QTimeZone::owned_from_offset_seconds(0), )), VariantTest::QPoint => QVariant::from(&QPoint::new(1, 3)), VariantTest::QPointF => QVariant::from(&QPointF::new(1.0, 3.0)),