-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqtmain_romacons.py
More file actions
255 lines (216 loc) · 10.5 KB
/
qtmain_romacons.py
File metadata and controls
255 lines (216 loc) · 10.5 KB
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
# CSD Facility
# qtmain_romacons.py
# PyQt GUI wrapper of ROMACONS processing logic.
# Largely a lazy copy/paste of qtmain_geotek.py.
import logging, os, re, sys, time, traceback
from PyQt5 import QtWidgets, QtCore
import common
import romacons
from gui import FileListPanel, errbox, infobox, inputbox, ProgressPanel, TwoButtonPanel, ButtonPanel
from prefs import Preferences
class UnmatchedFilenamesError(Exception):
def __init__(self, message):
super(UnmatchedFilenamesError, self).__init__(message)
self.message = message
class MainWindow(QtWidgets.QDialog):
def __init__(self, app):
QtWidgets.QDialog.__init__(self)
self.VERSION = "2.4"
self.app = app
self.app_path = None # init'd in self.initAppPath()
self.initAppPath()
self.initGUI()
self.installRulers()
self.initPrefs()
def initGUI(self):
self.setWindowTitle("CSD Facility ROMACONS Geotek Image Converter v{}".format(self.VERSION))
vlayout = QtWidgets.QVBoxLayout(self)
vlayout.setContentsMargins(10,5,10,5)
listLabel = "Images to be converted: click Add, or drag and drop files into the list below."
self.imageList = FileListPanel(listLabel)
self.imageList.addButton.setAutoDefault(False)
self.imageList.rmButton.setAutoDefault(False)
vlayout.addWidget(self.imageList, 1)
settingsGroupBox = QtWidgets.QGroupBox("Converter Settings")
settingsGroupLayout = QtWidgets.QVBoxLayout()
self.dpi = QtWidgets.QLineEdit()
self.trim = QtWidgets.QLineEdit()
self.icdScaling = QtWidgets.QLineEdit()
self.saveDefaultsButton = QtWidgets.QToolButton()
self.saveDefaultsButton.setText("Save Current Settings as Default")
self.saveDefaultsButton.clicked.connect(self.saveDefaultSettings)
dpiLayout = QtWidgets.QHBoxLayout()
dpiLayout.setSpacing(5)
dpiLayout.addWidget(QtWidgets.QLabel("Image and Ruler resolution:"))
dpiLayout.addWidget(self.dpi, QtCore.Qt.AlignLeft)
self.dpi.setFixedWidth(50)
dpiLayout.addWidget(QtWidgets.QLabel("DPI"))
dpiLayout.addStretch(5)
trimLayout = QtWidgets.QHBoxLayout()
trimLayout.setSpacing(5)
trimLayout.addWidget(QtWidgets.QLabel("Trim from top of core image:"))
trimLayout.addWidget(self.trim)
self.trim.setFixedWidth(50)
trimLayout.addWidget(QtWidgets.QLabel("inches"))
trimLayout.addStretch(3)
icdScalingLayout = QtWidgets.QHBoxLayout()
icdScalingLayout.setSpacing(5)
icdScalingLayout.addWidget(QtWidgets.QLabel("Resize ICD-ready image to:"))
icdScalingLayout.addWidget(self.icdScaling)
self.icdScaling.setFixedWidth(50)
icdScalingLayout.addWidget(QtWidgets.QLabel("% of original"))
icdScalingLayout.addStretch(3)
rulerLayout = QtWidgets.QHBoxLayout()
self.rulerCombo = QtWidgets.QComboBox()
self.rulerCombo.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
rulerLayout.addWidget(QtWidgets.QLabel("Add Ruler:"))
rulerLayout.addWidget(self.rulerCombo)
settingsGroupLayout.addLayout(dpiLayout)
settingsGroupLayout.addLayout(trimLayout)
settingsGroupLayout.addLayout(icdScalingLayout)
settingsGroupLayout.addLayout(rulerLayout)
settingsGroupLayout.addSpacing(15)
settingsGroupLayout.addWidget(self.saveDefaultsButton)
settingsGroupBox.setLayout(settingsGroupLayout)
vlayout.addSpacing(10)
vlayout.addWidget(settingsGroupBox)
self.convertButton = QtWidgets.QPushButton("Convert Images")
self.convertButton.clicked.connect(self.processImageFiles)
self.convertButton.setAutoDefault(False)
# Button appearance is odd in QStackedLayout without adding to ButtonPanel
self.buttonPanel = ButtonPanel(self.convertButton)
self.progressPanel = ProgressPanel(self, "Converting...")
self.stackedLayout = QtWidgets.QStackedLayout()
self.stackedLayout.addWidget(self.buttonPanel)
self.stackedLayout.addWidget(self.progressPanel)
self.stackedLayout.setCurrentIndex(0)
vlayout.addLayout(self.stackedLayout, stretch=0)
def showProgressLayout(self, show):
self.stackedLayout.setCurrentIndex(1 if show else 0)
def initAppPath(self):
try:
self.app_path = common.get_app_path()
except common.InvalidApplicationPathError as iape:
errbox(self, "Invalid Application Path", "Couldn't find application directory, exiting.")
raise iape # re-raise and bail
def initPrefs(self):
prefPath = os.path.join(self.app_path, "romacons_prefs.pk")
self.prefs = Preferences(prefPath)
self.installPrefs()
def installRulers(self):
rulersPath = os.path.join(self.app_path, "rulers")
try:
common.mkdir_if_needed(rulersPath)
except:
err = sys.exc_info()
errbox(self, "Process failed", "{}".format("Failed to create {}.\nUnhandled error {}: {}".format(rulersPath, err[0], err[1])))
logging.error(traceback.format_exc())
rulerFiles = [f for f in os.listdir(rulersPath) if os.path.isfile(os.path.join(rulersPath, f))
and os.path.basename(f)[0] != '.'] # no hidden files
if len(rulerFiles) == 0:
errbox(self, message="No ruler files were found. Add one or more ruler files to the rulers folder and restart.")
else:
self.rulerCombo.addItems(rulerFiles)
def installPrefs(self):
geom = self.prefs.get("windowGeometry", None)
if geom is not None:
self.setGeometry(geom)
self.dpi.setText(self.prefs.get("dpi", "508"))
self.trim.setText(self.prefs.get("trim", "0.25"))
self.icdScaling.setText(self.prefs.get("icdScaling", "30"))
ruler = self.prefs.get("ruler", "")
rulerIdx = self.rulerCombo.findText(ruler)
self.rulerCombo.setCurrentIndex(rulerIdx if rulerIdx >= 0 else 0)
def savePrefs(self):
self.prefs.set("windowGeometry", self.geometry())
self.prefs.write()
def saveDefaultSettings(self):
self.prefs.set("dpi", self.dpi.text())
self.prefs.set("trim", self.trim.text())
self.prefs.set("icdScaling", self.icdScaling.text())
self.prefs.set("ruler", self.rulerCombo.currentText())
settingsItems = [
f"Resolution: {self.dpi.text()} DPI",
f"Trim: {self.trim.text()} inches",
f"ICD Scaling: {self.icdScaling.text()}%",
f"Ruler: {self.rulerCombo.currentText()}",
]
settingsStr = "Saved Default Settings.\n\n" + '\n'.join(settingsItems)
infobox(self, 'Saved Default Settings', settingsStr)
# override QWidget.closeEvent()
def closeEvent(self, event):
self.savePrefs()
event.accept() # allow window to close - event.ignore() to veto close
def getRulerPath(self):
return os.path.join(self.app_path, "rulers", str(self.rulerCombo.currentText()))
# Strip rotation suffix (-rot[digit] or _rot[digit]) from image filenames.
# If all stripped names match, return as output filename.
# Otherwise, prompt user for output filename. If none is given, throw an error.
def getOutputFilename(self, imgFiles):
stripped_filenames = []
for f in imgFiles:
base = os.path.basename(f)
base, _ = os.path.splitext(base)
# remove rotation suffix from base name
base_no_suffix = re.sub(r'[-_]rot\d', '', base)
stripped_filenames.append(base_no_suffix)
if len(set(stripped_filenames)) != 1:
text, confirmed = inputbox(self, "Enter Filename", "Enter a name for the merged image file:", stripped_filenames[0])
if not confirmed or len(text) == 0:
raise UnmatchedFilenamesError(f"Filenames: {stripped_filenames}")
else:
return text
else:
print(f"Good base filename: {stripped_filenames[0]}")
return stripped_filenames[0]
def processImageFiles(self):
imgFiles = self.imageList.getFiles()
if len(imgFiles) == 0:
infobox(self, "No Images", "Add at least one image to be converted.")
return
success = False
self.showProgressLayout(True)
self.progressPanel.clear()
romacons.setProgressListener(self.progressPanel)
try:
dpi = float(self.dpi.text())
if dpi <= 0:
errbox(self, "Invalid DPI", "DPI must be greater than zero.")
return
trimTxt = self.trim.text()
trim = float(trimTxt if len(trimTxt) > 0 else 0)
if trim < 0:
errbox(self, "Invalid trim length", "Trim length cannot be negative.")
return
icdScaling = float(self.icdScaling.text())
if icdScaling <= 0:
errbox(self, "Invalid ICD Scaling", "ICD scaling % must be greater than zero.")
return
outputBaseName = self.getOutputFilename(imgFiles)
romacons.prepare_romacons(imgFiles, self.getRulerPath(), dpi, trim, icdScaling, outputBaseName, self.app_path)
success = True
except common.RulerTooShortError as e:
errbox(self, "Ruler Too Short", "{}".format(e.message))
except common.UnexpectedColorDepthError as e:
errbox(self, "Unexpected Color Depth", "{}".format(e.message))
except ValueError: # raised by float()
errbox(self, "Expected Numeric Input", "Invalid DPI, trim, or ICD Scaling value, all of which must be numeric.")
except UnmatchedFilenamesError as e:
errbox(self, "Unmatched Filenames", f"Base filenames (with '_rot#' removed) must match.\n{e.message}")
except:
err = sys.exc_info()
errbox(self, "Process failed", "{}".format("Unhandled error {}: {}".format(err[0], err[1])))
logging.error(traceback.format_exc())
finally:
self.showProgressLayout(False)
self.progressPanel.clear()
if success:
infobox(self, "Yay!", f"Successfully merged {len(imgFiles)} rotated image files.\n\nSaved {outputBaseName} as TIFF, JPEG, and ICD JPEG.")
self.imageList.clear()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow(app)
window.setModal(False)
window.show()
sys.exit(app.exec_())