Beispiel #1
0
        public DialogImport(string file, CircuitProject target)
        {
            this.FileName    = file;
            this.DataContext = this;
            this.InitializeComponent();
            Mainframe mainframe = App.Mainframe;
            Thread    thread    = new Thread(new ThreadStart(() => {
                try {
                    CircuitProject import   = CircuitProject.Create(file);
                    List <CircuitInfo> list = new List <CircuitInfo>();
                    foreach (LogicalCircuit circuit in import.LogicalCircuitSet)
                    {
                        list.Add(new CircuitInfo(circuit, target.LogicalCircuitSet.FindByLogicalCircuitId(circuit.LogicalCircuitId) == null));
                    }
                    list.Sort(CircuitDescriptorComparer.Comparer);
                    this.List = list;
                    this.NotifyPropertyChanged("List");
                } catch (SnapStoreException snapStoreException) {
                    Tracer.Report("DialogImport.Load", snapStoreException);
                    mainframe.ErrorMessage(Properties.Resources.ErrorFileCorrupted(file), snapStoreException);
                    mainframe.Dispatcher.BeginInvoke(new Action(() => { this.Close(); }));
                } catch (Exception exception) {
                    Tracer.Report("DialogImport.Load", exception);
                    mainframe.ReportException(exception);
                    mainframe.Dispatcher.BeginInvoke(new Action(() => { this.Close(); }));
                }
            }));

            //TextNote validation will instantiate FlowDocument that in some cases required to happened only on STA.
            thread.SetApartmentState(ApartmentState.STA);
            thread.IsBackground = true;
            thread.Name         = "ImportLoader";
            thread.Priority     = ThreadPriority.AboveNormal;
            thread.Start();
        }
 private void ButtonLoadClick(object sender, RoutedEventArgs e)
 {
     try {
         OpenFileDialog dialog = new OpenFileDialog();
         dialog.InitialDirectory = Mainframe.IsDirectoryPathValid(this.openFileFolder.Value) ? this.openFileFolder.Value : Mainframe.DefaultProjectFolder();
         bool?result = dialog.ShowDialog(this);
         if (result.HasValue && result.Value)
         {
             this.openFileFolder.Value = Path.GetDirectoryName(dialog.FileName);
             int    addressBitWidth = this.AddressBitWidth;
             int    dataBitWidth    = this.DataBitWidth;
             byte[] buffer          = new byte[Memory.BytesPerCellFor(dataBitWidth)];
             int    cellCount       = Memory.NumberCellsFor(addressBitWidth);
             Tracer.Assert(cellCount * buffer.Length == this.data.Length);
             using (FileStream stream = File.Open(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                 for (int i = 0; i < cellCount; i++)
                 {
                     int readed = stream.Read(buffer, 0, buffer.Length);
                     if (readed <= 0)
                     {
                         Array.Clear(this.data, i * buffer.Length, this.data.Length - i * buffer.Length);
                         break;
                     }
                     int value = Memory.CellValue(buffer, Math.Min(8 * readed, dataBitWidth), 0);
                     Memory.SetCellValue(this.data, dataBitWidth, i, value);
                 }
             }
             this.FunctionMemory = new MemoryEditor(this.data, addressBitWidth, dataBitWidth);
         }
     } catch (Exception exception) {
         App.Mainframe.ReportException(exception);
     }
 }
 private void InsertImage()
 {
     try {
         OpenFileDialog dialog = new OpenFileDialog();
         dialog.InitialDirectory = Mainframe.IsDirectoryPathValid(this.openImageFolder.Value) ? this.openImageFolder.Value : Mainframe.DefaultPictureFolder();
         dialog.Filter           = Properties.Resources.ImageFileFilter;
         dialog.FilterIndex      = 0;
         bool?result = dialog.ShowDialog(this);
         if (result.HasValue && result.Value)
         {
             string path = dialog.FileName;
             this.openImageFolder.Value = Path.GetDirectoryName(path);
             Uri uri;
             if (Uri.TryCreate(path, UriKind.Absolute, out uri))
             {
                 Image image = new Image();
                 image.BeginInit();
                 image.Source   = new BitmapImage(uri);
                 image.MaxWidth = image.Source.Width;
                 image.EndInit();
                 Span span = new Span(this.editor.Selection.End, this.editor.Selection.End);
                 span.Inlines.Add(image);
             }
         }
     } catch (Exception exception) {
         App.Mainframe.ReportException(exception);
     }
 }
Beispiel #4
0
 public Editor(Mainframe mainframe, string file) : base(mainframe, Editor.Create(mainframe, file))
 {
     this.File = file;
     // Assume loading taken only one transaction. If auto saved file is loaded a new empty transaction is created, so set this to 1 to mark store dirty.
     this.savedVersion          = 1;
     this.CircuitDescriptorList = new CircuitDescriptorList(this.CircuitProject);
     this.switcher = new Switcher(this);
 }
 internal static void DeleteFile(string file)
 {
     if (Mainframe.IsFileExists(file))
     {
         try {
             File.Delete(file);
         } catch (Exception exception) {
             Tracer.Report("Mainframe.DeleteFile", exception);
         }
     }
 }
 internal static string AutoSaveFile(string file)
 {
     if (Mainframe.IsFilePathValid(file) && Path.HasExtension(file))
     {
         string extension = Path.GetExtension(file);
         if (!string.IsNullOrEmpty(extension) && 2 < extension.Length)
         {
             return(file.Substring(0, file.Length - 2) + "~$");
         }
     }
     return(null);
 }
 public static void OpenFile(string fileName)
 {
     if (string.IsNullOrEmpty(fileName))
     {
         throw new ArgumentNullException(nameof(fileName));
     }
     if (!Mainframe.IsFilePathValid(fileName) || !File.Exists(fileName))
     {
         throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "File \"{0}\" does not exist", fileName));
     }
     App.Mainframe.Dispatcher.Invoke(() => App.Mainframe.Open(fileName));
 }
        private void Save()
        {
            string file = this.Editor.File;

            if (Mainframe.IsFilePathValid(file))
            {
                this.Save(file);
            }
            else
            {
                this.SaveAs();
            }
        }
