Skip to content

Commit

Permalink
#205 SIQuester: show mark near correct answer option
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirKhil committed Feb 7, 2024
1 parent 84269de commit b61e00f
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 55 deletions.
6 changes: 6 additions & 0 deletions src/SIQuester/SIQuester.ViewModel/Helpers/IndexLabelHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SIQuester.ViewModel.Helpers;

internal static class IndexLabelHelper
{
internal static string GetIndexLabel(int index) => index < 26 ? ((char)('A' + index)).ToString() : 'A' + (index - 25).ToString();
}
105 changes: 62 additions & 43 deletions src/SIQuester/SIQuester.ViewModel/Items/QuestionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,66 +264,85 @@ private void SetQuestionType_Executed(object? arg)

private void SetAnswerType_Executed(object? arg)
{
if (Parameters == null || arg == null)
try
{
return;
}

var answerType = (string)arg;
var document = OwnerTheme?.OwnerRound?.OwnerPackage?.Document ?? throw new InvalidOperationException("document is undefined");

if (Parameters == null || arg == null)
{
return;
}

if (answerType == StepParameterValues.SetAnswerTypeType_Text)
{
// Default value; remove parameter
Parameters.RemoveParameter(QuestionParameterNames.AnswerType);
Parameters.RemoveParameter(QuestionParameterNames.AnswerOptions);
OnPropertyChanged(nameof(AnswerType));
return;
}
var answerType = (string)arg;

if (!Parameters.TryGetValue(QuestionParameterNames.AnswerType, out var answerTypeParameter))
{
answerTypeParameter = new StepParameterViewModel(this, new StepParameter
if (answerType == StepParameterValues.SetAnswerTypeType_Text)
{
Type = StepParameterTypes.Simple,
SimpleValue = answerType
});
// Default value; remove parameter
using var innerChange = document.OperationsManager.BeginComplexChange();

Parameters.AddParameter(QuestionParameterNames.AnswerType, answerTypeParameter);
}
else
{
answerTypeParameter.Model.SimpleValue = answerType;
}
Parameters.RemoveParameter(QuestionParameterNames.AnswerType);
Parameters.RemoveParameter(QuestionParameterNames.AnswerOptions);
OnPropertyChanged(nameof(AnswerType));

if (answerType == StepParameterValues.SetAnswerTypeType_Select)
{
var options = new StepParameter
innerChange.Commit();
return;
}

using var change = document.OperationsManager.BeginComplexChange();

if (!Parameters.TryGetValue(QuestionParameterNames.AnswerType, out var answerTypeParameter))
{
Type = StepParameterTypes.Group,
GroupValue = new StepParameters()
};
answerTypeParameter = new StepParameterViewModel(this, new StepParameter
{
Type = StepParameterTypes.Simple,
SimpleValue = answerType
});

static StepParameter answerOptionGenerator() => new()
Parameters.AddParameter(QuestionParameterNames.AnswerType, answerTypeParameter);
}
else
{
Type = StepParameterTypes.Content,
ContentValue = new List<ContentItem>
answerTypeParameter.Model.SimpleValue = answerType;
}

if (answerType == StepParameterValues.SetAnswerTypeType_Select)
{
var options = new StepParameter
{
Type = StepParameterTypes.Group,
GroupValue = new StepParameters()
};

static StepParameter answerOptionGenerator() => new()
{
Type = StepParameterTypes.Content,
ContentValue = new List<ContentItem>
{
new() { Type = ContentTypes.Text, Value = "" },
}
};
};

for (var i = 0; i < AppSettings.Default.SelectOptionCount; i++)
{
var label = i < 26 ? ((char)('A' + i)).ToString() : 'A' + (i - 25).ToString();
options.GroupValue.Add(label, answerOptionGenerator());
for (var i = 0; i < AppSettings.Default.SelectOptionCount; i++)
{
options.GroupValue.Add(IndexLabelHelper.GetIndexLabel(i), answerOptionGenerator());
}

var optionsViewModel = new StepParameterViewModel(this, options);

Parameters.AddParameter(QuestionParameterNames.AnswerOptions, optionsViewModel);
Right.ClearOneByOne();
Right.Add(IndexLabelHelper.GetIndexLabel(0));
Wrong.ClearOneByOne();
}

