PDF-Embanner/pdfembannersrc/editwmark.py

261 lines
11 KiB
Python
Raw Normal View History

2018-05-18 06:49:19 +00:00
# -*-coding:utf-8 -*
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
from pdfembannersrc import strings
import logging
logger = logging.getLogger()
class InterfaceEdit(tk.Toplevel):
"""
Edition for wmark
"""
def __init__(self, parent, wmark, **kwargs):
tk.Toplevel.__init__(self, parent)
self.transient(parent)
self.grab_set()
self.geometry("500x300")
self.bind("<Return>", self.ok)
self.bind("<Escape>", self.close)
self.bind('<<ComboboxSelected>>', self.comboselectmaj)
self.wmark = wmark
self.name = tk.StringVar()
if(self.wmark.name is not None):self.name.set(self.wmark.name)
self.position = tk.StringVar()
if(self.wmark.position is not None):self.position.set(self.wmark.position)
self.userposition = tk.StringVar()
if(self.wmark.userposition is not None and self.position.get()==strings.custom):
self.userposition.set("{},{}".format(self.wmark.userposition[0],self.wmark.userposition[1]))
self.text = tk.StringVar()
if(self.wmark.text is not None):self.text.set(self.wmark.text)
self.size = tk.StringVar()
if(self.wmark.size is not None):self.size.set(self.wmark.size)
self.notfirstpage = self.wmark.notfirstpage
self.notlastpage = self.wmark.notlastpage
self.onlyfirstpage = self.wmark.onlyfirstpage
self.onlylastpage = self.wmark.onlylastpage
self.notpages = tk.StringVar()
if(self.wmark.notpages is not None):self.notpages.set(self.wmark.notpages)
self.onlypages = tk.StringVar()
if(self.wmark.onlypages is not None):self.onlypages.set(self.wmark.onlypages)
self.bold = self.wmark.bold
self.italic = self.wmark.italic
self.boxed = self.wmark.boxed
self.lined = self.wmark.lined
self.title("Edit {}".format(self.wmark.name))
self.f = tk.Frame(self, width=768, height=576, **kwargs)
self.f.pack(fill=tk.BOTH)
# Création de nos widgets
self.f.columnconfigure(1, weight=1)
self.f.rowconfigure(10, weight=1)
tk.Label(self.f, text="Name:").grid(row=0, column=0)
tk.Entry(self.f, textvariable=self.name).grid(row=0, column=1, columnspan=2, sticky=tk.W+tk.E)
tk.Label(self.f, text="Text:", fg="red").grid(row=1, column=0)
tk.Entry(self.f, textvariable=self.text).grid(row=1, column=1, columnspan=2, sticky=tk.W+tk.E)
tk.Label(self.f, text="Position:").grid(row=2, column=0)
ttk.Combobox(self.f, state='readonly', textvariable=self.position, values=strings.positionlist).grid(row=2, column=1, columnspan=2, sticky=tk.W+tk.E)
self.Wuserposition = tk.Entry(self.f, textvariable=self.userposition, state='readonly')
self.Wuserposition.grid(row=3, column=1, columnspan=2, sticky=tk.W+tk.E)
if(self.position.get()==strings.custom):self.Wuserposition['state']='normal'
self.sf = ttk.Labelframe(self.f, text='Text formatting')
self.sf.grid(row=4, column=0, columnspan=3, sticky=tk.W+tk.E)
self.pf = ttk.Labelframe(self.f, text='Pages to apply')
self.pf.grid(row=5, column=0, columnspan=3, sticky=tk.W+tk.E)
self.pf.columnconfigure(0, weight=1)
self.pf.columnconfigure(1, weight=1)
self.Wnotfirstpage = tk.Checkbutton(self.pf, text="Not on first page", command=self.toggle_notfirstpage)
self.Wnotfirstpage.grid(row=0, column=0)
if(self.notfirstpage):self.Wnotfirstpage.select()
self.Wonlyfirstpage = tk.Checkbutton(self.pf, text="Only on first page", command=self.toggle_onlyfirstpage)
self.Wonlyfirstpage.grid(row=0, column=1)
if(self.onlyfirstpage):self.Wonlyfirstpage.select()
self.Wnotlastpage = tk.Checkbutton(self.pf, text="Not on last page", command=self.toggle_notlastpage)
self.Wnotlastpage.grid(row=1, column=0)
if(self.notlastpage):self.Wnotlastpage.select()
self.Wonlylastpage = tk.Checkbutton(self.pf, text="Only on last page", command=self.toggle_onlylastpage)
self.Wonlylastpage.grid(row=1, column=1)
if(self.onlylastpage):self.Wonlylastpage.select()
tk.Label(self.pf, text="Not on pages:").grid(row=2, column=0)
tk.Entry(self.pf, textvariable=self.notpages).grid(row=2, column=1)
tk.Label(self.pf, text="Only on pages:").grid(row=3, column=0)
tk.Entry(self.pf, textvariable=self.onlypages).grid(row=3, column=1)
self.sf.columnconfigure(0, weight=1)
self.sf.columnconfigure(1, weight=1)
self.sf.columnconfigure(2, weight=1)
tk.Label(self.sf, text="Size:").grid(row=0, column=0)
ttk.Combobox(self.sf, state='readonly', textvariable=self.size, values=strings.sizelist).grid(row=0, column=1, columnspan=2)
self.Wbold = tk.Checkbutton(self.sf, text="Bold", command=self.toggle_bold)
self.Wbold.grid(row=1, column=0)
if(self.bold):self.Wbold.select()
self.Witalic = tk.Checkbutton(self.sf, text="Italic", command=self.toggle_italic)
self.Witalic.grid(row=1, column=1)
if(self.italic):self.Witalic.select()
self.Wboxed = tk.Checkbutton(self.sf, text="Boxed", command=self.toggle_boxed)
self.Wboxed.grid(row=2, column=0)
if(self.boxed):self.Wboxed.select()
self.Wlined = tk.Checkbutton(self.sf, text="Lined", command=self.toggle_lined)
self.Wlined.grid(row=2, column=1)
if(self.lined):self.Wlined.select()
tk.Button(self.f, text="Ok", command=self.ok).grid(row=10, column=1, columnspan=2, sticky=tk.W+tk.E)
tk.Button(self.f, text="Close", command=self.close).grid(row=10, column=0)
ttk.Separator(self.f, orient="vertical").grid(row=0, column=3, rowspan=7, sticky=tk.N+tk.S, padx=5, pady=5)
self.tf = ttk.Labelframe(self.f, text='Text directives')
self.tf.grid(row=0, column=4, columnspan=2, rowspan=4)
tk.Label(self.tf, text="%p").grid(row=1, column=0)
tk.Label(self.tf, text="%P").grid(row=2, column=0)
tk.Label(self.tf, text="Page number").grid(row=1, column=1, sticky=tk.W)
tk.Label(self.tf, text="Total page number").grid(row=2, column=1, sticky=tk.W)
self.tf = ttk.Labelframe(self.f, text='Text directives')
self.tf.grid(row=4, column=4, columnspan=2, rowspan=2)
tk.Label(self.tf, text="Custom position:", bg="grey").grid(row=1, column=0, sticky=tk.W+tk.E)
tk.Label(self.tf, text="coma-separated coord-").grid(row=2, column=0, sticky=tk.W)
tk.Label(self.tf, text="inates between 0 and 1").grid(row=3, column=0, sticky=tk.W)
tk.Label(self.tf, text="Not/only on pages:", bg="grey").grid(row=4, column=0, sticky=tk.W+tk.E)
tk.Label(self.tf, text="use e for even pages").grid(row=5, column=0, sticky=tk.W)
tk.Label(self.tf, text="and o for even pages").grid(row=6, column=0, sticky=tk.W)
tk.Label(self.tf, text="end is the last page").grid(row=7, column=0, sticky=tk.W)
tk.Label(self.tf, text="Separator: ; ranges: -").grid(row=8, column=0, sticky=tk.W)
def ok(self, *args):
"""
Save and quit
"""
self.wmark.name = self.name.get()
if(self.position.get()==''):
self.wmark.position = None
else:
self.wmark.position = self.position.get()
if(self.position.get()==strings.custom):
up = self.userposition.get().split(',')
if(len(up)==2):
x = up[0]
y = up[1]
try:
x = float(x)
y = float(y)
except:
messagebox.showwarning(title="Position", message="User defined position have to be coma-separated coordinates between 0 and 1")
return
if(x>=0. and y>=0. and y<=1. and x<=1.):
self.wmark.userposition = (x, y)
else:
messagebox.showwarning(title="Position", message="User defined position have to be coma-separated coordinates between 0 and 1")
return
else:
messagebox.showwarning(title="Position", message="User defined position have to be coma-separated coordinates between 0 and 1")
return
else:
self.wmark.userposition = None
self.wmark.text = self.text.get()
if(self.size.get()==''):
self.wmark.size=None
else:
self.wmark.size = self.size.get()
self.wmark.notfirstpage = self.notfirstpage
self.wmark.notlastpage = self.notlastpage
self.wmark.notpages = self.notpages.get()
self.wmark.onlyfirstpage = self.onlyfirstpage
self.wmark.onlylastpage = self.onlylastpage
self.wmark.onlypages = self.onlypages.get()
self.wmark.Wlabel["text"] = self.wmark.name
self.wmark.bold = self.bold
self.wmark.italic = self.italic
self.wmark.boxed = self.boxed
self.wmark.lined = self.lined
self.close()
def close(self, *args):
self.destroy()
def toggle_notfirstpage(self):
if(self.notfirstpage):
self.notfirstpage = False
self.Wnotfirstpage.deselect()
else:
self.notfirstpage = True
self.onlyfirstpage=False
self.Wnotfirstpage.select()
self.Wonlyfirstpage.deselect()
def toggle_notlastpage(self):
if(self.notlastpage):
self.notlastpage = False
self.Wnotlastpage.deselect()
else:
self.notlastpage = True
self.onlylastpage = False
self.Wnotlastpage.select()
self.Wonlylastpage.deselect()
def toggle_onlyfirstpage(self):
if(self.onlyfirstpage):
self.onlyfirstpage = False
self.Wonlyfirstpage.deselect()
else:
self.onlyfirstpage = True
self.notfirstpage = False
self.Wonlyfirstpage.select()
self.Wnotfirstpage.deselect()
def toggle_onlylastpage(self):
if(self.onlylastpage):
self.onlylastpage = False
self.Wonlylastpage.deselect()
else:
self.onlylastpage = True
self.notlastpage = False
self.Wonlylastpage.select()
self.Wnotlastpage.deselect()
def toggle_bold(self):
if(self.bold):
self.bold = False
self.Wbold.deselect()
else:
self.bold = True
self.Wbold.select()
def toggle_italic(self):
if(self.italic):
self.italic = False
self.Witalic.deselect()
else:
self.italic = True
self.Witalic.select()
def toggle_boxed(self):
if(self.boxed):
self.boxed = False
self.Wboxed.deselect()
else:
self.boxed = True
self.Wboxed.select()
def toggle_lined(self):
if(self.lined):
self.lined = False
self.Wlined.deselect()
else:
self.lined = True
self.Wlined.select()
def comboselectmaj(self, *args):
if(self.position.get()==strings.custom):
self.Wuserposition['state']='normal'
else:
self.Wuserposition['state']='readonly'