Skip to content

Commit

Permalink
#116 SIQuester: allow to create package preview
Browse files Browse the repository at this point in the history
Remove outdated HTML exports
  • Loading branch information
VladimirKhil committed Apr 14, 2024
1 parent c14eb40 commit b74d746
Show file tree
Hide file tree
Showing 18 changed files with 287 additions and 1,206 deletions.
4 changes: 0 additions & 4 deletions src/SIQuester/SIQuester.ViewModel/Model/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ public enum ExportFormats
{
[Description("ExportFormatsDinabank")]
Dinabank,
[Description("ExportFormatsTvSI")]
TvSI,
[Description("ExportFormatsSns")]
Sns,
[Description("ExportFormatsDb")]
Db
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ protected PlatformManager()

public abstract Tuple<int, int, int>? GetCurrentItemSelectionArea();

/// <summary>
/// Creates document preview image and saves it to file.
/// </summary>
/// <param name="document">Document to process.</param>
public abstract void CreatePreview(SIDocument document);

public abstract string[]? ShowOpenUI();

public abstract string[]? ShowMediaOpenUI(string mediaCategory);
Expand All @@ -38,7 +44,13 @@ public abstract bool ShowSaveUI(
Dictionary<string, string>? filter,
[NotNullWhen(true)] ref string? filename);

public abstract bool ShowExportUI(string title, Dictionary<string, string> filter, ref string filename, ref int filterIndex, out Encoding encoding, out bool start);
public abstract bool ShowExportUI(
string title,
Dictionary<string, string> filter,
[NotNullWhen(true)] ref string? filename,
ref int filterIndex,
out Encoding encoding,
out bool start);

public abstract string? ShowImportUI(string fileExtension, string fileFilter);

Expand Down
43 changes: 43 additions & 0 deletions src/SIQuester/SIQuester.ViewModel/PreviewViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using SIPackages;

namespace SIQuester.ViewModel;

public sealed class PreviewViewModel
{
public SIDocument Document { get; private set; }

public SortedDictionary<string, int> Content { get; } = new();

public int QuestionCount { get; private set; }

public PreviewViewModel(SIDocument document)
{
Document = document;
var questionCount = 0;

foreach (var round in document.Package.Rounds)
{
foreach (var theme in round.Themes)
{
foreach (var question in theme.Questions)
{
questionCount++;

foreach (var contentItem in question.GetContent())
{
if (Content.TryGetValue(contentItem.Type, out var value))
{
Content[contentItem.Type] = value + 1;
}
else
{
Content[contentItem.Type] = 1;
}
}
}
}
}

QuestionCount = questionCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private async void Save_Executed(object? arg)
{
OnClosed();
await ExportAsync(filename, index, encoding);

if (start)
{
try
Expand Down
215 changes: 8 additions & 207 deletions src/SIQuester/SIQuester.ViewModel/Workspaces/QDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,37 +330,20 @@ public bool SearchFailed
public ICommand SaveAsTemplate { get; private set; }

/// <summary>
/// Экспорт в HTML
/// Exports package into preview image.
/// </summary>
public ICommand ExportHtml { get; private set; }
/// <summary>
/// Экспорт в HTML
/// </summary>
public ICommand ExportPrintHtml { get; private set; }
/// <summary>
/// Экспорт в HTML
/// </summary>
public ICommand ExportFormattedHtml { get; private set; }
public ICommand ExportPreview { get; private set; }

/// <summary>
/// Экспорт в HTML
/// Exports package to question database format.
/// </summary>
public ICommand ExportBase { get; private set; }
/// <summary>
/// Экспорт в HTML
/// </summary>
public ICommand ExportMirc { get; private set; }
/// <summary>
/// Экспорт в HTML
/// </summary>
public ICommand ExportTvSI { get; private set; }
/// <summary>
/// Экспорт в HTML
/// </summary>
public ICommand ExportSns { get; private set; }

/// <summary>
/// Экспорт в формат Динабанка
/// </summary>
public ICommand ExportDinabank { get; private set; }

/// <summary>
/// Экспорт в табличный формат
/// </summary>
Expand Down Expand Up @@ -1163,13 +1146,8 @@ internal QDocument(
SaveAs = new AsyncCommand(SaveAs_Executed);
SaveAsTemplate = new AsyncCommand(SaveAsTemplate_Executed);

ExportHtml = new SimpleCommand(ExportHtml_Executed);
ExportPrintHtml = new SimpleCommand(ExportPrintHtml_Executed);
ExportFormattedHtml = new SimpleCommand(ExportFormattedHtml_Executed);
ExportPreview = new SimpleCommand(ExportPreview_Executed);
ExportBase = new SimpleCommand(ExportBase_Executed);
ExportMirc = new SimpleCommand(ExportMirc_Executed);
ExportTvSI = new SimpleCommand(ExportTvSI_Executed);
ExportSns = new SimpleCommand(ExportSns_Executed);
ExportDinabank = new SimpleCommand(ExportDinabank_Executed);
ExportTable = new SimpleCommand(ExportTable_Executed);
ExportYaml = new SimpleCommand(ExportYaml_Executed);
Expand Down Expand Up @@ -1949,187 +1927,10 @@ internal void TransformPackage(string xslt)
}
}

private void ExportHtml_Executed(object? arg) =>
TransformPackage(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ygpackagekey3.0.xslt"));

private void ExportPrintHtml_Executed(object? arg) =>
TransformPackage(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ygpackagesimple3.0.xslt"));

private void ExportFormattedHtml_Executed(object? arg) => Dialog = new ExportHtmlViewModel(this);
private void ExportPreview_Executed(object? arg) => PlatformManager.Instance.CreatePreview(Document);

private void ExportBase_Executed(object? arg) => Dialog = new ExportViewModel(this, ExportFormats.Db);

private void ExportMirc_Executed(object? arg)
{
try
{
string? filename = string.Format("{0}IRCScriptFile", FileName.Replace(".", "-"));

var filter = new Dictionary<string, string>
{
[Resources.TextFiles] = "txt"
};

if (PlatformManager.Instance.ShowSaveUI(Resources.ToIRC, "txt", filter, ref filename))
{
var file = new StringBuilder();
file.AppendLine(Document.Package.Rounds.Count.ToString());

Document.Package.Rounds.ForEach(round =>
{
file.AppendLine(round.Themes.Count.ToString());
round.Themes.ForEach(theme => file.AppendLine(theme.Questions.Count.ToString()));
});

file.AppendLine();
file.AppendLine(Resources.ToIRCtext);
file.AppendLine();

int pind = 1, rind = 1, tind = 1, qind = 1;

file.AppendLine(string.Format("[p{0}name]", pind));
file.AppendLine(Document.Package.Name);

file.AppendLine(string.Format("[p{0}auth]", pind));
file.AppendLine(string.Join(Environment.NewLine, Document.GetRealAuthors(Document.Package.Info.Authors)));

file.AppendLine(string.Format("[p{0}sour]", pind));
file.AppendLine(string.Join(Environment.NewLine, Document.GetRealSources(Document.Package.Info.Sources)));

file.AppendLine(string.Format("[p{0}comm]", pind));
file.AppendLine(Document.Package.Info.Comments.Text.GrowFirstLetter().EndWithPoint());

Document.Package.Rounds.ForEach(round =>
{
file.AppendLine(string.Format("[r{0}name]", rind));
file.AppendLine(round.Name);
file.AppendLine(string.Format("[r{0}type]", rind));

if (round.Type == RoundTypes.Standart)
file.AppendLine(Resources.Simple);
else
file.AppendLine(Resources.Final);

file.AppendLine(string.Format("[r{0}auth]", rind));
file.AppendLine(string.Join(Environment.NewLine, Document.GetRealAuthors(round.Info.Authors)));
file.AppendLine(string.Format("[r{0}sour]", rind));
file.AppendLine(string.Join(Environment.NewLine, Document.GetRealSources(round.Info.Sources)));
file.AppendLine(string.Format("[r{0}comm]", rind));
file.AppendLine(round.Info.Comments.Text.GrowFirstLetter().EndWithPoint());

round.Themes.ForEach(theme =>
{
file.AppendLine(string.Format("[t{0}name]", tind));
file.AppendLine(theme.Name);
file.AppendLine(string.Format("[t{0}auth]", tind));
file.AppendLine(string.Join(Environment.NewLine, Document.GetRealAuthors(theme.Info.Authors)));
file.AppendLine(string.Format("[t{0}sour]", tind));
file.AppendLine(string.Join(Environment.NewLine, Document.GetRealSources(theme.Info.Sources)));
file.AppendLine(string.Format("[t{0}comm]", tind));
file.AppendLine(theme.Info.Comments.Text.GrowFirstLetter().EndWithPoint());

theme.Questions.ForEach(question =>
{
file.AppendLine(string.Format("[q{0}price]", qind));
file.AppendLine(question.Price.ToString());
file.AppendLine(string.Format("[q{0}type]", qind));
file.AppendLine(question.TypeName);

var qText = new StringBuilder();
var showmanComments = new StringBuilder();

foreach (var item in question.GetContent())
{
switch (item.Type)
{
case AtomTypes.Image:
if (showmanComments.Length > 0)
{
showmanComments.AppendLine();
}

showmanComments.Append($"* {Resources.MircImage}: ");
showmanComments.Append(item.Value);
break;

case AtomTypes.Audio:
case AtomTypes.AudioNew:
if (showmanComments.Length > 0)
{
showmanComments.AppendLine();
}

showmanComments.Append($"* {Resources.MircAudio}: ");
showmanComments.Append(item.Value);
break;

case AtomTypes.Video:
if (showmanComments.Length > 0)
{
showmanComments.AppendLine();
}

showmanComments.Append($"* {Resources.MircVideo}: ");
showmanComments.Append(item.Value);
break;

default:
if (qText.Length > 0)
{
qText.AppendLine();
}

qText.Append(item.Value);
break;
}
}

var comments = question.Info.Comments.Text.GrowFirstLetter().EndWithPoint();

if (showmanComments.Length == 0 || comments.Length > 0)
{
if (showmanComments.Length > 0)
showmanComments.AppendLine();

showmanComments.Append(comments);
}

file.AppendLine(string.Format("[q{0}text]", qind));
file.AppendLine(qText.ToString());
file.AppendLine(string.Format("[q{0}right]", qind));
file.AppendLine(string.Join(Environment.NewLine, question.Right.ToArray()));
file.AppendLine(string.Format("[q{0}wrong]", qind));
file.AppendLine(string.Join(Environment.NewLine, question.Wrong.ToArray()));
file.AppendLine(string.Format("[q{0}auth]", qind));
file.AppendLine(string.Join(Environment.NewLine, Document.GetRealAuthors(question.Info.Authors)));
file.AppendLine(string.Format("[q{0}sour]", qind));
file.AppendLine(string.Join(Environment.NewLine, Document.GetRealSources(question.Info.Sources)));
file.AppendLine(string.Format("[q{0}comm]", qind));
file.AppendLine(showmanComments.ToString());

qind++;
});

tind++;
});

rind++;
});

using var writer = new StreamWriter(filename, false);
writer.Write(file);
}
}
catch (Exception exc)
{
OnError(exc);
}
}

private void ExportTvSI_Executed(object? arg) => Dialog = new ExportViewModel(this, ExportFormats.TvSI);

private void ExportSns_Executed(object? arg) => Dialog = new ExportViewModel(this, ExportFormats.Sns);

private void ExportDinabank_Executed(object? arg) => Dialog = new ExportViewModel(this, ExportFormats.Dinabank);

private async void ExportTable_Executed(object? arg)
Expand Down
21 changes: 21 additions & 0 deletions src/SIQuester/SIQuester/Converters/ContentTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using SIPackages.Core;
using SIPackages.Properties;
using System.Globalization;
using System.Windows.Data;

namespace SIQuester.Converters;

public sealed class ContentTypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => value switch
{
ContentTypes.Text => Properties.Resources.Text,
ContentTypes.Image => Resources.Image,
ContentTypes.Audio => Resources.Audio,
ContentTypes.Video => Resources.Video,
ContentTypes.Html => Resources.Html,
_ => value
};

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
}
Loading

0 comments on commit b74d746

Please sign in to comment.