đ„Ÿđ Liste des traces GPX issus d'un GPS (python desktop tableauqt.py)
Dossier travail: $HOME/media/dplus/cartographie/analyse-gpx/
Prérequis: uv (Archlinux, yay -S uv)
Tableau
Description
Le script tableauqt.py lance une petite application Qt qui charge un fichier JSON contenant des traces GPX, puis affiche ces données dans une table avec :
- recherche (filtre texte sur toutes les colonnes)
- tri par colonne (avec tri spécial sur
jouren date) - affichage/masquage de colonnes âcachablesâ (
id/latitude/longitudeetgpx) - thĂšme clair/sombre
- copie des GPX sélectionnés vers un dossier temporaire
- sauvegarde des prĂ©fĂ©rences dâUI (colonnes visibles, tri, thĂšme) dans un fichier de config JSON.
- Drag n drop
Fonctionnement concret
- Au démarrage, il lit un fichier
config.json(ou celui passé via--config) et en extrait :json_path(le JSON à afficher),gpx_dir(dossier contenant les.gpx),- éventuellement
temp_dir,visible_columns,default_sort,window,theme.
- Il charge
tracestableau.jsonen mémoire (self.data) puis affiche toutes les lignes. - Chaque ligne correspond à un élément avec les colonnes :
id, latitude, longitude, lieu, commune, cp, distance, vitesse, niveau, jour, gpx. - La barre âRechercheâ filtre la table en gardant les lignes oĂč le texte recherchĂ© apparaĂźt dans nâimporte quelle colonne.
- Cliquer sur un en-tĂȘte trie la table, et il met une flĂšche
â/âsur lâen-tĂȘte de la colonne active. - Les boutons âToggle id/lat/lonâ et âToggle gpxâ masquent/affichent ces colonnes et mettent Ă jour la vue + la config.
- âCopier GPX sĂ©lectionâ :
- récupÚre
gpxpour les lignes sélectionnées, - copie les fichiers correspondants depuis
GPX_DIRversTEMP_DIR(avecshutil.copy2), - affiche la liste copiée dans la console.
- récupÚre
- Le bouton âAfficher sĂ©lection (id)â affiche juste les
iddes lignes sĂ©lectionnĂ©es dans la console. geo_cache/gĂ©ocodage nâest pas concernĂ© ici : il travaille uniquement sur le JSON dĂ©jĂ produit.
ParamĂštres config.json
Fichier ~/.config/tableau/config.json
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
{
"default_sort": {
"column": "jour",
"descending": true
},
"json_path": "/sharenfs/traces-gpx/tracestableau.json",
"gpx_dir": "/sharenfs/traces-gpx",
"temp_dir": "/tmp/gpx-studio-drop",
"visible_columns": [
"lieu",
"commune",
"cp",
"distance",
"vitesse",
"niveau",
"jour",
"gpx"
],
"window": {
"width": 1200,
"height": 700,
"icon_path": "/home/yann/.config/tableau/tableau-gpx.png"
},
"theme": {
"mode": "dark"
}
}
Script tableauqt.py
Qt utilise le protocole DnD natif (Wayland/XDG ou X11), donc le drag sortant de fichiers GPX vers Dolphin, GPX Studio ou autre fonctionne directement.
~/media/dplus/cartographie/analyse-gpx/tableauqt.py
Ce que fait le port :
QTableView+QAbstractTableModel: le drag sortant est implĂ©mentĂ© viamimeData()du modĂšle, qui retourne les fichiers GPX des lignes sĂ©lectionnĂ©es entext/uri-list(le standard que toutes les applications comprennent).- Tri par clic dâen-tĂȘte via
QSortFilterProxyModel(avec lâindicateur âČ/⌠intĂ©grĂ©), recherche instantanĂ©e sur toutes les colonnes, tout comme votre version. - Les mĂȘmes fonctionnalitĂ©s : affichage sĂ©lection uniquement, toggle des colonnes id/lat/lon/gpx, copie des GPX vers
/tmp/gpx-studio-drop, thĂšme clair/sombre (via QSS), icĂŽne et dimensions de fenĂȘtre. - La persistance dans
config.jsonest conservĂ©e (colonnes visibles, tri, thĂšme, taille de fenĂȘtre), avec le mĂȘme format de clĂ©s : votreconfig.jsonactuel fonctionne tel quel.
Ajout importation et intégration fichier GPX
Choix par rapport Ă ton script dâimport :
- Géocodage dans un thread (
QThread) : Nominatim impose environ 1 requĂȘte par seconde, lâinterface ne se fige donc pas. Une barre de progression avec bouton Annuler sâaffiche pendant lâimport. - JSON relu et réécrit proprement (
json.dump, Ă©criture atomique) au lieu de la bidouille sur le]final. Les nouveauxidpartent demax(id) + 1. - Doublons ignorĂ©s dâaprĂšs le nom du fichier GPX, comme dans ton script. Le fichier est copiĂ© dans
gpx_dir. geo_cache.jsonest stockĂ© Ă cĂŽtĂ© detracestableau.json. Un Ă©chec de gĂ©ocodage nâest pas mis en cache, donc il sera retentĂ© la fois suivante.- Fichiers sources conservĂ©s : ton script les supprimait aprĂšs import, je ne lâai pas reproduit par prudence.
- Fichier
addressPointsJS : il nâest rĂ©gĂ©nĂ©rĂ© que si tu ajoutes"js_path": "..."dansconfig.json.
Dépendances : pip install gpxpy geopy
1
uv pip install gpxpy geopy
Le fichier modifié tableauqt.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#!/usr/bin/env python3
# tableauqt.py - port PySide6 de tableau.py (tkinter -> Qt)
#
# Installation :
# pip install PySide6
#
# Lancement :
# python tableauqt.py --config config.json
#
# Le drag & drop sortant (fichiers GPX vers une autre appli) utilise
# QDrag + QMimeData avec des URLs de fichiers : c'est la méthode native
# Qt, bien plus fiable que TkinterDnD2 sous KDE Plasma / Wayland.
import time
import gpxpy
from geopy.geocoders import Nominatim
from PySide6.QtCore import QThread, Signal
from PySide6.QtWidgets import QProgressDialog
import argparse
import json
import shutil
import sys
from datetime import datetime
from pathlib import Path
from PySide6.QtCore import (
QAbstractTableModel, QMimeData, QModelIndex, QSortFilterProxyModel,
QUrl, Qt,
)
from PySide6.QtGui import QAction, QDrag, QIcon
from PySide6.QtWidgets import (
QApplication, QFileDialog, QHBoxLayout, QHeaderView, QLineEdit,
QMainWindow, QMessageBox, QPushButton, QStatusBar, QTableView,
)
# --- config par défaut (écrasées par le fichier config.json) ---
JSON_PATH = "/home/yann/media/dplus/cartographie/analyse-gpx/traces/tracestableau.json"
GPX_DIR = Path("/home/yann/media/dplus/cartographie/analyse-gpx/traces")
TEMP_DIR = "/tmp/gpx-studio-drop"
JS_PATH = None # optionnel : fichier "var addressPoints = [...]"
COLUMNS = ["id", "latitude", "longitude", "lieu", "commune", "cp",
"distance", "vitesse", "niveau", "jour", "gpx"]
HIDEABLE_COLS = {"id", "latitude", "longitude", "gpx"}
DEFAULT_VISIBLE = {"lieu", "commune", "cp", "distance", "vitesse", "niveau", "jour", "gpx"}
COL_WIDTHS = {"lieu": 220, "gpx": 220, "commune": 170, "jour": 95,
"id": 90, "latitude": 90, "longitude": 90}
def load_config(path):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def save_config(path, cfg):
with open(path, "w", encoding="utf-8") as f:
json.dump(cfg, f, ensure_ascii=False, indent=2)
# --- modĂšle ---
class TraceTableModel(QAbstractTableModel):
"""ModĂšle plat sur la liste de dicts du JSON."""
def __init__(self, rows, parent=None):
super().__init__(parent)
self.rows = rows
# Qt API ------------------------------------------------------------
def rowCount(self, parent=QModelIndex()):
return 0 if parent.isValid() else len(self.rows)
def columnCount(self, parent=QModelIndex()):
return len(COLUMNS)
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return None
row = self.rows[index.row()]
col = COLUMNS[index.column()]
if role in (Qt.DisplayRole, Qt.EditRole):
v = row.get(col, "")
return "" if v is None else str(v)
if role == Qt.UserRole:
return row
return None
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return COLUMNS[section]
return None
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsDragEnabled
def mimeTypes(self):
return ["text/uri-list"]
def mimeData(self, indexes):
"""Drag sortant : les fichiers GPX des lignes sélectionnées."""
mime = QMimeData()
urls, seen = [], set()
for idx in indexes:
row = self.rows[idx.row()]
name = row.get("gpx", "")
if not name:
continue
p = GPX_DIR / str(name)
if p.is_file() and p not in seen:
seen.add(p)
urls.append(QUrl.fromLocalFile(str(p)))
if not urls:
return None
mime.setUrls(urls)
return mime
# helpers ------------------------------------------------------------
def row_at(self, proxy_index, proxy_model):
src_index = proxy_model.mapToSource(proxy_index)
return self.rows[src_index.row()]
# --- import GPX ---
def _fmt(v, nd=2):
return f"{v:.{nd}f}"
def _to_float(v):
try:
return float(v)
except (TypeError, ValueError):
return None
def _write_json_atomic(path, rows):
tmp = Path(str(path) + ".tmp")
with open(tmp, "w", encoding="utf-8") as f:
json.dump(rows, f, ensure_ascii=False, indent=1)
f.write("\n")
tmp.replace(path)
def _write_js(path, rows):
with open(path, "w", encoding="utf-8") as f:
f.write("var addressPoints = [\n")
for r in rows:
arr = [_to_float(r.get("latitude")), _to_float(r.get("longitude")),
r.get("lieu", ""), r.get("commune", ""), r.get("cp", ""),
r.get("distance", ""), r.get("vitesse", ""),
r.get("niveau", ""), r.get("jour", ""), r.get("gpx", ""),
_to_int(r.get("id"))]
f.write(json.dumps(arr, ensure_ascii=False) + ",\n")
f.write("];\n")
def _log(msg):
print(f"[import] {msg}", file=sys.stderr, flush=True)
class ImportWorker(QThread):
"""Parse les GPX + géocodage inverse (lent) hors du thread GUI."""
progress = Signal(int, str) # index, nom du fichier en cours
step = Signal(int) # nombre de fichiers terminés
done = Signal(list, list, list) # [(entry, Path)], erreurs, ignorés
def __init__(self, files, existing_names, start_id, cache_path, parent=None):
super().__init__(parent)
self.files = [Path(f) for f in files]
self.existing = set(existing_names)
self.start_id = start_id
self.cache_path = Path(cache_path)
def run(self):
try:
self._run()
except Exception:
import traceback
traceback.print_exc()
self.done.emit([], ["erreur interne du worker (voir le terminal)"], [])
def _run(self):
_log(f"démarrage : {len(self.files)} fichier(s)")
cache = {}
if self.cache_path.exists():
try:
cache = json.loads(self.cache_path.read_text(encoding="utf-8"))
except Exception:
cache = {}
self.geolocator = Nominatim(user_agent="tableauqt-gpx-import", timeout=10)
entries, errors, skipped = [], [], []
next_id = self.start_id
for i, path in enumerate(self.files):
if self.isInterruptionRequested():
break
name = path.name
self.progress.emit(i, name)
_log(f"{i + 1}/{len(self.files)} : {name}")
if name in self.existing:
skipped.append(name)
self.step.emit(i + 1)
continue
try:
entry = self._build_entry(path, next_id, cache)
except Exception as e:
_log(f" erreur : {e!r}")
errors.append(f"{name} : {e}")
self.step.emit(i + 1)
continue
entries.append((entry, path))
self.existing.add(name)
next_id += 1
self.step.emit(i + 1)
try:
self.cache_path.write_text(
json.dumps(cache, ensure_ascii=False, indent=2), encoding="utf-8")
except Exception:
pass
self.done.emit(entries, errors, skipped)
# ------------------------------------------------------------------
def _reverse(self, lat, lon, cache):
key = f"{round(lat, 6)},{round(lon, 6)}"
if cache.get(key):
return cache[key]
last = None
for attempt in range(3):
if self.isInterruptionRequested():
raise RuntimeError("annulé")
try:
_log(f" géocodage {key} (essai {attempt + 1})")
loc = self.geolocator.reverse(f"{lat},{lon}")
if loc and getattr(loc, "address", None):
cache[key] = loc.address
time.sleep(1.1) # politique d'usage Nominatim : 1 req/s
return loc.address
break
except Exception as e:
last = e
_log(f" échec : {e!r}")
time.sleep(2)
raise RuntimeError(f"géocodage impossible ({last or 'aucune adresse'})")
def _build_entry(self, path, new_id, cache):
with open(path, "r", encoding="utf-8") as f:
gpx = gpxpy.parse(f)
points = []
for trk in gpx.tracks:
for seg in trk.segments:
points.extend(seg.points)
_log(f" gpx parsé : {len(points)} points")
if not points:
raise ValueError("aucun point de trace")
lat, lon = points[0].latitude, points[0].longitude
parts = [p.strip() for p in self._reverse(lat, lon, cache).split(",")]
z = len(parts)
def part(idx):
return parts[idx] if 0 <= idx < z else ""
try:
length_2d = gpx.length_2d() or 0.0
except Exception:
length_2d = 0.0
try:
md = gpx.get_moving_data()
moving_time, moving_distance = md.moving_time, md.moving_distance
except Exception:
moving_time = moving_distance = 0.0
try:
uphill, _down = gpx.get_uphill_downhill()
except Exception:
uphill = 0.0
try:
start_time, _end = gpx.get_time_bounds()
except Exception:
start_time = None
vmoy = (moving_distance / moving_time) * 3.6 if moving_time else 0.0
return {
"id": new_id,
"latitude": lat,
"longitude": lon,
"lieu": part(z - 8),
"commune": part(z - 7),
"cp": part(z - 2),
"distance": _fmt(length_2d / 1000.0, 3),
"vitesse": _fmt(vmoy),
"niveau": _fmt(uphill or 0.0),
"jour": start_time.strftime("%Y-%m-%d") if start_time else "n/a",
"gpx": path.name,
}
# --- fenĂȘtre principale ---
class MainWindow(QMainWindow):
def __init__(self, json_path, visible_columns, default_sort,
config_path=None, window_cfg=None, theme_cfg=None):
super().__init__()
self.config_path = config_path
self.window_cfg = window_cfg or {}
self.theme_mode = (theme_cfg or {}).get("mode", "light")
self.show_only_selection = False
self.selected_ids = set()
self.sort_col = default_sort["column"]
self.sort_desc = bool(default_sort.get("descending", True))
self.visible_cols = set(visible_columns)
with open(json_path, "r", encoding="utf-8") as f:
self.data = json.load(f)
self.filtered = list(self.data)
self.model = TraceTableModel(self.filtered)
self.proxy = QSortFilterProxyModel(self)
self.proxy.setSourceModel(self.model)
self.proxy.setFilterCaseSensitivity(Qt.CaseInsensitive)
self.proxy.setFilterKeyColumn(-1) # toutes les colonnes
self.setWindowTitle("Table JSON - tri + recherche (Qt)")
w = int(self.window_cfg.get("width", 1200))
h = int(self.window_cfg.get("height", 700))
self.resize(w, h)
self._apply_window_icon()
# --- UI ---
top = self._top_bar()
self.table = QTableView()
self.table.setModel(self.proxy)
self.table.setSelectionMode(QTableView.ExtendedSelection)
self.table.setSelectionBehavior(QTableView.SelectRows)
self.table.setDragEnabled(True)
self.table.setDragDropMode(QTableView.DragOnly)
self.table.setSortingEnabled(True)
self.table.verticalHeader().setVisible(False)
self.table.setAlternatingRowColors(True)
self.table.setShowGrid(False)
header = self.table.horizontalHeader()
header.setSectionResizeMode(QHeaderView.Interactive)
header.sortIndicatorChanged.connect(self._on_sort_changed)
self._apply_column_layout()
bottom = self._bottom_bar()
central = self._central_widget(top, self.table, bottom)
self.setCentralWidget(central)
self.setStatusBar(QStatusBar())
# tri initial
col = COLUMNS.index(self.sort_col) if self.sort_col in COLUMNS else 0
self.table.sortByColumn(
col, Qt.DescendingOrder if self.sort_desc else Qt.AscendingOrder)
self.apply_theme()
# --- construction UI ---
def _central_widget(self, top, table, bottom):
from PySide6.QtWidgets import QWidget, QVBoxLayout
w = QWidget()
lay = QVBoxLayout(w)
lay.setContentsMargins(10, 8, 10, 8)
lay.addWidget(top)
lay.addWidget(table, 1)
lay.addWidget(bottom)
return w
def _top_bar(self):
from PySide6.QtWidgets import QWidget, QLabel
w = QWidget()
lay = QHBoxLayout(w)
lay.setContentsMargins(0, 0, 0, 0)
lay.addWidget(QLabel("Recherche :"))
self.search_edit = QLineEdit()
self.search_edit.setClearButtonEnabled(True)
self.search_edit.textChanged.connect(self.apply_filter)
lay.addWidget(self.search_edit, 1)
return w
def _bottom_bar(self):
from PySide6.QtWidgets import QWidget
w = QWidget()
lay = QHBoxLayout(w)
lay.setContentsMargins(0, 0, 0, 0)
self.btn_selection = QPushButton("Afficher sélection (id)")
self.btn_selection.clicked.connect(self.toggle_selection_view)
lay.addWidget(self.btn_selection)
self.btn_theme = QPushButton("ThĂšme: clair/sombre")
self.btn_theme.clicked.connect(self.toggle_theme)
lay.addWidget(self.btn_theme)
btn_idlatlon = QPushButton("Toggle id/lat/lon")
btn_idlatlon.clicked.connect(
lambda: self.toggle_hidden(["id", "latitude", "longitude"]))
lay.addWidget(btn_idlatlon)
btn_gpx = QPushButton("Toggle gpx")
btn_gpx.clicked.connect(lambda: self.toggle_hidden(["gpx"]))
lay.addWidget(btn_gpx)
btn_copy = QPushButton("Copier GPX sélection")
btn_copy.clicked.connect(self.copy_selected_gpx_to_temp)
lay.addWidget(btn_copy)
self.btn_import = QPushButton("Importation")
self.btn_import.clicked.connect(self.import_gpx)
lay.addWidget(self.btn_import)
lay.addStretch(1)
return w
def _apply_window_icon(self):
icon_file = self.window_cfg.get("icon_path")
if not icon_file:
return
p = Path(icon_file).expanduser().resolve()
if p.exists():
self.setWindowIcon(QIcon(str(p)))
# --- colonnes ---
def _apply_column_layout(self):
for i, c in enumerate(COLUMNS):
if c in HIDEABLE_COLS and c not in self.visible_cols:
self.table.hideColumn(i)
else:
self.table.showColumn(i)
self.table.setColumnWidth(i, COL_WIDTHS.get(c, 80))
def toggle_hidden(self, which):
any_visible = any(c in self.visible_cols for c in which)
if any_visible:
self.visible_cols -= set(which)
else:
self.visible_cols |= set(which)
self._apply_column_layout()
self.save_ui_state()
# --- tri / filtre ---
def _on_sort_changed(self, column, order):
self.sort_col = COLUMNS[column]
self.sort_desc = (order == Qt.DescendingOrder)
self.save_ui_state()
def apply_filter(self):
q = self.search_edit.text().strip()
if self.show_only_selection:
# repasse en vue complÚte puis réapplique la sélection
self.toggle_selection_view()
self.proxy.setFilterFixedString(q)
# --- sélection ---
def _selected_ids(self):
ids = set()
for idx in self.table.selectionModel().selectedRows(0):
src = self.proxy.mapToSource(idx)
row = self.model.rows[src.row()]
try:
ids.add(int(row.get("id")))
except (TypeError, ValueError):
pass
return ids
def toggle_selection_view(self):
if not self.show_only_selection:
self.selected_ids = self._selected_ids()
if not self.selected_ids:
return
self.show_only_selection = True
self.filtered = [r for r in self.data
if _to_int(r.get("id")) in self.selected_ids]
self.btn_selection.setText("Afficher tout")
else:
self.show_only_selection = False
self.filtered = list(self.data)
self.btn_selection.setText("Afficher sélection (id)")
# réapplique le filtre de recherche et le tri courants
self.model.beginResetModel()
self.model.rows = self.filtered
self.model.endResetModel()
self.proxy.setFilterFixedString(self.search_edit.text().strip())
# --- GPX ---
def _selected_gpx_names(self):
names = []
gpx_col = COLUMNS.index("gpx")
for idx in self.table.selectionModel().selectedRows():
src = self.proxy.mapToSource(idx)
name = self.model.rows[src.row()].get("gpx", "")
if name:
names.append(str(name))
return names
def copy_selected_gpx_to_temp(self):
temp_dir = TEMP_DIR
names = self._selected_gpx_names()
if not names:
QMessageBox.information(self, "Copier GPX",
"Aucune ligne sélectionnée (ou colonne gpx vide).")
return
Path(temp_dir).mkdir(parents=True, exist_ok=True)
copied, missing = [], []
for name in names:
src = GPX_DIR / name
if src.is_file():
shutil.copy2(src, Path(temp_dir) / src.name)
copied.append(src.name)
else:
missing.append(str(src))
msg = f"{len(copied)} fichier(s) copié(s) dans :\n{temp_dir}"
if missing:
msg += "\n\nIntrouvables :\n" + "\n".join(missing)
QMessageBox.information(self, "Copier GPX", msg)
# --- importation ---
def import_gpx(self):
start_dir = getattr(self, "_last_import_dir", str(Path.home()))
files, _ = QFileDialog.getOpenFileNames(
self, "Importer des traces GPX", start_dir,
"Traces GPX (*.gpx *.GPX)")
if not files:
return
self._last_import_dir = str(Path(files[0]).parent)
existing = {str(r.get("gpx", "")) for r in self.data}
ids = [i for i in (_to_int(r.get("id")) for r in self.data) if i is not None]
start_id = (max(ids) + 1) if ids else 0
cache_path = Path(JSON_PATH).parent / "geo_cache.json"
# worker créé AVANT le dialogue : s'il échoue, rien ne reste bloqué
self._worker = ImportWorker(files, existing, start_id, cache_path, self)
self.btn_import.setEnabled(False)
self._dlg = QProgressDialog("ImportationâŠ", "Annuler", 0, len(files), self)
self._dlg.setWindowTitle("Importation GPX")
self._dlg.setWindowModality(Qt.WindowModal)
self._dlg.setMinimumDuration(0)
self._dlg.setAutoClose(False)
self._dlg.setAutoReset(False)
self._dlg.setValue(0)
self._worker.progress.connect(self._on_import_progress)
self._worker.step.connect(self._dlg.setValue)
self._worker.done.connect(self._on_import_done)
self._dlg.canceled.connect(self._worker.requestInterruption)
self._worker.start()
def _on_import_progress(self, i, name):
self._dlg.setLabelText(f"({i + 1}/{self._dlg.maximum()}) {name}")
self._dlg.setValue(i)
def _on_import_done(self, entries, errors, skipped):
self._dlg.canceled.disconnect()
self._dlg.close()
self.btn_import.setEnabled(True)
added = 0
Path(GPX_DIR).mkdir(parents=True, exist_ok=True)
for entry, src in entries:
dest = Path(GPX_DIR) / src.name
try:
if not dest.exists() or dest.resolve() != src.resolve():
shutil.copy2(src, dest)
except Exception as e:
errors.append(f"{src.name} : copie impossible ({e})")
continue
self.data.append(entry)
added += 1
if added:
try:
_write_json_atomic(Path(JSON_PATH), self.data)
if JS_PATH:
_write_js(JS_PATH, self.data)
except Exception as e:
errors.append(f"écriture du JSON impossible : {e}")
# retour Ă la vue complĂšte et rafraĂźchissement du modĂšle
self.show_only_selection = False
self.btn_selection.setText("Afficher sélection (id)")
self.filtered = list(self.data)
self.model.beginResetModel()
self.model.rows = self.filtered
self.model.endResetModel()
msg = f"{added} trace(s) importée(s)."
if skipped:
msg += f"\n{len(skipped)} déjà présente(s), ignorée(s)."
if errors:
msg += "\n\nErreurs :\n" + "\n".join(errors)
self.statusBar().showMessage(f"Importation terminée : {added} ajoutée(s)", 8000)
QMessageBox.information(self, "Importation", msg)
# --- thĂšme ---
def apply_theme(self):
is_dark = self.theme_mode == "dark"
app = QApplication.instance()
if is_dark:
app.setStyleSheet(DARK_QSS)
else:
app.setStyleSheet("")
self.btn_theme.setText(f"ThĂšme: {'sombre' if is_dark else 'clair'}")
def toggle_theme(self):
self.theme_mode = "light" if self.theme_mode == "dark" else "dark"
self.apply_theme()
self.save_theme_mode()
# --- persistance ---
def save_ui_state(self):
if not self.config_path:
return
cfg = load_config(self.config_path)
cfg["visible_columns"] = [c for c in COLUMNS if c in self.visible_cols]
cfg["default_sort"] = {"column": self.sort_col,
"descending": bool(self.sort_desc)}
cfg["window"] = {"width": self.width(), "height": self.height()}
save_config(self.config_path, cfg)
def save_theme_mode(self):
if not self.config_path:
return
cfg = load_config(self.config_path)
cfg["theme"] = cfg.get("theme", {})
cfg["theme"]["mode"] = self.theme_mode
save_config(self.config_path, cfg)
def closeEvent(self, event):
self.save_ui_state()
super().closeEvent(event)
def _to_int(v):
try:
return int(v)
except (TypeError, ValueError):
return None
DARK_QSS = """
QWidget { background-color: #1e1e1e; color: #eaeaea; }
QTableView {
background-color: #2b2b2b; color: #eaeaea;
gridline-color: #3c3c3c; alternate-background-color: #303030;
selection-background-color: #094771; selection-color: #ffffff;
}
QHeaderView::section {
background-color: #303030; color: #eaeaea;
border: 1px solid #3c3c3c; padding: 4px;
}
QLineEdit, QPushButton {
background-color: #2b2b2b; border: 1px solid #3c3c3c;
border-radius: 4px; padding: 4px 8px;
}
QPushButton:hover { background-color: #3a3a3a; }
QStatusBar { background-color: #1e1e1e; }
"""
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--config", default="config.json",
help="chemin du fichier config")
args = parser.parse_args()
cfg = load_config(args.config)
global JSON_PATH, GPX_DIR, TEMP_DIR, JS_PATH
JSON_PATH = cfg["json_path"]
GPX_DIR = Path(cfg["gpx_dir"])
TEMP_DIR = cfg.get("temp_dir", "/tmp/gpx-studio-drop")
JS_PATH = cfg.get("js_path")
visible_columns = cfg.get("visible_columns", list(DEFAULT_VISIBLE))
default_sort = cfg.get("default_sort", {"column": "jour", "descending": True})
window_cfg = cfg.get("window", {})
theme_cfg = cfg.get("theme", {})
app = QApplication(sys.argv)
win = MainWindow(JSON_PATH, visible_columns, default_sort,
config_path=args.config, window_cfg=window_cfg,
theme_cfg=theme_cfg)
win.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
Parfait, lâimport est donc opĂ©rationnel : sĂ©lection de plusieurs .gpx, gĂ©ocodage hors du thread de lâinterface, ajout au JSON et copie dans gpx_dir.
Deux idées si tu veux aller plus loin :
- Suppression des sources aprĂšs import, comme dans ton script dâorigine, avec une case Ă cocher dans le dialogue ou une clĂ© dans
config.json. - Glisser-déposer entrant : déposer des
.gpxsur la fenĂȘtre pour lancer lâimport, en rĂ©utilisant le mĂȘmeImportWorker.
uv (recommandé)
Dans le dossier de tableauqt.py :
1
cd ~/media/dplus/cartographie/analyse-gpx # adapter : lĂ oĂč se trouve tableau.py
Environnement
1
2
3
# Environnement et Pyside
uv venv --python /usr/bin/python # Python systĂšme, donc tkinter garanti
uv pip install PySide6
Lancement
1
2
3
4
# lancement
uv run --no-project python tableauqt.py --config config.json
# ou directement :
.venv/bin/python tableauqt.py --config config.json
Une diffĂ©rence notable : sous Qt, le tri en mode âAfficher sĂ©lectionâ est gĂ©rĂ© par le proxy, donc trier en vue filtrĂ©e ne rĂ©ordonne plus vos donnĂ©es sources, ce qui corrige un effet de bord prĂ©sent dans la version tkinter.
Installer tableau
Créer le script ~/.local/bin/tableau :
1
2
3
#!/bin/sh
cd ~/media/dplus/cartographie/analyse-gpx || exit 1
exec .venv/bin/python tableauqt.py --config config.json "$@"
Le rendre exécutable
1
chmod +x ~/.local/bin/tableau
Lanceur KDE desktop ~/.local/share/applications/tableau-gpx.desktop
1
2
3
4
5
6
7
8
9
10
11
[Desktop Entry]
Categories=Utility;
Exec=/home/yann/.local/bin/tableau --config /home/yann/.config/tableau/config.json
Icon=/home/yann/.config/tableau/tableau-gpx.png
Name=Tableau GPX
NoDisplay=false
PrefersNonDefaultGPU=false
StartupNotify=true
Terminal=false
Type=Application
X-KDE-SubstituteUID=false
Remplacer
/home/yannsi autre utilisateur
Structure finale
~/.config/tableau
1
2
3
4
.config/tableau/
âââ config.json
âââ tableau-gpx.ico
âââ tableau-gpx.png
Application tableau: ~/.local/bin/tableau
Lanceur KDE desktop ~/.local/share/applications/tableau-gpx.desktop
