Microscopic Traffic Simulator
MainViewModel.cs
Go to the documentation of this file.
3 using System;
4 using System.IO;
5 using System.Windows.Input;
6 
7 namespace Microscopic_Traffic_Simulator.ViewModels
8 {
13  {
17  private IInteractions interactions;
18 
22  private ISettings settings;
23 
27  private Action<string> switchLanguage;
28 
32  public event EventHandler AnotherTopologyInitializedOrOpened;
33 
37  private ViewModelBase currentTopPanelViewModel;
41  public ViewModelBase CurrentTopPanelViewModel
42  {
43  get { return currentTopPanelViewModel; }
44  set { currentTopPanelViewModel = value; OnPropertyChanged("CurrentTopPanelViewModel"); }
45  }
46 
50  private CanvasViewModel canvasViewModel;
54  public CanvasViewModel CanvasViewModel { get { return canvasViewModel; } }
55 
59  private ConstructionViewModel constructionViewModel;
64  {
65  get { return constructionViewModel; }
66  }
67 
71  private SimulationControlViewModel simulationControlViewModel;
76  {
77  get { return simulationControlViewModel; }
78  }
79 
84  private string geometricTopologyName;
89  public string GeometricTopologyName
90  {
91  get { return geometricTopologyName; }
92  set { geometricTopologyName = value; OnPropertyChanged("GeometricTopologyName"); }
93  }
94 
98  private bool hasChangesSaved;
102  public bool HasChangesSaved
103  {
104  get { return hasChangesSaved; }
105  set { hasChangesSaved = value; OnPropertyChanged("HasChangesSaved"); }
106  }
107 
114  private bool HasChangesSavedOrIsNeverSavedAndEmpty
115  {
116  get
117  {
118  return hasChangesSaved ||
119  (string.IsNullOrEmpty(settings.LastGeometricTopologyPath) &&
120  (ConstructionViewModel.GeometricTopology == null ||
121  constructionViewModel.GeometricTopology.IsEmpty));
122  }
123  }
124 
128  private string uiCultureName;
132  public string UICultureName
133  {
134  get { return uiCultureName; }
135  set { uiCultureName = value; OnPropertyChanged("UICultureName"); }
136  }
137 
141  private ICommand newGeometricTopologyCommand;
145  public ICommand NewGeometricTopologyCommand
146  {
147  get
148  {
149  if (newGeometricTopologyCommand == null)
150  {
151  newGeometricTopologyCommand =
152  new ObservableRelayCommand(i => NewGeometricTopology((string)i));
153  }
154  return newGeometricTopologyCommand;
155  }
156  }
157 
161  private ICommand openGeometricTopologyCommand;
165  public ICommand OpenGeometricTopologyCommand
166  {
167  get
168  {
169  if (openGeometricTopologyCommand == null)
170  {
171  openGeometricTopologyCommand =
172  new ObservableRelayCommand(i => ChooseGeometricTopologyAndOpenIt());
173  }
174  return openGeometricTopologyCommand;
175  }
176  }
177 
181  private ICommand saveGeometricTopologyAsCommand;
185  public ICommand SaveGeometricTopologyAsCommand
186  {
187  get
188  {
189  if (saveGeometricTopologyAsCommand == null)
190  {
191  saveGeometricTopologyAsCommand = new RelayCommand(
192  i => ChooseGeometricTopologyAndSaveItAs());
193  }
194  return saveGeometricTopologyAsCommand;
195  }
196  }
197 
201  private ICommand saveGeometricTopologyCommand;
205  public ICommand SaveGeometricTopologyCommand
206  {
207  get
208  {
209  if (saveGeometricTopologyCommand == null)
210  {
211  saveGeometricTopologyCommand = new RelayCommand(
212  i => ChooseGeometricTopologyAndSaveIt(), i => !hasChangesSaved);
213  }
214  return saveGeometricTopologyCommand;
215  }
216  }
217 
221  private ICommand switchLanguageCommand = null;
225  public ICommand SwitchLanguageCommand
226  {
227  get
228  {
229  if (switchLanguageCommand == null)
230  {
231  switchLanguageCommand = new RelayCommand(
232  uiCultureName => SwitchLanguage((string)uiCultureName));
233  }
234  return switchLanguageCommand;
235  }
236  }
237 
244  public MainViewModel(IInteractions interactions, ISettings settings, Action<string> switchLanguage)
245  {
246  messenger = new Messenger();
247  canvasViewModel = new CanvasViewModel(messenger);
248  constructionViewModel = new ConstructionViewModel(messenger);
249  simulationControlViewModel = new SimulationControlViewModel(messenger, interactions, settings);
250  currentTopPanelViewModel = constructionViewModel;
251 
252  messenger.GetEvent<ConstructionViewModel.BuildCellularTopologyMessage>().Subscribe(
253  (geometricTopology, parameters) => CurrentTopPanelViewModel = simulationControlViewModel);
254  messenger.GetEvent<SimulationControlViewModel.SwitchToConstructionModeMessage>().Subscribe(
255  () => CurrentTopPanelViewModel = constructionViewModel);
256  messenger.GetEvent<ConstructionViewModel.GeometricTopologyModifiedMessage>().Subscribe(
257  () => HasChangesSaved = false);
258 
259  this.interactions = interactions;
260  this.settings = settings;
261  this.switchLanguage = switchLanguage;
262  }
263 
270  public void Initialize(string newTopologyName, string languageOnStartup)
271  {
272  UICultureName = languageOnStartup;
273  if (string.IsNullOrEmpty(settings.LastGeometricTopologyPath) ||
274  !File.Exists(settings.LastGeometricTopologyPath))
275  {
276  NewGeometricTopology(newTopologyName);
277  }
278  else
279  {
280  try
281  {
282  OpenGeometricTopology(settings.LastGeometricTopologyPath);
283  }
284  catch
285  {
286  settings.LastGeometricTopologyPath = string.Empty;
287  NewGeometricTopology(newTopologyName);
288  }
289  }
290  }
291 
296  internal void NewGeometricTopology(string newTopologyName)
297  {
298  //check whether not to be saved the potential currently opened topology.
299  if (!TestIfCurrentTopologyChangesAreSavedAndAskForAction())
300  return;
301 
302  GeometricTopologyName = newTopologyName;
303  settings.LastGeometricTopologyPath = null;
304  settings.Save();
305  constructionViewModel.CreateNewGeometricTopology();
306  HasChangesSaved = false;
307  OnAnotherTopologyCreatedOrOpened();
308  CurrentTopPanelViewModel = constructionViewModel;
309  }
310 
315  private void OpenGeometricTopology(string path)
316  {
317  constructionViewModel.OpenGeometricTopology(path);
318  GeometricTopologyName = Path.GetFileNameWithoutExtension(path);
319  HasChangesSaved = true;
320  settings.LastGeometricTopologyPath = path;
321  settings.Save();
322  OnAnotherTopologyCreatedOrOpened();
323  CurrentTopPanelViewModel = constructionViewModel;
324  }
325 
330  private void SaveGeometricTopology(string path)
331  {
332  constructionViewModel.SaveGeometricTopology(path);
333  GeometricTopologyName = Path.GetFileNameWithoutExtension(path);
334  HasChangesSaved = true;
335  settings.LastGeometricTopologyPath = path;
336  settings.Save();
337  }
338 
342  private void ChooseGeometricTopologyAndOpenIt()
343  {
344  if (!TestIfCurrentTopologyChangesAreSavedAndAskForAction())
345  return;
346 
347  string pathToOpeningTopologyFile = interactions.GetPathToOpenTopologyFile();
348  if (pathToOpeningTopologyFile == null)
349  return;
350  try
351  {
352  OpenGeometricTopology(pathToOpeningTopologyFile);
353  }
354  catch (Exception ex)
355  {
356  interactions.ScreamErrorMessage(ex.Message);
357  }
358  }
359 
364  private void ChooseGeometricTopologyAndSaveIt()
365  {
366  if (settings.LastGeometricTopologyPath == null)
367  ChooseGeometricTopologyAndSaveItAs();
368  else
369  {
370  try
371  {
372  SaveGeometricTopology(settings.LastGeometricTopologyPath);
373  }
374  catch (Exception ex)
375  {
376  interactions.ScreamErrorMessage(ex.Message);
377  }
378  }
379  }
380 
385  private void ChooseGeometricTopologyAndSaveItAs()
386  {
387  string pathToSaveTopologyFile = interactions.GetPathToSaveTopologyFile();
388  if (pathToSaveTopologyFile == null)
389  return;
390  try
391  {
392  SaveGeometricTopology(pathToSaveTopologyFile);
393  }
394  catch (Exception ex)
395  {
396  interactions.ScreamErrorMessage(ex.Message);
397  }
398  }
399 
404  private bool TestIfCurrentTopologyChangesAreSavedAndAskForAction()
405  {
406  if (!HasChangesSavedOrIsNeverSavedAndEmpty)
407  {
408  bool? result = interactions.SaveChangesYesNoCancel();
409 
410  if (result == null)
411  return false;
412  else if (result.Value)
413  {
414  string savePath;
415  if (settings.LastGeometricTopologyPath == null)
416  {
417  savePath = interactions.GetPathToSaveTopologyFile();
418  if (savePath == null)
419  return false;
420  }
421  else
422  savePath = settings.LastGeometricTopologyPath;
423  try
424  {
425  SaveGeometricTopology(savePath);
426  }
427  catch (Exception ex)
428  {
429  interactions.ScreamErrorMessage(ex.Message);
430  return false;
431  }
432  }
433  }
434  return true;
435  }
436 
441  private void SwitchLanguage(string uiCultureName)
442  {
443  switchLanguage(uiCultureName);
444  UICultureName = uiCultureName;
445  }
446 
451  internal bool CanBeClosed()
452  {
453  return TestIfCurrentTopologyChangesAreSavedAndAskForAction();
454  }
455 
459  private void OnAnotherTopologyCreatedOrOpened()
460  {
461  if (CurrentTopPanelViewModel == SimulationControlViewModel &&
462  SimulationControlViewModel.CellularTopology.Simulation.SimulationState != SimulationState.NotRunning)
463  {
464  SimulationControlViewModel.CellularTopology.GeneratorsManager.DetachFromGeneratorEvents();
465  }
466  if (AnotherTopologyInitializedOrOpened != null)
467  AnotherTopologyInitializedOrOpened(this, EventArgs.Empty);
468  }
469 
473  internal ParametersViewModel GetParametersViewModel()
474  {
475  ParametersViewModel parametersViewModel = new ParametersViewModel(messenger, settings, interactions);
476  parametersViewModel.Initialize();
477  return parametersViewModel;
478  }
479  }
480 }
void Initialize(string newTopologyName, string languageOnStartup)
Creates new or open last opened geometric topology.
void ScreamErrorMessage(string errorMessage)
Screaming error message.
Interface of application settings.
Definition: ISettings.cs:6
string LastGeometricTopologyPath
Item for path to file where is the last opened topology.
Definition: ISettings.cs:11
Class representing messenger for communicating between viewmodels.
Definition: Messenger.cs:9
EventHandler AnotherTopologyInitializedOrOpened
Event for initialization or opening of new topology.
Command class for binding UI commands with view models.
Definition: RelayCommand.cs:10
Message for sending information about modification of geometric topology.
Interface for interactions with user.
Definition: IInteractions.cs:6
void Initialize()
Initialize parameters view model by getting last used or default parameters.
bool SaveChangesYesNoCancel()
Asking whether to save changes or not or if to interrupt the starting action.
MainViewModel(IInteractions interactions, ISettings settings, Action< string > switchLanguage)
Initialization of main viewmodel.
Class representing view model of parameters view.
string GetPathToOpenTopologyFile()
Asking which topology file to open for loading.
Message for sending geometric topology simulation control viewmodel to build cellular topology from i...
Extended relay command class by adding event after executing command. Useful for performing additiona...
void Save()
Saves application settings.
string GetPathToSaveTopologyFile()
Asking which topology file the data to be saved into.