Beispiel #9
0
 private void ListBoxMouseDoubleClick(object sender, MouseButtonEventArgs e)
 {
     if (sender is ListBox listBox)
     {
         if (listBox.SelectedItem is LogicalCircuit selected)
         {
             Mainframe mainframe = (Mainframe)this.Owner;
             Editor    editor    = mainframe.Editor;
             editor.OpenLogicalCircuit(selected);
             editor.Select(editor.CircuitProject.CircuitSymbolSet.SelectByCircuit(this.LogicalCircuit).Where(s => s.LogicalCircuit == selected));
         }
     }
 }
 public DialogOptions(Mainframe mainframe)
 {
     this.mainframe            = mainframe;
     this.RecentFileRange      = PinDescriptor.NumberRange(1, 24);
     this.CurrentCulture       = App.CurrentCulture;
     this.AutoSaveIntervalList = PinDescriptor.NumberRange(1, 15);
     this.AutoSaveInterval     = Math.Max(1, Math.Min((this.mainframe.AutoSaveInterval != 0) ? this.mainframe.AutoSaveInterval / 60 : 5, 15));
     this.DataContext          = this;
     this.InitializeComponent();
     this.loadLastFile.IsChecked      = Settings.User.LoadLastFileOnStartup;
     this.autoSave.IsChecked          = this.mainframe.AutoSaveInterval != 0;
     this.showGrid.IsChecked          = this.mainframe.ShowGrid;
     this.gateShape.SelectedItem      = this.GateShapeList.First(d => d.Value == Settings.User.GateShape);
     this.maxRecentFiles.SelectedItem = Settings.User.MaxRecentFileCount;
 }
        private string DefaultFileName()
        {
            string imagePath = this.imageExportFolder.Value;

            if (!Mainframe.IsDirectoryPathValid(imagePath))
            {
                imagePath = Mainframe.DefaultPictureFolder();
            }
            string name = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}",
                                        this.editor.Project.Name,
                                        this.editor.Project.LogicalCircuit.Name,
                                        this.Encoder.Name
                                        );

            return(Path.Combine(imagePath, name));
        }
		private void ButtonSaveClick(object sender, RoutedEventArgs e) {
			try {
				SaveFileDialog dialog = new SaveFileDialog();
				dialog.InitialDirectory = Mainframe.IsDirectoryPathValid(this.openFileFolder.Value) ? this.openFileFolder.Value : Mainframe.DefaultProjectFolder();
				bool? result = dialog.ShowDialog(this);
				if(result.HasValue && result.Value) {
					string file = dialog.FileName;
					this.openFileFolder.Value = Path.GetDirectoryName(file);
					using(FileStream stream = File.Open(file, FileMode.Create, FileAccess.Write, FileShare.Write)) {
						stream.Write(this.data, 0, this.data.Length);
						stream.Flush();
					}
				}
			} catch(Exception exception) {
				App.Mainframe.ReportException(exception);
			}
		}
