Skip to content

Commit

Permalink
Export to Word files
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwerty committed Jan 4, 2017
1 parent a963d23 commit fd11c84
Show file tree
Hide file tree
Showing 27 changed files with 910 additions and 325 deletions.
1 change: 1 addition & 0 deletions WpfApp/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
</connectionStrings>
<appSettings>
<add key="WebcamPath" value="webcam/Camera.exe" />
<add key="SettingsFilesEncoding" value="UTF-8"/>
</appSettings>
</configuration>
8 changes: 8 additions & 0 deletions WpfApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Configuration;
using System.IO;
using System.Windows;
using NLog;
using WpfApp.Framework;
using WpfApp.Settings;

namespace WpfApp
{
Expand Down Expand Up @@ -56,5 +58,11 @@ private static Uri GetFontsXamlUri()
Console.WriteLine(xaml);
return new Uri(xaml, UriKind.Relative);
}
public static void EnsureAllDirectories()
{
if (Directory.Exists(AppFilePaths.PersonImages)) return;
Directory.CreateDirectory(AppFilePaths.PersonImages);
Logger.Trace("Directories was created");
}
}
}
8 changes: 5 additions & 3 deletions WpfApp/Dictionaries/Converters.xaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converter="clr-namespace:WpfApp.View.Converter">
xmlns:converter="clr-namespace:WpfApp.View.Converter"
xmlns:settings="clr-namespace:WpfApp.Settings">

<converter:SexConverter x:Key="SexConverter"/>
<converter:ExpenseTypeConverter x:Key="ExpenseTypeConverter"/>
<!--GroupsConverter key is in ChildDetailsViewModel-->
<converter:GroupsConverter x:Key="GroupsConverter"
Nursery="Начинающая" Junior1="I младшая" Junior2="II младшая"
Middle="Средняя" Older="Старшая" Preparatory="Подготовительная" />

<converter:ImageConverter x:Key="ChildImageConverter" ImageType="Child" DefaultImage="../resources/no_picture.jpg" />
<converter:DateTimeConverter x:Key="DateConverter" Format="dd.MM.yyyy"/>
<converter:DateTimeConverter x:Key="TimeConverter" Format="HH:mm:ss"/>
<converter:DateTimeConverter x:Key="DateConverter" Format="{x:Static settings:OtherSettings.DateFormat}"/>
<converter:DateTimeConverter x:Key="TimeConverter" Format="{x:Static settings:OtherSettings.DateFormat}"/>
<converter:YearsOldConverter x:Key="YearsOldConverter"/>
<converter:ToBoolConverter x:Key="ToBoolConverter"/>
<converter:ToNegateBoolConverter x:Key="ToNegateBoolConverter"/>
Expand Down
4 changes: 4 additions & 0 deletions WpfApp/Dictionaries/Styles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
</Style>

