public RouteFileFormat(string fileFilter, IRouteFileImporter importer) : base(fileFilter) { this.importer = importer; }
private DialogResult Import() { // validate file names if (imageSourceType != SourceType.Url && mapImageFileName.Text != "" && !File.Exists(mapImageFileName.Text)) { MessageBox.Show(Strings.MapImageFileDoesNotExist, Strings.InvalidMapImage, MessageBoxButtons.OK, MessageBoxIcon.Error); return(DialogResult.Cancel); } if (routeFromFile.Checked && !File.Exists(routeFileName.Text)) { MessageBox.Show(Strings.RouteFileDoesNotExist, Strings.InvalidRoute, MessageBoxButtons.OK, MessageBoxIcon.Error); return(DialogResult.Cancel); } IRouteImporter routeImporter = null; if (routeFromFile.Checked) { IRouteFileImporter routeFileImporter = ((RouteFileFormat)routeFileFormatComboBox.SelectedItem).Importer; routeFileImporter.FileName = routeFileName.Text; routeImporter = routeFileImporter; } else if (routeFromGpsDevice.Checked) { GPSDevice gpsDevice = routeGpsDevice.SelectedItem as GPSDevice; if (gpsDevice == null) { MessageBox.Show(Strings.NoGPSDevicesConnectedMessageBox, Strings.InvalidRoute, MessageBoxButtons.OK, MessageBoxIcon.Error); return(DialogResult.Cancel); } routeImporter = gpsDevice.Importer; } routeImporter.BeginWork += routeImporter_BeginWork; routeImporter.WorkProgress += routeImporter_WorkProgress; routeImporter.EndWork += routeImporter_EndWork; DialogResult result; try { result = routeImporter.ShowPreImportDialogs(); } catch (Exception ex) { Cursor = Cursors.Default; Util.ShowExceptionMessageBox(ex, Strings.InvalidRoute); return(DialogResult.Cancel); } if (result == DialogResult.OK) { try { routeImporter.Import(); } catch (Exception ex) { routeImporter.ImportResult.Succeeded = false; routeImporter.ImportResult.Error = ImportError.Unknown; routeImporter.ImportResult.ErrorMessage = ex.Message; routeImporter.ImportResult.Exception = ex; } if (!routeImporter.ImportResult.Succeeded) { // an error occured, show relevant error info and cancel creation of new document. switch (routeImporter.ImportResult.Error) { case ImportError.NoWaypoints: routeImporter.ImportResult.ErrorMessage = Strings.RouteImportError_NoWaypoints; break; case ImportError.NoWaypointTimes: routeImporter.ImportResult.ErrorMessage = Strings.RouteImportError_NoWaypointTimes; break; } Cursor = Cursors.Default; if (routeImporter.ImportResult.Exception != null) { Util.ShowExceptionMessageBox(routeImporter.ImportResult.Exception, Strings.InvalidRoute); } else { MessageBox.Show(routeImporter.ImportResult.ErrorMessage, Strings.InvalidRoute, MessageBoxButtons.OK, MessageBoxIcon.Error); } return(DialogResult.Cancel); } try { if (string.IsNullOrEmpty(mapImageFileName.Text)) { var blankMap = new Bitmap(1500, 1500); using (Graphics g = Graphics.FromImage(blankMap)) { g.Clear(Color.White); } Map = new Map(blankMap); } else if (!ImageHasBeenTransformed) { CreateMapAndSetInitialTransformations(); } else { // don't care about transformations embedded in jpg, qrt or kmz files since the original file has been transformed Map = new Map(new Bitmap(transformedImage)); } } catch (Exception ex) { Cursor = Cursors.Default; Util.ShowExceptionMessageBox(ex, Strings.InvalidMapImage); return(DialogResult.Cancel); } ImportResult = routeImporter.ImportResult; if (!string.IsNullOrEmpty(mapImageFileName.Text)) { Util.ApplicationSettings.AddRecentMapImageFileName(mapImageFileName.Text); } if (routeFromFile.Checked) { Util.ApplicationSettings.AddRecentRouteFileName(routeFileName.Text); } } return(result); }
private void Import() { if (routeFromFile.Checked && !File.Exists(routeFileName.Text)) { MessageBox.Show(Strings.RouteFileDoesNotExist, Strings.InvalidRoute, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } IRouteImporter routeImporter = null; if (routeFromFile.Checked) { IRouteFileImporter routeFileImporter = ((RouteFileFormat)routeFileFormatComboBox.SelectedItem).Importer; routeFileImporter.FileName = routeFileName.Text; routeImporter = routeFileImporter; } else if (routeFromGpsDevice.Checked) { GPSDevice gpsDevice = routeGpsDevice.SelectedItem as GPSDevice; if (gpsDevice == null) { MessageBox.Show(Strings.NoGPSDevicesConnectedMessageBox, Strings.InvalidRoute, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } routeImporter = gpsDevice.Importer; } routeImporter.BeginWork += routeImporter_BeginWork; routeImporter.WorkProgress += routeImporter_WorkProgress; routeImporter.EndWork += routeImporter_EndWork; if (routeImporter.ShowPreImportDialogs() == DialogResult.OK) { try { routeImporter.Import(); } catch (Exception ex) { routeImporter.ImportResult.Succeeded = false; routeImporter.ImportResult.Error = ImportError.Unknown; routeImporter.ImportResult.ErrorMessage = ex.Message; routeImporter.ImportResult.Exception = ex; } if (!routeImporter.ImportResult.Succeeded) { // an error occured, show relevant error info and cancel creation of new session. switch (routeImporter.ImportResult.Error) { case ImportError.NoWaypoints: routeImporter.ImportResult.ErrorMessage = Strings.RouteImportError_NoWaypoints; break; case ImportError.NoWaypointTimes: routeImporter.ImportResult.ErrorMessage = Strings.RouteImportError_NoWaypointTimes; break; } Cursor = Cursors.Default; if (routeImporter.ImportResult.Exception != null) { Util.ShowExceptionMessageBox(routeImporter.ImportResult.Exception, Strings.InvalidRoute); } else { MessageBox.Show(routeImporter.ImportResult.ErrorMessage, Strings.InvalidRoute, MessageBoxButtons.OK, MessageBoxIcon.Error); } return; } // add new session to session collection var monochromeColors = new Color[] { Color.Red, Color.Blue, Color.DarkGreen, Color.DarkOrange, Color.DarkGray }; SessionSettings ss = Document.Settings.DefaultSessionSettings.Copy(); ss.RouteLineSettingsCollection[WaypointAttribute.Pace].MonochromeColor = Color.FromArgb(160, monochromeColors[ Sessions.Count % monochromeColors. Length]); ss.RouteLineSettingsCollection[WaypointAttribute.Pace].MonochromeWidth = 3; Session s = new Session( routeImporter.ImportResult.Route, routeImporter.ImportResult.Laps, Document.Map.Image.Size, Document.Sessions.CalculateAverageTransformation().TransformationMatrix, Document.ProjectionOrigin, ss); s.CreateAdjustedRoute(); Sessions.Add(s); // update session grid sessionGrid.DataSource = CreateBindingSource(Sessions); } }
private DialogResult Import() { // validate file names if (mapImageFromFile.Checked && !File.Exists(mapImageFileName.Text)) { MessageBox.Show(Strings.MapImageFileDoesNotExist, Strings.InvalidMapImage, MessageBoxButtons.OK, MessageBoxIcon.Error); return(DialogResult.Cancel); } if (routeFromFile.Checked && !File.Exists(routeFileName.Text)) { MessageBox.Show(Strings.RouteFileDoesNotExist, Strings.InvalidRoute, MessageBoxButtons.OK, MessageBoxIcon.Error); return(DialogResult.Cancel); } IRouteImporter routeImporter = null; if (routeFromFile.Checked) { IRouteFileImporter routeFileImporter = ((RouteFileFormat)routeFileFormatComboBox.SelectedItem).Importer; routeFileImporter.FileName = routeFileName.Text; routeImporter = routeFileImporter; } else if (routeFromGpsDevice.Checked) { GPSDevice gpsDevice = routeGpsDevice.SelectedItem as GPSDevice; if (gpsDevice == null) { MessageBox.Show(Strings.NoGPSDevicesConnectedMessageBox, Strings.InvalidRoute, MessageBoxButtons.OK, MessageBoxIcon.Error); return(DialogResult.Cancel); } routeImporter = gpsDevice.Importer; } routeImporter.BeginWork += routeImporter_BeginWork; routeImporter.WorkProgress += routeImporter_WorkProgress; routeImporter.EndWork += routeImporter_EndWork; DialogResult result; try { result = routeImporter.ShowPreImportDialogs(); } catch (Exception ex) { Cursor = Cursors.Default; Util.ShowExceptionMessageBox(ex, Strings.InvalidRoute); return(DialogResult.Cancel); } if (result == DialogResult.OK) { try { routeImporter.Import(); } catch (Exception ex) { routeImporter.ImportResult.Succeeded = false; routeImporter.ImportResult.Error = ImportError.Unknown; routeImporter.ImportResult.ErrorMessage = ex.Message; routeImporter.ImportResult.Exception = ex; } if (!routeImporter.ImportResult.Succeeded) { // an error occured, show relevant error info and cancel creation of new document. switch (routeImporter.ImportResult.Error) { case ImportError.NoWaypoints: routeImporter.ImportResult.ErrorMessage = Strings.RouteImportError_NoWaypoints; break; case ImportError.NoWaypointTimes: routeImporter.ImportResult.ErrorMessage = Strings.RouteImportError_NoWaypointTimes; break; } Cursor = Cursors.Default; if (routeImporter.ImportResult.Exception != null) { Util.ShowExceptionMessageBox(routeImporter.ImportResult.Exception, Strings.InvalidRoute); } else { MessageBox.Show(routeImporter.ImportResult.ErrorMessage, Strings.InvalidRoute, MessageBoxButtons.OK, MessageBoxIcon.Error); } return(DialogResult.Cancel); } try { if (mapImageFromFile.Checked) { switch (mapImageFileFormatComboBox.SelectedIndex) { case 0: // map image from image file Map = new Map(mapImageFileName.Text, MapSourceType.FileSystem, MapStorageType.Inline); // is it a QuickRoute image? if yes, use embedded transformation matrix var ed = QuickRouteJpegExtensionData.FromJpegFile(mapImageFileName.Text); if (ed != null && ed.Sessions != null && ed.Sessions.Count > 0) { InitialTransformation = ed.Sessions.CalculateAverageTransformation(); } break; case 1: // map image from QuickRoute file Document d = Document.OpenFromQrt(mapImageFileName.Text); if (d != null) { Map = d.Map; InitialTransformation = d.Sessions.CalculateAverageTransformation(); } break; case 2: // map image from kmz file var kmz = new KmzDocument(mapImageFileName.Text); if (kmz.ImageStream != null) { Map = new Map(kmz.ImageStream); InitialTransformation = kmz.Transformation; } break; } } else if (mapImageBlank.Checked) { var blankMap = new Bitmap(1500, 1500); using (Graphics g = Graphics.FromImage(blankMap)) { g.Clear(Color.White); } Map = new Map(blankMap); } else if (mapImageFromUrl.Checked) { Map = new Map(mapImageUrl.Text, MapSourceType.Url, MapStorageType.Inline); } } catch (Exception ex) { Cursor = Cursors.Default; Util.ShowExceptionMessageBox(ex, Strings.InvalidMapImage); return(DialogResult.Cancel); } ImportResult = routeImporter.ImportResult; if (mapImageFromFile.Checked) { Util.ApplicationSettings.AddRecentMapImageFileName(mapImageFileName.Text); } if (routeFromFile.Checked) { Util.ApplicationSettings.AddRecentRouteFileName(routeFileName.Text); } } return(result); }