-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathqged2dot.py
executable file
·277 lines (243 loc) · 10.1 KB
/
qged2dot.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
#!/usr/bin/env python3
#
# Copyright Miklos Vajna
#
# SPDX-License-Identifier: MPL-2.0
#
"""Qt-based GUI for ged2dot."""
import io
import os
import sys
import traceback
import webbrowser
from PyQt6 import QtGui
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWidgets import QCheckBox
from PyQt6.QtWidgets import QComboBox
from PyQt6.QtWidgets import QDialogButtonBox
from PyQt6.QtWidgets import QFileDialog
from PyQt6.QtWidgets import QGridLayout
from PyQt6.QtWidgets import QLabel
from PyQt6.QtWidgets import QLineEdit
from PyQt6.QtWidgets import QMessageBox
from PyQt6.QtWidgets import QPushButton
from PyQt6.QtWidgets import QSpinBox
from PyQt6.QtWidgets import QStatusBar
from PyQt6.QtWidgets import QVBoxLayout
from PyQt6.QtWidgets import QWidget
import pygraphviz # type: ignore
import ged2dot
class Widgets:
"""Contains widgets which store shared state."""
def __init__(self, window: QWidget) -> None:
self.input_value = QLineEdit(window)
self.output_value = QLineEdit(window)
self.rootfamily_value = QComboBox(window)
self.familydepth_value = QSpinBox(window)
self.imagedir_value = QLineEdit(window)
self.nameorder_value = QCheckBox(window)
self.statusbar = QStatusBar()
def set_input(self) -> None:
"""Handler for the input button."""
try:
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.FileMode.ExistingFile) # pylint: disable=no-member
dialog.setNameFilters(["GEDCOM files (*.ged)"])
if not dialog.exec():
return
files = dialog.selectedFiles()
assert len(files) == 1
ged_path = files[0]
self.input_value.setText(ged_path)
import_config = {
'input': ged_path,
}
ged_import = ged2dot.GedcomImport()
graph = ged_import.load(import_config)
self.rootfamily_value.clear()
for node in graph:
if not isinstance(node, ged2dot.Family):
continue
help_string = ""
if node.husb and node.husb.get_surname():
help_string += node.husb.get_surname()
help_string += "-"
if node.wife and node.wife.get_surname():
help_string += node.wife.get_surname()
key = f"{node.get_identifier()} ({help_string})"
self.rootfamily_value.addItem(key, node.get_identifier())
self.update_status()
except Exception: # pylint: disable=broad-except
self.print_traceback()
def set_output(self) -> None:
"""Handler for the output button."""
dialog = QFileDialog()
dialog.setAcceptMode(QFileDialog.AcceptMode.AcceptSave) # pylint: disable=no-member
name_filters = [
"SVG files (*.svg)",
"PNG files (*.png)",
"Graphviz files (*.dot)",
]
dialog.setNameFilters(name_filters)
if not dialog.exec():
return
files = dialog.selectedFiles()
assert len(files) == 1
self.output_value.setText(files[0])
self.update_status()
def set_imagedir(self) -> None:
"""Handler for the imagedir button."""
dialog = QFileDialog()
dialog.setFileMode(QFileDialog.FileMode.Directory) # pylint: disable=no-member
if not dialog.exec():
return
files = dialog.selectedFiles()
assert len(files) == 1
self.imagedir_value.setText(files[0])
def convert(self) -> None:
"""Does the actual conversion."""
try:
config = {
"input": self.input_value.text(),
"output": self.output_value.text(),
"rootfamily": self.rootfamily_value.currentData(),
"familydepth": str(self.familydepth_value.value()),
"imagedir": self.imagedir_value.text(),
"nameorder": "little",
}
if not self.nameorder_value.isChecked():
config["nameorder"] = "big"
invoke_dot = False
if not self.output_value.text().endswith(".dot"):
invoke_dot = True
config["output"] = self.output_value.text() + ".dot"
self.statusbar.showMessage("Converting to " + config["output"] + "...")
ged2dot.convert(config)
if invoke_dot:
self.statusbar.showMessage("Converting to " + self.output_value.text() + "...")
self.to_graphic(config["output"], self.output_value.text())
webbrowser.open("file://" + self.output_value.text())
self.statusbar.showMessage("Conversion finished successfully.")
except Exception: # pylint: disable=broad-except
self.print_traceback()
@staticmethod
def to_graphic(dot_path: str, graphic_path: str) -> None:
"""Convert the generated .dot further to .png/.svg, using dot."""
graph = pygraphviz.AGraph(dot_path)
if graphic_path.endswith(".png"):
graph.draw(graphic_path, format="png", prog="dot")
else:
graph.draw(graphic_path, format="svg", prog="dot")
@staticmethod
def print_traceback() -> None:
"""Shows the exception to the user when it would not be caught."""
msg = QMessageBox()
msg.setIcon(QMessageBox.Icon.Warning) # pylint: disable=no-member
msg.setText("Conversion failed.")
with io.StringIO() as stream:
traceback.print_exc(file=stream)
stream.seek(0)
msg.setDetailedText(stream.read())
msg.exec()
def update_status(self) -> None:
"""Updates the statusbar depending on what should be the next action."""
if not self.input_value.text():
message = "Select an input."
elif not self.output_value.text():
message = "Select an output."
else:
message = "Press OK to start the conversion."
self.statusbar.showMessage(message)
class Application:
"""Manages shared state of the app."""
def __init__(self) -> None:
self.qt_app = QApplication(sys.argv)
self.window = QWidget()
self.layout = QVBoxLayout()
self.grid_layout = QGridLayout()
self.widgets = Widgets(self.window)
def setup_input(self) -> None:
"""Sets up in the input row."""
input_key = QLabel(self.window)
input_key.setText("Input:")
self.grid_layout.addWidget(input_key, 0, 0)
self.grid_layout.addWidget(self.widgets.input_value, 0, 1)
input_button = QPushButton(self.window)
input_button.setText("Browse...")
input_button.clicked.connect(self.widgets.set_input)
self.grid_layout.addWidget(input_button, 0, 2)
def setup_output(self) -> None:
"""Sets up in the output row."""
output_key = QLabel(self.window)
output_key.setText("Output:")
self.grid_layout.addWidget(output_key, 1, 0)
self.grid_layout.addWidget(self.widgets.output_value, 1, 1)
output_button = QPushButton(self.window)
output_button.setText("Browse...")
output_button.clicked.connect(self.widgets.set_output)
self.grid_layout.addWidget(output_button, 1, 2)
def setup_rootfamily(self) -> None:
"""Sets up the root family row."""
rootfamily_key = QLabel(self.window)
rootfamily_key.setText("Root family:")
self.grid_layout.addWidget(rootfamily_key, 2, 0)
self.grid_layout.addWidget(self.widgets.rootfamily_value, 2, 1)
def setup_familydepth(self) -> None:
"""Sets up the familydepth row."""
rootfamily_key = QLabel(self.window)
rootfamily_key.setText("Family depth:")
self.grid_layout.addWidget(rootfamily_key, 3, 0)
self.widgets.familydepth_value.setValue(3)
self.grid_layout.addWidget(self.widgets.familydepth_value, 3, 1)
def setup_imagedir(self) -> None:
"""Sets up the imagedir row."""
imagedir_key = QLabel(self.window)
imagedir_key.setText("Image directory:")
self.grid_layout.addWidget(imagedir_key, 4, 0)
self.grid_layout.addWidget(self.widgets.imagedir_value, 4, 1)
imagedir_button = QPushButton(self.window)
imagedir_button.setText("Browse...")
imagedir_button.clicked.connect(self.widgets.set_imagedir)
self.grid_layout.addWidget(imagedir_button, 4, 2)
def setup_nameorder(self) -> None:
"""Sets up the nameorder row."""
nameorder_key = QLabel(self.window)
nameorder_key.setText("Name order:")
self.grid_layout.addWidget(nameorder_key, 5, 0)
self.widgets.nameorder_value.setText("Given name first")
self.widgets.nameorder_value.setChecked(True)
self.grid_layout.addWidget(self.widgets.nameorder_value, 5, 1)
def exec(self) -> None:
"""Starts the main loop."""
self.window.setWindowTitle("ged2dot")
icon_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "icon.svg")
self.window.setWindowIcon(QtGui.QIcon(icon_path))
self.window.setLayout(self.layout)
self.window.show()
sys.exit(self.qt_app.exec())
def main() -> None:
"""Commandline interface to this module."""
app = Application()
app.setup_input()
app.setup_output()
app.setup_rootfamily()
app.setup_familydepth()
app.setup_imagedir()
app.setup_nameorder()
app.layout.addLayout(app.grid_layout)
button_box = QDialogButtonBox()
standard_buttons = QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel
button_box.setStandardButtons(standard_buttons)
app.layout.addWidget(button_box)
cancel_button = button_box.button(QDialogButtonBox.StandardButton.Cancel)
assert cancel_button
cancel_button.clicked.connect(sys.exit)
ok_button = button_box.button(QDialogButtonBox.StandardButton.Ok)
assert ok_button
ok_button.clicked.connect(app.widgets.convert)
app.layout.addWidget(app.widgets.statusbar)
app.widgets.update_status()
app.exec()
if __name__ == "__main__":
main()
# vim:set shiftwidth=4 softtabstop=4 expandtab: