commit 4eceb1ac312221c71a1d56c087913f1b4aa20025
parent 91450f11ff4e31aa056e42f86be5c29610629b7b
Author: lumidify <nobody@lumidify.org>
Date:   Thu,  9 Apr 2020 09:03:00 +0200
At least kind of scale the images properly
Diffstat:
| M | README |  |  | 15 | +-------------- | 
| M | config.json |  |  | 2 | +- | 
| M | viewer2.pl |  |  | 72 | +++++++++++++++++++++++++++++++++++++++++++++++++++++------------------- | 
3 files changed, 55 insertions(+), 34 deletions(-)
diff --git a/README b/README
@@ -1,18 +1,5 @@
 This is a stupid little flashcard reader for LaTeX files. You probably
-don't want to use it. The images aren't even resized properly. It just
-works for me, that's why I use it.
-
-The maximum image size is currently hardcoded for my monitor size.
-If you actually want to use this for some weird reason and don't have
-a 1280x800 monitor with the exact bars in my setup of the dwm window
-manager, maybe change it.
-Just change the following two lines in the `gui` function:
-
-    my $w_final = $w < 1280 ? $w : 1280;
-    my $h_final = $h < 600 ? $h : 600;
-
-1280 is the maximum width and 600 the maximum height of the backside
-of the flashcard.
+don't want to use it. It just works for me, that's why I use it.
 
 `viewer.pl` is the old version using Tk, `viewer2.pl` is the new version
 using Gtk2.
diff --git a/config.json b/config.json
@@ -1 +1 @@
-{"cards":{"06":3,"02":3,"03":4,"01":3,"04":3,"05":3}}
+{"cards":{"02":4,"03":5,"05":4,"04":10,"01":10,"06":4}}
diff --git a/viewer2.pl b/viewer2.pl
@@ -83,9 +83,29 @@ sub find_card_view_count {
 	}
 }
 
+sub load_images_front {
+	my ($image_front1, $image_front2, $window_w, $card_id) = @_;
+	my ($fmt, $w, $h) = Gtk2::Gdk::Pixbuf->get_file_info("cache/${card_id}_front1.png");
+	$w = $w < $window_w ? $w : $window_w;
+	my $pixbuf1 = Gtk2::Gdk::Pixbuf->new_from_file_at_size("cache/${card_id}_front1.png", $w, -1);
+	($fmt, $w, $h) = Gtk2::Gdk::Pixbuf->get_file_info("cache/${card_id}_front1.png");
+	$w = $w < $window_w ? $w : $window_w;
+	my $pixbuf2 = Gtk2::Gdk::Pixbuf->new_from_file_at_size("cache/${card_id}_front2.png", $w, -1);
+	$image_front1->set_from_pixbuf($pixbuf1);
+	$image_front2->set_from_pixbuf($pixbuf2);
+}
+
+sub load_image_back {
+	my ($image_back, $window_w, $image_back_h, $card_id) = @_;
+	my ($fmt, $w, $h) = Gtk2::Gdk::Pixbuf->get_file_info("cache/${card_id}_back.png");
+	my $w_final = $w < $window_w ? $w : $window_w;
+	my $h_final = $h < $image_back_h ? $h : $image_back_h;
+	my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_size("cache/${card_id}_back.png", $w_final, $h_final);
+	$image_back->set_from_pixbuf($pixbuf);
+}
+
 sub next_card {
-	my ($config, $cards, $cards_backreference, $img_vbox, $window, $cur_card_id) = @_;
-	$img_vbox->foreach(sub {$_[0]->destroy});
+	my ($config, $cards, $cards_backreference, $image_front1, $image_front2, $window_w, $window, $cur_card_id) = @_;
 	my ($view_count, $card_id);
 	if ($cur_card_id && ($card_id = $cards_backreference->{$cur_card_id})) {
 		$view_count = find_card_view_count $cards, $card_id;
@@ -98,13 +118,7 @@ sub next_card {
 		}
 	}
 	$window->set_title($card_id);
-	my $pixbuf1 = Gtk2::Gdk::Pixbuf->new_from_file("cache/${card_id}_front1.png");
-	my $pixbuf2 = Gtk2::Gdk::Pixbuf->new_from_file("cache/${card_id}_front2.png");
-	my $image1 = Gtk2::Image->new_from_pixbuf($pixbuf1);
-	my $image2 = Gtk2::Image->new_from_pixbuf($pixbuf2);
-	$img_vbox->pack_start($image1, FALSE, FALSE, 0);
-	$img_vbox->pack_start($image2, FALSE, FALSE, 0);
-	$img_vbox->show_all;
+	load_images_front $image_front1, $image_front2, $window_w, $card_id;
 	return ($view_count, $card_id);
 }
 
