Skip to content

Commit

Permalink
Refactor flipdialog
Browse files Browse the repository at this point in the history
  • Loading branch information
atareao committed Sep 29, 2019
1 parent 7ef0c2d commit e6aa310
Show file tree
Hide file tree
Showing 5 changed files with 345 additions and 121 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -57,7 +57,7 @@ Common required dependencies,
#### Only for Nautilus

* gir1.2-nautilus-3.0
* * python-nautilus
* python-nautilus

#### Only for Nemo

Expand Down
145 changes: 95 additions & 50 deletions src/pdf-tools/basedialog.py
Expand Up @@ -46,15 +46,85 @@
from tools import get_pages_from_ranges
from tools import str2int

def generate_widget_row(text, widget=None):
row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
label = Gtk.Label(text, xalign=0)
hbox.pack_start(label, True, True, 0)
if widget is not None:
hbox.pack_start(widget, False, True, 0)
return row

def generate_separator_row():
row = Gtk.ListBoxRow()
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
vbox.pack_start(Gtk.Separator(), True, True, 0)
vbox.set_margin_top(10)
vbox.set_margin_bottom(10)
row.add(vbox)
return row

def generate_title_row(text, gray=False):
if gray:
label = Gtk.Label()
label.set_markup(
'<span foreground="gray">{}</span>'.format(text))
else:
label = Gtk.Label(text)
label.set_width_chars(10)
label.set_alignment(0, 0.5)
row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
hbox.pack_start(label, True, True, 0)
return row


def generate_entry_row(text):
textBox = Gtk.Entry()
row = generate_widget_row(text, textBox)
return row, textBox


def generate_check_row(text, parent=None, callback=None):
check = Gtk.RadioButton.new_from_widget(parent)
if callback is not None:
check.connect("notify::active", callback, str(text))
row = generate_widget_row(text, check)
return check, row


def generate_check_entry_row(text, parent, callback=None):
row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
label = Gtk.Label(text, xalign=0)
entry = Gtk.Entry()
check = Gtk.RadioButton.new_from_widget(parent)
if callback is not None:
check.connect("notify::active", callback, str(text))
hbox.pack_start(label, True, True, 0)
hbox.pack_start(entry, True, True, 0)
hbox.pack_start(check, False, True, 0)
return check, entry, row


def generate_swith_row(text, callback=None):
switch = Gtk.Switch()
if callback is not None:
switch.connect("notify::active", callback, str(text))
row = generate_widget_row(text, switch)
return switch, row

def generate_button(icon, tooltip_text, callback):
button = Gtk.Button()
button.set_tooltip_text(tooltip_text)
button.set_image(Gtk.Image.new_from_gicon(Gio.ThemedIcon(
name=icon), Gtk.IconSize.BUTTON))
button.connect('clicked', callback)
return button

def set_separator():
row = Gtk.ListBoxRow()
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
vbox.pack_start(Gtk.Separator(), True, True, 0)
vbox.set_margin_top(10)
vbox.set_margin_bottom(10)
row.add(vbox)
return row

class BaseDialog(Gtk.Dialog):
def __init__(self, title= '', filename=None, window=None):
Expand Down Expand Up @@ -122,18 +192,11 @@ def init_headerbar(self):
self.hb.set_title(self.get_title())
self.set_titlebar(self.hb)

button0 = Gtk.Button()
button0.set_tooltip_text(_('First page'))
button0.set_image(Gtk.Image.new_from_gicon(Gio.ThemedIcon(
name='go-first-symbolic'), Gtk.IconSize.BUTTON))
button0.connect('clicked', self.first_page)
button0 = generate_button('go-first-symbolic', _('First page'),
self.first_page)
self.hb.pack_start(button0)

button1 = Gtk.Button()
button1.set_tooltip_text(_('Previous page'))
button1.set_image(Gtk.Image.new_from_gicon(Gio.ThemedIcon(
name='go-previous-symbolic'), Gtk.IconSize.BUTTON))
button1.connect('clicked', self.previous_page)
button1 = generate_button('go-previous-symbolic', _('Previous page'),
self.previous_page)
self.hb.pack_start(button1)

self.show_title_page = Gtk.Entry()
Expand All @@ -143,18 +206,12 @@ def init_headerbar(self):
self.show_title_page.connect('activate', self.on_current_page_activate)
self.hb.pack_start(self.show_title_page)

button2 = Gtk.Button()
button2.set_tooltip_text(_('Next page'))
button2.set_image(Gtk.Image.new_from_gicon(Gio.ThemedIcon(
name='go-next-symbolic'), Gtk.IconSize.BUTTON))
button2.connect('clicked', self.next_page)
button2 = generate_button('go-next-symbolic', _('Next page'),
self.next_page)
self.hb.pack_start(button2)

button3 = Gtk.Button()
button3.set_tooltip_text(_('Last page'))
button3.set_image(Gtk.Image.new_from_gicon(Gio.ThemedIcon(
name='go-last-symbolic'), Gtk.IconSize.BUTTON))
button3.connect('clicked', self.last_page)
button3 = generate_button('go-last-symbolic', _('Last page'),
self.last_page)
self.hb.pack_start(button3)

popover = self.create_popover()
Expand All @@ -181,18 +238,12 @@ def create_popover(self):
row.add(hbox)
self.popover_listbox.add(row)

button0 = Gtk.Button()
button0.set_tooltip_text(_('First page'))
button0.set_image(Gtk.Image.new_from_gicon(Gio.ThemedIcon(
name='go-first-symbolic'), Gtk.IconSize.BUTTON))
button0.connect('clicked', self.first_page)
button0 = generate_button('go-first-symbolic', _('First page'),
self.first_page)
hbox.pack_start(button0, True, True, 0)

button1 = Gtk.Button()
button1.set_tooltip_text(_('Previous page'))
button1.set_image(Gtk.Image.new_from_gicon(Gio.ThemedIcon(
name='go-previous-symbolic'), Gtk.IconSize.BUTTON))
button1.connect('clicked', self.previous_page)
button1 = generate_button('go-previous-symbolic', _('Previous page'),
self.previous_page)
hbox.pack_start(button1, True, True, 0)

self.show_page = Gtk.Entry()
Expand All @@ -201,20 +252,14 @@ def create_popover(self):
self.show_page.connect('activate', self.on_current_page_activate)
hbox.pack_start(self.show_page, True, True, 0)

button2 = Gtk.Button()
button2.set_tooltip_text(_('Next page'))
button2.set_image(Gtk.Image.new_from_gicon(Gio.ThemedIcon(
name='go-next-symbolic'), Gtk.IconSize.BUTTON))
button2.connect('clicked', self.next_page)
button2 = generate_button('go-next-symbolic', _('Next page'),
self.next_page)
hbox.pack_start(button2, True, True, 0)

button3 = Gtk.Button()
button3.set_tooltip_text(_('Last page'))
button3.set_image(Gtk.Image.new_from_gicon(Gio.ThemedIcon(
name='go-last-symbolic'), Gtk.IconSize.BUTTON))
button3.connect('clicked', self.last_page)
button3 = generate_button('go-last-symbolic', _('Last page'),
self.last_page)
hbox.pack_start(button3, True, True, 0)
self.popover_listbox.add(set_separator())
self.popover_listbox.add(generate_separator_row())

self.init_adicional_popover()

Expand Down
99 changes: 30 additions & 69 deletions src/pdf-tools/flipdialog.py
Expand Up @@ -34,7 +34,9 @@
from comun import _
from tools import get_ranges
from tools import get_pages_from_ranges
from basedialog import BaseDialog
from basedialog import BaseDialog, generate_separator_row, generate_title_row
from basedialog import generate_swith_row, generate_check_entry_row
from basedialog import generate_check_row
class PageOptions():
def __init__(self, rotation_angle, flip_horizontal, flip_vertical):
self.rotation_angle = rotation_angle
Expand Down Expand Up @@ -65,60 +67,21 @@ def set_page(self, page):
flip_vertical)

def init_adicional_popover(self):
self.popover_listbox.add(generate_title_row(_('Apply'), True))

def set_title_row(texto, gray=False):
if gray:
label = Gtk.Label()
label.set_markup(
'<span foreground="gray">{}</span>'.format(texto))
else:
label = Gtk.Label(texto)
label.set_width_chars(10)
label.set_alignment(0, 0.5)
row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
hbox.pack_start(label, True, True, 0)
return row

self.popover_listbox.add(set_title_row(_('Apply'), True))

def set_option_rotate_apply(texto, check=None,
parent=None, entry=False):
row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
label = Gtk.Label(texto, xalign=0)
if check is None:
check = Gtk.RadioButton.new_from_widget(parent)
check.connect("notify::active", self.slider_on_value_changed,
str(texto))
hbox.pack_start(label, True, True, 0)

if entry:
textBox = Gtk.Entry()
hbox.pack_start(textBox, True, True, 0)
else:
textBox = None
hbox.pack_start(check, False, True, 0)
return check, row, textBox

