From 8fb49b8929e01a668c7cadb612fa232052dea9cc Mon Sep 17 00:00:00 2001 From: Alison Watson Date: Wed, 10 Jul 2019 07:15:52 -0400 Subject: [PATCH] tycho: add generic drawView function for IProjectView --- rust-qt-binding-generator | 2 +- tycho/bindings.json | 7 +++++++ tycho/cc/interface.h | 10 ++++++++++ tycho/cc/mapview.cc | 9 ++------- tycho/cc/tycho.h | 4 ---- tycho/source/cc.rs | 26 +++++++++++++++++++++----- tycho/source/gui/mapview.rs | 6 ++++++ 7 files changed, 47 insertions(+), 17 deletions(-) diff --git a/rust-qt-binding-generator b/rust-qt-binding-generator index 8a37b35..2a8664e 160000 --- a/rust-qt-binding-generator +++ b/rust-qt-binding-generator @@ -1 +1 @@ -Subproject commit 8a37b359ca9dba414d84472b1750e952a42710f6 +Subproject commit 2a8664e9bfed74150c6a2df28c78f2d6b3f1a674 diff --git a/tycho/bindings.json b/tycho/bindings.json index 778711c..a957c79 100644 --- a/tycho/bindings.json +++ b/tycho/bindings.json @@ -59,6 +59,13 @@ "type": "Widget", "baseClass": "IProjectView", "functions": { + "drawView": { + "return": "void", + "mut": false, + "arguments": [ + {"name": "paint", "type": "*mut void"} + ] + } }, "properties": { }, diff --git a/tycho/cc/interface.h b/tycho/cc/interface.h index 1e7ad1e..007f1d3 100644 --- a/tycho/cc/interface.h +++ b/tycho/cc/interface.h @@ -3,6 +3,7 @@ #pragma clang diagnostic ignored "-Winconsistent-missing-override" #include +#include #include class IProjectModel : public QAbstractItemModel @@ -40,6 +41,15 @@ public: using QWidget::QWidget; virtual ~IProjectView() {} + + virtual void drawView(void *paint) const = 0; + +protected: + void paintEvent(QPaintEvent *event) override + { + QPainter painter{this}; + drawView(static_cast(&painter)); + } }; // EOF diff --git a/tycho/cc/mapview.cc b/tycho/cc/mapview.cc index 1e3ea06..bf26a10 100644 --- a/tycho/cc/mapview.cc +++ b/tycho/cc/mapview.cc @@ -14,16 +14,11 @@ MapView::~MapView() dbgPrintFunc(); } -void MapView::paintEvent(QPaintEvent *) -{ - [[maybe_unused]] QPainter painter{this}; -} - // TODO: move to paint.cc extern "C" { - void paint_point(QPainter *view, int x, int y) + void paint_point(QPainter *paint, int x, int y) { - view->drawPoint(x, y); + paint->drawPoint(x, y); } } diff --git a/tycho/cc/tycho.h b/tycho/cc/tycho.h index b91dbd3..3762132 100644 --- a/tycho/cc/tycho.h +++ b/tycho/cc/tycho.h @@ -15,7 +15,6 @@ #include #include #include -#include #include "../ui/ui_about.h" #include "../ui/ui_license.h" @@ -55,9 +54,6 @@ public: explicit MapView(Project *parent); ~MapView(); -protected: - void paintEvent(QPaintEvent *event) override; - private: MapModel *const m_mapModel; }; diff --git a/tycho/source/cc.rs b/tycho/source/cc.rs index 01029ce..995694c 100644 --- a/tycho/source/cc.rs +++ b/tycho/source/cc.rs @@ -1,8 +1,24 @@ //! C++ functions. -mod ffi { +use maraiah::ffi; + +mod static_ffi { + use maraiah::ffi; + + pub type QPainter = ffi::c_void; + extern "C" { - pub fn critical_msg(title: maraiah::ffi::NT, msg: maraiah::ffi::NT); + pub fn critical_msg(title: ffi::NT, msg: ffi::NT); + pub fn paint_point(paint: *mut QPainter, x: ffi::c_int, y: ffi::c_int); + } +} + +pub use static_ffi::QPainter; + +pub fn paint_point(paint: *mut QPainter, x: i32, y: i32) +{ + unsafe { + static_ffi::paint_point(paint, x.into(), y.into()); } } @@ -10,11 +26,11 @@ pub fn critical_msg(title: T, msg: U) where T: ToString, U: ToString { - let title = std::ffi::CString::new(title.to_string()).unwrap(); - let msg = std::ffi::CString::new(msg.to_string()).unwrap(); + let title = ffi::CString::new(title.to_string()).unwrap(); + let msg = ffi::CString::new(msg.to_string()).unwrap(); unsafe { - ffi::critical_msg(title.as_ptr(), msg.as_ptr()); + static_ffi::critical_msg(title.as_ptr(), msg.as_ptr()); } } diff --git a/tycho/source/gui/mapview.rs b/tycho/source/gui/mapview.rs index 02e5672..f005c05 100644 --- a/tycho/source/gui/mapview.rs +++ b/tycho/source/gui/mapview.rs @@ -1,6 +1,7 @@ //! Map view. use super::qobj::*; +use crate::cc; impl IMapViewTrait for IMapView { @@ -16,6 +17,11 @@ impl IMapViewTrait for IMapView /// Returns the emitter of `self`. fn emit(&mut self) -> &mut IMapViewEmitter {&mut self.emit} + + fn draw_view(&self, paint: *mut cc::QPainter) + { + cc::paint_point(paint, 20, 20); + } } impl Drop for IMapView