Skip to content

Instantly share code, notes, and snippets.

@seungwon0
Created July 30, 2012 02:24
Show Gist options
  • Save seungwon0/3203479 to your computer and use it in GitHub Desktop.
Save seungwon0/3203479 to your computer and use it in GitHub Desktop.
Simple GUI for qrencode
#!/usr/bin/env perl
#
# qrencode-gui.pl - Simple GUI for qrencode
#
# Requires qrencode.
#
# Seungwon Jeong <[email protected]>
#
# Copyright (C) 2012 by Seungwon Jeong
#
# 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 perl5i::2;
use Glib qw< TRUE FALSE >;
use Gtk2 -init;
use File::Temp;
my $text_view = Gtk2::TextView->new;
my $encode_button = Gtk2::Button->new('_Encode');
$encode_button->signal_connect( clicked => \&encode );
my $image = Gtk2::Image->new;
my $save_as_button = Gtk2::Button->new_from_stock('gtk-save-as');
$save_as_button->set_sensitive(FALSE);
$save_as_button->signal_connect( clicked => \&save_as );
my $table = Gtk2::Table->new( 2, 2 );
$table->set_border_width(5);
$table->set_row_spacings(5);
$table->set_col_spacings(5);
$table->attach( $text_view, 0, 1, 0, 1, [qw< expand fill>],
[qw< expand fill >], 0, 0 );
$table->attach( $encode_button, 0, 1, 1, 2, [qw< shrink fill>],
[qw< shrink fill >], 0, 0 );
$table->attach( $image, 1, 2, 0, 1, [qw< shrink fill>], [qw< shrink fill >],
0, 0 );
$table->attach( $save_as_button, 1, 2, 1, 2, [qw< shrink fill>],
[qw< shrink fill >], 0, 0 );
my $tooltips = Gtk2::Tooltips->new;
$tooltips->set_tip( $text_view, 'Input data' );
$tooltips->set_tip( $encode_button, 'Encode input data' );
$tooltips->set_tip( $image, 'QR code' );
$tooltips->set_tip( $save_as_button, 'Save QR code as image file' );
my $window = Gtk2::Window->new;
$window->set_title('qrencode - QR Code Generator');
$window->signal_connect( destroy => sub { Gtk2->main_quit; } );
$window->add($table);
$window->set_default_size( 400, 200 );
$window->show_all;
Gtk2->main;
sub encode {
my $text_buffer = $text_view->get_buffer;
my $data = $text_buffer->get_text( $text_buffer->get_start_iter,
$text_buffer->get_end_iter, TRUE );
return if length $data == 0;
my $temp_file = File::Temp->new;
system 'qrencode', '-o', $temp_file->filename, $data;
$image->set_from_file( $temp_file->filename );
$save_as_button->set_sensitive(TRUE);
return;
}
sub save_as {
my $jpg_file_filter = Gtk2::FileFilter->new;
$jpg_file_filter->set_name('JPEG Image');
$jpg_file_filter->add_mime_type('image/jpeg');
$jpg_file_filter->add_pattern('*.jpg');
my $png_file_filter = Gtk2::FileFilter->new;
$png_file_filter->set_name('PNG Image');
$png_file_filter->add_mime_type('image/png');
$png_file_filter->add_pattern('*.png');
my $file_chooser_dialog = Gtk2::FileChooserDialog->new(
'Please choose a file to save QR code.',
$window, 'save',
_OK => 'ok',
_Cancel => 'cancel',
);
$file_chooser_dialog->add_filter($jpg_file_filter);
$file_chooser_dialog->add_filter($png_file_filter);
if ( $file_chooser_dialog->run eq 'ok' ) {
my $filename = $file_chooser_dialog->get_filename;
my $type = ( $filename =~ /[.] jpg $/xms ) ? 'jpeg' : 'png';
$image->get_pixbuf->save( $filename, $type );
}
$file_chooser_dialog->destroy;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment