[niepce] fwk: RatingLabel is now just a gtk::Widget



commit b34adb72c2bd67c3c68f6db9ef5edb3c110f97e3
Author: Hubert Figuière <hub figuiere net>
Date:   Mon May 23 23:09:00 2022 -0400

    fwk: RatingLabel is now just a gtk::Widget
    
    - suppress clippy warning about Lazy
    - use snapshot and not gdk pixbuf

 Cargo.lock                                         |  1 +
 crates/npc-fwk/Cargo.toml                          |  1 +
 crates/npc-fwk/src/toolkit/widgets/rating_label.rs | 79 ++++++++++------------
 niepce-main/src/niepce/ui/library_cell_renderer.rs |  2 +-
 src/fwk/toolkit/metadatawidget.cpp                 | 10 ++-
 5 files changed, 44 insertions(+), 49 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 296f62f..1ce92fa 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -753,6 +753,7 @@ dependencies = [
  "gio-sys",
  "glib",
  "glib-sys",
+ "graphene-rs",
  "gtk4",
  "gtk4-sys",
  "lazy_static",
diff --git a/crates/npc-fwk/Cargo.toml b/crates/npc-fwk/Cargo.toml
index 788f008..14e9abb 100644
--- a/crates/npc-fwk/Cargo.toml
+++ b/crates/npc-fwk/Cargo.toml
@@ -20,6 +20,7 @@ gtk4-sys = "*"
 gdk4 = "^0.4.6"
 gdk-pixbuf-sys = "*"
 gdk-pixbuf = "^0.15.6"
+graphene-rs = "0.15.1"
 gtk4 = "^0.4.6"
 lazy_static = "^1.2.0"
 libc = "0.2.39"
diff --git a/crates/npc-fwk/src/toolkit/widgets/rating_label.rs 
b/crates/npc-fwk/src/toolkit/widgets/rating_label.rs
index 100802c..56a2c6d 100644
--- a/crates/npc-fwk/src/toolkit/widgets/rating_label.rs
+++ b/crates/npc-fwk/src/toolkit/widgets/rating_label.rs
@@ -21,29 +21,28 @@ use libc::c_int;
 use std::cell::Cell;
 
 use gdk4::prelude::*;
-use gdk_pixbuf::Pixbuf;
 use glib::subclass::prelude::*;
 use glib::subclass::Signal;
 use glib::translate::*;
 use gtk4::prelude::*;
 use gtk4::subclass::prelude::*;
 
-use once_cell::unsync::Lazy;
-
 struct Pixbufs {
-    star: Pixbuf,
-    unstar: Pixbuf,
+    star: gdk4::Texture,
+    unstar: gdk4::Texture,
 }
 
-const PIXBUFS: Lazy<Pixbufs> = Lazy::new(|| Pixbufs {
-    star: Pixbuf::from_resource("/org/gnome/Niepce/pixmaps/niepce-set-star.png").unwrap(),
-    unstar: Pixbuf::from_resource("/org/gnome/Niepce/pixmaps/niepce-unset-star.png").unwrap(),
-});
+lazy_static::lazy_static! {
+    static ref PIXBUFS: Pixbufs = Pixbufs {
+        star: gdk4::Texture::from_resource("/org/gnome/Niepce/pixmaps/niepce-set-star.png"),
+        unstar: gdk4::Texture::from_resource("/org/gnome/Niepce/pixmaps/niepce-unset-star.png"),
+    };
+}
 
 glib::wrapper! {
     pub struct RatingLabel(
         ObjectSubclass<RatingLabelPriv>)
-        @extends gtk4::DrawingArea, gtk4::Widget;
+        @extends gtk4::Widget;
 }
 
 pub struct RatingLabelPriv {
@@ -76,7 +75,7 @@ impl RatingLabelPriv {
 impl ObjectSubclass for RatingLabelPriv {
     const NAME: &'static str = "RatingLabel";
     type Type = RatingLabel;
-    type ParentType = gtk4::DrawingArea;
+    type ParentType = gtk4::Widget;
 
     fn new() -> Self {
         Self {
@@ -92,12 +91,9 @@ impl ObjectImpl for RatingLabelPriv {
 
         let click = gtk4::GestureClick::new();
         click.connect_pressed(glib::clone!(@weak obj => move |gesture, n, x, y| {
-            let this = Self::from_instance(&obj);
-            this.press_event(gesture, n, x, y);
+            obj.imp().press_event(gesture, n, x, y);
         }));
         obj.add_controller(&click);
-
-        obj.set_draw_func(&RatingLabel::draw_func);
     }
 
     fn signals() -> &'static [Signal] {
@@ -162,17 +158,16 @@ pub trait RatingLabelExt {
 
 impl RatingLabelExt for RatingLabel {
     fn set_rating(&self, rating: i32) {
-        let priv_ = RatingLabelPriv::from_instance(self);
-        priv_.set_rating(rating);
+        self.imp().set_rating(rating);
     }
 }
 
 impl RatingLabel {
-    pub fn star() -> Pixbuf {
+    pub fn star() -> gdk4::Texture {
         PIXBUFS.star.clone()
     }
 
-    pub fn unstar() -> Pixbuf {
+    pub fn unstar() -> gdk4::Texture {
         PIXBUFS.unstar.clone()
     }
 
@@ -183,10 +178,10 @@ impl RatingLabel {
     }
 
     pub fn draw_rating(
-        cr: &cairo::Context,
+        snapshot: &gtk4::Snapshot,
         rating: i32,
-        star: &Pixbuf,
-        unstar: &Pixbuf,
+        star: &gdk4::Texture,
+        unstar: &gdk4::Texture,
         x: f32,
         y: f32,
     ) {
@@ -196,26 +191,17 @@ impl RatingLabel {
         let h = star.height() as f32;
         let mut y = y;
         y -= h;
-        let mut x = x;
+        snapshot.save();
+        snapshot.translate(&graphene::Point::new(x, y));
         for i in 1..=5 {
             if i <= rating {
-                cr.set_source_pixbuf(star, x.into(), y.into());
+                star.snapshot(snapshot.upcast_ref::<gdk4::Snapshot>(), w as f64, h as f64);
             } else {
-                cr.set_source_pixbuf(unstar, x.into(), y.into());
+                unstar.snapshot(snapshot.upcast_ref::<gdk4::Snapshot>(), w as f64, h as f64);
             }
-            on_err_out!(cr.paint());
-            x += w;
+            snapshot.translate(&graphene::Point::new(w, 0.0));
         }
-    }
-
-    fn draw_func(widget: &gtk4::DrawingArea, cr: &cairo::Context, _: i32, _: i32) {
-        let star = RatingLabel::star();
-        let x = 0_f32;
-        let y = star.height() as f32;
-        let rating = RatingLabelPriv::from_instance(widget.downcast_ref::<RatingLabel>().unwrap())
-            .rating
-            .get(); // this shouldn't fail.
-        RatingLabel::draw_rating(cr, rating, &star, &RatingLabel::unstar(), x, y);
+        snapshot.restore();
     }
 
     pub fn rating_value_from_hit_x(x: f64) -> i32 {
@@ -226,15 +212,13 @@ impl RatingLabel {
     pub fn new(rating: i32, editable: bool) -> Self {
         let obj: Self = glib::Object::new(&[]).expect("Failed to create RatingLabel");
 
-        let priv_ = RatingLabelPriv::from_instance(&obj);
+        let priv_ = &obj.imp();
         priv_.set_editable(editable);
         priv_.set_rating(rating);
         obj
     }
 }
 
-impl DrawingAreaImpl for RatingLabelPriv {}
-
 impl WidgetImpl for RatingLabelPriv {
     fn measure(
         &self,
@@ -250,6 +234,17 @@ impl WidgetImpl for RatingLabelPriv {
 
         (m, m, -1, -1)
     }
+
+    fn snapshot(&self, widget: &Self::Type, snapshot: &gtk4::Snapshot) {
+        let star = RatingLabel::star();
+        let x = 0_f32;
+        let y = star.height() as f32;
+        let rating = (widget.downcast_ref::<RatingLabel>().unwrap())
+            .imp()
+            .rating
+            .get(); // this shouldn't fail.
+        RatingLabel::draw_rating(snapshot, rating, &star, &RatingLabel::unstar(), x, y);
+    }
 }
 
 #[no_mangle]
@@ -265,10 +260,10 @@ pub extern "C" fn fwk_rating_label_new(rating: c_int, editable: bool) -> *mut gt
 /// Dereference the widget pointer.
 #[no_mangle]
 pub unsafe extern "C" fn fwk_rating_label_set_rating(
-    widget: *mut gtk4_sys::GtkDrawingArea,
+    widget: *mut gtk4_sys::GtkWidget,
     rating: i32,
 ) {
-    let rating_label = gtk4::DrawingArea::from_glib_none(widget)
+    let rating_label = gtk4::Widget::from_glib_none(widget)
         .downcast::<RatingLabel>()
         .expect("Not a RatingLabel widget");
     rating_label.set_rating(rating);
diff --git a/niepce-main/src/niepce/ui/library_cell_renderer.rs 
b/niepce-main/src/niepce/ui/library_cell_renderer.rs
index 35ce172..d9465c0 100644
--- a/niepce-main/src/niepce/ui/library_cell_renderer.rs
+++ b/niepce-main/src/niepce/ui/library_cell_renderer.rs
@@ -546,7 +546,7 @@ impl CellRendererImpl for LibraryCellRendererPriv {
             let x = r.x() + CELL_PADDING;
             let y = r.y() + r.height() - CELL_PADDING;
             RatingLabel::draw_rating(
-                &cr,
+                snapshot,
                 rating,
                 &RatingLabel::star(),
                 &RatingLabel::unstar(),
diff --git a/src/fwk/toolkit/metadatawidget.cpp b/src/fwk/toolkit/metadatawidget.cpp
index 1170ce4..2c9df53 100644
--- a/src/fwk/toolkit/metadatawidget.cpp
+++ b/src/fwk/toolkit/metadatawidget.cpp
@@ -17,7 +17,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-
 #include <utility>
 
 #include <boost/lexical_cast.hpp>
@@ -25,7 +24,6 @@
 #include <boost/rational.hpp>
 
 #include <glibmm/i18n.h>
-#include <gtkmm/drawingarea.h>
 #include <gtkmm/entry.h>
 #include <gtkmm/eventcontrollerfocus.h>
 #include <gtkmm/label.h>
@@ -74,9 +72,9 @@ void MetaDataWidget::rating_callback(GtkWidget* w, gint rating, gpointer user_da
 Gtk::Widget*
 MetaDataWidget::create_star_rating_widget(bool readonly, ffi::NiepcePropertyIdx id)
 {
-    Gtk::DrawingArea* r =
+    Gtk::Widget* r =
         Gtk::manage(Glib::wrap(
-                        GTK_DRAWING_AREA(ffi::fwk_rating_label_new(0, !readonly))));
+                        GTK_WIDGET(ffi::fwk_rating_label_new(0, !readonly))));
     if (!readonly) {
         r->set_data("id", GINT_TO_POINTER(id));
         g_signal_connect(r->gobj(), "rating-changed", G_CALLBACK(rating_callback), this);
@@ -225,7 +223,7 @@ void MetaDataWidget::clear_widget(const std::pair<const ffi::NiepcePropertyIdx,
         tv->get_buffer()->set_text("");
         return;
     }
-    Gtk::DrawingArea* rl = dynamic_cast<Gtk::DrawingArea*>(p.second);
+    Gtk::Widget* rl = dynamic_cast<Gtk::Widget*>(p.second);
     if (rl) {
         ffi::fwk_rating_label_set_rating(rl->gobj(), 0);
         return;
@@ -322,7 +320,7 @@ bool MetaDataWidget::set_star_rating_data(Gtk::Widget* w,
     try {
         int rating = fwk_property_value_get_integer(value.get());
         AutoFlag flag(m_update);
-        ffi::fwk_rating_label_set_rating(static_cast<Gtk::DrawingArea*>(w)->gobj(), rating);
+        ffi::fwk_rating_label_set_rating(static_cast<Gtk::Widget*>(w)->gobj(), rating);
     }
     catch(...) {
         return false;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]