Beispiel #13
0
 public void AutoSave()
 {
     try {
         if (!string.IsNullOrEmpty(this.File) && this.HasChanges && this.autoSavedVersion != this.CircuitProject.Version)
         {
             string file = Mainframe.AutoSaveFile(this.File);
             if (!string.IsNullOrEmpty(file))
             {
                 Mainframe.DeleteFile(file);
                 this.CircuitProject.SaveSnapshot(file);
                 Mainframe.Hide(file);
             }
             this.autoSavedVersion = this.CircuitProject.Version;
         }
     } catch (Exception exception) {
         Tracer.Report("Editor.AutoSave", exception);
     }
 }
 private void OpenRecent(string file)
 {
     if (Mainframe.IsFilePathValid(file) && File.Exists(file))
     {
         if (this.Editor == null || this.EnsureSaved())
         {
             this.Edit(file);
         }
     }
     else if (file != null)
     {
         MessageBoxResult result = DialogMessage.Show(this, this.Title,
                                                      Properties.Resources.MessageInvalidRecentFile(file), null,
                                                      MessageBoxImage.Question, MessageBoxButton.YesNo
                                                      );
         if (result == MessageBoxResult.Yes)
         {
             Settings.User.DeleteRecentFile(file);
         }
     }
 }
Beispiel #15
0
        private static CircuitProject Create(Mainframe mainframe, string file)
        {
            bool   useAutoSaveFile = false;
            string autoSaveFile    = Mainframe.AutoSaveFile(file);

            if (Mainframe.IsFileExists(autoSaveFile))
            {
                App.Dispatch(() => {
                    MessageBoxResult result = DialogMessage.Show(
                        mainframe,
                        Properties.Resources.TitleApplication,
                        Properties.Resources.MessageLoadAutoSavedFile(file),
                        null,
                        MessageBoxImage.Question,
                        MessageBoxButton.YesNo
                        );
                    if (result == MessageBoxResult.Yes)
                    {
                        useAutoSaveFile = true;
                    }
                });
                if (!useAutoSaveFile)
                {
                    Mainframe.DeleteFile(autoSaveFile);
                }
            }
            if (!useAutoSaveFile)
            {
                autoSaveFile = file;
            }
            CircuitProject project = CircuitProject.Create(autoSaveFile);

            if (useAutoSaveFile)
            {
                project.InOmitTransaction(() => {});
            }
            return(project);
        }
 private void ButtonFileClick(object sender, RoutedEventArgs e)
 {
     try {
         string file = this.FilePath;
         if (!Mainframe.IsFilePathValid(file))
         {
             file = this.DefaultFileName();
         }
         SaveFileDialog dialog = new SaveFileDialog();
         dialog.InitialDirectory = Path.GetDirectoryName(file);
         dialog.FileName         = Path.GetFileName(file);
         dialog.Filter           = Properties.Resources.ImageFileFilter;
         dialog.DefaultExt       = this.Encoder.Name;
         bool?result = dialog.ShowDialog(this);
         if (result.HasValue && result.Value)
         {
             this.SetFilePath(dialog.FileName);
             this.SetEncoder(this.CurrentExtension());
         }
     } catch (Exception exception) {
         App.Mainframe.ReportException(exception);
     }
 }
 private void ButtonOkClick(object sender, RoutedEventArgs e)
 {
     try {
         BindingExpression filePathBindingExpression = BindingOperations.GetBindingExpression(this.fileName, TextBox.TextProperty);
         filePathBindingExpression.UpdateSource();
         if (File.Exists(this.FilePath))
         {
             if (MessageBoxResult.No == DialogMessage.Show(this, this.Title, Properties.Resources.MessageImageFileExists(this.FilePath),
                                                           null, MessageBoxImage.Warning, MessageBoxButton.YesNo
                                                           ))
             {
                 return;
             }
         }
         if (!Mainframe.IsFilePathValid(this.FilePath))
         {
             DialogMessage.Show(this, this.Title,
                                Properties.Resources.ImagePathInvalid, null, MessageBoxImage.Error, MessageBoxButton.OK
                                );
             return;
         }
         RenderTargetBitmap bitmap = this.editor.ExportImage();
         Tracer.Assert(bitmap != null);
         using (FileStream stream = new FileStream(this.FilePath, FileMode.Create)) {
             BitmapEncoder encoder = this.Encoder.BitmapEncoder;
             encoder.Frames.Clear();
             encoder.Frames.Add(BitmapFrame.Create(bitmap));
             encoder.Save(stream);
         }
         this.imageExportType.Value   = this.Encoder.Name;
         this.imageExportFolder.Value = Path.GetDirectoryName(this.FilePath);
         e.Handled         = true;
         this.DialogResult = true;
     } catch (Exception exception) {
         App.Mainframe.ReportException(exception);
     }
 }
 private void Open()
 {
     if (this.Editor == null || this.EnsureSaved())
     {
         OpenFileDialog dialog = new OpenFileDialog();
         string         file   = Settings.User.RecentFile();
         if (Mainframe.IsFilePathValid(file))
         {
             dialog.InitialDirectory = Path.GetDirectoryName(Path.GetFullPath(file));
         }
         else
         {
             dialog.InitialDirectory = Mainframe.DefaultProjectFolder();
         }
         dialog.Filter     = Mainframe.FileFilter;
         dialog.DefaultExt = Mainframe.FileExtention;
         bool?result = dialog.ShowDialog(this);
         if (result.HasValue && result.Value)
         {
             file = dialog.FileName;
             this.Edit(file);
         }
     }
 }
 private void Import()
 {
     if (this.Editor != null && this.Editor.InEditMode)
     {
         string dir    = Mainframe.DefaultProjectFolder();
         string recent = Settings.User.RecentFile();
         if (Mainframe.IsFilePathValid(recent))
         {
             dir = Path.GetDirectoryName(recent);
         }
         SettingsStringCache location = new SettingsStringCache(Settings.User, "ImportFile.Folder", dir);
         OpenFileDialog      dialog   = new OpenFileDialog();
         dialog.Filter           = Mainframe.FileFilter;
         dialog.DefaultExt       = Mainframe.FileExtention;
         dialog.InitialDirectory = Mainframe.IsDirectoryPathValid(location.Value) ? location.Value : Mainframe.DefaultProjectFolder();
         bool?result = dialog.ShowDialog(this);
         if (result.HasValue && result.Value)
         {
             string file = dialog.FileName;
             location.Value = Path.GetDirectoryName(file);
             this.Editor.Import(file);
         }
     }
 }
 public static bool IsFileExists(string file)
 {
     return(Mainframe.IsFilePathValid(file) && File.Exists(file));
 }
