#![allow(dead_code)]
use cpp::{cpp, cpp_class};
use std::fmt::Display;
use std::os::raw::c_char;
cpp! {{
#include <QtGui/QImage>
#include <QtGui/QPixmap>
}}
cpp_class!(
pub unsafe struct QImage as "QImage"
);
impl QImage {
pub fn data(&self) -> &[u8] {
unsafe {
let len = cpp!([self as "const QImage*"] -> usize as "std::size_t" { return self->sizeInBytes(); });
let data = cpp!([self as "const QImage*"] -> *const u8 as "const unsigned char*" { return self->bits(); });
return core::slice::from_raw_parts(data, len);
}
}
pub fn size(&self) -> QSize {
cpp!(unsafe [self as "const QImage*"] -> QSize as "QSize" { return self->size(); })
}
}
cpp_class!(
pub unsafe struct QPixmap as "QPixmap"
);
impl QPixmap {
pub fn size(&self) -> QSize {
cpp!(unsafe [self as "const QPixmap*"] -> QSize as "QSize" { return self->size(); })
}
}
cpp_class!(
#[derive(PartialEq, PartialOrd, Eq, Ord)]
pub unsafe struct QByteArray as "QByteArray"
);
impl QByteArray {
pub fn to_slice(&self) -> &[u8] {
unsafe {
let mut size: usize = 0;
let c_ptr = cpp!([self as "const QByteArray*", mut size as "size_t"] -> *const u8 as "const char*" {
size = self->size();
return self->constData();
});
std::slice::from_raw_parts(c_ptr, size)
}
}
pub fn to_str(&self) -> Result<&str, std::str::Utf8Error> {
std::str::from_utf8(self.to_slice())
}
}
impl<'a> From<&'a [u8]> for QByteArray {
fn from(s: &'a [u8]) -> QByteArray {
let len = s.len();
let ptr = s.as_ptr();
cpp!(unsafe [len as "size_t", ptr as "char*"] -> QByteArray as "QByteArray" {
return QByteArray(ptr, len);
})
}
}
impl<'a> From<&'a str> for QByteArray {
fn from(s: &'a str) -> QByteArray {
s.as_bytes().into()
}
}
impl From<String> for QByteArray {
fn from(s: String) -> QByteArray {
QByteArray::from(&*s)
}
}
impl From<QString> for QByteArray {
fn from(s: QString) -> QByteArray {
cpp!(unsafe [s as "QString"] -> QByteArray as "QByteArray" {
return std::move(s).toUtf8();
})
}
}
impl Display for QByteArray {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
unsafe {
let c_ptr = cpp!([self as "const QByteArray*"] -> *const c_char as "const char*" {
return self->constData();
});
f.write_str(std::ffi::CStr::from_ptr(c_ptr).to_str().map_err(|_| Default::default())?)
}
}
}
impl std::fmt::Debug for QByteArray {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
}
cpp_class!(
#[derive(PartialEq, PartialOrd, Eq, Ord)]
pub unsafe struct QString as "QString"
);
impl QString {
pub fn to_slice(&self) -> &[u16] {
unsafe {
let mut size: usize = 0;
let c_ptr = cpp!([self as "const QString*", mut size as "size_t"] -> *const u16 as "const QChar*" {
size = self->size();
return self->constData();
});
std::slice::from_raw_parts(c_ptr, size)
}
}
}
impl<'a> From<&'a str> for QString {
fn from(s: &'a str) -> QString {
let len = s.len();
let ptr = s.as_ptr();
cpp!(unsafe [len as "size_t", ptr as "char*"] -> QString as "QString" {
return QString::fromUtf8(ptr, len);
})
}
}
impl From<String> for QString {
fn from(s: String) -> QString {
QString::from(&*s)
}
}
impl Into<String> for QString {
fn into(self) -> String {
String::from_utf16_lossy(self.to_slice())
}
}
#[repr(C)]
#[derive(Default, Clone, Copy, PartialEq, Debug)]
pub struct QSize {
pub width: u32,
pub height: u32,
}
#[repr(C)]
#[derive(Default, Clone, Copy, PartialEq, Debug)]
pub struct QSizeF {
pub width: qreal,
pub height: qreal,
}
#[repr(C)]
#[derive(Default, Clone, Copy, PartialEq, Debug)]
pub struct QPoint {
pub x: i32,
pub y: i32,
}
#[repr(C)]
#[derive(Default, Clone, Copy, PartialEq, Debug)]
pub struct QPointF {
pub x: qreal,
pub y: qreal,
}
#[repr(C)]
#[derive(Default, Clone, Copy, PartialEq, Debug)]
pub struct QMargins {
pub left: i32,
pub top: i32,
pub right: i32,
pub bottom: i32,
}
#[allow(non_camel_case_types)]
pub type qreal = f64;
#[repr(C)]
#[derive(Default, Clone, Copy, PartialEq, Debug)]
pub struct QRectF {
pub x: qreal,
pub y: qreal,
pub width: qreal,
pub height: qreal,
}