Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Input;
- using Passport.GUI.DB;
- using System.Data.Entity;
- using System.Windows;
- using Passport.GUI.ViewModels.Commands;
- using Passport.GUI.Models;
- using System.Data;
- using OxyPlot;
- using System.Collections.ObjectModel;
- using Passport.GUI.ViewModels.Plots;
- using OxyPlot.Axes;
- using System;
- using OxyPlot.Series;
- using OxyPlot.Annotations;
- using System.Threading;
- namespace Passport.GUI.ViewModels
- {
- class PassportPageViewModel : AbstractTab
- {
- private bool isReadOnly;
- public bool IsReadOnly
- {
- get
- {
- return isReadOnly;
- }
- set
- {
- isReadOnly = value;
- if (isReadOnly == false)
- {
- Name = Name + "..";
- }
- OnPropertyChanged("IsReadOnly");
- }
- }
- public DBPassport Passport { get; set; }
- private List<DBUklon> uklons;
- public List<DBUklon> Uklons
- {
- get
- {
- return uklons;
- }
- set
- {
- uklons = value;
- OnPropertyChanged("Uklons");
- }
- }
- private List<DBVSM> vsms;
- public List<DBVSM> VSMs
- {
- get
- {
- return vsms;
- }
- set
- {
- vsms = value;
- OnPropertyChanged("VSMs");
- }
- }
- public ICommand ChangeReadOnlyModeCommand { get; set; }
- public ICommand SaveToDB { get; set; }
- private void saveToDB(PassportPageViewModel context)
- {
- using (var db = new PassportDBContext())
- {
- Passport = context.Passport;
- Uklons = context.Uklons;
- VSMs = context.VSMs;
- if (Passport.Id == 0)
- {
- db.DBPassports.Add(Passport);
- }
- else
- {
- db.Entry(Passport).State = EntityState.Modified;
- }
- int Id = Passport.Id;
- foreach (var u in Uklons)
- {
- if (u.Id == 0)
- {
- u.PassportId = Id;
- db.DBUklons.Add(u);
- }
- else
- {
- db.Entry(u).State = EntityState.Modified;
- }
- }
- foreach (var v in VSMs)
- {
- if (v.Id == 0)
- {
- v.PassportId = Id;
- db.DBVSMs.Add(v);
- }
- else
- {
- db.Entry(v).State = EntityState.Modified;
- }
- }
- IsReadOnly = true;
- Name = "Участок № " + Passport.AreaNum.ToString();
- db.SaveChanges();
- context.CleanDirty();
- context.CurvSpec = new CurvSpecModel(VSMs);
- }
- }
- public bool IsDirty()
- {
- if (Passport.IsDirty() || !(VSMs.Where(p => p.IsDirty() == true).Count() == 0) || !(Uklons.Where(p => p.IsDirty() == true).Count() == 0))
- { return true; }
- return false;
- }
- public void CleanDirty()
- {
- Passport.CleanDirty();
- foreach (DBVSM vsm in VSMs)
- {
- vsm.CleanDirty();
- }
- foreach (DBUklon ukl in Uklons)
- {
- ukl.CleanDirty();
- }
- }
- private CurvSpecModel curvSpec;
- public CurvSpecModel CurvSpec {
- get
- {
- return curvSpec;
- }
- set
- {
- curvSpec = value;
- OnPropertyChanged("CurvSpec");
- }
- }
- public PlotModel UklonPlotModelZ { get; set; }
- public PlotModel VsmPlotModel { get; set; }
- public PlotModel UklonHeightModel { get; set; }
- public PlotController StaticControl { get; set; }
- public void testSer(Object o, OxyMouseEventArgs e)
- {
- //MessageBox.Show("WORKED");
- }
- public PassportPageViewModel()
- {
- ChangeReadOnlyModeCommand = new RelayCommand(p => { IsReadOnly = true; });
- Passport = new DBPassport();
- Name = "Новый участок";
- Uklons = new List<DBUklon>();
- VSMs = new List<DBVSM>();
- ///Удалить если работает с ptableviewmodel
- SaveToDB = new RelayCommand(p => saveToDB((PassportPageViewModel)p));
- }
- //Действующий конструктор
- public PassportPageViewModel(DBPassport passport)
- {
- ChangeReadOnlyModeCommand = new RelayCommand(p => { IsReadOnly = false; });
- Passport = passport;
- Name = "Участок № " + Passport.AreaNum;
- using (var db = new PassportDBContext())
- {
- Uklons = (from u in db.DBUklons where u.PassportId.Equals(Passport.Id) select u).ToList<DBUklon>();
- VSMs = (from v in db.DBVSMs where v.PassportId.Equals(Passport.Id) select v).ToList<DBVSM>();
- }
- StaticControl = new CustomUnbindedPlotControl();
- UklonPlotModelZ = PlotFun.UklonPlotModelL(Uklons, passport.BgnPK);
- VsmPlotModel = PlotFun.VSMPlotModelAnn(VSMs, passport.BgnPK);
- UklonHeightModel = PlotFun.UklonHeightModelz(Uklons, passport.BgnPK, passport.BgnHeight);
- UklonPlotModelZ.Axes[0].AxisChanged += UklonPlotModelZ_XAxisChanged;
- UklonHeightModel.Axes[0].AxisChanged += UklonHeightModel_XAxisChanged;
- LineAnnotation pointer1 = new LineAnnotation { Type = LineAnnotationType.Vertical, X = 0, Color = OxyColors.Green };
- UklonHeightModel.Annotations.Add(pointer1);
- LineSeries prevSelectedUklonLine = null;
- int selectedUklonSeriesInd = -1;
- UklonHeightModel.MouseMove += (s, e) =>
- {
- ScreenPoint pos = new ScreenPoint(e.Position.X, e.Position.Y);
- LineSeries selectedL = UklonHeightModel.GetSeriesFromPoint(pos) as LineSeries;
- var smth = VsmPlotModel.Axes[0].Transform(0, 0, UklonHeightModel.Axes[1]).Y;
- var ind1 = UklonHeightModel.Annotations.IndexOf(pointer1);
- var a = (LineAnnotation)UklonHeightModel.Annotations[ind1];
- var newx = a.InverseTransform(new ScreenPoint(e.Position.X, e.Position.Y));
- if (Math.Abs(a.X - newx.X) > 1)
- {
- a.Color = OxyColors.Green;
- a.X = Math.Round(newx.X);
- if (UklonHeightModel.Series.IndexOf(selectedL) != -1)
- {
- selectedUklonSeriesInd = UklonHeightModel.Series.IndexOf(selectedL);
- DBUklon v = Uklons[selectedUklonSeriesInd];
- TrackerHitResult nearestP = UklonHeightModel.Series[0].GetNearestPoint(e.Position, true);
- a.Text = "М: " + a.X.ToString() + Environment.NewLine +
- "В: " + Math.Round(nearestP.DataPoint.Y, 2).ToString();
- a.TextOrientation = AnnotationTextOrientation.Horizontal;
- a.TextHorizontalAlignment = OxyPlot.HorizontalAlignment.Left;
- a.TextPadding = 5;
- if (selectedL != null)
- {
- selectedL.Color = OxyColors.Red;
- selectedL.StrokeThickness = 3;
- }
- if (prevSelectedUklonLine != null && selectedL != prevSelectedUklonLine)
- {
- prevSelectedUklonLine.Color = OxyColors.Black;
- prevSelectedUklonLine.StrokeThickness = 2;
- }
- prevSelectedUklonLine = selectedL;
- }
- UklonHeightModel.InvalidatePlot(false);
- }
- };
- RectangleAnnotation range = null;
- double startx = double.NaN;
- VsmPlotModel.MouseDown += (s, e) =>
- {
- if (e.ChangedButton == OxyMouseButton.Left && e.IsShiftDown == true)
- {
- if (range == null)
- {
- // Create and add the annotation to the plot
- range = new RectangleAnnotation { Fill = OxyColors.SkyBlue };
- VsmPlotModel.Annotations.Add(range);
- VsmPlotModel.InvalidatePlot(true);
- }
- startx = range.InverseTransform(e.Position).X;
- range.MinimumX = startx;
- range.MaximumX = startx;
- VsmPlotModel.InvalidatePlot(true);
- e.Handled = true;
- }
- };
- VsmPlotModel.MouseMove += (s, e) =>
- {
- if (e.IsShiftDown == true && !double.IsNaN(startx))
- {
- var x = range.InverseTransform(e.Position).X;
- range.MinimumX = Math.Min(x, startx);
- range.MaximumX = Math.Max(x, startx);
- range.Text = string.Format("? cos(x) dx = {0:0.00}", Math.Sin(range.MaximumX) - Math.Sin(range.MinimumX));
- VsmPlotModel.Subtitle = string.Format("Integrating from {0:0.00} to {1:0.00}", range.MinimumX, range.MaximumX);
- VsmPlotModel.InvalidatePlot(true);
- e.Handled = true;
- }
- };
- VsmPlotModel.MouseUp += (s, e) =>
- {
- startx = double.NaN;
- };
- LineAnnotation pointer2 = new LineAnnotation { Type = LineAnnotationType.Vertical, X = 0, Color = OxyColors.Green};
- VsmPlotModel.Annotations.Add(pointer2);
- LineSeries prevSelectedVsmLine = null;
- int selectedVSMSeriesInd = -1;
- VsmPlotModel.MouseDown += (s, e) =>
- {
- if (e.IsShiftDown == true)
- {
- var re = Axis.InverseTransform(e.Position, VsmPlotModel.Axes[0], VsmPlotModel.Axes[1]);
- VsmPlotModel.Annotations.Add(new LineAnnotation()
- {
- Type = LineAnnotationType.Vertical,
- X = re.X
- });
- }
- };
- VsmPlotModel.MouseMove += testSer;
- VsmPlotModel.MouseMove += (s, e) =>
- {
- ScreenPoint pos = new ScreenPoint(e.Position.X, VsmPlotModel.Axes[0].Transform(0, 0, VsmPlotModel.Axes[1]).Y);
- LineSeries selectedL = (LineSeries)VsmPlotModel.GetSeriesFromPoint(pos);
- var smth = VsmPlotModel.Axes[0].Transform(0, 0, VsmPlotModel.Axes[1]).Y;
- var ind2 = VsmPlotModel.Annotations.IndexOf(pointer2);
- var a = (LineAnnotation)VsmPlotModel.Annotations[ind2];
- var newx = a.InverseTransform(new ScreenPoint(e.Position.X, e.Position.Y));
- if (Math.Abs(a.X - newx.X) > 1)
- {
- a.Color = OxyColors.Green;
- a.X = Math.Round(newx.X);
- if (VsmPlotModel.Series.IndexOf(selectedL) != -1)
- {
- selectedVSMSeriesInd = VsmPlotModel.Series.IndexOf(selectedL);
- DBVSM v = VSMs[selectedVSMSeriesInd];
- if (v.CurvRad == 0)
- {
- a.Text = "М: " + a.X.ToString() + System.Environment.NewLine +
- "Длина: " + v.Length.ToString() + "м.";
- }
- else
- {
- double decimal_degrees = (v.Length * 180.0) / (Math.PI * v.CurvRad);
- double rads = Math.PI * decimal_degrees / 180.0;
- int sec = (int)Math.Round(decimal_degrees * 3600);
- int deg = sec / 3600;
- sec = Math.Abs(sec % 3600);
- int min = sec / 60;
- sec %= 60;
- a.Text = "М: " + a.X.ToString() + System.Environment.NewLine +
- "Уг: " + deg + '°' + min + '\'' + sec + "\'\'" + System.Environment.NewLine +
- "Р: " + v.CurvRad + "м." + System.Environment.NewLine +
- "Т: " + Math.Round(v.CurvRad * Math.Tan(rads / 2), 2) + "м." + System.Environment.NewLine +
- "L: " + v.Length.ToString() + "м." + System.Environment.NewLine +
- "l1: " + v.BgnClothLen.ToString() + "м." + System.Environment.NewLine +
- "l2: " + v.EndClothLen.ToString() + "м.";
- }
- if (selectedL != null)
- {
- selectedL.Color = OxyColors.Red;
- selectedL.StrokeThickness = 3;
- }
- if (prevSelectedVsmLine != null && selectedL != prevSelectedVsmLine)
- {
- prevSelectedVsmLine.Color = OxyColors.Black;
- prevSelectedVsmLine.StrokeThickness = 2;
- }
- prevSelectedVsmLine = selectedL;
- if (a.X < selectedL.MinX || a.X > selectedL.MaxX)
- { //a.Color = OxyColors.Transparent;
- a.Text = "";
- selectedL.Color = OxyColors.Black;
- selectedL.StrokeThickness = 1;
- }
- };
- a.TextOrientation = AnnotationTextOrientation.Horizontal;
- a.TextHorizontalAlignment = OxyPlot.HorizontalAlignment.Left;
- a.TextPadding = 5;
- VsmPlotModel.InvalidatePlot(false);
- }
- };
- CurvSpec = new CurvSpecModel(VSMs);
- SaveToDB = new RelayCommand(p => saveToDB((PassportPageViewModel)p));
- }
- private void UklonHeightModel_XAxisChanged(object sender, AxisChangedEventArgs e)
- {
- if ((UklonHeightModel.Axes[0].ActualMinimum != UklonPlotModelZ.Axes[0].ActualMinimum)
- && UklonHeightModel.Axes[0].ActualMaximum != UklonPlotModelZ.Axes[0].ActualMaximum)
- {
- UklonPlotModelZ.Axes[0].Zoom(UklonHeightModel.Axes[0].ActualMinimum, UklonHeightModel.Axes[0].ActualMaximum);
- UklonPlotModelZ.InvalidatePlot(true);
- }
- }
- private void UklonPlotModelZ_XAxisChanged(object sender, AxisChangedEventArgs e)
- {
- if ((UklonPlotModelZ.Axes[0].ActualMinimum != UklonHeightModel.Axes[0].ActualMinimum)
- && UklonPlotModelZ.Axes[0].ActualMaximum != UklonHeightModel.Axes[0].ActualMaximum)
- {
- UklonHeightModel.Axes[0].Zoom(UklonPlotModelZ.Axes[0].ActualMinimum, UklonPlotModelZ.Axes[0].ActualMaximum);
- UklonHeightModel.InvalidatePlot(true);
- }
- }
- public PassportPageViewModel(int passportId)
- {
- ChangeReadOnlyModeCommand = new RelayCommand(p => { IsReadOnly = false; });
- using (var db = new PassportDBContext())
- {
- Passport = (from p in db.DBPassports where p.Id == passportId select p).First<DBPassport>();
- Uklons = (from u in db.DBUklons where u.PassportId.Equals(Passport.Id) select u).ToList<DBUklon>();
- VSMs = (from v in db.DBVSMs where v.PassportId.Equals(Passport.Id) select v).ToList<DBVSM>();
- }
- CurvSpec = new CurvSpecModel(VSMs);
- Name = "Участок № " + Passport.AreaNum;
- SaveToDB = new RelayCommand(p => saveToDB((PassportPageViewModel)p));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement