프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkupToPDF / Controls / Etc / ImgControl.cs @ 9005f973

이력 | 보기 | 이력해설 | 다운로드 (21 KB)

1
using System;
2
using System.Net;
3
using System.Windows;
4
using System.Windows.Controls;
5
using System.Windows.Documents;
6
using System.Windows.Ink;
7
using System.Windows.Input;
8
using System.Windows.Media;
9
using System.Windows.Media.Animation;
10
using System.Windows.Shapes;
11
using System.Collections.Generic;
12
using System.ComponentModel;
13
using System.Linq;
14
using MarkupToPDF.Controls.Common;
15
using MarkupToPDF.Common;
16
using MarkupToPDF.Serialize.Core;
17
using MarkupToPDF.Serialize.S_Control;
18
using System.Windows.Media.Imaging;
19

    
20
namespace MarkupToPDF.Controls.Etc
21
{
22
    [TemplatePart(Name = "PART_Image", Type = typeof(Image))]
23
    public class ImgControl : CommentUserInfo, IDisposable, INormal, INotifyPropertyChanged, IViewBox, IMarkupCommonData
24
    {
25
        #region 초기선언
26
        private const string PART_Image = "PART_Image";
27
        public Image Base_Image = null;
28
        #endregion
29

    
30
        static ImgControl()
31
        {
32
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImgControl), new FrameworkPropertyMetadata(typeof(ImgControl)));
33
            //Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(new Uri("/MarkupToPDF;Component/Themes/generic.xaml")) as ResourceDictionary);
34
            //ResourceDictionary dictionary = new ResourceDictionary();
35
            //dictionary.Source = new Uri("/MarkupToPDF;component/themes/generic.xaml", UriKind.RelativeOrAbsolute);
36
            //Application.Current.Resources.MergedDictionaries.Add(dictionary);
37
             //System.Diagnostics.Debug.WriteLine("resource Count :" + Application.Current.Resources.MergedDictionaries.Count);
38
        }
39

    
40
        public ImgControl()
41
        {
42
            //this.DefaultStyleKey = typeof(ImgControl);
43
        }
44

    
45
        public void Dispose()
46
        {
47
            //GC.Collect();
48
            ////GC.SuppressFinalize(this);
49
            this.Base_Image = null;
50
        }
51
        public event PropertyChangedEventHandler PropertyChanged;
52
        protected void OnPropertyChanged(string propName)
53
        {
54
            if (PropertyChanged != null)
55
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
56
        }
57

    
58
        public override void ApplyOverViewData()
59
        {
60
            this.OverViewPathData = this.PathData;
61
        }
62

    
63
        #region Dependency Properties
64

    
65
        public static readonly DependencyProperty IsSelectedProperty =
66
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(ImgControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
67

    
68
        public static readonly DependencyProperty ControlTypeProperty =
69
            DependencyProperty.Register("ControlType", typeof(ControlType), typeof(ImgControl), new FrameworkPropertyMetadata(ControlType.ImgControl));
70

    
71
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
72
       "PathData", typeof(Geometry), typeof(ImgControl), null);
73
        public static readonly DependencyProperty OverViewPathDataProperty = DependencyProperty.Register(
74
        "OverViewPathData", typeof(Geometry), typeof(ImgControl), null);
75

    
76
        public static readonly DependencyProperty ImageDataProperty = DependencyProperty.Register(
77
            "ImageData", typeof(ImageSource), typeof(ImgControl), new PropertyMetadata(null));
78
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
79
            "UserID", typeof(string), typeof(ImgControl), new PropertyMetadata(null));
80
        public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register(
81
            "FilePath", typeof(string), typeof(ImgControl), new PropertyMetadata(null));
82
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
83
            "StartPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
84
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
85
            "EndPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(100, 100), PointValueChanged));
86
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
87
            "PointSet", typeof(List<Point>), typeof(ImgControl), new PropertyMetadata(new List<Point>()));
88
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
89
             "TopRightPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
90
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
91
              "LeftBottomPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
92
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(ImgControl),
93
                        new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
94
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(ImgControl),
95
                        new PropertyMetadata((double)0, OnCenterXYChanged));
96
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(ImgControl),
97
                        new PropertyMetadata((double)0, OnCenterXYChanged));
98

    
99
        #endregion
100
        #region PropertyChanged Method
101

    
102
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
103
        {
104
            //var instance = (ImgControl)sender;
105

    
106
            //if (e.OldValue != e.NewValue && instance.Base_Image != null)
107
            //{
108

    
109
            //    instance.SetValue(e.Property, e.NewValue);
110

    
111
            //    if (instance.IsSelected)
112
            //    {
113
            //        //instance.Base_Image.Stroke = new SolidColorBrush(Colors.Blue);
114
            //    }
115
            //    else
116
            //    {
117
            //        //instance.Base_Image.Stroke = new SolidColorBrush(Colors.Red);
118
            //    }
119
            //}
120
        }
