-
Notifications
You must be signed in to change notification settings - Fork 502
/
qt_widgets.rs
203 lines (175 loc) · 7.11 KB
/
qt_widgets.rs
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
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
/*!
This module contains all the native Qt widget implementation that forwards to QStyle.
Same as in i_slint_core::items, when When adding an item or a property,
it needs to be kept in sync with different place.
- It needs to be changed in this module
- the Widget list in lib.rs
- In the compiler: builtins.slint
- For the C++ code (new item only): the build.rs to export the new item, and the `using` declaration in slint.h
- Don't forget to update the documentation
*/
#![allow(non_upper_case_globals)]
use crate::qt_window::QPainterPtr;
use const_field_offset::FieldOffsets;
use core::pin::Pin;
use cpp::cpp;
use i_slint_core::graphics::{Color, Rect};
use i_slint_core::input::{
FocusEvent, InputEventFilterResult, InputEventResult, KeyEvent, KeyEventResult, MouseEvent,
};
use i_slint_core::item_rendering::{CachedRenderingData, ItemRenderer};
use i_slint_core::items::{Item, ItemConsts, ItemRc, ItemVTable, RenderingResult, VoidArg};
use i_slint_core::layout::{LayoutInfo, Orientation};
#[cfg(feature = "rtti")]
use i_slint_core::rtti::*;
use i_slint_core::window::WindowRc;
use i_slint_core::{
declare_item_vtable, Callback, ItemVTable_static, Property, SharedString, SharedVector,
};
use i_slint_core_macros::*;
use std::rc::Rc;
type ItemRendererRef<'a> = &'a mut dyn ItemRenderer;
/// Helper macro to get the size from the width and height property,
/// and return Default::default in case the size is too small
macro_rules! get_size {
($self:ident) => {{
let width = $self.width();
let height = $self.height();
if width < 1. || height < 1. {
return Default::default();
};
qttypes::QSize { width: width as _, height: height as _ }
}};
}
macro_rules! fn_render {
($this:ident $dpr:ident $size:ident $painter:ident $widget:ident $initial_state:ident => $($tt:tt)*) => {
fn render(self: Pin<&Self>, backend: &mut &mut dyn ItemRenderer, _self_rc: &ItemRc) -> RenderingResult {
let $dpr: f32 = backend.scale_factor();
let window = backend.window();
let active: bool = window.active();
// This should include self.enabled() as well, but not every native widget
// has that property right now.
let $initial_state = cpp!(unsafe [ active as "bool" ] -> i32 as "int" {
QStyle::State state(QStyle::State_None);
if (active)
state |= QStyle::State_Active;
return (int)state;
});
if let Some(painter) = <dyn std::any::Any>::downcast_mut::<QPainterPtr>(backend.as_any()) {
let $size: qttypes::QSize = get_size!(self);
let $this = self;
painter.save();
let $widget = cpp!(unsafe [painter as "QPainterPtr*"] -> * const () as "QWidget*" {
return (*painter)->device()->devType() == QInternal::Widget ? static_cast<QWidget *>((*painter)->device()) : nullptr;
});
let $painter = painter;
$($tt)*
$painter.restore();
} else {
// Fallback: this happen when the Qt backend is not used and the gl backend is used instead
backend.draw_cached_pixmap(
&self.cached_rendering_data,
&|callback| {
let width = self.width() * $dpr;
let height = self.height() * $dpr;
if width < 1. || height < 1. {
return Default::default();
};
let $size = qttypes::QSize { width: width as _, height: height as _ };
let mut imgarray = QImageWrapArray::new($size, $dpr);
let img = &mut imgarray.img;
let mut painter = cpp!(unsafe [img as "QImage*"] -> QPainterPtr as "std::unique_ptr<QPainter>" {
return std::make_unique<QPainter>(img);
});
let $widget: * const () = core::ptr::null();
let $painter = &mut painter;
let $this = self;
$($tt)*
drop(painter);
imgarray.draw(callback);
},
);
}
RenderingResult::ContinueRenderingChildren
}
};
}
struct QImageWrapArray {
/// The image reference the array, so the array must outlive the image without being detached or accessed
img: qttypes::QImage,
array: SharedVector<u8>,
}
impl QImageWrapArray {
pub fn new(size: qttypes::QSize, dpr: f32) -> Self {
let mut array = SharedVector::default();
array.resize((size.width * size.height * 4) as usize, 0);
let array_ptr = array.make_mut_slice().as_mut_ptr();
let img = cpp!(unsafe [size as "QSize", array_ptr as "uchar*", dpr as "float"] -> qttypes::QImage as "QImage" {
ensure_initialized();
QImage img(array_ptr, size.width(), size.height(), size.width() * 4, QImage::Format_RGBA8888_Premultiplied);
img.setDevicePixelRatio(dpr);
return img;
});
QImageWrapArray { img, array }
}
pub fn draw(&self, callback: &mut dyn FnMut(u32, u32, &[u8])) {
let size = self.img.size();
callback(size.width, size.height, self.array.as_slice());
}
}
cpp! {{
#include <QtWidgets/QApplication>
#include <QtWidgets/QStyle>
#include <QtWidgets/QStyleOption>
#include <QtWidgets/QStyleFactory>
#include <QtGui/QPainter>
#include <QtGui/QClipboard>
#include <QtCore/QMimeData>
#include <QtCore/QDebug>
#include <QtCore/QScopeGuard>
using QPainterPtr = std::unique_ptr<QPainter>;
/// Make sure there is an instance of QApplication.
/// The `from_qt_backend` argument specifies if we know that we are running
/// the Qt backend, or if we are just drawing widgets
void ensure_initialized(bool from_qt_backend = false)
{
if (qApp) {
return;
}
if (!from_qt_backend) {
// When not using the Qt backend, Qt is not in control of the event loop
// so we should set this flag.
QCoreApplication::setAttribute(Qt::AA_PluginApplication, true);
}
static int argc = 1;
static char argv[] = "Slint";
static char *argv2[] = { argv };
// Leak the QApplication, otherwise it crashes on exit
// (because the QGuiApplication destructor access some Q_GLOBAL_STATIC which are already gone)
new QApplication(argc, argv2);
}
}}
mod button;
pub use button::*;
mod checkbox;
pub use checkbox::*;
mod spinbox;
pub use spinbox::*;
mod slider;
pub use slider::*;
mod groupbox;
pub use groupbox::*;
mod lineedit;
pub use lineedit::*;
mod scrollview;
pub use scrollview::*;
mod listviewitem;
pub use listviewitem::*;
mod combobox;
pub use combobox::*;
mod tabwidget;
pub use tabwidget::*;
mod stylemetrics;
pub use stylemetrics::*;