mirror of
https://github.com/gpxstudio/gpx.studio.git
synced 2026-10-27 02:27:18 +00:00
route full support
This commit is contained in:
1 parent
c842da8dc1
commit
b6c0f5190a
91 files changed
+2523
-847
No files matched your search
@@ -49,7 +49,7 @@ pub fn select_elements<H: History>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Selects all the elements of the same kind as the selected ones, in the same place: all the
|
/// Selects all the elements of the same kind as the selected ones, in the same place: all the
|
||||||
/// files, the tracks of the file, the segments of the track, the waypoints of the file. All the
|
/// files, the routes or the tracks of the file, the segments of the track, the waypoints of the file. All the
|
||||||
/// files when nothing is selected.
|
/// files when nothing is selected.
|
||||||
pub fn select_all<H: History>(engine: &mut Engine<H>) -> Outcome {
|
pub fn select_all<H: History>(engine: &mut Engine<H>) -> Outcome {
|
||||||
execute(engine, Action::SelectAll)
|
execute(engine, Action::SelectAll)
|
||||||
@@ -62,6 +62,19 @@ pub fn arrow_select<H: History>(engine: &mut Engine<H>, down: bool, add: bool) -
|
|||||||
execute(engine, Action::ArrowSelect { down, add })
|
execute(engine, Action::ArrowSelect { down, add })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn select_routes<H: History>(
|
||||||
|
engine: &mut Engine<H>,
|
||||||
|
file_id: Uuid,
|
||||||
|
route_ids: &[Uuid],
|
||||||
|
mode: engine::SelectMode,
|
||||||
|
) -> Outcome {
|
||||||
|
let selection = engine::Selection::Route {
|
||||||
|
file_id: engine::FileId(file_id),
|
||||||
|
rte_ids: route_ids.iter().copied().map(engine::RouteId).collect(),
|
||||||
|
};
|
||||||
|
select_elements(engine, selection, mode)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn select_tracks<H: History>(
|
pub fn select_tracks<H: History>(
|
||||||
engine: &mut Engine<H>,
|
engine: &mut Engine<H>,
|
||||||
file_id: Uuid,
|
file_id: Uuid,
|
||||||
@@ -304,6 +317,10 @@ pub fn style<H: History>(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_route<H: History>(engine: &mut Engine<H>) -> Outcome {
|
||||||
|
edit(engine, Command::NewRoute(engine::NewRoute))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new_track<H: History>(engine: &mut Engine<H>) -> Outcome {
|
pub fn new_track<H: History>(engine: &mut Engine<H>) -> Outcome {
|
||||||
edit(engine, Command::NewTrack(engine::NewTrack))
|
edit(engine, Command::NewTrack(engine::NewTrack))
|
||||||
}
|
}
|
||||||
@@ -575,7 +592,7 @@ pub fn route<H: History>(
|
|||||||
at_revision(engine, revision, |engine| {
|
at_revision(engine, revision, |engine| {
|
||||||
edit(
|
edit(
|
||||||
engine,
|
engine,
|
||||||
Command::Route(engine::Route {
|
Command::Reroute(engine::Reroute {
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
lng,
|
lng,
|
||||||
@@ -618,7 +635,7 @@ pub fn change_loop_start<H: History>(engine: &mut Engine<H>, revision: u32, inde
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cuts the file, the track or the segment (see `SplitType`) of the trackpoint `at` of the
|
/// Cuts the file, the route, the track or the segment (see `SplitType`) of the trackpoint `at` of the
|
||||||
/// selection in two, there: the trackpoint ends the first part and starts the second one. A file that
|
/// selection in two, there: the trackpoint ends the first part and starts the second one. A file that
|
||||||
/// is cut is kept (and its two parts are new files) if the history needs it, see
|
/// is cut is kept (and its two parts are new files) if the history needs it, see
|
||||||
/// [`History::KEEPS_ORIGINALS`].
|
/// [`History::KEEPS_ORIGINALS`].
|
||||||
|
|||||||
+74
-16
@@ -131,6 +131,10 @@ pub enum Selection {
|
|||||||
File {
|
File {
|
||||||
file_ids: Vec<Uuid>,
|
file_ids: Vec<Uuid>,
|
||||||
},
|
},
|
||||||
|
Route {
|
||||||
|
file_id: Uuid,
|
||||||
|
route_ids: Vec<Uuid>,
|
||||||
|
},
|
||||||
Track {
|
Track {
|
||||||
file_id: Uuid,
|
file_id: Uuid,
|
||||||
track_ids: Vec<Uuid>,
|
track_ids: Vec<Uuid>,
|
||||||
@@ -157,6 +161,10 @@ impl From<&engine::Selection> for Selection {
|
|||||||
S::File { file_ids } => Self::File {
|
S::File { file_ids } => Self::File {
|
||||||
file_ids: file_ids.iter().map(|id| id.0).collect(),
|
file_ids: file_ids.iter().map(|id| id.0).collect(),
|
||||||
},
|
},
|
||||||
|
S::Route { file_id, rte_ids } => Self::Route {
|
||||||
|
file_id: file_id.0,
|
||||||
|
route_ids: rte_ids.iter().map(|id| id.0).collect(),
|
||||||
|
},
|
||||||
S::Track { file_id, trk_ids } => Self::Track {
|
S::Track { file_id, trk_ids } => Self::Track {
|
||||||
file_id: file_id.0,
|
file_id: file_id.0,
|
||||||
track_ids: trk_ids.iter().map(|id| id.0).collect(),
|
track_ids: trk_ids.iter().map(|id| id.0).collect(),
|
||||||
@@ -187,6 +195,10 @@ impl From<Selection> for engine::Selection {
|
|||||||
Selection::File { file_ids } => S::File {
|
Selection::File { file_ids } => S::File {
|
||||||
file_ids: file_ids.into_iter().map(FileId).collect(),
|
file_ids: file_ids.into_iter().map(FileId).collect(),
|
||||||
},
|
},
|
||||||
|
Selection::Route { file_id, route_ids } => S::Route {
|
||||||
|
file_id: FileId(file_id),
|
||||||
|
rte_ids: route_ids.into_iter().map(engine::RouteId).collect(),
|
||||||
|
},
|
||||||
Selection::Track { file_id, track_ids } => S::Track {
|
Selection::Track { file_id, track_ids } => S::Track {
|
||||||
file_id: FileId(file_id),
|
file_id: FileId(file_id),
|
||||||
trk_ids: track_ids.into_iter().map(engine::TrackId).collect(),
|
trk_ids: track_ids.into_iter().map(engine::TrackId).collect(),
|
||||||
@@ -229,6 +241,10 @@ pub enum MoveTarget {
|
|||||||
Files {
|
Files {
|
||||||
index: f64,
|
index: f64,
|
||||||
},
|
},
|
||||||
|
Routes {
|
||||||
|
file_id: Uuid,
|
||||||
|
index: f64,
|
||||||
|
},
|
||||||
Tracks {
|
Tracks {
|
||||||
file_id: Uuid,
|
file_id: Uuid,
|
||||||
index: f64,
|
index: f64,
|
||||||
@@ -250,6 +266,10 @@ impl From<MoveTarget> for engine::MoveTarget {
|
|||||||
let index = |index: f64| index.max(0.0) as usize;
|
let index = |index: f64| index.max(0.0) as usize;
|
||||||
match target {
|
match target {
|
||||||
MoveTarget::Files { index: i } => T::Files { index: index(i) },
|
MoveTarget::Files { index: i } => T::Files { index: index(i) },
|
||||||
|
MoveTarget::Routes { file_id, index: i } => T::Routes {
|
||||||
|
file_id: FileId(file_id),
|
||||||
|
index: index(i),
|
||||||
|
},
|
||||||
MoveTarget::Tracks { file_id, index: i } => T::Tracks {
|
MoveTarget::Tracks { file_id, index: i } => T::Tracks {
|
||||||
file_id: FileId(file_id),
|
file_id: FileId(file_id),
|
||||||
index: index(i),
|
index: index(i),
|
||||||
@@ -286,6 +306,9 @@ impl Clipboard {
|
|||||||
engine::ClipboardIds::Files(ids) => {
|
engine::ClipboardIds::Files(ids) => {
|
||||||
("files", strings(ids.into_iter().map(|id| id.0).collect()))
|
("files", strings(ids.into_iter().map(|id| id.0).collect()))
|
||||||
}
|
}
|
||||||
|
engine::ClipboardIds::Routes(ids) => {
|
||||||
|
("routes", strings(ids.into_iter().map(|id| id.0).collect()))
|
||||||
|
}
|
||||||
engine::ClipboardIds::Tracks(ids) => {
|
engine::ClipboardIds::Tracks(ids) => {
|
||||||
("tracks", strings(ids.into_iter().map(|id| id.0).collect()))
|
("tracks", strings(ids.into_iter().map(|id| id.0).collect()))
|
||||||
}
|
}
|
||||||
@@ -315,14 +338,15 @@ pub struct FileStructure {
|
|||||||
name: String,
|
name: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
desc: Option<String>,
|
desc: Option<String>,
|
||||||
|
routes: Vec<RouteNode>,
|
||||||
tracks: Vec<TrackNode>,
|
tracks: Vec<TrackNode>,
|
||||||
waypoints: Vec<WaypointNode>,
|
waypoints: Vec<WaypointNode>,
|
||||||
waypoints_rev: String,
|
waypoints_rev: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The name, description and style of a route or a track.
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct TrackNode {
|
struct PathInfo {
|
||||||
id: String,
|
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
name: Option<String>,
|
name: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
@@ -333,6 +357,33 @@ struct TrackNode {
|
|||||||
opacity: Option<f64>,
|
opacity: Option<f64>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
width: Option<f64>,
|
width: Option<f64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&engine::PathInfo> for PathInfo {
|
||||||
|
fn from(info: &engine::PathInfo) -> Self {
|
||||||
|
Self {
|
||||||
|
name: info.name.clone(),
|
||||||
|
desc: info.desc.clone(),
|
||||||
|
color: info.color.clone(),
|
||||||
|
opacity: info.opacity,
|
||||||
|
width: info.width,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct RouteNode {
|
||||||
|
id: String,
|
||||||
|
#[serde(flatten)]
|
||||||
|
info: PathInfo,
|
||||||
|
segment: SegmentNode,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct TrackNode {
|
||||||
|
id: String,
|
||||||
|
#[serde(flatten)]
|
||||||
|
info: PathInfo,
|
||||||
segments: Vec<SegmentNode>,
|
segments: Vec<SegmentNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,6 +394,16 @@ struct SegmentNode {
|
|||||||
length: f64,
|
length: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<&engine::SegmentNode> for SegmentNode {
|
||||||
|
fn from(seg: &engine::SegmentNode) -> Self {
|
||||||
|
Self {
|
||||||
|
id: seg.id.0.to_string(),
|
||||||
|
rev: seg.rev_id.0.to_string(),
|
||||||
|
length: seg.len as f64,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct WaypointNode {
|
struct WaypointNode {
|
||||||
id: String,
|
id: String,
|
||||||
@@ -358,25 +419,22 @@ impl FileStructure {
|
|||||||
id: file.id.0.to_string(),
|
id: file.id.0.to_string(),
|
||||||
name: file.name.clone(),
|
name: file.name.clone(),
|
||||||
desc: file.desc.clone(),
|
desc: file.desc.clone(),
|
||||||
|
routes: file
|
||||||
|
.routes
|
||||||
|
.iter()
|
||||||
|
.map(|rte| RouteNode {
|
||||||
|
id: rte.id.0.to_string(),
|
||||||
|
info: (&rte.info).into(),
|
||||||
|
segment: (&rte.segment).into(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
tracks: file
|
tracks: file
|
||||||
.tracks
|
.tracks
|
||||||
.iter()
|
.iter()
|
||||||
.map(|trk| TrackNode {
|
.map(|trk| TrackNode {
|
||||||
id: trk.id.0.to_string(),
|
id: trk.id.0.to_string(),
|
||||||
name: trk.name.clone(),
|
info: (&trk.info).into(),
|
||||||
desc: trk.desc.clone(),
|
segments: trk.segments.iter().map(Into::into).collect(),
|
||||||
color: trk.color.clone(),
|
|
||||||
opacity: trk.opacity,
|
|
||||||
width: trk.width,
|
|
||||||
segments: trk
|
|
||||||
.segments
|
|
||||||
.iter()
|
|
||||||
.map(|seg| SegmentNode {
|
|
||||||
id: seg.id.0.to_string(),
|
|
||||||
rev: seg.rev_id.0.to_string(),
|
|
||||||
length: seg.len as f64,
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
waypoints: file
|
waypoints: file
|
||||||
|
|||||||
@@ -76,9 +76,7 @@ pub fn trackpoint<H: History>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A file as GPX (UTF-8 bytes), `None` if it does not exist. The options say which data of the
|
/// A file as GPX (UTF-8 bytes), `None` if it does not exist. The options say which data of the
|
||||||
/// trackpoints to keep, and with `as_route` that the segments are written as routes (`rte`)
|
/// trackpoints to keep.
|
||||||
/// instead of tracks: the anchors are the route points, and the trackpoints between them the
|
|
||||||
/// points of their path.
|
|
||||||
pub fn export_file<H: History>(
|
pub fn export_file<H: History>(
|
||||||
engine: &Engine<H>,
|
engine: &Engine<H>,
|
||||||
file_id: Uuid,
|
file_id: Uuid,
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
//! between them are kept: every part comes after the previous one in the new order, as late as the
|
//! between them are kept: every part comes after the previous one in the new order, as late as the
|
||||||
//! original one came after the part that followed it.
|
//! original one came after the part that followed it.
|
||||||
|
|
||||||
use crate::{Track, TrackSegment, Trackpoint};
|
use crate::{Route, Track, TrackSegment, Trackpoint};
|
||||||
|
|
||||||
/// The time of the first trackpoint of a segment.
|
/// The time of the first trackpoint of a segment.
|
||||||
pub fn start_time(segment: &TrackSegment) -> Option<i64> {
|
pub fn start_time(segment: &TrackSegment) -> Option<i64> {
|
||||||
@@ -94,6 +94,19 @@ pub fn reverse_segments(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reverses the routes of a file, and their trackpoints, like the segments of a track (see
|
||||||
|
/// [`reverse_segments`]).
|
||||||
|
pub fn reverse_routes(routes: &mut [Route], original_next: Option<i64>, new_previous: Option<i64>) {
|
||||||
|
routes.reverse();
|
||||||
|
let (mut next, mut previous) = (original_next, new_previous);
|
||||||
|
for route in routes.iter_mut() {
|
||||||
|
let original_start = start_time(&route.seg);
|
||||||
|
reverse_segment(&mut route.seg, next, previous);
|
||||||
|
next = original_start;
|
||||||
|
previous = end_time(&route.seg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Reverses the tracks of a file, and their segments and trackpoints.
|
/// Reverses the tracks of a file, and their segments and trackpoints.
|
||||||
pub fn reverse_tracks(tracks: &mut [Track], original_next: Option<i64>, new_previous: Option<i64>) {
|
pub fn reverse_tracks(tracks: &mut [Track], original_next: Option<i64>, new_previous: Option<i64>) {
|
||||||
tracks.reverse();
|
tracks.reverse();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{Link, Track, Waypoints};
|
use crate::{Link, Route, Track, TrackSegment, Waypoints};
|
||||||
|
|
||||||
use super::common::uuid_id;
|
use super::common::uuid_id;
|
||||||
|
|
||||||
@@ -8,6 +8,9 @@ uuid_id!(FileId);
|
|||||||
pub struct File {
|
pub struct File {
|
||||||
pub id: FileId,
|
pub id: FileId,
|
||||||
pub info: FileInfo,
|
pub info: FileInfo,
|
||||||
|
/// The routes come before the tracks, as in GPX: everything that goes through the segments
|
||||||
|
/// of a file (statistics, the elevation profile...) takes the ones of the routes first.
|
||||||
|
pub rte: Vec<Route>,
|
||||||
pub trk: Vec<Track>,
|
pub trk: Vec<Track>,
|
||||||
pub wpt: Waypoints,
|
pub wpt: Waypoints,
|
||||||
}
|
}
|
||||||
@@ -29,3 +32,50 @@ pub struct Author {
|
|||||||
/// The author has a single link, unlike the other elements.
|
/// The author has a single link, unlike the other elements.
|
||||||
pub link: Option<Link>,
|
pub link: Option<Link>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Where a segment is in its file: the one of a route, or one of a track.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum SegmentIndex {
|
||||||
|
Route(usize),
|
||||||
|
Track { trk: usize, seg: usize },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl File {
|
||||||
|
/// The segments of the routes, then the ones of the tracks.
|
||||||
|
pub fn segments(&self) -> impl Iterator<Item = &TrackSegment> {
|
||||||
|
self.rte
|
||||||
|
.iter()
|
||||||
|
.map(|rte| &rte.seg)
|
||||||
|
.chain(self.trk.iter().flat_map(|trk| &trk.trkseg))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where the segments are, in the order of [`File::segments`].
|
||||||
|
pub fn segment_indices(&self) -> impl Iterator<Item = SegmentIndex> + '_ {
|
||||||
|
(0..self.rte.len())
|
||||||
|
.map(SegmentIndex::Route)
|
||||||
|
.chain(self.trk.iter().enumerate().flat_map(|(trk, track)| {
|
||||||
|
(0..track.trkseg.len()).map(move |seg| SegmentIndex::Track { trk, seg })
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The segment at `index`. Panics if there is none.
|
||||||
|
pub fn segment(&self, index: SegmentIndex) -> &TrackSegment {
|
||||||
|
match index {
|
||||||
|
SegmentIndex::Route(rte) => &self.rte[rte].seg,
|
||||||
|
SegmentIndex::Track { trk, seg } => &self.trk[trk].trkseg[seg],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The segment at `index`. Panics if there is none.
|
||||||
|
pub fn segment_mut(&mut self, index: SegmentIndex) -> &mut TrackSegment {
|
||||||
|
match index {
|
||||||
|
SegmentIndex::Route(rte) => &mut self.rte[rte].seg,
|
||||||
|
SegmentIndex::Track { trk, seg } => &mut self.trk[trk].trkseg[seg],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether the file has no route nor track.
|
||||||
|
pub fn has_no_path(&self) -> bool {
|
||||||
|
self.rte.is_empty() && self.trk.is_empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ mod chunk;
|
|||||||
mod chunked;
|
mod chunked;
|
||||||
mod common;
|
mod common;
|
||||||
mod file;
|
mod file;
|
||||||
|
mod route;
|
||||||
mod segment;
|
mod segment;
|
||||||
mod track;
|
mod track;
|
||||||
mod trackpoint;
|
mod trackpoint;
|
||||||
@@ -14,6 +15,7 @@ pub use chunk::*;
|
|||||||
pub use chunked::*;
|
pub use chunked::*;
|
||||||
pub use common::*;
|
pub use common::*;
|
||||||
pub use file::*;
|
pub use file::*;
|
||||||
|
pub use route::*;
|
||||||
pub use segment::*;
|
pub use segment::*;
|
||||||
pub use track::*;
|
pub use track::*;
|
||||||
pub use trackpoint::*;
|
pub use trackpoint::*;
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
use crate::{TrackInfo, TrackSegment};
|
||||||
|
|
||||||
|
use super::common::uuid_id;
|
||||||
|
|
||||||
|
uuid_id!(RouteId);
|
||||||
|
|
||||||
|
/// A route of the file (`rte`): like a track, but with a single segment, its points.
|
||||||
|
///
|
||||||
|
/// It has the information of a track: GPX gives the same to both.
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub struct Route {
|
||||||
|
pub id: RouteId,
|
||||||
|
pub info: TrackInfo,
|
||||||
|
pub seg: TrackSegment,
|
||||||
|
}
|
||||||
+43
-44
@@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
Author, Chunk, File, Link, LngLat, Track, TrackSegment, Trackpoint, TrackpointCategories,
|
Author, Chunk, File, Link, LngLat, Route, Track, TrackSegment, Trackpoint,
|
||||||
TrackpointChunk, Waypoint, WaypointChunk, distance,
|
TrackpointCategories, TrackpointChunk, Waypoint, WaypointChunk, distance,
|
||||||
};
|
};
|
||||||
use chrono::DateTime;
|
use chrono::DateTime;
|
||||||
use quick_xml::Error;
|
use quick_xml::Error;
|
||||||
@@ -18,6 +18,7 @@ enum GPXElement {
|
|||||||
Author(Author),
|
Author(Author),
|
||||||
Link(Link),
|
Link(Link),
|
||||||
Text,
|
Text,
|
||||||
|
/// A track, or a route, whose information is read the same way.
|
||||||
Track(Track),
|
Track(Track),
|
||||||
Segment(TrackSegment),
|
Segment(TrackSegment),
|
||||||
Trackpoint(Trackpoint),
|
Trackpoint(Trackpoint),
|
||||||
@@ -396,7 +397,7 @@ pub fn parse(data: &[u8], categories: &mut TrackpointCategories) -> Result<File,
|
|||||||
let mut stack: Vec<GPXElement> = vec![];
|
let mut stack: Vec<GPXElement> = vec![];
|
||||||
let mut trkpt_chunk = TrackpointChunk::default();
|
let mut trkpt_chunk = TrackpointChunk::default();
|
||||||
let mut wpt_chunk = WaypointChunk::default();
|
let mut wpt_chunk = WaypointChunk::default();
|
||||||
// Routes are read as tracks of a single segment, see `RouteReader`. The points of the detailed
|
// The segment of a route is read by `RouteReader`. The points of the detailed
|
||||||
// path that a route point comes with (`gpxx:rpt`) are collected until the route point is read.
|
// path that a route point comes with (`gpxx:rpt`) are collected until the route point is read.
|
||||||
let mut route = RouteReader::default();
|
let mut route = RouteReader::default();
|
||||||
let mut rpt_points: Vec<Trackpoint> = vec![];
|
let mut rpt_points: Vec<Trackpoint> = vec![];
|
||||||
@@ -530,12 +531,12 @@ pub fn parse(data: &[u8], categories: &mut TrackpointCategories) -> Result<File,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
"rte" => {
|
"rte" => {
|
||||||
if let Some(GPXElement::Track(mut trk)) = stack.pop() {
|
if let Some(GPXElement::Track(trk)) = stack.pop() {
|
||||||
let trkseg = std::mem::take(&mut route).finish();
|
gpx.rte.push(Route {
|
||||||
if !trkseg.is_empty() {
|
info: trk.info,
|
||||||
trk.trkseg.push(trkseg);
|
seg: std::mem::take(&mut route).finish(),
|
||||||
}
|
..Default::default()
|
||||||
gpx.trk.push(trk);
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"rtept" => {
|
"rtept" => {
|
||||||
@@ -740,38 +741,37 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_routes_as_tracks() {
|
fn test_parse_routes() {
|
||||||
let gpx = parse_data("with_routes");
|
let gpx = parse_data("with_routes");
|
||||||
|
|
||||||
assert_eq!(gpx.trk.len(), 2);
|
assert!(gpx.trk.is_empty());
|
||||||
let trk = &gpx.trk[0];
|
assert_eq!(gpx.rte.len(), 2);
|
||||||
assert_eq!(trk.info.name.as_deref(), Some("route 1"));
|
let rte = &gpx.rte[0];
|
||||||
assert_eq!(trk.info.type_.as_deref(), Some("Cycling"));
|
assert_eq!(rte.info.name.as_deref(), Some("route 1"));
|
||||||
assert_eq!(trk.trkseg.len(), 1);
|
assert_eq!(rte.info.type_.as_deref(), Some("Cycling"));
|
||||||
let trkseg = &trk.trkseg[0];
|
assert_eq!(rte.seg.len(), 49);
|
||||||
assert_eq!(trkseg.len(), 49);
|
let trkpt = &rte.seg[0];
|
||||||
let trkpt = &trkseg[0];
|
|
||||||
assert_eq!(trkpt.coordinates.lat, 50.790867);
|
assert_eq!(trkpt.coordinates.lat, 50.790867);
|
||||||
assert_eq!(trkpt.coordinates.lng, 4.404968);
|
assert_eq!(trkpt.coordinates.lng, 4.404968);
|
||||||
assert_eq!(trkpt.ele, 109.0);
|
assert_eq!(trkpt.ele, 109.0);
|
||||||
|
|
||||||
let trk = &gpx.trk[1];
|
let rte = &gpx.rte[1];
|
||||||
assert_eq!(trk.info.name.as_deref(), Some("route 2"));
|
assert_eq!(rte.info.name.as_deref(), Some("route 2"));
|
||||||
assert_eq!(trk.trkseg[0].len(), 28);
|
assert_eq!(rte.seg.len(), 28);
|
||||||
|
assert_ne!(gpx.rte[0].id, rte.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_route_point_extensions() {
|
fn test_parse_route_point_extensions() {
|
||||||
let gpx = parse_data("with_route_extensions");
|
let gpx = parse_data("with_route_extensions");
|
||||||
|
|
||||||
// a route without points has no segment
|
// a route without points has an empty segment
|
||||||
assert_eq!(gpx.trk.len(), 2);
|
assert_eq!(gpx.rte.len(), 2);
|
||||||
assert!(gpx.trk[1].trkseg.is_empty());
|
assert!(gpx.rte[1].seg.is_empty());
|
||||||
|
|
||||||
let trk = &gpx.trk[0];
|
let rte = &gpx.rte[0];
|
||||||
assert_eq!(trk.info.name.as_deref(), Some("with extensions"));
|
assert_eq!(rte.info.name.as_deref(), Some("with extensions"));
|
||||||
assert_eq!(trk.trkseg.len(), 1);
|
let points: Vec<_> = rte.seg.iter().collect();
|
||||||
let points: Vec<_> = trk.trkseg[0].iter().collect();
|
|
||||||
// the route points are kept, the points of the detailed path follow the one they come with
|
// the route points are kept, the points of the detailed path follow the one they come with
|
||||||
let coordinates: Vec<_> = points
|
let coordinates: Vec<_> = points
|
||||||
.iter()
|
.iter()
|
||||||
@@ -788,14 +788,13 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_route_points_are_the_anchors_and_nothing_else_is() {
|
fn test_route_points_are_the_anchors_and_nothing_else_is() {
|
||||||
let gpx = parse_data("with_route_extensions");
|
let gpx = parse_data("with_route_extensions");
|
||||||
let anchors: Vec<_> = gpx.trk[0].trkseg[0].iter().map(|p| p.anchor).collect();
|
let anchors: Vec<_> = gpx.rte[0].seg.iter().map(|p| p.anchor).collect();
|
||||||
assert_eq!(anchors, [Some(0), None, None, Some(0), Some(0)]);
|
assert_eq!(anchors, [Some(0), None, None, Some(0), Some(0)]);
|
||||||
|
|
||||||
// no anchor is computed for the points of a route that has none in between
|
// no anchor is computed for the points of a route that has none in between
|
||||||
let gpx = parse_data("with_routes");
|
let gpx = parse_data("with_routes");
|
||||||
for trk in &gpx.trk {
|
for rte in &gpx.rte {
|
||||||
let segment = &trk.trkseg[0];
|
assert!(rte.seg.iter().all(|p| p.anchor == Some(0)));
|
||||||
assert!(segment.iter().all(|p| p.anchor == Some(0)));
|
|
||||||
}
|
}
|
||||||
// while a track gets the anchors that its shape needs
|
// while a track gets the anchors that its shape needs
|
||||||
let track = parse_data("with_tracks");
|
let track = parse_data("with_tracks");
|
||||||
@@ -807,14 +806,14 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_parse_the_points_of_the_detailed_path_of_a_route() {
|
fn test_parse_the_points_of_the_detailed_path_of_a_route() {
|
||||||
let gpx = parse_data("with_route_points");
|
let gpx = parse_data("with_route_points");
|
||||||
assert_eq!(gpx.trk.len(), 1);
|
assert_eq!(gpx.rte.len(), 1);
|
||||||
let trk = &gpx.trk[0];
|
let rte = &gpx.rte[0];
|
||||||
assert_eq!(trk.info.name.as_deref(), Some("Lausanne"));
|
assert_eq!(rte.info.name.as_deref(), Some("Lausanne"));
|
||||||
assert_eq!(trk.info.cmt.as_deref(), Some("comment"));
|
assert_eq!(rte.info.cmt.as_deref(), Some("comment"));
|
||||||
assert_eq!(trk.info.desc.as_deref(), Some("description"));
|
assert_eq!(rte.info.desc.as_deref(), Some("description"));
|
||||||
assert_eq!(trk.info.type_.as_deref(), Some("Cycling"));
|
assert_eq!(rte.info.type_.as_deref(), Some("Cycling"));
|
||||||
|
|
||||||
let points: Vec<_> = trk.trkseg[0].iter().cloned().collect();
|
let points: Vec<_> = rte.seg.iter().cloned().collect();
|
||||||
// 3 route points and 4 points between them, the repeated one being kept
|
// 3 route points and 4 points between them, the repeated one being kept
|
||||||
assert_eq!(points.len(), 7);
|
assert_eq!(points.len(), 7);
|
||||||
let kinds: Vec<_> = points.iter().map(|p| p.anchor.is_some()).collect();
|
let kinds: Vec<_> = points.iter().map(|p| p.anchor.is_some()).collect();
|
||||||
@@ -846,7 +845,7 @@ mod tests {
|
|||||||
</rtept>
|
</rtept>
|
||||||
</rte></gpx>"#;
|
</rte></gpx>"#;
|
||||||
let gpx = parse(data.as_bytes(), &mut Default::default()).unwrap();
|
let gpx = parse(data.as_bytes(), &mut Default::default()).unwrap();
|
||||||
let points: Vec<_> = gpx.trk[0].trkseg[0].iter().cloned().collect();
|
let points: Vec<_> = gpx.rte[0].seg.iter().cloned().collect();
|
||||||
let coordinates: Vec<_> = points.iter().map(|p| p.coordinates.lat).collect();
|
let coordinates: Vec<_> = points.iter().map(|p| p.coordinates.lat).collect();
|
||||||
assert_eq!(coordinates, [1.0, 2.0, 2.1, 2.2]);
|
assert_eq!(coordinates, [1.0, 2.0, 2.1, 2.2]);
|
||||||
// the route point is an anchor, and so is the end of the path
|
// the route point is an anchor, and so is the end of the path
|
||||||
@@ -861,7 +860,7 @@ mod tests {
|
|||||||
<gpxx:RoutePointExtension><gpxx:rpt lat="1.5" lon="1.5"/></gpxx:RoutePointExtension>
|
<gpxx:RoutePointExtension><gpxx:rpt lat="1.5" lon="1.5"/></gpxx:RoutePointExtension>
|
||||||
</extensions></rtept></rte></gpx>"#;
|
</extensions></rtept></rte></gpx>"#;
|
||||||
let gpx = parse(data.as_bytes(), &mut Default::default()).unwrap();
|
let gpx = parse(data.as_bytes(), &mut Default::default()).unwrap();
|
||||||
let anchors: Vec<_> = gpx.trk[0].trkseg[0].iter().map(|p| p.anchor).collect();
|
let anchors: Vec<_> = gpx.rte[0].seg.iter().map(|p| p.anchor).collect();
|
||||||
assert_eq!(anchors, [Some(0), Some(0)]);
|
assert_eq!(anchors, [Some(0), Some(0)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -930,14 +929,14 @@ mod tests {
|
|||||||
assert_eq!(waypoints[0].links[2].text.as_deref(), Some("three"));
|
assert_eq!(waypoints[0].links[2].text.as_deref(), Some("three"));
|
||||||
assert!(waypoints[1].links.is_empty());
|
assert!(waypoints[1].links.is_empty());
|
||||||
|
|
||||||
// tracks, and routes which are read as tracks
|
// tracks, and routes
|
||||||
assert_eq!(gpx.trk.len(), 2);
|
assert_eq!((gpx.trk.len(), gpx.rte.len()), (1, 1));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
hrefs(&gpx.trk[0].info.links),
|
hrefs(&gpx.trk[0].info.links),
|
||||||
["https://example.com/trk-1", "https://example.com/trk-2"]
|
["https://example.com/trk-1", "https://example.com/trk-2"]
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
hrefs(&gpx.trk[1].info.links),
|
hrefs(&gpx.rte[0].info.links),
|
||||||
["https://example.com/rte-1", "https://example.com/rte-2"]
|
["https://example.com/rte-1", "https://example.com/rte-2"]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+114
-119
@@ -8,9 +8,6 @@ use crate::{File, Link, Track, Trackpoint, TrackpointCategories, Waypoint};
|
|||||||
|
|
||||||
/// What the written file contains: the data of the trackpoints that are `false` are left out.
|
/// What the written file contains: the data of the trackpoints that are `false` are left out.
|
||||||
/// The same type tells which data a file has, see [`File::exportable_data`].
|
/// The same type tells which data a file has, see [`File::exportable_data`].
|
||||||
///
|
|
||||||
/// With `as_route`, the file is written with routes instead of tracks, see [`write`]. It is not
|
|
||||||
/// data: it is off in `ALL` and `NONE`, and in what [`File::exportable_data`] gives.
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct ExportOptions {
|
pub struct ExportOptions {
|
||||||
pub time: bool,
|
pub time: bool,
|
||||||
@@ -20,8 +17,6 @@ pub struct ExportOptions {
|
|||||||
pub power: bool,
|
pub power: bool,
|
||||||
/// The OpenStreetMap data: surface, highway, SAC scale and MTB scale.
|
/// The OpenStreetMap data: surface, highway, SAC scale and MTB scale.
|
||||||
pub osm: bool,
|
pub osm: bool,
|
||||||
/// Writes every segment as a route (`rte`) instead of a track.
|
|
||||||
pub as_route: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ExportOptions {
|
impl ExportOptions {
|
||||||
@@ -36,7 +31,6 @@ impl ExportOptions {
|
|||||||
atemp: value,
|
atemp: value,
|
||||||
power: value,
|
power: value,
|
||||||
osm: value,
|
osm: value,
|
||||||
as_route: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +43,6 @@ impl ExportOptions {
|
|||||||
atemp: self.atemp || other.atemp,
|
atemp: self.atemp || other.atemp,
|
||||||
power: self.power || other.power,
|
power: self.power || other.power,
|
||||||
osm: self.osm || other.osm,
|
osm: self.osm || other.osm,
|
||||||
as_route: self.as_route || other.as_route,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,12 +52,7 @@ impl File {
|
|||||||
/// options of [`write`] can leave out.
|
/// options of [`write`] can leave out.
|
||||||
pub fn exportable_data(&self) -> ExportOptions {
|
pub fn exportable_data(&self) -> ExportOptions {
|
||||||
let mut data = ExportOptions::NONE;
|
let mut data = ExportOptions::NONE;
|
||||||
for point in self
|
for point in self.segments().flat_map(|seg| seg.iter()) {
|
||||||
.trk
|
|
||||||
.iter()
|
|
||||||
.flat_map(|trk| &trk.trkseg)
|
|
||||||
.flat_map(|seg| seg.iter())
|
|
||||||
{
|
|
||||||
data.time |= point.time.is_some();
|
data.time |= point.time.is_some();
|
||||||
data.hr |= point.hr.is_some();
|
data.hr |= point.hr.is_some();
|
||||||
data.cad |= point.cad.is_some();
|
data.cad |= point.cad.is_some();
|
||||||
@@ -89,10 +77,9 @@ type XmlWriter = Writer<Vec<u8>>;
|
|||||||
/// Writes a file as GPX 1.1, in UTF-8. The surface, highway, SAC scale and MTB scale of the trackpoints are
|
/// Writes a file as GPX 1.1, in UTF-8. The surface, highway, SAC scale and MTB scale of the trackpoints are
|
||||||
/// the names of their codes in `categories`.
|
/// the names of their codes in `categories`.
|
||||||
///
|
///
|
||||||
/// The segments are written as tracks, or with `options.as_route` as routes: a segment becomes a
|
/// The routes are written as routes (`rte`): the anchors of their segment are the `rtept` and the
|
||||||
/// `rte` that has the information of its track, its anchors are the `rtept` and the trackpoints
|
/// trackpoints between two anchors are the `gpxx:rpt` of the `rtept` before them (which only have
|
||||||
/// between two anchors are the `gpxx:rpt` of the `rtept` before them (which only have a
|
/// a position, unlike the `rtept`). The first and last trackpoints always are `rtept`.
|
||||||
/// position, unlike the `rtept`). The first and last trackpoints always are `rtept`.
|
|
||||||
pub fn write(file: &File, categories: &TrackpointCategories, options: ExportOptions) -> Vec<u8> {
|
pub fn write(file: &File, categories: &TrackpointCategories, options: ExportOptions) -> Vec<u8> {
|
||||||
let mut writer = Writer::new_with_indent(Vec::new(), b' ', 4);
|
let mut writer = Writer::new_with_indent(Vec::new(), b' ', 4);
|
||||||
// writing to memory does not fail
|
// writing to memory does not fail
|
||||||
@@ -100,9 +87,9 @@ pub fn write(file: &File, categories: &TrackpointCategories, options: ExportOpti
|
|||||||
writer.into_inner()
|
writer.into_inner()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_file(
|
fn write_file<'a>(
|
||||||
w: &mut XmlWriter,
|
w: &mut XmlWriter,
|
||||||
file: &File,
|
file: &'a File,
|
||||||
categories: &TrackpointCategories,
|
categories: &TrackpointCategories,
|
||||||
options: ExportOptions,
|
options: ExportOptions,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
@@ -136,20 +123,18 @@ fn write_file(
|
|||||||
for wpt in file.wpt.iter() {
|
for wpt in file.wpt.iter() {
|
||||||
write_waypoint(w, wpt, options)?;
|
write_waypoint(w, wpt, options)?;
|
||||||
}
|
}
|
||||||
|
// a single unnamed route or track is named like the file
|
||||||
|
let alone = file.rte.len() + file.trk.len() == 1;
|
||||||
|
let name = |info: &'a crate::TrackInfo| match &info.name {
|
||||||
|
Some(name) if !name.is_empty() => Some(name.as_str()),
|
||||||
|
_ if alone => Some(file.info.name.as_str()),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
for rte in &file.rte {
|
||||||
|
write_route(w, &rte.info, &rte.seg, name(&rte.info), categories, options)?;
|
||||||
|
}
|
||||||
for trk in &file.trk {
|
for trk in &file.trk {
|
||||||
// a single unnamed track is named like the file
|
write_track(w, trk, name(&trk.info), categories, options)?;
|
||||||
let name = match (&trk.info.name, file.trk.len()) {
|
|
||||||
(Some(name), _) if !name.is_empty() => Some(name.as_str()),
|
|
||||||
(_, 1) => Some(file.info.name.as_str()),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
if options.as_route {
|
|
||||||
for segment in trk.trkseg.iter().filter(|segment| !segment.is_empty()) {
|
|
||||||
write_route(w, trk, segment, name, categories, options)?;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
write_track(w, trk, name, categories, options)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
@@ -311,16 +296,16 @@ fn write_track(
|
|||||||
|
|
||||||
fn write_route(
|
fn write_route(
|
||||||
w: &mut XmlWriter,
|
w: &mut XmlWriter,
|
||||||
trk: &Track,
|
info: &crate::TrackInfo,
|
||||||
segment: &crate::TrackSegment,
|
segment: &crate::TrackSegment,
|
||||||
name: Option<&str>,
|
name: Option<&str>,
|
||||||
categories: &TrackpointCategories,
|
categories: &TrackpointCategories,
|
||||||
options: ExportOptions,
|
options: ExportOptions,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
w.create_element("rte").write_inner_content(|w| {
|
w.create_element("rte").write_inner_content(|w| {
|
||||||
write_track_info(w, name, &trk.info)?;
|
write_track_info(w, name, info)?;
|
||||||
let points: Vec<&Trackpoint> = segment.iter().collect();
|
let points: Vec<&Trackpoint> = segment.iter().collect();
|
||||||
let last = points.len() - 1;
|
let last = points.len().saturating_sub(1);
|
||||||
let is_anchor = |i: usize| i == 0 || i == last || points[i].anchor.is_some();
|
let is_anchor = |i: usize| i == 0 || i == last || points[i].anchor.is_some();
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < points.len() {
|
while i < points.len() {
|
||||||
@@ -455,9 +440,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn points(file: &File) -> Vec<Trackpoint> {
|
fn points(file: &File) -> Vec<Trackpoint> {
|
||||||
file.trk
|
file.segments()
|
||||||
.iter()
|
|
||||||
.flat_map(|trk| &trk.trkseg)
|
|
||||||
.flat_map(|seg| seg.iter().cloned())
|
.flat_map(|seg| seg.iter().cloned())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
@@ -595,11 +578,20 @@ mod tests {
|
|||||||
assert!(file.exportable_data().power);
|
assert!(file.exportable_data().power);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_route() -> ExportOptions {
|
/// The file with each segment of its tracks as a route, which has the information of its
|
||||||
ExportOptions {
|
/// track.
|
||||||
as_route: true,
|
fn as_routes(mut file: File) -> File {
|
||||||
..ExportOptions::ALL
|
file.rte = std::mem::take(&mut file.trk)
|
||||||
}
|
.into_iter()
|
||||||
|
.flat_map(|trk| {
|
||||||
|
trk.trkseg.into_iter().map(move |seg| crate::Route {
|
||||||
|
info: trk.info.clone(),
|
||||||
|
seg,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
file
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count(text: &str, pattern: &str) -> usize {
|
fn count(text: &str, pattern: &str) -> usize {
|
||||||
@@ -607,19 +599,17 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_export_as_route_writes_routes_instead_of_tracks() {
|
fn test_routes_are_written_with_their_route_points_and_their_path() {
|
||||||
let (file, categories) = read("with_route_points");
|
let (file, categories) = read("with_route_points");
|
||||||
let text = written_text(&file, &categories, as_route());
|
let text = written_text(&file, &categories, ExportOptions::ALL);
|
||||||
|
|
||||||
assert_eq!(count(&text, "<rte>"), 1);
|
assert_eq!(count(&text, "<rte>"), 1);
|
||||||
assert_eq!(count(&text, "<trk>"), 0);
|
assert_eq!(count(&text, "<trk>"), 0);
|
||||||
assert_eq!(count(&text, "<trkseg>"), 0);
|
|
||||||
assert_eq!(count(&text, "<trkpt"), 0);
|
assert_eq!(count(&text, "<trkpt"), 0);
|
||||||
// the anchors are the route points, the trackpoints between them the points of their path
|
// the anchors are the route points, the trackpoints between them the points of their path
|
||||||
assert_eq!(count(&text, "<rtept"), 3);
|
assert_eq!(count(&text, "<rtept"), 3);
|
||||||
assert_eq!(count(&text, "<gpxx:rpt"), 4);
|
assert_eq!(count(&text, "<gpxx:rpt"), 4);
|
||||||
assert_eq!(count(&text, "<gpxx:RoutePointExtension>"), 2);
|
assert_eq!(count(&text, "<gpxx:RoutePointExtension>"), 2);
|
||||||
// a route has the information of its track
|
|
||||||
assert!(text.contains("<name>Lausanne</name>"));
|
assert!(text.contains("<name>Lausanne</name>"));
|
||||||
assert!(text.contains("<cmt>comment</cmt>"));
|
assert!(text.contains("<cmt>comment</cmt>"));
|
||||||
assert!(text.contains("<desc>description</desc>"));
|
assert!(text.contains("<desc>description</desc>"));
|
||||||
@@ -634,14 +624,14 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_a_route_survives_being_exported_as_a_route() {
|
fn test_a_route_survives_being_written() {
|
||||||
let (file, mut categories) = read("with_route_points");
|
let (file, mut categories) = read("with_route_points");
|
||||||
let written = write(&file, &categories, as_route());
|
let written = write(&file, &categories, ExportOptions::ALL);
|
||||||
let again = parse(&written, &mut categories).unwrap();
|
let again = parse(&written, &mut categories).unwrap();
|
||||||
|
|
||||||
assert_eq!(again.trk.len(), 1);
|
assert_eq!(again.rte.len(), 1);
|
||||||
assert_eq!(again.trk[0].info, file.trk[0].info);
|
assert_eq!(again.rte[0].info, file.rte[0].info);
|
||||||
let (a, b) = (&file.trk[0].trkseg[0], &again.trk[0].trkseg[0]);
|
let (a, b) = (&file.rte[0].seg, &again.rte[0].seg);
|
||||||
assert_eq!(a.len(), b.len());
|
assert_eq!(a.len(), b.len());
|
||||||
for (a, b) in a.iter().zip(b.iter()) {
|
for (a, b) in a.iter().zip(b.iter()) {
|
||||||
assert_eq!(a.coordinates.lat, b.coordinates.lat);
|
assert_eq!(a.coordinates.lat, b.coordinates.lat);
|
||||||
@@ -659,85 +649,102 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_tracks_become_one_route_per_segment_with_the_information_of_the_track() {
|
fn test_the_anchors_of_a_route_are_its_route_points() {
|
||||||
let (file, mut categories) = read("with_tracks_and_segments");
|
let (file, mut categories) = read("with_tracks_and_segments");
|
||||||
let segments: usize = file.trk.iter().map(|t| t.trkseg.len()).sum();
|
let file = as_routes(file);
|
||||||
assert!(segments > 2);
|
assert!(file.rte.len() > 2);
|
||||||
let written = write(&file, &categories, as_route());
|
let written = write(&file, &categories, ExportOptions::ALL);
|
||||||
let text = String::from_utf8(written.clone()).unwrap();
|
|
||||||
assert_eq!(count(&text, "<rte>"), segments);
|
|
||||||
assert_eq!(count(&text, "<trk>"), 0);
|
|
||||||
// every route starts and ends with a route point
|
|
||||||
assert_eq!(count(&text, "</rte>"), segments);
|
|
||||||
|
|
||||||
let again = parse(&written, &mut categories).unwrap();
|
let again = parse(&written, &mut categories).unwrap();
|
||||||
assert_eq!(again.trk.len(), segments);
|
assert_eq!(again.rte.len(), file.rte.len());
|
||||||
let mut routes = again.trk.iter();
|
let anchors = |s: &crate::TrackSegment| {
|
||||||
for track in &file.trk {
|
s.iter()
|
||||||
for segment in &track.trkseg {
|
.enumerate()
|
||||||
let route = routes.next().unwrap();
|
.filter(|(_, p)| p.anchor.is_some())
|
||||||
// the information of the track, repeated for each of its segments
|
.map(|(i, _)| i)
|
||||||
assert_eq!(route.info.name, track.info.name);
|
.collect::<Vec<_>>()
|
||||||
assert_eq!(route.info.type_, track.info.type_);
|
};
|
||||||
let (a, b) = (segment, &route.trkseg[0]);
|
for (a, b) in file.rte.iter().zip(&again.rte) {
|
||||||
assert_eq!(a.len(), b.len());
|
assert_eq!(a.info.name, b.info.name);
|
||||||
for (a, b) in a.iter().zip(b.iter()) {
|
assert_eq!(a.seg.len(), b.seg.len());
|
||||||
assert_eq!(
|
assert_eq!(anchors(&a.seg), anchors(&b.seg));
|
||||||
(a.coordinates.lat, a.coordinates.lng),
|
|
||||||
(b.coordinates.lat, b.coordinates.lng)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
// the anchors of the segment are the ones of its route
|
|
||||||
let anchors = |s: &crate::TrackSegment| {
|
|
||||||
s.iter()
|
|
||||||
.enumerate()
|
|
||||||
.filter(|(_, p)| p.anchor.is_some())
|
|
||||||
.map(|(i, _)| i)
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
};
|
|
||||||
assert_eq!(anchors(a), anchors(b));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_the_ends_of_a_route_are_route_points_even_without_anchors() {
|
fn test_the_ends_of_a_route_are_route_points_even_without_anchors() {
|
||||||
let (mut file, categories) = read("simple");
|
let (file, categories) = read("simple");
|
||||||
file.trk[0].trkseg[0].update_all(|_, point| point.anchor = None);
|
let mut file = as_routes(file);
|
||||||
let len = file.trk[0].trkseg[0].len();
|
file.rte[0].seg.update_all(|_, point| point.anchor = None);
|
||||||
let text = written_text(&file, &categories, as_route());
|
let len = file.rte[0].seg.len();
|
||||||
|
let text = written_text(&file, &categories, ExportOptions::ALL);
|
||||||
assert_eq!(count(&text, "<rtept"), 2);
|
assert_eq!(count(&text, "<rtept"), 2);
|
||||||
assert_eq!(count(&text, "<gpxx:rpt"), len - 2);
|
assert_eq!(count(&text, "<gpxx:rpt"), len - 2);
|
||||||
// and a single trackpoint is a route of one point
|
// and a single trackpoint is a route of one point
|
||||||
file.trk[0].trkseg[0].splice(1, len, vec![]);
|
file.rte[0].seg.splice(1, len, vec![]);
|
||||||
let text = written_text(&file, &categories, as_route());
|
let text = written_text(&file, &categories, ExportOptions::ALL);
|
||||||
assert_eq!(count(&text, "<rtept"), 1);
|
assert_eq!(count(&text, "<rtept"), 1);
|
||||||
assert_eq!(count(&text, "<gpxx:rpt"), 0);
|
assert_eq!(count(&text, "<gpxx:rpt"), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_empty_segments_are_not_routes() {
|
fn test_routes_keep_their_style() {
|
||||||
let (mut file, categories) = read("simple");
|
|
||||||
file.trk[0].trkseg.push(crate::TrackSegment::default());
|
|
||||||
let text = written_text(&file, &categories, as_route());
|
|
||||||
assert_eq!(count(&text, "<rte>"), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_routes_have_the_style_of_their_track() {
|
|
||||||
let (file, categories) = read("with_style");
|
let (file, categories) = read("with_style");
|
||||||
let text = written_text(&file, &categories, as_route());
|
let file = as_routes(file);
|
||||||
|
let text = written_text(&file, &categories, ExportOptions::ALL);
|
||||||
assert!(count(&text, "<gpx_style:line>") >= 1);
|
assert!(count(&text, "<gpx_style:line>") >= 1);
|
||||||
assert_eq!(count(&text, "<gpx_style:line>"), count(&text, "<rte>"));
|
assert_eq!(count(&text, "<gpx_style:line>"), count(&text, "<rte>"));
|
||||||
let again = parse(text.as_bytes(), &mut Default::default()).unwrap();
|
let again = parse(text.as_bytes(), &mut Default::default()).unwrap();
|
||||||
assert_eq!(again.trk[0].info.color, file.trk[0].info.color);
|
assert_eq!(again.rte[0].info.color, file.rte[0].info.color);
|
||||||
assert_eq!(again.trk[0].info.width, file.trk[0].info.width);
|
assert_eq!(again.rte[0].info.width, file.rte[0].info.width);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_routes_are_written_as_routes_before_the_tracks() {
|
||||||
|
let (mut file, categories) = read("with_tracks");
|
||||||
|
let (routes, _) = read("with_routes");
|
||||||
|
file.rte = routes.rte.clone();
|
||||||
|
let text = written_text(&file, &categories, ExportOptions::ALL);
|
||||||
|
assert_eq!(count(&text, "<rte>"), routes.rte.len());
|
||||||
|
assert_eq!(count(&text, "<trk>"), file.trk.len());
|
||||||
|
assert!(text.rfind("</rte>").unwrap() < text.find("<trk>").unwrap());
|
||||||
|
|
||||||
|
let again = parse(text.as_bytes(), &mut categories.clone()).unwrap();
|
||||||
|
assert_eq!(again.rte.len(), file.rte.len());
|
||||||
|
assert_eq!(again.trk.len(), file.trk.len());
|
||||||
|
for (a, b) in file.rte.iter().zip(&again.rte) {
|
||||||
|
assert_eq!(a.info, b.info);
|
||||||
|
same_points(
|
||||||
|
&a.seg.iter().cloned().collect::<Vec<_>>(),
|
||||||
|
&b.seg.iter().cloned().collect::<Vec<_>>(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
same_points(&points(&file), &points(&again));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_an_empty_route_is_written_and_a_single_route_is_named_like_the_file() {
|
||||||
|
let mut file = File::default();
|
||||||
|
file.info.name = "file".into();
|
||||||
|
file.rte.push(crate::Route::default());
|
||||||
|
let text = written_text(&file, &Default::default(), ExportOptions::ALL);
|
||||||
|
assert_eq!(count(&text, "<rte>"), 1);
|
||||||
|
assert_eq!(count(&text, "<rtept"), 0);
|
||||||
|
let again = parse(text.as_bytes(), &mut Default::default()).unwrap();
|
||||||
|
assert_eq!(again.rte.len(), 1);
|
||||||
|
assert_eq!(again.rte[0].info.name.as_deref(), Some("file"));
|
||||||
|
|
||||||
|
// not when there are others
|
||||||
|
file.trk.push(Track::default());
|
||||||
|
let text = written_text(&file, &Default::default(), ExportOptions::ALL);
|
||||||
|
// the metadata only
|
||||||
|
assert_eq!(count(&text, "<name>file</name>"), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_route_points_keep_their_data_and_the_options_apply_to_them() {
|
fn test_route_points_keep_their_data_and_the_options_apply_to_them() {
|
||||||
let (file, categories) = read("with_hr");
|
let (file, categories) = read("with_hr");
|
||||||
let with_all = written_text(&file, &categories, as_route());
|
let file = as_routes(file);
|
||||||
|
let with_all = written_text(&file, &categories, ExportOptions::ALL);
|
||||||
assert!(with_all.contains("<gpxtpx:hr>"));
|
assert!(with_all.contains("<gpxtpx:hr>"));
|
||||||
let without = written_text(
|
let without = written_text(
|
||||||
&file,
|
&file,
|
||||||
@@ -745,22 +752,10 @@ mod tests {
|
|||||||
ExportOptions {
|
ExportOptions {
|
||||||
hr: false,
|
hr: false,
|
||||||
time: false,
|
time: false,
|
||||||
as_route: true,
|
|
||||||
..ExportOptions::ALL
|
..ExportOptions::ALL
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
assert!(!without.contains("<gpxtpx:hr>"));
|
assert!(!without.contains("<gpxtpx:hr>"));
|
||||||
assert!(!without.contains("<time>"));
|
assert!(!without.contains("<time>"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_the_route_option_is_not_data() {
|
|
||||||
let (file, _) = read("with_route_points");
|
|
||||||
assert!(!file.exportable_data().as_route);
|
|
||||||
// it is a way to write the data, never part of them
|
|
||||||
let all = std::hint::black_box(ExportOptions::ALL);
|
|
||||||
assert!(!all.as_route && !ExportOptions::NONE.as_route);
|
|
||||||
assert!(as_route().union(ExportOptions::NONE).as_route);
|
|
||||||
assert!(!ExportOptions::ALL.union(ExportOptions::NONE).as_route);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
use crate::{Apply, CommandError, Edit, Editor, File, State, Track, update_selected};
|
use crate::{
|
||||||
|
Apply, CommandError, Edit, Editor, File, Route, State, Track, TrackInfo, update_selected,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Metadata<'a> {
|
pub struct Metadata<'a> {
|
||||||
@@ -13,21 +15,33 @@ impl Apply for Metadata<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Metadata<'_> {
|
||||||
|
fn set(&self, info: &mut TrackInfo) -> Edit {
|
||||||
|
info.name = Some(self.name.to_owned());
|
||||||
|
info.desc = Some(self.desc.to_owned());
|
||||||
|
Edit::Changed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Editor for Metadata<'_> {
|
impl Editor for Metadata<'_> {
|
||||||
fn file(&mut self, file: &mut File) -> Edit {
|
fn file(&mut self, file: &mut File) -> Edit {
|
||||||
file.info.name = self.name.to_owned();
|
file.info.name = self.name.to_owned();
|
||||||
file.info.desc = Some(self.desc.to_owned());
|
file.info.desc = Some(self.desc.to_owned());
|
||||||
// a single track is named like the file
|
// a single route or track is named like the file
|
||||||
if let [track] = file.trk.as_mut_slice() {
|
match (file.rte.as_mut_slice(), file.trk.as_mut_slice()) {
|
||||||
track.info.name = Some(self.name.to_owned());
|
([route], []) => route.info.name = Some(self.name.to_owned()),
|
||||||
|
([], [track]) => track.info.name = Some(self.name.to_owned()),
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
Edit::Changed
|
Edit::Changed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn route(&mut self, route: &mut Route) -> Edit {
|
||||||
|
self.set(&mut route.info)
|
||||||
|
}
|
||||||
|
|
||||||
fn track(&mut self, track: &mut Track) -> Edit {
|
fn track(&mut self, track: &mut Track) -> Edit {
|
||||||
track.info.name = Some(self.name.to_owned());
|
self.set(&mut track.info)
|
||||||
track.info.desc = Some(self.desc.to_owned());
|
|
||||||
Edit::Changed
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
mod metadata;
|
mod metadata;
|
||||||
|
mod new_route;
|
||||||
mod new_track;
|
mod new_track;
|
||||||
mod new_track_segment;
|
mod new_track_segment;
|
||||||
mod style;
|
mod style;
|
||||||
|
|
||||||
pub use metadata::*;
|
pub use metadata::*;
|
||||||
|
pub use new_route::*;
|
||||||
pub use new_track::*;
|
pub use new_track::*;
|
||||||
pub use new_track_segment::*;
|
pub use new_track_segment::*;
|
||||||
pub use style::*;
|
pub use style::*;
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
use crate::{Apply, CommandError, Edit, Editor, File, Route, State, update_selected};
|
||||||
|
|
||||||
|
use super::new_track::common_style;
|
||||||
|
|
||||||
|
/// Adds an empty route to the selected files, with the style that their routes and tracks agree
|
||||||
|
/// on (see [`crate::NewTrack`]).
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct NewRoute;
|
||||||
|
|
||||||
|
impl Apply for NewRoute {
|
||||||
|
fn apply(mut self, state: &mut State) -> Result<(), CommandError> {
|
||||||
|
update_selected(state, &mut self);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Editor for NewRoute {
|
||||||
|
fn file(&mut self, file: &mut File) -> Edit {
|
||||||
|
file.rte.push(Route {
|
||||||
|
info: common_style(file),
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
Edit::Changed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::{New, engine::command::fixture::Fixture};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_route_has_the_style_of_the_tracks_and_an_empty_segment() {
|
||||||
|
let mut fx = Fixture::default();
|
||||||
|
New {
|
||||||
|
name: "file",
|
||||||
|
trackpoint: None,
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
crate::NewTrack.apply(&mut fx.state()).unwrap();
|
||||||
|
let file = std::rc::Rc::make_mut(fx.files.values_mut().next().unwrap());
|
||||||
|
file.trk[0].info.color = Some("ff0000".into());
|
||||||
|
NewRoute.apply(&mut fx.state()).unwrap();
|
||||||
|
NewRoute.apply(&mut fx.state()).unwrap();
|
||||||
|
|
||||||
|
let file = fx.files.values().next().unwrap();
|
||||||
|
assert_eq!(file.rte.len(), 2);
|
||||||
|
assert_ne!(file.rte[0].id, file.rte[1].id);
|
||||||
|
assert!(file.rte[0].seg.is_empty());
|
||||||
|
assert_eq!(file.rte[1].info.color.as_deref(), Some("ff0000"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{Apply, CommandError, Edit, Editor, File, State, Track, update_selected};
|
use crate::{Apply, CommandError, Edit, Editor, File, State, Track, TrackInfo, update_selected};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct NewTrack;
|
pub struct NewTrack;
|
||||||
@@ -12,20 +12,41 @@ impl Apply for NewTrack {
|
|||||||
|
|
||||||
impl Editor for NewTrack {
|
impl Editor for NewTrack {
|
||||||
fn file(&mut self, file: &mut File) -> Edit {
|
fn file(&mut self, file: &mut File) -> Edit {
|
||||||
let mut track = Track::default();
|
file.trk.push(Track {
|
||||||
track.info.color = common(file, |t| &t.info.color);
|
info: common_style(file),
|
||||||
track.info.opacity = common(file, |t| &t.info.opacity);
|
..Default::default()
|
||||||
track.info.width = common(file, |t| &t.info.width);
|
});
|
||||||
file.trk.push(track);
|
|
||||||
Edit::Changed
|
Edit::Changed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the value of a style attribute if all existing tracks agree on it.
|
/// The style that all the routes and tracks of the file agree on: each attribute is set if they
|
||||||
fn common<T: Clone + PartialEq>(file: &File, get: impl Fn(&Track) -> &Option<T>) -> Option<T> {
|
/// all have the same value for it.
|
||||||
let mut tracks = file.trk.iter();
|
pub(super) fn common_style(file: &File) -> TrackInfo {
|
||||||
let first = get(tracks.next()?);
|
let infos: Vec<&TrackInfo> = file
|
||||||
tracks.all(|t| get(t) == first).then(|| first.clone())?
|
.rte
|
||||||
|
.iter()
|
||||||
|
.map(|rte| &rte.info)
|
||||||
|
.chain(file.trk.iter().map(|trk| &trk.info))
|
||||||
|
.collect();
|
||||||
|
TrackInfo {
|
||||||
|
color: common(&infos, |info| &info.color),
|
||||||
|
opacity: common(&infos, |info| &info.opacity),
|
||||||
|
width: common(&infos, |info| &info.width),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the value of a style attribute if all the infos agree on it.
|
||||||
|
fn common<T: Clone + PartialEq>(
|
||||||
|
infos: &[&TrackInfo],
|
||||||
|
get: impl Fn(&TrackInfo) -> &Option<T>,
|
||||||
|
) -> Option<T> {
|
||||||
|
let mut infos = infos.iter();
|
||||||
|
let first = get(infos.next()?);
|
||||||
|
infos
|
||||||
|
.all(|info| get(info) == first)
|
||||||
|
.then(|| first.clone())?
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{Apply, CommandError, Edit, Editor, State, Track, update_selected};
|
use crate::{Apply, CommandError, Edit, Editor, Route, State, Track, TrackInfo, update_selected};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Style<'a> {
|
pub struct Style<'a> {
|
||||||
@@ -14,21 +14,31 @@ impl Apply for Style<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Editor for Style<'_> {
|
impl Style<'_> {
|
||||||
fn track(&mut self, track: &mut Track) -> Edit {
|
fn set(&self, info: &mut TrackInfo) -> Edit {
|
||||||
if let Some(color) = self.color {
|
if let Some(color) = self.color {
|
||||||
track.info.color = Some(color.to_owned());
|
info.color = Some(color.to_owned());
|
||||||
}
|
}
|
||||||
if self.opacity.is_some() {
|
if self.opacity.is_some() {
|
||||||
track.info.opacity = self.opacity;
|
info.opacity = self.opacity;
|
||||||
}
|
}
|
||||||
if self.width.is_some() {
|
if self.width.is_some() {
|
||||||
track.info.width = self.width;
|
info.width = self.width;
|
||||||
}
|
}
|
||||||
Edit::Changed
|
Edit::Changed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Editor for Style<'_> {
|
||||||
|
fn route(&mut self, route: &mut Route) -> Edit {
|
||||||
|
self.set(&mut route.info)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn track(&mut self, track: &mut Track) -> Edit {
|
||||||
|
self.set(&mut track.info)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::rc::Rc;
|
|||||||
use crate::{Apply, CommandError, FileId, Files, Selection, State, Waypoint};
|
use crate::{Apply, CommandError, FileId, Files, Selection, State, Waypoint};
|
||||||
|
|
||||||
/// Deletes the selected elements. With `whole_files`, the files holding the selected elements
|
/// Deletes the selected elements. With `whole_files`, the files holding the selected elements
|
||||||
/// are deleted instead, even if only a track or a waypoint is selected.
|
/// are deleted instead, even if only a route, a track or a waypoint is selected.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Delete {
|
pub struct Delete {
|
||||||
pub whole_files: bool,
|
pub whole_files: bool,
|
||||||
@@ -23,6 +23,21 @@ impl Apply for Delete {
|
|||||||
state.order.0.retain(|id| !file_ids.contains(id));
|
state.order.0.retain(|id| !file_ids.contains(id));
|
||||||
Selection::Empty
|
Selection::Empty
|
||||||
}
|
}
|
||||||
|
Selection::Route { file_id, rte_ids } => {
|
||||||
|
let file = state
|
||||||
|
.files
|
||||||
|
.get_mut(file_id)
|
||||||
|
.ok_or(CommandError::NothingToDo)?;
|
||||||
|
let file = Rc::make_mut(file);
|
||||||
|
let len = file.rte.len();
|
||||||
|
file.rte.retain(|rte| !rte_ids.contains(&rte.id));
|
||||||
|
if file.rte.len() == len {
|
||||||
|
return Err(CommandError::NothingToDo);
|
||||||
|
}
|
||||||
|
Selection::File {
|
||||||
|
file_ids: [*file_id].into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
Selection::Track { file_id, trk_ids } => {
|
Selection::Track { file_id, trk_ids } => {
|
||||||
let file = state
|
let file = state
|
||||||
.files
|
.files
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use std::{collections::HashSet, rc::Rc};
|
use std::{collections::HashSet, rc::Rc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Apply, CommandError, FileId, Files, Selection, State, Waypoint, copy_file, copy_segment,
|
Apply, CommandError, FileId, Files, Selection, State, Waypoint, copy_file, copy_route,
|
||||||
copy_track, copy_waypoint,
|
copy_segment, copy_track, copy_waypoint,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -35,6 +35,30 @@ impl Apply for Duplicate {
|
|||||||
state.order.0.splice(at..at, order);
|
state.order.0.splice(at..at, order);
|
||||||
Selection::File { file_ids: copies }
|
Selection::File { file_ids: copies }
|
||||||
}
|
}
|
||||||
|
Selection::Route { file_id, rte_ids } => {
|
||||||
|
let file = state
|
||||||
|
.files
|
||||||
|
.get_mut(file_id)
|
||||||
|
.ok_or(CommandError::NothingToDo)?;
|
||||||
|
let file = Rc::make_mut(file);
|
||||||
|
let mut copies = HashSet::new();
|
||||||
|
append_copies(
|
||||||
|
&mut file.rte,
|
||||||
|
|rte| rte_ids.contains(&rte.id),
|
||||||
|
|rte| {
|
||||||
|
let copy = copy_route(rte);
|
||||||
|
copies.insert(copy.id);
|
||||||
|
copy
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if copies.is_empty() {
|
||||||
|
return Err(CommandError::NothingToDo);
|
||||||
|
}
|
||||||
|
Selection::Route {
|
||||||
|
file_id: *file_id,
|
||||||
|
rte_ids: copies,
|
||||||
|
}
|
||||||
|
}
|
||||||
Selection::Track { file_id, trk_ids } => {
|
Selection::Track { file_id, trk_ids } => {
|
||||||
let file = state
|
let file = state
|
||||||
.files
|
.files
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ use crate::{
|
|||||||
pub enum MoveTarget {
|
pub enum MoveTarget {
|
||||||
/// The list of files.
|
/// The list of files.
|
||||||
Files { index: usize },
|
Files { index: usize },
|
||||||
|
/// The routes of a file.
|
||||||
|
Routes { file_id: FileId, index: usize },
|
||||||
/// The tracks of a file.
|
/// The tracks of a file.
|
||||||
Tracks { file_id: FileId, index: usize },
|
Tracks { file_id: FileId, index: usize },
|
||||||
/// The segments of a track.
|
/// The segments of a track.
|
||||||
@@ -26,9 +28,10 @@ pub enum MoveTarget {
|
|||||||
///
|
///
|
||||||
/// What can go where:
|
/// What can go where:
|
||||||
/// - files: among the files;
|
/// - files: among the files;
|
||||||
/// - tracks: among the files (each track becomes a file), or among the tracks of a file;
|
/// - routes and tracks: among the files (each one becomes a file), or among the routes or the
|
||||||
/// - segments: among the files (each segment becomes a file), among the tracks of a file (each
|
/// tracks of a file, where they keep their kind (see [`Destination`]);
|
||||||
/// segment becomes a track), or among the segments of a track;
|
/// - segments: among the files (each segment becomes a file), among the routes or the tracks of a
|
||||||
|
/// file (each segment becomes a route or a track), or among the segments of a track;
|
||||||
/// - waypoints, or all the waypoints of a file (the waypoints node): among the waypoints of a
|
/// - waypoints, or all the waypoints of a file (the waypoints node): among the waypoints of a
|
||||||
/// file.
|
/// file.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -41,6 +44,9 @@ impl From<MoveTarget> for Destination {
|
|||||||
fn from(target: MoveTarget) -> Self {
|
fn from(target: MoveTarget) -> Self {
|
||||||
match target {
|
match target {
|
||||||
MoveTarget::Files { index } => Destination::Files(Place::Index(index)),
|
MoveTarget::Files { index } => Destination::Files(Place::Index(index)),
|
||||||
|
MoveTarget::Routes { file_id, index } => {
|
||||||
|
Destination::Routes(file_id, Place::Index(index))
|
||||||
|
}
|
||||||
MoveTarget::Tracks { file_id, index } => {
|
MoveTarget::Tracks { file_id, index } => {
|
||||||
Destination::Tracks(file_id, Place::Index(index))
|
Destination::Tracks(file_id, Place::Index(index))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,21 @@
|
|||||||
use std::{collections::HashSet, hash::Hash};
|
use std::{collections::HashSet, hash::Hash};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Apply, ClipboardContent, CommandError, Destination, FileId, Place, Selection, State, TrackId,
|
Apply, ClipboardContent, CommandError, Destination, FileId, Place, RouteId, Selection, State,
|
||||||
TrackSegmentId, WaypointId, transfer, waypoint_ids,
|
TrackId, TrackSegmentId, WaypointId, transfer, waypoint_ids,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Pastes what was copied or cut (see [`crate::Clipboard`]).
|
/// Pastes what was copied or cut (see [`crate::Clipboard`]).
|
||||||
///
|
///
|
||||||
/// Where it goes depends on the selection, see [`crate::Clipboard::can_paste`]:
|
/// Where it goes depends on the selection, see [`crate::Clipboard::can_paste`]:
|
||||||
/// - nothing selected: the files are copied, and tracks and segments become new files (named
|
/// - nothing selected: the files are copied, and routes, tracks and segments become new files
|
||||||
/// like their track, or like the file they come from);
|
/// (named like their route or track, or like the file they come from);
|
||||||
/// - a file: the tracks are added to it, segments become new tracks in it, and the waypoints are
|
/// - a file: the routes and tracks are added to it, segments become new tracks in it, and the
|
||||||
/// added to its own;
|
/// waypoints are added to its own;
|
||||||
/// - a track: the segments are added to it, tracks go after it in its file;
|
/// - a route: routes, and segments as new routes, go after it in its file, tracks go before the
|
||||||
|
/// other tracks (right after the routes);
|
||||||
|
/// - a track: the segments are added to it, tracks go after it in its file, and routes after the
|
||||||
|
/// other routes;
|
||||||
/// - a segment: the segments go after it in its track;
|
/// - a segment: the segments go after it in its track;
|
||||||
/// - the waypoints or a waypoint: the waypoints are added to their file, after the waypoint.
|
/// - the waypoints or a waypoint: the waypoints are added to their file, after the waypoint.
|
||||||
///
|
///
|
||||||
@@ -46,6 +49,14 @@ impl Apply for Paste {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn route_ids_of(state: &State, file_id: FileId) -> Vec<RouteId> {
|
||||||
|
state
|
||||||
|
.files
|
||||||
|
.get(&file_id)
|
||||||
|
.map(|file| file.rte.iter().map(|rte| rte.id).collect())
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
fn track_ids_of(state: &State, file_id: FileId) -> Vec<TrackId> {
|
fn track_ids_of(state: &State, file_id: FileId) -> Vec<TrackId> {
|
||||||
state
|
state
|
||||||
.files
|
.files
|
||||||
@@ -79,14 +90,34 @@ fn destination(state: &State, content: &ClipboardContent) -> Result<Destination,
|
|||||||
};
|
};
|
||||||
Ok(match (content, &*state.selection) {
|
Ok(match (content, &*state.selection) {
|
||||||
(ClipboardContent::Files(_), _) => Destination::Files(Place::End),
|
(ClipboardContent::Files(_), _) => Destination::Files(Place::End),
|
||||||
(ClipboardContent::Tracks(_) | ClipboardContent::Segments(_), Selection::Empty) => {
|
(
|
||||||
Destination::Files(Place::End)
|
ClipboardContent::Routes(_)
|
||||||
|
| ClipboardContent::Tracks(_)
|
||||||
|
| ClipboardContent::Segments(_),
|
||||||
|
Selection::Empty,
|
||||||
|
) => Destination::Files(Place::End),
|
||||||
|
(ClipboardContent::Routes(_), Selection::File { file_ids }) => {
|
||||||
|
Destination::Routes(last_file(file_ids)?, Place::End)
|
||||||
|
}
|
||||||
|
// routes and tracks keep their kind (see `Destination`)
|
||||||
|
(
|
||||||
|
ClipboardContent::Routes(_)
|
||||||
|
| ClipboardContent::Tracks(_)
|
||||||
|
| ClipboardContent::Segments(_),
|
||||||
|
Selection::Route { file_id, rte_ids },
|
||||||
|
) => {
|
||||||
|
let last =
|
||||||
|
last_selected(route_ids_of(state, *file_id).into_iter(), rte_ids).ok_or(nothing)?;
|
||||||
|
Destination::Routes(*file_id, Place::After(last))
|
||||||
}
|
}
|
||||||
(
|
(
|
||||||
ClipboardContent::Tracks(_) | ClipboardContent::Segments(_),
|
ClipboardContent::Tracks(_) | ClipboardContent::Segments(_),
|
||||||
Selection::File { file_ids },
|
Selection::File { file_ids },
|
||||||
) => Destination::Tracks(last_file(file_ids)?, Place::End),
|
) => Destination::Tracks(last_file(file_ids)?, Place::End),
|
||||||
(ClipboardContent::Tracks(_), Selection::Track { file_id, trk_ids }) => {
|
(
|
||||||
|
ClipboardContent::Routes(_) | ClipboardContent::Tracks(_),
|
||||||
|
Selection::Track { file_id, trk_ids },
|
||||||
|
) => {
|
||||||
let last =
|
let last =
|
||||||
last_selected(track_ids_of(state, *file_id).into_iter(), trk_ids).ok_or(nothing)?;
|
last_selected(track_ids_of(state, *file_id).into_iter(), trk_ids).ok_or(nothing)?;
|
||||||
Destination::Tracks(*file_id, Place::After(last))
|
Destination::Tracks(*file_id, Place::After(last))
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
use std::{collections::HashSet, rc::Rc};
|
use std::{collections::HashSet, rc::Rc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ClipboardContent, ClipboardSegment, ClipboardTrack, CommandError, File, FileId, Selection,
|
ClipboardContent, ClipboardRoute, ClipboardSegment, ClipboardTrack, CommandError, File, FileId,
|
||||||
State, Track, TrackId, TrackSegment, TrackSegmentId, Waypoint, WaypointId, copy_file,
|
Route, RouteId, Selection, State, Track, TrackId, TrackSegment, TrackSegmentId, Waypoint,
|
||||||
copy_segment, copy_track, copy_waypoint,
|
WaypointId, copy_file, copy_route, copy_segment, copy_track, copy_waypoint,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Where elements are put, among the others of the same list.
|
/// Where elements are put, among the others of the same list.
|
||||||
@@ -37,15 +37,20 @@ impl<T: Copy + PartialEq> Place<T> {
|
|||||||
|
|
||||||
/// Where elements go. What can go where:
|
/// Where elements go. What can go where:
|
||||||
/// - files: among the files;
|
/// - files: among the files;
|
||||||
|
/// - routes: among the files (each route becomes a file, named like the route, or like the file
|
||||||
|
/// it comes from), or among the routes of a file. Among the tracks of a file, they stay routes,
|
||||||
|
/// which come before the tracks: they go after the other routes;
|
||||||
/// - tracks: among the files (each track becomes a file, named like the track, or like the file
|
/// - tracks: among the files (each track becomes a file, named like the track, or like the file
|
||||||
/// it comes from), or among the tracks of a file;
|
/// it comes from), or among the tracks of a file. Among the routes of a file, they stay tracks,
|
||||||
|
/// which come after the routes: they go before the other tracks;
|
||||||
/// - segments: among the files (each segment becomes a file with a track, named like its track,
|
/// - segments: among the files (each segment becomes a file with a track, named like its track,
|
||||||
/// or like its file), among the tracks of a file (each segment becomes a track), or among the
|
/// or like its file), among the routes or the tracks of a file (each segment becomes a route or
|
||||||
/// segments of a track;
|
/// a track), or among the segments of a track;
|
||||||
/// - waypoints: among the waypoints of a file.
|
/// - waypoints: among the waypoints of a file.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum Destination {
|
pub enum Destination {
|
||||||
Files(Place<FileId>),
|
Files(Place<FileId>),
|
||||||
|
Routes(FileId, Place<RouteId>),
|
||||||
Tracks(FileId, Place<TrackId>),
|
Tracks(FileId, Place<TrackId>),
|
||||||
Segments(FileId, TrackId, Place<TrackSegmentId>),
|
Segments(FileId, TrackId, Place<TrackSegmentId>),
|
||||||
Waypoints(FileId, Place<WaypointId>),
|
Waypoints(FileId, Place<WaypointId>),
|
||||||
@@ -93,6 +98,14 @@ pub fn remove_elements(state: &mut State, content: &ClipboardContent) {
|
|||||||
state.files.retain(|id, _| !ids.contains(id));
|
state.files.retain(|id, _| !ids.contains(id));
|
||||||
state.order.0.retain(|id| !ids.contains(id));
|
state.order.0.retain(|id| !ids.contains(id));
|
||||||
}
|
}
|
||||||
|
ClipboardContent::Routes(routes) => {
|
||||||
|
let ids: HashSet<RouteId> = routes.iter().map(|rte| rte.route.id).collect();
|
||||||
|
for file in state.files.values_mut() {
|
||||||
|
if file.rte.iter().any(|rte| ids.contains(&rte.id)) {
|
||||||
|
Rc::make_mut(file).rte.retain(|rte| !ids.contains(&rte.id));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
ClipboardContent::Tracks(tracks) => {
|
ClipboardContent::Tracks(tracks) => {
|
||||||
let ids: HashSet<TrackId> = tracks.iter().map(|trk| trk.track.id).collect();
|
let ids: HashSet<TrackId> = tracks.iter().map(|trk| trk.track.id).collect();
|
||||||
for file in state.files.values_mut() {
|
for file in state.files.values_mut() {
|
||||||
@@ -141,7 +154,9 @@ pub fn transfer(
|
|||||||
// the destination has to be there, before anything is changed
|
// the destination has to be there, before anything is changed
|
||||||
let exists = match to {
|
let exists = match to {
|
||||||
Destination::Files(_) => true,
|
Destination::Files(_) => true,
|
||||||
Destination::Tracks(file_id, _) => state.files.contains_key(&file_id),
|
Destination::Routes(file_id, _) | Destination::Tracks(file_id, _) => {
|
||||||
|
state.files.contains_key(&file_id)
|
||||||
|
}
|
||||||
Destination::Segments(file_id, trk_id, _) => state
|
Destination::Segments(file_id, trk_id, _) => state
|
||||||
.files
|
.files
|
||||||
.get(&file_id)
|
.get(&file_id)
|
||||||
@@ -152,17 +167,21 @@ pub fn transfer(
|
|||||||
(content, to),
|
(content, to),
|
||||||
(ClipboardContent::Files(_), Destination::Files(_))
|
(ClipboardContent::Files(_), Destination::Files(_))
|
||||||
| (
|
| (
|
||||||
ClipboardContent::Tracks(_),
|
ClipboardContent::Routes(_) | ClipboardContent::Tracks(_),
|
||||||
Destination::Files(_) | Destination::Tracks(..)
|
Destination::Files(_) | Destination::Routes(..) | Destination::Tracks(..)
|
||||||
)
|
)
|
||||||
| (
|
| (
|
||||||
ClipboardContent::Segments(_),
|
ClipboardContent::Segments(_),
|
||||||
Destination::Files(_) | Destination::Tracks(..) | Destination::Segments(..),
|
Destination::Files(_)
|
||||||
|
| Destination::Routes(..)
|
||||||
|
| Destination::Tracks(..)
|
||||||
|
| Destination::Segments(..),
|
||||||
)
|
)
|
||||||
| (ClipboardContent::Waypoints(_), Destination::Waypoints(..))
|
| (ClipboardContent::Waypoints(_), Destination::Waypoints(..))
|
||||||
);
|
);
|
||||||
let empty = match content {
|
let empty = match content {
|
||||||
ClipboardContent::Files(files) => files.is_empty(),
|
ClipboardContent::Files(files) => files.is_empty(),
|
||||||
|
ClipboardContent::Routes(routes) => routes.is_empty(),
|
||||||
ClipboardContent::Tracks(tracks) => tracks.is_empty(),
|
ClipboardContent::Tracks(tracks) => tracks.is_empty(),
|
||||||
ClipboardContent::Segments(segments) => segments.is_empty(),
|
ClipboardContent::Segments(segments) => segments.is_empty(),
|
||||||
ClipboardContent::Waypoints(waypoints) => waypoints.is_empty(),
|
ClipboardContent::Waypoints(waypoints) => waypoints.is_empty(),
|
||||||
@@ -189,6 +208,7 @@ pub fn transfer(
|
|||||||
.collect();
|
.collect();
|
||||||
Ok(add_files(state, files, place))
|
Ok(add_files(state, files, place))
|
||||||
}
|
}
|
||||||
|
(ClipboardContent::Routes(routes), to) => transfer_routes(state, routes, copy, to),
|
||||||
(ClipboardContent::Tracks(tracks), to) => transfer_tracks(state, tracks, copy, to),
|
(ClipboardContent::Tracks(tracks), to) => transfer_tracks(state, tracks, copy, to),
|
||||||
(ClipboardContent::Segments(segments), to) => transfer_segments(state, segments, copy, to),
|
(ClipboardContent::Segments(segments), to) => transfer_segments(state, segments, copy, to),
|
||||||
(ClipboardContent::Waypoints(waypoints), Destination::Waypoints(file_id, place)) => {
|
(ClipboardContent::Waypoints(waypoints), Destination::Waypoints(file_id, place)) => {
|
||||||
@@ -198,6 +218,89 @@ pub fn transfer(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Puts the routes among the routes of a file, at `place`, and gives the selection of them.
|
||||||
|
fn insert_routes(
|
||||||
|
state: &mut State,
|
||||||
|
file_id: FileId,
|
||||||
|
place: Place<RouteId>,
|
||||||
|
routes: Vec<Route>,
|
||||||
|
) -> Result<Selection, CommandError> {
|
||||||
|
let ids: HashSet<RouteId> = routes.iter().map(|rte| rte.id).collect();
|
||||||
|
let file = file_mut(state, file_id)?;
|
||||||
|
let existing: Vec<RouteId> = file.rte.iter().map(|rte| rte.id).collect();
|
||||||
|
let at = place.position(&existing);
|
||||||
|
file.rte.splice(at..at, routes);
|
||||||
|
Ok(Selection::Route {
|
||||||
|
file_id,
|
||||||
|
rte_ids: ids,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transfer_routes(
|
||||||
|
state: &mut State,
|
||||||
|
routes: &[ClipboardRoute],
|
||||||
|
copy: bool,
|
||||||
|
to: Destination,
|
||||||
|
) -> Result<Selection, CommandError> {
|
||||||
|
let routes: Vec<(Route, &str)> = routes
|
||||||
|
.iter()
|
||||||
|
.map(|clip| {
|
||||||
|
let route = if copy {
|
||||||
|
copy_route(&clip.route)
|
||||||
|
} else {
|
||||||
|
clip.route.clone()
|
||||||
|
};
|
||||||
|
(route, clip.file_name.as_str())
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
match to {
|
||||||
|
Destination::Files(place) => {
|
||||||
|
let files = routes
|
||||||
|
.into_iter()
|
||||||
|
.map(|(route, file_name)| {
|
||||||
|
let name = route
|
||||||
|
.info
|
||||||
|
.name
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| file_name.to_owned());
|
||||||
|
Rc::new(File {
|
||||||
|
rte: vec![route],
|
||||||
|
..new_file(&name, vec![])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
Ok(add_files(state, files, place))
|
||||||
|
}
|
||||||
|
Destination::Routes(file_id, place) => {
|
||||||
|
let routes = routes.into_iter().map(|(route, _)| route).collect();
|
||||||
|
insert_routes(state, file_id, place, routes)
|
||||||
|
}
|
||||||
|
Destination::Tracks(file_id, _) => {
|
||||||
|
let routes = routes.into_iter().map(|(route, _)| route).collect();
|
||||||
|
insert_routes(state, file_id, Place::End, routes)
|
||||||
|
}
|
||||||
|
_ => Err(CommandError::NothingToDo),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Puts the tracks among the tracks of a file, at `place`, and gives the selection of them.
|
||||||
|
fn insert_tracks(
|
||||||
|
state: &mut State,
|
||||||
|
file_id: FileId,
|
||||||
|
place: Place<TrackId>,
|
||||||
|
tracks: Vec<Track>,
|
||||||
|
) -> Result<Selection, CommandError> {
|
||||||
|
let ids: HashSet<TrackId> = tracks.iter().map(|trk| trk.id).collect();
|
||||||
|
let file = file_mut(state, file_id)?;
|
||||||
|
let existing: Vec<TrackId> = file.trk.iter().map(|trk| trk.id).collect();
|
||||||
|
let at = place.position(&existing);
|
||||||
|
file.trk.splice(at..at, tracks);
|
||||||
|
Ok(Selection::Track {
|
||||||
|
file_id,
|
||||||
|
trk_ids: ids,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn transfer_tracks(
|
fn transfer_tracks(
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
tracks: &[ClipboardTrack],
|
tracks: &[ClipboardTrack],
|
||||||
@@ -231,16 +334,12 @@ fn transfer_tracks(
|
|||||||
Ok(add_files(state, files, place))
|
Ok(add_files(state, files, place))
|
||||||
}
|
}
|
||||||
Destination::Tracks(file_id, place) => {
|
Destination::Tracks(file_id, place) => {
|
||||||
let tracks: Vec<Track> = tracks.into_iter().map(|(track, _)| track).collect();
|
let tracks = tracks.into_iter().map(|(track, _)| track).collect();
|
||||||
let ids: HashSet<TrackId> = tracks.iter().map(|trk| trk.id).collect();
|
insert_tracks(state, file_id, place, tracks)
|
||||||
let file = file_mut(state, file_id)?;
|
}
|
||||||
let existing: Vec<TrackId> = file.trk.iter().map(|trk| trk.id).collect();
|
Destination::Routes(file_id, _) => {
|
||||||
let at = place.position(&existing);
|
let tracks = tracks.into_iter().map(|(track, _)| track).collect();
|
||||||
file.trk.splice(at..at, tracks);
|
insert_tracks(state, file_id, Place::Index(0), tracks)
|
||||||
Ok(Selection::Track {
|
|
||||||
file_id,
|
|
||||||
trk_ids: ids,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
_ => Err(CommandError::NothingToDo),
|
_ => Err(CommandError::NothingToDo),
|
||||||
}
|
}
|
||||||
@@ -276,20 +375,22 @@ fn transfer_segments(
|
|||||||
.collect();
|
.collect();
|
||||||
Ok(add_files(state, files, place))
|
Ok(add_files(state, files, place))
|
||||||
}
|
}
|
||||||
|
Destination::Routes(file_id, place) => {
|
||||||
|
let routes = segments
|
||||||
|
.into_iter()
|
||||||
|
.map(|(seg, _)| Route {
|
||||||
|
seg,
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
insert_routes(state, file_id, place, routes)
|
||||||
|
}
|
||||||
Destination::Tracks(file_id, place) => {
|
Destination::Tracks(file_id, place) => {
|
||||||
let tracks: Vec<Track> = segments
|
let tracks = segments
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(segment, _)| own_track(segment))
|
.map(|(segment, _)| own_track(segment))
|
||||||
.collect();
|
.collect();
|
||||||
let ids = tracks.iter().map(|trk| trk.id).collect();
|
insert_tracks(state, file_id, place, tracks)
|
||||||
let file = file_mut(state, file_id)?;
|
|
||||||
let existing: Vec<TrackId> = file.trk.iter().map(|trk| trk.id).collect();
|
|
||||||
let at = place.position(&existing);
|
|
||||||
file.trk.splice(at..at, tracks);
|
|
||||||
Ok(Selection::Track {
|
|
||||||
file_id,
|
|
||||||
trk_ids: ids,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
Destination::Segments(file_id, trk_id, place) => {
|
Destination::Segments(file_id, trk_id, place) => {
|
||||||
let segments: Vec<TrackSegment> =
|
let segments: Vec<TrackSegment> =
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ mod file;
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub(crate) mod fixture;
|
pub(crate) mod fixture;
|
||||||
mod pattern;
|
mod pattern;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod routes_tests;
|
||||||
mod tools;
|
mod tools;
|
||||||
|
|
||||||
pub use apply::*;
|
pub use apply::*;
|
||||||
@@ -33,11 +35,12 @@ pub enum Command<'a> {
|
|||||||
DeleteWaypoint(DeleteWaypoint),
|
DeleteWaypoint(DeleteWaypoint),
|
||||||
Metadata(Metadata<'a>),
|
Metadata(Metadata<'a>),
|
||||||
Style(Style<'a>),
|
Style(Style<'a>),
|
||||||
|
NewRoute(NewRoute),
|
||||||
NewTrack(NewTrack),
|
NewTrack(NewTrack),
|
||||||
NewTrackSegment(NewTrackSegment),
|
NewTrackSegment(NewTrackSegment),
|
||||||
Reverse(Reverse),
|
Reverse(Reverse),
|
||||||
RoundTrip(RoundTrip),
|
RoundTrip(RoundTrip),
|
||||||
Route(Route<'a>),
|
Reroute(Reroute<'a>),
|
||||||
NewWaypoint(NewWaypoint<'a>),
|
NewWaypoint(NewWaypoint<'a>),
|
||||||
EditWaypoint(EditWaypoint<'a>),
|
EditWaypoint(EditWaypoint<'a>),
|
||||||
MoveWaypoint(MoveWaypoint),
|
MoveWaypoint(MoveWaypoint),
|
||||||
@@ -68,11 +71,12 @@ impl Apply for Command<'_> {
|
|||||||
Command::DeleteWaypoint(c) => c.apply(state),
|
Command::DeleteWaypoint(c) => c.apply(state),
|
||||||
Command::Metadata(c) => c.apply(state),
|
Command::Metadata(c) => c.apply(state),
|
||||||
Command::Style(c) => c.apply(state),
|
Command::Style(c) => c.apply(state),
|
||||||
|
Command::NewRoute(c) => c.apply(state),
|
||||||
Command::NewTrack(c) => c.apply(state),
|
Command::NewTrack(c) => c.apply(state),
|
||||||
Command::NewTrackSegment(c) => c.apply(state),
|
Command::NewTrackSegment(c) => c.apply(state),
|
||||||
Command::Reverse(c) => c.apply(state),
|
Command::Reverse(c) => c.apply(state),
|
||||||
Command::RoundTrip(c) => c.apply(state),
|
Command::RoundTrip(c) => c.apply(state),
|
||||||
Command::Route(c) => c.apply(state),
|
Command::Reroute(c) => c.apply(state),
|
||||||
Command::NewWaypoint(c) => c.apply(state),
|
Command::NewWaypoint(c) => c.apply(state),
|
||||||
Command::EditWaypoint(c) => c.apply(state),
|
Command::EditWaypoint(c) => c.apply(state),
|
||||||
Command::MoveWaypoint(c) => c.apply(state),
|
Command::MoveWaypoint(c) => c.apply(state),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{File, Track, TrackSegment, Waypoint};
|
use crate::{File, Route, Track, TrackSegment, Waypoint};
|
||||||
|
|
||||||
// The trackpoints of a segment are shared chunks, so copying elements is cheap.
|
// The trackpoints of a segment are shared chunks, so copying elements is cheap.
|
||||||
|
|
||||||
@@ -18,10 +18,20 @@ pub fn copy_track(track: &Track) -> Track {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A copy of the file and of its tracks and segments, with new ids.
|
/// A copy of the route and of its segment, with new ids.
|
||||||
|
pub fn copy_route(route: &Route) -> Route {
|
||||||
|
Route {
|
||||||
|
id: Default::default(),
|
||||||
|
seg: copy_segment(&route.seg),
|
||||||
|
..route.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A copy of the file and of its routes, tracks and segments, with new ids.
|
||||||
pub fn copy_file(file: &File) -> File {
|
pub fn copy_file(file: &File) -> File {
|
||||||
File {
|
File {
|
||||||
id: Default::default(),
|
id: Default::default(),
|
||||||
|
rte: file.rte.iter().map(copy_route).collect(),
|
||||||
trk: file.trk.iter().map(copy_track).collect(),
|
trk: file.trk.iter().map(copy_track).collect(),
|
||||||
..file.clone()
|
..file.clone()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ pub fn update_segments(
|
|||||||
let file = files
|
let file = files
|
||||||
.entry(location.file_id)
|
.entry(location.file_id)
|
||||||
.or_insert_with(|| (*state.files[&location.file_id]).clone());
|
.or_insert_with(|| (*state.files[&location.file_id]).clone());
|
||||||
f(i, &mut file.trk[location.trk].trkseg[location.seg]);
|
f(i, file.segment_mut(location.index));
|
||||||
}
|
}
|
||||||
for (file_id, file) in files {
|
for (file_id, file) in files {
|
||||||
state.files.insert(file_id, Rc::new(file));
|
state.files.insert(file_id, Rc::new(file));
|
||||||
@@ -31,6 +31,8 @@ mod tests {
|
|||||||
|
|
||||||
use crate::engine::command::fixture::Fixture;
|
use crate::engine::command::fixture::Fixture;
|
||||||
|
|
||||||
|
use crate::SegmentIndex;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn fixture() -> (Fixture, FileId) {
|
fn fixture() -> (Fixture, FileId) {
|
||||||
@@ -49,13 +51,11 @@ mod tests {
|
|||||||
let locations = [
|
let locations = [
|
||||||
SegmentLocation {
|
SegmentLocation {
|
||||||
file_id: id,
|
file_id: id,
|
||||||
trk: 1,
|
index: SegmentIndex::Track { trk: 1, seg: 0 },
|
||||||
seg: 0,
|
|
||||||
},
|
},
|
||||||
SegmentLocation {
|
SegmentLocation {
|
||||||
file_id: id,
|
file_id: id,
|
||||||
trk: 0,
|
index: SegmentIndex::Track { trk: 0, seg: 0 },
|
||||||
seg: 0,
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
let mut calls = vec![];
|
let mut calls = vec![];
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::{File, FileId, Files, Selection, State, Track, TrackSegment, Waypoint};
|
use crate::{File, FileId, Files, Route, Selection, State, Track, TrackSegment, Waypoint};
|
||||||
|
|
||||||
/// What an [`Editor`] hook did to the element it was given.
|
/// What an [`Editor`] hook did to the element it was given.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
@@ -21,13 +21,19 @@ impl Edit {
|
|||||||
|
|
||||||
/// Independent edit of each selected element, without needing the rest of the state.
|
/// Independent edit of each selected element, without needing the rest of the state.
|
||||||
///
|
///
|
||||||
/// A selected file, track or segment is passed to the hook of its own level. By default a
|
/// A selected file, route, track or segment is passed to the hook of its own level. By default
|
||||||
/// file is forwarded to its tracks and a track to its segments, so an editor only overrides
|
/// a file is forwarded to its routes and tracks, and a route or a track to its segments, so an
|
||||||
/// the level(s) it cares about (metadata: file and track, style: track, reverse: segment).
|
/// editor only overrides the level(s) it cares about (metadata: file, route and track, style:
|
||||||
/// Waypoints are only edited when selected, never through their file.
|
/// route and track, reverse: segment). Waypoints are only edited when selected, never through
|
||||||
|
/// their file.
|
||||||
pub trait Editor {
|
pub trait Editor {
|
||||||
fn file(&mut self, file: &mut File) -> Edit {
|
fn file(&mut self, file: &mut File) -> Edit {
|
||||||
edit_each(&mut file.trk, |trk| self.track(trk))
|
let routes = edit_each(&mut file.rte, |rte| self.route(rte));
|
||||||
|
routes.merge(edit_each(&mut file.trk, |trk| self.track(trk)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn route(&mut self, route: &mut Route) -> Edit {
|
||||||
|
edit_segment(self, &mut route.seg)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn track(&mut self, track: &mut Track) -> Edit {
|
fn track(&mut self, track: &mut Track) -> Edit {
|
||||||
@@ -95,7 +101,7 @@ fn edit_waypoints<E: Editor + ?Sized>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Applies the editor to every selected file, track or segment.
|
/// Applies the editor to every selected file, route, track or segment.
|
||||||
pub fn update_selected<E: Editor>(state: &mut State, editor: &mut E) {
|
pub fn update_selected<E: Editor>(state: &mut State, editor: &mut E) {
|
||||||
match &*state.selection {
|
match &*state.selection {
|
||||||
Selection::File { file_ids } => {
|
Selection::File { file_ids } => {
|
||||||
@@ -103,6 +109,15 @@ pub fn update_selected<E: Editor>(state: &mut State, editor: &mut E) {
|
|||||||
update_file(state.files, *id, |file| editor.file(file));
|
update_file(state.files, *id, |file| editor.file(file));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Selection::Route { file_id, rte_ids } => {
|
||||||
|
update_file(state.files, *file_id, |file| {
|
||||||
|
edit_where(
|
||||||
|
&mut file.rte,
|
||||||
|
|rte| rte_ids.contains(&rte.id),
|
||||||
|
|rte| editor.route(rte),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
}
|
||||||
Selection::Track { file_id, trk_ids } => {
|
Selection::Track { file_id, trk_ids } => {
|
||||||
update_file(state.files, *file_id, |file| {
|
update_file(state.files, *file_id, |file| {
|
||||||
edit_where(
|
edit_where(
|
||||||
|
|||||||
@@ -0,0 +1,604 @@
|
|||||||
|
//! The commands on routes: they work as on tracks, a route being a track of a single segment.
|
||||||
|
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
Apply, Clean, CleanType, Clipboard, ClipboardContent, CommandError, Crop, Delete, Duplicate,
|
||||||
|
Extract, File, FileId, LngLat, LngLatBounds, Merge, MergeType, Metadata, Move, MoveTarget,
|
||||||
|
Paste, Reverse, RouteId, SegmentIndex, Selection, Split, SplitType, Style,
|
||||||
|
engine::command::fixture::Fixture, parse,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A file with the 2 routes of `with_routes` and the 2 tracks (of 2 segments) of
|
||||||
|
/// `with_tracks_and_segments`, which is the only file and is in the order.
|
||||||
|
fn fixture() -> (Fixture, FileId) {
|
||||||
|
let read = |name: &str| {
|
||||||
|
let data = std::fs::read(format!("data/{name}.gpx")).unwrap();
|
||||||
|
parse(&data, &mut Default::default()).unwrap()
|
||||||
|
};
|
||||||
|
let mut file = read("with_tracks_and_segments");
|
||||||
|
file.rte = read("with_routes").rte;
|
||||||
|
assert_eq!((file.rte.len(), file.trk.len()), (2, 2));
|
||||||
|
let mut fx = Fixture::default();
|
||||||
|
let id = file.id;
|
||||||
|
fx.files.insert(id, Rc::new(file));
|
||||||
|
fx.order.0.push(id);
|
||||||
|
(fx, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn file(fx: &Fixture, id: FileId) -> &File {
|
||||||
|
&fx.files[&id]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn routes(fx: &Fixture, id: FileId, indices: &[usize]) -> Selection {
|
||||||
|
Selection::Route {
|
||||||
|
file_id: id,
|
||||||
|
rte_ids: indices.iter().map(|i| file(fx, id).rte[*i].id).collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn whole(id: FileId) -> Selection {
|
||||||
|
Selection::File {
|
||||||
|
file_ids: [id].into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lengths(file: &File) -> Vec<usize> {
|
||||||
|
file.segments().map(|segment| segment.len()).collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_the_segments_of_a_file_are_the_ones_of_its_routes_then_its_tracks() {
|
||||||
|
let (fx, id) = fixture();
|
||||||
|
let file = file(&fx, id);
|
||||||
|
let locations = whole(id).segment_locations(&fx.files, &fx.order.0);
|
||||||
|
let indices: Vec<SegmentIndex> = locations.iter().map(|l| l.index).collect();
|
||||||
|
assert_eq!(
|
||||||
|
indices,
|
||||||
|
[
|
||||||
|
SegmentIndex::Route(0),
|
||||||
|
SegmentIndex::Route(1),
|
||||||
|
SegmentIndex::Track { trk: 0, seg: 0 },
|
||||||
|
SegmentIndex::Track { trk: 0, seg: 1 },
|
||||||
|
SegmentIndex::Track { trk: 1, seg: 0 },
|
||||||
|
SegmentIndex::Track { trk: 1, seg: 1 },
|
||||||
|
]
|
||||||
|
);
|
||||||
|
// a selection of routes covers their segments only
|
||||||
|
let selected = routes(&fx, id, &[1]).segment_locations(&fx.files, &fx.order.0);
|
||||||
|
assert_eq!(selected.len(), 1);
|
||||||
|
assert_eq!(selected[0].segment(&fx.files).id, file.rte[1].seg.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_routes_are_selected_like_tracks() {
|
||||||
|
let (fx, id) = fixture();
|
||||||
|
let ids: Vec<RouteId> = file(&fx, id).rte.iter().map(|r| r.id).collect();
|
||||||
|
|
||||||
|
let mut selection = routes(&fx, id, &[0]);
|
||||||
|
selection.extend(routes(&fx, id, &[1]));
|
||||||
|
assert_eq!(selection, routes(&fx, id, &[0, 1]));
|
||||||
|
selection.toggle(routes(&fx, id, &[0]));
|
||||||
|
assert_eq!(selection, routes(&fx, id, &[1]));
|
||||||
|
// routes and tracks are not selected together
|
||||||
|
let track = Selection::Track {
|
||||||
|
file_id: id,
|
||||||
|
trk_ids: [file(&fx, id).trk[0].id].into(),
|
||||||
|
};
|
||||||
|
selection.extend(track.clone());
|
||||||
|
assert_eq!(selection, track);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
routes(&fx, id, &[0]).all_at_level(&fx.files, &fx.order.0),
|
||||||
|
Some(routes(&fx, id, &[0, 1]))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
routes(&fx, id, &[0]).neighbour(&fx.files, &fx.order.0, true),
|
||||||
|
Some(routes(&fx, id, &[1]))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
routes(&fx, id, &[1]).neighbour(&fx.files, &fx.order.0, true),
|
||||||
|
None
|
||||||
|
);
|
||||||
|
assert_eq!(routes(&fx, id, &[1]).to_files(), whole(id));
|
||||||
|
|
||||||
|
let mut gone = Selection::Route {
|
||||||
|
file_id: id,
|
||||||
|
rte_ids: [ids[0], RouteId::default()].into(),
|
||||||
|
};
|
||||||
|
gone.retain_existing(&fx.files);
|
||||||
|
assert_eq!(gone, routes(&fx, id, &[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_delete_and_duplicate_routes() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let kept = file(&fx, id).rte[1].id;
|
||||||
|
fx.selection = routes(&fx, id, &[0]);
|
||||||
|
Delete { whole_files: false }
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(file(&fx, id).rte.len(), 1);
|
||||||
|
assert_eq!(file(&fx, id).rte[0].id, kept);
|
||||||
|
assert_eq!(file(&fx, id).trk.len(), 2);
|
||||||
|
assert_eq!(fx.selection, whole(id));
|
||||||
|
|
||||||
|
fx.selection = routes(&fx, id, &[0]);
|
||||||
|
Duplicate.apply(&mut fx.state()).unwrap();
|
||||||
|
let file = file(&fx, id);
|
||||||
|
assert_eq!(file.rte.len(), 2);
|
||||||
|
assert_ne!(file.rte[1].id, kept);
|
||||||
|
assert_ne!(file.rte[1].seg.id, file.rte[0].seg.id);
|
||||||
|
assert_eq!(file.rte[1].info, file.rte[0].info);
|
||||||
|
assert_eq!(
|
||||||
|
fx.selection,
|
||||||
|
Selection::Route {
|
||||||
|
file_id: id,
|
||||||
|
rte_ids: [file.rte[1].id].into()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_metadata_and_style_of_routes() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
fx.selection = routes(&fx, id, &[1]);
|
||||||
|
Metadata {
|
||||||
|
name: "renamed",
|
||||||
|
desc: "about",
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
Style {
|
||||||
|
color: Some("00ff00"),
|
||||||
|
opacity: None,
|
||||||
|
width: Some(3.0),
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
let file = file(&fx, id);
|
||||||
|
let info = &file.rte[1].info;
|
||||||
|
assert_eq!(info.name.as_deref(), Some("renamed"));
|
||||||
|
assert_eq!(info.desc.as_deref(), Some("about"));
|
||||||
|
assert_eq!(info.color.as_deref(), Some("00ff00"));
|
||||||
|
assert_eq!(info.width, Some(3.0));
|
||||||
|
assert_ne!(file.rte[0].info.name.as_deref(), Some("renamed"));
|
||||||
|
|
||||||
|
// the style of a file is the one of its routes and tracks
|
||||||
|
fx.selection = whole(id);
|
||||||
|
Style {
|
||||||
|
color: Some("ff0000"),
|
||||||
|
opacity: None,
|
||||||
|
width: None,
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
let file = &fx.files[&id];
|
||||||
|
assert!(
|
||||||
|
file.rte
|
||||||
|
.iter()
|
||||||
|
.map(|r| &r.info)
|
||||||
|
.chain(file.trk.iter().map(|t| &t.info))
|
||||||
|
.all(|info| info.color.as_deref() == Some("ff0000"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_copy_and_paste_routes() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let copied = file(&fx, id).rte[0].clone();
|
||||||
|
fx.clipboard = Clipboard::new(&routes(&fx, id, &[0]), &fx.files, &fx.order.0, false);
|
||||||
|
assert!(matches!(
|
||||||
|
fx.clipboard.as_ref().unwrap().content,
|
||||||
|
ClipboardContent::Routes(_)
|
||||||
|
));
|
||||||
|
|
||||||
|
// after the selected route
|
||||||
|
fx.selection = routes(&fx, id, &[1]);
|
||||||
|
Paste.apply(&mut fx.state()).unwrap();
|
||||||
|
let file = file(&fx, id);
|
||||||
|
assert_eq!(file.rte.len(), 3);
|
||||||
|
assert_ne!(file.rte[2].id, copied.id);
|
||||||
|
assert_eq!(file.rte[2].seg.len(), copied.seg.len());
|
||||||
|
assert!(
|
||||||
|
matches!(&fx.selection, Selection::Route { rte_ids, .. } if rte_ids.contains(&file.rte[2].id))
|
||||||
|
);
|
||||||
|
|
||||||
|
// nothing selected: a new file, named like the route
|
||||||
|
fx.selection = Selection::Empty;
|
||||||
|
Paste.apply(&mut fx.state()).unwrap();
|
||||||
|
assert_eq!(fx.order.0.len(), 2);
|
||||||
|
let new = &fx.files[&fx.order.0[1]];
|
||||||
|
assert_eq!(new.rte.len(), 1);
|
||||||
|
assert!(new.trk.is_empty());
|
||||||
|
assert_eq!(Some(&new.info.name), copied.info.name.as_ref());
|
||||||
|
|
||||||
|
// a track selected: among the routes of its file
|
||||||
|
fx.selection = Selection::Track {
|
||||||
|
file_id: id,
|
||||||
|
trk_ids: [fx.files[&id].trk[0].id].into(),
|
||||||
|
};
|
||||||
|
Paste.apply(&mut fx.state()).unwrap();
|
||||||
|
assert_eq!(fx.files[&id].rte.len(), 4);
|
||||||
|
|
||||||
|
// segments do not go in a route, but become routes next to it
|
||||||
|
let file = &fx.files[&id];
|
||||||
|
let track = &file.trk[0];
|
||||||
|
fx.clipboard = Clipboard::new(
|
||||||
|
&Selection::TrackSegment {
|
||||||
|
file_id: id,
|
||||||
|
trk_id: track.id,
|
||||||
|
trkseg_ids: [track.trkseg[1].id].into(),
|
||||||
|
},
|
||||||
|
&fx.files,
|
||||||
|
&fx.order.0,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
fx.selection = routes(&fx, id, &[0]);
|
||||||
|
Paste.apply(&mut fx.state()).unwrap();
|
||||||
|
let file = &fx.files[&id];
|
||||||
|
assert_eq!(file.rte.len(), 5);
|
||||||
|
assert_eq!(file.rte[1].seg.len(), file.trk[0].trkseg[1].len());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cut_tracks_pasted_on_a_route_go_first_among_the_tracks() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let moved = file(&fx, id).trk[1].id;
|
||||||
|
fx.clipboard = Clipboard::new(
|
||||||
|
&Selection::Track {
|
||||||
|
file_id: id,
|
||||||
|
trk_ids: [moved].into(),
|
||||||
|
},
|
||||||
|
&fx.files,
|
||||||
|
&fx.order.0,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
fx.selection = routes(&fx, id, &[0]);
|
||||||
|
Paste.apply(&mut fx.state()).unwrap();
|
||||||
|
let file = file(&fx, id);
|
||||||
|
assert_eq!(file.trk.len(), 2);
|
||||||
|
assert_eq!(file.trk[0].id, moved);
|
||||||
|
assert_eq!(file.rte.len(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_move_routes() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let ids: Vec<RouteId> = file(&fx, id).rte.iter().map(|r| r.id).collect();
|
||||||
|
Move {
|
||||||
|
what: routes(&fx, id, &[1]),
|
||||||
|
to: MoveTarget::Routes {
|
||||||
|
file_id: id,
|
||||||
|
index: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
let order: Vec<RouteId> = file(&fx, id).rte.iter().map(|r| r.id).collect();
|
||||||
|
assert_eq!(order, [ids[1], ids[0]]);
|
||||||
|
|
||||||
|
// to the files: a file of its own
|
||||||
|
Move {
|
||||||
|
what: routes(&fx, id, &[0]),
|
||||||
|
to: MoveTarget::Files { index: 1 },
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(file(&fx, id).rte.len(), 1);
|
||||||
|
let new = &fx.files[&fx.order.0[1]];
|
||||||
|
assert_eq!(new.rte[0].id, ids[1]);
|
||||||
|
|
||||||
|
// a segment moved among the routes becomes one
|
||||||
|
let track = &file(&fx, id).trk[0];
|
||||||
|
let segment = track.trkseg[0].id;
|
||||||
|
Move {
|
||||||
|
what: Selection::TrackSegment {
|
||||||
|
file_id: id,
|
||||||
|
trk_id: track.id,
|
||||||
|
trkseg_ids: [segment].into(),
|
||||||
|
},
|
||||||
|
to: MoveTarget::Routes {
|
||||||
|
file_id: id,
|
||||||
|
index: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
let file = file(&fx, id);
|
||||||
|
assert_eq!(file.rte[1].seg.id, segment);
|
||||||
|
assert_eq!(file.trk[0].trkseg.len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_routes_and_tracks_moved_among_each_other_keep_their_kind() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let route = file(&fx, id).rte[0].id;
|
||||||
|
let track = file(&fx, id).trk[1].id;
|
||||||
|
|
||||||
|
// a route among the tracks: after the other routes, which come first
|
||||||
|
Move {
|
||||||
|
what: routes(&fx, id, &[0]),
|
||||||
|
to: MoveTarget::Tracks {
|
||||||
|
file_id: id,
|
||||||
|
index: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
let moved = file(&fx, id);
|
||||||
|
assert_eq!((moved.rte.len(), moved.trk.len()), (2, 2));
|
||||||
|
assert_eq!(moved.rte[1].id, route);
|
||||||
|
assert_eq!(
|
||||||
|
fx.selection,
|
||||||
|
Selection::Route {
|
||||||
|
file_id: id,
|
||||||
|
rte_ids: [route].into()
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// a track among the routes: before the other tracks, which come after the routes
|
||||||
|
Move {
|
||||||
|
what: Selection::Track {
|
||||||
|
file_id: id,
|
||||||
|
trk_ids: [track].into(),
|
||||||
|
},
|
||||||
|
to: MoveTarget::Routes {
|
||||||
|
file_id: id,
|
||||||
|
index: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
let moved = file(&fx, id);
|
||||||
|
assert_eq!((moved.rte.len(), moved.trk.len()), (2, 2));
|
||||||
|
assert_eq!(moved.trk[0].id, track);
|
||||||
|
|
||||||
|
// and copied routes pasted on a track stay routes
|
||||||
|
fx.clipboard = Clipboard::new(&routes(&fx, id, &[0]), &fx.files, &fx.order.0, false);
|
||||||
|
fx.selection = Selection::Track {
|
||||||
|
file_id: id,
|
||||||
|
trk_ids: [track].into(),
|
||||||
|
};
|
||||||
|
Paste.apply(&mut fx.state()).unwrap();
|
||||||
|
let pasted = file(&fx, id);
|
||||||
|
assert_eq!((pasted.rte.len(), pasted.trk.len()), (3, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_merge_routes() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let before = lengths(file(&fx, id));
|
||||||
|
let first = file(&fx, id).rte[0].id;
|
||||||
|
fx.selection = routes(&fx, id, &[0, 1]);
|
||||||
|
let merge = |type_| Merge {
|
||||||
|
type_,
|
||||||
|
remove_gaps: false,
|
||||||
|
keep_originals: false,
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
merge(MergeType::Group).apply(&mut fx.state()),
|
||||||
|
Err(CommandError::NothingToDo)
|
||||||
|
);
|
||||||
|
merge(MergeType::Connect).apply(&mut fx.state()).unwrap();
|
||||||
|
let file = file(&fx, id);
|
||||||
|
assert_eq!(file.rte.len(), 1);
|
||||||
|
assert_eq!(file.rte[0].id, first);
|
||||||
|
assert_eq!(file.rte[0].seg.len(), before[0] + before[1]);
|
||||||
|
assert_eq!(file.trk.len(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_merge_files_with_routes() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let other = File {
|
||||||
|
rte: file(&fx, id).rte.iter().map(crate::copy_route).collect(),
|
||||||
|
..File::default()
|
||||||
|
};
|
||||||
|
let other_id = other.id;
|
||||||
|
fx.files.insert(other_id, Rc::new(other));
|
||||||
|
fx.order.0.push(other_id);
|
||||||
|
let total: usize = lengths(file(&fx, id)).iter().sum::<usize>()
|
||||||
|
+ lengths(&fx.files[&other_id]).iter().sum::<usize>();
|
||||||
|
|
||||||
|
let mut grouped = Fixture {
|
||||||
|
files: fx.files.clone(),
|
||||||
|
order: fx.order.clone(),
|
||||||
|
selection: Selection::File {
|
||||||
|
file_ids: [id, other_id].into(),
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
Merge {
|
||||||
|
type_: MergeType::Group,
|
||||||
|
remove_gaps: false,
|
||||||
|
keep_originals: false,
|
||||||
|
}
|
||||||
|
.apply(&mut grouped.state())
|
||||||
|
.unwrap();
|
||||||
|
let merged = &grouped.files[&id];
|
||||||
|
assert_eq!((merged.rte.len(), merged.trk.len()), (4, 2));
|
||||||
|
assert!(!grouped.files.contains_key(&other_id));
|
||||||
|
|
||||||
|
// connected in the first route, which comes first
|
||||||
|
fx.selection = Selection::File {
|
||||||
|
file_ids: [id, other_id].into(),
|
||||||
|
};
|
||||||
|
Merge {
|
||||||
|
type_: MergeType::Connect,
|
||||||
|
remove_gaps: false,
|
||||||
|
keep_originals: false,
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
let merged = &fx.files[&id];
|
||||||
|
assert_eq!((merged.rte.len(), merged.trk.len()), (1, 0));
|
||||||
|
assert_eq!(merged.rte[0].seg.len(), total);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_a_file_with_routes() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let names: Vec<_> = file(&fx, id)
|
||||||
|
.rte
|
||||||
|
.iter()
|
||||||
|
.map(|r| r.info.name.clone().unwrap())
|
||||||
|
.collect();
|
||||||
|
fx.selection = whole(id);
|
||||||
|
Extract {
|
||||||
|
keep_originals: false,
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
// one file per route and one per track
|
||||||
|
assert_eq!(fx.order.0.len(), 4);
|
||||||
|
let files: Vec<&File> = fx.order.0.iter().map(|id| &*fx.files[id]).collect();
|
||||||
|
for (file, name) in files.iter().zip(&names) {
|
||||||
|
assert_eq!((file.rte.len(), file.trk.len()), (1, 0));
|
||||||
|
assert_eq!(&file.info.name, name);
|
||||||
|
}
|
||||||
|
for file in &files[2..] {
|
||||||
|
assert_eq!((file.rte.len(), file.trk.len()), (0, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
// a route has nothing to extract
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
fx.selection = routes(&fx, id, &[0]);
|
||||||
|
assert_eq!(
|
||||||
|
Extract {
|
||||||
|
keep_originals: false
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state()),
|
||||||
|
Err(CommandError::NothingToDo)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_split_a_route() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let before = file(&fx, id).rte[1].clone();
|
||||||
|
fx.selection = routes(&fx, id, &[1]);
|
||||||
|
Split {
|
||||||
|
at: 10,
|
||||||
|
split_type: SplitType::Routes,
|
||||||
|
keep_originals: false,
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
// two routes, which share the trackpoint
|
||||||
|
let split = file(&fx, id);
|
||||||
|
assert_eq!(split.rte.len(), 3);
|
||||||
|
assert_eq!(split.rte[1].id, before.id);
|
||||||
|
assert_ne!(split.rte[2].id, before.id);
|
||||||
|
assert_eq!(split.rte[1].seg.len(), 11);
|
||||||
|
assert_eq!(split.rte[2].seg.len(), before.seg.len() - 10);
|
||||||
|
assert_eq!(split.rte[2].info, before.info);
|
||||||
|
|
||||||
|
// a route is not a track nor a segment, and the other way around
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
fx.selection = whole(id);
|
||||||
|
let segment_start = lengths(file(&fx, id))[..2].iter().sum::<usize>() as u32;
|
||||||
|
for (at, split_type) in [
|
||||||
|
(10, SplitType::Tracks),
|
||||||
|
(10, SplitType::Segments),
|
||||||
|
(segment_start + 10, SplitType::Routes),
|
||||||
|
] {
|
||||||
|
assert_eq!(
|
||||||
|
Split {
|
||||||
|
at,
|
||||||
|
split_type,
|
||||||
|
keep_originals: false,
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state()),
|
||||||
|
Err(CommandError::NothingToDo)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
assert_eq!(file(&fx, id).rte.len(), 2);
|
||||||
|
|
||||||
|
// into two files: the following routes and the tracks go to the second one
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let first_len = file(&fx, id).rte[0].seg.len();
|
||||||
|
fx.selection = whole(id);
|
||||||
|
Split {
|
||||||
|
at: 5,
|
||||||
|
split_type: SplitType::Files,
|
||||||
|
keep_originals: false,
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(fx.order.0.len(), 2);
|
||||||
|
let (a, b) = (&fx.files[&fx.order.0[0]], &fx.files[&fx.order.0[1]]);
|
||||||
|
assert_eq!(a.id, id);
|
||||||
|
assert_eq!((a.rte.len(), a.trk.len()), (1, 0));
|
||||||
|
assert_eq!(a.rte[0].seg.len(), 6);
|
||||||
|
assert_eq!((b.rte.len(), b.trk.len()), (2, 2));
|
||||||
|
assert_eq!(b.rte[0].seg.len(), first_len - 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_crop_removes_the_routes_left_without_trackpoints() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let len = lengths(file(&fx, id));
|
||||||
|
fx.selection = whole(id);
|
||||||
|
// the second route only
|
||||||
|
let start = len[0] as u32;
|
||||||
|
Crop {
|
||||||
|
start,
|
||||||
|
end: start + len[1] as u32 - 1,
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
let file = file(&fx, id);
|
||||||
|
assert_eq!((file.rte.len(), file.trk.len()), (1, 0));
|
||||||
|
assert_eq!(file.rte[0].seg.len(), len[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_clean_removes_the_routes_left_without_trackpoints() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
fx.selection = routes(&fx, id, &[0]);
|
||||||
|
let everywhere = LngLatBounds {
|
||||||
|
sw: LngLat {
|
||||||
|
lng: -180.0,
|
||||||
|
lat: -90.0,
|
||||||
|
},
|
||||||
|
ne: LngLat {
|
||||||
|
lng: 180.0,
|
||||||
|
lat: 90.0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
Clean {
|
||||||
|
bounds: everywhere,
|
||||||
|
type_: CleanType::Inside,
|
||||||
|
trkpt: true,
|
||||||
|
wpt: false,
|
||||||
|
}
|
||||||
|
.apply(&mut fx.state())
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(file(&fx, id).rte.len(), 1);
|
||||||
|
assert_eq!(fx.selection, whole(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_reverse_a_route_and_the_routes_of_a_file() {
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let before = file(&fx, id).clone();
|
||||||
|
fx.selection = routes(&fx, id, &[0]);
|
||||||
|
Reverse.apply(&mut fx.state()).unwrap();
|
||||||
|
let route = &file(&fx, id).rte[0].seg;
|
||||||
|
let original = &before.rte[0].seg;
|
||||||
|
assert_eq!(
|
||||||
|
route[0].coordinates.lat,
|
||||||
|
original[original.len() - 1].coordinates.lat
|
||||||
|
);
|
||||||
|
assert_eq!(file(&fx, id).rte[1].seg.rev_id, before.rte[1].seg.rev_id);
|
||||||
|
|
||||||
|
let (mut fx, id) = fixture();
|
||||||
|
let before = file(&fx, id).clone();
|
||||||
|
fx.selection = whole(id);
|
||||||
|
Reverse.apply(&mut fx.state()).unwrap();
|
||||||
|
let file = file(&fx, id);
|
||||||
|
// the routes are in the opposite order, and still before the tracks
|
||||||
|
assert_eq!(file.rte[0].id, before.rte[1].id);
|
||||||
|
assert_eq!(file.trk[0].id, before.trk[1].id);
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@ impl Apply for ChangeLoopStart {
|
|||||||
let index = self.index as usize;
|
let index = self.index as usize;
|
||||||
let target = find_target(state, index, index)?.ok_or(CommandError::NothingToDo)?;
|
let target = find_target(state, index, index)?.ok_or(CommandError::NothingToDo)?;
|
||||||
let mut file = (*state.files[&target.location.file_id]).clone();
|
let mut file = (*state.files[&target.location.file_id]).clone();
|
||||||
let segment = &mut file.trk[target.location.trk].trkseg[target.location.seg];
|
let segment = file.segment_mut(target.location.index);
|
||||||
let (start, len) = (target.start, segment.len());
|
let (start, len) = (target.start, segment.len());
|
||||||
if start == 0 || start >= len {
|
if start == 0 || start >= len {
|
||||||
// it already is the start, or it is not a trackpoint
|
// it already is the start, or it is not a trackpoint
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ pub enum CleanType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Removes the trackpoints and/or waypoints of the selection lying inside (or outside) a
|
/// Removes the trackpoints and/or waypoints of the selection lying inside (or outside) a
|
||||||
/// rectangle. A selected file cleans its tracks and its waypoints, a selected track or segment
|
/// rectangle. A selected file cleans its routes, its tracks and its waypoints, a selected route,
|
||||||
/// only trackpoints, selected waypoints only waypoints.
|
/// track or segment only trackpoints, selected waypoints only waypoints.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Clean {
|
pub struct Clean {
|
||||||
pub bounds: LngLatBounds,
|
pub bounds: LngLatBounds,
|
||||||
@@ -85,6 +85,11 @@ struct Cleaner<'a> {
|
|||||||
impl Editor for Cleaner<'_> {
|
impl Editor for Cleaner<'_> {
|
||||||
fn file(&mut self, file: &mut File) -> Edit {
|
fn file(&mut self, file: &mut File) -> Edit {
|
||||||
let mut edit = Edit::Unchanged;
|
let mut edit = Edit::Unchanged;
|
||||||
|
for rte in &mut file.rte {
|
||||||
|
if self.route(rte) == Edit::Changed {
|
||||||
|
edit = Edit::Changed;
|
||||||
|
}
|
||||||
|
}
|
||||||
for trk in &mut file.trk {
|
for trk in &mut file.trk {
|
||||||
if self.track(trk) == Edit::Changed {
|
if self.track(trk) == Edit::Changed {
|
||||||
edit = Edit::Changed;
|
edit = Edit::Changed;
|
||||||
@@ -124,24 +129,21 @@ impl Editor for Cleaner<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes what the clean left empty: the emptied segments and the tracks that lost all their
|
/// Removes what the clean left empty: the emptied segments, the routes of these segments and the
|
||||||
/// segments. A file is never removed, even if nothing is left in it: it stays, empty. The
|
/// tracks that lost all their segments. A file is never removed, even if nothing is left in it: it stays, empty. The
|
||||||
/// selection falls back to the closest element still existing.
|
/// selection falls back to the closest element still existing.
|
||||||
fn prune(state: &mut State, emptied: &HashSet<TrackSegmentId>, wpt_files: &HashSet<FileId>) {
|
fn prune(state: &mut State, emptied: &HashSet<TrackSegmentId>, wpt_files: &HashSet<FileId>) {
|
||||||
let ids: Vec<FileId> = state
|
let ids: Vec<FileId> = state
|
||||||
.files
|
.files
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(id, file)| {
|
.filter(|(id, file)| {
|
||||||
wpt_files.contains(id)
|
wpt_files.contains(id) || file.segments().any(|seg| emptied.contains(&seg.id))
|
||||||
|| file
|
|
||||||
.trk
|
|
||||||
.iter()
|
|
||||||
.any(|trk| trk.trkseg.iter().any(|seg| emptied.contains(&seg.id)))
|
|
||||||
})
|
})
|
||||||
.map(|(id, _)| *id)
|
.map(|(id, _)| *id)
|
||||||
.collect();
|
.collect();
|
||||||
for id in ids {
|
for id in ids {
|
||||||
let mut file = (*state.files[&id]).clone();
|
let mut file = (*state.files[&id]).clone();
|
||||||
|
file.rte.retain(|rte| !emptied.contains(&rte.seg.id));
|
||||||
file.trk.retain_mut(|trk| {
|
file.trk.retain_mut(|trk| {
|
||||||
let len = trk.trkseg.len();
|
let len = trk.trkseg.len();
|
||||||
trk.trkseg.retain(|seg| !emptied.contains(&seg.id));
|
trk.trkseg.retain(|seg| !emptied.contains(&seg.id));
|
||||||
@@ -167,6 +169,27 @@ fn fixed_selection(state: &State) -> Selection {
|
|||||||
Selection::File { file_ids }
|
Selection::File { file_ids }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Selection::Route { file_id, rte_ids } => {
|
||||||
|
let Some(file) = files.get(file_id) else {
|
||||||
|
return Selection::Empty;
|
||||||
|
};
|
||||||
|
let rte_ids: HashSet<_> = file
|
||||||
|
.rte
|
||||||
|
.iter()
|
||||||
|
.map(|rte| rte.id)
|
||||||
|
.filter(|id| rte_ids.contains(id))
|
||||||
|
.collect();
|
||||||
|
if rte_ids.is_empty() {
|
||||||
|
Selection::File {
|
||||||
|
file_ids: [*file_id].into(),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Selection::Route {
|
||||||
|
file_id: *file_id,
|
||||||
|
rte_ids,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Selection::Track { file_id, trk_ids } => {
|
Selection::Track { file_id, trk_ids } => {
|
||||||
let Some(file) = files.get(file_id) else {
|
let Some(file) = files.get(file_id) else {
|
||||||
return Selection::Empty;
|
return Selection::Empty;
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ use crate::{
|
|||||||
/// Keeps the trackpoints `start` to `end` (both included) of the selection, which are counted
|
/// Keeps the trackpoints `start` to `end` (both included) of the selection, which are counted
|
||||||
/// over its segments one after the other (as in the `RoutingBuffer`), and removes the others.
|
/// over its segments one after the other (as in the `RoutingBuffer`), and removes the others.
|
||||||
///
|
///
|
||||||
/// The segments that have no trackpoint left are removed. So are the tracks that have no segment
|
/// The segments that have no trackpoint left are removed, and so are the routes of these segments.
|
||||||
/// left, if tracks or files are selected (not if only segments are), and the files that have no
|
/// So are the tracks that have no segment left, if tracks or files are selected (not if only
|
||||||
/// trackpoint left, if files are selected. A range that goes past the last trackpoint stops at it.
|
/// segments are), and the files that have no trackpoint left, if files are selected. A range that goes past the last trackpoint stops at it.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Crop {
|
pub struct Crop {
|
||||||
pub start: u32,
|
pub start: u32,
|
||||||
@@ -43,8 +43,7 @@ impl Apply for Crop {
|
|||||||
.segment_locations(state.files, &state.order.0)
|
.segment_locations(state.files, &state.order.0)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|location| {
|
.map(|location| {
|
||||||
let len =
|
let len = location.segment(state.files).len();
|
||||||
state.files[&location.file_id].trk[location.trk].trkseg[location.seg].len();
|
|
||||||
let keep = (len > 0 && start < offset + len && end >= offset)
|
let keep = (len > 0 && start < offset + len && end >= offset)
|
||||||
.then(|| (start.saturating_sub(offset), (end - offset).min(len - 1)));
|
.then(|| (start.saturating_sub(offset), (end - offset).min(len - 1)));
|
||||||
offset += len;
|
offset += len;
|
||||||
@@ -73,14 +72,14 @@ impl Apply for Crop {
|
|||||||
let mut removed: HashSet<TrackSegmentId> = HashSet::new();
|
let mut removed: HashSet<TrackSegmentId> = HashSet::new();
|
||||||
let mut had_points: HashSet<FileId> = HashSet::new();
|
let mut had_points: HashSet<FileId> = HashSet::new();
|
||||||
for plan in &plans {
|
for plan in &plans {
|
||||||
let SegmentLocation { file_id, trk, seg } = plan.location;
|
let SegmentLocation { file_id, index } = plan.location;
|
||||||
let file = files
|
let file = files
|
||||||
.entry(file_id)
|
.entry(file_id)
|
||||||
.or_insert_with(|| (*state.files[&file_id]).clone());
|
.or_insert_with(|| (*state.files[&file_id]).clone());
|
||||||
if plan.len > 0 {
|
if plan.len > 0 {
|
||||||
had_points.insert(file_id);
|
had_points.insert(file_id);
|
||||||
}
|
}
|
||||||
let segment = &mut file.trk[trk].trkseg[seg];
|
let segment = file.segment_mut(index);
|
||||||
match plan.keep {
|
match plan.keep {
|
||||||
None => {
|
None => {
|
||||||
removed.insert(segment.id);
|
removed.insert(segment.id);
|
||||||
@@ -91,6 +90,8 @@ impl Apply for Crop {
|
|||||||
|
|
||||||
let mut gone: Vec<FileId> = vec![];
|
let mut gone: Vec<FileId> = vec![];
|
||||||
for (file_id, file) in files.iter_mut() {
|
for (file_id, file) in files.iter_mut() {
|
||||||
|
// a route has its segment, or is not there
|
||||||
|
file.rte.retain(|route| !removed.contains(&route.seg.id));
|
||||||
// the tracks that lost segments
|
// the tracks that lost segments
|
||||||
let touched: Vec<bool> = file
|
let touched: Vec<bool> = file
|
||||||
.trk
|
.trk
|
||||||
@@ -108,7 +109,7 @@ impl Apply for Crop {
|
|||||||
file.trk
|
file.trk
|
||||||
.retain(|track| !(touched.next().unwrap() && track.trkseg.is_empty()));
|
.retain(|track| !(touched.next().unwrap() && track.trkseg.is_empty()));
|
||||||
}
|
}
|
||||||
if prune_files && file.trk.is_empty() && had_points.contains(file_id) {
|
if prune_files && file.has_no_path() && had_points.contains(file_id) {
|
||||||
gone.push(*file_id);
|
gone.push(*file_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,9 +14,7 @@ impl Apply for Elevation<'_> {
|
|||||||
.segment_locations(state.files, &state.order.0);
|
.segment_locations(state.files, &state.order.0);
|
||||||
let count: usize = locations
|
let count: usize = locations
|
||||||
.iter()
|
.iter()
|
||||||
.map(|location| {
|
.map(|location| location.segment(state.files).len())
|
||||||
state.files[&location.file_id].trk[location.trk].trkseg[location.seg].len()
|
|
||||||
})
|
|
||||||
.sum();
|
.sum();
|
||||||
if count == 0 {
|
if count == 0 {
|
||||||
return Err(CommandError::NothingToDo);
|
return Err(CommandError::NothingToDo);
|
||||||
|
|||||||
@@ -1,22 +1,23 @@
|
|||||||
use std::{collections::HashSet, rc::Rc};
|
use std::{collections::HashSet, rc::Rc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Apply, CommandError, File, FileId, LngLat, LngLatBounds, Selection, State, Track, TrackId,
|
Apply, CommandError, File, FileId, LngLat, LngLatBounds, Route, Selection, State, Track,
|
||||||
TrackSegment, Waypoint, Waypoints, copy_file, copy_waypoint, crossarc_lnglat, distance,
|
TrackId, TrackSegment, Waypoint, Waypoints, copy_file, copy_waypoint, crossarc_lnglat,
|
||||||
|
distance,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Splits the selected elements that are made of several segments into elements that have one
|
/// Splits the selected elements that are made of several segments into elements that have one
|
||||||
/// each:
|
/// each:
|
||||||
///
|
///
|
||||||
/// - files: a file with several tracks becomes one file per track, in which the track is split
|
/// - files: a file with several routes and tracks becomes one file per route and per track, in
|
||||||
/// into one track per segment. A file with a single track becomes one file per segment. The new
|
/// which the track is split into one track per segment. A file with a single track becomes one
|
||||||
/// files are in the place of the file, which goes, and are selected. They are named after the
|
/// file per segment. The new files are in the place of the file, which goes, and are selected.
|
||||||
/// track, or the file, and numbered. The waypoints go to the files that have the segments
|
/// They are named after the route or track, or the file, and numbered. The waypoints go to the files that have the segments
|
||||||
/// closest to them (to the first file if the file has no trackpoint).
|
/// closest to them (to the first file if the file has no trackpoint).
|
||||||
/// - tracks: each selected track is replaced by one track per segment, which are selected. They
|
/// - tracks: each selected track is replaced by one track per segment, which are selected. They
|
||||||
/// are named after the track and numbered.
|
/// are named after the track and numbered.
|
||||||
///
|
///
|
||||||
/// What has a single segment is left as it is. There is nothing to do if nothing was changed.
|
/// What has a single segment is left as it is, which routes always are. There is nothing to do if nothing was changed.
|
||||||
///
|
///
|
||||||
/// With `keep_originals`, the files that are extracted stay where they are, and the new files come
|
/// With `keep_originals`, the files that are extracted stay where they are, and the new files come
|
||||||
/// right after each of them, and only the new files are selected. Extracting
|
/// right after each of them, and only the new files are selected. Extracting
|
||||||
@@ -46,8 +47,7 @@ fn extract_files(
|
|||||||
let mut extracted: Vec<(FileId, Vec<File>)> = vec![];
|
let mut extracted: Vec<(FileId, Vec<File>)> = vec![];
|
||||||
for id in state.order.0.iter().filter(|id| file_ids.contains(id)) {
|
for id in state.order.0.iter().filter(|id| file_ids.contains(id)) {
|
||||||
let file = &state.files[id];
|
let file = &state.files[id];
|
||||||
let segments: usize = file.trk.iter().map(|track| track.trkseg.len()).sum();
|
if file.segments().count() > 1 {
|
||||||
if segments > 1 {
|
|
||||||
let mut files = extract_file(file);
|
let mut files = extract_file(file);
|
||||||
if keep_originals {
|
if keep_originals {
|
||||||
// the tracks and segments are the ones of the file, which stays: they need new ids
|
// the tracks and segments are the ones of the file, which stays: they need new ids
|
||||||
@@ -95,60 +95,65 @@ fn extract_files(
|
|||||||
|
|
||||||
/// The files that a file with several segments becomes.
|
/// The files that a file with several segments becomes.
|
||||||
fn extract_file(file: &File) -> Vec<File> {
|
fn extract_file(file: &File) -> Vec<File> {
|
||||||
// for each new file: its tracks and its name, and the segments that go with it
|
// for each new file: its routes, its tracks and its name, and the segments that go with it
|
||||||
struct Part {
|
struct Part {
|
||||||
|
routes: Vec<Route>,
|
||||||
tracks: Vec<Track>,
|
tracks: Vec<Track>,
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
let (parts, owner): (Vec<Part>, Vec<usize>) = if file.trk.len() > 1 {
|
let name = |name: &Option<String>, i: usize| {
|
||||||
// one file per track, the segments of a track follow it
|
name.clone()
|
||||||
let parts = file
|
.unwrap_or_else(|| format!("{} ({})", file.info.name, i + 1))
|
||||||
.trk
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, track)| Part {
|
|
||||||
tracks: split_track(track),
|
|
||||||
name: track
|
|
||||||
.info
|
|
||||||
.name
|
|
||||||
.clone()
|
|
||||||
.unwrap_or_else(|| format!("{} ({})", file.info.name, i + 1)),
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
let owner = file
|
|
||||||
.trk
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.flat_map(|(i, track)| track.trkseg.iter().map(move |_| i))
|
|
||||||
.collect();
|
|
||||||
(parts, owner)
|
|
||||||
} else {
|
|
||||||
// one file per segment
|
|
||||||
let track = &file.trk[0];
|
|
||||||
let base = track
|
|
||||||
.info
|
|
||||||
.name
|
|
||||||
.clone()
|
|
||||||
.unwrap_or_else(|| file.info.name.clone());
|
|
||||||
let parts = track
|
|
||||||
.trkseg
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(i, segment)| Part {
|
|
||||||
tracks: vec![Track {
|
|
||||||
id: if i == 0 { track.id } else { Default::default() },
|
|
||||||
trkseg: vec![segment.clone()],
|
|
||||||
..track.clone()
|
|
||||||
}],
|
|
||||||
name: format!("{base} ({})", i + 1),
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
let owner = (0..track.trkseg.len()).collect();
|
|
||||||
(parts, owner)
|
|
||||||
};
|
};
|
||||||
|
let (parts, owner): (Vec<Part>, Vec<usize>) =
|
||||||
|
if file.rte.len() + file.trk.len() > 1 {
|
||||||
|
// one file per route and per track, the segments of a track follow it
|
||||||
|
let routes = file.rte.iter().enumerate().map(|(i, route)| Part {
|
||||||
|
routes: vec![route.clone()],
|
||||||
|
tracks: vec![],
|
||||||
|
name: name(&route.info.name, i),
|
||||||
|
});
|
||||||
|
let tracks = file.trk.iter().enumerate().map(|(i, track)| Part {
|
||||||
|
routes: vec![],
|
||||||
|
tracks: split_track(track),
|
||||||
|
name: name(&track.info.name, file.rte.len() + i),
|
||||||
|
});
|
||||||
|
let parts = routes.chain(tracks).collect();
|
||||||
|
let owner =
|
||||||
|
(0..file.rte.len())
|
||||||
|
.chain(file.trk.iter().enumerate().flat_map(|(i, track)| {
|
||||||
|
track.trkseg.iter().map(move |_| file.rte.len() + i)
|
||||||
|
}))
|
||||||
|
.collect();
|
||||||
|
(parts, owner)
|
||||||
|
} else {
|
||||||
|
// one file per segment
|
||||||
|
let track = &file.trk[0];
|
||||||
|
let base = track
|
||||||
|
.info
|
||||||
|
.name
|
||||||
|
.clone()
|
||||||
|
.unwrap_or_else(|| file.info.name.clone());
|
||||||
|
let parts = track
|
||||||
|
.trkseg
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, segment)| Part {
|
||||||
|
routes: vec![],
|
||||||
|
tracks: vec![Track {
|
||||||
|
id: if i == 0 { track.id } else { Default::default() },
|
||||||
|
trkseg: vec![segment.clone()],
|
||||||
|
..track.clone()
|
||||||
|
}],
|
||||||
|
name: format!("{base} ({})", i + 1),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let owner = (0..track.trkseg.len()).collect();
|
||||||
|
(parts, owner)
|
||||||
|
};
|
||||||
|
|
||||||
// the waypoints go with the segments that are the closest to them
|
// the waypoints go with the segments that are the closest to them
|
||||||
let segments: Vec<&TrackSegment> = file.trk.iter().flat_map(|track| &track.trkseg).collect();
|
let segments: Vec<&TrackSegment> = file.segments().collect();
|
||||||
let mut waypoints: Vec<Vec<Waypoint>> = vec![vec![]; parts.len()];
|
let mut waypoints: Vec<Vec<Waypoint>> = vec![vec![]; parts.len()];
|
||||||
for waypoint in file.wpt.iter() {
|
for waypoint in file.wpt.iter() {
|
||||||
let mut owners: Vec<usize> = closest_segments(&segments, waypoint.coordinates)
|
let mut owners: Vec<usize> = closest_segments(&segments, waypoint.coordinates)
|
||||||
@@ -176,6 +181,7 @@ fn extract_file(file: &File) -> Vec<File> {
|
|||||||
.map(|(part, waypoints)| {
|
.map(|(part, waypoints)| {
|
||||||
let mut extracted = File {
|
let mut extracted = File {
|
||||||
id: Default::default(),
|
id: Default::default(),
|
||||||
|
rte: part.routes,
|
||||||
trk: part.tracks,
|
trk: part.tracks,
|
||||||
wpt: Waypoints::default(),
|
wpt: Waypoints::default(),
|
||||||
..file.clone()
|
..file.clone()
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ impl Apply for InsertAnchor {
|
|||||||
.selection
|
.selection
|
||||||
.segment_locations(state.files, &state.order.0)
|
.segment_locations(state.files, &state.order.0)
|
||||||
{
|
{
|
||||||
let segment = &state.files[&location.file_id].trk[location.trk].trkseg[location.seg];
|
let segment = location.segment(state.files);
|
||||||
let mut points = segment.iter();
|
let mut points = segment.iter();
|
||||||
let Some(mut previous) = points.next() else {
|
let Some(mut previous) = points.next() else {
|
||||||
continue;
|
continue;
|
||||||
@@ -81,7 +81,7 @@ impl Apply for InsertAnchor {
|
|||||||
let closest = closest.ok_or(CommandError::NothingToDo)?;
|
let closest = closest.ok_or(CommandError::NothingToDo)?;
|
||||||
|
|
||||||
let mut file = (*state.files[&closest.location.file_id]).clone();
|
let mut file = (*state.files[&closest.location.file_id]).clone();
|
||||||
let segment = &mut file.trk[closest.location.trk].trkseg[closest.location.seg];
|
let segment = file.segment_mut(closest.location.index);
|
||||||
let before = closest.before;
|
let before = closest.before;
|
||||||
// on a trackpoint already, if it is closer than `SNAP_DISTANCE`
|
// on a trackpoint already, if it is closer than `SNAP_DISTANCE`
|
||||||
let snapped = if segment.len() == 1 {
|
let snapped = if segment.len() == 1 {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
use std::{collections::HashSet, rc::Rc};
|
use std::{collections::HashSet, rc::Rc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Apply, CommandError, FileId, GlobalStatistics, Selection, State, Statistics, Track, TrackId,
|
Apply, CommandError, FileId, GlobalStatistics, Route, RouteId, Selection, State, Statistics,
|
||||||
TrackSegment, TrackSegmentId, Waypoint, Waypoints, copy_track, replace_trackpoints,
|
Track, TrackId, TrackSegment, TrackSegmentId, Waypoint, Waypoints, copy_route, copy_track,
|
||||||
|
replace_trackpoints,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
|
||||||
@@ -16,13 +17,16 @@ pub enum MergeType {
|
|||||||
|
|
||||||
/// Merges the selected elements into the first of them:
|
/// Merges the selected elements into the first of them:
|
||||||
///
|
///
|
||||||
/// - files: the other files are deleted. The tracks of all of them are in the first file, as
|
/// - files: the other files are deleted. The routes and tracks of all of them are in the first
|
||||||
/// they are with `Group`, or all connected in the first track with `Connect`. The waypoints of
|
/// file, as they are with `Group`, or all connected in the first of them (a route or a track,
|
||||||
|
/// the routes of a file coming before its tracks) with `Connect`. The waypoints of
|
||||||
/// all of them are in the first file too, except the ones that are the same. With
|
/// all of them are in the first file too, except the ones that are the same. With
|
||||||
/// `keep_originals`, no file is deleted nor changed: the result is a new file (with the info of
|
/// `keep_originals`, no file is deleted nor changed: the result is a new file (with the info of
|
||||||
/// the first one and new ids), which comes right after the last of the files and is selected.
|
/// the first one and new ids), which comes right after the last of the files and is selected.
|
||||||
/// - tracks of a file: the first track gets their segments, as they are with `Group`, or all
|
/// - tracks of a file: the first track gets their segments, as they are with `Group`, or all
|
||||||
/// connected in one segment with `Connect`. The other tracks are removed.
|
/// connected in one segment with `Connect`. The other tracks are removed.
|
||||||
|
/// - routes of a file: the first route gets their trackpoints, all connected with `Connect`, and
|
||||||
|
/// the other routes are removed. A route has a single segment, so there is nothing to `Group`.
|
||||||
/// - segments of a track: they are all connected in a segment at the position of the first one
|
/// - segments of a track: they are all connected in a segment at the position of the first one
|
||||||
/// with `Connect`. With `Group` they are put one after the other, at that position.
|
/// with `Connect`. With `Group` they are put one after the other, at that position.
|
||||||
///
|
///
|
||||||
@@ -44,6 +48,7 @@ impl Apply for Merge {
|
|||||||
let selection = state.selection.clone();
|
let selection = state.selection.clone();
|
||||||
match &selection {
|
match &selection {
|
||||||
Selection::File { file_ids } => self.merge_files(state, file_ids),
|
Selection::File { file_ids } => self.merge_files(state, file_ids),
|
||||||
|
Selection::Route { file_id, rte_ids } => self.merge_routes(state, *file_id, rte_ids),
|
||||||
Selection::Track { file_id, trk_ids } => self.merge_tracks(state, *file_id, trk_ids),
|
Selection::Track { file_id, trk_ids } => self.merge_tracks(state, *file_id, trk_ids),
|
||||||
Selection::TrackSegment {
|
Selection::TrackSegment {
|
||||||
file_id,
|
file_id,
|
||||||
@@ -77,21 +82,26 @@ impl Merge {
|
|||||||
return Err(CommandError::NothingToDo);
|
return Err(CommandError::NothingToDo);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut routes: Vec<Route> = vec![];
|
||||||
let mut tracks: Vec<Track> = vec![];
|
let mut tracks: Vec<Track> = vec![];
|
||||||
|
let mut segments: Vec<TrackSegment> = vec![];
|
||||||
let mut waypoints: Vec<Waypoint> = vec![];
|
let mut waypoints: Vec<Waypoint> = vec![];
|
||||||
for id in &ordered {
|
for id in &ordered {
|
||||||
let file = &state.files[id];
|
let file = &state.files[id];
|
||||||
|
routes.extend(file.rte.iter().cloned());
|
||||||
tracks.extend(file.trk.iter().cloned());
|
tracks.extend(file.trk.iter().cloned());
|
||||||
|
segments.extend(file.segments().cloned());
|
||||||
for waypoint in file.wpt.iter() {
|
for waypoint in file.wpt.iter() {
|
||||||
if !waypoints.iter().any(|kept| kept.same_content(waypoint)) {
|
if !waypoints.iter().any(|kept| kept.same_content(waypoint)) {
|
||||||
waypoints.push(waypoint.clone());
|
waypoints.push(waypoint.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let segments: Vec<TrackSegment> = tracks
|
// the first route or track, which holds the connected segment
|
||||||
.iter()
|
let first_is_route = ordered.iter().find_map(|id| {
|
||||||
.flat_map(|track| track.trkseg.iter().cloned())
|
let file = &state.files[id];
|
||||||
.collect();
|
(!file.has_no_path()).then(|| !file.rte.is_empty())
|
||||||
|
});
|
||||||
|
|
||||||
match self.type_ {
|
match self.type_ {
|
||||||
MergeType::Group if ordered.len() == 1 => return Err(CommandError::NothingToDo),
|
MergeType::Group if ordered.len() == 1 => return Err(CommandError::NothingToDo),
|
||||||
@@ -99,24 +109,35 @@ impl Merge {
|
|||||||
return Err(CommandError::NothingToDo);
|
return Err(CommandError::NothingToDo);
|
||||||
}
|
}
|
||||||
MergeType::Connect if !segments.is_empty() => {
|
MergeType::Connect if !segments.is_empty() => {
|
||||||
// a single track, which is the first one, with the connected segment
|
// a single route or track, which is the first one, with the connected segment
|
||||||
let merged = connect(&segments, segments[0].id, self.remove_gaps);
|
let merged = connect(&segments, segments[0].id, self.remove_gaps);
|
||||||
let first = Track {
|
if first_is_route == Some(true) {
|
||||||
trkseg: vec![merged],
|
let first = Route {
|
||||||
..tracks[0].clone()
|
seg: merged,
|
||||||
};
|
..routes[0].clone()
|
||||||
tracks = vec![first];
|
};
|
||||||
|
(routes, tracks) = (vec![first], vec![]);
|
||||||
|
} else {
|
||||||
|
let first = Track {
|
||||||
|
trkseg: vec![merged],
|
||||||
|
..tracks[0].clone()
|
||||||
|
};
|
||||||
|
(routes, tracks) = (vec![], vec![first]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut target = (*state.files[&target_id]).clone();
|
let mut target = (*state.files[&target_id]).clone();
|
||||||
|
target.rte = routes;
|
||||||
target.trk = tracks;
|
target.trk = tracks;
|
||||||
target.wpt = Waypoints::default();
|
target.wpt = Waypoints::default();
|
||||||
target.wpt.insert_at(0, waypoints);
|
target.wpt.insert_at(0, waypoints);
|
||||||
if self.keep_originals {
|
if self.keep_originals {
|
||||||
// a new file, whose tracks and segments do not share their ids with the originals
|
// a new file, whose routes, tracks and segments do not share their ids with the
|
||||||
|
// originals
|
||||||
target.id = Default::default();
|
target.id = Default::default();
|
||||||
|
target.rte = target.rte.iter().map(copy_route).collect();
|
||||||
target.trk = target.trk.iter().map(copy_track).collect();
|
target.trk = target.trk.iter().map(copy_track).collect();
|
||||||
let at = state
|
let at = state
|
||||||
.order
|
.order
|
||||||
@@ -139,6 +160,33 @@ impl Merge {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn merge_routes(
|
||||||
|
&self,
|
||||||
|
state: &mut State,
|
||||||
|
file_id: FileId,
|
||||||
|
rte_ids: &HashSet<RouteId>,
|
||||||
|
) -> Result<(), CommandError> {
|
||||||
|
let file = state.files.get(&file_id).ok_or(CommandError::NothingToDo)?;
|
||||||
|
let selected: Vec<usize> = (0..file.rte.len())
|
||||||
|
.filter(|i| rte_ids.contains(&file.rte[*i].id))
|
||||||
|
.collect();
|
||||||
|
if selected.len() < 2 || self.type_ == MergeType::Group {
|
||||||
|
return Err(CommandError::NothingToDo);
|
||||||
|
}
|
||||||
|
let first = selected[0];
|
||||||
|
let segments: Vec<TrackSegment> =
|
||||||
|
selected.iter().map(|i| file.rte[*i].seg.clone()).collect();
|
||||||
|
let mut file = (**file).clone();
|
||||||
|
file.rte[first].seg = connect(&segments, segments[0].id, self.remove_gaps);
|
||||||
|
let mut index = 0;
|
||||||
|
file.rte.retain(|_| {
|
||||||
|
index += 1;
|
||||||
|
!selected[1..].contains(&(index - 1))
|
||||||
|
});
|
||||||
|
state.files.insert(file_id, Rc::new(file));
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn merge_tracks(
|
fn merge_tracks(
|
||||||
&self,
|
&self,
|
||||||
state: &mut State,
|
state: &mut State,
|
||||||
|
|||||||
@@ -58,7 +58,8 @@ impl Apply for NewWaypoint<'_> {
|
|||||||
.filter(|id| file_ids.contains(id))
|
.filter(|id| file_ids.contains(id))
|
||||||
.copied()
|
.copied()
|
||||||
.collect(),
|
.collect(),
|
||||||
Selection::Track { file_id, .. }
|
Selection::Route { file_id, .. }
|
||||||
|
| Selection::Track { file_id, .. }
|
||||||
| Selection::TrackSegment { file_id, .. }
|
| Selection::TrackSegment { file_id, .. }
|
||||||
| Selection::Waypoints { file_id }
|
| Selection::Waypoints { file_id }
|
||||||
| Selection::Waypoint { file_id, .. } => vec![*file_id],
|
| Selection::Waypoint { file_id, .. } => vec![*file_id],
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ impl Apply for Reduce {
|
|||||||
let mut changed = vec![];
|
let mut changed = vec![];
|
||||||
let mut kept_points = vec![];
|
let mut kept_points = vec![];
|
||||||
for location in &locations {
|
for location in &locations {
|
||||||
let segment = &state.files[&location.file_id].trk[location.trk].trkseg[location.seg];
|
let segment = location.segment(state.files);
|
||||||
let kept = reduce_indices(segment, self.tolerance);
|
let kept = reduce_indices(segment, self.tolerance);
|
||||||
if kept.len() == segment.len() {
|
if kept.len() == segment.len() {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Apply, CommandError, File, FileId, Selection, State, TrackId, TrackSegmentId, end_time,
|
Apply, CommandError, File, FileId, RouteId, Selection, State, TrackId, TrackSegmentId,
|
||||||
reverse_segment, reverse_segments, reverse_tracks, start_time, track_end_time,
|
end_time, reverse_routes, reverse_segment, reverse_segments, reverse_tracks, start_time,
|
||||||
track_start_time,
|
track_end_time, track_start_time,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Reverses the direction of the selected elements: the whole of each selected file (its tracks,
|
/// Reverses the direction of the selected elements: the whole of each selected file (its routes,
|
||||||
/// the segments of its tracks and their trackpoints are in the opposite order), of each selected
|
/// its tracks, the segments of its tracks and their trackpoints are in the opposite order; the
|
||||||
/// track (its segments and their trackpoints), or each selected segment on its own (its
|
/// routes and the tracks are two wholes, as the routes stay before the tracks), of each selected
|
||||||
|
/// track (its segments and their trackpoints), or each selected route or segment on its own (its
|
||||||
/// trackpoints). The timestamps are mirrored, see [`reverse_segment`].
|
/// trackpoints). The timestamps are mirrored, see [`reverse_segment`].
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Reverse;
|
pub struct Reverse;
|
||||||
@@ -20,9 +21,7 @@ impl Apply for Reverse {
|
|||||||
.segment_locations(state.files, &state.order.0);
|
.segment_locations(state.files, &state.order.0);
|
||||||
let points: usize = locations
|
let points: usize = locations
|
||||||
.iter()
|
.iter()
|
||||||
.map(|location| {
|
.map(|location| location.segment(state.files).len())
|
||||||
state.files[&location.file_id].trk[location.trk].trkseg[location.seg].len()
|
|
||||||
})
|
|
||||||
.sum();
|
.sum();
|
||||||
if points <= 1 {
|
if points <= 1 {
|
||||||
return Err(CommandError::NothingToDo);
|
return Err(CommandError::NothingToDo);
|
||||||
@@ -40,12 +39,18 @@ impl Apply for Reverse {
|
|||||||
Selection::File { file_ids } => {
|
Selection::File { file_ids } => {
|
||||||
for file_id in file_ids {
|
for file_id in file_ids {
|
||||||
edit(file_id, &|file| {
|
edit(file_id, &|file| {
|
||||||
|
let end = file.rte.last().and_then(|rte| end_time(&rte.seg));
|
||||||
|
let start = file.rte.first().and_then(|rte| start_time(&rte.seg));
|
||||||
|
reverse_routes(&mut file.rte, end, start);
|
||||||
let end = file.trk.last().and_then(track_end_time);
|
let end = file.trk.last().and_then(track_end_time);
|
||||||
let start = file.trk.first().and_then(track_start_time);
|
let start = file.trk.first().and_then(track_start_time);
|
||||||
reverse_tracks(&mut file.trk, end, start);
|
reverse_tracks(&mut file.trk, end, start);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Selection::Route { file_id, rte_ids } => edit(file_id, &|file| {
|
||||||
|
reverse_selected_routes(file, rte_ids);
|
||||||
|
}),
|
||||||
Selection::Track { file_id, trk_ids } => edit(file_id, &|file| {
|
Selection::Track { file_id, trk_ids } => edit(file_id, &|file| {
|
||||||
reverse_selected_tracks(file, trk_ids);
|
reverse_selected_tracks(file, trk_ids);
|
||||||
}),
|
}),
|
||||||
@@ -66,6 +71,13 @@ impl Apply for Reverse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn reverse_selected_routes(file: &mut File, ids: &std::collections::HashSet<RouteId>) {
|
||||||
|
for route in file.rte.iter_mut().filter(|route| ids.contains(&route.id)) {
|
||||||
|
let (end, start) = (end_time(&route.seg), start_time(&route.seg));
|
||||||
|
reverse_segment(&mut route.seg, end, start);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn reverse_selected_tracks(file: &mut File, ids: &std::collections::HashSet<TrackId>) {
|
fn reverse_selected_tracks(file: &mut File, ids: &std::collections::HashSet<TrackId>) {
|
||||||
for track in file.trk.iter_mut().filter(|track| ids.contains(&track.id)) {
|
for track in file.trk.iter_mut().filter(|track| ids.contains(&track.id)) {
|
||||||
let end = track.trkseg.last().and_then(end_time);
|
let end = track.trkseg.last().and_then(end_time);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ impl Apply for RoundTrip {
|
|||||||
.selection
|
.selection
|
||||||
.segment_locations(state.files, &state.order.0)
|
.segment_locations(state.files, &state.order.0)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|l| state.files[&l.file_id].trk[l.trk].trkseg[l.seg].len() >= 2)
|
.filter(|l| l.segment(state.files).len() >= 2)
|
||||||
.collect();
|
.collect();
|
||||||
if locations.is_empty() {
|
if locations.is_empty() {
|
||||||
return Err(CommandError::NothingToDo);
|
return Err(CommandError::NothingToDo);
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Apply, Categories, CommandError, FileId, SegmentLocation, Selection, State, Statistics, Track,
|
Apply, Categories, CommandError, FileId, SegmentIndex, SegmentLocation, Selection, State,
|
||||||
TrackSegment, Trackpoint, distance, replace_trackpoints,
|
Statistics, Track, TrackSegment, Trackpoint, distance, replace_trackpoints,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The values of an OSM attribute (surface, highway...) of the new trackpoints of a [`Route`], as
|
/// The values of an OSM attribute (surface, highway...) of the new trackpoints of a [`Reroute`], as
|
||||||
/// the intervals of consecutive trackpoints that share one: `starts[i]` is the index of the first
|
/// the intervals of consecutive trackpoints that share one: `starts[i]` is the index of the first
|
||||||
/// trackpoint of the interval `i`, and `values[i]` its value, 0 when unknown, else 1 + the index
|
/// trackpoint of the interval `i`, and `values[i]` its value, 0 when unknown, else 1 + the index
|
||||||
/// of its name in `names`. No intervals means that the attribute is unknown everywhere.
|
/// of its name in `names`. No intervals means that the attribute is unknown everywhere.
|
||||||
@@ -64,7 +64,7 @@ impl RouteCategory<'_> {
|
|||||||
/// anchors anyway. The ones right before and after the range, between which the route goes, become
|
/// anchors anyway. The ones right before and after the range, between which the route goes, become
|
||||||
/// shown at every zoom level as well.
|
/// shown at every zoom level as well.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Route<'a> {
|
pub struct Reroute<'a> {
|
||||||
pub start: u32,
|
pub start: u32,
|
||||||
pub end: u32,
|
pub end: u32,
|
||||||
pub lng: &'a [f64],
|
pub lng: &'a [f64],
|
||||||
@@ -89,7 +89,7 @@ pub(super) struct Target {
|
|||||||
pub end: usize,
|
pub end: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Apply for Route<'_> {
|
impl Apply for Reroute<'_> {
|
||||||
fn apply(self, state: &mut State) -> Result<(), CommandError> {
|
fn apply(self, state: &mut State) -> Result<(), CommandError> {
|
||||||
let (start, end) = (self.start as usize, self.end as usize);
|
let (start, end) = (self.start as usize, self.end as usize);
|
||||||
let len = self.lng.len();
|
let len = self.lng.len();
|
||||||
@@ -152,14 +152,17 @@ impl Apply for Route<'_> {
|
|||||||
}
|
}
|
||||||
let seg = file.trk[trk].trkseg.len() - 1;
|
let seg = file.trk[trk].trkseg.len() - 1;
|
||||||
Target {
|
Target {
|
||||||
location: SegmentLocation { file_id, trk, seg },
|
location: SegmentLocation {
|
||||||
|
file_id,
|
||||||
|
index: SegmentIndex::Track { trk, seg },
|
||||||
|
},
|
||||||
start: 0,
|
start: 0,
|
||||||
end: 0,
|
end: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let segment = &mut file.trk[target.location.trk].trkseg[target.location.seg];
|
let segment = file.segment_mut(target.location.index);
|
||||||
let new_len = points.len();
|
let new_len = points.len();
|
||||||
let (speed, start_time) = speed_and_start_time(segment, &target, &points);
|
let (speed, start_time) = speed_and_start_time(segment, &target, &points);
|
||||||
let has_times = !segment.is_empty() && segment[0].time.is_some();
|
let has_times = !segment.is_empty() && segment[0].time.is_some();
|
||||||
@@ -208,7 +211,7 @@ pub(super) fn find_target(
|
|||||||
let segments: Vec<(SegmentLocation, usize)> = locations
|
let segments: Vec<(SegmentLocation, usize)> = locations
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|location| {
|
.map(|location| {
|
||||||
let segment = &state.files[&location.file_id].trk[location.trk].trkseg[location.seg];
|
let segment = location.segment(state.files);
|
||||||
(location, segment.len())
|
(location, segment.len())
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@@ -382,8 +385,8 @@ mod tests {
|
|||||||
lng: &'a [f64],
|
lng: &'a [f64],
|
||||||
lat: &'a [f64],
|
lat: &'a [f64],
|
||||||
ele: &'a [f64],
|
ele: &'a [f64],
|
||||||
) -> Route<'a> {
|
) -> Reroute<'a> {
|
||||||
Route {
|
Reroute {
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
lng,
|
lng,
|
||||||
@@ -459,7 +462,7 @@ mod tests {
|
|||||||
route(0, 0, &[1.0], &[], &[]).apply(&mut fx.state()),
|
route(0, 0, &[1.0], &[], &[]).apply(&mut fx.state()),
|
||||||
route(0, len + 1, &[], &[], &[]).apply(&mut fx.state()),
|
route(0, len + 1, &[], &[], &[]).apply(&mut fx.state()),
|
||||||
route(len + 1, len + 1, &[1.0], &[1.0], &[1.0]).apply(&mut fx.state()),
|
route(len + 1, len + 1, &[1.0], &[1.0], &[1.0]).apply(&mut fx.state()),
|
||||||
Route {
|
Reroute {
|
||||||
anchors: &[1],
|
anchors: &[1],
|
||||||
..route(0, 0, &[1.0], &[1.0], &[1.0])
|
..route(0, 0, &[1.0], &[1.0], &[1.0])
|
||||||
}
|
}
|
||||||
@@ -589,7 +592,7 @@ mod tests {
|
|||||||
|
|
||||||
// 5 new points replacing the 2 in the middle, two of them are anchors
|
// 5 new points replacing the 2 in the middle, two of them are anchors
|
||||||
let (lng, lat, ele) = ([1.0; 5], [2.0; 5], [3.0; 5]);
|
let (lng, lat, ele) = ([1.0; 5], [2.0; 5], [3.0; 5]);
|
||||||
Route {
|
Reroute {
|
||||||
anchors: &[1, 3],
|
anchors: &[1, 3],
|
||||||
..route(1, 3, &lng, &lat, &ele)
|
..route(1, 3, &lng, &lat, &ele)
|
||||||
}
|
}
|
||||||
@@ -639,7 +642,7 @@ mod tests {
|
|||||||
let names = ["asphalt".to_string(), "gravel".to_string()];
|
let names = ["asphalt".to_string(), "gravel".to_string()];
|
||||||
let (lng, lat, ele) = ([1.0; 4], [2.0; 4], [3.0; 4]);
|
let (lng, lat, ele) = ([1.0; 4], [2.0; 4], [3.0; 4]);
|
||||||
// asphalt, asphalt, unknown, gravel
|
// asphalt, asphalt, unknown, gravel
|
||||||
Route {
|
Reroute {
|
||||||
surface: RouteCategory {
|
surface: RouteCategory {
|
||||||
starts: &[0, 2, 3],
|
starts: &[0, 2, 3],
|
||||||
values: &[1, 0, 2],
|
values: &[1, 0, 2],
|
||||||
@@ -683,7 +686,7 @@ mod tests {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
for surface in broken {
|
for surface in broken {
|
||||||
let result = Route {
|
let result = Reroute {
|
||||||
surface,
|
surface,
|
||||||
..route(0, 0, &lng, &lat, &ele)
|
..route(0, 0, &lng, &lat, &ele)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Apply, CommandError, File, Selection, State, Track, TrackSegment, Waypoints, copy_file,
|
Apply, CommandError, File, Route, SegmentIndex, Selection, State, Track, TrackSegment,
|
||||||
copy_segment, copy_track, copy_waypoint,
|
Waypoints, copy_file, copy_route, copy_segment, copy_track, copy_waypoint,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::route::find_target;
|
use super::route::find_target;
|
||||||
@@ -13,24 +13,27 @@ use super::route::find_target;
|
|||||||
pub enum SplitType {
|
pub enum SplitType {
|
||||||
/// The file becomes two files, which both have the waypoints (copies, in the second one).
|
/// The file becomes two files, which both have the waypoints (copies, in the second one).
|
||||||
Files,
|
Files,
|
||||||
|
/// The route becomes two routes.
|
||||||
|
Routes,
|
||||||
/// The track becomes two tracks.
|
/// The track becomes two tracks.
|
||||||
Tracks,
|
Tracks,
|
||||||
/// The segment becomes two segments.
|
/// The segment of a track becomes two segments.
|
||||||
Segments,
|
Segments,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cuts the file, the track or the segment of the trackpoint `at` of the selection (see
|
/// Cuts the file, the route, the track or the segment of the trackpoint `at` of the selection (see
|
||||||
/// `RoutingBuffer`) in two, there: the trackpoint is the last one of the first part and the first
|
/// `RoutingBuffer`) in two, there: the trackpoint is the last one of the first part and the first
|
||||||
/// one of the second part.
|
/// one of the second part. Cutting a route needs the trackpoint to be in a route, and cutting a
|
||||||
|
/// track or a segment needs it to be in a track: there is nothing to do otherwise.
|
||||||
///
|
///
|
||||||
/// The first part keeps the ids of what is cut, the second one has new ids, as the elements that
|
/// The first part keeps the ids of what is cut, the second one has new ids, as the elements that
|
||||||
/// come after the trackpoint (the following segments of the track, the following tracks of the
|
/// come after the trackpoint (the following segments of the track, the following routes and
|
||||||
/// file), which are moved to it. When the file is cut, the new file comes right after it. The
|
/// tracks of the file, the routes coming before the tracks), which are moved to it. When the file
|
||||||
/// selection does not change.
|
/// is cut, the new file comes right after it. The selection does not change.
|
||||||
///
|
///
|
||||||
/// With `keep_originals`, a file that is cut (`SplitType::Files`) is left as it is: its two parts
|
/// With `keep_originals`, a file that is cut (`SplitType::Files`) is left as it is: its two parts
|
||||||
/// are two new files, with new ids, which come right after it and are selected. Cutting a track
|
/// are two new files, with new ids, which come right after it and are selected. Cutting a route, a
|
||||||
/// or a segment changes the file in place anyway, so the flag has no effect on them.
|
/// track or a segment changes the file in place anyway, so the flag has no effect on them.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Split {
|
pub struct Split {
|
||||||
pub at: u32,
|
pub at: u32,
|
||||||
@@ -43,104 +46,115 @@ impl Apply for Split {
|
|||||||
let at = self.at as usize;
|
let at = self.at as usize;
|
||||||
let target = find_target(state, at, at)?.ok_or(CommandError::NothingToDo)?;
|
let target = find_target(state, at, at)?.ok_or(CommandError::NothingToDo)?;
|
||||||
let location = target.location;
|
let location = target.location;
|
||||||
let (file_id, trk, seg, index) =
|
let (file_id, index) = (location.file_id, target.start);
|
||||||
(location.file_id, location.trk, location.seg, target.start);
|
let in_route = matches!(location.index, SegmentIndex::Route(_));
|
||||||
|
let fits = match self.split_type {
|
||||||
|
SplitType::Files => true,
|
||||||
|
SplitType::Routes => in_route,
|
||||||
|
SplitType::Tracks | SplitType::Segments => !in_route,
|
||||||
|
};
|
||||||
|
if !fits {
|
||||||
|
return Err(CommandError::NothingToDo);
|
||||||
|
}
|
||||||
let mut file = (*state.files[&file_id]).clone();
|
let mut file = (*state.files[&file_id]).clone();
|
||||||
if index >= file.trk[trk].trkseg[seg].len() {
|
if index >= file.segment(location.index).len() {
|
||||||
return Err(CommandError::InvalidData(
|
return Err(CommandError::InvalidData(
|
||||||
"trackpoint out of the segment".into(),
|
"trackpoint out of the segment".into(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let track = file.trk[trk].clone();
|
// with `SplitType::Files`, what goes to the second file: what comes after the trackpoint
|
||||||
let segment = &track.trkseg[seg];
|
let mut second_file = File {
|
||||||
let (first, second) = (keep_up_to(segment, index), keep_from(segment, index));
|
id: Default::default(),
|
||||||
// the track is cut: what is before the segment, and its first part, then the second part
|
rte: vec![],
|
||||||
// and what is after it
|
trk: vec![],
|
||||||
let split_track = || {
|
wpt: Waypoints::default(),
|
||||||
let mut first_track = track.clone();
|
..file.clone()
|
||||||
first_track.trkseg.truncate(seg);
|
|
||||||
first_track.trkseg.push(first.clone());
|
|
||||||
let mut second_track = Track {
|
|
||||||
id: Default::default(),
|
|
||||||
trkseg: vec![second.clone()],
|
|
||||||
..track.clone()
|
|
||||||
};
|
|
||||||
second_track
|
|
||||||
.trkseg
|
|
||||||
.extend(track.trkseg[seg + 1..].iter().map(copy_segment));
|
|
||||||
(first_track, second_track)
|
|
||||||
};
|
};
|
||||||
|
match location.index {
|
||||||
match self.split_type {
|
SegmentIndex::Route(rte) => {
|
||||||
SplitType::Segments => {
|
let route = file.rte[rte].clone();
|
||||||
file.trk[trk].trkseg.splice(seg..=seg, [first, second]);
|
let first = Route {
|
||||||
}
|
seg: keep_up_to(&route.seg, index),
|
||||||
SplitType::Tracks => {
|
..route.clone()
|
||||||
let (first_track, second_track) = split_track();
|
};
|
||||||
file.trk.splice(trk..=trk, [first_track, second_track]);
|
let second = Route {
|
||||||
}
|
|
||||||
SplitType::Files if self.keep_originals => {
|
|
||||||
let (first_track, second_track) = split_track();
|
|
||||||
// the parts are new files: nothing is shared by id with the file, nor between them
|
|
||||||
let first_file = copy_file(&File {
|
|
||||||
trk: file.trk[..trk]
|
|
||||||
.iter()
|
|
||||||
.cloned()
|
|
||||||
.chain([first_track])
|
|
||||||
.collect(),
|
|
||||||
..file.clone()
|
|
||||||
});
|
|
||||||
let mut second_file = File {
|
|
||||||
id: Default::default(),
|
id: Default::default(),
|
||||||
trk: vec![second_track],
|
seg: keep_from(&route.seg, index),
|
||||||
wpt: Waypoints::default(),
|
..route.clone()
|
||||||
..file.clone()
|
|
||||||
};
|
};
|
||||||
second_file
|
if self.split_type == SplitType::Routes {
|
||||||
.trk
|
file.rte.splice(rte..=rte, [first, second]);
|
||||||
.extend(file.trk[trk + 1..].iter().map(copy_track));
|
state.files.insert(file_id, Rc::new(file));
|
||||||
second_file
|
return Ok(());
|
||||||
.wpt
|
}
|
||||||
.insert_at(0, file.wpt.iter().map(copy_waypoint).collect());
|
// the following routes, and the tracks, which come after the routes
|
||||||
|
second_file.rte = [second]
|
||||||
let ids = [first_file.id, second_file.id];
|
.into_iter()
|
||||||
let position = state.order.0.iter().position(|id| *id == file_id);
|
.chain(file.rte[rte + 1..].iter().map(copy_route))
|
||||||
let at = position.map_or(state.order.0.len(), |p| p + 1);
|
.collect();
|
||||||
state.order.0.splice(at..at, ids);
|
second_file.trk = file.trk.iter().map(copy_track).collect();
|
||||||
state.files.insert(first_file.id, Rc::new(first_file));
|
file.rte.truncate(rte);
|
||||||
state.files.insert(second_file.id, Rc::new(second_file));
|
file.rte.push(first);
|
||||||
*state.selection = Selection::File {
|
file.trk.clear();
|
||||||
file_ids: ids.into(),
|
|
||||||
};
|
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
SplitType::Files => {
|
SegmentIndex::Track { trk, seg } => {
|
||||||
let (first_track, second_track) = split_track();
|
let track = file.trk[trk].clone();
|
||||||
let mut second_file = File {
|
let segment = &track.trkseg[seg];
|
||||||
|
let (first, second) = (keep_up_to(segment, index), keep_from(segment, index));
|
||||||
|
if self.split_type == SplitType::Segments {
|
||||||
|
file.trk[trk].trkseg.splice(seg..=seg, [first, second]);
|
||||||
|
state.files.insert(file_id, Rc::new(file));
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
// the track is cut: what is before the segment, and its first part, then the
|
||||||
|
// second part and what is after it
|
||||||
|
let mut first_track = track.clone();
|
||||||
|
first_track.trkseg.truncate(seg);
|
||||||
|
first_track.trkseg.push(first);
|
||||||
|
let mut second_track = Track {
|
||||||
id: Default::default(),
|
id: Default::default(),
|
||||||
trk: vec![second_track],
|
trkseg: vec![second],
|
||||||
wpt: Waypoints::default(),
|
..track.clone()
|
||||||
..file.clone()
|
|
||||||
};
|
};
|
||||||
second_file
|
second_track
|
||||||
.trk
|
.trkseg
|
||||||
.extend(file.trk[trk + 1..].iter().map(copy_track));
|
.extend(track.trkseg[seg + 1..].iter().map(copy_segment));
|
||||||
second_file
|
if self.split_type == SplitType::Tracks {
|
||||||
.wpt
|
file.trk.splice(trk..=trk, [first_track, second_track]);
|
||||||
.insert_at(0, file.wpt.iter().map(copy_waypoint).collect());
|
state.files.insert(file_id, Rc::new(file));
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
second_file.trk = [second_track]
|
||||||
|
.into_iter()
|
||||||
|
.chain(file.trk[trk + 1..].iter().map(copy_track))
|
||||||
|
.collect();
|
||||||
file.trk.truncate(trk);
|
file.trk.truncate(trk);
|
||||||
file.trk.push(first_track);
|
file.trk.push(first_track);
|
||||||
let position = state.order.0.iter().position(|id| *id == file_id);
|
|
||||||
state.order.0.insert(
|
|
||||||
position.map_or(state.order.0.len(), |p| p + 1),
|
|
||||||
second_file.id,
|
|
||||||
);
|
|
||||||
state.files.insert(second_file.id, Rc::new(second_file));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
state.files.insert(file_id, Rc::new(file));
|
|
||||||
|
// the file is cut: both parts have the waypoints
|
||||||
|
second_file
|
||||||
|
.wpt
|
||||||
|
.insert_at(0, file.wpt.iter().map(copy_waypoint).collect());
|
||||||
|
let position = state.order.0.iter().position(|id| *id == file_id);
|
||||||
|
let at = position.map_or(state.order.0.len(), |p| p + 1);
|
||||||
|
if self.keep_originals {
|
||||||
|
// the parts are new files: nothing is shared by id with the file, nor between them
|
||||||
|
let first_file = copy_file(&file);
|
||||||
|
let ids = [first_file.id, second_file.id];
|
||||||
|
state.order.0.splice(at..at, ids);
|
||||||
|
state.files.insert(first_file.id, Rc::new(first_file));
|
||||||
|
state.files.insert(second_file.id, Rc::new(second_file));
|
||||||
|
*state.selection = Selection::File {
|
||||||
|
file_ids: ids.into(),
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
state.order.0.insert(at, second_file.id);
|
||||||
|
state.files.insert(second_file.id, Rc::new(second_file));
|
||||||
|
state.files.insert(file_id, Rc::new(file));
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,10 +45,8 @@ impl Apply for Time {
|
|||||||
let locations = state
|
let locations = state
|
||||||
.selection
|
.selection
|
||||||
.segment_locations(state.files, &state.order.0);
|
.segment_locations(state.files, &state.order.0);
|
||||||
let segments: Vec<&TrackSegment> = locations
|
let segments: Vec<&TrackSegment> =
|
||||||
.iter()
|
locations.iter().map(|l| l.segment(state.files)).collect();
|
||||||
.map(|l| &state.files[&l.file_id].trk[l.trk].trkseg[l.seg])
|
|
||||||
.collect();
|
|
||||||
if segments.iter().all(|segment| segment.is_empty()) {
|
if segments.iter().all(|segment| segment.is_empty()) {
|
||||||
return Err(CommandError::NothingToDo);
|
return Err(CommandError::NothingToDo);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
|
|||||||
use crate::{FileId, FileWaypointsRevisionId, Files, TrackSegmentId, TrackSegmentRevisionId};
|
use crate::{FileId, FileWaypointsRevisionId, Files, TrackSegmentId, TrackSegmentRevisionId};
|
||||||
|
|
||||||
/// Coordinates buffers, as flat `[lng, lat, lng, lat, ...]` arrays so that they can be handed
|
/// Coordinates buffers, as flat `[lng, lat, lng, lat, ...]` arrays so that they can be handed
|
||||||
/// to the UI without per-point calls: one per track segment (its trackpoints) and one per file
|
/// to the UI without per-point calls: one per segment of a route or track (its trackpoints) and one per file
|
||||||
/// (its waypoints). A buffer is only rebuilt when the revision of its segment (or the waypoints
|
/// (its waypoints). A buffer is only rebuilt when the revision of its segment (or the waypoints
|
||||||
/// of its file) changes, so editing a segment does not touch the buffers of the others.
|
/// of its file) changes, so editing a segment does not touch the buffers of the others.
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
@@ -30,7 +30,7 @@ impl CoordinatesCache {
|
|||||||
self.waypoints
|
self.waypoints
|
||||||
.insert(file.id, (file.wpt.rev_id, coordinates));
|
.insert(file.id, (file.wpt.rev_id, coordinates));
|
||||||
}
|
}
|
||||||
for seg in file.trk.iter().flat_map(|trk| &trk.trkseg) {
|
for seg in file.segments() {
|
||||||
segments.insert(seg.id);
|
segments.insert(seg.id);
|
||||||
if self
|
if self
|
||||||
.segments
|
.segments
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
File, FileId, FileWaypointsRevisionId, TrackId, TrackSegmentId, TrackSegmentRevisionId,
|
File, FileId, FileWaypointsRevisionId, RouteId, TrackId, TrackInfo, TrackSegment,
|
||||||
WaypointId,
|
TrackSegmentId, TrackSegmentRevisionId, WaypointId,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// What the UI needs to display a file: its name and the structure of its tracks and waypoints,
|
/// What the UI needs to display a file: its name and the structure of its routes, tracks and
|
||||||
|
/// waypoints,
|
||||||
/// with the ids that reference every element. Coordinates are not part of it, they are read
|
/// with the ids that reference every element. Coordinates are not part of it, they are read
|
||||||
/// from the buffers of the [`CoordinatesCache`](crate::CoordinatesCache) (the `i`-th waypoint
|
/// from the buffers of the [`CoordinatesCache`](crate::CoordinatesCache) (the `i`-th waypoint
|
||||||
/// of a file is the `i`-th coordinates pair of its waypoint buffer, same for the trackpoints of
|
/// of a file is the `i`-th coordinates pair of its waypoint buffer, same for the trackpoints of
|
||||||
@@ -13,21 +14,47 @@ pub struct FileStructure {
|
|||||||
pub id: FileId,
|
pub id: FileId,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub desc: Option<String>,
|
pub desc: Option<String>,
|
||||||
|
pub routes: Vec<RouteNode>,
|
||||||
pub tracks: Vec<TrackNode>,
|
pub tracks: Vec<TrackNode>,
|
||||||
pub waypoints: Vec<WaypointNode>,
|
pub waypoints: Vec<WaypointNode>,
|
||||||
/// Changes whenever the waypoints of the file (hence their buffer) change.
|
/// Changes whenever the waypoints of the file (hence their buffer) change.
|
||||||
pub wpt_rev_id: FileWaypointsRevisionId,
|
pub wpt_rev_id: FileWaypointsRevisionId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The name, description and style of a route or a track (the style only when the file defines
|
||||||
|
/// it).
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct TrackNode {
|
pub struct PathInfo {
|
||||||
pub id: TrackId,
|
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
pub desc: Option<String>,
|
pub desc: Option<String>,
|
||||||
/// Style of the track, only when the file defines it.
|
|
||||||
pub color: Option<String>,
|
pub color: Option<String>,
|
||||||
pub opacity: Option<f64>,
|
pub opacity: Option<f64>,
|
||||||
pub width: Option<f64>,
|
pub width: Option<f64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<&TrackInfo> for PathInfo {
|
||||||
|
fn from(info: &TrackInfo) -> Self {
|
||||||
|
Self {
|
||||||
|
name: info.name.clone(),
|
||||||
|
desc: info.desc.clone(),
|
||||||
|
color: info.color.clone(),
|
||||||
|
opacity: info.opacity,
|
||||||
|
width: info.width,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct RouteNode {
|
||||||
|
pub id: RouteId,
|
||||||
|
pub info: PathInfo,
|
||||||
|
pub segment: SegmentNode,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct TrackNode {
|
||||||
|
pub id: TrackId,
|
||||||
|
pub info: PathInfo,
|
||||||
pub segments: Vec<SegmentNode>,
|
pub segments: Vec<SegmentNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,31 +73,38 @@ pub struct WaypointNode {
|
|||||||
pub sym: Option<String>,
|
pub sym: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<&TrackSegment> for SegmentNode {
|
||||||
|
fn from(seg: &TrackSegment) -> Self {
|
||||||
|
Self {
|
||||||
|
id: seg.id,
|
||||||
|
rev_id: seg.rev_id,
|
||||||
|
len: seg.len(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FileStructure {
|
impl FileStructure {
|
||||||
pub fn new(file: &File) -> Self {
|
pub fn new(file: &File) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: file.id,
|
id: file.id,
|
||||||
name: file.info.name.clone(),
|
name: file.info.name.clone(),
|
||||||
desc: file.info.desc.clone(),
|
desc: file.info.desc.clone(),
|
||||||
|
routes: file
|
||||||
|
.rte
|
||||||
|
.iter()
|
||||||
|
.map(|rte| RouteNode {
|
||||||
|
id: rte.id,
|
||||||
|
info: (&rte.info).into(),
|
||||||
|
segment: (&rte.seg).into(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
tracks: file
|
tracks: file
|
||||||
.trk
|
.trk
|
||||||
.iter()
|
.iter()
|
||||||
.map(|trk| TrackNode {
|
.map(|trk| TrackNode {
|
||||||
id: trk.id,
|
id: trk.id,
|
||||||
name: trk.info.name.clone(),
|
info: (&trk.info).into(),
|
||||||
desc: trk.info.desc.clone(),
|
segments: trk.trkseg.iter().map(Into::into).collect(),
|
||||||
color: trk.info.color.clone(),
|
|
||||||
opacity: trk.info.opacity,
|
|
||||||
width: trk.info.width,
|
|
||||||
segments: trk
|
|
||||||
.trkseg
|
|
||||||
.iter()
|
|
||||||
.map(|seg| SegmentNode {
|
|
||||||
id: seg.id,
|
|
||||||
rev_id: seg.rev_id,
|
|
||||||
len: seg.len(),
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
waypoints: file
|
waypoints: file
|
||||||
@@ -112,11 +146,8 @@ mod tests {
|
|||||||
);
|
);
|
||||||
assert_eq!(node.tracks.len(), file.trk.len());
|
assert_eq!(node.tracks.len(), file.trk.len());
|
||||||
for (n, t) in node.tracks.iter().zip(&file.trk) {
|
for (n, t) in node.tracks.iter().zip(&file.trk) {
|
||||||
assert_eq!((n.id, &n.name, &n.desc), (t.id, &t.info.name, &t.info.desc));
|
assert_eq!(n.id, t.id);
|
||||||
assert_eq!(
|
assert_eq!(n.info, PathInfo::from(&t.info));
|
||||||
(&n.color, n.opacity, n.width),
|
|
||||||
(&t.info.color, t.info.opacity, t.info.width)
|
|
||||||
);
|
|
||||||
assert_eq!(n.segments.len(), t.trkseg.len());
|
assert_eq!(n.segments.len(), t.trkseg.len());
|
||||||
for (n, s) in n.segments.iter().zip(&t.trkseg) {
|
for (n, s) in n.segments.iter().zip(&t.trkseg) {
|
||||||
assert_eq!((n.id, n.rev_id, n.len), (s.id, s.rev_id, s.len()));
|
assert_eq!((n.id, n.rev_id, n.len), (s.id, s.rev_id, s.len()));
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ impl RoutingBuffer {
|
|||||||
.map(|files| selection.segment_locations(files, order))
|
.map(|files| selection.segment_locations(files, order))
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
{
|
{
|
||||||
let segment = &files.unwrap()[&location.file_id].trk[location.trk].trkseg[location.seg];
|
let segment = location.segment(files.unwrap());
|
||||||
signature.push((segment.id, segment.rev_id));
|
signature.push((segment.id, segment.rev_id));
|
||||||
if segment.is_empty() {
|
if segment.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -18,15 +18,13 @@ impl StatisticsCache {
|
|||||||
let mut stale: HashSet<TrackSegmentRevisionId> = self.map.keys().copied().collect();
|
let mut stale: HashSet<TrackSegmentRevisionId> = self.map.keys().copied().collect();
|
||||||
if let Some(state) = state {
|
if let Some(state) = state {
|
||||||
for file in state.values() {
|
for file in state.values() {
|
||||||
for trk in file.trk.iter() {
|
for trkseg in file.segments() {
|
||||||
for trkseg in trk.trkseg.iter() {
|
match self.map.entry(trkseg.rev_id) {
|
||||||
match self.map.entry(trkseg.rev_id) {
|
Entry::Occupied(_) => {
|
||||||
Entry::Occupied(_) => {
|
stale.remove(&trkseg.rev_id);
|
||||||
stale.remove(&trkseg.rev_id);
|
}
|
||||||
}
|
Entry::Vacant(e) => {
|
||||||
Entry::Vacant(e) => {
|
e.insert(Statistics::compute(trkseg));
|
||||||
e.insert(Statistics::compute(trkseg));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,6 +70,15 @@ impl StatisticsCache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Selection::Route { file_id, rte_ids } => {
|
||||||
|
if let Some(file) = state.get(file_id) {
|
||||||
|
for rte in file.rte.iter() {
|
||||||
|
if rte_ids.contains(&rte.id) {
|
||||||
|
self.add_segment_stats(&rte.seg, &mut selected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Selection::Track { file_id, trk_ids } => {
|
Selection::Track { file_id, trk_ids } => {
|
||||||
if let Some(file) = state.get(file_id) {
|
if let Some(file) = state.get(file_id) {
|
||||||
for trk in file.trk.iter() {
|
for trk in file.trk.iter() {
|
||||||
@@ -129,8 +136,8 @@ impl StatisticsCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn add_file_stats<'a>(&'a self, file: &'a File, selected: &mut Vec<SelectedSegment<'a>>) {
|
fn add_file_stats<'a>(&'a self, file: &'a File, selected: &mut Vec<SelectedSegment<'a>>) {
|
||||||
for trk in file.trk.iter() {
|
for trkseg in file.segments() {
|
||||||
self.add_track_stats(trk, selected);
|
self.add_segment_stats(trkseg, selected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ impl<H: History> Engine<H> {
|
|||||||
.find(|wpt| wpt.id == *id)
|
.find(|wpt| wpt.id == *id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A trackpoint of a segment of a file. `None` if it does not exist.
|
/// A trackpoint of a segment (of a route or a track) of a file. `None` if it does not exist.
|
||||||
pub fn trackpoint(
|
pub fn trackpoint(
|
||||||
&self,
|
&self,
|
||||||
file_id: &FileId,
|
file_id: &FileId,
|
||||||
@@ -93,9 +93,7 @@ impl<H: History> Engine<H> {
|
|||||||
.history
|
.history
|
||||||
.current()?
|
.current()?
|
||||||
.get(file_id)?
|
.get(file_id)?
|
||||||
.trk
|
.segments()
|
||||||
.iter()
|
|
||||||
.flat_map(|trk| &trk.trkseg)
|
|
||||||
.find(|seg| seg.id == *segment_id)?;
|
.find(|seg| seg.id == *segment_id)?;
|
||||||
(index < segment.len()).then(|| &segment[index])
|
(index < segment.len()).then(|| &segment[index])
|
||||||
}
|
}
|
||||||
@@ -109,11 +107,7 @@ impl<H: History> Engine<H> {
|
|||||||
self.selection
|
self.selection
|
||||||
.segment_locations(files, &self.order.0)
|
.segment_locations(files, &self.order.0)
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|location| {
|
.flat_map(|location| reduction_distances(location.segment(files)))
|
||||||
reduction_distances(
|
|
||||||
&files[&location.file_id].trk[location.trk].trkseg[location.seg],
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,18 @@ use std::rc::Rc;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
File, FileId, Files, Selection, Track, TrackId, TrackSegment, TrackSegmentId, Waypoint,
|
File, FileId, Files, Route, RouteId, Selection, Track, TrackId, TrackSegment, TrackSegmentId,
|
||||||
WaypointId,
|
Waypoint, WaypointId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// A route in the clipboard, with the name of the file it comes from (the name of the new file
|
||||||
|
/// when it is pasted on its own).
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct ClipboardRoute {
|
||||||
|
pub route: Route,
|
||||||
|
pub file_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
/// A track in the clipboard, with the name of the file it comes from (the name of the new file
|
/// A track in the clipboard, with the name of the file it comes from (the name of the new file
|
||||||
/// when it is pasted on its own).
|
/// when it is pasted on its own).
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@@ -31,6 +39,7 @@ pub struct ClipboardSegment {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ClipboardContent {
|
pub enum ClipboardContent {
|
||||||
Files(Vec<Rc<File>>),
|
Files(Vec<Rc<File>>),
|
||||||
|
Routes(Vec<ClipboardRoute>),
|
||||||
Tracks(Vec<ClipboardTrack>),
|
Tracks(Vec<ClipboardTrack>),
|
||||||
Segments(Vec<ClipboardSegment>),
|
Segments(Vec<ClipboardSegment>),
|
||||||
Waypoints(Vec<Waypoint>),
|
Waypoints(Vec<Waypoint>),
|
||||||
@@ -84,6 +93,19 @@ impl Clipboard {
|
|||||||
.filter_map(|id| files.get(id).cloned())
|
.filter_map(|id| files.get(id).cloned())
|
||||||
.collect(),
|
.collect(),
|
||||||
),
|
),
|
||||||
|
Selection::Route { file_id, rte_ids } => {
|
||||||
|
let file = files.get(file_id)?;
|
||||||
|
ClipboardContent::Routes(
|
||||||
|
file.rte
|
||||||
|
.iter()
|
||||||
|
.filter(|rte| rte_ids.contains(&rte.id))
|
||||||
|
.map(|rte| ClipboardRoute {
|
||||||
|
route: rte.clone(),
|
||||||
|
file_name: file.info.name.clone(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
Selection::Track { file_id, trk_ids } => {
|
Selection::Track { file_id, trk_ids } => {
|
||||||
let file = files.get(file_id)?;
|
let file = files.get(file_id)?;
|
||||||
ClipboardContent::Tracks(
|
ClipboardContent::Tracks(
|
||||||
@@ -144,32 +166,37 @@ impl Clipboard {
|
|||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
match &self.content {
|
match &self.content {
|
||||||
ClipboardContent::Files(files) => files.is_empty(),
|
ClipboardContent::Files(files) => files.is_empty(),
|
||||||
|
ClipboardContent::Routes(routes) => routes.is_empty(),
|
||||||
ClipboardContent::Tracks(tracks) => tracks.is_empty(),
|
ClipboardContent::Tracks(tracks) => tracks.is_empty(),
|
||||||
ClipboardContent::Segments(segments) => segments.is_empty(),
|
ClipboardContent::Segments(segments) => segments.is_empty(),
|
||||||
ClipboardContent::Waypoints(waypoints) => waypoints.is_empty(),
|
ClipboardContent::Waypoints(waypoints) => waypoints.is_empty(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether the content can be pasted when `target` is selected: files and tracks can be
|
/// Whether the content can be pasted when `target` is selected: files, routes and tracks can
|
||||||
/// pasted anywhere above them in the file tree, and nothing selected stands for the list of
|
/// be pasted anywhere above them in the file tree (routes and tracks are at the same level),
|
||||||
/// files.
|
/// and nothing selected stands for the list of files.
|
||||||
///
|
///
|
||||||
/// | copied | pasted when selected |
|
/// | copied | pasted when selected |
|
||||||
/// |-----------|------------------------------------------|
|
/// |-----------|-----------------------------------------------|
|
||||||
/// | files | nothing, a file |
|
/// | files | nothing, a file |
|
||||||
/// | tracks | nothing, a file, a track |
|
/// | routes | nothing, a file, a route, a track |
|
||||||
/// | segments | nothing, a file, a track, a segment |
|
/// | tracks | nothing, a file, a route, a track |
|
||||||
/// | waypoints | a file, the waypoints, a waypoint |
|
/// | segments | nothing, a file, a route, a track, a segment |
|
||||||
|
/// | waypoints | a file, the waypoints, a waypoint |
|
||||||
pub fn can_paste(&self, target: &Selection) -> bool {
|
pub fn can_paste(&self, target: &Selection) -> bool {
|
||||||
use ClipboardContent as C;
|
use ClipboardContent as C;
|
||||||
use Selection::*;
|
use Selection::*;
|
||||||
matches!(
|
matches!(
|
||||||
(&self.content, target),
|
(&self.content, target),
|
||||||
(C::Files(_), Empty | File { .. })
|
(C::Files(_), Empty | File { .. })
|
||||||
| (C::Tracks(_), Empty | File { .. } | Track { .. })
|
| (
|
||||||
|
C::Routes(_) | C::Tracks(_),
|
||||||
|
Empty | File { .. } | Route { .. } | Track { .. }
|
||||||
|
)
|
||||||
| (
|
| (
|
||||||
C::Segments(_),
|
C::Segments(_),
|
||||||
Empty | File { .. } | Track { .. } | TrackSegment { .. }
|
Empty | File { .. } | Route { .. } | Track { .. } | TrackSegment { .. }
|
||||||
)
|
)
|
||||||
| (
|
| (
|
||||||
C::Waypoints(_),
|
C::Waypoints(_),
|
||||||
@@ -183,6 +210,7 @@ impl Clipboard {
|
|||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum ClipboardIds {
|
pub enum ClipboardIds {
|
||||||
Files(Vec<FileId>),
|
Files(Vec<FileId>),
|
||||||
|
Routes(Vec<RouteId>),
|
||||||
Tracks(Vec<TrackId>),
|
Tracks(Vec<TrackId>),
|
||||||
Segments(Vec<TrackSegmentId>),
|
Segments(Vec<TrackSegmentId>),
|
||||||
Waypoints(Vec<WaypointId>),
|
Waypoints(Vec<WaypointId>),
|
||||||
@@ -194,6 +222,9 @@ impl ClipboardContent {
|
|||||||
ClipboardContent::Files(files) => {
|
ClipboardContent::Files(files) => {
|
||||||
ClipboardIds::Files(files.iter().map(|file| file.id).collect())
|
ClipboardIds::Files(files.iter().map(|file| file.id).collect())
|
||||||
}
|
}
|
||||||
|
ClipboardContent::Routes(routes) => {
|
||||||
|
ClipboardIds::Routes(routes.iter().map(|rte| rte.route.id).collect())
|
||||||
|
}
|
||||||
ClipboardContent::Tracks(tracks) => {
|
ClipboardContent::Tracks(tracks) => {
|
||||||
ClipboardIds::Tracks(tracks.iter().map(|trk| trk.track.id).collect())
|
ClipboardIds::Tracks(tracks.iter().map(|trk| trk.track.id).collect())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use crate::{FileId, Files, TrackId, TrackSegmentId, WaypointId};
|
use crate::{
|
||||||
|
File, FileId, Files, RouteId, SegmentIndex, TrackId, TrackSegment, TrackSegmentId, WaypointId,
|
||||||
|
};
|
||||||
|
|
||||||
/// How a new selection combines with the current one.
|
/// How a new selection combines with the current one.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
|
||||||
@@ -23,6 +25,10 @@ pub enum Selection {
|
|||||||
File {
|
File {
|
||||||
file_ids: HashSet<FileId>,
|
file_ids: HashSet<FileId>,
|
||||||
},
|
},
|
||||||
|
Route {
|
||||||
|
file_id: FileId,
|
||||||
|
rte_ids: HashSet<RouteId>,
|
||||||
|
},
|
||||||
Track {
|
Track {
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
trk_ids: HashSet<TrackId>,
|
trk_ids: HashSet<TrackId>,
|
||||||
@@ -41,12 +47,19 @@ pub enum Selection {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Where a track segment is: its file, then its track and its own position in it.
|
/// Where a segment is: its file, then its place in it (its route, or its track and its own
|
||||||
|
/// position in it).
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub struct SegmentLocation {
|
pub struct SegmentLocation {
|
||||||
pub file_id: FileId,
|
pub file_id: FileId,
|
||||||
pub trk: usize,
|
pub index: SegmentIndex,
|
||||||
pub seg: usize,
|
}
|
||||||
|
|
||||||
|
impl SegmentLocation {
|
||||||
|
/// The segment, in `files`. Panics if it is not there.
|
||||||
|
pub fn segment<'a>(&self, files: &'a Files) -> &'a TrackSegment {
|
||||||
|
files[&self.file_id].segment(self.index)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Selection {
|
impl Selection {
|
||||||
@@ -89,23 +102,18 @@ impl Selection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Selection {
|
impl Selection {
|
||||||
/// The track segments covered by the selection, in order: the segments of several selected
|
/// The segments covered by the selection, in order: the segments of several selected files
|
||||||
/// files follow `order` (the files missing from it come last), then the tracks and segments
|
/// follow `order` (the files missing from it come last), then the routes, tracks and segments
|
||||||
/// follow the file. Waypoints are not segments: selecting only waypoints covers none.
|
/// follow the file (see [`File::segments`]). Waypoints are not segments: selecting only
|
||||||
|
/// waypoints covers none.
|
||||||
pub fn segment_locations(&self, files: &Files, order: &[FileId]) -> Vec<SegmentLocation> {
|
pub fn segment_locations(&self, files: &Files, order: &[FileId]) -> Vec<SegmentLocation> {
|
||||||
let mut locations = vec![];
|
let mut locations = vec![];
|
||||||
let mut add = |file_id: FileId,
|
let mut add = |file_id: FileId, filter: &dyn Fn(&File, SegmentIndex) -> bool| {
|
||||||
trk_filter: &dyn Fn(&crate::Track) -> bool,
|
|
||||||
seg_filter: &dyn Fn(&crate::TrackSegment) -> bool| {
|
|
||||||
let Some(file) = files.get(&file_id) else {
|
let Some(file) = files.get(&file_id) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
for (trk, track) in file.trk.iter().enumerate().filter(|(_, t)| trk_filter(t)) {
|
for index in file.segment_indices().filter(|index| filter(file, *index)) {
|
||||||
for (seg, segment) in track.trkseg.iter().enumerate() {
|
locations.push(SegmentLocation { file_id, index });
|
||||||
if seg_filter(segment) {
|
|
||||||
locations.push(SegmentLocation { file_id, trk, seg });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match self {
|
match self {
|
||||||
@@ -116,25 +124,35 @@ impl Selection {
|
|||||||
.filter(|id| file_ids.contains(id))
|
.filter(|id| file_ids.contains(id))
|
||||||
.chain(file_ids.iter().filter(|id| !order.contains(id)));
|
.chain(file_ids.iter().filter(|id| !order.contains(id)));
|
||||||
for id in ordered {
|
for id in ordered {
|
||||||
add(*id, &|_| true, &|_| true);
|
add(*id, &|_, _| true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Selection::Track { file_id, trk_ids } => {
|
Selection::Route { file_id, rte_ids } => add(*file_id, &|file, index| match index {
|
||||||
add(*file_id, &|trk| trk_ids.contains(&trk.id), &|_| true)
|
SegmentIndex::Route(rte) => rte_ids.contains(&file.rte[rte].id),
|
||||||
}
|
SegmentIndex::Track { .. } => false,
|
||||||
|
}),
|
||||||
|
Selection::Track { file_id, trk_ids } => add(*file_id, &|file, index| match index {
|
||||||
|
SegmentIndex::Track { trk, .. } => trk_ids.contains(&file.trk[trk].id),
|
||||||
|
SegmentIndex::Route(_) => false,
|
||||||
|
}),
|
||||||
Selection::TrackSegment {
|
Selection::TrackSegment {
|
||||||
file_id,
|
file_id,
|
||||||
trk_id,
|
trk_id,
|
||||||
trkseg_ids,
|
trkseg_ids,
|
||||||
} => add(*file_id, &|trk| trk.id == *trk_id, &|seg| {
|
} => add(*file_id, &|file, index| match index {
|
||||||
trkseg_ids.contains(&seg.id)
|
SegmentIndex::Track { trk, seg } => {
|
||||||
|
let track = &file.trk[trk];
|
||||||
|
track.id == *trk_id && trkseg_ids.contains(&track.trkseg[seg].id)
|
||||||
|
}
|
||||||
|
SegmentIndex::Route(_) => false,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
locations
|
locations
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Drops what does not exist anymore (e.g. after an undo): files, and the tracks, segments
|
/// Drops what does not exist anymore (e.g. after an undo): files, and the routes, tracks,
|
||||||
/// and waypoints of the files that remain. A selection left with nothing becomes empty.
|
/// segments and waypoints of the files that remain. A selection left with nothing becomes
|
||||||
|
/// empty.
|
||||||
pub fn retain_existing(&mut self, files: &Files) {
|
pub fn retain_existing(&mut self, files: &Files) {
|
||||||
let empty = match self {
|
let empty = match self {
|
||||||
Selection::Empty => false,
|
Selection::Empty => false,
|
||||||
@@ -142,6 +160,13 @@ impl Selection {
|
|||||||
file_ids.retain(|id| files.contains_key(id));
|
file_ids.retain(|id| files.contains_key(id));
|
||||||
file_ids.is_empty()
|
file_ids.is_empty()
|
||||||
}
|
}
|
||||||
|
Selection::Route { file_id, rte_ids } => match files.get(file_id) {
|
||||||
|
Some(file) => {
|
||||||
|
rte_ids.retain(|id| file.rte.iter().any(|rte| rte.id == *id));
|
||||||
|
rte_ids.is_empty()
|
||||||
|
}
|
||||||
|
None => true,
|
||||||
|
},
|
||||||
Selection::Track { file_id, trk_ids } => match files.get(file_id) {
|
Selection::Track { file_id, trk_ids } => match files.get(file_id) {
|
||||||
Some(file) => {
|
Some(file) => {
|
||||||
trk_ids.retain(|id| file.trk.iter().any(|trk| trk.id == *id));
|
trk_ids.retain(|id| file.trk.iter().any(|trk| trk.id == *id));
|
||||||
@@ -185,6 +210,13 @@ impl Selection {
|
|||||||
(Selection::File { file_ids }, Selection::File { file_ids: other }) => {
|
(Selection::File { file_ids }, Selection::File { file_ids: other }) => {
|
||||||
file_ids.extend(other)
|
file_ids.extend(other)
|
||||||
}
|
}
|
||||||
|
(
|
||||||
|
Selection::Route { file_id, rte_ids },
|
||||||
|
Selection::Route {
|
||||||
|
file_id: other_file,
|
||||||
|
rte_ids: other,
|
||||||
|
},
|
||||||
|
) if *file_id == other_file => rte_ids.extend(other),
|
||||||
(
|
(
|
||||||
Selection::Track { file_id, trk_ids },
|
Selection::Track { file_id, trk_ids },
|
||||||
Selection::Track {
|
Selection::Track {
|
||||||
@@ -232,6 +264,16 @@ impl Selection {
|
|||||||
toggle_ids(file_ids, other);
|
toggle_ids(file_ids, other);
|
||||||
file_ids.is_empty()
|
file_ids.is_empty()
|
||||||
}
|
}
|
||||||
|
(
|
||||||
|
Selection::Route { file_id, rte_ids },
|
||||||
|
Selection::Route {
|
||||||
|
file_id: other_file,
|
||||||
|
rte_ids: other,
|
||||||
|
},
|
||||||
|
) if *file_id == other_file => {
|
||||||
|
toggle_ids(rte_ids, other);
|
||||||
|
rte_ids.is_empty()
|
||||||
|
}
|
||||||
(
|
(
|
||||||
Selection::Track { file_id, trk_ids },
|
Selection::Track { file_id, trk_ids },
|
||||||
Selection::Track {
|
Selection::Track {
|
||||||
@@ -325,7 +367,8 @@ impl Selection {
|
|||||||
match self {
|
match self {
|
||||||
Selection::Empty => Selection::Empty,
|
Selection::Empty => Selection::Empty,
|
||||||
Selection::File { .. } => self.clone(),
|
Selection::File { .. } => self.clone(),
|
||||||
Selection::Track { file_id, .. }
|
Selection::Route { file_id, .. }
|
||||||
|
| Selection::Track { file_id, .. }
|
||||||
| Selection::TrackSegment { file_id, .. }
|
| Selection::TrackSegment { file_id, .. }
|
||||||
| Selection::Waypoints { file_id }
|
| Selection::Waypoints { file_id }
|
||||||
| Selection::Waypoint { file_id, .. } => Selection::File {
|
| Selection::Waypoint { file_id, .. } => Selection::File {
|
||||||
@@ -335,7 +378,7 @@ impl Selection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Selects all the elements of the same kind as the selected ones, in the same place: all
|
/// Selects all the elements of the same kind as the selected ones, in the same place: all
|
||||||
/// the files, all the tracks of the file, all the segments of the track, all the waypoints
|
/// the files, all the routes (or tracks) of the file, all the segments of the track, all the waypoints
|
||||||
/// of the file. All the files when nothing is selected. `None` when there is nothing to
|
/// of the file. All the files when nothing is selected. `None` when there is nothing to
|
||||||
/// select (the waypoints node, or a file that does not exist).
|
/// select (the waypoints node, or a file that does not exist).
|
||||||
pub fn all_at_level(&self, files: &Files, order: &[FileId]) -> Option<Selection> {
|
pub fn all_at_level(&self, files: &Files, order: &[FileId]) -> Option<Selection> {
|
||||||
@@ -343,6 +386,10 @@ impl Selection {
|
|||||||
Selection::Empty | Selection::File { .. } => Some(Selection::File {
|
Selection::Empty | Selection::File { .. } => Some(Selection::File {
|
||||||
file_ids: order.iter().copied().collect(),
|
file_ids: order.iter().copied().collect(),
|
||||||
}),
|
}),
|
||||||
|
Selection::Route { file_id, .. } => Some(Selection::Route {
|
||||||
|
file_id: *file_id,
|
||||||
|
rte_ids: files.get(file_id)?.rte.iter().map(|rte| rte.id).collect(),
|
||||||
|
}),
|
||||||
Selection::Track { file_id, .. } => Some(Selection::Track {
|
Selection::Track { file_id, .. } => Some(Selection::Track {
|
||||||
file_id: *file_id,
|
file_id: *file_id,
|
||||||
trk_ids: files.get(file_id)?.trk.iter().map(|trk| trk.id).collect(),
|
trk_ids: files.get(file_id)?.trk.iter().map(|trk| trk.id).collect(),
|
||||||
@@ -371,7 +418,7 @@ impl Selection {
|
|||||||
|
|
||||||
/// The element next to the selected ones, as a selection of that one element: the next one
|
/// The element next to the selected ones, as a selection of that one element: the next one
|
||||||
/// (`down`) or the previous one among the elements of the same kind, in the same place.
|
/// (`down`) or the previous one among the elements of the same kind, in the same place.
|
||||||
/// The tracks, segments and waypoints stop at their ends, the files wrap around and skip
|
/// The routes, tracks, segments and waypoints stop at their ends, the files wrap around and skip
|
||||||
/// the ones that are already selected. `None` when there is no such element.
|
/// the ones that are already selected. `None` when there is no such element.
|
||||||
pub fn neighbour(&self, files: &Files, order: &[FileId], down: bool) -> Option<Selection> {
|
pub fn neighbour(&self, files: &Files, order: &[FileId], down: bool) -> Option<Selection> {
|
||||||
match self {
|
match self {
|
||||||
@@ -393,6 +440,13 @@ impl Selection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Selection::Route { file_id, rte_ids } => {
|
||||||
|
let ids: Vec<RouteId> = files.get(file_id)?.rte.iter().map(|r| r.id).collect();
|
||||||
|
Some(Selection::Route {
|
||||||
|
file_id: *file_id,
|
||||||
|
rte_ids: [neighbour(&ids, rte_ids, down)?].into(),
|
||||||
|
})
|
||||||
|
}
|
||||||
Selection::Track { file_id, trk_ids } => {
|
Selection::Track { file_id, trk_ids } => {
|
||||||
let ids: Vec<TrackId> = files.get(file_id)?.trk.iter().map(|t| t.id).collect();
|
let ids: Vec<TrackId> = files.get(file_id)?.trk.iter().map(|t| t.id).collect();
|
||||||
Some(Selection::Track {
|
Some(Selection::Track {
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ use serde::{Deserialize, Serialize};
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
File, FileId, FileInfo, Track, TrackId, TrackInfo, TrackSegment, TrackSegmentId,
|
File, FileId, FileInfo, Route, RouteId, Track, TrackId, TrackInfo, TrackSegment,
|
||||||
TrackpointCategories, TrackpointChunk, TrackpointChunkId, Waypoint, WaypointChunk,
|
TrackSegmentId, TrackpointCategories, TrackpointChunk, TrackpointChunkId, Waypoint,
|
||||||
WaypointChunkId, Waypoints,
|
WaypointChunk, WaypointChunkId, Waypoints,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The version that is written.
|
/// The version that is written.
|
||||||
@@ -54,10 +54,17 @@ fn parse<'a, T: Deserialize<'a>>(data: &'a [u8]) -> Result<T, DecodeError> {
|
|||||||
/// A file without its trackpoints and waypoints, which are in chunks.
|
/// A file without its trackpoints and waypoints, which are in chunks.
|
||||||
pub struct FileRecord {
|
pub struct FileRecord {
|
||||||
info: FileInfo,
|
info: FileInfo,
|
||||||
|
routes: Vec<RouteRecord>,
|
||||||
tracks: Vec<TrackRecord>,
|
tracks: Vec<TrackRecord>,
|
||||||
waypoints: Vec<WaypointChunkId>,
|
waypoints: Vec<WaypointChunkId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct RouteRecord {
|
||||||
|
id: RouteId,
|
||||||
|
info: TrackInfo,
|
||||||
|
segment: SegmentRecord,
|
||||||
|
}
|
||||||
|
|
||||||
struct TrackRecord {
|
struct TrackRecord {
|
||||||
id: TrackId,
|
id: TrackId,
|
||||||
info: TrackInfo,
|
info: TrackInfo,
|
||||||
@@ -69,24 +76,35 @@ struct SegmentRecord {
|
|||||||
chunks: Vec<TrackpointChunkId>,
|
chunks: Vec<TrackpointChunkId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<v1::SegmentRecord1> for SegmentRecord {
|
||||||
|
fn from(segment: v1::SegmentRecord1) -> Self {
|
||||||
|
Self {
|
||||||
|
id: TrackSegmentId(segment.id),
|
||||||
|
chunks: segment.chunks.into_iter().map(TrackpointChunkId).collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<v1::FileRecord1> for FileRecord {
|
impl From<v1::FileRecord1> for FileRecord {
|
||||||
fn from(record: v1::FileRecord1) -> Self {
|
fn from(record: v1::FileRecord1) -> Self {
|
||||||
Self {
|
Self {
|
||||||
info: record.info.into(),
|
info: record.info.into(),
|
||||||
|
routes: record
|
||||||
|
.routes
|
||||||
|
.into_iter()
|
||||||
|
.map(|route| RouteRecord {
|
||||||
|
id: RouteId(route.id),
|
||||||
|
info: route.info.into(),
|
||||||
|
segment: route.segment.into(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
tracks: record
|
tracks: record
|
||||||
.tracks
|
.tracks
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|track| TrackRecord {
|
.map(|track| TrackRecord {
|
||||||
id: TrackId(track.id),
|
id: TrackId(track.id),
|
||||||
info: track.info.into(),
|
info: track.info.into(),
|
||||||
segments: track
|
segments: track.segments.into_iter().map(Into::into).collect(),
|
||||||
.segments
|
|
||||||
.into_iter()
|
|
||||||
.map(|segment| SegmentRecord {
|
|
||||||
id: TrackSegmentId(segment.id),
|
|
||||||
chunks: segment.chunks.into_iter().map(TrackpointChunkId).collect(),
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
waypoints: record.waypoints.into_iter().map(WaypointChunkId).collect(),
|
waypoints: record.waypoints.into_iter().map(WaypointChunkId).collect(),
|
||||||
@@ -108,16 +126,27 @@ impl FileRecord {
|
|||||||
info: self.info,
|
info: self.info,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
for track in self.tracks {
|
let segment = |record: SegmentRecord| {
|
||||||
let mut trkseg = Vec::with_capacity(track.segments.len());
|
let mut built = TrackSegment::default();
|
||||||
for segment in track.segments {
|
built.id = record.id;
|
||||||
let mut built = TrackSegment::default();
|
for chunk in &record.chunks {
|
||||||
built.id = segment.id;
|
built.push_shared(trackpoint_chunks(chunk)?);
|
||||||
for chunk in &segment.chunks {
|
|
||||||
built.push_shared(trackpoint_chunks(chunk)?);
|
|
||||||
}
|
|
||||||
trkseg.push(built);
|
|
||||||
}
|
}
|
||||||
|
Some(built)
|
||||||
|
};
|
||||||
|
for route in self.routes {
|
||||||
|
file.rte.push(Route {
|
||||||
|
id: route.id,
|
||||||
|
info: route.info,
|
||||||
|
seg: segment(route.segment)?,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
for track in self.tracks {
|
||||||
|
let trkseg = track
|
||||||
|
.segments
|
||||||
|
.into_iter()
|
||||||
|
.map(segment)
|
||||||
|
.collect::<Option<Vec<_>>>()?;
|
||||||
file.trk.push(Track {
|
file.trk.push(Track {
|
||||||
id: track.id,
|
id: track.id,
|
||||||
info: track.info,
|
info: track.info,
|
||||||
@@ -292,6 +321,20 @@ mod tests {
|
|||||||
});
|
});
|
||||||
let mut empty = TrackSegment::default();
|
let mut empty = TrackSegment::default();
|
||||||
empty.id = TrackSegmentId(id(6));
|
empty.id = TrackSegmentId(id(6));
|
||||||
|
// a route, whose segment shares the chunk of the track
|
||||||
|
let mut route_segment = TrackSegment::default();
|
||||||
|
route_segment.id = TrackSegmentId(id(10));
|
||||||
|
route_segment.push_shared(segment.chunks()[0].clone());
|
||||||
|
file.rte.push(Route {
|
||||||
|
id: RouteId(id(9)),
|
||||||
|
info: TrackInfo {
|
||||||
|
name: Some("route".into()),
|
||||||
|
type_: Some("Hiking".into()),
|
||||||
|
color: Some("00ff00".into()),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
seg: route_segment,
|
||||||
|
});
|
||||||
file.trk.push(Track {
|
file.trk.push(Track {
|
||||||
id: TrackId(id(2)),
|
id: TrackId(id(2)),
|
||||||
info: TrackInfo {
|
info: TrackInfo {
|
||||||
@@ -352,9 +395,7 @@ mod tests {
|
|||||||
HashMap<WaypointChunkId, Rc<WaypointChunk>>,
|
HashMap<WaypointChunkId, Rc<WaypointChunk>>,
|
||||||
) {
|
) {
|
||||||
let trackpoints = file
|
let trackpoints = file
|
||||||
.trk
|
.segments()
|
||||||
.iter()
|
|
||||||
.flat_map(|track| &track.trkseg)
|
|
||||||
.flat_map(|segment| segment.chunks().iter().map(|c| (c.id, c.clone())))
|
.flat_map(|segment| segment.chunks().iter().map(|c| (c.id, c.clone())))
|
||||||
.collect();
|
.collect();
|
||||||
let waypoints = file
|
let waypoints = file
|
||||||
@@ -373,19 +414,28 @@ mod tests {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn assert_same_segment(a: &TrackSegment, b: &TrackSegment) {
|
||||||
|
assert_eq!(a.id, b.id);
|
||||||
|
assert_eq!(a.len(), b.len());
|
||||||
|
for (a, b) in a.chunks().iter().zip(b.chunks()) {
|
||||||
|
assert!(Rc::ptr_eq(a, b), "the chunks are the ones that were given");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn assert_same_file(a: &File, b: &File) {
|
fn assert_same_file(a: &File, b: &File) {
|
||||||
assert_eq!(a.id, b.id);
|
assert_eq!(a.id, b.id);
|
||||||
assert_eq!(a.info, b.info);
|
assert_eq!(a.info, b.info);
|
||||||
|
assert_eq!(a.rte.len(), b.rte.len());
|
||||||
|
for (a, b) in a.rte.iter().zip(&b.rte) {
|
||||||
|
assert_eq!((a.id, &a.info), (b.id, &b.info));
|
||||||
|
assert_same_segment(&a.seg, &b.seg);
|
||||||
|
}
|
||||||
assert_eq!(a.trk.len(), b.trk.len());
|
assert_eq!(a.trk.len(), b.trk.len());
|
||||||
for (a, b) in a.trk.iter().zip(&b.trk) {
|
for (a, b) in a.trk.iter().zip(&b.trk) {
|
||||||
assert_eq!((a.id, &a.info), (b.id, &b.info));
|
assert_eq!((a.id, &a.info), (b.id, &b.info));
|
||||||
assert_eq!(a.trkseg.len(), b.trkseg.len());
|
assert_eq!(a.trkseg.len(), b.trkseg.len());
|
||||||
for (a, b) in a.trkseg.iter().zip(&b.trkseg) {
|
for (a, b) in a.trkseg.iter().zip(&b.trkseg) {
|
||||||
assert_eq!(a.id, b.id);
|
assert_same_segment(a, b);
|
||||||
assert_eq!(a.len(), b.len());
|
|
||||||
for (a, b) in a.chunks().iter().zip(b.chunks()) {
|
|
||||||
assert!(Rc::ptr_eq(a, b), "the chunks are the ones that were given");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert_eq!(a.wpt.len(), b.wpt.len());
|
assert_eq!(a.wpt.len(), b.wpt.len());
|
||||||
@@ -425,7 +475,7 @@ mod tests {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.build(file.id, &|_| None, &|_| None)
|
.build(file.id, &|_| None, &|_| None)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(built.trk.is_empty() && built.wpt.is_empty());
|
assert!(built.rte.is_empty() && built.trk.is_empty() && built.wpt.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The names of the categories of a trackpoint.
|
/// The names of the categories of a trackpoint.
|
||||||
@@ -611,13 +661,15 @@ mod tests {
|
|||||||
"70733a2f2f6578616d706c652e636f6d2f736f6d656f6e65021868747470733a2f2f6578616d706c652e636f",
|
"70733a2f2f6578616d706c652e636f6d2f736f6d656f6e65021868747470733a2f2f6578616d706c652e636f",
|
||||||
"6d2f66696c65012074657874206f662068747470733a2f2f6578616d706c652e636f6d2f66696c651a687474",
|
"6d2f66696c65012074657874206f662068747470733a2f2f6578616d706c652e636f6d2f66696c651a687474",
|
||||||
"70733a2f2f6578616d706c652e636f6d2f66696c652d32012274657874206f662068747470733a2f2f657861",
|
"70733a2f2f6578616d706c652e636f6d2f66696c652d32012274657874206f662068747470733a2f2f657861",
|
||||||
"6d706c652e636f6d2f66696c652d320180a0abfef96201100000000000000000000000000000000201057472",
|
"6d706c652e636f6d2f66696c652d320180a0abfef9620110000000000000000000000000000000090105726f",
|
||||||
"61636b0103636d740104646573630103737263021968747470733a2f2f6578616d706c652e636f6d2f747261",
|
"75746500000000010648696b696e6701063030666630300000100000000000000000000000000000000a0110",
|
||||||
"636b012174657874206f662068747470733a2f2f6578616d706c652e636f6d2f747261636b1b68747470733a",
|
"000000000000000000000000000000040110000000000000000000000000000000020105747261636b010363",
|
||||||
"2f2f6578616d706c652e636f6d2f747261636b2d32012374657874206f662068747470733a2f2f6578616d70",
|
"6d740104646573630103737263021968747470733a2f2f6578616d706c652e636f6d2f747261636b01217465",
|
||||||
"6c652e636f6d2f747261636b2d3201074379636c696e67010666663030303001000000000000e03f01000000",
|
"7874206f662068747470733a2f2f6578616d706c652e636f6d2f747261636b1b68747470733a2f2f6578616d",
|
||||||
"0000001840021000000000000000000000000000000003011000000000000000000000000000000004100000",
|
"706c652e636f6d2f747261636b2d32012374657874206f662068747470733a2f2f6578616d706c652e636f6d",
|
||||||
"000000000000000000000000000600011000000000000000000000000000000005",
|
"2f747261636b2d3201074379636c696e67010666663030303001000000000000e03f01000000000000184002",
|
||||||
|
"1000000000000000000000000000000003011000000000000000000000000000000004100000000000000000",
|
||||||
|
"000000000000000600011000000000000000000000000000000005",
|
||||||
);
|
);
|
||||||
const TRACKPOINTS_V1: &str = concat!(
|
const TRACKPOINTS_V1: &str = concat!(
|
||||||
"01020667726176656c0761737068616c74010470617468010668696b696e6701046d74623003000000000000",
|
"01020667726176656c0761737068616c74010470617468010668696b696e6701046d74623003000000000000",
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ use serde::{Deserialize, Serialize};
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Author, Categories, File, FileInfo, Link, LngLat, TrackInfo, Trackpoint, TrackpointCategories,
|
Author, Categories, File, FileInfo, Link, LngLat, TrackInfo, TrackSegment, Trackpoint,
|
||||||
Waypoint, WaypointId,
|
TrackpointCategories, Waypoint, WaypointId,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
@@ -133,10 +133,18 @@ impl From<TrackInfo1> for TrackInfo {
|
|||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct FileRecord1 {
|
pub struct FileRecord1 {
|
||||||
pub info: FileInfo1,
|
pub info: FileInfo1,
|
||||||
|
pub routes: Vec<RouteRecord1>,
|
||||||
pub tracks: Vec<TrackRecord1>,
|
pub tracks: Vec<TrackRecord1>,
|
||||||
pub waypoints: Vec<Uuid>,
|
pub waypoints: Vec<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct RouteRecord1 {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub info: TrackInfo1,
|
||||||
|
pub segment: SegmentRecord1,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct TrackRecord1 {
|
pub struct TrackRecord1 {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
@@ -150,24 +158,35 @@ pub struct SegmentRecord1 {
|
|||||||
pub chunks: Vec<Uuid>,
|
pub chunks: Vec<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl SegmentRecord1 {
|
||||||
|
fn new(segment: &TrackSegment) -> Self {
|
||||||
|
Self {
|
||||||
|
id: segment.id.0,
|
||||||
|
chunks: segment.chunks().iter().map(|chunk| chunk.id.0).collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl FileRecord1 {
|
impl FileRecord1 {
|
||||||
pub fn new(file: &File) -> Self {
|
pub fn new(file: &File) -> Self {
|
||||||
Self {
|
Self {
|
||||||
info: (&file.info).into(),
|
info: (&file.info).into(),
|
||||||
|
routes: file
|
||||||
|
.rte
|
||||||
|
.iter()
|
||||||
|
.map(|route| RouteRecord1 {
|
||||||
|
id: route.id.0,
|
||||||
|
info: (&route.info).into(),
|
||||||
|
segment: SegmentRecord1::new(&route.seg),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
tracks: file
|
tracks: file
|
||||||
.trk
|
.trk
|
||||||
.iter()
|
.iter()
|
||||||
.map(|track| TrackRecord1 {
|
.map(|track| TrackRecord1 {
|
||||||
id: track.id.0,
|
id: track.id.0,
|
||||||
info: (&track.info).into(),
|
info: (&track.info).into(),
|
||||||
segments: track
|
segments: track.trkseg.iter().map(SegmentRecord1::new).collect(),
|
||||||
.trkseg
|
|
||||||
.iter()
|
|
||||||
.map(|segment| SegmentRecord1 {
|
|
||||||
id: segment.id.0,
|
|
||||||
chunks: segment.chunks().iter().map(|chunk| chunk.id.0).collect(),
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
waypoints: file.wpt.chunks().iter().map(|chunk| chunk.id.0).collect(),
|
waypoints: file.wpt.chunks().iter().map(|chunk| chunk.id.0).collect(),
|
||||||
|
|||||||
@@ -217,8 +217,7 @@ impl Persister {
|
|||||||
.files
|
.files
|
||||||
.get(id)
|
.get(id)
|
||||||
.is_none_or(|saved| !Rc::ptr_eq(saved, file));
|
.is_none_or(|saved| !Rc::ptr_eq(saved, file));
|
||||||
let segments = file.trk.iter().flat_map(|track| &track.trkseg);
|
for chunk in file.segments().flat_map(|segment| segment.chunks()) {
|
||||||
for chunk in segments.flat_map(|segment| segment.chunks()) {
|
|
||||||
let key = ChunkKey::trackpoints(chunk.id.0);
|
let key = ChunkKey::trackpoints(chunk.id.0);
|
||||||
if chunks.insert(key) && changed && !self.chunks.contains(&key) {
|
if chunks.insert(key) && changed && !self.chunks.contains(&key) {
|
||||||
batch
|
batch
|
||||||
|
|||||||
@@ -313,7 +313,6 @@ pub struct ExportArguments {
|
|||||||
atemp: bool,
|
atemp: bool,
|
||||||
power: bool,
|
power: bool,
|
||||||
osm: bool,
|
osm: bool,
|
||||||
as_route: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ExportArguments> for ExportOptions {
|
impl From<ExportArguments> for ExportOptions {
|
||||||
@@ -325,7 +324,6 @@ impl From<ExportArguments> for ExportOptions {
|
|||||||
atemp: arguments.atemp,
|
atemp: arguments.atemp,
|
||||||
power: arguments.power,
|
power: arguments.power,
|
||||||
osm: arguments.osm,
|
osm: arguments.osm,
|
||||||
as_route: arguments.as_route,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,11 @@ pub fn style(
|
|||||||
state.run(move |e| api::style(e, color.as_deref(), opacity, width))
|
state.run(move |e| api::style(e, color.as_deref(), opacity, width))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tauri::command(async)]
|
||||||
|
pub fn new_route(state: State<AppState>) -> api::Outcome {
|
||||||
|
state.run(api::new_route)
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command(async)]
|
#[tauri::command(async)]
|
||||||
pub fn new_track(state: State<AppState>) -> api::Outcome {
|
pub fn new_track(state: State<AppState>) -> api::Outcome {
|
||||||
state.run(api::new_track)
|
state.run(api::new_track)
|
||||||
|
|||||||
@@ -79,7 +79,6 @@ pub fn export_file(
|
|||||||
atemp: bool,
|
atemp: bool,
|
||||||
power: bool,
|
power: bool,
|
||||||
osm: bool,
|
osm: bool,
|
||||||
as_route: bool,
|
|
||||||
) -> Bytes {
|
) -> Bytes {
|
||||||
let options = ExportOptions {
|
let options = ExportOptions {
|
||||||
time,
|
time,
|
||||||
@@ -88,7 +87,6 @@ pub fn export_file(
|
|||||||
atemp,
|
atemp,
|
||||||
power,
|
power,
|
||||||
osm,
|
osm,
|
||||||
as_route,
|
|
||||||
};
|
};
|
||||||
optional(state.read(move |e| api::export_file(e, file_id, options)))
|
optional(state.read(move |e| api::export_file(e, file_id, options)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,16 @@ pub fn arrow_select(state: State<AppState>, down: bool, add: bool) -> api::Outco
|
|||||||
state.run(move |e| api::arrow_select(e, down, add))
|
state.run(move |e| api::arrow_select(e, down, add))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tauri::command(async)]
|
||||||
|
pub fn select_routes(
|
||||||
|
state: State<AppState>,
|
||||||
|
file_id: Uuid,
|
||||||
|
route_ids: Vec<Uuid>,
|
||||||
|
mode: SelectMode,
|
||||||
|
) -> api::Outcome {
|
||||||
|
state.run(move |e| api::select_routes(e, file_id, &route_ids, mode))
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command(async)]
|
#[tauri::command(async)]
|
||||||
pub fn select_tracks(
|
pub fn select_tracks(
|
||||||
state: State<AppState>,
|
state: State<AppState>,
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ fn register_commands<R: tauri::Runtime>(builder: tauri::Builder<R>) -> tauri::Bu
|
|||||||
selection::select,
|
selection::select,
|
||||||
selection::select_all,
|
selection::select_all,
|
||||||
selection::arrow_select,
|
selection::arrow_select,
|
||||||
|
selection::select_routes,
|
||||||
selection::select_tracks,
|
selection::select_tracks,
|
||||||
selection::select_segments,
|
selection::select_segments,
|
||||||
selection::select_waypoint_group,
|
selection::select_waypoint_group,
|
||||||
@@ -134,6 +135,7 @@ fn register_commands<R: tauri::Runtime>(builder: tauri::Builder<R>) -> tauri::Bu
|
|||||||
// edits and tools
|
// edits and tools
|
||||||
edit::metadata,
|
edit::metadata,
|
||||||
edit::style,
|
edit::style,
|
||||||
|
edit::new_route,
|
||||||
edit::new_track,
|
edit::new_track,
|
||||||
edit::new_track_segment,
|
edit::new_track_segment,
|
||||||
edit::new_waypoint,
|
edit::new_waypoint,
|
||||||
|
|||||||
+1
-1
@@ -257,7 +257,7 @@ fn waypoints_exports_and_routing_revisions() {
|
|||||||
|
|
||||||
let exported = app.bytes(
|
let exported = app.bytes(
|
||||||
"export_file",
|
"export_file",
|
||||||
json!({ "fileId": file, "time": true, "hr": true, "cad": true, "atemp": true, "power": true, "osm": true, "asRoute": false }),
|
json!({ "fileId": file, "time": true, "hr": true, "cad": true, "atemp": true, "power": true, "osm": true }),
|
||||||
);
|
);
|
||||||
assert_eq!(exported[0], 1);
|
assert_eq!(exported[0], 1);
|
||||||
assert!(
|
assert!(
|
||||||
|
|||||||
@@ -1191,6 +1191,7 @@ mod tests {
|
|||||||
/// What the file holds: the name of its track (its own name is the one of the file).
|
/// What the file holds: the name of its track (its own name is the one of the file).
|
||||||
fn content(&self, id: Uuid) -> String {
|
fn content(&self, id: Uuid) -> String {
|
||||||
self.engine.file_structure(&FileId(id)).unwrap().tracks[0]
|
self.engine.file_structure(&FileId(id)).unwrap().tracks[0]
|
||||||
|
.info
|
||||||
.name
|
.name
|
||||||
.clone()
|
.clone()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ pub fn style(color: Option<String>, opacity: Option<f64>, width: Option<f64>) ->
|
|||||||
run(|e| api::style(e, color.as_deref(), opacity, width))
|
run(|e| api::style(e, color.as_deref(), opacity, width))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn new_route() -> Outcome {
|
||||||
|
run(api::new_route)
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn new_track() -> Outcome {
|
pub fn new_track() -> Outcome {
|
||||||
run(api::new_track)
|
run(api::new_track)
|
||||||
|
|||||||
@@ -45,10 +45,8 @@ pub fn selection_statistics() -> GlobalStatistics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A file as GPX (UTF-8 bytes), `undefined` if it does not exist. The options say which data of the trackpoints
|
/// A file as GPX (UTF-8 bytes), `undefined` if it does not exist. The options say which data of the trackpoints
|
||||||
/// to keep, and with `as_route` that the segments are written as routes (`rte`) instead of tracks:
|
/// to keep.
|
||||||
/// the anchors are the route points, and the trackpoints between them the points of their path.
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
pub fn export_file(
|
pub fn export_file(
|
||||||
file_id: &str,
|
file_id: &str,
|
||||||
time: bool,
|
time: bool,
|
||||||
@@ -57,7 +55,6 @@ pub fn export_file(
|
|||||||
atemp: bool,
|
atemp: bool,
|
||||||
power: bool,
|
power: bool,
|
||||||
osm: bool,
|
osm: bool,
|
||||||
as_route: bool,
|
|
||||||
) -> Option<Vec<u8>> {
|
) -> Option<Vec<u8>> {
|
||||||
let id = parse_uuid(file_id)?;
|
let id = parse_uuid(file_id)?;
|
||||||
let options = ExportOptions {
|
let options = ExportOptions {
|
||||||
@@ -67,7 +64,6 @@ pub fn export_file(
|
|||||||
atemp,
|
atemp,
|
||||||
power,
|
power,
|
||||||
osm,
|
osm,
|
||||||
as_route,
|
|
||||||
};
|
};
|
||||||
with_engine(|e| api::export_file(e, id, options)).flatten()
|
with_engine(|e| api::export_file(e, id, options)).flatten()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,14 @@ pub fn arrow_select(down: bool, add: bool) -> Outcome {
|
|||||||
// with the selection when it holds elements of the same kind in the same place (same file, same
|
// with the selection when it holds elements of the same kind in the same place (same file, same
|
||||||
// track for segments), otherwise they replace it.
|
// track for segments), otherwise they replace it.
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn select_routes(file_id: &str, route_ids_bytes: &[u8], mode: SelectMode) -> Outcome {
|
||||||
|
match parse_uuid(file_id).zip(uuid_list(route_ids_bytes)) {
|
||||||
|
Some((file_id, ids)) => run(|e| api::select_routes(e, file_id, &ids, mode.into())),
|
||||||
|
None => fail("the ids are not UUIDs"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn select_tracks(file_id: &str, track_ids_bytes: &[u8], mode: SelectMode) -> Outcome {
|
pub fn select_tracks(file_id: &str, track_ids_bytes: &[u8], mode: SelectMode) -> Outcome {
|
||||||
match parse_uuid(file_id).zip(uuid_list(track_ids_bytes)) {
|
match parse_uuid(file_id).zip(uuid_list(track_ids_bytes)) {
|
||||||
|
|||||||
+10
-14
@@ -16,6 +16,7 @@ use crate::types::*;
|
|||||||
const SIMPLE: &[u8] = include_bytes!("../../gpx-rs/data/simple.gpx");
|
const SIMPLE: &[u8] = include_bytes!("../../gpx-rs/data/simple.gpx");
|
||||||
const WITH_TIME: &[u8] = include_bytes!("../../gpx-rs/data/with_time.gpx");
|
const WITH_TIME: &[u8] = include_bytes!("../../gpx-rs/data/with_time.gpx");
|
||||||
const WITH_SURFACE: &[u8] = include_bytes!("../../gpx-rs/data/with_surface.gpx");
|
const WITH_SURFACE: &[u8] = include_bytes!("../../gpx-rs/data/with_surface.gpx");
|
||||||
|
const WITH_ROUTES: &[u8] = include_bytes!("../../gpx-rs/data/with_routes.gpx");
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static LAST: std::cell::RefCell<JsValue> = const { std::cell::RefCell::new(JsValue::UNDEFINED) };
|
static LAST: std::cell::RefCell<JsValue> = const { std::cell::RefCell::new(JsValue::UNDEFINED) };
|
||||||
@@ -112,10 +113,7 @@ fn test_unknown_file_has_no_structure() {
|
|||||||
assert!(crate::queries::files::file_structure(&id).is_undefined());
|
assert!(crate::queries::files::file_structure(&id).is_undefined());
|
||||||
assert!(crate::queries::files::file_structure("not an id").is_undefined());
|
assert!(crate::queries::files::file_structure("not an id").is_undefined());
|
||||||
assert!(crate::queries::files::file_statistics(&id).is_undefined());
|
assert!(crate::queries::files::file_statistics(&id).is_undefined());
|
||||||
assert!(
|
assert!(crate::queries::files::export_file(&id, true, true, true, true, true, true).is_none());
|
||||||
crate::queries::files::export_file(&id, true, true, true, true, true, true, false)
|
|
||||||
.is_none()
|
|
||||||
);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
crate::queries::coordinates::segment_coordinates(&id).length(),
|
crate::queries::coordinates::segment_coordinates(&id).length(),
|
||||||
0
|
0
|
||||||
@@ -279,8 +277,7 @@ fn test_export_and_exportable_data() {
|
|||||||
let ids = order();
|
let ids = order();
|
||||||
|
|
||||||
let bytes =
|
let bytes =
|
||||||
crate::queries::files::export_file(&ids[0], true, true, true, true, true, true, false)
|
crate::queries::files::export_file(&ids[0], true, true, true, true, true, true).unwrap();
|
||||||
.unwrap();
|
|
||||||
assert!(String::from_utf8(bytes).unwrap().contains("<gpx"));
|
assert!(String::from_utf8(bytes).unwrap().contains("<gpx"));
|
||||||
|
|
||||||
// the time is the first bit
|
// the time is the first bit
|
||||||
@@ -300,18 +297,17 @@ fn test_export_and_exportable_data() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen_test]
|
#[wasm_bindgen_test]
|
||||||
fn test_export_as_route() {
|
fn test_export_tracks_and_routes() {
|
||||||
started_with(&[(SIMPLE, "plain")]);
|
started_with(&[(SIMPLE, "track"), (WITH_ROUTES, "routes")]);
|
||||||
let id = order().remove(0);
|
let text = |id: &str| {
|
||||||
let text = |as_route| {
|
|
||||||
let bytes =
|
let bytes =
|
||||||
crate::queries::files::export_file(&id, true, true, true, true, true, true, as_route)
|
crate::queries::files::export_file(id, true, true, true, true, true, true).unwrap();
|
||||||
.unwrap();
|
|
||||||
String::from_utf8(bytes).unwrap()
|
String::from_utf8(bytes).unwrap()
|
||||||
};
|
};
|
||||||
let tracks = text(false);
|
let ids = order();
|
||||||
|
let tracks = text(&ids[0]);
|
||||||
assert!(tracks.contains("<trk>") && !tracks.contains("<rte>"));
|
assert!(tracks.contains("<trk>") && !tracks.contains("<rte>"));
|
||||||
let routes = text(true);
|
let routes = text(&ids[1]);
|
||||||
assert!(routes.contains("<rte>") && !routes.contains("<trk>"));
|
assert!(routes.contains("<rte>") && !routes.contains("<trk>"));
|
||||||
assert!(routes.contains("<rtept"));
|
assert!(routes.contains("<rtept"));
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-5
@@ -28,16 +28,19 @@ const FILE_STRUCTURE_TS: &str = r#"
|
|||||||
export type Selection =
|
export type Selection =
|
||||||
| { type: 'empty' }
|
| { type: 'empty' }
|
||||||
| { type: 'file'; fileIds: string[] }
|
| { type: 'file'; fileIds: string[] }
|
||||||
|
| { type: 'route'; fileId: string; routeIds: string[] }
|
||||||
| { type: 'track'; fileId: string; trackIds: string[] }
|
| { type: 'track'; fileId: string; trackIds: string[] }
|
||||||
| { type: 'segment'; fileId: string; trackId: string; segmentIds: string[] }
|
| { type: 'segment'; fileId: string; trackId: string; segmentIds: string[] }
|
||||||
| { type: 'waypoints'; fileId: string }
|
| { type: 'waypoints'; fileId: string }
|
||||||
| { type: 'waypoint'; fileId: string; waypointIds: string[] };
|
| { type: 'waypoint'; fileId: string; waypointIds: string[] };
|
||||||
/**
|
/**
|
||||||
* Where moved elements go: a list of the file tree and the position in it, counted among the
|
* Where moved elements go: a list of the file tree and the position in it, counted among the
|
||||||
* elements of the list that are not moved.
|
* elements of the list that are not moved. Routes and tracks keep their kind: moved among the
|
||||||
|
* other kind, routes go after the routes, tracks before the tracks.
|
||||||
*/
|
*/
|
||||||
export type MoveTarget =
|
export type MoveTarget =
|
||||||
| { type: 'files'; index: number }
|
| { type: 'files'; index: number }
|
||||||
|
| { type: 'routes'; fileId: string; index: number }
|
||||||
| { type: 'tracks'; fileId: string; index: number }
|
| { type: 'tracks'; fileId: string; index: number }
|
||||||
| { type: 'segments'; fileId: string; trackId: string; index: number }
|
| { type: 'segments'; fileId: string; trackId: string; index: number }
|
||||||
| { type: 'waypoints'; fileId: string; index: number };
|
| { type: 'waypoints'; fileId: string; index: number };
|
||||||
@@ -83,7 +86,7 @@ export interface SelectionPoint {
|
|||||||
}
|
}
|
||||||
/** What was copied or cut, to be pasted: the kind of the elements and their ids. */
|
/** What was copied or cut, to be pasted: the kind of the elements and their ids. */
|
||||||
export interface Clipboard {
|
export interface Clipboard {
|
||||||
type: 'files' | 'tracks' | 'segments' | 'waypoints';
|
type: 'files' | 'routes' | 'tracks' | 'segments' | 'waypoints';
|
||||||
ids: string[];
|
ids: string[];
|
||||||
cut: boolean;
|
cut: boolean;
|
||||||
}
|
}
|
||||||
@@ -119,6 +122,8 @@ export interface FileStructure {
|
|||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
desc?: string;
|
desc?: string;
|
||||||
|
/** The routes come before the tracks, in the file and in the statistics. */
|
||||||
|
routes: RouteNode[];
|
||||||
tracks: TrackNode[];
|
tracks: TrackNode[];
|
||||||
waypoints: WaypointNode[];
|
waypoints: WaypointNode[];
|
||||||
/** Changes when the waypoints of the file change: refetch their coordinates. */
|
/** Changes when the waypoints of the file change: refetch their coordinates. */
|
||||||
@@ -146,14 +151,22 @@ export interface GlobalStatistics {
|
|||||||
/** Absent when there are no trackpoints. */
|
/** Absent when there are no trackpoints. */
|
||||||
bounds?: { west: number; south: number; east: number; north: number };
|
bounds?: { west: number; south: number; east: number; north: number };
|
||||||
}
|
}
|
||||||
export interface TrackNode {
|
/** The name, description and style of a route or a track. */
|
||||||
id: string;
|
export interface PathInfo {
|
||||||
name?: string;
|
name?: string;
|
||||||
desc?: string;
|
desc?: string;
|
||||||
/** Style of the track, only present when the file defines it. */
|
/** Style, only present when the file defines it. */
|
||||||
color?: string;
|
color?: string;
|
||||||
opacity?: number;
|
opacity?: number;
|
||||||
width?: number;
|
width?: number;
|
||||||
|
}
|
||||||
|
/** A route: like a track, with a single segment. */
|
||||||
|
export interface RouteNode extends PathInfo {
|
||||||
|
id: string;
|
||||||
|
segment: SegmentNode;
|
||||||
|
}
|
||||||
|
export interface TrackNode extends PathInfo {
|
||||||
|
id: string;
|
||||||
segments: SegmentNode[];
|
segments: SegmentNode[];
|
||||||
}
|
}
|
||||||
export interface SegmentNode {
|
export interface SegmentNode {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ pub enum MergeType {
|
|||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub enum SplitType {
|
pub enum SplitType {
|
||||||
Files,
|
Files,
|
||||||
|
Routes,
|
||||||
Tracks,
|
Tracks,
|
||||||
Segments,
|
Segments,
|
||||||
}
|
}
|
||||||
@@ -23,6 +24,7 @@ impl From<SplitType> for engine::SplitType {
|
|||||||
fn from(t: SplitType) -> Self {
|
fn from(t: SplitType) -> Self {
|
||||||
match t {
|
match t {
|
||||||
SplitType::Files => Self::Files,
|
SplitType::Files => Self::Files,
|
||||||
|
SplitType::Routes => Self::Routes,
|
||||||
SplitType::Tracks => Self::Tracks,
|
SplitType::Tracks => Self::Tracks,
|
||||||
SplitType::Segments => Self::Segments,
|
SplitType::Segments => Self::Segments,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,8 +105,10 @@
|
|||||||
let desktop = $derived($workspace !== undefined);
|
let desktop = $derived($workspace !== undefined);
|
||||||
let selectedIds = $derived(selectedFileIds($selection));
|
let selectedIds = $derived(selectedFileIds($selection));
|
||||||
let anyUnsaved = $derived(Object.values($workspace?.files ?? {}).some((file) => file.dirty));
|
let anyUnsaved = $derived(Object.values($workspace?.files ?? {}).some((file) => file.dirty));
|
||||||
// metadata and style can be edited for files and tracks only
|
// metadata and style can be edited for files, routes and tracks only
|
||||||
let filesOrTracksSelected = $derived($selection.type === 'file' || $selection.type === 'track');
|
let filesOrTracksSelected = $derived(
|
||||||
|
$selection.type === 'file' || $selection.type === 'route' || $selection.type === 'track'
|
||||||
|
);
|
||||||
|
|
||||||
function switchBasemaps() {
|
function switchBasemaps() {
|
||||||
[$currentBasemap, $previousBasemap] = [$previousBasemap, $currentBasemap];
|
[$currentBasemap, $previousBasemap] = [$previousBasemap, $currentBasemap];
|
||||||
@@ -308,6 +310,13 @@
|
|||||||
<Plus size="16" />
|
<Plus size="16" />
|
||||||
{i18n._('menu.new_track')}
|
{i18n._('menu.new_track')}
|
||||||
</Menubar.Item>
|
</Menubar.Item>
|
||||||
|
<Menubar.Item
|
||||||
|
onclick={() => engine.newRoute()}
|
||||||
|
disabled={selectionCount !== 1}
|
||||||
|
>
|
||||||
|
<Plus size="16" />
|
||||||
|
{i18n._('menu.new_route')}
|
||||||
|
</Menubar.Item>
|
||||||
{:else if $selection.type === 'track'}
|
{:else if $selection.type === 'track'}
|
||||||
<Menubar.Separator />
|
<Menubar.Separator />
|
||||||
<Menubar.Item
|
<Menubar.Item
|
||||||
|
|||||||
@@ -21,7 +21,6 @@
|
|||||||
Orbit,
|
Orbit,
|
||||||
Thermometer,
|
Thermometer,
|
||||||
SquareActivity,
|
SquareActivity,
|
||||||
Route,
|
|
||||||
} from '@lucide/svelte';
|
} from '@lucide/svelte';
|
||||||
import { i18n } from '$lib/i18n/i18n.svelte';
|
import { i18n } from '$lib/i18n/i18n.svelte';
|
||||||
import { engine, type ExportOptions } from '$lib/engine';
|
import { engine, type ExportOptions } from '$lib/engine';
|
||||||
@@ -38,7 +37,6 @@
|
|||||||
atemp: true,
|
atemp: true,
|
||||||
power: true,
|
power: true,
|
||||||
osm: false,
|
osm: false,
|
||||||
asRoute: false,
|
|
||||||
});
|
});
|
||||||
// the files that are exported
|
// the files that are exported
|
||||||
let fileIds: string[] = $derived.by(() => {
|
let fileIds: string[] = $derived.by(() => {
|
||||||
@@ -61,7 +59,6 @@
|
|||||||
atemp: false,
|
atemp: false,
|
||||||
power: false,
|
power: false,
|
||||||
osm: false,
|
osm: false,
|
||||||
asRoute: false,
|
|
||||||
});
|
});
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
const ids = fileIds;
|
const ids = fileIds;
|
||||||
@@ -75,8 +72,6 @@
|
|||||||
atemp: !available.atemp,
|
atemp: !available.atemp,
|
||||||
power: !available.power,
|
power: !available.power,
|
||||||
osm: !available.osm,
|
osm: !available.osm,
|
||||||
// always possible
|
|
||||||
asRoute: false,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -131,7 +126,6 @@
|
|||||||
atemp: exportOptions.atemp && !hide.atemp,
|
atemp: exportOptions.atemp && !hide.atemp,
|
||||||
power: exportOptions.power && !hide.power,
|
power: exportOptions.power && !hide.power,
|
||||||
osm: exportOptions.osm && !hide.osm,
|
osm: exportOptions.osm && !hide.osm,
|
||||||
asRoute: exportOptions.asRoute,
|
|
||||||
};
|
};
|
||||||
if (exportState.current === ExportState.SELECTION) {
|
if (exportState.current === ExportState.SELECTION) {
|
||||||
exportSelectedFiles(options);
|
exportSelectedFiles(options);
|
||||||
@@ -211,13 +205,6 @@
|
|||||||
{i18n._('quantities.osm_extensions')}
|
{i18n._('quantities.osm_extensions')}
|
||||||
</Label>
|
</Label>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex flex-row items-center gap-1.5">
|
|
||||||
<Checkbox id="export-as-route" bind:checked={exportOptions.asRoute} />
|
|
||||||
<Label for="export-as-route" class="flex flex-row items-center gap-1">
|
|
||||||
<Route size="16" />
|
|
||||||
{i18n._('menu.export_as_route', 'Export as route')}
|
|
||||||
</Label>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</Dialog.Content>
|
</Dialog.Content>
|
||||||
|
|||||||
@@ -30,6 +30,12 @@
|
|||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'file':
|
case 'file':
|
||||||
return structure.name;
|
return structure.name;
|
||||||
|
case 'route': {
|
||||||
|
const routeIndex = structure.routes.findIndex((r) => r.id === node.routeId);
|
||||||
|
return (
|
||||||
|
structure.routes[routeIndex]?.name ?? `${i18n._('gpx.route')} ${routeIndex + 1}`
|
||||||
|
);
|
||||||
|
}
|
||||||
case 'track': {
|
case 'track': {
|
||||||
const trackIndex = structure.tracks.findIndex((t) => t.id === node.trackId);
|
const trackIndex = structure.tracks.findIndex((t) => t.id === node.trackId);
|
||||||
return (
|
return (
|
||||||
@@ -75,7 +81,7 @@
|
|||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if node.type === 'segment' || node.type === 'waypoint'}
|
{#if node.type === 'route' || node.type === 'segment' || node.type === 'waypoint'}
|
||||||
<FileListNodeLabel {fileState} {node} {label} />
|
<FileListNodeLabel {fileState} {node} {label} />
|
||||||
{:else if recursive}
|
{:else if recursive}
|
||||||
<CollapsibleTreeNode {id} bind:this={collapsible}>
|
<CollapsibleTreeNode {id} bind:this={collapsible}>
|
||||||
@@ -84,7 +90,11 @@
|
|||||||
{/snippet}
|
{/snippet}
|
||||||
{#snippet content()}
|
{#snippet content()}
|
||||||
<div class="ml-4">
|
<div class="ml-4">
|
||||||
<FileListNodeContent {fileState} {node} />
|
{#if node.type === 'file' && fileState.structure.routes.length > 0}
|
||||||
|
<FileListNodeContent {fileState} {node} part="routes" />
|
||||||
|
{:else}
|
||||||
|
<FileListNodeContent {fileState} {node} />
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/snippet}
|
{/snippet}
|
||||||
</CollapsibleTreeNode>
|
</CollapsibleTreeNode>
|
||||||
|
|||||||
@@ -10,16 +10,21 @@
|
|||||||
let {
|
let {
|
||||||
fileState,
|
fileState,
|
||||||
node,
|
node,
|
||||||
waypointRoot = false,
|
part = 'tracks',
|
||||||
}: {
|
}: {
|
||||||
/** State of the file of the node (not needed for the root). */
|
/** State of the file of the node (not needed for the root). */
|
||||||
fileState?: FileState;
|
fileState?: FileState;
|
||||||
/** The node whose children are listed, `null` for the list of the files. */
|
/** The node whose children are listed, `null` for the list of the files. */
|
||||||
node: FileTreeNode | null;
|
node: FileTreeNode | null;
|
||||||
/** List the node standing for the waypoints of the file, instead of its tracks. */
|
/**
|
||||||
waypointRoot?: boolean;
|
* For a file, which of its lists this is: its routes, its tracks, or the node standing for
|
||||||
|
* its waypoints. Each one is followed by the next ones.
|
||||||
|
*/
|
||||||
|
part?: 'routes' | 'tracks' | 'waypoints';
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
|
let waypointRoot = part === 'waypoints';
|
||||||
|
|
||||||
const { files, order } = engine;
|
const { files, order } = engine;
|
||||||
|
|
||||||
let container: HTMLElement;
|
let container: HTMLElement;
|
||||||
@@ -27,9 +32,11 @@
|
|||||||
node === null
|
node === null
|
||||||
? 'file'
|
? 'file'
|
||||||
: node.type === 'file'
|
: node.type === 'file'
|
||||||
? waypointRoot
|
? part === 'waypoints'
|
||||||
? 'waypoints'
|
? 'waypoints'
|
||||||
: 'track'
|
: part === 'routes'
|
||||||
|
? 'route'
|
||||||
|
: 'track'
|
||||||
: node.type === 'waypoints'
|
: node.type === 'waypoints'
|
||||||
? 'waypoint'
|
? 'waypoint'
|
||||||
: 'segment';
|
: 'segment';
|
||||||
@@ -74,7 +81,7 @@
|
|||||||
{/each}
|
{/each}
|
||||||
{:else if fileState}
|
{:else if fileState}
|
||||||
{#if node.type === 'file'}
|
{#if node.type === 'file'}
|
||||||
{#if waypointRoot}
|
{#if part === 'waypoints'}
|
||||||
{#if fileState.structure.waypoints.length > 0}
|
{#if fileState.structure.waypoints.length > 0}
|
||||||
<div data-id="waypoints">
|
<div data-id="waypoints">
|
||||||
<FileListNode
|
<FileListNode
|
||||||
@@ -83,6 +90,15 @@
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
{:else if part === 'routes'}
|
||||||
|
{#each fileState.structure.routes as route (route.id)}
|
||||||
|
<div data-id={route.id}>
|
||||||
|
<FileListNode
|
||||||
|
{fileState}
|
||||||
|
node={{ type: 'route', fileId: node.fileId, routeId: route.id }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
{:else}
|
{:else}
|
||||||
{#each fileState.structure.tracks as track (track.id)}
|
{#each fileState.structure.tracks as track (track.id)}
|
||||||
<div data-id={track.id}>
|
<div data-id={track.id}>
|
||||||
@@ -120,8 +136,10 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{#if fileState && node?.type === 'file' && !waypointRoot}
|
{#if fileState && node?.type === 'file' && part === 'routes'}
|
||||||
<FileListNodeContent {fileState} {node} waypointRoot={true} />
|
<FileListNodeContent {fileState} {node} part="tracks" />
|
||||||
|
{:else if fileState && node?.type === 'file' && part === 'tracks'}
|
||||||
|
<FileListNodeContent {fileState} {node} part="waypoints" />
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<style lang="postcss">
|
<style lang="postcss">
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
Scissors,
|
Scissors,
|
||||||
FileStack,
|
FileStack,
|
||||||
FileX,
|
FileX,
|
||||||
|
Route,
|
||||||
} from '@lucide/svelte';
|
} from '@lucide/svelte';
|
||||||
import { getContext, untrack } from 'svelte';
|
import { getContext, untrack } from 'svelte';
|
||||||
import { i18n } from '$lib/i18n/i18n.svelte';
|
import { i18n } from '$lib/i18n/i18n.svelte';
|
||||||
@@ -34,6 +35,7 @@
|
|||||||
elementId,
|
elementId,
|
||||||
isInClipboard,
|
isInClipboard,
|
||||||
isSelected,
|
isSelected,
|
||||||
|
pathInfo,
|
||||||
selectedElementIds,
|
selectedElementIds,
|
||||||
selectedFileIds,
|
selectedFileIds,
|
||||||
selectionSize,
|
selectionSize,
|
||||||
@@ -62,20 +64,23 @@
|
|||||||
|
|
||||||
let singleSelection = $derived(selectionSize($selection) === 1);
|
let singleSelection = $derived(selectionSize($selection) === 1);
|
||||||
|
|
||||||
|
// a route or a track: it has a name, a description and a style, like a file
|
||||||
|
let isPath = $derived(node.type === 'route' || node.type === 'track');
|
||||||
|
|
||||||
let nodeColors: string[] = $derived.by(() => {
|
let nodeColors: string[] = $derived.by(() => {
|
||||||
const { tracks } = fileState.structure;
|
const { routes, tracks } = fileState.structure;
|
||||||
if (node.type === 'file') {
|
if (node.type === 'file') {
|
||||||
// the colors defined by the tracks, or else the one of the file
|
// the colors defined by the routes and tracks, or else the one of the file
|
||||||
const colors = [
|
const colors = [
|
||||||
...new Set(
|
...new Set(
|
||||||
tracks.flatMap((track) =>
|
[...routes, ...tracks].flatMap((path) =>
|
||||||
track.color !== undefined ? [normalizeColor(track.color)] : []
|
path.color !== undefined ? [normalizeColor(path.color)] : []
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
return colors.length > 0 ? colors : [fileState.color];
|
return colors.length > 0 ? colors : [fileState.color];
|
||||||
} else if (node.type === 'track') {
|
} else if (isPath) {
|
||||||
const color = tracks.find((track) => track.id === node.trackId)?.color;
|
const color = pathInfo(fileState.structure, node)?.color;
|
||||||
return [color !== undefined ? normalizeColor(color) : fileState.color];
|
return [color !== undefined ? normalizeColor(color) : fileState.color];
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
@@ -141,11 +146,11 @@
|
|||||||
? 'w-full h-7'
|
? 'w-full h-7'
|
||||||
: 'w-max h-9 px-1.5'} pointer-events-auto"
|
: 'w-max h-9 px-1.5'} pointer-events-auto"
|
||||||
>
|
>
|
||||||
{#if node.type === 'file' || node.type === 'track'}
|
{#if node.type === 'file' || isPath}
|
||||||
<MetadataDialog bind:open={openEditMetadata} {fileState} {node} />
|
<MetadataDialog bind:open={openEditMetadata} {fileState} {node} />
|
||||||
<StyleDialog bind:open={openEditStyle} {fileState} {node} />
|
<StyleDialog bind:open={openEditStyle} {fileState} {node} />
|
||||||
{/if}
|
{/if}
|
||||||
{#if level === 'file' || level === 'track'}
|
{#if level === 'file' || isPath}
|
||||||
<div
|
<div
|
||||||
class="absolute {orientation === 'vertical'
|
class="absolute {orientation === 'vertical'
|
||||||
? 'top-0 bottom-0 right-0 w-1'
|
? 'top-0 bottom-0 right-0 w-1'
|
||||||
@@ -197,7 +202,9 @@
|
|||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{#if level === 'segment'}
|
{#if level === 'route'}
|
||||||
|
<Route size="16" class="mx-1 shrink-0" />
|
||||||
|
{:else if level === 'segment'}
|
||||||
<Waypoints size="16" class="mx-1 shrink-0" />
|
<Waypoints size="16" class="mx-1 shrink-0" />
|
||||||
{:else if level === 'waypoint'}
|
{:else if level === 'waypoint'}
|
||||||
{#if symbolKey && symbols[symbolKey].icon}
|
{#if symbolKey && symbols[symbolKey].icon}
|
||||||
@@ -238,7 +245,7 @@
|
|||||||
</Button>
|
</Button>
|
||||||
</ContextMenu.Trigger>
|
</ContextMenu.Trigger>
|
||||||
<ContextMenu.Content>
|
<ContextMenu.Content>
|
||||||
{#if node.type === 'file' || node.type === 'track'}
|
{#if node.type === 'file' || isPath}
|
||||||
<ContextMenu.Item
|
<ContextMenu.Item
|
||||||
disabled={!singleSelection}
|
disabled={!singleSelection}
|
||||||
onclick={() => (editMetadata.current = true)}
|
onclick={() => (editMetadata.current = true)}
|
||||||
@@ -269,6 +276,10 @@
|
|||||||
<Plus size="16" />
|
<Plus size="16" />
|
||||||
{i18n._('menu.new_track')}
|
{i18n._('menu.new_track')}
|
||||||
</ContextMenu.Item>
|
</ContextMenu.Item>
|
||||||
|
<ContextMenu.Item disabled={!singleSelection} onclick={() => engine.newRoute()}>
|
||||||
|
<Plus size="16" />
|
||||||
|
{i18n._('menu.new_route')}
|
||||||
|
</ContextMenu.Item>
|
||||||
<ContextMenu.Separator />
|
<ContextMenu.Separator />
|
||||||
{:else if node.type === 'track'}
|
{:else if node.type === 'track'}
|
||||||
<ContextMenu.Item
|
<ContextMenu.Item
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
import { i18n } from '$lib/i18n/i18n.svelte';
|
import { i18n } from '$lib/i18n/i18n.svelte';
|
||||||
import { editMetadata } from '$lib/components/file-list/metadata/utils.svelte';
|
import { editMetadata } from '$lib/components/file-list/metadata/utils.svelte';
|
||||||
import { engine, type FileState } from '$lib/engine';
|
import { engine, type FileState } from '$lib/engine';
|
||||||
import type { FileTreeNode } from '$lib/engine/selection-helpers';
|
import { pathInfo, type FileTreeNode } from '$lib/engine/selection-helpers';
|
||||||
import { showEngineError } from '$lib/actions/file-actions';
|
import { showEngineError } from '$lib/actions/file-actions';
|
||||||
|
|
||||||
let {
|
let {
|
||||||
@@ -21,16 +21,13 @@
|
|||||||
open: boolean;
|
open: boolean;
|
||||||
} = $props();
|
} = $props();
|
||||||
|
|
||||||
let track = $derived(
|
// the route or the track
|
||||||
node.type === 'track'
|
let path = $derived(pathInfo(fileState.structure, node));
|
||||||
? fileState.structure.tracks.find((track) => track.id === node.trackId)
|
|
||||||
: undefined
|
|
||||||
);
|
|
||||||
let name: string = $derived(
|
let name: string = $derived(
|
||||||
node.type === 'file' ? fileState.structure.name : (track?.name ?? '')
|
node.type === 'file' ? fileState.structure.name : (path?.name ?? '')
|
||||||
);
|
);
|
||||||
let description: string = $derived(
|
let description: string = $derived(
|
||||||
node.type === 'file' ? (fileState.structure.desc ?? '') : (track?.desc ?? '')
|
node.type === 'file' ? (fileState.structure.desc ?? '') : (path?.desc ?? '')
|
||||||
);
|
);
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
|
|||||||
@@ -5,13 +5,22 @@ import { tick } from 'svelte';
|
|||||||
import { engine, type MoveTarget, type Selection } from '$lib/engine';
|
import { engine, type MoveTarget, type Selection } from '$lib/engine';
|
||||||
import { isSelected, type FileTreeNode } from '$lib/engine/selection-helpers';
|
import { isSelected, type FileTreeNode } from '$lib/engine/selection-helpers';
|
||||||
|
|
||||||
/** What a list holds: the files, the tracks of a file, the segments of a track, or the waypoints. */
|
/**
|
||||||
export type ListLevel = 'file' | 'track' | 'segment' | 'waypoints' | 'waypoint';
|
* What a list holds: the files, the routes or the tracks of a file, the segments of a track, or the
|
||||||
|
* waypoints.
|
||||||
|
*/
|
||||||
|
export type ListLevel = 'file' | 'route' | 'track' | 'segment' | 'waypoints' | 'waypoint';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Where the elements of a list can be moved to (a segment on its own can become a route). Routes
|
||||||
|
* and tracks can be dropped among each other, but keep their kind: the engine puts them with the
|
||||||
|
* others of their kind, the routes coming first.
|
||||||
|
*/
|
||||||
export const allowedMoves: Record<ListLevel, ListLevel[]> = {
|
export const allowedMoves: Record<ListLevel, ListLevel[]> = {
|
||||||
file: ['file'],
|
file: ['file'],
|
||||||
track: ['file', 'track'],
|
route: ['file', 'route', 'track'],
|
||||||
segment: ['file', 'track', 'segment'],
|
track: ['file', 'route', 'track'],
|
||||||
|
segment: ['file', 'route', 'track', 'segment'],
|
||||||
waypoints: ['waypoints'],
|
waypoints: ['waypoints'],
|
||||||
waypoint: ['waypoints', 'waypoint'],
|
waypoint: ['waypoints', 'waypoint'],
|
||||||
};
|
};
|
||||||
@@ -103,6 +112,8 @@ export class SortableFileList {
|
|||||||
switch (this._sortableLevel) {
|
switch (this._sortableLevel) {
|
||||||
case 'file':
|
case 'file':
|
||||||
return { type: 'file', fileId: id };
|
return { type: 'file', fileId: id };
|
||||||
|
case 'route':
|
||||||
|
return parent ? { type: 'route', fileId: parent.fileId, routeId: id } : undefined;
|
||||||
case 'track':
|
case 'track':
|
||||||
return parent ? { type: 'track', fileId: parent.fileId, trackId: id } : undefined;
|
return parent ? { type: 'track', fileId: parent.fileId, trackId: id } : undefined;
|
||||||
case 'segment':
|
case 'segment':
|
||||||
@@ -129,6 +140,8 @@ export class SortableFileList {
|
|||||||
switch (this._sortableLevel) {
|
switch (this._sortableLevel) {
|
||||||
case 'file':
|
case 'file':
|
||||||
return { type: 'file', fileIds: ids };
|
return { type: 'file', fileIds: ids };
|
||||||
|
case 'route':
|
||||||
|
return parent ? { type: 'route', fileId: parent.fileId, routeIds: ids } : undefined;
|
||||||
case 'track':
|
case 'track':
|
||||||
return parent ? { type: 'track', fileId: parent.fileId, trackIds: ids } : undefined;
|
return parent ? { type: 'track', fileId: parent.fileId, trackIds: ids } : undefined;
|
||||||
case 'segment':
|
case 'segment':
|
||||||
@@ -155,6 +168,8 @@ export class SortableFileList {
|
|||||||
switch (this._sortableLevel) {
|
switch (this._sortableLevel) {
|
||||||
case 'file':
|
case 'file':
|
||||||
return { type: 'files', index };
|
return { type: 'files', index };
|
||||||
|
case 'route':
|
||||||
|
return parent ? { type: 'routes', fileId: parent.fileId, index } : undefined;
|
||||||
case 'track':
|
case 'track':
|
||||||
return parent ? { type: 'tracks', fileId: parent.fileId, index } : undefined;
|
return parent ? { type: 'tracks', fileId: parent.fileId, index } : undefined;
|
||||||
case 'segment':
|
case 'segment':
|
||||||
@@ -275,6 +290,9 @@ export class SortableFileList {
|
|||||||
case 'file':
|
case 'file':
|
||||||
request = engine.select(selected);
|
request = engine.select(selected);
|
||||||
break;
|
break;
|
||||||
|
case 'route':
|
||||||
|
request = engine.selectRoutes(parent!.fileId, selected);
|
||||||
|
break;
|
||||||
case 'track':
|
case 'track':
|
||||||
request = engine.selectTracks(parent!.fileId, selected);
|
request = engine.selectTracks(parent!.fileId, selected);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
import { settings } from '$lib/state/settings';
|
import { settings } from '$lib/state/settings';
|
||||||
import { engine, type FileState } from '$lib/engine';
|
import { engine, type FileState } from '$lib/engine';
|
||||||
import { normalizeColor } from '$lib/engine/file-colors';
|
import { normalizeColor } from '$lib/engine/file-colors';
|
||||||
import { selectedFileIds, type FileTreeNode } from '$lib/engine/selection-helpers';
|
import { pathInfo, selectedFileIds, type FileTreeNode } from '$lib/engine/selection-helpers';
|
||||||
import { untrack } from 'svelte';
|
import { untrack } from 'svelte';
|
||||||
|
|
||||||
let {
|
let {
|
||||||
@@ -38,17 +38,15 @@
|
|||||||
width = $defaultWidth;
|
width = $defaultWidth;
|
||||||
color = fileState.color;
|
color = fileState.color;
|
||||||
|
|
||||||
const { tracks } = fileState.structure;
|
const paths = [...fileState.structure.routes, ...fileState.structure.tracks];
|
||||||
|
const path = pathInfo(fileState.structure, node);
|
||||||
if (node.type === 'file') {
|
if (node.type === 'file') {
|
||||||
opacity = tracks.find((track) => track.opacity !== undefined)?.opacity ?? opacity;
|
opacity = paths.find((path) => path.opacity !== undefined)?.opacity ?? opacity;
|
||||||
width = tracks.find((track) => track.width !== undefined)?.width ?? width;
|
width = paths.find((path) => path.width !== undefined)?.width ?? width;
|
||||||
} else if (node.type === 'track') {
|
} else if (path) {
|
||||||
const track = tracks.find((track) => track.id === node.trackId);
|
color = path.color !== undefined ? normalizeColor(path.color) : color;
|
||||||
if (track) {
|
opacity = path.opacity ?? opacity;
|
||||||
color = track.color !== undefined ? normalizeColor(track.color) : color;
|
width = path.width ?? width;
|
||||||
opacity = track.opacity ?? opacity;
|
|
||||||
width = track.width ?? width;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
colorChanged = false;
|
colorChanged = false;
|
||||||
|
|||||||
@@ -118,7 +118,11 @@ export class GPXLayer {
|
|||||||
return { type: 'file', fileId: this.fileId };
|
return { type: 'file', fileId: this.fileId };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The node of a segment of the map: its route, which has no other, or the segment itself. */
|
||||||
segmentNode(properties: GeoJSON.GeoJsonProperties): FileTreeNode {
|
segmentNode(properties: GeoJSON.GeoJsonProperties): FileTreeNode {
|
||||||
|
if (properties!.routeId !== undefined) {
|
||||||
|
return { type: 'route', fileId: this.fileId, routeId: properties!.routeId };
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
type: 'segment',
|
type: 'segment',
|
||||||
fileId: this.fileId,
|
fileId: this.fileId,
|
||||||
@@ -181,7 +185,11 @@ export class GPXLayer {
|
|||||||
const visibleSegmentIds = state.segments.features
|
const visibleSegmentIds = state.segments.features
|
||||||
.filter(
|
.filter(
|
||||||
({ properties }) =>
|
({ properties }) =>
|
||||||
!isSegmentHidden(state, properties.trackId, properties.segmentId)
|
!isSegmentHidden(
|
||||||
|
state,
|
||||||
|
properties.routeId ?? properties.trackId!,
|
||||||
|
properties.segmentId
|
||||||
|
)
|
||||||
)
|
)
|
||||||
.map(({ properties }) => properties.segmentId);
|
.map(({ properties }) => properties.segmentId);
|
||||||
const segmentFilter: FilterSpecification = [
|
const segmentFilter: FilterSpecification = [
|
||||||
@@ -415,7 +423,7 @@ export class GPXLayer {
|
|||||||
|
|
||||||
layerOnClick(e: MapLayerMouseEvent) {
|
layerOnClick(e: MapLayerMouseEvent) {
|
||||||
const properties = e.features![0].properties!;
|
const properties = e.features![0].properties!;
|
||||||
const { trackId, segmentId } = properties;
|
const { routeId, trackId, segmentId } = properties;
|
||||||
const selectionType = this.selectionState.type;
|
const selectionType = this.selectionState.type;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
@@ -441,10 +449,15 @@ export class GPXLayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const mode = e.originalEvent.ctrlKey || e.originalEvent.metaKey ? 'add' : 'replace';
|
const mode = e.originalEvent.ctrlKey || e.originalEvent.metaKey ? 'add' : 'replace';
|
||||||
const { tracks } = get(this.file).structure;
|
const { routes, tracks } = get(this.file).structure;
|
||||||
const segmentCount = tracks.reduce((count, track) => count + track.segments.length, 0);
|
const segmentCount =
|
||||||
|
routes.length + tracks.reduce((count, track) => count + track.segments.length, 0);
|
||||||
if (get(treeFileView) && segmentCount > 1) {
|
if (get(treeFileView) && segmentCount > 1) {
|
||||||
// Select inner item
|
// Select inner item
|
||||||
|
if (routeId !== undefined) {
|
||||||
|
engine.selectRoutes(this.fileId, [routeId], mode);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const track = tracks.find((track) => track.id === trackId);
|
const track = tracks.find((track) => track.id === trackId);
|
||||||
if (track && track.segments.length > 1) {
|
if (track && track.segments.length > 1) {
|
||||||
engine.selectSegments(this.fileId, trackId, [segmentId], mode);
|
engine.selectSegments(this.fileId, trackId, [segmentId], mode);
|
||||||
|
|||||||
@@ -15,13 +15,14 @@
|
|||||||
const files = engine.files;
|
const files = engine.files;
|
||||||
const statistics = engine.statistics;
|
const statistics = engine.statistics;
|
||||||
|
|
||||||
/** The number of segments of a file or of one of its tracks. */
|
/** The number of segments of a file (its routes have one each) or of one of its tracks. */
|
||||||
function segmentCount(fileId: string, trackId?: string) {
|
function segmentCount(fileId: string, trackId?: string) {
|
||||||
const file = $files.get(fileId);
|
const file = $files.get(fileId);
|
||||||
const tracks = file ? get(file).structure.tracks : [];
|
const structure = file ? get(file).structure : undefined;
|
||||||
return tracks
|
const routes = trackId === undefined ? (structure?.routes.length ?? 0) : 0;
|
||||||
|
return (structure?.tracks ?? [])
|
||||||
.filter((track) => trackId === undefined || track.id === trackId)
|
.filter((track) => trackId === undefined || track.id === trackId)
|
||||||
.reduce((count, track) => count + track.segments.length, 0);
|
.reduce((count, track) => count + track.segments.length, routes);
|
||||||
}
|
}
|
||||||
|
|
||||||
// files or tracks, which all have several segments
|
// files or tracks, which all have several segments
|
||||||
|
|||||||
@@ -31,11 +31,13 @@
|
|||||||
onDestroy(() => request.release());
|
onDestroy(() => request.release());
|
||||||
let segmentCount = $derived($statistics.anchors?.segmentStarts.length ?? 0);
|
let segmentCount = $derived($statistics.anchors?.segmentStarts.length ?? 0);
|
||||||
|
|
||||||
// several files, tracks or segments, or a single file or track with several segments
|
// several files, routes, tracks or segments, or a single file or track with several segments
|
||||||
let canMergeTraces = $derived.by(() => {
|
let canMergeTraces = $derived.by(() => {
|
||||||
switch ($selection.type) {
|
switch ($selection.type) {
|
||||||
case 'file':
|
case 'file':
|
||||||
return $selection.fileIds.length > 1 || segmentCount > 1;
|
return $selection.fileIds.length > 1 || segmentCount > 1;
|
||||||
|
case 'route':
|
||||||
|
return $selection.routeIds.length > 1;
|
||||||
case 'track':
|
case 'track':
|
||||||
return $selection.trackIds.length > 1 || segmentCount > 1;
|
return $selection.trackIds.length > 1 || segmentCount > 1;
|
||||||
case 'segment':
|
case 'segment':
|
||||||
@@ -45,7 +47,8 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// several files or tracks, that can be put together
|
// several files or tracks, that can be put together (a route has a single segment: routes
|
||||||
|
// cannot)
|
||||||
let canMergeContents = $derived(
|
let canMergeContents = $derived(
|
||||||
($selection.type === 'file' && $selection.fileIds.length > 1) ||
|
($selection.type === 'file' && $selection.fileIds.length > 1) ||
|
||||||
($selection.type === 'track' && $selection.trackIds.length > 1)
|
($selection.type === 'track' && $selection.trackIds.length > 1)
|
||||||
|
|||||||
@@ -172,9 +172,10 @@
|
|||||||
updateEnd();
|
updateEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
// a single file, track or segment
|
// a single file, route, track or segment
|
||||||
let canUpdate = $derived(
|
let canUpdate = $derived(
|
||||||
($selection.type === 'file' && $selection.fileIds.length === 1) ||
|
($selection.type === 'file' && $selection.fileIds.length === 1) ||
|
||||||
|
($selection.type === 'route' && $selection.routeIds.length === 1) ||
|
||||||
($selection.type === 'track' && $selection.trackIds.length === 1) ||
|
($selection.type === 'track' && $selection.trackIds.length === 1) ||
|
||||||
($selection.type === 'segment' && $selection.segmentIds.length === 1)
|
($selection.type === 'segment' && $selection.segmentIds.length === 1)
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
import { onDestroy } from 'svelte';
|
import { onDestroy } from 'svelte';
|
||||||
import { getURLForLanguage } from '$lib/utils';
|
import { getURLForLanguage } from '$lib/utils';
|
||||||
import { engine } from '$lib/engine';
|
import { engine } from '$lib/engine';
|
||||||
|
import { coversSegments } from '$lib/engine/selection-helpers';
|
||||||
import { minTolerance, ReducedLayer, tolerance } from './utils.svelte';
|
import { minTolerance, ReducedLayer, tolerance } from './utils.svelte';
|
||||||
|
|
||||||
let props: { class?: string } = $props();
|
let props: { class?: string } = $props();
|
||||||
@@ -23,9 +24,7 @@
|
|||||||
const request = engine.requestStatistics();
|
const request = engine.requestStatistics();
|
||||||
request.set(['anchors']);
|
request.set(['anchors']);
|
||||||
|
|
||||||
let validSelection = $derived(
|
let validSelection = $derived(coversSegments($selection));
|
||||||
$selection.type === 'file' || $selection.type === 'track' || $selection.type === 'segment'
|
|
||||||
);
|
|
||||||
|
|
||||||
let reducedLayer = new ReducedLayer();
|
let reducedLayer = new ReducedLayer();
|
||||||
|
|
||||||
|
|||||||
@@ -31,6 +31,7 @@
|
|||||||
import { settings } from '$lib/state/settings';
|
import { settings } from '$lib/state/settings';
|
||||||
import { map } from '$lib/components/map/map';
|
import { map } from '$lib/components/map/map';
|
||||||
import { engine } from '$lib/engine';
|
import { engine } from '$lib/engine';
|
||||||
|
import { coversSegments } from '$lib/engine/selection-helpers';
|
||||||
import { newFileName } from '$lib/actions/file-actions';
|
import { newFileName } from '$lib/actions/file-actions';
|
||||||
import { mapCursor, MapCursorState } from '$lib/state/map-cursor';
|
import { mapCursor, MapCursorState } from '$lib/state/map-cursor';
|
||||||
import { RoutingControls } from './routing-controls';
|
import { RoutingControls } from './routing-controls';
|
||||||
@@ -54,10 +55,8 @@
|
|||||||
const selection = engine.selection;
|
const selection = engine.selection;
|
||||||
let routingControls: RoutingControls | undefined = undefined;
|
let routingControls: RoutingControls | undefined = undefined;
|
||||||
|
|
||||||
// the tool works on files, tracks and segments, not on waypoints
|
// the tool works on files, routes, tracks and segments, not on waypoints
|
||||||
let validSelection = $derived(
|
let validSelection = $derived(coversSegments($selection));
|
||||||
$selection.type === 'file' || $selection.type === 'track' || $selection.type === 'segment'
|
|
||||||
);
|
|
||||||
|
|
||||||
function createFileWithPoint(e: any) {
|
function createFileWithPoint(e: any) {
|
||||||
if ($selection.type === 'empty') {
|
if ($selection.type === 'empty') {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import { map } from '$lib/components/map/map';
|
|||||||
import { ANCHOR_LAYER_KEY } from '$lib/components/map/style';
|
import { ANCHOR_LAYER_KEY } from '$lib/components/map/style';
|
||||||
import { settings } from '$lib/state/settings';
|
import { settings } from '$lib/state/settings';
|
||||||
import { closestPointIndexIn } from '$lib/geo/closest-point';
|
import { closestPointIndexIn } from '$lib/geo/closest-point';
|
||||||
|
import { coversSegments } from '$lib/engine/selection-helpers';
|
||||||
import {
|
import {
|
||||||
engine,
|
engine,
|
||||||
type Selection,
|
type Selection,
|
||||||
@@ -53,7 +54,7 @@ type AnchorFeature = GeoJSON.Feature<GeoJSON.Point, AnchorProperties>;
|
|||||||
|
|
||||||
/** Whether the tool has to show controls for a selection: it covers segments, or can get some. */
|
/** Whether the tool has to show controls for a selection: it covers segments, or can get some. */
|
||||||
function isRoutable(selection: Selection) {
|
function isRoutable(selection: Selection) {
|
||||||
return selection.type === 'file' || selection.type === 'track' || selection.type === 'segment';
|
return coversSegments(selection);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { splitAs, SplitType } from '$lib/components/toolbar/tools/scissors/scissors';
|
import { availableSplitTypes, splitAs } from '$lib/components/toolbar/tools/scissors/scissors';
|
||||||
import Help from '$lib/components/Help.svelte';
|
import Help from '$lib/components/Help.svelte';
|
||||||
import { Label } from '$lib/components/ui/label/index.js';
|
import { Label } from '$lib/components/ui/label/index.js';
|
||||||
import { Button } from '$lib/components/ui/button';
|
import { Button } from '$lib/components/ui/button';
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
import { getURLForLanguage } from '$lib/utils';
|
import { getURLForLanguage } from '$lib/utils';
|
||||||
import { slicedStatistics } from '$lib/state/selection-statistics';
|
import { slicedStatistics } from '$lib/state/selection-statistics';
|
||||||
import { engine } from '$lib/engine';
|
import { engine } from '$lib/engine';
|
||||||
|
import { coversSegments } from '$lib/engine/selection-helpers';
|
||||||
|
|
||||||
let props: {
|
let props: {
|
||||||
class?: string;
|
class?: string;
|
||||||
@@ -24,13 +25,20 @@
|
|||||||
const selection = engine.selection;
|
const selection = engine.selection;
|
||||||
const statistics = engine.statistics;
|
const statistics = engine.statistics;
|
||||||
|
|
||||||
// the tool works on files, tracks and segments, not on waypoints
|
// the ways the selection can be split, which follow its content
|
||||||
let validSelection = $derived(
|
let splitTypes = $derived.by(() => {
|
||||||
($selection.type === 'file' ||
|
// what is selected can change without the selection: the files are edited
|
||||||
$selection.type === 'track' ||
|
void $statistics;
|
||||||
$selection.type === 'segment') &&
|
return availableSplitTypes($selection);
|
||||||
$statistics.length > 0
|
});
|
||||||
);
|
$effect(() => {
|
||||||
|
if (!splitTypes.includes($splitAs)) {
|
||||||
|
$splitAs = splitTypes[0];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// the tool works on files, routes, tracks and segments, not on waypoints
|
||||||
|
let validSelection = $derived(coversSegments($selection) && $statistics.length > 0);
|
||||||
// The slider does work for each of its steps every time it changes, which is too slow with
|
// The slider does work for each of its steps every time it changes, which is too slow with
|
||||||
// the thousands of trackpoints of a long track: it has a limited number of steps, which are
|
// the thousands of trackpoints of a long track: it has a limited number of steps, which are
|
||||||
// spread over the trackpoints.
|
// spread over the trackpoints.
|
||||||
@@ -123,7 +131,7 @@
|
|||||||
{i18n._('gpx.' + $splitAs)}
|
{i18n._('gpx.' + $splitAs)}
|
||||||
</Select.Trigger>
|
</Select.Trigger>
|
||||||
<Select.Content>
|
<Select.Content>
|
||||||
{#each Object.values(SplitType) as splitType}
|
{#each splitTypes as splitType}
|
||||||
<Select.Item value={splitType}>{i18n._('gpx.' + splitType)}</Select.Item>
|
<Select.Item value={splitType}>{i18n._('gpx.' + splitType)}</Select.Item>
|
||||||
{/each}
|
{/each}
|
||||||
</Select.Content>
|
</Select.Content>
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { get, writable, type Writable } from 'svelte/store';
|
import { get, writable, type Writable } from 'svelte/store';
|
||||||
import { engine, type SplitType as EngineSplitType } from '$lib/engine';
|
import { engine, type Selection, type SplitType as EngineSplitType } from '$lib/engine';
|
||||||
|
import { selectedFileIds } from '$lib/engine/selection-helpers';
|
||||||
import { closestPointIndexIn } from '$lib/geo/closest-point';
|
import { closestPointIndexIn } from '$lib/geo/closest-point';
|
||||||
|
|
||||||
/** What a split cuts in two, the values being the ones of the engine. */
|
/** What a split cuts in two, the values being the ones of the engine. */
|
||||||
export const SplitType = {
|
export const SplitType = {
|
||||||
FILES: 'files',
|
FILES: 'files',
|
||||||
|
ROUTES: 'routes',
|
||||||
TRACKS: 'tracks',
|
TRACKS: 'tracks',
|
||||||
SEGMENTS: 'segments',
|
SEGMENTS: 'segments',
|
||||||
} as const satisfies Record<string, EngineSplitType>;
|
} as const satisfies Record<string, EngineSplitType>;
|
||||||
@@ -12,11 +14,62 @@ export type SplitType = (typeof SplitType)[keyof typeof SplitType];
|
|||||||
|
|
||||||
export let splitAs: Writable<SplitType> = writable(SplitType.FILES);
|
export let splitAs: Writable<SplitType> = writable(SplitType.FILES);
|
||||||
|
|
||||||
|
/** The ids of the segments of the routes of the files of the selection. */
|
||||||
|
export function routeSegmentIds(selection: Selection): Set<string> {
|
||||||
|
const files = get(engine.files);
|
||||||
|
return new Set(
|
||||||
|
selectedFileIds(selection).flatMap((fileId) => {
|
||||||
|
const file = files.get(fileId);
|
||||||
|
return file ? get(file).structure.routes.map((route) => route.segment.id) : [];
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splits at the trackpoint of a selected segment that is the closest to `point`, as `splitAs` says.
|
* The ways the selection can be split: as files always, as routes if it has routes, as tracks or
|
||||||
|
* segments if it has tracks with segments (the engine does nothing for a trackpoint that is not in
|
||||||
|
* what it is asked to cut).
|
||||||
|
*/
|
||||||
|
export function availableSplitTypes(selection: Selection): SplitType[] {
|
||||||
|
let routes = false;
|
||||||
|
let tracks = false;
|
||||||
|
if (selection.type === 'route') {
|
||||||
|
routes = true;
|
||||||
|
} else if (selection.type === 'track' || selection.type === 'segment') {
|
||||||
|
tracks = true;
|
||||||
|
} else {
|
||||||
|
const files = get(engine.files);
|
||||||
|
for (const fileId of selectedFileIds(selection)) {
|
||||||
|
const file = files.get(fileId);
|
||||||
|
if (file) {
|
||||||
|
const { structure } = get(file);
|
||||||
|
routes ||= structure.routes.length > 0;
|
||||||
|
tracks ||= structure.tracks.some((track) => track.segments.length > 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
SplitType.FILES,
|
||||||
|
...(routes ? [SplitType.ROUTES] : []),
|
||||||
|
...(tracks ? [SplitType.TRACKS, SplitType.SEGMENTS] : []),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Whether splitting as `type` cuts something at a trackpoint of a segment, which is a route's or not. */
|
||||||
|
export function cuts(type: SplitType, inRoute: boolean): boolean {
|
||||||
|
return type === SplitType.FILES || (type === SplitType.ROUTES) === inRoute;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Splits at the trackpoint of a selected segment that is the closest to `point`, as `splitAs` says
|
||||||
|
* (nothing happens if the segment cannot be split that way).
|
||||||
* The statistics of the engine need to have the anchors (the scissors tool asks for them).
|
* The statistics of the engine need to have the anchors (the scissors tool asks for them).
|
||||||
*/
|
*/
|
||||||
export function splitAtPoint(segmentId: string, point: { lng: number; lat: number }) {
|
export function splitAtPoint(segmentId: string, point: { lng: number; lat: number }) {
|
||||||
|
const inRoute = routeSegmentIds(get(engine.selection)).has(segmentId);
|
||||||
|
if (!cuts(get(splitAs), inRoute)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const statistics = get(engine.statistics);
|
const statistics = get(engine.statistics);
|
||||||
const anchors = statistics.anchors;
|
const anchors = statistics.anchors;
|
||||||
const segment = anchors?.segmentIds.indexOf(segmentId) ?? -1;
|
const segment = anchors?.segmentIds.indexOf(segmentId) ?? -1;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { currentTool, Tool } from '$lib/components/toolbar/tools';
|
import { currentTool, Tool } from '$lib/components/toolbar/tools';
|
||||||
import { splitAs } from '$lib/components/toolbar/tools/scissors/scissors';
|
import { cuts, routeSegmentIds, splitAs } from '$lib/components/toolbar/tools/scissors/scissors';
|
||||||
import { Scissors } from 'lucide-static';
|
import { Scissors } from 'lucide-static';
|
||||||
import { get } from 'svelte/store';
|
import { get } from 'svelte/store';
|
||||||
import { mapCursor, MapCursorState } from '$lib/state/map-cursor';
|
import { mapCursor, MapCursorState } from '$lib/state/map-cursor';
|
||||||
@@ -7,6 +7,7 @@ import type { GeoJSONSource, Map, MapLayerMouseEvent } from 'maplibre-gl';
|
|||||||
import { ANCHOR_LAYER_KEY } from '$lib/components/map/style';
|
import { ANCHOR_LAYER_KEY } from '$lib/components/map/style';
|
||||||
import type { MapLayerEventManager } from '$lib/components/map/map-layer-event-manager';
|
import type { MapLayerEventManager } from '$lib/components/map/map-layer-event-manager';
|
||||||
import { loadSVGIcon } from '$lib/utils';
|
import { loadSVGIcon } from '$lib/utils';
|
||||||
|
import { coversSegments } from '$lib/engine/selection-helpers';
|
||||||
import {
|
import {
|
||||||
engine,
|
engine,
|
||||||
type Selection,
|
type Selection,
|
||||||
@@ -15,9 +16,9 @@ import {
|
|||||||
} from '$lib/engine';
|
} from '$lib/engine';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The anchors inside the segments of the selection, where a click splits the file, the track or
|
* The anchors inside the segments of the selection, where a click splits the file, the route, the
|
||||||
* the segment: a command of the engine, which finds them by the index of their trackpoint in the
|
* track or the segment: a command of the engine, which finds them by the index of their trackpoint
|
||||||
* selection.
|
* in the selection. Only the segments that can be split as `splitAs` says have them.
|
||||||
*/
|
*/
|
||||||
export class SplitControls {
|
export class SplitControls {
|
||||||
map: Map;
|
map: Map;
|
||||||
@@ -53,6 +54,7 @@ export class SplitControls {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
this.unsubscribes.push(currentTool.subscribe(this.addIfNeeded.bind(this)));
|
this.unsubscribes.push(currentTool.subscribe(this.addIfNeeded.bind(this)));
|
||||||
|
this.unsubscribes.push(splitAs.subscribe(this.addIfNeeded.bind(this)));
|
||||||
this.unsubscribes.push(
|
this.unsubscribes.push(
|
||||||
engine.selection.subscribe((selection) => {
|
engine.selection.subscribe((selection) => {
|
||||||
this.selection = selection;
|
this.selection = selection;
|
||||||
@@ -62,12 +64,8 @@ export class SplitControls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addIfNeeded() {
|
addIfNeeded() {
|
||||||
// the tool works on files, tracks and segments, not on waypoints
|
// the tool works on files, routes, tracks and segments, not on waypoints
|
||||||
const scissors =
|
const scissors = get(currentTool) === Tool.SCISSORS && coversSegments(this.selection);
|
||||||
get(currentTool) === Tool.SCISSORS &&
|
|
||||||
(this.selection.type === 'file' ||
|
|
||||||
this.selection.type === 'track' ||
|
|
||||||
this.selection.type === 'segment');
|
|
||||||
this.request.set(scissors ? ['anchors'] : []);
|
this.request.set(scissors ? ['anchors'] : []);
|
||||||
if (!scissors) {
|
if (!scissors) {
|
||||||
this.remove();
|
this.remove();
|
||||||
@@ -85,6 +83,8 @@ export class SplitControls {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
const starts = anchors.segmentStarts;
|
const starts = anchors.segmentStarts;
|
||||||
|
const routeSegments = routeSegmentIds(this.selection);
|
||||||
|
const type = get(splitAs);
|
||||||
const features: GeoJSON.Feature[] = [];
|
const features: GeoJSON.Feature[] = [];
|
||||||
let segment = 0;
|
let segment = 0;
|
||||||
for (let i = 0; i < anchors.indices.length; i++) {
|
for (let i = 0; i < anchors.indices.length; i++) {
|
||||||
@@ -94,7 +94,8 @@ export class SplitControls {
|
|||||||
segment++;
|
segment++;
|
||||||
}
|
}
|
||||||
const end = segment + 1 < starts.length ? starts[segment + 1] : statistics.length;
|
const end = segment + 1 < starts.length ? starts[segment + 1] : statistics.length;
|
||||||
if (index > starts[segment] && index < end - 1) {
|
const inRoute = routeSegments.has(anchors.segmentIds[segment]);
|
||||||
|
if (index > starts[segment] && index < end - 1 && cuts(type, inRoute)) {
|
||||||
features.push({
|
features.push({
|
||||||
type: 'Feature',
|
type: 'Feature',
|
||||||
geometry: {
|
geometry: {
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ export async function createTauriBackend(): Promise<Backend> {
|
|||||||
select: (fileIds, mode) => invoke('select', { fileIds, mode }),
|
select: (fileIds, mode) => invoke('select', { fileIds, mode }),
|
||||||
select_all: () => invoke('select_all'),
|
select_all: () => invoke('select_all'),
|
||||||
arrow_select: (down, add) => invoke('arrow_select', { down, add }),
|
arrow_select: (down, add) => invoke('arrow_select', { down, add }),
|
||||||
|
select_routes: (fileId, routeIds, mode) =>
|
||||||
|
invoke('select_routes', { fileId, routeIds, mode }),
|
||||||
select_tracks: (fileId, trackIds, mode) =>
|
select_tracks: (fileId, trackIds, mode) =>
|
||||||
invoke('select_tracks', { fileId, trackIds, mode }),
|
invoke('select_tracks', { fileId, trackIds, mode }),
|
||||||
select_segments: (fileId, trackId, segmentIds, mode) =>
|
select_segments: (fileId, trackId, segmentIds, mode) =>
|
||||||
@@ -81,6 +83,7 @@ export async function createTauriBackend(): Promise<Backend> {
|
|||||||
}),
|
}),
|
||||||
metadata: (name, desc) => invoke('metadata', { name, desc }),
|
metadata: (name, desc) => invoke('metadata', { name, desc }),
|
||||||
style: (color, opacity, width) => invoke('style', { color, opacity, width }),
|
style: (color, opacity, width) => invoke('style', { color, opacity, width }),
|
||||||
|
new_route: () => invoke('new_route'),
|
||||||
new_track: () => invoke('new_track'),
|
new_track: () => invoke('new_track'),
|
||||||
new_track_segment: () => invoke('new_track_segment'),
|
new_track_segment: () => invoke('new_track_segment'),
|
||||||
|
|
||||||
@@ -156,7 +159,7 @@ export async function createTauriBackend(): Promise<Backend> {
|
|||||||
(await invoke('waypoint', { fileId, waypointId })) ?? undefined,
|
(await invoke('waypoint', { fileId, waypointId })) ?? undefined,
|
||||||
trackpoint: async (fileId, segmentId, index) =>
|
trackpoint: async (fileId, segmentId, index) =>
|
||||||
(await invoke('trackpoint', { fileId, segmentId, index })) ?? undefined,
|
(await invoke('trackpoint', { fileId, segmentId, index })) ?? undefined,
|
||||||
export_file: async (fileId, time, hr, cad, atemp, power, osm, asRoute) => {
|
export_file: async (fileId, time, hr, cad, atemp, power, osm) => {
|
||||||
const file = await optionalArray('export_file', Uint8Array, {
|
const file = await optionalArray('export_file', Uint8Array, {
|
||||||
fileId,
|
fileId,
|
||||||
time,
|
time,
|
||||||
@@ -165,7 +168,6 @@ export async function createTauriBackend(): Promise<Backend> {
|
|||||||
atemp,
|
atemp,
|
||||||
power,
|
power,
|
||||||
osm,
|
osm,
|
||||||
asRoute,
|
|
||||||
});
|
});
|
||||||
return file as Uint8Array<ArrayBuffer> | undefined;
|
return file as Uint8Array<ArrayBuffer> | undefined;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -146,6 +146,7 @@ export interface Backend {
|
|||||||
select(fileIds: string[], mode: SelectMode): Promise<Outcome>;
|
select(fileIds: string[], mode: SelectMode): Promise<Outcome>;
|
||||||
select_all(): Promise<Outcome>;
|
select_all(): Promise<Outcome>;
|
||||||
arrow_select(down: boolean, add: boolean): Promise<Outcome>;
|
arrow_select(down: boolean, add: boolean): Promise<Outcome>;
|
||||||
|
select_routes(fileId: string, routeIds: string[], mode: SelectMode): Promise<Outcome>;
|
||||||
select_tracks(fileId: string, trackIds: string[], mode: SelectMode): Promise<Outcome>;
|
select_tracks(fileId: string, trackIds: string[], mode: SelectMode): Promise<Outcome>;
|
||||||
select_segments(
|
select_segments(
|
||||||
fileId: string,
|
fileId: string,
|
||||||
@@ -173,6 +174,7 @@ export interface Backend {
|
|||||||
move_elements(what: Selection, to: MoveTarget): Promise<Outcome>;
|
move_elements(what: Selection, to: MoveTarget): Promise<Outcome>;
|
||||||
metadata(name: string, desc: string): Promise<Outcome>;
|
metadata(name: string, desc: string): Promise<Outcome>;
|
||||||
style(color?: string, opacity?: number, width?: number): Promise<Outcome>;
|
style(color?: string, opacity?: number, width?: number): Promise<Outcome>;
|
||||||
|
new_route(): Promise<Outcome>;
|
||||||
new_track(): Promise<Outcome>;
|
new_track(): Promise<Outcome>;
|
||||||
new_track_segment(): Promise<Outcome>;
|
new_track_segment(): Promise<Outcome>;
|
||||||
|
|
||||||
@@ -262,8 +264,7 @@ export interface Backend {
|
|||||||
cad: boolean,
|
cad: boolean,
|
||||||
atemp: boolean,
|
atemp: boolean,
|
||||||
power: boolean,
|
power: boolean,
|
||||||
osm: boolean,
|
osm: boolean
|
||||||
asRoute: boolean
|
|
||||||
): Promise<Uint8Array<ArrayBuffer> | undefined>;
|
): Promise<Uint8Array<ArrayBuffer> | undefined>;
|
||||||
exportable_data(fileIds: string[]): Promise<number>;
|
exportable_data(fileIds: string[]): Promise<number>;
|
||||||
reduction_distances(): Promise<Float64Array>;
|
reduction_distances(): Promise<Float64Array>;
|
||||||
|
|||||||
@@ -29,15 +29,19 @@ export async function createWasmBackend(): Promise<Backend> {
|
|||||||
const mode = (m: SelectMode) =>
|
const mode = (m: SelectMode) =>
|
||||||
({ replace: w.SelectMode.Replace, add: w.SelectMode.Add, toggle: w.SelectMode.Toggle })[m];
|
({ replace: w.SelectMode.Replace, add: w.SelectMode.Add, toggle: w.SelectMode.Toggle })[m];
|
||||||
const splitType = (t: SplitType) =>
|
const splitType = (t: SplitType) =>
|
||||||
({ files: w.SplitType.Files, tracks: w.SplitType.Tracks, segments: w.SplitType.Segments })[
|
({
|
||||||
t
|
files: w.SplitType.Files,
|
||||||
];
|
routes: w.SplitType.Routes,
|
||||||
|
tracks: w.SplitType.Tracks,
|
||||||
|
segments: w.SplitType.Segments,
|
||||||
|
})[t];
|
||||||
const mergeType = (t: MergeType) => (t === 'connect' ? w.MergeType.Connect : w.MergeType.Group);
|
const mergeType = (t: MergeType) => (t === 'connect' ? w.MergeType.Connect : w.MergeType.Group);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
select: lift((ids, m) => w.select(idsToBytes(ids), mode(m))),
|
select: lift((ids, m) => w.select(idsToBytes(ids), mode(m))),
|
||||||
select_all: lift(w.select_all),
|
select_all: lift(w.select_all),
|
||||||
arrow_select: lift(w.arrow_select),
|
arrow_select: lift(w.arrow_select),
|
||||||
|
select_routes: lift((file, ids, m) => w.select_routes(file, idsToBytes(ids), mode(m))),
|
||||||
select_tracks: lift((file, ids, m) => w.select_tracks(file, idsToBytes(ids), mode(m))),
|
select_tracks: lift((file, ids, m) => w.select_tracks(file, idsToBytes(ids), mode(m))),
|
||||||
select_segments: lift((file, track, ids, m) =>
|
select_segments: lift((file, track, ids, m) =>
|
||||||
w.select_segments(file, track, idsToBytes(ids), mode(m))
|
w.select_segments(file, track, idsToBytes(ids), mode(m))
|
||||||
@@ -75,6 +79,7 @@ export async function createWasmBackend(): Promise<Backend> {
|
|||||||
move_elements: lift(w.move_elements),
|
move_elements: lift(w.move_elements),
|
||||||
metadata: lift(w.metadata),
|
metadata: lift(w.metadata),
|
||||||
style: lift(w.style),
|
style: lift(w.style),
|
||||||
|
new_route: lift(w.new_route),
|
||||||
new_track: lift(w.new_track),
|
new_track: lift(w.new_track),
|
||||||
new_track_segment: lift(w.new_track_segment),
|
new_track_segment: lift(w.new_track_segment),
|
||||||
|
|
||||||
|
|||||||
@@ -175,6 +175,35 @@ describe('selection', () => {
|
|||||||
assert.ok(get(engine.statistics).length > trackLength);
|
assert.ok(get(engine.statistics).length > trackLength);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('selects routes, whose segments are on the map', async () => {
|
||||||
|
await load('with_routes');
|
||||||
|
const file = fileStates()[0];
|
||||||
|
const { id, routes, tracks } = file.structure;
|
||||||
|
assert.equal(routes.length, 2);
|
||||||
|
assert.equal(tracks.length, 0);
|
||||||
|
// the segments of the map are the ones of the routes
|
||||||
|
assert.deepEqual(
|
||||||
|
file.segments.features.map((f) => f.properties.routeId),
|
||||||
|
routes.map((route) => route.id)
|
||||||
|
);
|
||||||
|
assert.deepEqual(
|
||||||
|
file.segments.features.map((f) => f.properties.segmentId),
|
||||||
|
routes.map((route) => route.segment.id)
|
||||||
|
);
|
||||||
|
|
||||||
|
await engine.selectRoutes(id, [routes[1].id]);
|
||||||
|
assert.deepEqual(get(engine.selection), {
|
||||||
|
type: 'route',
|
||||||
|
fileId: id,
|
||||||
|
routeIds: [routes[1].id],
|
||||||
|
});
|
||||||
|
assert.equal(get(engine.statistics).length, routes[1].segment.length);
|
||||||
|
|
||||||
|
await engine.select([id]);
|
||||||
|
assert.equal(await engine.newRoute(), true);
|
||||||
|
assert.equal(fileStates()[0].structure.routes.length, 3);
|
||||||
|
});
|
||||||
|
|
||||||
it('ignores what does not exist', async () => {
|
it('ignores what does not exist', async () => {
|
||||||
await load('simple');
|
await load('simple');
|
||||||
const before = get(engine.selection);
|
const before = get(engine.selection);
|
||||||
@@ -415,42 +444,32 @@ describe('export', () => {
|
|||||||
await engine.exportFile(id, { ...ALL_EXPORT_OPTIONS, ...options })
|
await engine.exportFile(id, { ...ALL_EXPORT_OPTIONS, ...options })
|
||||||
);
|
);
|
||||||
|
|
||||||
it('writes tracks, or routes when asked to', async () => {
|
it('writes tracks as tracks', async () => {
|
||||||
await load('with_tracks_and_segments');
|
await load('with_tracks_and_segments');
|
||||||
const file = fileStates()[0];
|
const file = fileStates()[0];
|
||||||
const segments = file.structure.tracks.reduce(
|
const segments = file.structure.tracks.reduce(
|
||||||
(sum, track) => sum + track.segments.length,
|
(sum, track) => sum + track.segments.length,
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
const id = file.structure.id;
|
const tracks = await text(file.structure.id);
|
||||||
|
|
||||||
const tracks = await text(id);
|
|
||||||
assert.ok(tracks.includes('<trk>') && !tracks.includes('<rte>'));
|
assert.ok(tracks.includes('<trk>') && !tracks.includes('<rte>'));
|
||||||
assert.equal(tracks.match(/<trkseg>/g)?.length, segments);
|
assert.equal(tracks.match(/<trkseg>/g)?.length, segments);
|
||||||
|
|
||||||
const routes = await text(id, { asRoute: true });
|
|
||||||
assert.ok(routes.includes('<rte>') && !routes.includes('<trk>'));
|
|
||||||
assert.equal(routes.match(/<rte>/g)?.length, segments);
|
|
||||||
assert.ok(routes.includes('<rtept'));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not count the way a file is written among its data', async () => {
|
|
||||||
await load('simple');
|
|
||||||
const data = await engine.exportableData(get(engine.order));
|
|
||||||
assert.equal(data.asRoute, false);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can read back the routes it writes', async () => {
|
it('can read back the routes it writes', async () => {
|
||||||
await load('with_tracks_and_segments');
|
await load('with_routes');
|
||||||
const id = get(engine.order)[0];
|
const [id] = get(engine.order);
|
||||||
const before = fileStates()[0].segments.features.length;
|
const before = fileStates()[0].structure.routes.length;
|
||||||
const routes = (await engine.exportFile(id, { ...ALL_EXPORT_OPTIONS, asRoute: true }))!;
|
const routes = (await engine.exportFile(id))!;
|
||||||
|
const written = new TextDecoder().decode(routes);
|
||||||
|
assert.equal(written.match(/<rte>/g)?.length, before);
|
||||||
|
assert.ok(!written.includes('<trk>') && written.includes('<rtept'));
|
||||||
|
|
||||||
await engine.loadFiles([{ data: routes, name: 'routes' }]);
|
await engine.loadFiles([{ data: routes, name: 'routes' }]);
|
||||||
const [, loaded] = get(engine.order);
|
const [, loaded] = get(engine.order);
|
||||||
const state = get(get(engine.files).get(loaded)!);
|
const state = get(get(engine.files).get(loaded)!);
|
||||||
// one route per segment, each read as a track of one segment
|
assert.equal(state.structure.routes.length, before);
|
||||||
assert.equal(state.structure.tracks.length, before);
|
assert.equal(state.structure.tracks.length, 0);
|
||||||
assert.equal(state.segments.features.length, before);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -182,6 +182,10 @@ class Engine {
|
|||||||
|
|
||||||
// Selecting elements inside a file. Ids that do not exist are ignored.
|
// Selecting elements inside a file. Ids that do not exist are ignored.
|
||||||
|
|
||||||
|
selectRoutes(fileId: string, routeIds: string[], mode: SelectMode = 'replace') {
|
||||||
|
return this.run((b) => b.select_routes(fileId, routeIds, mode));
|
||||||
|
}
|
||||||
|
|
||||||
selectTracks(fileId: string, trackIds: string[], mode: SelectMode = 'replace') {
|
selectTracks(fileId: string, trackIds: string[], mode: SelectMode = 'replace') {
|
||||||
return this.run((b) => b.select_tracks(fileId, trackIds, mode));
|
return this.run((b) => b.select_tracks(fileId, trackIds, mode));
|
||||||
}
|
}
|
||||||
@@ -209,6 +213,8 @@ class Engine {
|
|||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'file':
|
case 'file':
|
||||||
return this.select([node.fileId], mode);
|
return this.select([node.fileId], mode);
|
||||||
|
case 'route':
|
||||||
|
return this.selectRoutes(node.fileId, [node.routeId], mode);
|
||||||
case 'track':
|
case 'track':
|
||||||
return this.selectTracks(node.fileId, [node.trackId], mode);
|
return this.selectTracks(node.fileId, [node.trackId], mode);
|
||||||
case 'segment':
|
case 'segment':
|
||||||
@@ -359,6 +365,11 @@ class Engine {
|
|||||||
|
|
||||||
// Edits of the selection
|
// Edits of the selection
|
||||||
|
|
||||||
|
/** Adds an empty route to the selected files. */
|
||||||
|
newRoute() {
|
||||||
|
return this.run((b) => b.new_route());
|
||||||
|
}
|
||||||
|
|
||||||
newTrack() {
|
newTrack() {
|
||||||
return this.run((b) => b.new_track());
|
return this.run((b) => b.new_track());
|
||||||
}
|
}
|
||||||
@@ -868,8 +879,7 @@ class Engine {
|
|||||||
options.cad,
|
options.cad,
|
||||||
options.atemp,
|
options.atemp,
|
||||||
options.power,
|
options.power,
|
||||||
options.osm,
|
options.osm
|
||||||
options.asRoute
|
|
||||||
),
|
),
|
||||||
undefined
|
undefined
|
||||||
);
|
);
|
||||||
@@ -885,8 +895,6 @@ class Engine {
|
|||||||
atemp: (bits & 8) !== 0,
|
atemp: (bits & 8) !== 0,
|
||||||
power: (bits & 16) !== 0,
|
power: (bits & 16) !== 0,
|
||||||
osm: (bits & 32) !== 0,
|
osm: (bits & 32) !== 0,
|
||||||
// not a data
|
|
||||||
asRoute: false,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,13 @@ import { normalizeColor, type FileColorAllocator } from '$lib/engine/file-colors
|
|||||||
import type { Visibility } from '$lib/engine/file-visibility';
|
import type { Visibility } from '$lib/engine/file-visibility';
|
||||||
import type { Backend } from './backend';
|
import type { Backend } from './backend';
|
||||||
import { toPositions } from './convert';
|
import { toPositions } from './convert';
|
||||||
import { EMPTY_STATISTICS, type FileState, type SegmentProperties } from './types';
|
import {
|
||||||
|
EMPTY_STATISTICS,
|
||||||
|
type FileState,
|
||||||
|
type PathInfo,
|
||||||
|
type SegmentNode,
|
||||||
|
type SegmentProperties,
|
||||||
|
} from './types';
|
||||||
|
|
||||||
/** Reads the state of a file, reusing from `previous` what did not change. */
|
/** Reads the state of a file, reusing from `previous` what did not change. */
|
||||||
export async function readFileState(
|
export async function readFileState(
|
||||||
@@ -18,9 +24,10 @@ export async function readFileState(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const paths: PathInfo[] = [...structure.routes, ...structure.tracks];
|
||||||
const color = colors.resolve(
|
const color = colors.resolve(
|
||||||
id,
|
id,
|
||||||
structure.tracks.map((track) => track.color)
|
paths.map((path) => path.color)
|
||||||
);
|
);
|
||||||
|
|
||||||
const previousSegments = new Map(
|
const previousSegments = new Map(
|
||||||
@@ -31,38 +38,42 @@ export async function readFileState(
|
|||||||
| Feature<LineString, SegmentProperties>
|
| Feature<LineString, SegmentProperties>
|
||||||
| Promise<Feature<LineString, SegmentProperties>>
|
| Promise<Feature<LineString, SegmentProperties>>
|
||||||
)[] = [];
|
)[] = [];
|
||||||
structure.tracks.forEach((track, trackIndex) => {
|
const addSegment = (
|
||||||
track.segments.forEach((segment, segmentIndex) => {
|
segment: SegmentNode,
|
||||||
const properties: SegmentProperties = {
|
path: PathInfo,
|
||||||
fileId: id,
|
parent: Pick<SegmentProperties, 'routeId' | 'trackId'>
|
||||||
trackId: track.id,
|
) => {
|
||||||
segmentId: segment.id,
|
const properties: SegmentProperties = {
|
||||||
trackIndex,
|
fileId: id,
|
||||||
segmentIndex,
|
...parent,
|
||||||
rev: segment.rev,
|
segmentId: segment.id,
|
||||||
color: track.color !== undefined ? normalizeColor(track.color) : color,
|
rev: segment.rev,
|
||||||
opacity: track.opacity,
|
color: path.color !== undefined ? normalizeColor(path.color) : color,
|
||||||
width: track.width,
|
opacity: path.opacity,
|
||||||
};
|
width: path.width,
|
||||||
const old = previousSegments.get(segment.id);
|
};
|
||||||
const unchanged =
|
const old = previousSegments.get(segment.id);
|
||||||
old !== undefined &&
|
const unchanged =
|
||||||
(Object.keys(properties) as (keyof SegmentProperties)[]).every(
|
old !== undefined &&
|
||||||
(key) => old.properties[key] === properties[key]
|
(Object.keys(properties) as (keyof SegmentProperties)[]).every(
|
||||||
);
|
(key) => old.properties[key] === properties[key]
|
||||||
segments.push(
|
|
||||||
unchanged
|
|
||||||
? old
|
|
||||||
: backend.segment_coordinates(segment.id).then((coordinates) => ({
|
|
||||||
type: 'Feature',
|
|
||||||
geometry: {
|
|
||||||
type: 'LineString',
|
|
||||||
coordinates: toPositions(coordinates),
|
|
||||||
},
|
|
||||||
properties,
|
|
||||||
}))
|
|
||||||
);
|
);
|
||||||
});
|
segments.push(
|
||||||
|
unchanged
|
||||||
|
? old
|
||||||
|
: backend.segment_coordinates(segment.id).then((coordinates) => ({
|
||||||
|
type: 'Feature',
|
||||||
|
geometry: {
|
||||||
|
type: 'LineString',
|
||||||
|
coordinates: toPositions(coordinates),
|
||||||
|
},
|
||||||
|
properties,
|
||||||
|
}))
|
||||||
|
);
|
||||||
|
};
|
||||||
|
structure.routes.forEach((route) => addSegment(route.segment, route, { routeId: route.id }));
|
||||||
|
structure.tracks.forEach((track) => {
|
||||||
|
track.segments.forEach((segment) => addSegment(segment, track, { trackId: track.id }));
|
||||||
});
|
});
|
||||||
const segmentFeatures = await Promise.all(segments);
|
const segmentFeatures = await Promise.all(segments);
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ const structure: FileStructure = {
|
|||||||
name: 'file',
|
name: 'file',
|
||||||
waypointsRev: 'wpt-rev',
|
waypointsRev: 'wpt-rev',
|
||||||
waypoints: [{ id: 'W1' }, { id: 'W2' }],
|
waypoints: [{ id: 'W1' }, { id: 'W2' }],
|
||||||
|
routes: [],
|
||||||
tracks: [
|
tracks: [
|
||||||
{ id: 'T1', segments: [segment('S1'), segment('S2')] },
|
{ id: 'T1', segments: [segment('S1'), segment('S2')] },
|
||||||
{ id: 'T2', segments: [segment('S3')] },
|
{ id: 'T2', segments: [segment('S3')] },
|
||||||
@@ -134,6 +135,29 @@ describe('file visibility', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('hides a route, which is a leaf like a waypoint', () => {
|
||||||
|
const withRoute: FileStructure = {
|
||||||
|
...structure,
|
||||||
|
routes: [{ id: 'R', segment: segment('RS') }],
|
||||||
|
};
|
||||||
|
const state = (...actions: [ids: string[], hidden: boolean][]): VisibilityState => {
|
||||||
|
let visibility: Visibility = new Map();
|
||||||
|
for (const [ids, hidden] of actions) {
|
||||||
|
visibility = setHidden(withRoute, visibility, ids, hidden);
|
||||||
|
}
|
||||||
|
return { structure: withRoute, visibility };
|
||||||
|
};
|
||||||
|
assert.equal(isHidden(state([['R'], true]), 'R'), true);
|
||||||
|
assert.equal(isHidden(state([['R'], true]), 'F'), false);
|
||||||
|
assert.equal(isSegmentHidden(state([['R'], true]), 'R', 'RS'), true);
|
||||||
|
// with its file, and shown on its own below it
|
||||||
|
assert.equal(isHidden(state([['F'], true]), 'R'), true);
|
||||||
|
const shown = state([['F'], true], [['R'], false]);
|
||||||
|
assert.equal(isHidden(shown, 'R'), false);
|
||||||
|
assert.equal(isHidden(shown, 'F'), false);
|
||||||
|
assert.equal(isHidden(shown, 'T1'), true);
|
||||||
|
});
|
||||||
|
|
||||||
it('does not consider unknown ids hidden', () => {
|
it('does not consider unknown ids hidden', () => {
|
||||||
assert.equal(isHidden(apply([['F'], true]), 'unknown'), false);
|
assert.equal(isHidden(apply([['F'], true]), 'unknown'), false);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import type { FileStructure } from 'gpx-wasm';
|
import type { FileStructure } from 'gpx-wasm';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visibility the user set explicitly, by element id (file, track, segment, waypoint, or the
|
* Visibility the user set explicitly, by element id (file, route, track, segment, waypoint, or the
|
||||||
* waypoints of the file as a whole, see `waypointsKey`): `false` hides, `true` shows. An element
|
* waypoints of the file as a whole, see `waypointsKey`): `false` hides, `true` shows. An element
|
||||||
* without entry follows its parent, and everything is shown by default.
|
* without entry follows its parent, and everything is shown by default.
|
||||||
*/
|
*/
|
||||||
@@ -15,7 +15,10 @@ export function waypointsKey(fileId: string): string {
|
|||||||
return `${fileId}:waypoints`;
|
return `${fileId}:waypoints`;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Ids from the file down to `id` in the file tree (file, tracks, segments / waypoints group). */
|
/**
|
||||||
|
* Ids from the file down to `id` in the file tree (file, routes, tracks, segments / waypoints
|
||||||
|
* group).
|
||||||
|
*/
|
||||||
function pathTo(structure: FileStructure, id: string): string[] {
|
function pathTo(structure: FileStructure, id: string): string[] {
|
||||||
const fileId = structure.id;
|
const fileId = structure.id;
|
||||||
if (id === fileId) {
|
if (id === fileId) {
|
||||||
@@ -27,6 +30,9 @@ function pathTo(structure: FileStructure, id: string): string[] {
|
|||||||
if (structure.waypoints.some((waypoint) => waypoint.id === id)) {
|
if (structure.waypoints.some((waypoint) => waypoint.id === id)) {
|
||||||
return [fileId, waypointsKey(fileId), id];
|
return [fileId, waypointsKey(fileId), id];
|
||||||
}
|
}
|
||||||
|
if (structure.routes.some((route) => route.id === id)) {
|
||||||
|
return [fileId, id];
|
||||||
|
}
|
||||||
for (const track of structure.tracks) {
|
for (const track of structure.tracks) {
|
||||||
if (track.id === id) {
|
if (track.id === id) {
|
||||||
return [fileId, id];
|
return [fileId, id];
|
||||||
@@ -40,7 +46,11 @@ function pathTo(structure: FileStructure, id: string): string[] {
|
|||||||
|
|
||||||
function children(structure: FileStructure, id: string): string[] {
|
function children(structure: FileStructure, id: string): string[] {
|
||||||
if (id === structure.id) {
|
if (id === structure.id) {
|
||||||
return [waypointsKey(id), ...structure.tracks.map((track) => track.id)];
|
return [
|
||||||
|
waypointsKey(id),
|
||||||
|
...structure.routes.map((route) => route.id),
|
||||||
|
...structure.tracks.map((track) => track.id),
|
||||||
|
];
|
||||||
}
|
}
|
||||||
if (id === waypointsKey(structure.id)) {
|
if (id === waypointsKey(structure.id)) {
|
||||||
return structure.waypoints.map((waypoint) => waypoint.id);
|
return structure.waypoints.map((waypoint) => waypoint.id);
|
||||||
@@ -77,12 +87,13 @@ export function isHidden({ structure, visibility }: VisibilityState, id: string)
|
|||||||
|
|
||||||
// Leaf elements have no descendants: no need to look at the structure.
|
// Leaf elements have no descendants: no need to look at the structure.
|
||||||
|
|
||||||
|
/** Whether a segment is hidden: `parentId` is its route (which is a leaf), or its track. */
|
||||||
export function isSegmentHidden(
|
export function isSegmentHidden(
|
||||||
{ structure, visibility }: VisibilityState,
|
{ structure, visibility }: VisibilityState,
|
||||||
trackId: string,
|
parentId: string,
|
||||||
segmentId: string
|
segmentId: string
|
||||||
): boolean {
|
): boolean {
|
||||||
return followsHidden(visibility, [structure.id, trackId, segmentId]);
|
return followsHidden(visibility, [structure.id, parentId, segmentId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isWaypointHidden(
|
export function isWaypointHidden(
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
|
|||||||
import type { Clipboard, FileStructure, Selection } from 'gpx-wasm';
|
import type { Clipboard, FileStructure, Selection } from 'gpx-wasm';
|
||||||
import type { VisibilityState } from '$lib/engine/file-visibility';
|
import type { VisibilityState } from '$lib/engine/file-visibility';
|
||||||
import {
|
import {
|
||||||
|
coversSegments,
|
||||||
elementId,
|
elementId,
|
||||||
hasSelectionWithin,
|
hasSelectionWithin,
|
||||||
isCovered,
|
isCovered,
|
||||||
@@ -16,10 +17,12 @@ import {
|
|||||||
type FileTreeNode,
|
type FileTreeNode,
|
||||||
} from '$lib/engine/selection-helpers';
|
} from '$lib/engine/selection-helpers';
|
||||||
|
|
||||||
// Two files; F1 has the tracks T1 (segments S1, S2) and T2 (segment S3), and waypoints W1, W2.
|
// Two files; F1 has the route R1, the tracks T1 (segments S1, S2) and T2 (segment S3), and
|
||||||
|
// waypoints W1, W2.
|
||||||
const nodes: Record<string, FileTreeNode> = {
|
const nodes: Record<string, FileTreeNode> = {
|
||||||
F1: { type: 'file', fileId: 'F1' },
|
F1: { type: 'file', fileId: 'F1' },
|
||||||
F2: { type: 'file', fileId: 'F2' },
|
F2: { type: 'file', fileId: 'F2' },
|
||||||
|
R1: { type: 'route', fileId: 'F1', routeId: 'R1' },
|
||||||
T1: { type: 'track', fileId: 'F1', trackId: 'T1' },
|
T1: { type: 'track', fileId: 'F1', trackId: 'T1' },
|
||||||
T2: { type: 'track', fileId: 'F1', trackId: 'T2' },
|
T2: { type: 'track', fileId: 'F1', trackId: 'T2' },
|
||||||
S1: { type: 'segment', fileId: 'F1', trackId: 'T1', segmentId: 'S1' },
|
S1: { type: 'segment', fileId: 'F1', trackId: 'T1', segmentId: 'S1' },
|
||||||
@@ -34,6 +37,7 @@ const nodes: Record<string, FileTreeNode> = {
|
|||||||
|
|
||||||
const empty: Selection = { type: 'empty' };
|
const empty: Selection = { type: 'empty' };
|
||||||
const files = (...fileIds: string[]): Selection => ({ type: 'file', fileIds });
|
const files = (...fileIds: string[]): Selection => ({ type: 'file', fileIds });
|
||||||
|
const routes = (...routeIds: string[]): Selection => ({ type: 'route', fileId: 'F1', routeIds });
|
||||||
const tracks = (...trackIds: string[]): Selection => ({ type: 'track', fileId: 'F1', trackIds });
|
const tracks = (...trackIds: string[]): Selection => ({ type: 'track', fileId: 'F1', trackIds });
|
||||||
const segments = (trackId: string, ...segmentIds: string[]): Selection => ({
|
const segments = (trackId: string, ...segmentIds: string[]): Selection => ({
|
||||||
type: 'segment',
|
type: 'segment',
|
||||||
@@ -54,6 +58,15 @@ function matching(selection: Selection, predicate: typeof isSelected): string[]
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('selection helpers', () => {
|
describe('selection helpers', () => {
|
||||||
|
it('tells whether the selection is made of segments', () => {
|
||||||
|
for (const selection of [files('F1'), routes('R1'), tracks('T1'), segments('T1', 'S1')]) {
|
||||||
|
assert.equal(coversSegments(selection), true);
|
||||||
|
}
|
||||||
|
for (const selection of [empty, waypoints, waypoint('W1')]) {
|
||||||
|
assert.equal(coversSegments(selection), false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it('tells whether the selection is empty', () => {
|
it('tells whether the selection is empty', () => {
|
||||||
assert.equal(isEmpty(empty), true);
|
assert.equal(isEmpty(empty), true);
|
||||||
assert.equal(isEmpty(files('F1')), false);
|
assert.equal(isEmpty(files('F1')), false);
|
||||||
@@ -72,7 +85,20 @@ describe('selection helpers', () => {
|
|||||||
it('gives the id of the element of a node', () => {
|
it('gives the id of the element of a node', () => {
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
Object.keys(nodes).map((name) => elementId(nodes[name])),
|
Object.keys(nodes).map((name) => elementId(nodes[name])),
|
||||||
['F1', 'F2', 'T1', 'T2', 'S1', 'S2', 'S3', 'F1:waypoints', 'W1', 'W2', 'F2:waypoints']
|
[
|
||||||
|
'F1',
|
||||||
|
'F2',
|
||||||
|
'R1',
|
||||||
|
'T1',
|
||||||
|
'T2',
|
||||||
|
'S1',
|
||||||
|
'S2',
|
||||||
|
'S3',
|
||||||
|
'F1:waypoints',
|
||||||
|
'W1',
|
||||||
|
'W2',
|
||||||
|
'F2:waypoints',
|
||||||
|
]
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -90,6 +116,7 @@ describe('selection helpers', () => {
|
|||||||
Object.keys(nodes).filter((name) => isInClipboard(c, nodes[name]));
|
Object.keys(nodes).filter((name) => isInClipboard(c, nodes[name]));
|
||||||
assert.deepEqual(inClipboard(clipboard('files', ['F2'])), ['F2']);
|
assert.deepEqual(inClipboard(clipboard('files', ['F2'])), ['F2']);
|
||||||
assert.deepEqual(inClipboard(clipboard('tracks', ['T2', 'S1'])), ['T2']);
|
assert.deepEqual(inClipboard(clipboard('tracks', ['T2', 'S1'])), ['T2']);
|
||||||
|
assert.deepEqual(inClipboard(clipboard('routes', ['R1', 'T1'])), ['R1']);
|
||||||
assert.deepEqual(inClipboard(clipboard('segments', ['S1', 'S3'])), ['S1', 'S3']);
|
assert.deepEqual(inClipboard(clipboard('segments', ['S1', 'S3'])), ['S1', 'S3']);
|
||||||
assert.deepEqual(inClipboard(clipboard('waypoints', ['W2'])), ['W2']);
|
assert.deepEqual(inClipboard(clipboard('waypoints', ['W2'])), ['W2']);
|
||||||
// not mixed up with the other kinds of elements
|
// not mixed up with the other kinds of elements
|
||||||
@@ -99,6 +126,7 @@ describe('selection helpers', () => {
|
|||||||
it('counts the selected elements', () => {
|
it('counts the selected elements', () => {
|
||||||
assert.equal(selectionSize(empty), 0);
|
assert.equal(selectionSize(empty), 0);
|
||||||
assert.equal(selectionSize(files('F1', 'F2')), 2);
|
assert.equal(selectionSize(files('F1', 'F2')), 2);
|
||||||
|
assert.equal(selectionSize(routes('R1')), 1);
|
||||||
assert.equal(selectionSize(tracks('T1')), 1);
|
assert.equal(selectionSize(tracks('T1')), 1);
|
||||||
assert.equal(selectionSize(segments('T1', 'S1', 'S2')), 2);
|
assert.equal(selectionSize(segments('T1', 'S1', 'S2')), 2);
|
||||||
assert.equal(selectionSize(waypoints), 1);
|
assert.equal(selectionSize(waypoints), 1);
|
||||||
@@ -111,6 +139,7 @@ describe('selection helpers', () => {
|
|||||||
{ fileId: 'F1', ids: ['F1'] },
|
{ fileId: 'F1', ids: ['F1'] },
|
||||||
{ fileId: 'F2', ids: ['F2'] },
|
{ fileId: 'F2', ids: ['F2'] },
|
||||||
]);
|
]);
|
||||||
|
assert.deepEqual(selectedElementIds(routes('R1')), [{ fileId: 'F1', ids: ['R1'] }]);
|
||||||
assert.deepEqual(selectedElementIds(tracks('T1', 'T2')), [
|
assert.deepEqual(selectedElementIds(tracks('T1', 'T2')), [
|
||||||
{ fileId: 'F1', ids: ['T1', 'T2'] },
|
{ fileId: 'F1', ids: ['T1', 'T2'] },
|
||||||
]);
|
]);
|
||||||
@@ -123,6 +152,7 @@ describe('selection helpers', () => {
|
|||||||
it('matches the selected nodes only', () => {
|
it('matches the selected nodes only', () => {
|
||||||
assert.deepEqual(matching(empty, isSelected), []);
|
assert.deepEqual(matching(empty, isSelected), []);
|
||||||
assert.deepEqual(matching(files('F1', 'F2'), isSelected), ['F1', 'F2']);
|
assert.deepEqual(matching(files('F1', 'F2'), isSelected), ['F1', 'F2']);
|
||||||
|
assert.deepEqual(matching(routes('R1'), isSelected), ['R1']);
|
||||||
assert.deepEqual(matching(tracks('T1', 'T2'), isSelected), ['T1', 'T2']);
|
assert.deepEqual(matching(tracks('T1', 'T2'), isSelected), ['T1', 'T2']);
|
||||||
assert.deepEqual(matching(segments('T1', 'S1'), isSelected), ['S1']);
|
assert.deepEqual(matching(segments('T1', 'S1'), isSelected), ['S1']);
|
||||||
assert.deepEqual(matching(waypoints, isSelected), ['WPTS']);
|
assert.deepEqual(matching(waypoints, isSelected), ['WPTS']);
|
||||||
@@ -146,6 +176,7 @@ describe('selection helpers', () => {
|
|||||||
|
|
||||||
it('covers the segments of a selected track, not its siblings', () => {
|
it('covers the segments of a selected track, not its siblings', () => {
|
||||||
assert.deepEqual(matching(tracks('T1'), isCovered), ['T1', 'S1', 'S2']);
|
assert.deepEqual(matching(tracks('T1'), isCovered), ['T1', 'S1', 'S2']);
|
||||||
|
assert.deepEqual(matching(routes('R1'), isCovered), ['R1']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('covers the waypoints below the waypoints node', () => {
|
it('covers the waypoints below the waypoints node', () => {
|
||||||
@@ -163,6 +194,7 @@ describe('selection helpers', () => {
|
|||||||
it('looks at the descendants of a file, whatever their kind', () => {
|
it('looks at the descendants of a file, whatever their kind', () => {
|
||||||
for (const selection of [
|
for (const selection of [
|
||||||
files('F1'),
|
files('F1'),
|
||||||
|
routes('R1'),
|
||||||
tracks('T1'),
|
tracks('T1'),
|
||||||
segments('T1', 'S1'),
|
segments('T1', 'S1'),
|
||||||
waypoints,
|
waypoints,
|
||||||
@@ -174,6 +206,10 @@ describe('selection helpers', () => {
|
|||||||
assert.equal(hasSelectionWithin(empty, nodes.F1), false);
|
assert.equal(hasSelectionWithin(empty, nodes.F1), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('looks at a route, which has no node below it', () => {
|
||||||
|
assert.deepEqual(matching(routes('R1'), hasSelectionWithin), ['F1', 'R1']);
|
||||||
|
});
|
||||||
|
|
||||||
it('looks at the segments of a track', () => {
|
it('looks at the segments of a track', () => {
|
||||||
assert.deepEqual(matching(tracks('T1'), hasSelectionWithin), ['F1', 'T1']);
|
assert.deepEqual(matching(tracks('T1'), hasSelectionWithin), ['F1', 'T1']);
|
||||||
assert.deepEqual(matching(segments('T1', 'S2'), hasSelectionWithin), [
|
assert.deepEqual(matching(segments('T1', 'S2'), hasSelectionWithin), [
|
||||||
@@ -201,6 +237,7 @@ describe('selection helpers', () => {
|
|||||||
name: 'file',
|
name: 'file',
|
||||||
waypointsRev: 'rev',
|
waypointsRev: 'rev',
|
||||||
waypoints: [{ id: 'W1' }, { id: 'W2' }],
|
waypoints: [{ id: 'W1' }, { id: 'W2' }],
|
||||||
|
routes: [],
|
||||||
tracks: [
|
tracks: [
|
||||||
{ id: 'T1', segments: [segment('S1'), segment('S2')] },
|
{ id: 'T1', segments: [segment('S1'), segment('S2')] },
|
||||||
{ id: 'T2', segments: [segment('S3')] },
|
{ id: 'T2', segments: [segment('S3')] },
|
||||||
|
|||||||
@@ -1,16 +1,19 @@
|
|||||||
import type { Clipboard, Selection } from 'gpx-wasm';
|
import type { Clipboard, FileStructure, PathInfo, Selection } from 'gpx-wasm';
|
||||||
import { isHidden, waypointsKey, type VisibilityState } from '$lib/engine/file-visibility';
|
import { isHidden, waypointsKey, type VisibilityState } from '$lib/engine/file-visibility';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An element of the file tree, to ask questions about the selection:
|
* An element of the file tree, to ask questions about the selection:
|
||||||
*
|
*
|
||||||
* file ─┬─ track ─── segment
|
* file ─┬─ route
|
||||||
|
* ├─ track ─── segment
|
||||||
* └─ waypoints ─── waypoint
|
* └─ waypoints ─── waypoint
|
||||||
*
|
*
|
||||||
* `waypoints` is the node standing for all the waypoints of a file.
|
* A route is like a track of a single segment, which is not a node of its own. `waypoints` is the
|
||||||
|
* node standing for all the waypoints of a file.
|
||||||
*/
|
*/
|
||||||
export type FileTreeNode =
|
export type FileTreeNode =
|
||||||
| { type: 'file'; fileId: string }
|
| { type: 'file'; fileId: string }
|
||||||
|
| { type: 'route'; fileId: string; routeId: string }
|
||||||
| { type: 'track'; fileId: string; trackId: string }
|
| { type: 'track'; fileId: string; trackId: string }
|
||||||
| { type: 'segment'; fileId: string; trackId: string; segmentId: string }
|
| { type: 'segment'; fileId: string; trackId: string; segmentId: string }
|
||||||
| { type: 'waypoints'; fileId: string }
|
| { type: 'waypoints'; fileId: string }
|
||||||
@@ -24,6 +27,8 @@ export function elementId(node: FileTreeNode): string {
|
|||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'file':
|
case 'file':
|
||||||
return node.fileId;
|
return node.fileId;
|
||||||
|
case 'route':
|
||||||
|
return node.routeId;
|
||||||
case 'track':
|
case 'track':
|
||||||
return node.trackId;
|
return node.trackId;
|
||||||
case 'segment':
|
case 'segment':
|
||||||
@@ -35,6 +40,31 @@ export function elementId(node: FileTreeNode): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The name, description and style of a route or track node, `undefined` for the other nodes. */
|
||||||
|
export function pathInfo(structure: FileStructure, node: FileTreeNode): PathInfo | undefined {
|
||||||
|
switch (node.type) {
|
||||||
|
case 'route':
|
||||||
|
return structure.routes.find((route) => route.id === node.routeId);
|
||||||
|
case 'track':
|
||||||
|
return structure.tracks.find((track) => track.id === node.trackId);
|
||||||
|
default:
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether the selection is made of segments: files, routes, tracks or segments, not waypoints (the
|
||||||
|
* selection the tools that work on trackpoints work on).
|
||||||
|
*/
|
||||||
|
export function coversSegments(selection: Selection): boolean {
|
||||||
|
return (
|
||||||
|
selection.type === 'file' ||
|
||||||
|
selection.type === 'route' ||
|
||||||
|
selection.type === 'track' ||
|
||||||
|
selection.type === 'segment'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function isEmpty(selection: Selection): boolean {
|
export function isEmpty(selection: Selection): boolean {
|
||||||
return selection.type === 'empty';
|
return selection.type === 'empty';
|
||||||
}
|
}
|
||||||
@@ -59,6 +89,8 @@ export function isInClipboard(clipboard: Clipboard | undefined, node: FileTreeNo
|
|||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'file':
|
case 'file':
|
||||||
return clipboard.type === 'files' && clipboard.ids.includes(node.fileId);
|
return clipboard.type === 'files' && clipboard.ids.includes(node.fileId);
|
||||||
|
case 'route':
|
||||||
|
return clipboard.type === 'routes' && clipboard.ids.includes(node.routeId);
|
||||||
case 'track':
|
case 'track':
|
||||||
return clipboard.type === 'tracks' && clipboard.ids.includes(node.trackId);
|
return clipboard.type === 'tracks' && clipboard.ids.includes(node.trackId);
|
||||||
case 'segment':
|
case 'segment':
|
||||||
@@ -77,6 +109,8 @@ export function selectionSize(selection: Selection): number {
|
|||||||
return 0;
|
return 0;
|
||||||
case 'file':
|
case 'file':
|
||||||
return selection.fileIds.length;
|
return selection.fileIds.length;
|
||||||
|
case 'route':
|
||||||
|
return selection.routeIds.length;
|
||||||
case 'track':
|
case 'track':
|
||||||
return selection.trackIds.length;
|
return selection.trackIds.length;
|
||||||
case 'segment':
|
case 'segment':
|
||||||
@@ -89,8 +123,8 @@ export function selectionSize(selection: Selection): number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ids of the selected elements, by file, as used by the visibility (a file, its tracks, its
|
* The ids of the selected elements, by file, as used by the visibility (a file, its routes, its
|
||||||
* segments, its waypoints, or `waypointsKey` for the waypoints node).
|
* tracks, its segments, its waypoints, or `waypointsKey` for the waypoints node).
|
||||||
*/
|
*/
|
||||||
export function selectedElementIds(selection: Selection): { fileId: string; ids: string[] }[] {
|
export function selectedElementIds(selection: Selection): { fileId: string; ids: string[] }[] {
|
||||||
switch (selection.type) {
|
switch (selection.type) {
|
||||||
@@ -98,6 +132,8 @@ export function selectedElementIds(selection: Selection): { fileId: string; ids:
|
|||||||
return [];
|
return [];
|
||||||
case 'file':
|
case 'file':
|
||||||
return selection.fileIds.map((fileId) => ({ fileId, ids: [fileId] }));
|
return selection.fileIds.map((fileId) => ({ fileId, ids: [fileId] }));
|
||||||
|
case 'route':
|
||||||
|
return [{ fileId: selection.fileId, ids: selection.routeIds }];
|
||||||
case 'track':
|
case 'track':
|
||||||
return [{ fileId: selection.fileId, ids: selection.trackIds }];
|
return [{ fileId: selection.fileId, ids: selection.trackIds }];
|
||||||
case 'segment':
|
case 'segment':
|
||||||
@@ -114,6 +150,12 @@ export function isSelected(selection: Selection, node: FileTreeNode): boolean {
|
|||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'file':
|
case 'file':
|
||||||
return selection.type === 'file' && selection.fileIds.includes(node.fileId);
|
return selection.type === 'file' && selection.fileIds.includes(node.fileId);
|
||||||
|
case 'route':
|
||||||
|
return (
|
||||||
|
selection.type === 'route' &&
|
||||||
|
selection.fileId === node.fileId &&
|
||||||
|
selection.routeIds.includes(node.routeId)
|
||||||
|
);
|
||||||
case 'track':
|
case 'track':
|
||||||
return (
|
return (
|
||||||
selection.type === 'track' &&
|
selection.type === 'track' &&
|
||||||
@@ -143,6 +185,7 @@ function parent(node: FileTreeNode): FileTreeNode | undefined {
|
|||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case 'file':
|
case 'file':
|
||||||
return undefined;
|
return undefined;
|
||||||
|
case 'route':
|
||||||
case 'track':
|
case 'track':
|
||||||
case 'waypoints':
|
case 'waypoints':
|
||||||
return { type: 'file', fileId: node.fileId };
|
return { type: 'file', fileId: node.fileId };
|
||||||
@@ -180,6 +223,7 @@ export function hasSelectionWithin(selection: Selection, node: FileTreeNode): bo
|
|||||||
isSelected(selection, node) ||
|
isSelected(selection, node) ||
|
||||||
(selection.type === 'waypoint' && selection.fileId === node.fileId)
|
(selection.type === 'waypoint' && selection.fileId === node.fileId)
|
||||||
);
|
);
|
||||||
|
case 'route':
|
||||||
case 'segment':
|
case 'segment':
|
||||||
case 'waypoint':
|
case 'waypoint':
|
||||||
return isSelected(selection, node);
|
return isSelected(selection, node);
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ export type {
|
|||||||
FilesUpdate,
|
FilesUpdate,
|
||||||
Outcome,
|
Outcome,
|
||||||
Selection,
|
Selection,
|
||||||
|
PathInfo,
|
||||||
|
RouteNode,
|
||||||
TrackNode,
|
TrackNode,
|
||||||
SegmentNode,
|
SegmentNode,
|
||||||
WaypointNode,
|
WaypointNode,
|
||||||
@@ -28,8 +30,11 @@ export type {
|
|||||||
*/
|
*/
|
||||||
export type MergeType = 'connect' | 'group';
|
export type MergeType = 'connect' | 'group';
|
||||||
|
|
||||||
/** What a split cuts in two: the file, the track or the segment of the trackpoint. */
|
/**
|
||||||
export type SplitType = 'files' | 'tracks' | 'segments';
|
* What a split cuts in two: the file, the route, the track or the segment of the trackpoint. Only
|
||||||
|
* a trackpoint of a route cuts a route, and only one of a track cuts a track or a segment.
|
||||||
|
*/
|
||||||
|
export type SplitType = 'files' | 'routes' | 'tracks' | 'segments';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* How elements combine with the current selection: they `replace` it, are `add`ed to it, or are
|
* How elements combine with the current selection: they `replace` it, are `add`ed to it, or are
|
||||||
@@ -39,17 +44,17 @@ export type SplitType = 'files' | 'tracks' | 'segments';
|
|||||||
*/
|
*/
|
||||||
export type SelectMode = 'replace' | 'add' | 'toggle';
|
export type SelectMode = 'replace' | 'add' | 'toggle';
|
||||||
|
|
||||||
|
/** A segment of the map: the one of a route (`routeId`), or one of a track (`trackId`). */
|
||||||
export type SegmentProperties = {
|
export type SegmentProperties = {
|
||||||
fileId: string;
|
fileId: string;
|
||||||
trackId: string;
|
routeId?: string;
|
||||||
|
trackId?: string;
|
||||||
segmentId: string;
|
segmentId: string;
|
||||||
trackIndex: number;
|
|
||||||
segmentIndex: number;
|
|
||||||
/** Changes when the trackpoints of the segment change. */
|
/** Changes when the trackpoints of the segment change. */
|
||||||
rev: string;
|
rev: string;
|
||||||
/** Color of the track, or else the base color of the file. */
|
/** Color of the route or track, or else the base color of the file. */
|
||||||
color: string;
|
color: string;
|
||||||
/** Only when the track defines it. */
|
/** Only when the route or track defines it. */
|
||||||
opacity?: number;
|
opacity?: number;
|
||||||
width?: number;
|
width?: number;
|
||||||
};
|
};
|
||||||
@@ -76,11 +81,14 @@ export type NewWaypoint = {
|
|||||||
/** Everything the UI knows about one file. */
|
/** Everything the UI knows about one file. */
|
||||||
export type FileState = {
|
export type FileState = {
|
||||||
structure: FileStructure;
|
structure: FileStructure;
|
||||||
/** Color of the file: the one defined by its tracks if any, otherwise one from the palette. */
|
/**
|
||||||
|
* Color of the file: the one defined by its routes and tracks if any, otherwise one from the
|
||||||
|
* palette.
|
||||||
|
*/
|
||||||
color: string;
|
color: string;
|
||||||
/** Global statistics of the file. */
|
/** Global statistics of the file. */
|
||||||
statistics: GlobalStatistics;
|
statistics: GlobalStatistics;
|
||||||
/** One LineString feature per track segment, in file order. */
|
/** One LineString feature per segment (of the routes, then of the tracks), in file order. */
|
||||||
segments: FeatureCollection<LineString, SegmentProperties>;
|
segments: FeatureCollection<LineString, SegmentProperties>;
|
||||||
/** One Point feature per waypoint, in file order. */
|
/** One Point feature per waypoint, in file order. */
|
||||||
waypoints: FeatureCollection<Point, WaypointProperties>;
|
waypoints: FeatureCollection<Point, WaypointProperties>;
|
||||||
@@ -120,11 +128,6 @@ export type ExportOptions = {
|
|||||||
power: boolean;
|
power: boolean;
|
||||||
/** The OpenStreetMap data: surface, highway, SAC scale and MTB scale. */
|
/** The OpenStreetMap data: surface, highway, SAC scale and MTB scale. */
|
||||||
osm: boolean;
|
osm: boolean;
|
||||||
/**
|
|
||||||
* Writes the segments as routes instead of tracks: the anchors are the route points, and the
|
|
||||||
* trackpoints between them the points of their path (which only keep their position).
|
|
||||||
*/
|
|
||||||
asRoute: boolean;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const ALL_EXPORT_OPTIONS: ExportOptions = {
|
export const ALL_EXPORT_OPTIONS: ExportOptions = {
|
||||||
@@ -134,7 +137,6 @@ export const ALL_EXPORT_OPTIONS: ExportOptions = {
|
|||||||
atemp: true,
|
atemp: true,
|
||||||
power: true,
|
power: true,
|
||||||
osm: true,
|
osm: true,
|
||||||
asRoute: false,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
"new": "New",
|
"new": "New",
|
||||||
"new_file": "New file",
|
"new_file": "New file",
|
||||||
"new_track": "New track",
|
"new_track": "New track",
|
||||||
|
"new_route": "New route",
|
||||||
"new_segment": "New segment",
|
"new_segment": "New segment",
|
||||||
"open": "Open...",
|
"open": "Open...",
|
||||||
"open_folder": "Open folder...",
|
"open_folder": "Open folder...",
|
||||||
@@ -46,7 +47,6 @@
|
|||||||
"export": "Export...",
|
"export": "Export...",
|
||||||
"export_all": "Export all...",
|
"export_all": "Export all...",
|
||||||
"export_options": "Export options",
|
"export_options": "Export options",
|
||||||
"export_as_route": "Export as route",
|
|
||||||
"support_message": "The tool is free to use, but not free to run. Please consider supporting the website if you use it frequently. Thank you!",
|
"support_message": "The tool is free to use, but not free to run. Please consider supporting the website if you use it frequently. Thank you!",
|
||||||
"support_button": "Help keep the website free",
|
"support_button": "Help keep the website free",
|
||||||
"download_file": "Download file",
|
"download_file": "Download file",
|
||||||
@@ -462,6 +462,8 @@
|
|||||||
"gpx": {
|
"gpx": {
|
||||||
"file": "File",
|
"file": "File",
|
||||||
"files": "Files",
|
"files": "Files",
|
||||||
|
"route": "Route",
|
||||||
|
"routes": "Routes",
|
||||||
"track": "Track",
|
"track": "Track",
|
||||||
"tracks": "Tracks",
|
"tracks": "Tracks",
|
||||||
"segment": "Segment",
|
"segment": "Segment",
|
||||||
|
|||||||
Reference in new issue
Block a user