@@ -117,6 +131,13 @@ sub gui {
         my $window = Gtk2::Window->new('toplevel');
 	$window->signal_connect(delete_event => sub {return FALSE});
 	$window->signal_connect(destroy => sub { Gtk2->main_quit; });
+	my $window_w = 0;
+	my $image_back_h = 0;
+	$window->signal_connect(configure_event => sub {
+		my ($window, $event) = @_;
+		$window_w = $event->width;
+		return FALSE;
+	});
 	$window->set_border_width(10);
 
 	my $view_count;
@@ -128,23 +149,26 @@ sub gui {
 	my $hbox = Gtk2::HBox->new(FALSE, 5);
 	my $button = Gtk2::Button->new_with_mnemonic("_Next card");
 
-	my $img_vbox = Gtk2::VBox->new();
+	my $image_front1 = Gtk2::Image->new();
+	my $image_front2 = Gtk2::Image->new();
+	my $image_back = Gtk2::Image->new();
+	$image_back->signal_connect(size_allocate => sub {
+		my ($image, $event) = @_;
+		$image_back_h = $event->height;
+		return FALSE;
+	}, $window);
+
 	$button->signal_connect(clicked => sub {
 		$view_count_increased = 0;
-		($view_count, $card_id) = next_card $config, $cards, $cards_backreference, $img_vbox, $window, $card_id;
+		($view_count, $card_id) = next_card $config, $cards, $cards_backreference, $image_front1, $image_front2, $window_w, $window, $card_id;
+		$image_back->clear();
 	}, $window);
 	$hbox->pack_start($button, FALSE, FALSE, 0);
 	my $label = Gtk2::Label->new("");
 	$button = Gtk2::Button->new_with_mnemonic("Reveal _back");
 	$button->signal_connect(clicked => sub {
 		if (!$view_count_increased) {
-			my ($fmt, $w, $h) = Gtk2::Gdk::Pixbuf->get_file_info("cache/${card_id}_back.png");
-			my $w_final = $w < 1280 ? $w : 1280;
-			my $h_final = $h < 600 ? $h : 600;
-			my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_size("cache/${card_id}_back.png", $w_final, $h_final);
-			my $image = Gtk2::Image->new_from_pixbuf($pixbuf);
-			$img_vbox->pack_start($image, FALSE, FALSE, 0);
-			$image->show;
+			load_image_back $image_back, $window_w, $image_back_h, $card_id;
 			$view_count_increased = 1;
 			$config->{cards}->{$card_id}++;
 			$cards->{$view_count+1}->{$card_id} = $cards->{$view_count}->{$card_id};
@@ -156,9 +180,19 @@ sub gui {
 		}
 	}, $window);
 	$hbox->pack_start($button, FALSE, FALSE, 0);
+	$button = Gtk2::Button->new_with_mnemonic("_Reload images");
+	$button->signal_connect(clicked => sub {
+		load_images_front $image_front1, $image_front2, $window_w, $card_id;
+		if ($view_count_increased) {
+			load_image_back $image_back, $window_w, $image_back_h, $card_id;
+		}
+	}, $window);
+	$hbox->pack_start($button, FALSE, FALSE, 0);
 	$hbox->pack_start($label, FALSE, FALSE, 0);
 	$vbox->pack_start($hbox, FALSE, FALSE, 0);
-	$vbox->pack_start($img_vbox, FALSE, FALSE, 0);
+	$vbox->pack_start($image_front1, FALSE, FALSE, 0);
+	$vbox->pack_start($image_front2, FALSE, FALSE, 0);
+	$vbox->pack_start($image_back, TRUE, TRUE, 0);
 	$window->add($vbox);
 	$window->show_all;
 	Gtk2->main;