forked from viktor-sarge/digitalasagor
/
editarea.py
332 lines (254 loc) · 11.6 KB
/
editarea.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# Copyright 2013 Regionbibliotek Halland
#
# This file is part of Digitala sagor.
#
# Digitala sagor 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.
#
# Digitala sagor 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 Digitala sagor. If not, see <http://www.gnu.org/licenses/>.
from Tkinter import StringVar, Button, Label, Frame, W, N, E, S, NORMAL, DISABLED, LEFT
import ttk
import tkFileDialog
import shutil
import os
import os.path
from tkMessageBox import showerror
import pygame.mixer
import PIL.Image as Image
import PIL.ImageTk as ImageTk
import PIL.PngImagePlugin
import PIL.GifImagePlugin
import PIL.JpegImagePlugin
import PIL.BmpImagePlugin
Image._initialized=2
from tooltip import ToolTip
from scrollbox import IndexedFrame
from language import lang
import language as lng
from soundbutton import SoundButton
import spmanager as spm
import fileformat as ff
import language as lng
from language import lang
_previewWidth = 125
_previewHeight = 100
_displayedFilenameLength = 16
class EditArea(IndexedFrame):
"""A panel that is used for defining frames in slideshows"""
def __init__(self, parent, wdir, tooltipwidth, ehThis, ehFromHere):
"""Initiate
Arguments
parent -- parent tkinter item
wdir -- working directory
tooltipwidth -- maximum width of the tooltip
ehThis -- callback for playing one frame
ehFromHere -- callback for playing the rest of the frames from a given position
"""
IndexedFrame.__init__(self, parent)
self._wdir = wdir
self._il = None
self._parent = parent
self._lblFrame = StringVar()
self._lblImagename = StringVar()
self._lblImagenamefull = StringVar()
self._lblSoundname = StringVar()
self._lblSoundnamefull = StringVar()
self._fullSoundName = ''
content = self._content
content.columnconfigure(0, weight = 1)
content.columnconfigure(1, weight = 1)
content.rowconfigure(0, weight = 1)
content.rowconfigure(1, weight = 1)
content.rowconfigure(2, weight = 1)
#Left group box
lf = ttk.LabelFrame(content)
self._lfSlide = lf
lf.grid(row = 0, column = 0, sticky = W + N + E + S, padx = 5)
button = Button(lf, text = lang[lng.txtPlayThisFrame] + '...', command = lambda arg=self: ehThis(arg))
button.grid(row = 1, column = 0, sticky = W + E, padx = 5, pady = 5)
button = Button(lf, text = lang[lng.txtPlayFromHere] + '...', command = lambda arg=self: ehFromHere(arg))
button.grid(row = 2, column = 0, sticky = W + E, padx = 5, pady = 5)
#Image group box
lf = ttk.LabelFrame(content, text = ' ' + lang[lng.txtImage] + ' ')
lf.grid(row = 0, column = 1, padx = 5, sticky = W + N + E + S)
label = Label(lf, textvariable = self._lblImagename, justify = LEFT)
label.grid(row = 0, column = 1, padx = 5, pady = 5, sticky = W)
tt = ToolTip(label, '', textvariable = self._lblImagenamefull, wraplength = tooltipwidth)
button = Button(lf, text = lang[lng.txtSelect] + '...', command = self._ehGetImage)
button.grid(row = 0, column = 0, sticky = W, padx = 5, pady = 5)
button.ref = self
imFrame = Frame(lf)
self._imageFrame = imFrame
imFrame['width'] = _previewWidth
imFrame['height'] = _previewHeight
imFrame.grid(row = 1, column = 1, padx = 5, pady = 5)
imFrame.grid_propagate(0)
lf = ttk.Labelframe(content, text = ' ' + lang[lng.txtSound] + ' ')
lf.grid(row = 0, column = 2, padx = 5, sticky = W + N + E + S)
#Sound group box
label = Label(lf, textvariable = self._lblSoundname)
label.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = W, columnspan = 10)
tt = ToolTip(label, '', textvariable = self._lblSoundnamefull, wraplength = tooltipwidth)
button = SoundButton(lf, lang[lng.txtListen], lang[lng.txtStop], self._clbPlaying)
self._btnSound = button
button.grid(row = 1, column = 0, sticky = W + E, padx = 5, pady = 5)
button = Button(lf, text = lang[lng.txtSelect] + '...', command = self._ehGetSound)
button.grid(row = 2, column = 0, sticky = W + E, padx = 5, pady = 5)
self._btnSelect = button
button = Button(lf, text = lang[lng.txtDelete], state = DISABLED, command = self._ehRemoveSound)
button.grid(row = 3, column = 0, sticky = W + E, padx = 5, pady = 5)
self._btnRemove = button
fr = Frame(lf, width = 160)
fr.grid(row = 4, column = 0, columnspan = 10)
def setFrame(self, frame, copy = True):
"""Set the contents of a frame
Arguments
frame -- the frame to set
copy -- if not false, the contents of frame will be copied to the working directory
"""
self._addImage(frame.getImage(), copy)
self._addSound(frame.getSound(), copy)
def update(self):
"""Update the frame index displayed on the panel"""
self._lfSlide['text'] = ' ' + lang[lng.txtSlide] + ' ' + str(self.index + 1) + ' '
def getImage(self):
"""Return the name and path to the image in this frame"""
return self._lblImagenamefull.get()
def getSound(self):
"""Return the name and path to the sound in this frame"""
return self._lblSoundnamefull.get()
def _getIndex(self):
"""Return the index of this frame"""
return self.index
def _setLabel(self, stringvar, text):
"""Set the text of a StringVar and prefix it with an ellipsis if it is too long
stringvar -- the StringVar
text -- the text
"""
dispName = text
if(len(dispName) > _displayedFilenameLength):
dispName = '...' + dispName[-_displayedFilenameLength:]
stringvar.set(dispName)
def _setImage(self, imageName):
"""Display the image of this frame
Argument
imageName -- the name of the image
"""
imageNameFull = os.path.join(self._wdir, imageName)
try:
image = Image.open(imageNameFull)
except IOError:
return
image.thumbnail((_previewWidth, _previewHeight), Image.ANTIALIAS)
imageTk = ImageTk.PhotoImage(image)
self._lblImagenamefull.set(imageName)
self._setLabel(self._lblImagename, imageName)
if(self._il != None):
self._il.destroy()
#Show image
imagelabel = Label(self._imageFrame, image = imageTk)
imagelabel.place(relx=.5, rely=.5, anchor="c")
self._il = imagelabel
self._imageRef = imageTk
def _setSound(self, soundName):
"""Display the sound of this frame
Argument
soundName -- the name of the sound
"""
self._lblSoundnamefull.set(soundName)
self._setLabel(self._lblSoundname, soundName)
def _addImage(self, imageNameExtPath, copy = True):
"""Add an image to this frame
Arguments
imageNameExtPath -- the full name of the image
copy -- if not false, the image will be copied to the working directory
"""
if(copy):
self._copyFile(imageNameExtPath)
self._setImage(os.path.basename(imageNameExtPath))
def _addSound(self, soundNameExtPath, copy = True):
"""Add a sound to this frame
Arguments
soundNameExtPath -- the full name of the sound
copy -- if not false, the image will be copied to the working directory
"""
if(soundNameExtPath != ''):
newstate = NORMAL
if(copy):
self._copyFile(soundNameExtPath)
else:
newstate = DISABLED
self._setSound(os.path.basename(soundNameExtPath))
self._btnSound.setSound(self._getLocalFilename(soundNameExtPath))
self._btnRemove.config(state = newstate)
def _getLocalFilename(self, filenamepath):
"""Return the full name and path that a specified file would get in this frame
Argument
filenamepath -- the full name and path of the file
"""
if(filenamepath != ''):
filename = os.path.basename(filenamepath)
return os.path.join(self._wdir, filename)
else:
return ''
def _copyFile(self, filenamepath):
"""Copy a file to this frame
Argument
filenamepath -- the full name and path of the file
"""
if(filenamepath != ''):
shutil.copyfile(filenamepath, self._getLocalFilename(filenamepath))
def _clbPlaying(self, isPlaying):
"""
To be called when a sound is playing
Argument
isPlaying -- a sound is playing
"""
if(isPlaying):
newstate = DISABLED
else:
newstate = NORMAL
self._btnSelect.config(state = newstate)
self._btnRemove.config(state = newstate)
def _ehGetImage(self):
"""Event handler for the add image button"""
initdir = spm.spmanager.getFirstPath([spm.ImageFolder,
spm.SoundFolder,
spm.MostRecentFolder])
filename = tkFileDialog.askopenfilename(initialdir = initdir, filetypes = ff.dlgImageFormats)
if(len(filename) > 0):
try:
self._addImage(filename)
except IOError:
showerror(lang[lng.txtCopyError], lang[lng.txtCouldNotCopy] + os.path.basename(filename))
return
self._parent.setDirty()
spm.spmanager.setPath(spm.ImageFolder, os.path.dirname(filename))
def _ehRemoveSound(self):
"""Event handler for removing sound"""
self._setSound('')
self._btnSound.setSound('')
self._btnRemove.config(state = DISABLED)
self._parent.setDirty()
def _ehGetSound(self):
"""Event handler for the add sound button"""
initdir = spm.spmanager.getFirstPath([spm.SoundFolder,
spm.ImageFolder,
spm.MostRecentFolder])
filename = tkFileDialog.askopenfilename(initialdir = initdir, filetypes = ff.dlgSoundFormats)
if(len(filename) > 0):
try:
self._addSound(filename)
except IOError:
showerror(lang[lng.txtCopyError], lang[lng.txtCouldNotCopy] + os.path.basename(filename))
return
self._parent.setDirty()
spm.spmanager.setPath(spm.SoundFolder, os.path.dirname(filename))