Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if FORMAT_STYLE
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Xml;
- using VL_Library.Helpers;
- namespace VL_UI.StyleTemplate
- {
- public class ViewTemplateStyleModel
- {
- public ViewTemplateStyleModel()
- {
- _styleTemplateList = new BindingList<StyleTemplate>();
- _window = null;
- SaveStyleTemplateCommand = new RelayCommand(new Action<object>(SaveStyleTemplatePress));
- AddNewStyleTemplateCommand = new RelayCommand(new Action<object>(AddStyleTemplatePress));
- CloseWindowCommand = new RelayCommand(new Action<object>(CloseWindowPress));
- ReloadStylesCommand = new RelayCommand(new Action<object>(ReloadStylePress));
- LoadStyleTemplate();
- InitEmptyStyleList();
- }
- public void SetWindow(StyleTemplateWindow window)
- {
- _window = window;
- }
- public BindingList<StyleTemplate> StyleTemplateList
- {
- get { return _styleTemplateList; }
- }
- public Dictionary<Key, StyleTemplate> StyleKeys
- {
- get { return _styleKeys; }
- }
- public ICommand SaveStyleTemplateCommand
- {
- get;
- set;
- }
- public ICommand AddNewStyleTemplateCommand
- {
- get;
- set;
- }
- public ICommand CloseWindowCommand
- {
- get;
- set;
- }
- public ICommand ReloadStylesCommand
- {
- get;
- set;
- }
- public void LoadStyleTemplate()
- {
- bool error = false;
- string fileName = VL_Library.Properties.Settings.Default.userDataPath + "\\templateStyle.xml";
- if (System.IO.File.Exists(fileName))
- {
- StreamEncryption stream = new StreamEncryption();
- if (stream.OpenStream(fileName, System.IO.FileAccess.Read, System.IO.FileMode.Open, false))
- {
- try
- {
- using (XmlTextReader reader = new XmlTextReader(stream.DataStream))
- {
- while (reader.Read())
- {
- if (reader.NodeType == XmlNodeType.Element && reader.Name == "style")
- {
- string id = reader.GetAttribute("id");
- string key = reader.GetAttribute("key");
- string sygnature = reader.GetAttribute("sygnature");
- string color = reader.GetAttribute("color");
- string bold = reader.GetAttribute("bold");
- string italic = reader.GetAttribute("italic");
- string underline = reader.GetAttribute("underline");
- string newline = reader.GetAttribute("newline");
- string alignleft = reader.GetAttribute("alignleft");
- string alignright = reader.GetAttribute("alignright");
- string aligncenter = reader.GetAttribute("aligncenter");
- string alignjustify = reader.GetAttribute("alignjustify");
- string fullstop = reader.GetAttribute("fullstop");
- string exclamationmark = reader.GetAttribute("exclamationmark");
- string questinmark = reader.GetAttribute("questionmark");
- Key tmpKey = (Key)Enum.Parse(typeof(Key), key, true);
- StyleTemplate tmpStyle = null;
- if(_styleKeys != null)
- tmpStyle = new StyleTemplate(tmpKey, _styleKeys.Count);
- else
- tmpStyle = new StyleTemplate(tmpKey, 0);
- tmpStyle.Number = id;
- tmpStyle.Sygnature = sygnature;
- tmpStyle.SelectedColor = tmpStyle.ColorStyle[Convert.ToInt32(color)];
- tmpStyle.Bold = Convert.ToBoolean(bold);
- tmpStyle.Italic = Convert.ToBoolean(italic);
- tmpStyle.Underline = Convert.ToBoolean(underline);
- tmpStyle.FullStop = Convert.ToBoolean(fullstop);
- tmpStyle.ExclamationMark = Convert.ToBoolean(exclamationmark);
- tmpStyle.QuestionMark = Convert.ToBoolean(questinmark);
- tmpStyle.NewLine = Convert.ToBoolean(newline);
- tmpStyle.AlignLeft = Convert.ToBoolean(alignleft);
- tmpStyle.AlignRight = Convert.ToBoolean(alignright);
- tmpStyle.AlignCenter = Convert.ToBoolean(aligncenter);
- tmpStyle.AlignJustify = Convert.ToBoolean(alignjustify);
- AddStyleTemplate(tmpKey, tmpStyle);
- }
- }
- }
- }
- catch (Exception e)
- {
- ErrorHandling.Instance.ErrorOccurred(e, "Nie można otworzyć pliku: " + fileName + "!\nNiektóre funkcje mogą nie działać poprawnie!\nCzy chcesz usunąć uszkodzony plik?", "", ErrorTypeEnum.ERROR_MSG,
- (Action)(() => { System.IO.File.Delete(fileName); }),
- null, null, NotificationButtonEnum.YesNo);
- error = true;
- }
- finally
- {
- stream.CloseStream(error);
- }
- }
- else
- {
- if (stream.Error.Message.Contains("Wrong HASH"))
- ErrorHandling.Instance.ErrorOccurred(stream.Error, "Nie można otworzyć pliku: " + fileName + "!\nNiektóre funkcje mogą nie działać poprawnie!\nCzy chcesz usunąć plik?",
- "", ErrorTypeEnum.ERROR_MSG,
- (Action)(() => { System.IO.File.Delete(fileName); }),
- null, null, NotificationButtonEnum.YesNo);
- else
- ErrorHandling.Instance.ErrorOccurred(stream.Error, "Nie można otworzyć pliku: " + fileName + "! Niektóre funkcje mogą nie działać poprawnie!", "",
- ErrorTypeEnum.ERROR_MSG, null, null, null, NotificationButtonEnum.OK);
- }
- }
- else
- {
- ErrorHandling.Instance.ErrorOccurred(null, "Plik: " + fileName + " nie istnieje!", "",
- ErrorTypeEnum.ERROR_CONTINUE, null, null, null, NotificationButtonEnum.OK);
- }
- }
- public void SaveStyleTemplate()
- {
- bool error = false;
- string fileName = VL_Library.Properties.Settings.Default.userDataPath + "\\templateStyle.xml";
- StreamEncryption stream = new StreamEncryption();
- if (stream.OpenStream(fileName, System.IO.FileAccess.Write, System.IO.FileMode.Create, false))
- {
- XmlWriterSettings settings = new XmlWriterSettings();
- settings.Indent = true;
- settings.IndentChars = "\t";
- settings.NewLineOnAttributes = true;
- XmlWriter writer = null;
- try
- {
- using (writer = XmlWriter.Create(stream.DataStream, settings))
- {
- writer.WriteStartElement("root");
- {
- writer.WriteStartElement("styles");
- {
- foreach (StyleTemplate tmp in _styleTemplateList)
- {
- writer.WriteStartElement("style");
- writer.WriteAttributeString("id", tmp.Number);
- writer.WriteAttributeString("key", tmp.KeyboardShortcut);
- writer.WriteAttributeString("sygnature", tmp.Sygnature);
- writer.WriteAttributeString("color", tmp.ColorStyle.IndexOf(tmp.SelectedColor).ToString());
- writer.WriteAttributeString("bold", tmp.Bold.ToString());
- writer.WriteAttributeString("italic", tmp.Italic.ToString());
- writer.WriteAttributeString("underline", tmp.Underline.ToString());
- writer.WriteAttributeString("fullstop", tmp.FullStop.ToString());
- writer.WriteAttributeString("questionmark", tmp.QuestionMark.ToString());
- writer.WriteAttributeString("exclamationmark", tmp.ExclamationMark.ToString());
- writer.WriteAttributeString("newline", tmp.NewLine.ToString());
- writer.WriteAttributeString("alignleft", tmp.AlignLeft.ToString());
- writer.WriteAttributeString("alignright", tmp.AlignRight.ToString());
- writer.WriteAttributeString("aligncenter", tmp.AlignCenter.ToString());
- writer.WriteAttributeString("alignjustify", tmp.AlignJustify.ToString());
- writer.WriteEndElement();
- }
- }
- writer.WriteEndElement();
- }
- writer.WriteEndElement();
- writer.Flush();
- }
- }
- catch (Exception e)
- {
- ErrorHandling.Instance.ErrorOccurred(e, "Błąd przy zapisie do pliku: " + fileName + "!", "",
- ErrorTypeEnum.ERROR_MSG, null, null, null, NotificationButtonEnum.OK);
- error = true;
- }
- finally
- {
- if (writer != null)
- writer.Close();
- stream.CloseStream(error);
- }
- }
- else
- {
- ErrorHandling.Instance.ErrorOccurred(stream.Error, "Błąd przy zapisie do pliku: " + fileName + "!", "",
- ErrorTypeEnum.ERROR_MSG, null, null, null, NotificationButtonEnum.OK);
- }
- }
- private void InitEmptyStyleList()
- {
- while (_styleTemplateList.Count < 7)
- AddNewStyleTemplate();
- }
- private bool AddNewStyleTemplate()
- {
- if (_styleTemplateList.Count() <= 6)
- {
- int elementNum = _styleTemplateList.Count() + 2;
- string shortKeyString = "F" + elementNum;
- Key tmpKey = (Key)Enum.Parse(typeof(Key), shortKeyString, true);
- return AddStyleTemplate(tmpKey);
- }
- return false;
- }
- private void ReloadStylePress(object obj)
- {
- _styleKeys.Clear();
- _styleTemplateList.Clear();
- LoadStyleTemplate();
- InitEmptyStyleList();
- }
- private void CloseWindowPress(object obj)
- {
- _window.Close();
- }
- private void AddStyleTemplatePress(object obj)
- {
- AddNewStyleTemplate();
- }
- private void SaveStyleTemplatePress(object obj)
- {
- SaveStyleTemplate();
- }
- private bool AddStyleTemplate(Key shortCut)
- {
- if (_styleKeys == null)
- _styleKeys = new Dictionary<Key, StyleTemplate>();
- if (_styleKeys.ContainsKey(shortCut))
- return false;
- _styleKeys.Add(shortCut, new StyleTemplate(shortCut, _styleKeys.Count));
- _styleTemplateList.Add(_styleKeys[shortCut]);
- return true;
- }
- private bool AddStyleTemplate(Key shortCut, StyleTemplate style)
- {
- if (_styleKeys == null)
- _styleKeys = new Dictionary<Key, StyleTemplate>();
- if (_styleKeys.ContainsKey(shortCut))
- return false;
- _styleKeys.Add(shortCut, style);
- _styleTemplateList.Add(style);
- return true;
- }
- private BindingList<StyleTemplate> _styleTemplateList;
- private Dictionary<Key, StyleTemplate> _styleKeys;
- private StyleTemplateWindow _window;
- }
- public class StyleTemplate : INotifyPropertyChanged
- {
- public StyleTemplate(Key keyboardShortcut, int num)
- {
- _fullStop = false;
- _exclamationMark = false;
- _questionMark = false;
- _bold = false;
- _italic = false;
- _underline = false;
- _sygnature = "Sygnatura";
- _number = num;
- _shortCut = keyboardShortcut;
- _alignLeft = true;
- if(_colorStyle == null)
- {
- _colorStyle = new List<ColorStyle>();
- _colorStyle.Add(new ColorStyle(Brushes.Black));
- _colorStyle.Add(new ColorStyle(Brushes.Blue));
- _colorStyle.Add(new ColorStyle(Brushes.Green));
- _colorStyle.Add(new ColorStyle(Brushes.Yellow));
- _colorStyle.Add(new ColorStyle(Brushes.Orange));
- _colorStyle.Add(new ColorStyle(Brushes.Red));
- _colorStyle.Add(new ColorStyle(Brushes.Pink));
- _colorStyle.Add(new ColorStyle(Brushes.Purple));
- _colorStyle.Add(new ColorStyle(Brushes.Brown));
- _colorStyle.Add(new ColorStyle(Brushes.DarkGray));
- _colorStyle.Add(new ColorStyle(Brushes.Cyan));
- }
- SelectedColor = _colorStyle[0];
- ResetStyleCommand = new RelayCommand(new Action<object>(ResetStylePress));
- }
- public ICommand ResetStyleCommand
- {
- get;
- set;
- }
- public void ApplyStyle(System.Windows.Documents.TextRange tmpRange)
- {
- bool space = false;
- if (tmpRange.Text.Length > 0 && Char.IsSeparator(tmpRange.Text.Last()))
- {
- space = true;
- }
- if (_fullStop && tmpRange.Text.Length > 0 && !Char.IsPunctuation(tmpRange.Text.Last()))
- tmpRange.Text = tmpRange.Text.TrimEnd() + "." + (space ? " " : "");
- if (_exclamationMark && tmpRange.Text.Length > 0 && !Char.IsPunctuation(tmpRange.Text.Last()))
- tmpRange.Text = tmpRange.Text.TrimEnd() + "!" + (space ? " " : "");
- if (_questionMark && tmpRange.Text.Length > 0 && !Char.IsPunctuation(tmpRange.Text.Last()))
- tmpRange.Text = tmpRange.Text.TrimEnd() + "?" + (space ? " " : "");
- if (!String.IsNullOrWhiteSpace(_sygnature))
- {
- if (!tmpRange.Text.StartsWith(_sygnature))
- {
- tmpRange.Text = _sygnature + ": " + tmpRange.Text.TrimStart();
- }
- }
- if(_newLine)
- {
- tmpRange.Text += "\r\n";
- if (_alignCenter)
- tmpRange.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Center);
- else if (_alignLeft)
- tmpRange.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Left);
- else if (_alignRight)
- tmpRange.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Right);
- else if (_alignJustify)
- tmpRange.ApplyPropertyValue(Paragraph.TextAlignmentProperty, TextAlignment.Justify);
- }
- if (_bold)
- tmpRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
- else
- tmpRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
- if (_underline)
- tmpRange.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);
- else
- tmpRange.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
- if (_italic)
- tmpRange.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Italic);
- else
- tmpRange.ApplyPropertyValue(TextElement.FontStyleProperty, FontStyles.Normal);
- tmpRange.ApplyPropertyValue(TextElement.ForegroundProperty, _selectedColor.TextColor);
- }
- public bool NewLine
- {
- get { return _newLine; }
- set { _newLine = value; if (!_newLine) TextAlignmentProperty = TextAlignment.Left; else UpdateTextAlign(); NotifyPropertyChange("NewLine"); }
- }
- public bool FullStop
- {
- get { return _fullStop; }
- set { ResetPunctuation(value); _fullStop = value; if (_fullStop)EndSign = "."; CheckEndSign(); NotifyPropertyChange("FullStop"); }
- }
- public bool ExclamationMark
- {
- get { return _exclamationMark; }
- set { ResetPunctuation(value); _exclamationMark = value; if (_exclamationMark)EndSign = "!"; CheckEndSign(); NotifyPropertyChange("Exclamationmark"); }
- }
- public bool QuestionMark
- {
- get { return _questionMark; }
- set { ResetPunctuation(value); _questionMark = value; if (_questionMark)EndSign = "?"; CheckEndSign(); NotifyPropertyChange("QuestionMark"); }
- }
- public bool Bold
- {
- get { return _bold; }
- set { _bold = value; if (_bold) FontWeightProperty = FontWeights.Bold; else FontWeightProperty = FontWeights.Normal; NotifyPropertyChange("Bold"); }
- }
- public bool Italic
- {
- get { return _italic; }
- set { _italic = value; if (_italic) FontStyleProperty = FontStyles.Italic; else FontStyleProperty = FontStyles.Normal; NotifyPropertyChange("Italic"); }
- }
- public bool Underline
- {
- get { return _underline; }
- set { _underline = value; if (_underline) TextDecorationProperty = TextDecorations.Underline; else TextDecorationProperty = null; NotifyPropertyChange("Underline"); }
- }
- public bool AlignLeft
- {
- get { return _alignLeft; }
- set { ResetAlign(value); _alignLeft = value; if (_alignLeft)TextAlignmentProperty = TextAlignment.Left; NotifyPropertyChange("AlignLeft"); CheckAlign(); }
- }
- public bool AlignRight
- {
- get { return _alignRight; }
- set { ResetAlign(value); _alignRight = value; if (_alignRight)TextAlignmentProperty = TextAlignment.Right; NotifyPropertyChange("AlignRight"); CheckAlign(); }
- }
- public bool AlignCenter
- {
- get { return _alignCenter; }
- set { ResetAlign(value); _alignCenter = value; if (_alignCenter)TextAlignmentProperty = TextAlignment.Center; NotifyPropertyChange("AlignCenter"); CheckAlign(); }
- }
- public bool AlignJustify
- {
- get { return _alignJustify; }
- set { ResetAlign(value); _alignJustify = value; if (_alignJustify)TextAlignmentProperty = TextAlignment.Justify; NotifyPropertyChange("AlignJustify"); CheckAlign(); }
- }
- public string Sygnature
- {
- get { return _sygnature; }
- set { _sygnature = value; NotifyPropertyChange("Sygnature"); }
- }
- public string Number
- {
- get { return Convert.ToString(_number); }
- set { _number = Int32.Parse(value); NotifyPropertyChange("Number"); }
- }
- public List<ColorStyle> ColorStyle
- {
- get { return _colorStyle; }
- }
- public TextAlignment TextAlignmentProperty
- {
- get { return _textAlignmentProperty; }
- set { _textAlignmentProperty = value; NotifyPropertyChange("TextAlignmentProperty"); }
- }
- public string EndSign
- {
- get { return _endSign; }
- set { _endSign = value; NotifyPropertyChange("EndSign"); }
- }
- public SolidColorBrush SelectedBrush
- {
- get { return _selectedColor.TextColor; }
- set { _selectedBrush = value; NotifyPropertyChange("SelectedBrush"); }
- }
- public FontStyle FontStyleProperty
- {
- get { return _fontStyleProperty; }
- set { _fontStyleProperty = value; NotifyPropertyChange("FontStyleProperty"); }
- }
- public FontWeight FontWeightProperty
- {
- get { return _fontWeightProperty; }
- set { _fontWeightProperty = value; NotifyPropertyChange("FontWeightProperty"); }
- }
- public TextDecorationCollection TextDecorationProperty
- {
- get { return _textDecoration; }
- set { _textDecoration = value; NotifyPropertyChange("TextDecorationProperty"); }
- }
- public ColorStyle SelectedColor
- {
- get { return _selectedColor; }
- set { _selectedColor = value; SelectedBrush = _selectedColor.TextColor; NotifyPropertyChange("SelectedColor"); }
- }
- public String KeyboardShortcut
- {
- get { return _shortCut.ToString(); }
- }
- private void ResetStylePress(object obj)
- {
- AlignLeft = true;
- Bold = false;
- Italic = false;
- Underline = false;
- Sygnature = "Sygnatura";
- SelectedColor = _colorStyle[0];
- FullStop = false;
- ExclamationMark = false;
- QuestionMark = false;
- NewLine = false;
- }
- private void UpdateTextAlign()
- {
- if (_alignCenter)
- TextAlignmentProperty = TextAlignment.Center;
- else if (_alignJustify)
- TextAlignmentProperty = TextAlignment.Justify;
- else if (_alignLeft)
- TextAlignmentProperty = TextAlignment.Left;
- else if (_alignRight)
- TextAlignmentProperty = TextAlignment.Right;
- }
- private void ResetPunctuation(bool reset)
- {
- if (reset)
- {
- EndSign = "";
- _exclamationMark = false;
- _fullStop = false;
- _questionMark = false;
- NotifyPropertyChange("QuestionMark");
- NotifyPropertyChange("Exclamationmark");
- NotifyPropertyChange("FullStop");
- }
- }
- private void CheckEndSign()
- {
- if (!_exclamationMark && !_fullStop && !_questionMark)
- EndSign = "";
- }
- private void ResetAlign(bool reset)
- {
- if (reset)
- {
- _alignCenter = false;
- _alignJustify = false;
- _alignLeft = false;
- _alignRight = false;
- NotifyPropertyChange("AlignJustify");
- NotifyPropertyChange("AlignCenter");
- NotifyPropertyChange("AlignLeft");
- NotifyPropertyChange("AlignRight");
- }
- }
- private void CheckAlign()
- {
- if (!_alignCenter && !_alignJustify && !_alignLeft && !_alignRight)
- {
- _alignLeft = true;
- TextAlignmentProperty = TextAlignment.Left;
- }
- NotifyPropertyChange("AlignLeft");
- }
- private bool _fullStop, _exclamationMark, _questionMark, _newLine, _alignLeft, _alignRight, _alignCenter, _alignJustify;
- private bool _bold, _underline, _italic;
- private string _sygnature, _endSign;
- private int _number;
- private ColorStyle _selectedColor;
- private SolidColorBrush _selectedBrush;
- private TextDecorationCollection _textDecoration;
- private FontWeight _fontWeightProperty;
- private FontStyle _fontStyleProperty;
- private TextAlignment _textAlignmentProperty;
- private static List<ColorStyle> _colorStyle;
- private Key _shortCut;
- protected void NotifyPropertyChange(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
- public class ColorStyle : INotifyPropertyChanged
- {
- public ColorStyle(SolidColorBrush brush)
- {
- _textColor = brush;
- }
- public ColorStyle()
- {
- _textColor = Brushes.Black;
- }
- public SolidColorBrush TextColor
- {
- get { return _textColor; }
- set { _textColor = value; NotifyPropertyChange("TextColor"); }
- }
- SolidColorBrush _textColor;
- protected void NotifyPropertyChange(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement