lab4
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ProgressBar\ProgressBar.csproj" />
|
||||
<ProjectReference Include="..\Tool\Tool.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -9,28 +9,60 @@
|
||||
<Grid>
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="30"/>
|
||||
<RowDefinition Height="100*"/>
|
||||
<RowDefinition Height="30" />
|
||||
<RowDefinition Height="30" />
|
||||
<RowDefinition Height="100*" />
|
||||
</Grid.RowDefinitions>
|
||||
<DataGrid x:Name="_dataGrid" d:ItemsSource="{d:SampleData ItemCount=5}" RenderTransformOrigin="0.5,0" Grid.Row="1"/>
|
||||
<DataGrid x:Name="_dataGrid" d:ItemsSource="{d:SampleData ItemCount=5}" RenderTransformOrigin="0.5,0"
|
||||
Grid.Row="2" />
|
||||
|
||||
<Grid Grid.Row="0">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="1.15*"/>
|
||||
<ColumnDefinition Width="1.1*"/>
|
||||
<ColumnDefinition Width="0.8*"/>
|
||||
<ColumnDefinition Width="0.8*"/>
|
||||
<ColumnDefinition Width="0.8*"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="1.15*" />
|
||||
<ColumnDefinition Width="1.1*" />
|
||||
<ColumnDefinition Width="0.8*" />
|
||||
<ColumnDefinition Width="0.8*" />
|
||||
<ColumnDefinition Width="0.8*" />
|
||||
<ColumnDefinition Width="*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<ComboBox IsEditable="False" Grid.Column="0" Margin="5,5,5,5" ItemsSource="{Binding FactoryMethods}" SelectedItem="{Binding SelectedMethod}" />
|
||||
|
||||
<ComboBox IsEditable="False" Grid.Column="0" Margin="5,5,5,5" ItemsSource="{Binding FactoryMethods}"
|
||||
SelectedItem="{Binding SelectedMethod}" />
|
||||
<Button Click="CreateManualToolButton_Click" Content="Add Manual Tool" Grid.Column="1" Margin="5,5,5,5" />
|
||||
<Button Click="CreateElectricToolButton_Click" Content="Add Electric Tool" Grid.Column="2" Margin="5,5,5,5" />
|
||||
<Button Click="CreateElectricToolButton_Click" Content="Add Electric Tool" Grid.Column="2"
|
||||
Margin="5,5,5,5" />
|
||||
<Button Click="EditSelectedButton_Click" Content="Edit" Grid.Column="3" Margin="5,5,5,5" />
|
||||
<Button Click="DeleteSelectedButton_Click" Content="Delete" Grid.Column="4" Margin="5,5,5,5" />
|
||||
<Button Click="GenerateButton_Click" Content="Generate" Grid.Column="5" Margin="5,5,5,5" />
|
||||
</Grid>
|
||||
|
||||
<Grid Grid.Row="1">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="0.8*" />
|
||||
<ColumnDefinition Width="45" />
|
||||
<ColumnDefinition Width="0.8*" />
|
||||
<ColumnDefinition Width="50" />
|
||||
<ColumnDefinition Width="0.8*" />
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="0.5*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Button Grid.Column="0" Content="Filter" Click="FilterButton_OnClick" Margin="5,5,10,5" />
|
||||
<Label Grid.Column="1" Content="Name:" />
|
||||
<TextBox Grid.Column="2" Text="{Binding NameFilter}" Margin="5,5,10,5" />
|
||||
<Label Grid.Column="3" Content="Weight:" />
|
||||
<Grid Grid.Column="4">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="0.5*" />
|
||||
<ColumnDefinition Width="20" />
|
||||
<ColumnDefinition Width="0.5*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBox Grid.Column="0" Text="{Binding WeightMinFilter}" Margin="5,5,10,5" />
|
||||
<Label Grid.Column="1" Content="≤" />
|
||||
<TextBox Grid.Column="2" Text="{Binding WeightMaxFilter}" Margin="5,5,10,5" />
|
||||
</Grid>
|
||||
<Button Content="Clear Filter" Click="ClearFilterButton_OnClick" Grid.Column="6" Margin="5,5,10,5" />
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Window>
|
||||
</Window>
|
||||
@@ -4,6 +4,9 @@ using System.Collections.ObjectModel;
|
||||
using Patterns;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace Lab2
|
||||
{
|
||||
@@ -17,6 +20,11 @@ namespace Lab2
|
||||
/// </summary>
|
||||
private ObservableCollection<Tool> _tools = new();
|
||||
|
||||
/// <summary>
|
||||
/// Источник представлений для данных
|
||||
/// </summary>
|
||||
private CollectionViewSource _viewSource = new();
|
||||
|
||||
/// <summary>
|
||||
/// Случайный генератор
|
||||
/// </summary>
|
||||
@@ -27,9 +35,7 @@ namespace Lab2
|
||||
/// </summary>
|
||||
private List<IToolFactoryMethod> _factoryMethods = new()
|
||||
{
|
||||
new ScissorsAndTrimmerFactoryMethod(),
|
||||
new ScytheAndLawnmowerFactoryMethod(),
|
||||
new PrototypeFactoryMethod(),
|
||||
new ScissorsAndTrimmerFactoryMethod(), new ScytheAndLawnmowerFactoryMethod(), new PrototypeFactoryMethod(),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
@@ -60,39 +66,42 @@ namespace Lab2
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Фильтр по имени инструмента.
|
||||
/// </summary>
|
||||
public string NameFilter { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Минимальный вес для фильтрации инструментов.
|
||||
/// </summary>
|
||||
public int WeightMinFilter { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Максимальный вес для фильтрации инструментов.
|
||||
/// </summary>
|
||||
public int WeightMaxFilter { get; set; } = 1_000;
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор
|
||||
/// </summary>
|
||||
public MainWindow()
|
||||
{
|
||||
DataContext = this;
|
||||
InitializeComponent();
|
||||
|
||||
SelectedMethod = _factoryMethods.First();
|
||||
_viewSource.Source = _tools;
|
||||
|
||||
DataContext = this;
|
||||
_dataGrid.ItemsSource = _tools;
|
||||
_dataGrid.ItemsSource = _viewSource.View;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Проверяет, является ли индекс валидным
|
||||
/// Возвращает выбранный в таблице инструмент.
|
||||
/// </summary>
|
||||
/// <param name="parIndex"> Индекс </param>
|
||||
/// <returns> True, если индекс валиден, иначе - false </returns>
|
||||
private bool IsValidIndex(int parIndex)
|
||||
{
|
||||
return _tools.Count > 0 && parIndex >= 0 && parIndex < _tools.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Возвращает выбранный инструмент
|
||||
/// </summary>
|
||||
/// <returns> Выбранный инструмент </returns>
|
||||
/// <returns>Выбранный инструмент или null, если ничего не выбрано.</returns>
|
||||
private Tool? GetSelectedItem()
|
||||
{
|
||||
if (!IsValidIndex(_dataGrid.SelectedIndex))
|
||||
return null;
|
||||
|
||||
return _tools[_dataGrid.SelectedIndex];
|
||||
return _dataGrid.SelectedItem as Tool;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -101,7 +110,8 @@ namespace Lab2
|
||||
/// <returns>Случайный триммер</returns>
|
||||
private Trimmer GenerateTrimmer()
|
||||
{
|
||||
return new Trimmer($"Tr{_random.Next(0, 100)}", (Material)_random.Next(0, 3), _random.Next(1, 100), (uint)_random.Next(1, 100));
|
||||
return new Trimmer($"Tr{_random.Next(0, 100)}", (Material)_random.Next(0, 3), _random.Next(1, 100),
|
||||
(uint)_random.Next(1, 100));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -110,7 +120,8 @@ namespace Lab2
|
||||
/// <returns>Случайная газонокосилка</returns>
|
||||
private Lawnmower GenerateLawnmower()
|
||||
{
|
||||
return new Lawnmower($"La{_random.Next(0, 100)}", (Material)_random.Next(0, 3), _random.Next(1, 100), (uint)_random.Next(1, 100), _random.Next(1, 100));
|
||||
return new Lawnmower($"La{_random.Next(0, 100)}", (Material)_random.Next(0, 3), _random.Next(1, 100),
|
||||
(uint)_random.Next(1, 100), _random.Next(1, 100));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -119,7 +130,8 @@ namespace Lab2
|
||||
/// <returns>Случайные ножницы</returns>
|
||||
private Scissors GenerateScissors()
|
||||
{
|
||||
return new Scissors($"Si{_random.Next(0, 100)}", (Material)_random.Next(0, 3), _random.Next(1, 100), (uint)_random.Next(1, 100), _random.Next(1, 100));
|
||||
return new Scissors($"Si{_random.Next(0, 100)}", (Material)_random.Next(0, 3), _random.Next(1, 100),
|
||||
(uint)_random.Next(1, 100), _random.Next(1, 100));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -128,7 +140,41 @@ namespace Lab2
|
||||
/// <returns>Случайная коса</returns>
|
||||
private Scythe GenerateScythe()
|
||||
{
|
||||
return new Scythe($"Sc{_random.Next(0, 100)}", (Material)_random.Next(0, 3), _random.Next(1, 100), (uint)_random.Next(1, 100), (BladeType)_random.Next(0, 3));
|
||||
return new Scythe($"Sc{_random.Next(0, 100)}", (Material)_random.Next(0, 3), _random.Next(1, 100),
|
||||
(uint)_random.Next(1, 100), (BladeType)_random.Next(0, 3));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Генерирует список инструментов.
|
||||
/// </summary>
|
||||
/// <param name="parAmount">Количество инструментов для генерации.</param>
|
||||
/// <param name="parProgress">Прогресс выполнения.</param>
|
||||
/// <param name="parCancellationToken">Токен отмены задачи.</param>
|
||||
/// <returns>Сгенерированный список инструментов.</returns>
|
||||
private List<Tool> GenerateTools(int parAmount, IProgress<int> parProgress,
|
||||
CancellationToken parCancellationToken)
|
||||
{
|
||||
var tools = new List<Tool>();
|
||||
for (int i = 0; i < parAmount; i++)
|
||||
{
|
||||
if (parCancellationToken.IsCancellationRequested)
|
||||
break;
|
||||
|
||||
Dispatcher.Invoke(() => parProgress.Report(i));
|
||||
|
||||
Tool tool = _random.Next(0, 4) switch
|
||||
{
|
||||
0 => GenerateScissors(),
|
||||
1 => GenerateScythe(),
|
||||
2 => GenerateTrimmer(),
|
||||
3 => GenerateLawnmower(),
|
||||
_ => GenerateScissors(),
|
||||
};
|
||||
|
||||
tools.Add(tool);
|
||||
}
|
||||
|
||||
return tools;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -185,16 +231,73 @@ namespace Lab2
|
||||
/// </summary>
|
||||
private void GenerateButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Tool tool = _random.Next(0, 4) switch
|
||||
int generateAmount = 1_000_000;
|
||||
|
||||
var progressWindow =
|
||||
new ProgressWindow.ProgressBarWindow("Tool generation", "Please wait...", generateAmount, false);
|
||||
|
||||
IProgress<int> progress = new Progress<int>(i =>
|
||||
{
|
||||
0 => GenerateScissors(),
|
||||
1 => GenerateScythe(),
|
||||
2 => GenerateTrimmer(),
|
||||
3 => GenerateLawnmower(),
|
||||
_ => GenerateScissors(),
|
||||
if (i % 60 == 0)
|
||||
progressWindow.AdvanceProgress(60);
|
||||
|
||||
if (i == generateAmount - 1)
|
||||
progressWindow.AdvanceProgress(0, "Generation completed");
|
||||
});
|
||||
|
||||
var cancellationTokenSource = new CancellationTokenSource();
|
||||
progressWindow.Cancelled += (s, e) => cancellationTokenSource.Cancel();
|
||||
var task = new Task<List<Tool>>(() => GenerateTools(generateAmount, progress, cancellationTokenSource.Token));
|
||||
task.Start();
|
||||
|
||||
progressWindow.ShowDialog();
|
||||
|
||||
if (cancellationTokenSource.IsCancellationRequested)
|
||||
return;
|
||||
|
||||
var tools = task.Result;
|
||||
|
||||
tools.EnsureCapacity(tools.Count + generateAmount);
|
||||
|
||||
var filter = _viewSource.View.Filter;
|
||||
_viewSource.View.Filter = null;
|
||||
|
||||
foreach (var tool in tools)
|
||||
_tools.Add(tool);
|
||||
|
||||
_viewSource.View.Filter = filter;
|
||||
_viewSource.View.Refresh();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обработчик нажатия кнопки "Filter". Применяет фильтры к инструментам.
|
||||
/// </summary>
|
||||
private void FilterButton_OnClick(object parSender, RoutedEventArgs parE)
|
||||
{
|
||||
_viewSource.View.Filter = o =>
|
||||
{
|
||||
var tool = o as Tool;
|
||||
|
||||
if (!string.IsNullOrEmpty(NameFilter) && !tool.Name.Contains(NameFilter))
|
||||
return false;
|
||||
|
||||
if (WeightMinFilter > WeightMaxFilter)
|
||||
return false;
|
||||
|
||||
return tool.Weight >= WeightMinFilter && tool.Weight <= WeightMaxFilter;
|
||||
};
|
||||
|
||||
_tools.Add(tool);
|
||||
_dataGrid.ScrollIntoView(_dataGrid.SelectedItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обработчик нажатия кнопки "Clear Filter". Сбрасывает фильтры.
|
||||
/// </summary>
|
||||
private void ClearFilterButton_OnClick(object parSender, RoutedEventArgs parE)
|
||||
{
|
||||
_viewSource.View.Filter = null;
|
||||
|
||||
_dataGrid.ScrollIntoView(_dataGrid.SelectedItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ namespace Lab2
|
||||
/// Открывает форму редактирования
|
||||
/// </summary>
|
||||
/// <param name="parTool"> Инструмент </param>
|
||||
/// <param name="parReadOnly">Доступность полей формы</param>
|
||||
/// <param name="parIsForced">Признак невозможности отмены</param>
|
||||
/// <returns> True, если редактирование прошло успешно, иначе - false </returns>
|
||||
public static bool ShowEditForm(Tool parTool, bool parReadOnly = false, bool parIsForced = false)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user