[niepce] npc-fwk: Start porting RatingLabel code to Rust



commit de3a3f9bb5152e39525a870cafc078ef0c2a2df6
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Jan 11 19:17:00 2020 -0500

    npc-fwk: Start porting RatingLabel code to Rust

 Cargo.lock                                         | 92 ++++++++++++++++++++++
 crates/npc-fwk/Cargo.toml                          |  3 +
 crates/npc-fwk/src/lib.rs                          |  4 +-
 crates/npc-fwk/src/toolkit/mod.rs                  | 19 +++++
 crates/npc-fwk/src/toolkit/widgets/mod.rs          | 20 +++++
 crates/npc-fwk/src/toolkit/widgets/rating_label.rs | 81 +++++++++++++++++++
 src/Makefile.am                                    |  2 +
 7 files changed, 220 insertions(+), 1 deletion(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 10d4e83..c7b84ca 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -526,6 +526,14 @@ name = "linked-hash-map"
 version = "0.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
+[[package]]
+name = "lock_api"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "log"
 version = "0.4.8"
@@ -542,6 +550,11 @@ dependencies = [
  "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "maybe-uninit"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
 [[package]]
 name = "memchr"
 version = "2.2.1"
@@ -598,10 +611,12 @@ dependencies = [
 name = "npc-fwk"
 version = "0.1.0"
 dependencies = [
+ "cairo-rs 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "cbindgen 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "exempi 2.5.0 (git+https://github.com/hfiguiere/exempi-rs.git?rev=53cfc05)",
  "gdk 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "gdk-pixbuf 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "gio 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "gio-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "glib 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -609,6 +624,7 @@ dependencies = [
  "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
  "multimap 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "once_cell 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "rexiv2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "try_opt 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
@@ -645,6 +661,14 @@ name = "numtoa"
 version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
+[[package]]
+name = "once_cell"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "pango"
 version = "0.8.0"
@@ -670,6 +694,30 @@ dependencies = [
  "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "parking_lot"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "lock_api 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "parking_lot_core"
+version = "0.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "peeking_take_while"
 version = "0.1.2"
@@ -912,11 +960,37 @@ dependencies = [
  "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "rustc_version"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "ryu"
 version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
+[[package]]
+name = "scopeguard"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
+[[package]]
+name = "semver"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "semver-parser"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+
 [[package]]
 name = "serde"
 version = "1.0.94"
@@ -964,6 +1038,14 @@ name = "slab"
 version = "0.4.2"
 source = "registry+https://github.com/rust-lang/crates.io-index";
 
+[[package]]
+name = "smallvec"
+version = "0.6.13"
+source = "registry+https://github.com/rust-lang/crates.io-index";
+dependencies = [
+ "maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "strsim"
 version = "0.8.0"
@@ -1211,8 +1293,10 @@ dependencies = [
 "checksum libloading 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"a5692f82b51823e27c4118b3e5c0d98aee9be90633ebc71ad12afef380b50219"
 "checksum libsqlite3-sys 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d3711dfd91a1081d2458ad2d06ea30a8755256e74038be2ad927d94e1c955ca8"
 "checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83"
+"checksum lock_api 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"79b2de95ecb4691949fea4716ca53cdbcfccb2c612e19644a8bad05edcf9f47b"
 "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = 
"14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
 "checksum lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c"
+"checksum maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
 "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e"
 "checksum multimap 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"2eb04b9f127583ed176e163fb9ec6f3e793b87e21deedd5734a69386a18a0151"
 "checksum nom 4.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
@@ -1220,8 +1304,11 @@ dependencies = [
 "checksum num-rational 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"f2885278d5fe2adc2f75ced642d52d879bffaceb5a2e0b1d4309ffdfb239b454"
 "checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = 
"6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32"
 "checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
+"checksum once_cell 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d584f08c2d717d5c23a6414fc2822b71c651560713e54fa7eace675f758a355e"
 "checksum pango 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"1e9c6b728f1be8edb5f9f981420b651d5ea30bdb9de89f1f1262d0084a020577"
 "checksum pango-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"86b93d84907b3cf0819bff8f13598ba72843bee579d5ebc2502e4b0367b4be7d"
+"checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252"
+"checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b"
 "checksum peeking_take_while 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
 "checksum pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = 
"5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
 "checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = 
"676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c"
@@ -1252,13 +1339,18 @@ dependencies = [
 "checksum rexiv2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"bd37fad736a6936a741e98c75127561c8ce4af83683489fff0586dee2f985ef4"
 "checksum rusqlite 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"c9d9118f1ce84d8d0b67f9779936432fb42bb620cef2122409d786892cce9a3c"
 "checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8"
+"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = 
"138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
 "checksum ryu 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997"
+"checksum scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d"
+"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
+"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
 "checksum serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)" = 
"076a696fdea89c19d3baed462576b8f6d663064414b5c793642da8dfeb99475b"
 "checksum serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = 
"652bc323d694dc925829725ec6c890156d8e70ae5202919869cb00fe2eff3788"
 "checksum serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"32f1926285523b2db55df263d2aa4eb69ddcfa7a7eade6430323637866b513ab"
 "checksum serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)" = 
"051c49229f282f7c6f3813f8286cc1e3323e8051823fce42c7ea80fe13521704"
 "checksum shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = 
"7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2"
 "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = 
"c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
+"checksum smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = 
"f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6"
 "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
 "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
 "checksum syn 0.15.31 (registry+https://github.com/rust-lang/crates.io-index)" = 
"d2b4cfac95805274c6afdb12d8f770fa2d27c045953e7b630a81801953699a9a"
diff --git a/crates/npc-fwk/Cargo.toml b/crates/npc-fwk/Cargo.toml
index 9835d49..be48945 100644
--- a/crates/npc-fwk/Cargo.toml
+++ b/crates/npc-fwk/Cargo.toml
@@ -8,6 +8,7 @@ build = "build.rs"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+cairo-rs = "*"
 chrono = "0.4.0"
 exempi = { version = "2.5.0", git = "https://github.com/hfiguiere/exempi-rs.git";, rev="53cfc05" }
 gio-sys = "*"
@@ -15,9 +16,11 @@ gio = "^0.8.0"
 glib-sys = "*"
 glib = { version = "^0.9.0" }
 gdk = "^0.12.0"
+gdk-pixbuf = "0.8.0"
 lazy_static = "^1.2.0"
 libc = "0.2.39"
 multimap = "0.4.0"
+once_cell = "^0"
 rexiv2 = "^0.7.0"
 try_opt = "0.1.1"
 
diff --git a/crates/npc-fwk/src/lib.rs b/crates/npc-fwk/src/lib.rs
index c2af9a1..a226c79 100644
--- a/crates/npc-fwk/src/lib.rs
+++ b/crates/npc-fwk/src/lib.rs
@@ -1,7 +1,7 @@
 /*
  * niepce - fwk/lib.rs
  *
- * Copyright (C) 2017-2019 Hubert Figuière
+ * Copyright (C) 2017-2020 Hubert Figuière
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -20,6 +20,7 @@
 extern crate chrono;
 extern crate exempi;
 extern crate gdk;
+extern crate gdk_pixbuf;
 extern crate gio;
 extern crate gio_sys;
 extern crate glib;
@@ -28,6 +29,7 @@ extern crate glib_sys;
 extern crate lazy_static;
 extern crate libc;
 extern crate multimap;
+extern crate once_cell;
 #[macro_use]
 extern crate try_opt;
 
diff --git a/crates/npc-fwk/src/toolkit/mod.rs b/crates/npc-fwk/src/toolkit/mod.rs
index 6ff5719..5adcdc8 100644
--- a/crates/npc-fwk/src/toolkit/mod.rs
+++ b/crates/npc-fwk/src/toolkit/mod.rs
@@ -1,4 +1,23 @@
+/*
+ * niepce - crates/npc-fwk/src/toolkit/mod.rs
+ *
+ * Copyright (C) 2020 Hubert Figuière
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
 pub mod mimetype;
+pub mod widgets;
 
 pub type Sender<T> = glib::Sender<T>;
 
diff --git a/crates/npc-fwk/src/toolkit/widgets/mod.rs b/crates/npc-fwk/src/toolkit/widgets/mod.rs
new file mode 100644
index 0000000..737ba53
--- /dev/null
+++ b/crates/npc-fwk/src/toolkit/widgets/mod.rs
@@ -0,0 +1,20 @@
+/*
+ * niepce - crates/npc-fwk/src/toolkit/widgets/mod.rs
+ *
+ * Copyright (C) 2020 Hubert Figuière
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+pub mod rating_label;
diff --git a/crates/npc-fwk/src/toolkit/widgets/rating_label.rs 
b/crates/npc-fwk/src/toolkit/widgets/rating_label.rs
new file mode 100644
index 0000000..5abc8d2
--- /dev/null
+++ b/crates/npc-fwk/src/toolkit/widgets/rating_label.rs
@@ -0,0 +1,81 @@
+/*
+ * niepce - crates/npc-fwk/src/toolkit/widgets/rating_label.rs
+ *
+ * Copyright (C) 2020 Hubert Figuière
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+use cairo;
+use gdk::prelude::*;
+use gdk_pixbuf::Pixbuf;
+use once_cell::unsync::Lazy;
+
+struct Pixbufs {
+    star: Pixbuf,
+    unstar: Pixbuf,
+}
+
+const PIXBUFS: Lazy<Pixbufs> = Lazy::new(|| Pixbufs {
+    star: Pixbuf::new_from_resource("/org/gnome/Niepce/pixmaps/niepce-set-star.png").unwrap(),
+    unstar: Pixbuf::new_from_resource("/org/gnome/Niepce/pixmaps/niepce-unset-star.png").unwrap(),
+});
+
+pub struct RatingLabel {}
+
+impl RatingLabel {
+    pub fn get_star() -> Pixbuf {
+        PIXBUFS.star.clone()
+    }
+
+    pub fn get_unstar() -> Pixbuf {
+        PIXBUFS.unstar.clone()
+    }
+
+    pub fn get_geometry() -> (i32, i32) {
+        let star = Self::get_star();
+        (star.get_width() * 5, star.get_height())
+    }
+
+    pub fn draw_rating(
+        cr: &cairo::Context,
+        rating: i32,
+        star: &Pixbuf,
+        unstar: &Pixbuf,
+        x: f64,
+        y: f64,
+    ) {
+        let rating = if rating == -1 { 0 } else { rating };
+
+        let w = star.get_width();
+        let h = star.get_height();
+        let mut y = y;
+        y -= h as f64;
+        let mut x = x;
+        for i in 1..5 {
+            if i <= rating {
+                cr.set_source_pixbuf(star, x, y);
+            } else {
+                cr.set_source_pixbuf(unstar, x, y);
+            }
+            cr.paint();
+            x += w as f64;
+        }
+    }
+
+    pub fn rating_value_from_hit_x(x: f64) -> i32 {
+        let width: f64 = Self::get_star().get_width().into();
+        (x / width).round() as i32
+    }
+}
diff --git a/src/Makefile.am b/src/Makefile.am
index b71b7ab..26b3880 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -38,6 +38,8 @@ RUST_SOURCES = \
        @top_srcdir@/crates/npc-fwk/src/lib.rs \
        @top_srcdir@/crates/npc-fwk/src/toolkit/mimetype.rs \
        @top_srcdir@/crates/npc-fwk/src/toolkit/mod.rs \
+       @top_srcdir@/crates/npc-fwk/src/toolkit/widgets/mod.rs \
+       @top_srcdir@/crates/npc-fwk/src/toolkit/widgets/rating_label.rs \
        @top_srcdir@/crates/npc-fwk/src/utils/exempi.rs \
        @top_srcdir@/crates/npc-fwk/src/utils/exiv2.rs \
        @top_srcdir@/crates/npc-fwk/src/utils/files.rs \


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