1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
// SPDX-FileContributor: Gerhard de Clercq <gerhard.declercq@kdab.com>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

// We are only using references to QColor so it is actually ffi safe as far as we are concerned
#![allow(improper_ctypes)]

use cxx::{memory::UniquePtrTarget, type_id, ExternType};
use std::{
    ffi::c_void,
    marker::{PhantomData, PhantomPinned},
    mem::MaybeUninit,
    pin::Pin,
};

extern "C" {
    #[link_name = "cxxqt1$qcolor$init$from$qcolor"]
    fn qcolor_init_from_qcolor(ptr: &mut MaybeUninit<cxx::UniquePtr<QColor>>, qcolor: &QColor);
    #[link_name = "cxxqt1$qcolor$init$from$rgba"]
    fn qcolor_init_from_rgba(
        ptr: &mut MaybeUninit<cxx::UniquePtr<QColor>>,
        red: i32,
        green: i32,
        blue: i32,
        alpha: i32,
    );
    #[link_name = "cxxqt1$qcolor$get$alpha"]
    fn qcolor_get_alpha(this: &QColor) -> i32;
    #[link_name = "cxxqt1$qcolor$get$red"]
    fn qcolor_get_red(this: &QColor) -> i32;
    #[link_name = "cxxqt1$qcolor$get$green"]
    fn qcolor_get_green(this: &QColor) -> i32;
    #[link_name = "cxxqt1$qcolor$get$blue"]
    fn qcolor_get_blue(this: &QColor) -> i32;
    #[link_name = "cxxqt1$qcolor$set$alpha"]
    fn qcolor_set_alpha(this: Pin<&mut QColor>, alpha: i32);
    #[link_name = "cxxqt1$qcolor$set$red"]
    fn qcolor_set_red(this: Pin<&mut QColor>, red: i32);
    #[link_name = "cxxqt1$qcolor$set$green"]
    fn qcolor_set_green(this: Pin<&mut QColor>, green: i32);
    #[link_name = "cxxqt1$qcolor$set$blue"]
    fn qcolor_set_blue(this: Pin<&mut QColor>, blue: i32);
}

/// Binding to Qt `QColor`.
///
/// # Invariants
///
/// As an invariant of this API and the static analysis of the cxx::bridge
/// macro, in Rust code we can never obtain a `QColor` by value. Qt's QColor
/// requires a move constructor and may hold internal pointers, which is not
/// compatible with Rust's move behavior. Instead in Rust code we will only ever
/// look at a QColor through a reference or smart pointer, as in `&QColor`
/// or `UniquePtr<QColor>`.
#[repr(C)]
pub struct QColor {
    _pinned: PhantomData<PhantomPinned>,
}

impl QColor {
    /// Create a new Rust Color from this QColor.
    /// This is a copy operation so any changes will not propagate to the original QColor.
    pub fn to_rust(&self) -> Color {
        Color::from_qcolor(self)
    }
}

// Safety:
//
// The code in this file ensures that QColor can only ever be allocated
// on the stack in pinned form which avoids the pitfalls of trying to
// move this type that has a non-trivial move constructor.
unsafe impl ExternType for QColor {
    type Id = type_id!("QColor");
    type Kind = cxx::kind::Opaque;
}

extern "C" {
    #[link_name = "cxxqt1$unique_ptr$qcolor$null"]
    fn unique_ptr_qcolor_null(this: *mut MaybeUninit<*mut c_void>);
    #[link_name = "cxxqt1$unique_ptr$qcolor$raw"]
    fn unique_ptr_qcolor_raw(this: *mut MaybeUninit<*mut c_void>, raw: *mut QColor);
    #[link_name = "cxxqt1$unique_ptr$qcolor$get"]
    fn unique_ptr_qcolor_get(this: *const MaybeUninit<*mut c_void>) -> *const QColor;
    #[link_name = "cxxqt1$unique_ptr$qcolor$release"]
    fn unique_ptr_qcolor_release(this: *mut MaybeUninit<*mut c_void>) -> *mut QColor;
    #[link_name = "cxxqt1$unique_ptr$qcolor$drop"]
    fn unique_ptr_qcolor_drop(this: *mut MaybeUninit<*mut c_void>);
}

// Safety: TODO
unsafe impl UniquePtrTarget for QColor {
    #[doc(hidden)]
    fn __typename(f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str("QColor")
    }