var optionsViewModel = new StepParameterViewModel(this, options);
change.Commit();

Parameters.AddParameter(QuestionParameterNames.AnswerOptions, optionsViewModel);
OnPropertyChanged(nameof(AnswerType));
}
catch (Exception exc)
{
PlatformSpecific.PlatformManager.Instance.Inform(exc.Message, true);
}

OnPropertyChanged(nameof(AnswerType));
}

private void SwitchEmpty_Executed(object? arg)
Expand Down
31 changes: 27 additions & 4 deletions src/SIQuester/SIQuester.ViewModel/StepParametersViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SIPackages;
using SIPackages.Core;
using SIQuester.ViewModel.Helpers;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
Expand All @@ -23,6 +24,10 @@ public sealed class StepParametersViewModel : ObservableCollection<StepParameter

public SimpleCommand DeleteItem { get; private set; }

public SimpleCommand MakeRight { get; private set; }

public QuestionViewModel Owner => _question;

public StepParametersViewModel(QuestionViewModel question, StepParameters parameters)
{
_question = question;
Expand All @@ -39,6 +44,7 @@ public StepParametersViewModel(QuestionViewModel question, StepParameters parame

AddItem = new SimpleCommand(AddItem_Executed);
DeleteItem = new SimpleCommand(DeleteItem_Executed);
MakeRight = new SimpleCommand(MakeRight_Executed);

UpdateCommands();

Expand Down Expand Up @@ -99,7 +105,7 @@ private void AddItem_Executed(object? arg)
}

var counter = Count;
var label = GetIndexLabel(counter);
var label = IndexLabelHelper.GetIndexLabel(counter);

var stepParameter = new StepParameter
{
Expand Down Expand Up @@ -130,13 +136,32 @@ private void DeleteItem_Executed(object? arg)

for (int i = 0; i < Count; i++)
{
this[i] = new StepParameterRecord(GetIndexLabel(i), this[i].Value);
this[i] = new StepParameterRecord(IndexLabelHelper.GetIndexLabel(i), this[i].Value);
}

UpdateCommands();
change.Commit();
}

private void MakeRight_Executed(object? arg)
{
var item = (StepParameterRecord?)arg;

if (!item.HasValue || _question.OwnerTheme?.OwnerRound?.OwnerPackage == null)
{
return;
}

if (_question.Right.Count == 0)
{
_question.Right.Add(item.Value.Key);
}
else
{
_question.Right[0] = item.Value.Key;
}
}

private void StepParametersViewModel_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
Expand Down Expand Up @@ -220,8 +245,6 @@ internal bool TryGetValue(string key, [NotNullWhen(true)] out StepParameterViewM
parameter = null;
return false;
}

private static string GetIndexLabel(int index) => index < 26 ? ((char)('A' + index)).ToString() : 'A' + (index - 25).ToString();
}

public record struct StepParameterRecord(string Key, StepParameterViewModel Value);
63 changes: 55 additions & 8 deletions src/SIQuester/SIQuester/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,10 @@
<Setter Property="ToolTip" Value="{x:Static lp:Resources.RemoveComplexAnswer}" />
<Setter Property="Content" Value="{StaticResource RemoveMarkerImage}" />
</MultiDataTrigger>

