1use crate::{Location, MapSource, ffi};
7#[cfg(feature = "v1_6")]
8#[cfg_attr(docsrs, doc(cfg(feature = "v1_6")))]
9use glib::object::ObjectType as _;
10use glib::{
11 prelude::*,
12 signal::{SignalHandlerId, connect_raw},
13 translate::*,
14};
15use std::boxed::Box as Box_;
16
17glib::wrapper! {
18 #[doc(alias = "ShumateViewport")]
82 pub struct Viewport(Object<ffi::ShumateViewport, ffi::ShumateViewportClass>) @implements Location;
83
84 match fn {
85 type_ => || ffi::shumate_viewport_get_type(),
86 }
87}
88
89impl Viewport {
90 #[doc(alias = "shumate_viewport_new")]
96 pub fn new() -> Viewport {
97 assert_initialized_main_thread!();
98 unsafe { from_glib_full(ffi::shumate_viewport_new()) }
99 }
100
101 pub fn builder() -> ViewportBuilder {
106 ViewportBuilder::new()
107 }
108
109 #[doc(alias = "shumate_viewport_get_max_zoom_level")]
115 #[doc(alias = "get_max_zoom_level")]
116 #[doc(alias = "max-zoom-level")]
117 pub fn max_zoom_level(&self) -> u32 {
118 unsafe { ffi::shumate_viewport_get_max_zoom_level(self.to_glib_none().0) }
119 }
120
121 #[doc(alias = "shumate_viewport_get_min_zoom_level")]
127 #[doc(alias = "get_min_zoom_level")]
128 #[doc(alias = "min-zoom-level")]
129 pub fn min_zoom_level(&self) -> u32 {
130 unsafe { ffi::shumate_viewport_get_min_zoom_level(self.to_glib_none().0) }
131 }
132
133 #[doc(alias = "shumate_viewport_get_reference_map_source")]
140 #[doc(alias = "get_reference_map_source")]
141 #[doc(alias = "reference-map-source")]
142 pub fn reference_map_source(&self) -> Option<MapSource> {
143 unsafe {
144 from_glib_none(ffi::shumate_viewport_get_reference_map_source(
145 self.to_glib_none().0,
146 ))
147 }
148 }
149
150 #[doc(alias = "shumate_viewport_get_rotation")]
156 #[doc(alias = "get_rotation")]
157 pub fn rotation(&self) -> f64 {
158 unsafe { ffi::shumate_viewport_get_rotation(self.to_glib_none().0) }
159 }
160
161 #[doc(alias = "shumate_viewport_get_zoom_level")]
167 #[doc(alias = "get_zoom_level")]
168 #[doc(alias = "zoom-level")]
169 pub fn zoom_level(&self) -> f64 {
170 unsafe { ffi::shumate_viewport_get_zoom_level(self.to_glib_none().0) }
171 }
172
173 #[doc(alias = "shumate_viewport_location_to_widget_coords")]
191 pub fn location_to_widget_coords(
192 &self,
193 widget: &impl IsA<gtk::Widget>,
194 latitude: f64,
195 longitude: f64,
196 ) -> (f64, f64) {
197 unsafe {
198 let mut x = std::mem::MaybeUninit::uninit();
199 let mut y = std::mem::MaybeUninit::uninit();
200 ffi::shumate_viewport_location_to_widget_coords(
201 self.to_glib_none().0,
202 widget.as_ref().to_glib_none().0,
203 latitude,
204 longitude,
205 x.as_mut_ptr(),
206 y.as_mut_ptr(),
207 );
208 (x.assume_init(), y.assume_init())
209 }
210 }
211
212 #[doc(alias = "shumate_viewport_set_max_zoom_level")]
216 #[doc(alias = "max-zoom-level")]
217 pub fn set_max_zoom_level(&self, max_zoom_level: u32) {
218 unsafe {
219 ffi::shumate_viewport_set_max_zoom_level(self.to_glib_none().0, max_zoom_level);
220 }
221 }
222
223 #[doc(alias = "shumate_viewport_set_min_zoom_level")]
227 #[doc(alias = "min-zoom-level")]
228 pub fn set_min_zoom_level(&self, min_zoom_level: u32) {
229 unsafe {
230 ffi::shumate_viewport_set_min_zoom_level(self.to_glib_none().0, min_zoom_level);
231 }
232 }
233
234 #[doc(alias = "shumate_viewport_set_reference_map_source")]
238 #[doc(alias = "reference-map-source")]
239 pub fn set_reference_map_source(&self, map_source: Option<&impl IsA<MapSource>>) {
240 unsafe {
241 ffi::shumate_viewport_set_reference_map_source(
242 self.to_glib_none().0,
243 map_source.map(|p| p.as_ref()).to_glib_none().0,
244 );
245 }
246 }
247
248 #[doc(alias = "shumate_viewport_set_rotation")]
252 #[doc(alias = "rotation")]
253 pub fn set_rotation(&self, rotation: f64) {
254 unsafe {
255 ffi::shumate_viewport_set_rotation(self.to_glib_none().0, rotation);
256 }
257 }
258
259 #[doc(alias = "shumate_viewport_set_zoom_level")]
263 #[doc(alias = "zoom-level")]
264 pub fn set_zoom_level(&self, zoom_level: f64) {
265 unsafe {
266 ffi::shumate_viewport_set_zoom_level(self.to_glib_none().0, zoom_level);
267 }
268 }
269
270 #[doc(alias = "shumate_viewport_widget_coords_to_location")]
287 pub fn widget_coords_to_location(
288 &self,
289 widget: &impl IsA<gtk::Widget>,
290 x: f64,
291 y: f64,
292 ) -> (f64, f64) {
293 unsafe {
294 let mut latitude = std::mem::MaybeUninit::uninit();
295 let mut longitude = std::mem::MaybeUninit::uninit();
296 ffi::shumate_viewport_widget_coords_to_location(
297 self.to_glib_none().0,
298 widget.as_ref().to_glib_none().0,
299 x,
300 y,
301 latitude.as_mut_ptr(),
302 longitude.as_mut_ptr(),
303 );
304 (latitude.assume_init(), longitude.assume_init())
305 }
306 }
307
308 #[cfg(feature = "v1_6")]
311 #[cfg_attr(docsrs, doc(cfg(feature = "v1_6")))]
312 #[doc(alias = "changed")]
313 pub fn connect_changed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
314 unsafe extern "C" fn changed_trampoline<F: Fn(&Viewport) + 'static>(
315 this: *mut ffi::ShumateViewport,
316 f: glib::ffi::gpointer,
317 ) {
318 unsafe {
319 let f: &F = &*(f as *const F);
320 f(&from_glib_borrow(this))
321 }
322 }
323 unsafe {
324 let f: Box_<F> = Box_::new(f);
325 connect_raw(
326 self.as_ptr() as *mut _,
327 c"changed".as_ptr(),
328 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
329 changed_trampoline::<F> as *const (),
330 )),
331 Box_::into_raw(f),
332 )
333 }
334 }
335
336 #[doc(alias = "max-zoom-level")]
337 pub fn connect_max_zoom_level_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
338 unsafe extern "C" fn notify_max_zoom_level_trampoline<F: Fn(&Viewport) + 'static>(
339 this: *mut ffi::ShumateViewport,
340 _param_spec: glib::ffi::gpointer,
341 f: glib::ffi::gpointer,
342 ) {
343 unsafe {
344 let f: &F = &*(f as *const F);
345 f(&from_glib_borrow(this))
346 }
347 }
348 unsafe {
349 let f: Box_<F> = Box_::new(f);
350 connect_raw(
351 self.as_ptr() as *mut _,
352 c"notify::max-zoom-level".as_ptr(),
353 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
354 notify_max_zoom_level_trampoline::<F> as *const (),
355 )),
356 Box_::into_raw(f),
357 )
358 }
359 }
360
361 #[doc(alias = "min-zoom-level")]
362 pub fn connect_min_zoom_level_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
363 unsafe extern "C" fn notify_min_zoom_level_trampoline<F: Fn(&Viewport) + 'static>(
364 this: *mut ffi::ShumateViewport,
365 _param_spec: glib::ffi::gpointer,
366 f: glib::ffi::gpointer,
367 ) {
368 unsafe {
369 let f: &F = &*(f as *const F);
370 f(&from_glib_borrow(this))
371 }
372 }
373 unsafe {
374 let f: Box_<F> = Box_::new(f);
375 connect_raw(
376 self.as_ptr() as *mut _,
377 c"notify::min-zoom-level".as_ptr(),
378 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
379 notify_min_zoom_level_trampoline::<F> as *const (),
380 )),
381 Box_::into_raw(f),
382 )
383 }
384 }
385
386 #[doc(alias = "reference-map-source")]
387 pub fn connect_reference_map_source_notify<F: Fn(&Self) + 'static>(
388 &self,
389 f: F,
390 ) -> SignalHandlerId {
391 unsafe extern "C" fn notify_reference_map_source_trampoline<F: Fn(&Viewport) + 'static>(
392 this: *mut ffi::ShumateViewport,
393 _param_spec: glib::ffi::gpointer,
394 f: glib::ffi::gpointer,
395 ) {
396 unsafe {
397 let f: &F = &*(f as *const F);
398 f(&from_glib_borrow(this))
399 }
400 }
401 unsafe {
402 let f: Box_<F> = Box_::new(f);
403 connect_raw(
404 self.as_ptr() as *mut _,
405 c"notify::reference-map-source".as_ptr(),
406 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
407 notify_reference_map_source_trampoline::<F> as *const (),
408 )),
409 Box_::into_raw(f),
410 )
411 }
412 }
413
414 #[doc(alias = "rotation")]
415 pub fn connect_rotation_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
416 unsafe extern "C" fn notify_rotation_trampoline<F: Fn(&Viewport) + 'static>(
417 this: *mut ffi::ShumateViewport,
418 _param_spec: glib::ffi::gpointer,
419 f: glib::ffi::gpointer,
420 ) {
421 unsafe {
422 let f: &F = &*(f as *const F);
423 f(&from_glib_borrow(this))
424 }
425 }
426 unsafe {
427 let f: Box_<F> = Box_::new(f);
428 connect_raw(
429 self.as_ptr() as *mut _,
430 c"notify::rotation".as_ptr(),
431 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
432 notify_rotation_trampoline::<F> as *const (),
433 )),
434 Box_::into_raw(f),
435 )
436 }
437 }
438
439 #[doc(alias = "zoom-level")]
440 pub fn connect_zoom_level_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
441 unsafe extern "C" fn notify_zoom_level_trampoline<F: Fn(&Viewport) + 'static>(
442 this: *mut ffi::ShumateViewport,
443 _param_spec: glib::ffi::gpointer,
444 f: glib::ffi::gpointer,
445 ) {
446 unsafe {
447 let f: &F = &*(f as *const F);
448 f(&from_glib_borrow(this))
449 }
450 }
451 unsafe {
452 let f: Box_<F> = Box_::new(f);
453 connect_raw(
454 self.as_ptr() as *mut _,
455 c"notify::zoom-level".as_ptr(),
456 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
457 notify_zoom_level_trampoline::<F> as *const (),
458 )),
459 Box_::into_raw(f),
460 )
461 }
462 }
463}
464
465impl Default for Viewport {
466 fn default() -> Self {
467 Self::new()
468 }
469}
470
471#[must_use = "The builder must be built to be used"]
476pub struct ViewportBuilder {
477 builder: glib::object::ObjectBuilder<'static, Viewport>,
478}
479
480impl ViewportBuilder {
481 fn new() -> Self {
482 Self {
483 builder: glib::object::Object::builder(),
484 }
485 }
486
487 pub fn max_zoom_level(self, max_zoom_level: u32) -> Self {
489 Self {
490 builder: self.builder.property("max-zoom-level", max_zoom_level),
491 }
492 }
493
494 pub fn min_zoom_level(self, min_zoom_level: u32) -> Self {
496 Self {
497 builder: self.builder.property("min-zoom-level", min_zoom_level),
498 }
499 }
500
501 pub fn reference_map_source(self, reference_map_source: &impl IsA<MapSource>) -> Self {
503 Self {
504 builder: self.builder.property(
505 "reference-map-source",
506 reference_map_source.clone().upcast(),
507 ),
508 }
509 }
510
511 pub fn rotation(self, rotation: f64) -> Self {
513 Self {
514 builder: self.builder.property("rotation", rotation),
515 }
516 }
517
518 pub fn zoom_level(self, zoom_level: f64) -> Self {
520 Self {
521 builder: self.builder.property("zoom-level", zoom_level),
522 }
523 }
524
525 pub fn latitude(self, latitude: f64) -> Self {
527 Self {
528 builder: self.builder.property("latitude", latitude),
529 }
530 }
531
532 pub fn longitude(self, longitude: f64) -> Self {
534 Self {
535 builder: self.builder.property("longitude", longitude),
536 }
537 }
538
539 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
542 pub fn build(self) -> Viewport {
543 assert_initialized_main_thread!();
544 self.builder.build()
545 }
546}