    #[doc(hidden)]
    fn __null() -> MaybeUninit<*mut c_void> {
        let mut repr = MaybeUninit::uninit();
        unsafe {
            unique_ptr_qcolor_null(&mut repr);
        }
        repr
    }

    #[doc(hidden)]
    unsafe fn __raw(raw: *mut Self) -> MaybeUninit<*mut c_void> {
        let mut repr = MaybeUninit::uninit();
        unique_ptr_qcolor_raw(&mut repr, raw);
        repr
    }

    #[doc(hidden)]
    unsafe fn __get(repr: MaybeUninit<*mut c_void>) -> *const Self {
        unique_ptr_qcolor_get(&repr)
    }

    #[doc(hidden)]
    unsafe fn __release(mut repr: MaybeUninit<*mut c_void>) -> *mut Self {
        unique_ptr_qcolor_release(&mut repr)
    }

    #[doc(hidden)]
    unsafe fn __drop(mut repr: MaybeUninit<*mut c_void>) {
        unique_ptr_qcolor_drop(&mut repr)
    }
}

/// The Rust representation of Qt's QColor
///
/// Internally this holds a UniquePtr to a QColor which has been constructed on the C++ side.
pub struct Color {
    // Note that once map_qt_value is removed later, this can become private again
    #[doc(hidden)]
    pub(crate) inner: cxx::UniquePtr<QColor>,
}

impl Color {
    /// Construct a Rust Color from an existing UniquePtr<QColor> this is a move operation
    ///
    /// This is used in QVariant::value so that we don't need to perform another copy
    pub(crate) fn from_unique_ptr(ptr: cxx::UniquePtr<QColor>) -> Self {
        Self { inner: ptr }
    }

    /// Construct a Rust Color from an existing QColor, this is a copy operation.
    pub fn from_qcolor(qcolor: &QColor) -> Self {
        Self {
            // Safety: TODO
            inner: unsafe {
                let mut ptr = MaybeUninit::<cxx::UniquePtr<QColor>>::zeroed();
                qcolor_init_from_qcolor(&mut ptr, qcolor);
                ptr.assume_init()
            },
        }
    }

    /// Construct a Rust Color from a given set of RGBA values
    pub fn from_rgba(red: i32, green: i32, blue: i32, alpha: i32) -> Self {
        Self {
            // Safety: TODO
            inner: unsafe {
                let mut ptr = MaybeUninit::<cxx::UniquePtr<QColor>>::zeroed();
                qcolor_init_from_rgba(&mut ptr, red, green, blue, alpha);
                ptr.assume_init()
            },
        }
    }

    pub fn alpha(&self) -> i32 {
        // Safety: TODO
        unsafe { qcolor_get_alpha(&self.inner) }
    }

    pub fn blue(&self) -> i32 {
        // Safety: TODO
        unsafe { qcolor_get_blue(&self.inner) }
    }

    pub fn green(&self) -> i32 {
        // Safety: TODO
        unsafe { qcolor_get_green(&self.inner) }
    }

    pub fn red(&self) -> i32 {
        // Safety: TODO
        unsafe { qcolor_get_red(&self.inner) }
    }

    pub fn set_alpha(&mut self, alpha: i32) {
        // Safety: TODO
        unsafe {
            qcolor_set_alpha(self.inner.pin_mut(), alpha);
        }
    }

    pub fn set_blue(&mut self, blue: i32) {
        // Safety: TODO
        unsafe {
            qcolor_set_blue(self.inner.pin_mut(), blue);
        }
    }

    pub fn set_green(&mut self, green: i32) {
        // Safety: TODO
        unsafe {
            qcolor_set_green(self.inner.pin_mut(), green);
        }
    }

    pub fn set_red(&mut self, red: i32) {
        // Safety: TODO
        unsafe {
            qcolor_set_red(self.inner.pin_mut(), red);
        }
    }
}

impl crate::ToUniquePtr for Color {
    type CppType = QColor;

    /// Retrieve the UniquePtr to the Qt QColor of this Rust Color
    /// so that this object can be passed back to C++.
    fn to_unique_ptr(self) -> cxx::UniquePtr<QColor> {
        self.inner
    }
}

impl From<&QColor> for Color {
    fn from(qcolor: &QColor) -> Self {
        qcolor.to_rust()
    }
}