Subversion Repositories Scribus

Rev

Rev 17163 | Rev 17648 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12418 jghali 1
#!/usr/bin/env python
2
# -*- coding: utf-8  -*-
3
 
4
# ****************************************************************************
5
#  This program is free software; you can redistribute it and/or modify 
6
#  it under the terms of the GNU General Public License as published by
7
#  the Free Software Foundation; either version 2 of the License, or
8
#  (at your option) any later version.
9
#
10
#  This program is distributed in the hope that it will be useful,
11
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
#  GNU General Public License for more details.
14
#
15
#  You should have received a copy of the GNU General Public License
16
#  along with this program; if not, write to the Free Software
17
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
# 
19
# ****************************************************************************
20
 
21
 
22
"""
17637 gpittman 23
 
12418 jghali 24
(C) 2005 by Thomas R. Koll, <tomk32@gmx.de>, http://verlag.tomk32.de
25
 
17637 gpittman 26
(c) 2008, 2010, 2012 modifications, additional features, and reversion back to using PIL again!    by Gregory Pittman
14649 jghali 27
 
12418 jghali 28
A simple script for exact placement of a frame (infobox)
29
over the current textbox, asking the user for the width
30
of the infobox and in which column to place it.
14649 jghali 31
 
12418 jghali 32
Some enhancements:
14649 jghali 33
 
12418 jghali 34
* You can now create a text frame or an image frame, and also load
35
an image.
14649 jghali 36
 
37
* More than one infobox can be added to a text frame by repeatedly running
38
  the script (ie, no name conflicts occur).
39
 
40
* Height and Y-Pos of top of infobox can be specified.
41
 
42
* Works with any page unit - pts, mm, in, and picas, cm, and even ciceros.
43
 
12418 jghali 44
* Infobox has Text Flows Around Frame activated, also
14649 jghali 45
  Scale Image to Frame for images.
12418 jghali 46
 
17637 gpittman 47
* If you load an image with the script, an exactly correct frame height is made.
48
 
12418 jghali 49
USAGE
14649 jghali 50
 
12418 jghali 51
Select a textframe, start the script and have phun
52
Default name for the infobox is 'infobox' + name_of_selected_frame,
53
but this can be changed.
54
 
55
 
56
"""
57
 
58
try:
59
    import scribus
60
except ImportError:
61
    print "Unable to import the 'scribus' module. This script will only run within"
62
    print "the Python interpreter embedded in Scribus. Try Script->Execute Script."
63
    sys.exit(1)
17637 gpittman 64
try:
65
    from PIL import Image
66
except ImportError:
67
    print "Unable to import the Python Imaging Library module."
68
    sys.exit(1)
12418 jghali 69
 
70
def main(argv):
71
    unit = scribus.getUnit()
72
    units = [' pts','mm',' inches',' picas','cm',' ciceros']
73
    unitlabel = units[unit]
74
    if scribus.selectionCount() == 0:
75
        scribus.messageBox('Scribus - Script Error',
76
            "There is no object selected.\nPlease select a text frame and try again.",
77
            scribus.ICON_WARNING, scribus.BUTTON_OK)
78
        sys.exit(2)
79
    if scribus.selectionCount() > 1:
80
        scribus.messageBox('Scribus - Script Error',
81
            "You have more than one object selected.\nPlease select one text frame and try again.",
82
            scribus.ICON_WARNING, scribus.BUTTON_OK)
83
        sys.exit(2)
84
    textbox = scribus.getSelectedObject()
85
    pageitems = scribus.getPageItems()
86
    boxcount = 1
87
    for item in pageitems:
88
        if (item[0] == textbox):
89
            if (item[1] != 4):
