-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayer_dialog.py
More file actions
185 lines (148 loc) · 5.7 KB
/
layer_dialog.py
File metadata and controls
185 lines (148 loc) · 5.7 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
"""
Dialog for HTTP API Plugin
"""
from PyQt5.QtCore import QThread, pyqtSignal, Qt
import json
import urllib.request
import urllib.error
import urllib.request
import urllib.error
from PyQt5.QtWidgets import (
QDialog,
QVBoxLayout,
QHBoxLayout,
QComboBox,
QPushButton,
QLabel,
QMessageBox,
QProgressBar,
)
from PyQt5.QtGui import QFont
class JsonLoaderThread(QThread):
"""Thread for loading JSON data from URL without blocking GUI"""
data_loaded = pyqtSignal(list)
error_occurred = pyqtSignal(str)
def __init__(self, url):
super().__init__()
self.url = url
def run(self):
try:
with urllib.request.urlopen(self.url, timeout=10) as response:
url_data = response.read().decode("utf-8")
data = json.loads(url_data)
self.data_loaded.emit(data)
except urllib.error.URLError as e:
self.error_occurred.emit(f"Network error: {str(e)}")
except json.JSONDecodeError as e:
self.error_occurred.emit(f"JSON parsing error: {str(e)}")
except Exception as e:
self.error_occurred.emit(f"Unexpected error: {str(e)}")
class LayerDropdownDialog(QDialog):
"""Main dialog window for the QGIS plugin"""
def __init__(self, parent=None):
super().__init__(parent)
self.json_data = None
self.setup_ui()
self.load_json_data()
def setup_ui(self):
"""Initialize the user interface"""
self.setWindowTitle("BornHack Layer Selector")
self.setMinimumSize(400, 200)
# Main layout
layout = QVBoxLayout()
# Title label
title = QLabel("Select a layer.")
title.setFont(QFont("Arial", 12, QFont.Bold))
title.setAlignment(Qt.AlignCenter)
layout.addWidget(title)
# Progress bar
self.progress_bar = QProgressBar()
self.progress_bar.setVisible(False)
layout.addWidget(self.progress_bar)
# Dropdown for JSON options
dropdown_layout = QHBoxLayout()
self.dropdown_label = QLabel("Select layer:")
self.dropdown = QComboBox()
self.dropdown.setEnabled(False)
self.dropdown.currentTextChanged.connect(self.on_selection_changed)
dropdown_layout.addWidget(self.dropdown_label)
dropdown_layout.addWidget(self.dropdown)
layout.addLayout(dropdown_layout)
# Selected value display
self.selected_label = QLabel("Selected: None")
self.selected_label.setStyleSheet("QLabel { color: blue; font-weight: bold; }")
layout.addWidget(self.selected_label)
# Action buttons
button_layout = QHBoxLayout()
self.ok_btn = QPushButton("OK")
self.cancel_btn = QPushButton("Cancel")
self.ok_btn.setEnabled(False)
self.ok_btn.clicked.connect(self.accept)
self.cancel_btn.clicked.connect(self.reject)
button_layout.addStretch()
button_layout.addWidget(self.ok_btn)
button_layout.addWidget(self.cancel_btn)
layout.addLayout(button_layout)
self.setLayout(layout)
def load_json_data(self):
"""Load JSON data."""
url = "https://bornhack.dk/maps/layers/"
# url = "http://localhost:8000/maps/layers/"
# Show progress bar
self.progress_bar.setVisible(True)
self.progress_bar.setRange(0, 0) # Indeterminate progress
# Start loading thread
self.loader_thread = JsonLoaderThread(url)
self.loader_thread.data_loaded.connect(self.on_data_loaded)
self.loader_thread.error_occurred.connect(self.on_error)
self.loader_thread.start()
def on_data_loaded(self, data):
"""Handle successful JSON data loading"""
self.json_data = data
self.populate_dropdown()
# Hide progress bar and re-enable controls
self.progress_bar.setVisible(False)
self.dropdown.setEnabled(True)
def on_error(self, error_msg):
"""Handle errors during JSON loading"""
self.progress_bar.setVisible(False)
QMessageBox.critical(
self, "Error Loading Data", f"Failed to load JSON data:\n{error_msg}"
)
def populate_dropdown(self):
"""Populate dropdown with JSON data"""
self.dropdown.clear()
if not self.json_data:
return
if isinstance(self.json_data, list):
for i, item in enumerate(self.json_data):
display_text = f"{item['camp']} - {item['team']} - {item['name']}"
self.dropdown.addItem(display_text)
else:
QMessageBox.critical(self, "Error Loading Data", "Failed to load JSON data")
def on_selection_changed(self, text):
"""Handle dropdown selection changes"""
if text:
self.selected_label.setText(f"Selected: {text}")
self.ok_btn.setEnabled(True)
else:
self.selected_label.setText("Selected: None")
self.ok_btn.setEnabled(False)
def get_selected_data(self):
"""Return the selected item's full data"""
if not self.json_data or self.dropdown.currentIndex() == -1:
return None
current_index = self.dropdown.currentIndex()
if isinstance(self.json_data, list):
return (
self.json_data[current_index]
if current_index < len(self.json_data)
else None
)
elif isinstance(self.json_data, dict):
keys = list(self.json_data.keys())
selected_key = keys[current_index] if current_index < len(keys) else None
return (
{selected_key: self.json_data[selected_key]} if selected_key else None
)
return None