Skip to content

Commit

Permalink
Fun with regular expressions
Browse files Browse the repository at this point in the history
- search with check box to use regular rexpression (c# syntax)
- left aligned list view columns
- search shows the average costs per day and month for the result
  • Loading branch information
nylssoft committed Apr 2, 2018
1 parent 63da929 commit 7aec590
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 60 deletions.
7 changes: 5 additions & 2 deletions Bank/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
Startup="Application_Startup"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="Padding" Value="5,0,0,0" />
</Style>
</Application.Resources>
</Application>
4 changes: 2 additions & 2 deletions Bank/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
<GridViewColumnHeader Tag="Text" Click="ListView_ColumnHeaderClick" Content="{x:Static props:Resources.COLUMN_TEXT}"/>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="100">
<GridViewColumn Width="70">
<GridViewColumn.Header>
<GridViewColumnHeader Tag="Amount" Click="ListView_ColumnHeaderClick" Content="{x:Static props:Resources.COLUMN_AMOUNT}" />
</GridViewColumn.Header>
Expand All @@ -193,7 +193,7 @@
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="100">
<GridViewColumn Width="90">
<GridViewColumn.Header>
<GridViewColumnHeader Tag="CurrentBalance" Click="ListView_ColumnHeaderClick" Content="{x:Static props:Resources.COLUMN_BALANCE}" />
</GridViewColumn.Header>
Expand Down
4 changes: 2 additions & 2 deletions Bank/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
[assembly: AssemblyVersion("1.0.4.5")]
[assembly: AssemblyFileVersion("1.0.4.5")]
[assembly: AssemblyVersion("1.0.4.6")]
[assembly: AssemblyFileVersion("1.0.4.6")]
[assembly: AssemblyInformationalVersion("1.0.4")]
[assembly: NeutralResourcesLanguage("")]
[assembly: Guid("508F852D-294F-4FDE-8B4E-3B146E326FBD")]
18 changes: 18 additions & 0 deletions Bank/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Bank/Properties/Resources.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@
<data name="LABEL_SEARCH_IN" xml:space="preserve">
<value>Suchen _in:</value>
</data>
<data name="LABEL_SEARCH_REGULAR_EXPR" xml:space="preserve">
<value>_Regulärer Ausdruck</value>
</data>
<data name="LABEL_TEXT" xml:space="preserve">
<value>T_ext:</value>
</data>
Expand Down Expand Up @@ -324,6 +327,9 @@
<data name="STATUS_AVERAGE_PER_MONTH_0" xml:space="preserve">
<value>Monatliche Durchschnittsbelastung: {0}</value>
</data>
<data name="STATUS_SEARCH_AVG_0_1_2_3" xml:space="preserve">
<value>{0} in {1} Tagen. Im Durchschnitt {2} pro Tag und {3} pro Monat.</value>
</data>
<data name="TEXT_COPYRIGHT" xml:space="preserve">
<value>Copyright 2018 Niels Stockfleth

Expand Down
6 changes: 6 additions & 0 deletions Bank/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@
<data name="LABEL_SEARCH_IN" xml:space="preserve">
<value>Search _in:</value>
</data>
<data name="LABEL_SEARCH_REGULAR_EXPR" xml:space="preserve">
<value>_Regular Expression</value>
</data>
<data name="LABEL_TEXT" xml:space="preserve">
<value>_Text:</value>
</data>
Expand Down Expand Up @@ -324,6 +327,9 @@
<data name="STATUS_AVERAGE_PER_MONTH_0" xml:space="preserve">
<value>Average balance per month: {0}.</value>
</data>
<data name="STATUS_SEARCH_AVG_0_1_2_3" xml:space="preserve">
<value>{0} in {1} days. Average {2} per day and {3} per month.</value>
</data>
<data name="TEXT_COPYRIGHT" xml:space="preserve">
<value>Copyright 2018 Niels Stockfleth

Expand Down
21 changes: 12 additions & 9 deletions Bank/SearchWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,31 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="{x:Static props:Resources.LABEL_SEARCH_FOR}" Target="{Binding ElementName=textBoxSearch}"/>
<TextBox Grid.Row="0" Grid.Column="1" x:Name="textBoxSearch" Height="23" Margin="0,0,5,0" LostFocus="TextBoxSearch_LostFocus" KeyUp="TextBoxSearch_KeyUp" MaxLength="1024"/>
<Label Grid.Row="0" Grid.Column="2" Content="{x:Static props:Resources.LABEL_FROM}" Target="{Binding ElementName=datePickerFrom}"/>
<DatePicker Grid.Row="0" Grid.Column="3" x:Name="datePickerFrom" SelectedDateChanged="DatePickerFrom_SelectedDateChanged" Height="23"/>
<Label Grid.Row="0" Grid.Column="4" Content="{x:Static props:Resources.LABEL_TO}" Target="{Binding ElementName=datePickerTo}"/>
<DatePicker Grid.Row="0" Grid.Column="5" x:Name="datePickerTo" SelectedDateChanged="DatePickerTo_SelectedDateChanged" Height="23"/>
<TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4" x:Name="textBoxSearch" Height="23" Margin="0,0,5,0" LostFocus="TextBoxSearch_LostFocus" KeyUp="TextBoxSearch_KeyUp" MaxLength="1024"/>
<CheckBox Grid.Row="0" Grid.Column="5" x:Name="checkBoxRegularExpression" Content="{x:Static props:Resources.LABEL_SEARCH_REGULAR_EXPR}" Margin="5,5,0,0" Click="CheckBox_Changed"/>
<Label Grid.Row="1" Grid.Column="0" x:Name="labelAccount" Content="{x:Static props:Resources.LABEL_SEARCH_IN}" />
<StackPanel Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="5" x:Name="stackPanelAccounts" Orientation="Horizontal"/>
<Label Grid.Row="2" Grid.Column="0" Content="{x:Static props:Resources.LABEL_FROM}" Target="{Binding ElementName=datePickerFrom}"/>
<DatePicker Grid.Row="2" Grid.Column="1" x:Name="datePickerFrom" SelectedDateChanged="DatePickerFrom_SelectedDateChanged" Margin="0,0,0,5"/>
<Label Grid.Row="2" Grid.Column="2" Content="{x:Static props:Resources.LABEL_TO}" Target="{Binding ElementName=datePickerTo}"/>
<DatePicker Grid.Row="2" Grid.Column="3" x:Name="datePickerTo" SelectedDateChanged="DatePickerTo_SelectedDateChanged" Margin="0,0,0,5"/>
</Grid>
<ListView Grid.Row="1" x:Name="listView" SelectionChanged="ListView_SelectionChanged">
<ListView.ItemContainerStyle>
Expand All @@ -47,7 +50,7 @@
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Width="100" DisplayMemberBinding="{Binding DateString}">
<GridViewColumn Width="90" DisplayMemberBinding="{Binding DateString}">
<GridViewColumn.Header>
<GridViewColumnHeader x:Name="gridViewColumHeaderDate" Tag="Date" Click="ListView_ColumnHeaderClick" Content="{x:Static props:Resources.COLUMN_DATE}"/>
</GridViewColumn.Header>
Expand All @@ -57,7 +60,7 @@
<GridViewColumnHeader Tag="Text" Click="ListView_ColumnHeaderClick" Content="{x:Static props:Resources.COLUMN_TEXT}"/>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="100">
<GridViewColumn Width="70">
<GridViewColumn.Header>
<GridViewColumnHeader Tag="Amount" Click="ListView_ColumnHeaderClick" Content="{x:Static props:Resources.COLUMN_AMOUNT}" />
</GridViewColumn.Header>
Expand All @@ -75,6 +78,6 @@
</GridView>
</ListView.View>
</ListView>
<TextBlock Grid.Row="2" x:Name="textBlockInfo" TextTrimming="CharacterEllipsis"/>
<TextBlock Grid.Row="2" x:Name="textBlockInfo" TextTrimming="CharacterEllipsis" Margin="0,5,0,5"/>
</Grid>
</Window>
128 changes: 83 additions & 45 deletions Bank/SearchWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
Expand All @@ -35,6 +36,7 @@ public partial class SearchWindow : Window
private DateTime? lastFromDate = null;
private DateTime? lastToDate = null;
private string lastSearchText = string.Empty;
private bool lastRegularExpr = false;
private ISet<string> lastAccountNames = new HashSet<string>();

public SearchWindow(Window owner, string title)
Expand Down Expand Up @@ -68,7 +70,7 @@ private void Init(Database database)
stackPanelAccounts.Children.Clear();
var nw = DateTime.Now;
refdate = new DateTime(nw.Year, nw.Month, nw.Day);
var last = new DateTime(refdate.Year - 1, 1, 1);
var last = refdate.Subtract(new TimeSpan(365,0,0,0));
var lastdays = (int)(refdate - last).TotalDays;
datePickerFrom.SelectedDate = refdate;
datePickerTo.SelectedDate = last;
Expand Down Expand Up @@ -177,62 +179,81 @@ private void ListView_ColumnHeaderClick(object sender, RoutedEventArgs e)

private void Search()
{
var search = textBoxSearch.Text.ToLower();
if (string.IsNullOrEmpty(search) ||
!datePickerFrom.SelectedDate.HasValue ||
!datePickerTo.SelectedDate.HasValue)
{
return;
}
var fromdays = Math.Max((int)((refdate - datePickerFrom.SelectedDate.Value).TotalDays), 0);
var todays = Math.Max((int)(refdate - datePickerTo.SelectedDate.Value).TotalDays + 1, 0);
if (todays < fromdays)
{
return;
}
ISet<string> accountNames = new HashSet<string>();
foreach (CheckBox cb in stackPanelAccounts.Children)
try
{
if (cb.IsChecked == true && cb.Tag is Account account)
var search = textBoxSearch.Text.ToLower();
if (string.IsNullOrEmpty(search) ||
!datePickerFrom.SelectedDate.HasValue ||
!datePickerTo.SelectedDate.HasValue)
{
accountNames.Add(account.Name);
return;
}
}
if (lastFromDate == datePickerFrom.SelectedDate &&
lastToDate == datePickerTo.SelectedDate &&
lastSearchText == textBoxSearch.Text &&
lastAccountNames.SetEquals(accountNames))
{
return;
}
lastFromDate = datePickerFrom.SelectedDate;
lastToDate = datePickerTo.SelectedDate;
lastSearchText = textBoxSearch.Text;
lastAccountNames = accountNames;
result.Clear();
foreach (var sb in all)
{
if (accountNames.Contains(sb.AccountName))
var fromdays = Math.Max((int)((refdate - datePickerFrom.SelectedDate.Value).TotalDays), 0);
var todays = Math.Max((int)(refdate - datePickerTo.SelectedDate.Value).TotalDays, 0);
if (todays < fromdays)
{
return;
}
ISet<string> accountNames = new HashSet<string>();
foreach (CheckBox cb in stackPanelAccounts.Children)
{
if (cb.IsChecked == true && cb.Tag is Account account)
{
accountNames.Add(account.Name);
}
}
if (lastFromDate == datePickerFrom.SelectedDate &&
lastToDate == datePickerTo.SelectedDate &&
lastSearchText == textBoxSearch.Text &&
lastRegularExpr == checkBoxRegularExpression.IsChecked &&
lastAccountNames.SetEquals(accountNames))
{
if (sb.Text.ToLower().Contains(search))
return;
}
lastFromDate = datePickerFrom.SelectedDate;
lastToDate = datePickerTo.SelectedDate;
lastSearchText = textBoxSearch.Text;
lastRegularExpr = checkBoxRegularExpression.IsChecked == true;
lastAccountNames = accountNames;
result.Clear();
bool regex = checkBoxRegularExpression.IsChecked == true;
foreach (var sb in all)
{
if (accountNames.Contains(sb.AccountName))
{
var x = (refdate - sb.Date).TotalDays;
if (x >= fromdays && x <= todays)
bool ok = false;
if (regex)
{
ok = Regex.IsMatch(sb.Text, search);
}
else
{
result.Add(sb);
ok = sb.Text.ToLower().Contains(search);
}
if (ok)
{
var x = (refdate - sb.Date).TotalDays;
if (x >= fromdays && x <= todays)
{
result.Add(sb);
}
}
}
}
if (sortDecorator == null)
{
sortDecorator = new SortDecorator(ListSortDirection.Descending);
sortDecorator.Click(gridViewColumHeaderDate);
var viewlist = (CollectionView)CollectionViewSource.GetDefaultView(listView.ItemsSource);
viewlist.SortDescriptions.Clear();
viewlist.SortDescriptions.Add(new SortDescription("Date", ListSortDirection.Descending));
}
UpdateStatus();
}
if (sortDecorator == null)
catch (Exception ex)
{
sortDecorator = new SortDecorator(ListSortDirection.Descending);
sortDecorator.Click(gridViewColumHeaderDate);
var viewlist = (CollectionView)CollectionViewSource.GetDefaultView(listView.ItemsSource);
viewlist.SortDescriptions.Clear();
viewlist.SortDescriptions.Add(new SortDescription("Date", ListSortDirection.Descending));
MessageBox.Show(ex.Message);
}
UpdateStatus();
}

private void UpdateStatus()
Expand Down Expand Up @@ -263,6 +284,23 @@ private void UpdateStatus()
else
{
status = string.Format(Properties.Resources.TOTAL_0, total);
var fromdays = Math.Max((int)((refdate - datePickerFrom.SelectedDate.Value).TotalDays), 0);
var todays = Math.Max((int)(refdate - datePickerTo.SelectedDate.Value).TotalDays, 0);
if (todays > fromdays)
{
var days = todays - fromdays;
long sum = 0;
foreach (var b in result)
{
sum += b.Amount;
}
status += " ";
status += string.Format(Properties.Resources.STATUS_SEARCH_AVG_0_1_2_3,
CurrencyConverter.ConvertToCurrencyString(sum),
days,
CurrencyConverter.ConvertToCurrencyString(sum / days),
CurrencyConverter.ConvertToCurrencyString((long)((sum / days) * 30.42)));
}
}
}
textBlockInfo.Text = status;
Expand Down

0 comments on commit 7aec590

Please sign in to comment.