Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
smourier committed Apr 5, 2024
1 parent d5c7499 commit 21153f7
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 56 deletions.
3 changes: 3 additions & 0 deletions SheetReader.Wpf.Test/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
Header="E_xit"
InputGestureText="Alt+F4" />
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Click="OpenWithExcel_Click" Header="Open with Excel..." />
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Click="About_Click" Header="About..." />
</MenuItem>
Expand Down
44 changes: 18 additions & 26 deletions SheetReader.Wpf.Test/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public MainWindow()
}

public string? FileName { get; set; }
public ObservableCollection<SheetData> Sheets { get; } = [];
public ObservableCollection<BookDocumentSheet> Sheets { get; } = [];

protected override void OnKeyDown(KeyEventArgs e)
{
Expand All @@ -40,6 +40,7 @@ protected override void OnKeyDown(KeyEventArgs e)
}
}

private void OpenWithExcel_Click(object sender, RoutedEventArgs e) => OpenWithExcel();
private void ClearRecentFiles_Click(object sender, RoutedEventArgs e) => Settings.Current.ClearRecentFiles();
private void Exit_Click(object sender, RoutedEventArgs e) => Close();
private void About_Click(object sender, RoutedEventArgs e) => MessageBox.Show(Assembly.GetEntryAssembly()!.GetCustomAttribute<AssemblyTitleAttribute>()!.Title + " - " + (IntPtr.Size == 4 ? "32" : "64") + "-bit" + Environment.NewLine + "Copyright (C) 2021-" + DateTime.Now.Year + " Simon Mourier. All rights reserved.", Assembly.GetEntryAssembly()!.GetCustomAttribute<AssemblyTitleAttribute>()!.Title, MessageBoxButton.OK, MessageBoxImage.Information);
Expand All @@ -60,6 +61,14 @@ private void Open_Click(object sender, RoutedEventArgs e)
Load(ofd.FileName);
}

private void OpenWithExcel()
{
if (FileName == null)
return;

Extensions.OpenFile(FileName);
}

private void OnFileOpened(object sender, RoutedEventArgs e)
{
const int fixedRecentItemsCount = 2;
Expand Down Expand Up @@ -88,30 +97,13 @@ private void Load(string? fileName)
return;

Sheets.Clear();
var book = new Book();
try
{
foreach (var sheet in book.EnumerateSheets(fileName))
var book = new BookDocument();
book.Load(fileName);
foreach (var sheet in book.Sheets)
{
// we need to load data for WPF data binding
var sheetData = new SheetData { Name = sheet.Name, IsHidden = !sheet.IsVisible };
foreach (var row in sheet.EnumerateRows())
{
var rowData = new RowData(row.Index, row.EnumerateCells().ToList());
sheetData.Rows.Add(rowData);
}

sheetData.Columns = sheet.EnumerateColumns().ToList();
if (sheetData.Columns.Count > 0)
{
sheetData.LastColumnIndex = sheetData.Columns.Max(x => x.Index);
}

if (sheetData.Rows.Count > 0)
{
sheetData.LastRowIndex = sheetData.Rows.Max(x => x.RowIndex);
}
Sheets.Add(sheetData);
Sheets.Add(sheet);
}
}
catch (Exception ex)
Expand All @@ -123,12 +115,12 @@ private void Load(string? fileName)
// not sure why, but with only 1 tab, binding doesn't work...
if (Sheets.Count == 1)
{
var dummy = new SheetData();
Sheets.Add(dummy);
Sheets.Remove(dummy);
var first = Sheets[0];
Sheets.Add(first);
Sheets.RemoveAt(1);
}
Title = "Sheet Reader - " + Path.GetFileName(fileName);

Title = "Sheet Reader - " + Path.GetFileName(fileName);
FileName = fileName;
Settings.Current.AddRecentFile(fileName);
}
Expand Down
12 changes: 12 additions & 0 deletions SheetReader.Wpf.Test/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -134,6 +135,17 @@ public static bool EqualsIgnoreCase(this string? thisString, string? text, bool
return t.Length == 0 ? null : t;
}

public static void OpenFile(string fileName)
{
ArgumentNullException.ThrowIfNull(fileName);
var psi = new ProcessStartInfo
{
FileName = fileName,
UseShellExecute = true
};
Process.Start(psi);
}

public static void RaiseMenuItemClickOnKeyGesture(this ItemsControl control, KeyEventArgs args, bool throwOnError = true)
{
ArgumentNullException.ThrowIfNull(args);
Expand Down
2 changes: 1 addition & 1 deletion SheetReader.Wpf.Test/Utilities/IOUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SheetReader.Wpf.Test.Utilities
{
public static class IOUtilities
internal static class IOUtilities
{
public static bool IsPathRooted(string path)
{
Expand Down
10 changes: 0 additions & 10 deletions SheetReader.Wpf/RowData.cs

This file was deleted.

8 changes: 5 additions & 3 deletions SheetReader.Wpf/SheetControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SheetControl : Control
public const string PartScrollViewerName = "PART_ScrollViewer";

public static readonly DependencyProperty SheetProperty = DependencyProperty.Register(nameof(Sheet),
typeof(SheetData),
typeof(BookDocumentSheet),
typeof(SheetControl),
new UIPropertyMetadata(null, (d, e) => ((SheetControl)d).OnSheetChanged()));

Expand All @@ -33,7 +33,7 @@ public class SheetControl : Control
typeof(SheetControl),
new UIPropertyMetadata(0.5));

public SheetData Sheet { get => (SheetData)GetValue(SheetProperty); set => SetValue(SheetProperty, value); }
public BookDocumentSheet Sheet { get => (BookDocumentSheet)GetValue(SheetProperty); set => SetValue(SheetProperty, value); }
public double ColumnWidth { get { return (double)GetValue(ColumnWidthProperty); } set { SetValue(ColumnWidthProperty, value); } }
public double RowHeight { get { return (double)GetValue(RowHeightProperty); } set { SetValue(RowHeightProperty, value); } }
public double GridLineSize { get => (double)GetValue(GridLineSizeProperty); set => SetValue(GridLineSizeProperty, value); }
Expand Down Expand Up @@ -193,7 +193,9 @@ protected override void OnRender(DrawingContext drawingContext)

for (var i = firstDrawnRowIndex; i <= lastDrawnRowIndex; i++)
{
var row = control.Sheet.Rows[i];
if (!control.Sheet.Rows.TryGetValue(i, out var row))
continue;

for (var j = firstDrawnColumnIndex; j <= lastDrawnColumnIndex; j++)
{
if (j >= row.Cells.Count)
Expand Down
16 changes: 0 additions & 16 deletions SheetReader.Wpf/SheetData.cs

This file was deleted.

0 comments on commit 21153f7

Please sign in to comment.