121

    
122
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
123
        {
124
            var instance = (ImgControl)sender;
125
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
126
            {
127
                instance.SetValue(e.Property, e.NewValue);
128
                instance.SetImage();
129
            }
130
        }
131
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
132
        {
133
            var instance = (ImgControl)sender;
134
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
135
            {
136
                instance.SetValue(e.Property, e.NewValue);
137
                instance.SetImage();
138
            }
139
        }
140
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
141
        {
142
            var instance = (ImgControl)sender;
143
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
144
            {
145
                instance.SetValue(e.Property, e.NewValue);
146
                instance.SetImage();
147
            }
148
        }
149
        #endregion
150
        #region Properties
151
        public double CenterX
152
        {
153
            get { return (double)GetValue(CenterXProperty); }
154
            set { SetValue(CenterXProperty, value); }
155
        }
156
        public string UserID
157
        {
158
            get { return (string)GetValue(UserIDProperty); }
159
            set
160
            {
161
                if (this.UserID != value)
162
                {
163
                    SetValue(UserIDProperty, value);
164
                    OnPropertyChanged("UserID");
165
                }
166
            }
167
        }
168
       
169
        public double CenterY
170
        {
171
            get { return (double)GetValue(CenterYProperty); }
172
            set { SetValue(CenterYProperty, value); }
173
        }
174
        public string FilePath
175
        {
176
            get { return (string)GetValue(FilePathProperty); }
177
            set { SetValue(FilePathProperty, value); }
178
        }
179
        public Point EndPoint
180
        {
181
            get { return (Point)GetValue(EndPointProperty); }
182
            set
183
            {
184
                if (this.EndPoint != value)
185
                {
186
                    SetValue(EndPointProperty, value);
187
                    OnPropertyChanged("EndPoint");
188
                }
189
            }
190
        }
191
        public List<Point> PointSet
192
        {
193
            get { return (List<Point>)GetValue(PointSetProperty); }
194
            set { SetValue(PointSetProperty, value); }
195
        }
196
        public Point StartPoint
197
        {
198
            get { return (Point)GetValue(StartPointProperty); }
199
            set
200
            {
201
                if (this.StartPoint != value)
202
                {
203
                    SetValue(StartPointProperty, value);
204
                    OnPropertyChanged("StartPoint");
205
                }
206
            }
207
        }
208
        public Point TopRightPoint
209
        {
210
            get { return (Point)GetValue(TopRightPointProperty); }
211
            set
212
            {
213
                SetValue(TopRightPointProperty, value);
214
                OnPropertyChanged("TopRightPoint");
215
            }
216
        }
217
        public Point LeftBottomPoint
218
        {
219
            get { return (Point)GetValue(LeftBottomPointProperty); }
220
            set
221
            {
222
                SetValue(LeftBottomPointProperty, value);
223
                OnPropertyChanged("LeftBottomPoint");
224
            }
225
        }
226

    
227
        public override double CommentAngle
228
        {
229
            get { return (double)GetValue(AngleProperty); }
230
            set
231
            {
232
                if (this.CommentAngle != value)
233
                {
234
                    SetValue(AngleProperty, value);
235
                }
236
            }
237
        }
238
        public ImageSource ImageData
239
        {
240
            get { return (ImageSource)this.GetValue(ImageDataProperty); }
241
            set { this.SetValue(ImageDataProperty, value); }
242
        }
243

    
244
         public override bool IsSelected
245
        {
246
            get
247
            {
248
                return (bool)GetValue(IsSelectedProperty);
249
            }
250
            set
251
            {
252
                SetValue(IsSelectedProperty, value);
253
                //OnPropertyChanged("IsSelected");
254
            }
255
        }
256

    
257
        public override ControlType ControlType
258
        {
259
            set
260
            {
261
                SetValue(ControlTypeProperty, value);
262
                OnPropertyChanged("ControlType");
263
            }
264
            get
265
            {
266
                return (ControlType)GetValue(ControlTypeProperty);
267
            }
268
        }
269

    
270
        public Geometry OverViewPathData
271
        {
272
            get { return (Geometry)GetValue(OverViewPathDataProperty); }
273
            set
274
            {
275
                SetValue(OverViewPathDataProperty, value);
276
                OnPropertyChanged("OverViewPathData");
277
            }
278
        }
279

    
280

    
281
        #endregion
282

    
283
        public override void OnApplyTemplate()