90
                scribus.messageBox('Scribus - Script Error',
91
                          "This is not a textframe. Try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
92
                sys.exit(2)
93
 
94
# While we're finding out what kind of frame is selected, we'll also make sure we
95
# will come up with a unique name for our infobox frame - it's possible we may want
96
# more than one for a multicolumn frame.
97
        if (item[0] == ("infobox" + str(boxcount) + textbox)):
98
                boxcount += 1
99
    left, top = scribus.getPosition(textbox)
100
    o_width, o_height = scribus.getSize(textbox)
101
    o_cols = int(scribus.getColumns(textbox))
102
    o_gap = scribus.getColumnGap(textbox)
103
 
104
    columns_width = 0
105
    column_pos = 0
106
    o_colwidth = (o_width - ((o_cols - 1) * o_gap)) / o_cols
107
    if (o_cols > 1):
108
        while (columns_width > o_cols or columns_width < 1):
109
            columns_width = scribus.valueDialog('Width',
110
                                            'How many columns width shall the '+
111
                                            'box be (max ' + str(o_cols) + ')?','1')
112
            columns_width = int(columns_width)
113
        if (columns_width < o_cols):
114
            max = o_cols - columns_width
115
            while (column_pos <= max and column_pos <= 1):
116
                column_pos = scribus.valueDialog('Placement',
117
                                         'In which column do you want '
118
                                         'to place the box (1 to ' +
119
                                         str(o_cols) + ')?','1')
120
            column_pos = int(column_pos) - 1
121
    if (o_cols == 1):
122
        columns_width = 1
123
    new_height = 0
124
    while (new_height == 0):
125
        new_height = scribus.valueDialog('Height','Your frame height is '+ str(o_height) +
126
                                                 unitlabel +'. How tall\n do you want your ' +
127
                                                 'infobox to be in '+ unitlabel +'?\n If you load an image, height will be\n calculated, so the value here does not\n matter.', str(o_height))
128
    new_top = -1
129
    while (new_top < 0):
130
        new_top = scribus.valueDialog('Y-Pos','The top of your infobox is currently\n'+ str(top) +
131
                                                 unitlabel +'. Where do you want \n' +
132
                                                 'the top to be in '+ unitlabel +'?', str(top))
133
    framename = scribus.valueDialog('Name of Frame','Name your frame or use this default name',"infobox" + str(boxcount) + textbox)
134
    frametype = 'text'
135
    frametype = scribus.valueDialog('Frame Type','Change to anything other\n than "text" for image frame.\nEnter "imageL" to also load an image',frametype)
136
    new_width = columns_width * o_colwidth + (columns_width-1) * o_gap
137
    new_left = left + ((column_pos) * o_colwidth) + ((column_pos) * o_gap)
138
    if (frametype == 'text'):
139
        new_textbox = scribus.createText(new_left, float(new_top), new_width, float(new_height),framename)
140
        scribus.setColumnGap(0, new_textbox)
141
        scribus.setColumns(1, new_textbox)
142
        scribus.textFlowMode(new_textbox, 1)
143
    else:
144
        if (frametype == 'imageL'):
145
            imageload = scribus.fileDialog('Load image','Images(*.jpg *.png *.tif *.JPG *.PNG *.jpeg *.JPEG *.TIF)',haspreview=1)
17637 gpittman 146
            im = Image.open(imageload)
147
            xsize, ysize = im.size
148
            new_height = float(ysize)/float(xsize)*new_width
12418 jghali 149
            new_image = scribus.createImage(new_left, float(new_top), new_width, float(new_height),framename)
150
            scribus.loadImage(imageload, new_image)
151
        else:
152
            new_image = scribus.createImage(new_left, float(new_top), new_width, float(new_height),framename)
153
        scribus.textFlowMode(new_image, 1)
17637 gpittman 154
        scribus.setScaleImageToFrame(1,1,new_image)
155
#        scribus.setScaleImageToFrame(scaletoframe=1, proportional=1, name=new_image)
12418 jghali 156
if __name__ == '__main__':
157
    # This script makes no sense without a document open
158
    if not scribus.haveDoc():
159
        scribus.messageBox('Scribus - Script Error', "No document open", scribus.ICON_WARNING, scribus.BUTTON_OK)
160
        sys.exit(1)
161
    # Disable redraws
162
    scribus.setRedraw(False)
163
    # Run the main script, ensuring redraws are re-enabled even if the
164
    # script aborts with an exception, and don't fail with an exception
165
    # even if the document is closed while the script runs.
166
    try:
167
        main(sys.argv)
168
    finally:
169
        try:
170
            scribus.setRedraw(True)
171
        except:
172
            pass
173