1#!/usr/bin/python
2# Align_image_in_frame.py
3# This version 2014.04.19
4"""
5This script will align an image inside a frame to one of 9 possible positions:
6Top left, top center, top right; middle left, middle center, middle right;
7or bottom left, bottom center, bottom right.
8
9USAGE
10Select one or more image frames. Run the script, which asks for your alignment
11choice (all selected frames will need to have the same alignment). Choose the
12position in the dialog radio button grid, click Align. Image(s) are aligned, and script quits.
13
14Note
15There is minimal error checking, in particular no checking for frame type.
16
17See the wiki page for further info:
18wiki.scribus.net/canvas/Align_an_Image_in_its_Frame
19
20"""
21
22import scribus
23
24try:
25    from Tkinter import *
26    from tkFont import Font
27except ImportError:
28    print "This script requires Python's Tkinter properly installed."
29    scribus.messageBox('Script failed',
30               'This script requires Python\'s Tkinter properly installed.',
31               scribus.ICON_CRITICAL)
32    sys.exit(1)
33
34
35
36class TkImageAlignmentWizard(Frame):
37    """ GUI interface for aligning an image in a frame"""
38
39    def __init__(self, master=None):
40        """ Setup the dialog """
41        # refernce to the localization dictionary
42        self.key = 'English'
43        Frame.__init__(self, master)
44        self.grid()
45        self.master.geometry('120x120-80+40')
46        self.master.title('Scribus Image Alignment Wizard')
47        #define widgets
48        # alignment options
49        self.alignLabel = Label(self, text='Select alignment:')
50        self.alignVar = StringVar()
51        self.alignRadio1 = Radiobutton(self, text='', variable=self.alignVar, value="TL")
52        self.alignRadio2 = Radiobutton(self, text='', variable=self.alignVar, value="TC")
53        self.alignRadio3 = Radiobutton(self, text='', variable=self.alignVar, value="TR")
54        self.alignRadio4 = Radiobutton(self, text='', variable=self.alignVar, value="ML")
55        self.alignRadio5 = Radiobutton(self, text='', variable=self.alignVar, value="MC")
56        self.alignRadio6 = Radiobutton(self, text='', variable=self.alignVar, value="MR")
57        self.alignRadio7 = Radiobutton(self, text='', variable=self.alignVar, value="BL")
58        self.alignRadio8 = Radiobutton(self, text='', variable=self.alignVar, value="BC")
59        self.alignRadio9 = Radiobutton(self, text='', variable=self.alignVar, value="BR")
60        self.alignButton = Button(self, text='Align', command=self.alignImage)
61
62        # setup values
63        self.alignRadio5.select()
64        # make layout
65        self.columnconfigure(0, pad=0)
66        currRow = 0
67        self.alignLabel.grid(column=0, row=currRow, columnspan=3)
68        currRow += 1
69        self.alignRadio1.grid(column=0, row=currRow)
70        self.alignRadio2.grid(column=1, row=currRow)
71        self.alignRadio3.grid(column=2, row=currRow)
72        currRow += 1
73        self.alignRadio4.grid(column=0, row=currRow)
74        self.alignRadio5.grid(column=1, row=currRow)
75        self.alignRadio6.grid(column=2, row=currRow)
76        currRow += 1
77        self.alignRadio7.grid(column=0, row=currRow)
78        self.alignRadio8.grid(column=1, row=currRow)
79        self.alignRadio9.grid(column=2, row=currRow)
80        currRow += 1
81        self.alignButton.grid(column=0, row=currRow, columnspan=3)
82#        self.doneButton.grid(column=3, row=currRow, columnspan=3)
83
84    def alignImage(self):
85        if scribus.haveDoc():
86	    restore_units = scribus.getUnit()   # since there is an issue with units other than points,
87	    scribus.setUnit(0)			# we switch to points then restore later.
88            nbrSelected = scribus.selectionCount()
89            objList = []
90            for i in range(nbrSelected):
91                objList.append(scribus.getSelectedObject(i))
92            scribus.deselectAll()
93            for i in range(nbrSelected):
94                try:
95                    obj = objList[i]
96                    scribus.selectObject(obj)
97                    frameW, frameH = scribus.getSize(obj)
98                    saveScaleX, saveScaleY = scribus.getImageScale(obj)
99                    scribus.setScaleImageToFrame(1, 0, obj)
100                    fullScaleX, fullScaleY = scribus.getImageScale(obj)
101                    scribus.setScaleImageToFrame(0, 0, obj)
102                    scribus.setImageScale(saveScaleX, saveScaleY, obj)
103                    imageW = frameW * (saveScaleX / fullScaleX)
104                    imageH = frameH * (saveScaleY / fullScaleY)
105                    imageX = 0.0
106                    imageY = 0.0
107
108                    if self.alignVar.get()[0] == "T":
109                        imageY = 0.0
110                    elif self.alignVar.get()[0] == "M":
111                        imageY = (frameH - imageH) / 2.0
112                    elif self.alignVar.get()[0] == "B":
113                        imageY = (frameH - imageH)
114                    if self.alignVar.get()[1] == "L":
115                        imageX = 0.0
116                    elif self.alignVar.get()[1] == "C":
117                        imageX = (frameW - imageW) / 2.0
118                    elif self.alignVar.get()[1] == "R":
119                        imageX = (frameW - imageW)
120
121                    scribus.setImageOffset(imageX, imageY, obj)
122                    scribus.docChanged(1)
123                    scribus.setRedraw(True)
124                    scribus.deselectAll()
125                except:
126                    nothing = "nothing"
127	    scribus.setUnit(restore_units)
128
129	    self.master.destroy()
130
131
132def main():
133    """ Application/Dialog loop with Scribus sauce around """
134    try:
135        root = Tk()
136        app = TkImageAlignmentWizard(root)
137        root.mainloop()
138    finally:
139        if scribus.haveDoc():
140            scribus.redrawAll()
141
142if __name__ == '__main__':
143    main()