284
        {
285
            base.OnApplyTemplate();
286
            Base_Image = GetTemplateChild(PART_Image) as Image;
287
            Base_Image.Width = 0;
288
            Base_Image.Height = 0;
289
            SetImage();
290
        }
291

    
292
        public void SetImage()
293
        {
294
            this.ApplyTemplate();
295
            Point mid = MathSet.FindCentroid(new List<Point>()
296
            {
297
                this.StartPoint,
298
                this.LeftBottomPoint,
299
                this.EndPoint,
300
                this.TopRightPoint,                
301
            });
302
            double AngleData = this.CommentAngle * -1;
303
            PathFigure pathFigure = new PathFigure();
304

    
305
            pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
306

    
307
            LineSegment lineSegment0 = new LineSegment();
308
            lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
309
            pathFigure.Segments.Add(lineSegment0);
310

    
311
            LineSegment lineSegment1 = new LineSegment();
312
            lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData);
313
            pathFigure.Segments.Add(lineSegment1);
314

    
315
            LineSegment lineSegment2 = new LineSegment();
316
            lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData);
317
            pathFigure.Segments.Add(lineSegment2);
318

    
319
            LineSegment lineSegment3 = new LineSegment();
320
            lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData);
321
            pathFigure.Segments.Add(lineSegment3);
322

    
323
            PathGeometry pathGeometry = new PathGeometry();
324
            pathGeometry.Figures = new PathFigureCollection();
325
            pathFigure.IsClosed = true;
326
            pathGeometry.Figures.Add(pathFigure);
327
            this.Base_Image.Width = pathGeometry.Bounds.Width;
328
            this.Base_Image.Height = pathGeometry.Bounds.Height;
329
             this.Tag = pathGeometry;
330

    
331
            Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_Image.Width / 2);
332
            Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_Image.Height / 2);
333

    
334
            this.PathData = pathGeometry;
335
            ApplyOverViewData();
336

    
337
        }
338
        public override void UpdateControl()
339
        {
340
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
341
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
342
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
343
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
344
        }
345

    
346
        public double LineSize { get; set; }
347

    
348
        public Geometry PathData { get; set; }
349

    
350
        /// <summary>
351
        /// call when mouse is moving while drawing control
352
        /// </summary>
353
        /// <param name="pt"></param>
354
        /// <param name="bAxisLocked"></param>
355
        public override void OnCreatingMouseMove(Point pt, bool bAxisLocked)
356
        {
357
            this.EndPoint = pt;
358
            this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
359
            this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
360
            
361
            this.PointSet = new List<Point>
362
            {
363
                this.StartPoint,
364
                this.LeftBottomPoint,
365
                this.EndPoint,
366
                this.TopRightPoint,
367
            };
368
        }
369

    
370
        /// <summary>
371
        /// move control point has same location of given pt along given delta
372
        /// </summary>
373
        /// <author>humkyung</author>
374
        /// <date>2019.06.20</date>
375
        /// <param name="pt"></param>
376
        /// <param name="dx"></param>
377
        /// <param name="dy"></param>
378
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy, bool bAxisLocked = false)
379
        {
380
            IPath path = (this as IPath);
381

    
382
            Point selected = MathSet.getNearPoint(path.PointSet, pt);
383
            selected.X += dx;
384
            selected.Y += dy;
385

    
386
            int idx = (this as IPath).PointSet.FindIndex(x => x.Equals(pt));
387

    
388
            var OppositeP = (idx + path.PointSet.Count / 2) % path.PointSet.Count;
389
            var PreviousP = (idx + (path.PointSet.Count - 1)) % path.PointSet.Count;
390
            var NextP = (idx + 1) % path.PointSet.Count;
391
            if (bAxisLocked)
392
            {
393
                var PrevV = path.PointSet[PreviousP] - path.PointSet[OppositeP];
394
                double PrevLength = PrevV.Length;
395
                PrevV.Normalize();
396

    
397
                var NextV = path.PointSet[NextP] - path.PointSet[OppositeP];
398
                double NextVLength = NextV.Length;
399
                NextV.Normalize();
400

    
401
                double _dx = selected.X - path.PointSet[OppositeP].X;
402
                double _dy = selected.Y - path.PointSet[OppositeP].Y;
403
                var dir = new Vector(_dx, _dy);
404
                if (PrevLength > NextVLength)
405
                {
406
                    double ratio = NextVLength / PrevLength;
407

    
408
                    double dot = MathSet.DotProduct(PrevV.X, PrevV.Y, dir.X, dir.Y);
409

    
410
                    path.PointSet[PreviousP] = path.PointSet[OppositeP] + PrevV * dot;
411
                    path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * dot * ratio;
412
                    path.PointSet[idx] = path.PointSet[OppositeP] + PrevV * dot + NextV * dot * ratio;
413
                }
414
                else
415
                {
416
                    double ratio = PrevLength / NextVLength;
417

    
418
                    double dot = MathSet.DotProduct(NextV.X, NextV.Y, dir.X, dir.Y);
419

    
420
                    path.PointSet[PreviousP] = path.PointSet[OppositeP] + PrevV * dot * ratio;
421
                    path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * dot;
422
                    path.PointSet[idx] = path.PointSet[OppositeP] + PrevV * dot * ratio + NextV * dot;
423
                }
424
            }
425
            else
426
            {
427
                var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[PreviousP]);
428
                var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[idx].X - path.PointSet[OppositeP].X,
429
                    path.PointSet[idx].Y - path.PointSet[OppositeP].Y);