self.check_this = Gtk.RadioButton.new_from_widget(None)
self.check_this.connect("notify::active", self.slider_on_value_changed,
'This page')
check, row, _ = set_option_rotate_apply('This page',
self.check_this)
self.check_this, row = generate_check_row(_('This page'), None,
self.slider_on_value_changed)
self.popover_listbox.add(row)
self.check_all, row, textBox = set_option_rotate_apply(
_('All'), None, self.check_this)
self.check_all, row = generate_check_row(_('All'), self.check_this,
self.slider_on_value_changed)
self.popover_listbox.add(row)
self.check_range, row, self.range = set_option_rotate_apply(
_('Range'), None, self.check_this, True)
self.check_range, self.range, row = generate_check_entry_row(
_('Range'), self.check_this, self.slider_on_value_changed)
self.popover_listbox.add(row)

self.popover_listbox.add(self.set_separator())
self.popover_listbox.add(generate_separator_row())

self.popover_listbox.add(set_title_row(_('Rotate'), True))
self.popover_listbox.add(generate_title_row(_('Rotate'), True))

def set_option_rotate_row(texto, check=None, parent=None):
row = Gtk.ListBoxRow()
Expand All @@ -133,25 +96,21 @@ def set_option_rotate_row(texto, check=None, parent=None):
hbox.pack_start(check, False, True, 0)
return check, row

self.rotate_0 = Gtk.RadioButton.new_from_widget(None)
self.rotate_0.connect("notify::active", self.slider_on_value_changed,
'0')
check, row =set_option_rotate_row('0', self.rotate_0)
self.rotate_0, row = generate_check_row(
'0', None, self.slider_on_value_changed)
self.popover_listbox.add(row)

self.rotate_90, row = set_option_rotate_row('90', None, self.rotate_0)
self.rotate_90, row = generate_check_row(
'90', self.rotate_0, self.slider_on_value_changed)
self.popover_listbox.add(row)

self.rotate_180, row = set_option_rotate_row('180', None,
self.rotate_0)
self.rotate_180, row = generate_check_row(
'180', self.rotate_0, self.slider_on_value_changed)
self.popover_listbox.add(row)

self.rotate_270, row = set_option_rotate_row('270', None,
self.rotate_0)
self.rotate_270, row = generate_check_row(
'270', self.rotate_0, self.slider_on_value_changed)
self.popover_listbox.add(row)

self.popover_listbox.add(self.set_separator())
self.popover_listbox.add(set_title_row(_('Flip'), True))
self.popover_listbox.add(generate_separator_row())
self.popover_listbox.add(generate_title_row(_('Flip'), True))

def set_option_flip_row(texto):
row = Gtk.ListBoxRow()
Expand All @@ -165,21 +124,23 @@ def set_option_flip_row(texto):
hbox.pack_start(check, False, True, 0)
return check, row

self.check_vertical, row = set_option_flip_row(_('Vertical'))
self.check_vertical, row = generate_swith_row(
_('Vertical'), self.slider_on_value_changed)
self.popover_listbox.add(row)
self.check_horizontal, row = set_option_flip_row(_('Horizontal'))
self.check_horizontal, row = generate_swith_row(
_('Horizontal'), self.slider_on_value_changed)
self.popover_listbox.add(row)

self.popover_listbox.add(self.set_separator())
self.popover_listbox.add(set_title_row(_('File name'), True))
self.popover_listbox.add(generate_separator_row())
self.popover_listbox.add(generate_title_row(_('File name'), True))

row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
label = Gtk.Label(_('Add to file'), xalign=0)
text = Gtk.Entry()
self.add_to_file = Gtk.Entry()
hbox.pack_start(label, True, True, 0)
hbox.pack_start(text, False, True, 0)
hbox.pack_start(self.add_to_file, False, True, 0)
self.popover_listbox.add(row)

def slider_on_value_changed(self, widget, value, name):
Expand Down
2 changes: 1 addition & 1 deletion src/pdf-tools/tools.py
Expand Up @@ -55,7 +55,7 @@ def update_preview_cb(file_chooser, preview):
filename = file_chooser.get_preview_filename()
try:
print('---', filename, '---')
pixbuf = tools.get_surface_from_pdf(filename, 250)
pixbuf = get_surface_from_pdf(filename, 250)
if pixbuf is not None:
preview.set_from_surface(pixbuf)
has_preview = True
Expand Down

0 comments on commit e6aa310

Please sign in to comment.