-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeojson_dialog.py
More file actions
93 lines (74 loc) · 2.82 KB
/
geojson_dialog.py
File metadata and controls
93 lines (74 loc) · 2.82 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
from qgis.PyQt.QtGui import QFont
from qgis.PyQt.QtWidgets import (
QMessageBox,
QFileDialog,
QDialog,
QVBoxLayout,
QTextEdit,
QPushButton,
QHBoxLayout,
QLabel,
QApplication,
)
class GeoJSONDialog(QDialog):
"""Dialog to display GeoJSON content for copy-pasting."""
def __init__(self, geojson_content, layer_name, parent=None):
super().__init__(parent)
self.setWindowTitle(f"GeoJSON Export - {layer_name}")
self.setModal(True)
self.resize(800, 600)
# Create layout
layout = QVBoxLayout()
# Add info label
info_label = QLabel(f"GeoJSON content for layer: <b>{layer_name}</b>")
layout.addWidget(info_label)
# Create text area
self.text_edit = QTextEdit()
self.text_edit.setPlainText(geojson_content)
self.text_edit.setReadOnly(True)
# Set monospace font for better JSON viewing
font = QFont("Courier New", 10)
font.setFixedPitch(True)
self.text_edit.setFont(font)
layout.addWidget(self.text_edit)
# Create buttons
button_layout = QHBoxLayout()
copy_button = QPushButton("Copy to Clipboard")
copy_button.clicked.connect(self.copy_to_clipboard)
save_button = QPushButton("Save to File")
save_button.clicked.connect(self.save_to_file)
close_button = QPushButton("Close")
close_button.clicked.connect(self.close)
button_layout.addWidget(copy_button)
button_layout.addWidget(save_button)
button_layout.addStretch()
button_layout.addWidget(close_button)
layout.addLayout(button_layout)
self.setLayout(layout)
# Store content for saving
self.geojson_content = geojson_content
self.layer_name = layer_name
def copy_to_clipboard(self):
"""Copy GeoJSON content to clipboard."""
clipboard = QApplication.clipboard()
clipboard.setText(self.geojson_content)
QMessageBox.information(self, "Copied", "GeoJSON content copied to clipboard!")
def save_to_file(self):
"""Save GeoJSON content to file."""
filename, _ = QFileDialog.getSaveFileName(
self,
"Save GeoJSON file",
f"{self.layer_name}.geojson",
"GeoJSON files (*.geojson);;All files (*.*)",
)
if filename:
if not filename.lower().endswith(".geojson"):
filename += ".geojson"
try:
with open(filename, "w", encoding="utf-8") as f:
f.write(self.geojson_content)
QMessageBox.information(self, "Saved", f"GeoJSON saved to:\n{filename}")
except Exception as e:
QMessageBox.critical(
self, "Save Error", f"Failed to save file:\n{str(e)}"
)