Skip to content

Commit

Permalink
#93 Add ability to compress images + option to set right answer based…
Browse files Browse the repository at this point in the history
… on file name
  • Loading branch information
VladimirKhil committed Nov 12, 2023
1 parent 5f69fa5 commit f8f9808
Show file tree
Hide file tree
Showing 13 changed files with 239 additions and 21 deletions.
21 changes: 17 additions & 4 deletions src/SIQuester/SIQuester.ViewModel/ContentItemsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SIPackages;
using SIPackages.Core;
using SIQuester.Model;
using SIQuester.ViewModel.PlatformSpecific;
using SIQuester.ViewModel.Properties;
using System.Collections.Specialized;
Expand Down Expand Up @@ -466,21 +467,33 @@ private bool AddAtomObject(string mediaType)
index = -1;
}

var atom = new ContentItemViewModel(new ContentItem
var contentItem = new ContentItemViewModel(new ContentItem
{
Type = mediaType,
Value = "",
Placement = mediaType == ContentTypes.Audio ? ContentPlacements.Background : ContentPlacements.Screen
});

Insert(index + 1, atom);
Insert(index + 1, contentItem);

var last = collection.Files.LastOrDefault();

if (last != null)
{
atom.Model.IsRef = true;
atom.Model.Value = last.Model.Name;
contentItem.Model.IsRef = true;
contentItem.Model.Value = last.Model.Name;

if (AppSettings.Default.SetRightAnswerFromFileName)
{
var question = Owner;

if (question.Right.Last().Length == 0)
{
question.Right.RemoveAt(question.Right.Count - 1);
}

question.Right.Add(Path.GetFileNameWithoutExtension(last.Model.Name));
}
}

document.ActiveItem = null;
Expand Down
21 changes: 21 additions & 0 deletions src/SIQuester/SIQuester.ViewModel/Model/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,25 @@ public bool CheckFileSize
}
}

private bool _setRightAnswerFromFileName = false;

/// <summary>
/// Sets right answer when media file is added to question.
/// </summary>
[DefaultValue(false)]
public bool SetRightAnswerFromFileName
{
get => _setRightAnswerFromFileName;
set
{
if (_setRightAnswerFromFileName != value)
{
_setRightAnswerFromFileName = value;
OnPropertyChanged();
}
}
}

private string? _language = null;

/// <summary>
Expand Down Expand Up @@ -559,5 +578,7 @@ internal void Reset()
_flatScale = defaultSettings._flatScale;
FlatLayoutMode = defaultSettings.FlatLayoutMode;
SelectOptionCount = DefaultSelectOptionCount;
CheckFileSize = defaultSettings.CheckFileSize;
SetRightAnswerFromFileName = defaultSettings.SetRightAnswerFromFileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ public abstract bool ShowSaveUI(
public abstract bool ConfirmExclWithWindow(string message);

public abstract void Exit();

public abstract string CompressImage(string imageUri);
}
2 changes: 1 addition & 1 deletion src/SIQuester/SIQuester.ViewModel/Workspaces/QDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ internal QDocument(

var msvmLogger = loggerFactory.CreateLogger<MediaStorageViewModel>();

Images = new MediaStorageViewModel(this, Document.Images, Resources.Images, msvmLogger);
Images = new MediaStorageViewModel(this, Document.Images, Resources.Images, msvmLogger, true);
Audio = new MediaStorageViewModel(this, Document.Audio, SIPackages.Properties.Resources.Audio, msvmLogger);
Video = new MediaStorageViewModel(this, Document.Video, SIPackages.Properties.Resources.Video, msvmLogger);
Html = new MediaStorageViewModel(this, Document.Html, SIPackages.Properties.Resources.Html, msvmLogger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using SIPackages;
using SIPackages.Core;
using SIQuester.ViewModel.Model;
using SIQuester.ViewModel.PlatformSpecific;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
Expand Down Expand Up @@ -90,6 +91,11 @@ public MediaItemViewModel? CurrentFile

public ICommand DeleteItem { get; private set; }

/// <summary>
/// Compresses media item.
/// </summary>
public ICommand CompressItem { get; private set; }

private readonly string _header;

private readonly string _name;
Expand Down Expand Up @@ -120,7 +126,7 @@ public string Filter
}
}

public MediaStorageViewModel(QDocument document, DataCollection collection, string header, ILogger<MediaStorageViewModel> logger)
public MediaStorageViewModel(QDocument document, DataCollection collection, string header, ILogger<MediaStorageViewModel> logger, bool canCompress = false)
{
_document = document;
_header = header;
Expand All @@ -131,6 +137,7 @@ public MediaStorageViewModel(QDocument document, DataCollection collection, stri

AddItem = new SimpleCommand(AddItem_Executed);
DeleteItem = new SimpleCommand(Delete_Executed);
CompressItem = new SimpleCommand(CompressItem_Executed) { CanBeExecuted = canCompress };
}

private void FillFiles(DataCollection collection)
Expand Down Expand Up @@ -167,7 +174,7 @@ private void Named_PropertyChanged(object? sender, PropertyChangedEventArgs e)

var newValue = item.Name;
var renamedExisting = !_added.Any(mi => mi.Model == item);
Tuple<string, string> tuple = null;
Tuple<string, string>? tuple = null;

if (renamedExisting)
{
Expand Down Expand Up @@ -286,25 +293,88 @@ private void Delete_Executed(object? arg)
}
}

private void PreviewRemove(MediaItemViewModel name)
private void CompressItem_Executed(object? arg)
{
if (!Files.Contains(name))
if (arg == null)
{
return;
}

if (_added.Contains(name))
var item = (MediaItemViewModel)arg;

if (item.MediaSource == null)
{
_added.Remove(name);
_removedStreams.Add(name, _streams[name]);
_streams.Remove(name);
return;
}

var sourceUri = item.MediaSource.Uri;
var newUri = PlatformManager.Instance.CompressImage(sourceUri);

if (newUri != sourceUri)
{
var newItem = CreateItem(item.Name);
var currentIndex = Files.IndexOf(item);

var returnToCurrent = CurrentFile == item;

PreviewRemove(item);
PreviewAdd(newItem, newUri, currentIndex);
HasPendingChanges = IsChanged();

if (returnToCurrent)
{
CurrentFile = newItem;
}

OnChanged(new CustomChange(
() =>
{
var returnToCurrent = CurrentFile == newItem;

PreviewRemove(newItem);
PreviewAdd(item, sourceUri);
HasPendingChanges = IsChanged();

if (returnToCurrent)
{
CurrentFile = item;
}
},
() =>
{
var returnToCurrent = CurrentFile == item;

PreviewRemove(item);
PreviewAdd(newItem, newUri);
HasPendingChanges = IsChanged();

if (returnToCurrent)
{
CurrentFile = newItem;
}
}));
}
}

private void PreviewRemove(MediaItemViewModel item)
{
if (!Files.Contains(item))
{
return;
}

if (_added.Contains(item))
{
_added.Remove(item);
_removedStreams.Add(item, _streams[item]);
_streams.Remove(item);
}
else
{
_removed.Add(name);
_removed.Add(item);
}

Files.Remove(name);
Files.Remove(item);
OnPropertyChanged(nameof(Files));
}

Expand Down Expand Up @@ -388,7 +458,7 @@ public async Task ApplyToAsync(DataCollection collection, bool final = false)

private void AddItem_Executed(object? arg)
{
var files = PlatformSpecific.PlatformManager.Instance.ShowMediaOpenUI(_name);
var files = PlatformManager.Instance.ShowMediaOpenUI(_name);

if (files == null)
{
Expand Down Expand Up @@ -440,7 +510,7 @@ public MediaItemViewModel AddFile(string file, string? name = null)
return item;
}

private void PreviewAdd(MediaItemViewModel item, string path)
private void PreviewAdd(MediaItemViewModel item, string path, int index = -1)
{
if (_removed.Contains(item))
{
Expand All @@ -463,9 +533,22 @@ private void PreviewAdd(MediaItemViewModel item, string path)

_added.Add(item);
_streams[item] = Tuple.Create(path, fileStream);

if (_removedStreams.ContainsKey(item))
{
_removedStreams.Remove(item);
}
}

if (index == -1)
{
Files.Add(item);
}
else
{
Files.Insert(index, item);
}

Files.Add(item);
OnPropertyChanged(nameof(Files));

HasPendingChanges = IsChanged();
Expand Down
50 changes: 50 additions & 0 deletions src/SIQuester/SIQuester/Implementation/DesktopManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shell;
using System.Windows.Xps.Packaging;
using System.Windows.Xps.Serialization;
Expand Down Expand Up @@ -1024,6 +1025,55 @@ public override bool ConfirmExclWithWindow(string message)

public override void Exit() => Application.Current.MainWindow?.Close();

public override string CompressImage(string imageUri)
{
var extension = Path.GetExtension(imageUri);

if (extension != ".jpg" && extension != ".png")
{
return imageUri;
}

var bitmapImage = new BitmapImage(new Uri(imageUri));

var width = bitmapImage.PixelWidth;
var height = bitmapImage.PixelHeight;

const double TargetPixelSize = 800.0;
const int TargetQualityLevel = 90;

if (width <= TargetPixelSize && height <= TargetPixelSize)
{
return imageUri;
}

var widthScale = TargetPixelSize / width;
var heightScale = TargetPixelSize / height;

var scale = Math.Min(widthScale, heightScale);

var resizedImage = new TransformedBitmap(bitmapImage, new ScaleTransform(scale, scale));

BitmapEncoder encoder = extension == ".jpg" ? new JpegBitmapEncoder
{
QualityLevel = TargetQualityLevel
} : new PngBitmapEncoder();

encoder.Frames.Add(BitmapFrame.Create(resizedImage));

var fileName = Path.GetFileName(imageUri);
var outputDir = Path.Combine(Path.GetTempPath(), AppSettings.ProductName, AppSettings.MediaFolderName, Guid.NewGuid().ToString());
Directory.CreateDirectory(outputDir);
var outputPath = Path.Combine(outputDir, fileName);

using (var fileStream = new FileStream(outputPath, FileMode.Create))
{
encoder.Save(fileStream);
}

return outputPath;
}

public void Dispose()
{

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.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,9 @@
<data name="SetLogo" xml:space="preserve">
<value>Set logo</value>
</data>
<data name="SetRightAnswerFromFileName" xml:space="preserve">
<value>Set right answer by name of file which is added to question</value>
</data>
<data name="SetTags" xml:space="preserve">
<value>Set tags</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions src/SIQuester/SIQuester/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,9 @@
<data name="SetLogo" xml:space="preserve">
<value>Задать логотип</value>
</data>
<data name="SetRightAnswerFromFileName" xml:space="preserve">
<value>Задавать правильный ответ по имени файла, добавляемого в вопрос</value>
</data>
<data name="SetTags" xml:space="preserve">
<value>Задать теги</value>
</data>
Expand Down
12 changes: 12 additions & 0 deletions src/SIQuester/SIQuester/View/FlatDocView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,18 @@ private void TryImportMedia(DragEventArgs e, string filePath, string mediaType)
Value = item.Model.Name,
Placement = contentType == ContentTypes.Audio ? ContentPlacements.Background : ContentPlacements.Screen
}));

if (AppSettings.Default.SetRightAnswerFromFileName)
{
var question = contentItemsViewModel.Owner;

if (question.Right.Last().Length == 0)
{
question.Right.RemoveAt(question.Right.Count - 1);
}

question.Right.Add(Path.GetFileNameWithoutExtension(item.Model.Name));
}
}

internal static void RecountPrices(Theme theme, int pos, bool down)
Expand Down
Loading

0 comments on commit f8f9808

Please sign in to comment.