| 1 | use std::fmt; |
| 2 | |
| 3 | use crate::{ |
| 4 | cpp, cpp_class, QByteArray, QDate, QDateTime, QString, QStringList, QTime, QUrl, QVariantList, |
| 5 | QVariantMap, |
| 6 | }; |
| 7 | |
| 8 | cpp_class!( |
| 9 | /// Wrapper around [`QVariant`][class] class. |
| 10 | /// |
| 11 | /// [class]: https://doc.qt.io/qt-5/qvariant.html |
| 12 | #[derive(PartialEq, Eq)] |
| 13 | pub unsafe struct QVariant as "QVariant" |
| 14 | ); |
| 15 | impl QVariant { |
| 16 | /// Wrapper around [`toByteArray()`][method] method. |
| 17 | /// |
| 18 | /// [method]: https://doc.qt.io/qt-5/qvariant.html#toByteArray |
| 19 | pub fn to_qbytearray(&self) -> QByteArray { |
| 20 | cpp!(unsafe [self as "const QVariant*" ] -> QByteArray as "QByteArray" { |
| 21 | return self->toByteArray(); |
| 22 | }) |
| 23 | } |
| 24 | |
| 25 | /// Wrapper around [`toBool()`][method] method. |
| 26 | /// |
| 27 | /// [method]: https://doc.qt.io/qt-5/qvariant.html#toBool |
| 28 | pub fn to_bool(&self) -> bool { |
| 29 | cpp!(unsafe [self as "const QVariant*" ] -> bool as "bool" { |
| 30 | return self->toBool(); |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | /// Wrapper around [`userType()`][method] method. |
| 35 | /// |
| 36 | /// [method]: https://doc.qt.io/qt-5/qvariant.html#userType |
| 37 | pub fn user_type(&self) -> i32 { |
| 38 | cpp!(unsafe [self as "const QVariant*" ] -> i32 as "int" { |
| 39 | return self->userType(); |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | /// Wrapper around [`isValid()`][method] method. |
| 44 | /// |
| 45 | /// [method]: https://doc.qt.io/qt-5/qvariant.html#isValid |
| 46 | pub fn is_valid(&self) -> bool { |
| 47 | cpp!(unsafe [self as "const QVariant*" ] -> bool as "bool" { |
| 48 | return self->isValid(); |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | /// Wrapper around [`isNull()`][method] method. |
| 53 | /// |
| 54 | /// [method]: https://doc.qt.io/qt-5/qvariant.html#isNull |
| 55 | pub fn is_null(&self) -> bool { |
| 56 | cpp!(unsafe [self as "const QVariant*" ] -> bool as "bool" { |
| 57 | return self->isNull(); |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | /// Wrapper around [`toMap()`][method] method. |
| 62 | /// |
| 63 | /// [method]: https://doc.qt.io/qt-5/qvariant.html#toMap |
| 64 | pub fn to_qvariantmap(&self) -> QVariantMap { |
| 65 | cpp!(unsafe [self as "const QVariant*" ] -> QVariantMap as "QVariantMap" { |
| 66 | return self->toMap(); |
| 67 | }) |
| 68 | } |
| 69 | |
| 70 | /// Wrapper around [`toString()`][method] method. |
| 71 | /// |
| 72 | /// [method]: https://doc.qt.io/qt-5/qvariant.html#toString |
| 73 | pub fn to_qstring(&self) -> QString { |
| 74 | cpp!(unsafe [self as "const QVariant*" ] -> QString as "QString" { |
| 75 | return self->toString(); |
| 76 | }) |
| 77 | } |
| 78 | |
| 79 | /// Wrapper around [`toInt()`][method] method. |
| 80 | /// |
| 81 | /// [method]: https://doc.qt.io/qt-5/qvariant.html#toInt |
| 82 | pub fn to_int(&self) -> u32 { |
| 83 | cpp!(unsafe [self as "const QVariant*" ] -> u32 as "int" { |
| 84 | return self->toInt(); |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | /// Wrapper around ['typeName()`][method] method. |
| 89 | /// |
| 90 | /// [method]: https://doc.qt.io/qt-5/qvariant.html#typeName |
| 91 | pub fn type_name(&self) -> QString { |
| 92 | cpp!(unsafe [self as "const QVariant*" ] -> QString as "QString" { |
| 93 | return self->typeName(); |
| 94 | }) |
| 95 | } |
| 96 | |
| 97 | // FIXME: do more wrappers |
| 98 | } |
| 99 | |
| 100 | impl fmt::Debug for QVariant { |
| 101 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 102 | let data: String = self.to_qstring().to_string(); |
| 103 | let qtype: String = self.type_name().to_string(); |
| 104 | if data.len() == 0 { |
| 105 | write!(f, "QVariant( {})" , qtype.as_str()) |
| 106 | } else { |
| 107 | write!(f, "QVariant( {}: \"{}\")" , qtype.as_str(), data.as_str()) |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | impl From<QString> for QVariant { |
| 113 | /// Wrapper around [`QVariant(const QString &)`][ctor] constructor. |
| 114 | /// |
| 115 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-14 |
| 116 | fn from(a: QString) -> QVariant { |
| 117 | cpp!(unsafe [a as "QString" ] -> QVariant as "QVariant" { |
| 118 | return QVariant(a); |
| 119 | }) |
| 120 | } |
| 121 | } |
| 122 | impl From<QVariantMap> for QVariant { |
| 123 | /// Wrapper around [`QVariant(const QMap<QString, QVariant> &)`][ctor] constructor. |
| 124 | /// |
| 125 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-22 |
| 126 | fn from(a: QVariantMap) -> QVariant { |
| 127 | cpp!(unsafe [a as "QVariantMap" ] -> QVariant as "QVariant" { |
| 128 | return QVariant(a); |
| 129 | }) |
| 130 | } |
| 131 | } |
| 132 | impl From<QByteArray> for QVariant { |
| 133 | /// Wrapper around [`QVariant(const QByteArray &)`][ctor] constructor. |
| 134 | /// |
| 135 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-12 |
| 136 | fn from(a: QByteArray) -> QVariant { |
| 137 | cpp!(unsafe [a as "QByteArray" ] -> QVariant as "QVariant" { |
| 138 | return QVariant(a); |
| 139 | }) |
| 140 | } |
| 141 | } |
| 142 | impl From<QDate> for QVariant { |
| 143 | /// Wrapper around [`QVariant(const QDate &)`][ctor] constructor. |
| 144 | /// |
| 145 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-18 |
| 146 | fn from(a: QDate) -> QVariant { |
| 147 | cpp!(unsafe [a as "QDate" ] -> QVariant as "QVariant" { |
| 148 | return QVariant(a); |
| 149 | }) |
| 150 | } |
| 151 | } |
| 152 | impl From<QTime> for QVariant { |
| 153 | /// Wrapper around [`QVariant(const QTime &)`][ctor] constructor. |
| 154 | /// |
| 155 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-19 |
| 156 | fn from(a: QTime) -> QVariant { |
| 157 | cpp!(unsafe [a as "QTime" ] -> QVariant as "QVariant" { |
| 158 | return QVariant(a); |
| 159 | }) |
| 160 | } |
| 161 | } |
| 162 | impl From<QDateTime> for QVariant { |
| 163 | /// Wrapper around [`QVariant(const QDateTime &)`][ctor] constructor. |
| 164 | /// |
| 165 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-20 |
| 166 | fn from(a: QDateTime) -> QVariant { |
| 167 | cpp!(unsafe [a as "QDateTime" ] -> QVariant as "QVariant" { |
| 168 | return QVariant(a); |
| 169 | }) |
| 170 | } |
| 171 | } |
| 172 | impl From<QUrl> for QVariant { |
| 173 | /// Wrapper around [`QVariant(const QUrl &)`][ctor] constructor. |
| 174 | /// |
| 175 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-35 |
| 176 | fn from(a: QUrl) -> QVariant { |
| 177 | cpp!(unsafe [a as "QUrl" ] -> QVariant as "QVariant" { |
| 178 | return QVariant(a); |
| 179 | }) |
| 180 | } |
| 181 | } |
| 182 | impl From<QVariantList> for QVariant { |
| 183 | /// Wrapper around [`QVariant(const QVariantList &)`][ctor] constructor. |
| 184 | /// |
| 185 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-21 |
| 186 | fn from(a: QVariantList) -> QVariant { |
| 187 | cpp!(unsafe [a as "QVariantList" ] -> QVariant as "QVariant" { |
| 188 | return QVariant(a); |
| 189 | }) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | impl From<QStringList> for QVariant { |
| 194 | /// Wrapper around [`QVariant(const QStringList &)`][ctor] constructor. |
| 195 | /// |
| 196 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-16 |
| 197 | fn from(a: QStringList) -> Self { |
| 198 | cpp!(unsafe [a as "QStringList" ] -> QVariant as "QVariant" { |
| 199 | return QVariant(a); |
| 200 | }) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | impl From<i32> for QVariant { |
| 205 | /// Wrapper around [`QVariant(int)`][ctor] constructor. |
| 206 | /// |
| 207 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-4 |
| 208 | fn from(a: i32) -> QVariant { |
| 209 | cpp!(unsafe [a as "int" ] -> QVariant as "QVariant" { |
| 210 | return QVariant(a); |
| 211 | }) |
| 212 | } |
| 213 | } |
| 214 | impl From<u32> for QVariant { |
| 215 | /// Wrapper around [`QVariant(uint)`][ctor] constructor. |
| 216 | /// |
| 217 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-5 |
| 218 | fn from(a: u32) -> QVariant { |
| 219 | cpp!(unsafe [a as "uint" ] -> QVariant as "QVariant" { |
| 220 | return QVariant(a); |
| 221 | }) |
| 222 | } |
| 223 | } |
| 224 | impl From<i64> for QVariant { |
| 225 | /// Wrapper around [`QVariant(int)`][ctor] constructor. |
| 226 | /// |
| 227 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-4 |
| 228 | fn from(a: i64) -> QVariant { |
| 229 | cpp!(unsafe [a as "qlonglong" ] -> QVariant as "QVariant" { |
| 230 | return QVariant(a); |
| 231 | }) |
| 232 | } |
| 233 | } |
| 234 | impl From<u64> for QVariant { |
| 235 | /// Wrapper around [`QVariant(uint)`][ctor] constructor. |
| 236 | /// |
| 237 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-5 |
| 238 | fn from(a: u64) -> QVariant { |
| 239 | cpp!(unsafe [a as "qulonglong" ] -> QVariant as "QVariant" { |
| 240 | return QVariant(a); |
| 241 | }) |
| 242 | } |
| 243 | } |
| 244 | impl From<f32> for QVariant { |
| 245 | /// Wrapper around [`QVariant(float)`][ctor] constructor. |
| 246 | /// |
| 247 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-10 |
| 248 | fn from(a: f32) -> QVariant { |
| 249 | cpp!(unsafe [a as "float" ] -> QVariant as "QVariant" { |
| 250 | return QVariant(a); |
| 251 | }) |
| 252 | } |
| 253 | } |
| 254 | impl From<f64> for QVariant { |
| 255 | /// Wrapper around [`QVariant(double)`][ctor] constructor. |
| 256 | /// |
| 257 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-9 |
| 258 | fn from(a: f64) -> QVariant { |
| 259 | cpp!(unsafe [a as "double" ] -> QVariant as "QVariant" { |
| 260 | return QVariant(a); |
| 261 | }) |
| 262 | } |
| 263 | } |
| 264 | impl From<bool> for QVariant { |
| 265 | /// Wrapper around [`QVariant(bool)`][ctor] constructor. |
| 266 | /// |
| 267 | /// [ctor]: https://doc.qt.io/qt-5/qvariant.html#QVariant-8 |
| 268 | fn from(a: bool) -> QVariant { |
| 269 | cpp!(unsafe [a as "bool" ] -> QVariant as "QVariant" { |
| 270 | return QVariant(a); |
| 271 | }) |
| 272 | } |
| 273 | } |
| 274 | impl<'a, T> From<&'a T> for QVariant |
| 275 | where |
| 276 | T: Into<QVariant> + Clone, |
| 277 | { |
| 278 | fn from(a: &'a T) -> QVariant { |
| 279 | (*a).clone().into() |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | #[cfg (test)] |
| 284 | mod tests { |
| 285 | use super::*; |
| 286 | |
| 287 | #[test ] |
| 288 | fn qvariant_debug_qstring() { |
| 289 | let qv: QVariant = QString::from("Hello, QVariant!" ).into(); |
| 290 | assert_eq!(qv.to_qstring().to_string(), "Hello, QVariant!" ); |
| 291 | assert_eq!(format!("{:?}" , qv), "QVariant(QString: \"Hello, QVariant! \")" ); |
| 292 | } |
| 293 | |
| 294 | #[test ] |
| 295 | fn qvariant_debug_bool() { |
| 296 | let qv = QVariant::from(false); |
| 297 | assert_eq!(qv.to_qstring().to_string(), String::from("false" )); |
| 298 | assert_eq!(format!("{:?}" , qv), "QVariant(bool: \"false \")" ); |
| 299 | } |
| 300 | |
| 301 | #[test ] |
| 302 | fn qvariant_debug_int() { |
| 303 | let qv = QVariant::from(313); |
| 304 | assert_eq!(qv.to_int(), 313); |
| 305 | assert_eq!(format!("{:?}" , qv), "QVariant(int: \"313 \")" ); |
| 306 | } |
| 307 | } |
| 308 | |