Skip to content

Commit afbf9d4

Browse files
committed
Add a few types
1 parent 3d1592d commit afbf9d4

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

qmetaobject/src/qmetatype.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ qdeclare_builtin_metatype! {QTime => 15}
270270
qdeclare_builtin_metatype! {QDateTime => 16}
271271
qdeclare_builtin_metatype! {QUrl => 17}
272272
qdeclare_builtin_metatype! {QRectF => 20}
273+
qdeclare_builtin_metatype! {QSize => 21}
274+
qdeclare_builtin_metatype! {QSizeF => 22}
275+
qdeclare_builtin_metatype! {QPoint => 25}
273276
qdeclare_builtin_metatype! {QPointF => 26}
274277
impl QMetaType for QVariant {
275278
fn register(_name: Option<&std::ffi::CStr>) -> i32 {
@@ -283,6 +286,7 @@ impl QMetaType for QVariant {
283286
}
284287
}
285288
qdeclare_builtin_metatype! {QModelIndex => 42}
289+
qdeclare_builtin_metatype! {QPixmap => 65}
286290
qdeclare_builtin_metatype! {QColor => 67}
287291
qdeclare_builtin_metatype! {QImage => 70}
288292

@@ -438,3 +442,18 @@ fn test_qvariant_datetime() {
438442
assert!(qtime == QTime::from_h_m_s_ms(10, 30, Some(40), Some(100)));
439443
assert!(qtime != QTime::from_h_m_s_ms(10, 30, Some(40), None));
440444
}
445+
446+
#[test]
447+
fn test_qvariant_qpoint_qrect() {
448+
// test that conversion through a variant lead the the right data
449+
assert_eq!(
450+
QPoint::from_qvariant(QPointF { x: 23.1, y: 54.2 }.to_qvariant()),
451+
Some(QPoint { x: 23, y: 54 })
452+
);
453+
let qrectf = QRectF { x: 4.1, y: 9.1, height: 7.3, width: 9.0 };
454+
assert_eq!(QRectF::from_qvariant(qrectf.to_qvariant()), Some(qrectf));
455+
assert_eq!(
456+
QSize::from_qvariant(QSizeF { width: 123.1, height: 254.2 }.to_qvariant()),
457+
Some(QSize { width: 123, height: 254 })
458+
);
459+
}

qmetaobject/tests/tests.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,3 +961,29 @@ fn component_status_changed() {
961961
assert_eq!(result.borrow().as_ref().unwrap().0, ComponentStatus::Loading);
962962
});
963963
}
964+
965+
#[test]
966+
fn test_qvariant_qimage_qpixmap() {
967+
let _lock = lock_for_test();
968+
// QPixmap need a QApplication
969+
let _app = QmlEngine::new();
970+
971+
let mut img = QImage::new(QSize { width: 12, height: 23 }, ImageFormat::ARGB32);
972+
img.fill(QColor::from_name("red"));
973+
img.set_pixel_color(2, 2, QColor::from_name("blue"));
974+
let pix: QPixmap = img.clone().into();
975+
assert_eq!(pix.size(), QSize { width: 12, height: 23 });
976+
let img2 = QImage::from_qvariant(pix.clone().to_qvariant()).unwrap();
977+
assert_eq!(img2.size(), QSize { width: 12, height: 23 });
978+
dbg!(img2.get_pixel_color(3, 4).get_rgba());
979+
assert!(img2.get_pixel_color(2, 2) == QColor::from_rgb_f(0., 0., 1.));
980+
assert!(img2.get_pixel_color(3, 4) == QColor::from_rgb_f(1., 0., 0.));
981+
982+
let mut img3: QImage = pix.into();
983+
assert!(img2 == img3);
984+
assert_eq!(img3.size(), QSize { width: 12, height: 23 });
985+
assert!(img3.get_pixel_color(2, 2) == QColor::from_rgb_f(0., 0., 1.));
986+
assert!(img3.get_pixel_color(8, 4) == QColor::from_rgb_f(1., 0., 0.));
987+
img3.set_pixel_color(8, 8, QColor::from_name("black"));
988+
assert!(img2 != img3);
989+
}

qttypes/src/lib.rs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ cpp! {{
8686
#include <QtCore/QUrl>
8787

8888
#include <QtGui/QImage>
89+
#include <QtGui/QPixmap>
8990
}}
9091

9192
cpp_class!(
@@ -1006,10 +1007,19 @@ impl QRectF {
10061007
})
10071008
}
10081009

