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

Qvariant improvements #308

Merged
merged 8 commits into from
Feb 25, 2024
Merged
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
18 changes: 18 additions & 0 deletions qttypes/src/qtcore/qlist/qvariantlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ impl IndexMut<usize> for QVariantList {
}
}

impl std::fmt::Debug for QVariantList {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_map().entries(self.into_iter().enumerate()).finish()
}
}

impl<'a> IntoIterator for &'a QVariantList {
type Item = &'a QVariant;
type IntoIter = QListIterator<'a, QVariantList, QVariant>;
Expand Down Expand Up @@ -160,4 +166,16 @@ mod tests {
assert_eq!(qs2.to_string(), "hello");
assert_eq!(qba4.to_string(), "hello");
}

#[test]
fn qvariantlist_debug() {
let mut list = QVariantList::default();
list.push(42.into());
list.push(QString::from("String!").into());
list.push(QByteArray::from("Bytearray!").into());
assert_eq!(
format!("{:?}", list),
"{0: QVariant(int: \"42\"), 1: QVariant(QString: \"String!\"), 2: QVariant(QByteArray: \"Bytearray!\")}"
);
}
}
72 changes: 70 additions & 2 deletions qttypes/src/qtcore/qvariant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;

use crate::{
cpp, cpp_class, QByteArray, QDate, QDateTime, QString, QStringList, QTime, QUrl, QVariantList,
cpp, cpp_class, QByteArray, QDate, QDateTime, QString, QStringList, QTime, QUrl, QVariantList, QVariantMap
};

cpp_class!(
Expand Down Expand Up @@ -57,12 +57,54 @@ impl QVariant {
})
}

/// Wrapper around [`toMap()`][method] method.
///
/// [method]: https://doc.qt.io/qt-5/qvariant.html#toMap
pub fn to_qvariantmap(&self) -> QVariantMap {
cpp!(unsafe [self as "const QVariant*"] -> QVariantMap as "QVariantMap" {
return self->toMap();
})
}

/// Wrapper around [`toString()`][method] method.
///
/// [method]: https://doc.qt.io/qt-5/qvariant.html#toString
pub fn to_qstring(&self) -> QString {
cpp!(unsafe [self as "const QVariant*"] -> QString as "QString" {
return self->toString();
})
}

/// Wrapper around [`toInt()`][method] method.
///
/// [method]: https://doc.qt.io/qt-5/qvariant.html#toInt
pub fn to_int(&self) -> u32 {
cpp!(unsafe [self as "const QVariant*"] -> u32 as "int" {
return self->toInt();
})
}

/// Wrapper around ['typeName()`][method] method.
///
/// [method]: https://doc.qt.io/qt-5/qvariant.html#typeName
pub fn type_name(&self) -> QString {
cpp!(unsafe [self as "const QVariant*"] -> QString as "QString" {
return self->typeName();
})
}

// FIXME: do more wrappers
}

impl fmt::Debug for QVariant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.to_qbytearray().to_string().as_str())
let data = self.to_qstring().to_string();
let qtype = self.type_name().to_string();
Comment on lines +101 to +102
Copy link
Member

Choose a reason for hiding this comment

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

it's probably not needed to convert to a rust String here.

if data.len() == 0 {
write!(f, "QVariant({})", qtype.as_str())
} else {
write!(f, "QVariant({}: \"{}\")", qtype.as_str(), data.as_str())
}
}
}

Expand Down Expand Up @@ -226,3 +268,29 @@ where
(*a).clone().into()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn qvariant_debug_qstring() {
let qv: QVariant = QString::from("Hello, QVariant!").into();
assert_eq!(qv.to_qstring().to_string(), "Hello, QVariant!");
assert_eq!(format!("{:?}", qv), "QVariant(QString: \"Hello, QVariant!\")");
}

#[test]
fn qvariant_debug_bool() {
let qv = QVariant::from(false);
assert_eq!(qv.to_qstring().to_string(), String::from("false"));
assert_eq!(format!("{:?}", qv), "QVariant(bool: \"false\")");
}

#[test]
fn qvariant_debug_int() {
let qv = QVariant::from(313);
assert_eq!(qv.to_int(), 313);
assert_eq!(format!("{:?}", qv), "QVariant(int: \"313\")");
}
}
Loading