/// Copyright (c) 2021 Iiro Iivanainen, Harri Linna, Jere Pakkanen, Riikka Vilavaara /// /// Permission is hereby granted, free of charge, to any person obtaining /// a copy of this software and associated documentation files (the /// "Software"), to deal in the Software without restriction, including /// without limitation the rights to use, copy, modify, merge, publish, /// distribute, sublicense, and/or sell copies of the Software, and to /// permit persons to whom the Software is furnished to do so, subject to /// the following conditions: /// /// The above copyright notice and this permission notice shall be included /// in all copies or substantial portions of the Software. /// /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, /// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF /// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. /// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY /// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, /// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE /// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. /// using System; using System.Windows; using System.Windows.Controls; using Microsoft.Win32; using System.IO; namespace GroundhogApp.Dialogs { /// /// Interaction logic for OpenMapDialog.xaml /// public partial class OpenMapDialog : Window { /// /// Constructor for OpenMapDialog /// public OpenMapDialog() { InitializeComponent(); ByteDepth = 1; } public string FilePath { get; set; } public int MapHeight { get; set; } public int MapWidth { get; set; } public int MapDepth { get; set; } public int ByteDepth { get; set; } public bool? LittleEndian { get; set; } public string FileTemplate { get; set; } public enum FileType { ImageSequence, Tiff3D, Raw } public FileType MapType { get; set; } /// /// Clicking Open button. /// /// Event sender. Not Used. /// private void OpenBtn_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new(); switch (FileTypeComboBox.SelectedIndex) { case 0: openFileDialog.Filter = "Tiff files (*.tif;*.tiff)|*.tif;*.tiff" + "|Png files (*.png;*.PNG)|*.png;*.PNG"; openFileDialog.Title = "Opening Image Sequence"; if (openFileDialog.ShowDialog() == true) PrepImgSeq(openFileDialog.FileName); break; case 1: openFileDialog.Filter = "Tiff files (*.tif;*.tiff)|*.tif;*.tiff"; openFileDialog.Title = "Opening Multiframe tiff"; if (openFileDialog.ShowDialog() == true) PrepTiff3D(openFileDialog.FileName); break; case 2: openFileDialog.Filter = "Raw files (*.raw)|*.raw"; openFileDialog.Title = "Opening Raw"; if (openFileDialog.ShowDialog() == true) PrepRaw(openFileDialog.FileName); break; default: break; } FilePathBox.Focus(); FilePathBox.Select(FilePathBox.Text.Length, 0); } /// /// Opening Multiframe Tiff file. /// /// File path private void PrepTiff3D(string path) { FilePathBox.Text = path; MapType = FileType.Tiff3D; } /// /// Opening Raw file. /// /// File path private void PrepRaw(string path) { FilePathBox.Text = path; MapType = FileType.Raw; } /// /// Opening Image Sequence file. /// /// File path private void PrepImgSeq(string path) { FilePathBox.Text = path; string template = ""; try { template = CoreLibrary.MapReader.MakeImgSeqTemplateGuess(path); } catch (IOException) { MessageBox.Show("Something went wrong with trying to make template guess.","Error"); } catch (ArgumentException) { MessageBox.Show("Something went wrong with trying to make template guess.", "Error"); } SeqTemplateBox.Text = template; MapType = FileType.ImageSequence; } /// /// Selection changes on FileTypeComboBox. /// /// Event sender. Not used. /// Event arguments. Not used. private void FileTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (!this.IsInitialized) return; switch (FileTypeComboBox.SelectedIndex) { case 0: ImgSeqPanel.Visibility = Visibility.Visible; Tiff3DPanel.Visibility = Visibility.Collapsed; RawPanel.Visibility = Visibility.Collapsed; MapType = FileType.ImageSequence; break; case 1: ImgSeqPanel.Visibility = Visibility.Collapsed; Tiff3DPanel.Visibility = Visibility.Visible; RawPanel.Visibility = Visibility.Collapsed; MapType = FileType.Tiff3D; break; case 2: ImgSeqPanel.Visibility = Visibility.Collapsed; Tiff3DPanel.Visibility = Visibility.Collapsed; RawPanel.Visibility = Visibility.Visible; MapType = FileType.Raw; break; default: break; } FilePathBox.Text = ""; } /// /// Event handler for clicking OK button on dialog. /// /// Event sender. Not used. /// Event arguments. Not used. private void OkBtn_Click(object sender, RoutedEventArgs e) { if (FilePathBox.Text.Trim().Length == 0) { _ = MessageBox.Show("Empty filepath.", "Error"); return; } FileInfo fi = new(FilePathBox.Text); if (!fi.Exists) { MessageBox.Show("File does not exist in specified location.", "Error"); return; } switch (FileTypeComboBox.SelectedIndex) { case 0: case 1: break; case 2: if (WidthErrorText.IsVisible || MapWidth <= 0) { MessageBox.Show("Invalid value: Width.", "Error"); return; } if (HeightErrorText.IsVisible || MapHeight <= 0) { MessageBox.Show("Invalid value: Height.", "Error"); return; } if (ImagesErrorText.IsVisible || MapDepth <= 0) { MessageBox.Show("Invalid value: Images.", "Error"); return; } break; default: break; } FilePath = FilePathBox.Text; FileTemplate = SeqTemplateBox.Text; LittleEndian = LittleEndianCheck.IsChecked; DialogResult = true; } /// /// Clicking Cancel button on dialog. /// /// Event sender. Not used. /// Event arguments. Not used. private void CancelBtn_Click(object sender, RoutedEventArgs e) { DialogResult = false; } /// /// Handler for SelectionChanged event for the BitBox element. /// /// Event sender. Not used. /// Event arguments. Not used. private void BitBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (!this.IsInitialized) return; switch (BitBox.SelectedIndex) { case 0: ByteDepth = 1; break; case 1: ByteDepth = 2; break; case 2: ByteDepth = 4; break; case 3: ByteDepth = 8; break; default: break; } CalculateRawRequestedSize(); } /// /// Event handler for TextChanged event for WidthBox element. /// /// Event sender. Not used. /// Event arguments. Not used. private void WidthBox_TextChanged(object sender, TextChangedEventArgs e) { ChangeDimension(WidthBox, WidthErrorText, x => MapWidth = x); } /// /// Event handler for TextChanged event for HeightBox element. /// /// Event sender. Not used. /// Event arguments. Not used. private void HeightBox_TextChanged(object sender, TextChangedEventArgs e) { ChangeDimension(HeightBox, HeightErrorText, x => MapHeight = x); } /// /// Event handler for TextChanged event for DepthBox element. /// /// Event sender. Not used. /// Event arguments. Not used. private void DepthBox_TextChanged(object sender, TextChangedEventArgs e) { ChangeDimension(DepthBox, ImagesErrorText, x => MapDepth = x); } /// /// Parses integer from a specified textbox as a dimension, /// shows or hides error message accordinly and uses action /// on parsed dimension. /// /// TextBox from which the value is parsed /// from. /// Which error textblock is used. /// Action that uses the parsed /// dimension private void ChangeDimension(TextBox box, TextBlock txt, Action setDimension) { if (int.TryParse(box.Text.Trim(), out int dim) && (dim > 0)) { box.Style = (Style)FindResource("DefaultTextBox"); setDimension(dim); txt.Visibility = Visibility.Collapsed; } else { box.Style = (Style)FindResource("ErrorTextBox"); txt.Visibility = Visibility.Visible; } CalculateRawRequestedSize(); } /// /// Event handler for TextChanged event for FilePathBox-element. /// /// Event sender. Not used. /// Event arguments. Not used. private void FilePathBox_TextChanged(object sender, TextChangedEventArgs e) { if (FilePathBox.Text.Trim().Length == 0) return; var fi = new FileInfo(FilePathBox.Text); if (fi.Exists) { RawFileSize.Text = fi.Length.ToString(); FilePathBox.Style = (Style) FindResource("DefaultTextBox"); FilePathErrorMsg.Text = ""; } else { RawFileSize.Text = "Unknown File"; FilePathBox.Style = (Style) FindResource("ErrorTextBox"); FilePathErrorMsg.Text = "File does not exist in specified location."; } } /// /// Calculates file size for the Raw file user is opening. /// Based on given dimensions. /// private void CalculateRawRequestedSize() { try { RawReqText.Text = checked(((ulong)MapWidth * (ulong)MapHeight * (ulong)MapDepth * (ulong)ByteDepth).ToString()); } catch (OverflowException) { RawReqText.Text = "Entered dimensions too large! Check again."; } } } }