Skip to content
Draft
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
12 changes: 11 additions & 1 deletion crates/cxx-qt-lib/src/core/qdate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
let date = ffi::qdate_from_string_enum(string, format);
if date.is_valid() {
Expand All @@ -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)
Expand Down
22 changes: 14 additions & 8 deletions crates/cxx-qt-lib/src/core/qdatetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
pub fn from_string_enum(string: &ffi::QString, format: ffi::DateFormat) -> Option<Self> {
let date = ffi::qdatetime_from_string(string, format);
if date.is_valid() {
Some(date)
Expand All @@ -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) {
Expand Down Expand Up @@ -393,7 +398,8 @@ impl<Tz: chrono::TimeZone> TryFrom<chrono::DateTime<Tz>> for QDateTime {
type Error = &'static str;

fn try_from(value: chrono::DateTime<Tz>) -> Result<Self, Self::Error> {
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())?,
Expand Down Expand Up @@ -439,7 +445,7 @@ impl TryFrom<QDateTime> for chrono::DateTime<chrono::Utc> {
#[cfg(feature = "time")]
impl From<time::OffsetDateTime> 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()),
Expand Down Expand Up @@ -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);
}
Expand All @@ -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::<chrono::FixedOffset>::try_from(qdatetime).unwrap(),
Expand Down Expand Up @@ -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::<chrono::Utc>::try_from(qdatetime).unwrap(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cxx-qt-lib/src/core/qtimezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
pub fn owned_from_offset_seconds(offset_seconds: i32) -> cxx::UniquePtr<Self> {
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<Self> {
pub fn owned_from_iana(iana_id: &ffi::QByteArray) -> cxx::UniquePtr<Self> {
ffi::qtimezone_from_iana(iana_id)
}

Expand Down
8 changes: 8 additions & 0 deletions crates/cxx-qt-lib/src/gui/qimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/rust/src/qtimezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod qtimezone_cxx {
}

fn construct_qtimezone() -> cxx::UniquePtr<QTimeZone> {
QTimeZone::from_iana(&QByteArray::from("Europe/London"))
QTimeZone::owned_from_iana(&QByteArray::from("Europe/London"))
}

fn read_qtimezone(t: &QTimeZone) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion tests/qt_types_standalone/rust/src/qvariant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
Loading