<Style x:Key="PlaceHolderTextBox" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
Expand All @@ -89,6 +91,7 @@
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
MaxLength="{TemplateBinding MaxLength}"
IsTabStop="True"
Panel.ZIndex="2" Style="{x:Null}">
</TextBox>
<TextBox Name="PlaceTextBox" Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}"
Expand All @@ -97,6 +100,7 @@
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
IsTabStop="False"
Panel.ZIndex="1">
<TextBox.Style>
<Style TargetType="TextBox">
Expand Down
10 changes: 5 additions & 5 deletions WpfApp/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ public class EntryPoint
public static void Main(string[] args)
{
#if DEBUG
// for SQL Server
// if SQL Server
// Database.Load();
EFlogger.EntityFramework6.EFloggerFor6.Initialize();
AppFilePaths.CreateAllDirectories();
App.EnsureAllDirectories();
App.Main();
#else
try
{
// for SQL Server
// if SQL Server
// Database.Load();
AppFilePaths.CreateAllDirectories();
App.EnsureAllDirectories();
App.Main();
}
catch (Exception e)
{
App.Logger.Fatal(e, "Exception");
App.Logger.Fatal(e, "Fatal error in App.Main");
MessageBox.Show("В программе произошла ошибка, пожалуйста, свяжитесь с разработчиками...", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Warning);
}
#endif
Expand Down
1 change: 0 additions & 1 deletion WpfApp/Framework/ViewViewModelPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ private void StartLifeCycle(Window window, ViewModelBase viewModel, Pipe pipe)
sw.Start();
var conf = WindowStateSaver.ConfigureWindow(View.OriginalString, window, viewModel);
sw.Stop();
Console.WriteLine(sw.Elapsed);
bool closed = false;
window.Closed += (s, e) =>
{
Expand Down
125 changes: 109 additions & 16 deletions WpfApp/Settings/AppFilePaths.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,132 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using DAL.Model;
using Newtonsoft.Json;

namespace WpfApp.Settings
{
public static class AppFilePaths
{
private const string Resources = "resources";
private static string Images { get; } = Path.Combine(Resources, "images");
public static string PersonImages { get; } = Path.Combine(Images, "people");
public static string ChildImages { get; } = PersonImages;
public static string ParentImages { get; } = PersonImages;
public static readonly string NoImage = Resources + Path.DirectorySeparatorChar + "no_picture.jpg";
public static readonly string Documents = Path.Combine(Resources, "documents");
public static readonly string DocTemplates = Path.Combine(Resources, "templates");

public static void CreateAllDirectories()
private const string DefaultResourceStructureFile = "resources/ru_default_resource_structure.json";
private static readonly string ResourceStructurePath = Resources + Path.DirectorySeparatorChar + "resource_structure.json";
public static IDocumentSettings MyDocumentSettings { get; }
public static string PersonImages { get; }
public static string ChildImages { get; }
public static string ParentImages { get; }
public static readonly string DocTemplates = Resources + Path.DirectorySeparatorChar + "templates";

public static string GetDocumentsDirectoryPathForChild(Child child, int year)
{
Directory.CreateDirectory(PersonImages);
App.Logger.Trace("Directories created");
var c = Path.DirectorySeparatorChar;
return Resources + c + MyDocumentSettings.Documents + c + year + c + child.Id;
}

public static string GetDocumentsDirecoryPathForChild(Child child, int year)
/// <summary>Returns relative path of template</summary>
private static string GetDocumentName(string document)
{
return Resources + Path.DirectorySeparatorChar +
MyDocumentSettings.Templates + Path.DirectorySeparatorChar +
MyDocumentSettings.FilenameOfDocumentsAndTemplates[document][0];
}
/// <summary>Returns file name of child's document</summary>
private static string GetDocumentName(string document, int childId)
{
return Path.Combine(Documents, year.ToString(), child.Id.ToString());
return string.Format(MyDocumentSettings.FilenameOfDocumentsAndTemplates[document][1], childId);
}

public static string GetAddedChildDocumentFileName(Child addedChild)
public static string GetAgreementFileName(Child child) => GetDocumentName("agreement", child.Id);
public static string GetAgreementTemplatePath() => GetDocumentName("agreement");

public static string GetPortfolioFileName(Child child) => GetDocumentName("portfolio", child.Id);
public static string GetPortfolioTemplatePath() => GetDocumentName("portfolio");

public static string GetTakingChildFileName(Child child) => GetDocumentName("taking_child", child.Id);

public static string GetTakingChildTemplatePath() => GetDocumentName("taking_child");

public static string GetOrderOfAdmissionFileName(Child child) => GetDocumentName("order_of_admission", child.Id);
public static string GetOrderOfAdmissionFileName(Child child, DateTime enterDate) =>
string.Format(MyDocumentSettings.FilenameOfDocumentsAndTemplates["order_of_admission"][1], child.Id, "_" + enterDate.ToString(OtherSettings.DateFormat));
public static string GetOrderOfAdmissionTemplatePath() => GetDocumentName("order_of_admission");

public static string GetNoticeFileName() => MyDocumentSettings.FilenameOfDocumentsAndTemplates["notices"][1];
public static string GetNoticeTemplatePath() => GetDocumentName("notices");

public static string GetMonthlyReceiptFileName(Child child) => GetDocumentName("monthly_receipt", child.Id);
public static string GetMonthlyReceiptTemplatePath() => GetDocumentName("monthly_receipt");

public static string GetAnnualReceiptFileName(Child child) => GetDocumentName("annual_receipt", child.Id);
public static string GetAnnualReceiptTemplatePath() => GetDocumentName("annual_receipt");

public static string GetAddingToArchiveFileName(Child child) => GetDocumentName("adding_to_archive", child.Id);
public static string GetAddingToArchiveFileName(Child child, DateTime expultionDate) =>
string.Format(MyDocumentSettings.FilenameOfDocumentsAndTemplates["adding_to_archive"][1], child.Id, "_" + expultionDate.ToString(OtherSettings.DateFormat));
public static string GetAddingToArchiveTemplatePath() => GetDocumentName("adding_to_archive");

public static string GetGroupTransferFileName(Child child) => GetDocumentName("transfer_group", child.Id);
public static string GetGroupTransferFileName(Child child, DateTime expultionDate, string groupName, string groupType) =>
string.Format(MyDocumentSettings.FilenameOfDocumentsAndTemplates["transfer_group"][1],
child.Id,
expultionDate.ToString(OtherSettings.DateFormat),
groupName,
groupType);
public static string GetGroupTransferTemplatePath() => GetDocumentName("transfer_group");

public static string GetGroupTypeChangedFileName() =>
MyDocumentSettings.FilenameOfDocumentsAndTemplates["group_type_changed"][1];
public static string GetGroupTypeChangedTemplatePath() => GetDocumentName("group_type_changed");


static AppFilePaths()
{
return addedChild.Id + "_added.docx";
DocumentSettings docSettings = null;
if (File.Exists(ResourceStructurePath))
{
var json = File.ReadAllText(ResourceStructurePath, OtherSettings.Encoding);
try
{
docSettings = JsonConvert.DeserializeObject<DocumentSettings>(json);
}
catch (Exception e)
{
App.Logger.Warn(e, $"Cannot deserialize \"{ResourceStructurePath}\"");
}
}
if (docSettings == null)
{
var defRes = new Uri(DefaultResourceStructureFile, UriKind.Relative);
string defJson;
// ReSharper disable once PossibleNullReferenceException
using (var stream = new StreamReader(Application.GetResourceStream(defRes).Stream))
defJson = stream.ReadToEnd();
App.Logger.Info("Default resource structure file initialized + \"" + DefaultResourceStructureFile + "\"");

docSettings = JsonConvert.DeserializeObject<DocumentSettings>(defJson);

var settings = JsonConvert.SerializeObject(docSettings, Formatting.Indented);
File.WriteAllText(ResourceStructurePath, settings, OtherSettings.Encoding);
}
MyDocumentSettings = docSettings;
ParentImages = ChildImages = PersonImages = Path.Combine(Resources, MyDocumentSettings.Images, "people");
}

public static string GetAddedChildDocumentTemplatePath()
public interface IDocumentSettings
{
string Documents { get; set; }
string Images { get; set; }
string Templates { get; set; }
IDictionary<string, string[]> FilenameOfDocumentsAndTemplates { get; set; }
}
public class DocumentSettings : IDocumentSettings
{
return Path.Combine(DocTemplates, "child_added.docx");
public string Images { get; set; }
public string Documents { get; set; }
public string Templates { get; set; }
public IDictionary<string, string[]> FilenameOfDocumentsAndTemplates { get; set; }
}
}
}
24 changes: 24 additions & 0 deletions WpfApp/Settings/OtherSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Configuration;
using System.Text;

namespace WpfApp.Settings
{
public static class OtherSettings
{
public const string DateFormat = "dd.MM.yyyy";
public const string TimeFormat = "HH:mm:ss";
/// <summary>Encoding of settings file (see AppConfig)</summary>
public static readonly Encoding Encoding;

public static string Str(this double value)
{
return value.ToString("0.#");
}

static OtherSettings()
{
var encodingStr = ConfigurationManager.AppSettings["SettingsFilesEncoding"];
Encoding = encodingStr == null ? Encoding.UTF8 : Encoding.GetEncoding(encodingStr);
}
}
}
30 changes: 30 additions & 0 deletions WpfApp/Util/CommonHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;

namespace WpfApp.Util
{
Expand Down Expand Up @@ -26,6 +28,23 @@ public static string GetUniqueString()
}
}

/// <example>file.txt => file (i).txt;</example>>
public static string ChangeFileNameIfFileExists(string path, int i = 2)
{
if (!File.Exists(path)) return path;

int dotPos = path.LastIndexOf('.');
string file = path.Substring(0, dotPos); // dir/file
string ext = path.Substring(dotPos); // .txt

string p;
do
{
p = $"{file} ({i++}){ext}";
} while (File.Exists(p));
return p;
}

public static string GetRightRussianWord(int num, string s1, string s2, string s5)
{
var n100 = num % 100;
Expand All @@ -36,5 +55,16 @@ public static string GetRightRussianWord(int num, string s1, string s2, string s
if (n10 > 1 && n10 < 5) return s2;
return s5;
}

public static void OpenFileOrDirectory(string path)
{
// #if OS_is_Windows
Process.Start("explorer.exe", $"\"{path}\"");
}
public static void OpenFileOrDirectoryWithSelected(string path)
{
// #if OS_is_Windows
Process.Start("explorer.exe", $"/select, \"{path}\"");
}
}
}
Loading

0 comments on commit fd11c84

Please sign in to comment.