forked from viktor-sarge/digitalaskyltar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edittab.py
153 lines (122 loc) · 5.53 KB
/
edittab.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
# Copyright 2013 Regionbibliotek Halland
#
# This file is part of Digitala skyltar.
#
# Digitala skyltar 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 skyltar 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 skyltar. If not, see <http://www.gnu.org/licenses/>.
import os.path
from common import getConfigParser
from Tkinter import Frame, Canvas, Scrollbar, Button, NORMAL, DISABLED, W, N, E, S, NSEW, VERTICAL, HORIZONTAL, ALL
from tkMessageBox import showwarning, showerror, askyesnocancel
from configfilewriter import ConfigFileWriter
import language as lng
from language import lang
class EditTab(Frame):
"""A tab that represents a config file"""
def __init__(self, filename, parentnotebook):
"""Create a new EditTab
Argument
filename -- name and path to the file being edited
parentnotebook -- the NoteBook in which this tab will be added
"""
Frame.__init__(self)
self._filename = filename
self._parentnotebook = parentnotebook
self._inputs = []
self.path = os.path.dirname(filename)
self._dirty = False
parentnotebook.add(self, state = 'normal')
self._setLabel()
#Set up GUI
self.rowconfigure(0, weight = 1)
self.columnconfigure(0, weight = 1)
vscroll = Scrollbar(self, orient = VERTICAL)
self._vscroll = vscroll
vscroll.grid(row=0, column=1, sticky=N+S)
hscroll = Scrollbar(self, orient = HORIZONTAL)
self._hscroll = hscroll
hscroll.grid(row=1, column=0, sticky=E+W)
canvas = Canvas(self, yscrollcommand = vscroll.set, xscrollcommand = hscroll.set)
self._canvas = canvas
canvas.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = NSEW)
vscroll['command'] = canvas.yview
hscroll['command'] = canvas.xview
scrollframe = Frame(canvas)
self._scrollframe = scrollframe
canvas.create_window(0, 0, window = scrollframe, anchor = N + W)
scrollframe.rowconfigure(0, weight = 1)
scrollframe.columnconfigure(0, weight = 1)
self._mainframe = Frame(scrollframe)
self._mainframe.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = NSEW)
cf = Frame(scrollframe)
self._control_frame = cf
cf.grid(row = 1, column = 0, padx = 5, pady = 5, sticky = E)
b = Button(cf, text = lang[lng.txtCopyImages], command = self._ehCopyImages)
self._btnCopy = b
b.grid(row = 0, column = 0, padx = 5, pady = 5)
b = Button(cf, text = lang[lng.txtSave], command = self._ehSave, state = DISABLED)
self._btnSave = b
b.grid(row = 0, column = 1, padx = 5, pady = 5)
b = Button(cf, text = lang[lng.txtClose], command = self._ehClose)
b.grid(row = 0, column = 2, padx = 5, pady = 5)
parentnotebook.after_idle(self._setCanvas)
def load(self):
"""If the config file exists, try to load it"""
if(os.path.exists(self._filename)):
cfgp = getConfigParser(self._filename)
for input in self._inputs:
input.readValue(cfgp)
def setDirty(self, dummy = None):
self._dirty = True
self._btnCopy.config(state = NORMAL)
self._btnSave.config(state = NORMAL)
self._setLabel()
def _setCanvas(self):
self._parentnotebook.update_idletasks()
self._canvas.config(scrollregion = self._canvas.bbox(ALL))
def _save(self):
"""Save configuration to file"""
cfw = ConfigFileWriter()
for input in self._inputs:
input.writeValue(cfw)
try:
cfw.writeToFile(self._filename)
except IOError:
showerror('Save to File', 'Could not save {}; please make sure you have permission and try again'.format(self._filename))
except Exception as e:
showerror('Save to File', 'Could not save {}; an unknown error occurred'.format(self._filename) + ': ' + str(e))
else:
self._dirty = False
self._btnSave.config(state = DISABLED)
self._setLabel()
def _setLabel(self):
"""Set the label of this tab"""
text = os.path.basename(self._filename)
if(self._dirty):
text = text + '*'
self._parentnotebook.tab(self, text = text)
def _ehCopyImages(self):
for input in self._inputs:
input.copyImages()
self.setDirty()
self._btnCopy.config(state = DISABLED)
def _ehClose(self):
if(self._dirty):
result = askyesnocancel('Unsaved Changes', 'There are unsaved changes. Do you want to save them before closing this tab?')
if(result is None):
return
elif(result):
self._save()
self._parentnotebook.forget(self)
def _ehSave(self):
self._save()