Beispiel #21
0
        protected DialogMemoryEditor(Memory memory)
        {
            string typeName = this.GetType().Name;

            this.openFileFolder = new SettingsStringCache(Settings.User, typeName + ".OpenFile.Folder", Mainframe.DefaultProjectFolder());
            this.DataHeight     = new SettingsGridLengthCache(Settings.User, typeName + ".Data.Height", memory.Writable ? "0.25*" : "0.75*");
            this.NoteHeight     = new SettingsGridLengthCache(Settings.User, typeName + ".Note.Height", memory.Writable ? "0.75*" : "0.25*");

            this.Memory = memory;
            this.data   = memory.MemoryValue();

            this.DataContext = this;
            this.InitializeComponent();

            this.addressBitWidth.ItemsSource = MemoryDescriptor.AddressBitWidthRange;
            this.dataBitWidth.ItemsSource    = PinDescriptor.BitWidthRange;
            IEnumerable <EnumDescriptor <bool> > writeOnList = MemoryDescriptor.WriteOnList;

            this.writeOn.ItemsSource = writeOnList;
            EnumDescriptor <MemoryOnStart>[] onStartList = new EnumDescriptor <MemoryOnStart>[] {
                new EnumDescriptor <MemoryOnStart>(MemoryOnStart.Random, Properties.Resources.MemoryOnStartRandom),
                new EnumDescriptor <MemoryOnStart>(MemoryOnStart.Zeros, Properties.Resources.MemoryOnStartZeros),
                new EnumDescriptor <MemoryOnStart>(MemoryOnStart.Ones, Properties.Resources.MemoryOnStartOnes),
                new EnumDescriptor <MemoryOnStart>(MemoryOnStart.Data, Properties.Resources.MemoryOnStartData)
            };
            this.onStart.ItemsSource = onStartList;

            this.addressBitWidth.SelectedItem = this.currentAddressBitWidth = this.Memory.AddressBitWidth;
            this.dataBitWidth.SelectedItem    = this.currentDataBitWidth = this.Memory.DataBitWidth;
            this.writeOn.SelectedItem         = writeOnList.First(d => d.Value == this.Memory.WriteOn1);
            this.onStart.SelectedItem         = onStartList.First(d => d.Value == (this.Memory.Writable ? this.Memory.OnStart : MemoryOnStart.Data));
            this.note.Text = this.Memory.Note;

            this.FunctionMemory = new MemoryEditor(this.data, this.Memory.AddressBitWidth, this.DataBitWidth);

            this.initialized = true;
        }