aboutsummaryrefslogtreecommitdiff
path: root/src/preferences/models.rs
blob: a595fc2c1ed1171870672336b4596bd58bfc2ca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Map, wardley map editor for linux
// Copyright (C) 2026 Rubén Beltrán del Río

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

use crate::tr;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Stage labels for custom stages
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct Stage {
    pub i: String,
    pub ii: String,
    pub iii: String,
    pub iv: String,
}

/// Custom stage definition
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct CustomStage {
    pub id: Uuid,
    pub name: String,
    pub stage: Stage,
}

impl Default for CustomStage {
    fn default() -> Self {
        Self {
            id: Uuid::new_v4(),
            name: "Custom Stage".to_string(),
            stage: Stage::default(),
        }
    }
}

impl CustomStage {
    pub fn cynefin_default() -> Self {
        Self {
            id: Uuid::new_v4(),
            name: tr!("stages.cynefin.name"),
            stage: Stage {
                i: tr!("stages.cynefin.i"),
                ii: tr!("stages.cynefin.ii"),
                iii: tr!("stages.cynefin.iii"),
                iv: tr!("stages.cynefin.iv"),
            },
        }
    }

    pub fn placeholder_default() -> Self {
        Self {
            id: Uuid::new_v4(),
            name: tr!("stages.new.name"),
            stage: Stage {
                i: tr!("stages.evolution_stage.i"),
                ii: tr!("stages.evolution_stage.ii"),
                iii: tr!("stages.evolution_stage.iii"),
                iv: tr!("stages.evolution_stage.iv"),
            },
        }
    }
}

/// Template for new maps
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Template {
    pub id: Uuid,
    pub name: String,
    pub content: String,
    #[serde(default)]
    pub is_default: bool,
}

impl Default for Template {
    fn default() -> Self {
        Self {
            id: Uuid::new_v4(),
            name: "Untitled".to_string(),
            content: String::new(),
            is_default: false,
        }
    }
}

impl Template {
    pub fn new(name: String, content: String, is_default: bool) -> Self {
        Self {
            id: Uuid::new_v4(),
            name,
            content,
            is_default,
        }
    }
}

/// JSON import/export format
/// All fields optional to support partial imports
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct UserPreferencesJson {
    // Map preferences
    pub show_map_background: Option<bool>,
    pub use_custom_font: Option<bool>,
    pub custom_font_name: Option<String>,
    pub default_export_format: Option<String>,
    pub use_smart_label_positioning: Option<bool>,

    // Editor preferences
    pub use_custom_editor_font: Option<bool>,
    pub custom_editor_font_name: Option<String>,
    pub editor_font_size: Option<f64>,
    pub soft_wrap_lines: Option<bool>,

    // Templates and stages
    pub map_templates: Option<Vec<Template>>,
    pub custom_stages: Option<Vec<CustomStage>>,

    // UI preferences
    pub view_style: Option<String>,
    pub zoom: Option<f64>,
}

/// Main preferences struct stored in app
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[allow(clippy::struct_excessive_bools)] // These are independent boolean preferences, not a state machine
pub struct UserPreferences {
    // Map preferences
    pub show_map_background: bool,
    pub use_custom_font: bool,
    pub custom_font_name: String,
    pub default_export_format: String,
    pub use_smart_label_positioning: bool,

    // Editor preferences
    pub use_custom_editor_font: bool,
    pub custom_editor_font_name: String,
    pub editor_font_size: f64,
    pub soft_wrap_lines: bool,

    // Templates and stages
    pub map_templates: Vec<Template>,
    pub custom_stages: Vec<CustomStage>,

    // UI preferences
    pub view_style: String,
    pub zoom: f64,
}

impl Default for UserPreferences {
    fn default() -> Self {
        Self {
            show_map_background: true,
            use_custom_font: false,
            custom_font_name: "Sans".to_string(),
            default_export_format: "png".to_string(),
            use_smart_label_positioning: true,

            use_custom_editor_font: false,
            custom_editor_font_name: "Monospace".to_string(),
            editor_font_size: 14.0,
            soft_wrap_lines: false,

            map_templates: vec![
                Template::new(
                    tr!("templates.defaults.example.title"),
                    tr!("templates.defaults.example.source"),
                    true,
                ),
                Template::new(tr!("templates.defaults.empty.title"), String::new(), false),
            ],
            custom_stages: vec![CustomStage::cynefin_default()],

            view_style: "horizontal".to_string(),
            zoom: 1.0,
        }
    }
}

impl UserPreferences {
    /// Convert to JSON export format
    pub fn to_json(&self) -> UserPreferencesJson {
        UserPreferencesJson {
            show_map_background: Some(self.show_map_background),
            use_custom_font: Some(self.use_custom_font),
            custom_font_name: Some(self.custom_font_name.clone()),
            default_export_format: Some(self.default_export_format.clone()),
            use_smart_label_positioning: Some(self.use_smart_label_positioning),
            use_custom_editor_font: Some(self.use_custom_editor_font),
            custom_editor_font_name: Some(self.custom_editor_font_name.clone()),
            editor_font_size: Some(self.editor_font_size),
            soft_wrap_lines: Some(self.soft_wrap_lines),
            map_templates: Some(self.map_templates.clone()),
            custom_stages: Some(self.custom_stages.clone()),
            view_style: Some(self.view_style.clone()),
            zoom: Some(self.zoom),
        }
    }

    /// Update from JSON import (partial updates supported)
    pub fn update_from_json(&mut self, json: &UserPreferencesJson) {
        if let Some(v) = json.show_map_background {
            self.show_map_background = v;
        }
        if let Some(ref v) = json.use_custom_font {
            self.use_custom_font = *v;
        }
        if let Some(ref v) = json.custom_font_name {
            self.custom_font_name.clone_from(v);
        }
        if let Some(ref v) = json.default_export_format {
            self.default_export_format.clone_from(v);
        }
        if let Some(v) = json.use_smart_label_positioning {
            self.use_smart_label_positioning = v;
        }
        if let Some(ref v) = json.use_custom_editor_font {
            self.use_custom_editor_font = *v;
        }
        if let Some(ref v) = json.custom_editor_font_name {
            self.custom_editor_font_name.clone_from(v);
        }
        if let Some(v) = json.editor_font_size {
            self.editor_font_size = v;
        }
        if let Some(v) = json.soft_wrap_lines {
            self.soft_wrap_lines = v;
        }
        if let Some(ref v) = json.map_templates {
            self.map_templates.clone_from(v);
        }
        if let Some(ref v) = json.custom_stages {
            self.custom_stages.clone_from(v);
        }
        if let Some(ref v) = json.view_style {
            self.view_style.clone_from(v);
        }
        if let Some(v) = json.zoom {
            self.zoom = v;
        }
    }

    /// Get the default template
    #[allow(dead_code)]
    pub fn default_template(&self) -> Option<&Template> {
        self.map_templates.iter().find(|t| t.is_default)
    }

    /// Set a template as default (unsets others)
    pub fn set_default_template(&mut self, id: Uuid) {
        for template in &mut self.map_templates {
            template.is_default = template.id == id;
        }
    }
}