<DataTrigger Binding="{Binding AnswerType}" Value="select">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
Expand Down Expand Up @@ -3410,15 +3414,14 @@
<ItemsControl.ItemTemplate>
<DataTemplate>
<DockPanel Background="Transparent">
<Border
x:Name="delete"
Margin="0,2,0,0"
Visibility="Hidden">
<StackPanel Margin="0,2,0,0" Orientation="Horizontal">
<Button
x:Name="bDelete"
Command="{Binding DataContext.GroupValue.DeleteItem, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"
ToolTip="{x:Static lp:Resources.Delete}"
VerticalAlignment="Center">
VerticalAlignment="Center"
Visibility="Hidden">
<Path Fill="#00FFFFFF" Stroke="#FF989898" StrokeThickness="1" Data="M0,0L9,9M0,9L9,0" />

<Button.Style>
Expand All @@ -3431,7 +3434,28 @@
</Style>
</Button.Style>
</Button>
</Border>

<Button
x:Name="bSetRight"
Command="{Binding DataContext.GroupValue.MakeRight, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"
ToolTip="{x:Static lp:Resources.MakeRight}"
Margin="2,0,0,0"
VerticalAlignment="Center"
Visibility="Hidden">
<Image Source="{StaticResource AnswerTypeImage}" Width="14" Height="14" />

<Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="ToolTip" Value="" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>

<TextBlock
x:Name="name"
Expand Down Expand Up @@ -3460,12 +3484,28 @@

<DataTemplate.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter TargetName="delete" Property="Visibility" Value="Visible" />
<Setter TargetName="bDelete" Property="Visibility" Value="Visible" />
<Setter TargetName="bSetRight" Property="Visibility" Value="Visible" />
</Trigger>

<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="delete" Property="Visibility" Value="Visible" />
<Setter TargetName="bDelete" Property="Visibility" Value="Visible" />
<Setter TargetName="bSetRight" Property="Visibility" Value="Visible" />
</Trigger>

<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource EqualsMultiConverter}">
<MultiBinding.Bindings>
<Binding Path="Key" />
<Binding Path="DataContext.GroupValue.Owner.Right[0]" RelativeSource="{RelativeSource AncestorType=ItemsControl}" />
</MultiBinding.Bindings>
</MultiBinding>
</DataTrigger.Binding>

<Setter TargetName="bSetRight" Property="Visibility" Value="Visible" />
<Setter TargetName="bSetRight" Property="IsEnabled" Value="False" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
Expand Down Expand Up @@ -3733,6 +3773,7 @@
Text="(" Style="{StaticResource BlockStyleKey}"/>

<StackPanel
x:Name="answers"
MaxWidth="400"
Grid.Row="1"
Grid.Column="4"
Expand Down Expand Up @@ -3780,6 +3821,12 @@
<DataTrigger Binding="{Binding IsDragged}" Value="True">
<Setter Property="Opacity" Value="0.2" />
</DataTrigger>

<DataTrigger Binding="{Binding AnswerType}" Value="select">
<Setter TargetName="open" Property="Visibility" Value="Collapsed" />
<Setter TargetName="answers" Property="Visibility" Value="Collapsed" />
<Setter TargetName="close" Property="Visibility" Value="Collapsed" />
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>

Expand Down
9 changes: 9 additions & 0 deletions src/SIQuester/SIQuester/Properties/Resources.Designer.cs

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

3 changes: 3 additions & 0 deletions src/SIQuester/SIQuester/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,9 @@
<data name="MakeNonEmpty" xml:space="preserve">
<value>Make non-empty</value>
</data>
<data name="MakeRight" xml:space="preserve">
<value>Set as right answer</value>
</data>
<data name="MircScript" xml:space="preserve">
<value>mIRC Script</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions src/SIQuester/SIQuester/Properties/Resources.ru-RU.resx
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,9 @@
<data name="MakeNonEmpty" xml:space="preserve">
<value>Сделать непустым</value>
</data>
<data name="MakeRight" xml:space="preserve">
<value>Задать как правильный ответ</value>
</data>
<data name="MircScript" xml:space="preserve">
<value>Скрипт mIRC</value>
</data>
Expand Down

0 comments on commit b61e00f

Please sign in to comment.