430
                path.PointSet[PreviousP] = new Point(path.PointSet[OppositeP].X + PreviousV.X * l, path.PointSet[OppositeP].Y + PreviousV.Y * l);
431

    
432
                var NextV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[NextP]);
433
                l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[idx].X - path.PointSet[OppositeP].X, path.PointSet
434
                    [idx].Y - path.PointSet[OppositeP].Y);
435
                path.PointSet[NextP] = new Point(path.PointSet[OppositeP].X + NextV.X * l, path.PointSet[OppositeP].Y + NextV.Y * l);
436

    
437
                path.PointSet[idx] = selected;
438
            }
439

    
440
            this.UpdateControl();
441
        }
442

    
443
        /// <summary>
444
        /// return ImgControl's area
445
        /// </summary>
446
        /// <author>humkyung</author>
447
        /// <date>2019.06.13</date>
448
        public override Rect ItemRect
449
        {
450
            get
451
            {
452
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
453
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
454
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
455
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
456

    
457
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
458
            }
459
        }
460

    
461
        /// <summary>
462
        /// Serialize this
463
        /// </summary>
464
        /// <param name="sUserId"></param>
465
        /// <returns></returns>
466
        public override string Serialize()
467
        {
468
            using (S_ImgControl STemp = new S_ImgControl())
469
            {
470
                STemp.Angle = this.CommentAngle;
471
                STemp.EndPoint = this.EndPoint;
472
                STemp.LB = this.LeftBottomPoint;
473
                STemp.Name = this.GetType().Name;
474
                STemp.PointSet = this.PointSet;
475
                STemp.StartPoint = this.StartPoint;
476
                STemp.UserID = this.UserID;
477
                STemp.Opac = this.Opacity;
478
                STemp.TR = this.TopRightPoint;
479
                STemp.ImagePath = this.FilePath;
480
                ///강인구 추가(2017.11.02)
481
                ///Memo 추가
482
                STemp.Memo = this.Memo;
483

    
484
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
485
            }
486
        }
487

    
488
        /// <summary>
489
        /// create a imgcontrol from given string
490
        /// </summary>
491
        /// <param name="str"></param>
492
        /// <returns></returns>
493
        public static ImgControl FromString(string str, SolidColorBrush brush, string sProjectNo, string baseUri)
494
        {
495
            ImgControl instance = null;
496
            using (S_ImgControl s = JsonSerializerHelper.JsonDeserialize<S_ImgControl>(str))
497
            {
498
                UriBuilder downloadUri = new UriBuilder(baseUri);
499
                UriBuilder uri = new UriBuilder(s.ImagePath);
500
                uri.Host = downloadUri.Host;
501
                uri.Port = downloadUri.Port;
502

    
503
                Image img = new Image();
504
                if (s.ImagePath.Contains(".svg"))
505
                {
506
                    SharpVectors.Converters.SvgImageExtension svgImage = new SharpVectors.Converters.SvgImageExtension(uri.ToString());
507
                    var svg = svgImage.ProvideValue(null);
508

    
509
                    img.Source = (DrawingImage)svg;
510
                }
511
                else
512
                {
513
                    img.Source = new BitmapImage(uri.Uri);
514
                }
515

    
516
                instance = new ImgControl
517
                {
518
                    CommentAngle = s.Angle,
519
                    StartPoint = s.StartPoint,
520
                    TopRightPoint = s.TR,
521
                    EndPoint = s.EndPoint,
522
                    LeftBottomPoint = s.LB,
523
                    PointSet = s.PointSet,
524
                    Opacity = s.Opac,
525
                    FilePath = uri.Uri.ToString(),
526
                    UserID = s.UserID,
527
                    ImageData = img.Source,
528
                    Memo = s.Memo 
529
                };
530

    
531
                instance.ImageData = img.Source;
532
            }
533

    
534
            return instance;
535
        }
536
    }
537
}
클립보드 이미지 추가 (최대 크기: 500 MB)