Skip to content
Open
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
2 changes: 2 additions & 0 deletions crates/cxx-qt-lib/include/gui/qregion.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "rust/cxx.h"

using QRegionRegionType = QRegion::RegionType;

// Define namespace otherwise we hit a GCC bug
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
namespace rust {
Expand Down
6 changes: 3 additions & 3 deletions crates/cxx-qt-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ pub use qstringlist::QStringList;
mod qt;
pub use qt::{
AspectRatioMode, BGMode, CaseSensitivity, ClipOperation, ConnectionType, DateFormat, FillRule,
KeyboardModifier, KeyboardModifiers, LayoutDirection, MouseButton, MouseButtons, Orientation,
Orientations, PenCapStyle, PenJoinStyle, PenStyle, SizeMode, SplitBehaviorFlags, TimeSpec,
TransformationMode,
GlobalColor, KeyboardModifier, KeyboardModifiers, LayoutDirection, MouseButton, MouseButtons,
Orientation, Orientations, PenCapStyle, PenJoinStyle, PenStyle, SizeMode, SplitBehaviorFlags,
TimeSpec, TransformationMode,
};

mod qtime;
Expand Down
48 changes: 47 additions & 1 deletion crates/cxx-qt-lib/src/core/qt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,51 @@ mod ffi {
RFC2822Date = 8,
}

/// Qt's predefined `QColor` objects.
#[repr(i32)]
enum GlobalColor {
/// 0 pixel value (for bitmaps)
color0,
/// 1 pixel value (for bitmaps)
color1,
/// Black (#000000)
black,
/// White (#ffffff)
white,
/// Dark gray (#808080)
darkGray,
/// Gray (#a0a0a4)
gray,
/// Light gray (#c0c0c0)
lightGray,
/// Red (#ff0000)
red,
/// Green (#00ff00)
green,
/// Blue (#0000ff)
blue,
/// Cyan (#00ffff)
cyan,
/// Magenta (#ff00ff)
magenta,
/// Yellow (#ffff00)
yellow,
/// Dark red (#800000)
darkRed,
/// Dark green (#008000)
darkGreen,
/// Dark blue (#000080)
darkBlue,
/// Dark cyan (#008080)
darkCyan,
/// Dark magenta (#ff00ff)
darkMagenta,
/// Dark yellow (#808000)
darkYellow,
/// a transparent black value (i.e., `QColor(0, 0, 0,0)`)
transparent,
}

/// This enum specifies how [`QString::split`](crate::QString::split) functions should behave with respect to empty strings.
#[repr(i32)]
enum SplitBehaviorFlags {
Expand Down Expand Up @@ -267,11 +312,12 @@ mod ffi {
type MouseButton;
type KeyboardModifier;
type Orientation;
type GlobalColor;
}
}

pub use ffi::{
AspectRatioMode, BGMode, CaseSensitivity, ClipOperation, DateFormat, FillRule,
AspectRatioMode, BGMode, CaseSensitivity, ClipOperation, DateFormat, FillRule, GlobalColor,
KeyboardModifier, LayoutDirection, MouseButton, Orientation, PenCapStyle, PenJoinStyle,
PenStyle, SizeMode, SplitBehaviorFlags, TimeSpec, TransformationMode,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/cxx-qt-lib/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ mod qpainter;
pub use qpainter::{QPainter, QPainterCompositionMode, QPainterRenderHint};

mod qregion;
pub use qregion::QRegion;
pub use qregion::{QRegion, QRegionRegionType};
18 changes: 17 additions & 1 deletion crates/cxx-qt-lib/src/gui/qcolor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cxx::{type_id, ExternType};
use std::fmt;
use std::mem::MaybeUninit;

use crate::{QString, QStringList};
use crate::{GlobalColor, QString, QStringList};

#[cxx::bridge]
mod ffi {
Expand All @@ -33,6 +33,12 @@ mod ffi {
ExtendedRgb,
}

#[namespace = "Qt"]
extern "C++" {
include!("cxx-qt-lib/qt.h");
type GlobalColor = crate::GlobalColor;
}

unsafe extern "C++" {
include!("cxx-qt-lib/qcolor.h");
type QColor = super::QColor;
Expand Down Expand Up @@ -270,6 +276,9 @@ mod ffi {
#[rust_name = "qcolor_init_from_qstring"]
fn construct(name: &QString) -> QColor;
#[doc(hidden)]
#[rust_name = "qcolor_init_from_globalcolor"]
fn construct(color: GlobalColor) -> QColor;
#[doc(hidden)]
#[rust_name = "qcolor_eq"]
fn operatorEq(a: &QColor, b: &QColor) -> bool;
}
Expand Down Expand Up @@ -525,6 +534,13 @@ impl Default for QColor {
}
}

impl From<GlobalColor> for QColor {
/// Constructs a new color with a color value of `color`.
fn from(color: GlobalColor) -> Self {
ffi::qcolor_init_from_globalcolor(color)
}
}

impl TryFrom<&str> for QColor {
type Error = &'static str;

Expand Down
27 changes: 27 additions & 0 deletions crates/cxx-qt-lib/src/gui/qpolygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ mod ffi {
#[rust_name = "qpolygon_init_qrect"]
fn construct(rect: &QRect, closed: bool) -> QPolygon;

#[doc(hidden)]
#[rust_name = "qpolygon_init_qvector_qpoint"]
fn construct(points: &QVector_QPoint) -> QPolygon;

#[doc(hidden)]
#[rust_name = "qpolygon_drop"]
fn drop(pen: &mut QPolygon);
Expand Down Expand Up @@ -206,6 +210,29 @@ impl DerefMut for QPolygon {
}
}

impl From<&QRect> for QPolygon {
/// Constructs a polygon from the given `rectangle`. The polygon just contains the four points of the rectangle ordered clockwise.
///
/// Note that the bottom-right corner of the rectangle is located at (`rectangle.x() + rectangle.width(), rectangle.y() + rectangle.height()`).
fn from(rectangle: &QRect) -> Self {
ffi::qpolygon_init_qrect(rectangle, false)
}
}
impl From<QRect> for QPolygon {
/// Constructs a polygon from the given `rectangle`. The polygon just contains the four points of the rectangle ordered clockwise.
///
/// Note that the bottom-right corner of the rectangle is located at (`rectangle.x() + rectangle.width(), rectangle.y() + rectangle.height()`).
fn from(rectangle: QRect) -> Self {
Self::from(&rectangle)
}
}

impl From<&QVector<QPoint>> for QPolygon {
fn from(value: &QVector<QPoint>) -> Self {
ffi::qpolygon_init_qvector_qpoint(value)
}
}

unsafe impl Upcast<QVector<QPoint>> for QPolygon {
unsafe fn upcast_ptr(this: *const Self) -> *const QVector<QPoint> {
ffi::upcast_qpolygon(this)
Expand Down
34 changes: 33 additions & 1 deletion crates/cxx-qt-lib/src/gui/qpolygonf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cxx_qt::casting::Upcast;
use std::fmt;
use std::ops::{Deref, DerefMut};

use crate::{QPointF, QVector};
use crate::{QPointF, QRectF, QVector};

#[cxx::bridge]
mod ffi {
Expand Down Expand Up @@ -101,6 +101,14 @@ mod ffi {
#[rust_name = "qpolygonf_init_default"]
fn construct() -> QPolygonF;

#[doc(hidden)]
#[rust_name = "qpolygonf_from_qrectf"]
fn construct(rectangle: &QRectF) -> QPolygonF;

#[doc(hidden)]
#[rust_name = "qpolygonf_from_qvector_qpointf"]
fn construct(points: &QVector_QPointF) -> QPolygonF;

#[doc(hidden)]
#[rust_name = "qpolygonf_drop"]
fn drop(pen: &mut QPolygonF);
Expand Down Expand Up @@ -185,6 +193,30 @@ impl DerefMut for QPolygonF {
}
}

impl From<&QVector<QPointF>> for QPolygonF {
/// Constructs a polygon containing the specified `points`.
fn from(points: &QVector<QPointF>) -> Self {
ffi::qpolygonf_from_qvector_qpointf(points)
}
}

impl From<&QRectF> for QPolygonF {
/// Constructs a closed polygon from the specified `rectangle`.
///
/// The polygon contains the four vertices of the rectangle in clockwise order starting and ending with the top-left vertex.
fn from(rectangle: &QRectF) -> Self {
ffi::qpolygonf_from_qrectf(rectangle)
}
}
impl From<QRectF> for QPolygonF {
/// Constructs a closed polygon from the specified `rectangle`.
///
/// The polygon contains the four vertices of the rectangle in clockwise order starting and ending with the top-left vertex.
fn from(rectangle: QRectF) -> Self {
Self::from(&rectangle)
}
}

unsafe impl Upcast<QVector<QPointF>> for QPolygonF {
unsafe fn upcast_ptr(this: *const Self) -> *const QVector<QPointF> {
ffi::upcast_qpolygonf(this)
Expand Down
64 changes: 64 additions & 0 deletions crates/cxx-qt-lib/src/gui/qregion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,35 @@ use std::mem::MaybeUninit;

#[cxx::bridge]
mod ffi {
/// Specifies the shape of the region to be created.
#[repr(i32)]
enum QRegionRegionType {
/// the region covers the entire rectangle.
Rectangle,
/// The region is an ellipse inside the rectangle.
Ellipse,
}

#[namespace = "Qt"]
extern "C++" {
include!("cxx-qt-lib/qt.h");
type FillRule = crate::FillRule;
}

unsafe extern "C++" {
include!("cxx-qt-lib/qregion.h");
type QRegion = super::QRegion;
type QRegionRegionType;

include!("cxx-qt-lib/qrect.h");
type QRect = crate::QRect;

include!("cxx-qt-lib/qpoint.h");
type QPoint = crate::QPoint;

include!("cxx-qt-lib/qpolygon.h");
type QPolygon = crate::QPolygon;

/// Returns the bounding rectangle of this region. An empty region gives a rectangle that is [`QRect::is_null`].
#[rust_name = "bounding_rect"]
fn boundingRect(self: &QRegion) -> QRect;
Expand Down Expand Up @@ -67,6 +86,12 @@ mod ffi {
#[doc(hidden)]
#[rust_name = "qregion_init_default"]
fn construct() -> QRegion;
#[doc(hidden)]
#[rust_name = "qregion_init_qrect_regiontype"]
fn construct(r: &QRect, t: QRegionRegionType) -> QRegion;
#[doc(hidden)]
#[rust_name = "qregion_init_qpolygon_fillrule"]
fn construct(a: &QPolygon, fill_rule: FillRule) -> QRegion;

#[doc(hidden)]
#[rust_name = "qregion_drop"]
Expand All @@ -78,6 +103,10 @@ mod ffi {
}
}

pub use ffi::QRegionRegionType;

use crate::{FillRule, QPolygon, QRect};

/// The `QRegion` class specifies a clip region for a painter.
///
/// Qt Documentation: [QRegion](https://doc.qt.io/qt/qregion.html#details)
Expand Down Expand Up @@ -105,6 +134,41 @@ impl Clone for QRegion {
}
}

impl QRegion {
/// Create a region based on the rectangle `r` with region type `t`.
///
/// If the rectangle is invalid a null region will be created.
pub fn from_rect(r: &QRect, t: QRegionRegionType) -> Self {
ffi::qregion_init_qrect_regiontype(r, t)
}

/// Constructs a polygon region from the point array `a` with the fill rule specified by `fill_rule`.
///
/// If `fill_rule` is [`FillRule::WindingFill`], the polygon region is defined using the winding algorithm; if it is [`FillRule::OddEvenFill`], the odd-even fill algorithm is used.
///
/// **Warning:** This constructor can be used to create complex regions that will slow down painting when used.
pub fn from_polygon(a: &QPolygon, fill_rule: FillRule) -> Self {
ffi::qregion_init_qpolygon_fillrule(a, fill_rule)
}
}

impl From<&QRect> for QRegion {
/// Create a region based on the rectangle `r` with region type [`QRegionRegionType::Rectangle`].
///
/// If the rectangle is invalid a null region will be created.
fn from(r: &QRect) -> Self {
ffi::qregion_init_qrect_regiontype(r, QRegionRegionType::Rectangle)
}
}
impl From<QRect> for QRegion {
/// Create a region based on the rectangle `r` with region type [`QRegionRegionType::Rectangle`].
///
/// If the rectangle is invalid a null region will be created.
fn from(r: QRect) -> Self {
Self::from(&r)
}
}

// Safety:
//
// Static checks on the C++ side to ensure the size is the same.
Expand Down
Loading