1009-
// XXX: shouldn't it be a wrapper for cpp call?
1010+
/// Same as the [`topLeft`][method] method.
1011+
///
1012+
/// [method]: https://doc.qt.io/qt-5/qrectf.html#topLeft
10101013
pub fn top_left(&self) -> QPointF {
10111014
QPointF { x: self.x, y: self.y }
10121015
}
1016+
1017+
/// Same as the [`isValid`][method] method.
1018+
///
1019+
/// [method]: https://doc.qt.io/qt-5/qrectf.html#isValid
1020+
pub fn is_valid(&self) -> bool {
1021+
self.width > 0. && self.height > 0.
1022+
}
10131023
}
10141024

10151025
/// Bindings for [`QPointF`][class] class.
@@ -1039,6 +1049,16 @@ impl std::ops::AddAssign for QPointF {
10391049
}
10401050
}
10411051

1052+
/// Bindings for [`QSizeF`][class] class.
1053+
///
1054+
/// [class]: https://doc.qt.io/qt-5/qsizef.html
1055+
#[repr(C)]
1056+
#[derive(Default, Clone, Copy, PartialEq, Debug)]
1057+
pub struct QSizeF {
1058+
pub width: qreal,
1059+
pub height: qreal,
1060+
}
1061+
10421062
#[test]
10431063
fn test_qpointf_qrectf() {
10441064
let rect = QRectF { x: 200., y: 150., width: 60., height: 75. };
@@ -1136,6 +1156,28 @@ pub struct QSize {
11361156
pub height: u32,
11371157
}
11381158

1159+
/// Bindings for [`QPoint`][class] class.
1160+
///
1161+
/// [class]: https://doc.qt.io/qt-5/qpoint.html
1162+
#[repr(C)]
1163+
#[derive(Default, Clone, Copy, PartialEq, Debug)]
1164+
pub struct QPoint {
1165+
pub x: i32,
1166+
pub y: i32,
1167+
}
1168+
1169+
/// Bindings for [`QMargins`][class] class.
1170+
///
1171+
/// [class]: https://doc.qt.io/qt-5/qmargins.html
1172+
#[repr(C)]
1173+
#[derive(Default, Clone, Copy, PartialEq, Debug)]
1174+
pub struct QMargins {
1175+
pub left: i32,
1176+
pub top: i32,
1177+
pub right: i32,
1178+
pub bottom: i32,
1179+
}
1180+
11391181
/// Bindings for [`QImage::Format`][class] enum class.
11401182
///
11411183
/// [class]: https://doc.qt.io/qt-5/qimage.html#Format-enum
@@ -1178,6 +1220,7 @@ cpp_class!(
11781220
/// Wrapper around [`QImage`][class] class.
11791221
///
11801222
/// [class]: https://doc.qt.io/qt-5/qimage.html
1223+
#[derive(Default, Clone, PartialEq)]
11811224
pub unsafe struct QImage as "QImage"
11821225
);
11831226
impl QImage {
@@ -1232,9 +1275,37 @@ impl QImage {
12321275
/// Wrapper around [`pixelColor(const QPoint &)`][method] method.
12331276
///
12341277
/// [method]: https://doc.qt.io/qt-5/qimage.html#pixelColor
1235-
pub fn get_pixel_color(&mut self, x: u32, y: u32) -> QColor {
1236-
cpp!(unsafe [self as "QImage*", x as "int", y as "int"] -> QColor as "QColor" {
1278+
pub fn get_pixel_color(&self, x: u32, y: u32) -> QColor {
1279+
cpp!(unsafe [self as "const QImage*", x as "int", y as "int"] -> QColor as "QColor" {
12371280
return self->pixelColor(x, y);
12381281
})
12391282
}
12401283
}
1284+
1285+
cpp_class!(
1286+
/// Wrapper around [`QPixmap`][class] class.
1287+
///
1288+
/// [class]: https://doc.qt.io/qt-5/qpixmap.html
1289+
pub unsafe struct QPixmap as "QPixmap"
1290+
);
1291+
1292+
impl QPixmap {
1293+
/// Wrapper around [`size()`][method] method.
1294+
///
1295+
/// [method]: https://doc.qt.io/qt-5/qpixmap.html#size
1296+
pub fn size(&self) -> QSize {
1297+
cpp!(unsafe [self as "const QPixmap*"] -> QSize as "QSize" { return self->size(); })
1298+
}
1299+
}
1300+
1301+
impl From<QPixmap> for QImage {
1302+
fn from(pixmap: QPixmap) -> Self {
1303+
cpp!(unsafe [pixmap as "QPixmap"] -> QImage as "QImage" { return pixmap.toImage(); })
1304+
}
1305+
}
1306+
1307+
impl From<QImage> for QPixmap {
1308+
fn from(image: QImage) -> Self {
1309+
cpp!(unsafe [image as "QImage"] -> QPixmap as "QPixmap" { return QPixmap::fromImage(image); })
1310+
}
1311+
}

0 commit comments

Comments
 (0)