프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Etc / ImgControl.cs @ 7b34fb3a

이력 | 보기 | 이력해설 | 다운로드 (22.1 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
using MarkupToPDF.Controls.Shape;
20
using System.Security.Policy;
21

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

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

    
42
        public ImgControl()
43
        {
44
            //this.DefaultStyleKey = typeof(ImgControl);
45
        }
46
        public override void Copy(CommentUserInfo lhs)
47
        {
48
            if(lhs is ImgControl imgControl)
49
            {
50
                this.CommentAngle = imgControl.CommentAngle;
51
                this.StartPoint = new Point(imgControl.StartPoint.X, imgControl.StartPoint.Y);
52
                this.TopRightPoint = new Point(imgControl.TopRightPoint.X, imgControl.TopRightPoint.Y);
53
                this.EndPoint = new Point(imgControl.EndPoint.X, imgControl.EndPoint.Y);
54
                this.LeftBottomPoint = new Point(imgControl.LeftBottomPoint.X, imgControl.LeftBottomPoint.Y);
55
                this.PointSet = imgControl.PointSet.ConvertAll(x => new Point(x.X, x.Y));
56
                this.Opacity = imgControl.Opacity;
57
                this.FilePath = imgControl.FilePath;
58
                this.UserID = imgControl.UserID;
59
                this.ImageData = imgControl.ImageData;
60
                this.Memo = imgControl.Memo;
61
            }
62
        }
63

    
64
        public override CommentUserInfo Clone()
65
        {
66
            var clone = new ImgControl();
67
            clone.Copy(this);
68
            return clone;
69
        }
70

    
71
        public void Dispose()
72
        {
73
            //GC.Collect();
74
            ////GC.SuppressFinalize(this);
75
            this.Base_Image = null;
76
        }
77
        public event PropertyChangedEventHandler PropertyChanged;
78
        protected void OnPropertyChanged(string propName)
79
        {
80
            if (PropertyChanged != null)
81
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
82
        }
83

    
84
        public override void ApplyOverViewData()
85
        {
86
            this.OverViewPathData = this.PathData;
87
        }
88

    
89
        #region Dependency Properties
90

    
91
        public static readonly DependencyProperty IsSelectedProperty =
92
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(ImgControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
93

    
94
        public static readonly DependencyProperty ControlTypeProperty =
95
            DependencyProperty.Register("ControlType", typeof(ControlType), typeof(ImgControl), new FrameworkPropertyMetadata(ControlType.ImgControl));
96

    
97
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
98
       "PathData", typeof(Geometry), typeof(ImgControl), null);
99
        public static readonly DependencyProperty OverViewPathDataProperty = DependencyProperty.Register(
100
        "OverViewPathData", typeof(Geometry), typeof(ImgControl), null);
101

    
102
        public static readonly DependencyProperty ImageDataProperty = DependencyProperty.Register(
103
            "ImageData", typeof(ImageSource), typeof(ImgControl), new PropertyMetadata(null));
104
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
105
            "UserID", typeof(string), typeof(ImgControl), new PropertyMetadata(null));
106
        public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register(
107
            "FilePath", typeof(string), typeof(ImgControl), new PropertyMetadata(null));
108
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
109
            "StartPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
110
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
111
            "EndPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(100, 100), PointValueChanged));
112
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
113
            "PointSet", typeof(List<Point>), typeof(ImgControl), new PropertyMetadata(new List<Point>()));
114
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
115
             "TopRightPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
116
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
117
              "LeftBottomPoint", typeof(Point), typeof(ImgControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
118
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(ImgControl),
119
                        new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
120
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(ImgControl),
121
                        new PropertyMetadata((double)0, OnCenterXYChanged));
122
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(ImgControl),
123
                        new PropertyMetadata((double)0, OnCenterXYChanged));
124

    
125
        #endregion
126
        #region PropertyChanged Method
127

    
128
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
129
        {
130
            //var instance = (ImgControl)sender;
131

    
132
            //if (e.OldValue != e.NewValue && instance.Base_Image != null)
133
            //{
134

    
135
            //    instance.SetValue(e.Property, e.NewValue);
136

    
137
            //    if (instance.IsSelected)
138
            //    {
139
            //        //instance.Base_Image.Stroke = new SolidColorBrush(Colors.Blue);
140
            //    }
141
            //    else
142
            //    {
143
            //        //instance.Base_Image.Stroke = new SolidColorBrush(Colors.Red);
144
            //    }
145
            //}
146
        }
147

    
148
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
149
        {
150
            var instance = (ImgControl)sender;
151
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
152
            {
153
                instance.SetValue(e.Property, e.NewValue);
154
                instance.SetImage();
155
            }
156
        }
157
        public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
158
        {
159
            var instance = (ImgControl)sender;
160
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
161
            {
162
                instance.SetValue(e.Property, e.NewValue);
163
                instance.SetImage();
164
            }
165
        }
166
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
167
        {
168
            var instance = (ImgControl)sender;
169
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
170
            {
171
                instance.SetValue(e.Property, e.NewValue);
172
                instance.SetImage();
173
            }
174
        }
175
        #endregion
176
        #region Properties
177
        public double CenterX
178
        {
179
            get { return (double)GetValue(CenterXProperty); }
180
            set { SetValue(CenterXProperty, value); }
181
        }
182
        public string UserID
183
        {
184
            get { return (string)GetValue(UserIDProperty); }
185
            set
186
            {
187
                if (this.UserID != value)
188
                {
189
                    SetValue(UserIDProperty, value);
190
                    OnPropertyChanged("UserID");
191
                }
192
            }
193
        }
194
       
195
        public double CenterY
196
        {
197
            get { return (double)GetValue(CenterYProperty); }
198
            set { SetValue(CenterYProperty, value); }
199
        }
200
        public string FilePath
201
        {
202
            get { return (string)GetValue(FilePathProperty); }
203
            set { SetValue(FilePathProperty, value); }
204
        }
205
        public Point EndPoint
206
        {
207
            get { return (Point)GetValue(EndPointProperty); }
208
            set
209
            {
210
                if (this.EndPoint != value)
211
                {
212
                    SetValue(EndPointProperty, value);
213
                    OnPropertyChanged("EndPoint");
214
                }
215
            }
216
        }
217
        public List<Point> PointSet
218
        {
219
            get { return (List<Point>)GetValue(PointSetProperty); }
220
            set { SetValue(PointSetProperty, value); }
221
        }
222
        public Point StartPoint
223
        {
224
            get { return (Point)GetValue(StartPointProperty); }
225
            set
226
            {
227
                if (this.StartPoint != value)
228
                {
229
                    SetValue(StartPointProperty, value);
230
                    OnPropertyChanged("StartPoint");
231
                }
232
            }
233
        }
234
        public Point TopRightPoint
235
        {
236
            get { return (Point)GetValue(TopRightPointProperty); }
237
            set
238
            {
239
                SetValue(TopRightPointProperty, value);
240
                OnPropertyChanged("TopRightPoint");
241
            }
242
        }
243
        public Point LeftBottomPoint
244
        {
245
            get { return (Point)GetValue(LeftBottomPointProperty); }
246
            set
247
            {
248
                SetValue(LeftBottomPointProperty, value);
249
                OnPropertyChanged("LeftBottomPoint");
250
            }
251
        }
252

    
253
        public override double CommentAngle
254
        {
255
            get { return (double)GetValue(AngleProperty); }
256
            set
257
            {
258
                if (this.CommentAngle != value)
259
                {
260
                    SetValue(AngleProperty, value);
261
                }
262
            }
263
        }
264
        public ImageSource ImageData
265
        {
266
            get { return (ImageSource)this.GetValue(ImageDataProperty); }
267
            set { this.SetValue(ImageDataProperty, value); }
268
        }
269

    
270
         public override bool IsSelected
271
        {
272
            get
273
            {
274
                return (bool)GetValue(IsSelectedProperty);
275
            }
276
            set
277
            {
278
                SetValue(IsSelectedProperty, value);
279
                //OnPropertyChanged("IsSelected");
280
            }
281
        }
282

    
283
        public override ControlType ControlType
284
        {
285
            set
286
            {
287
                SetValue(ControlTypeProperty, value);
288
                OnPropertyChanged("ControlType");
289
            }
290
            get
291
            {
292
                return (ControlType)GetValue(ControlTypeProperty);
293
            }
294
        }
295

    
296
        public Geometry OverViewPathData
297
        {
298
            get { return (Geometry)GetValue(OverViewPathDataProperty); }
299
            set
300
            {
301
                SetValue(OverViewPathDataProperty, value);
302
                OnPropertyChanged("OverViewPathData");
303
            }
304
        }
305

    
306

    
307
        #endregion
308

    
309
        public override void OnApplyTemplate()
310
        {
311
            base.OnApplyTemplate();
312
            Base_Image = GetTemplateChild(PART_Image) as Image;
313
            Base_Image.Width = 0;
314
            Base_Image.Height = 0;
315
            SetImage();
316
        }
317

    
318
        public void SetImage()
319
        {
320
            this.ApplyTemplate();
321
            Point mid = MathSet.FindCentroid(new List<Point>()
322
            {
323
                this.StartPoint,
324
                this.LeftBottomPoint,
325
                this.EndPoint,
326
                this.TopRightPoint,                
327
            });
328
            double AngleData = this.CommentAngle * -1;
329
            PathFigure pathFigure = new PathFigure();
330

    
331
            pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
332

    
333
            LineSegment lineSegment0 = new LineSegment();
334
            lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
335
            pathFigure.Segments.Add(lineSegment0);
336

    
337
            LineSegment lineSegment1 = new LineSegment();
338
            lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData);
339
            pathFigure.Segments.Add(lineSegment1);
340

    
341
            LineSegment lineSegment2 = new LineSegment();
342
            lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData);
343
            pathFigure.Segments.Add(lineSegment2);
344

    
345
            LineSegment lineSegment3 = new LineSegment();
346
            lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData);
347
            pathFigure.Segments.Add(lineSegment3);
348

    
349
            PathGeometry pathGeometry = new PathGeometry();
350
            pathGeometry.Figures = new PathFigureCollection();
351
            pathFigure.IsClosed = true;
352
            pathGeometry.Figures.Add(pathFigure);
353
            this.Base_Image.Width = pathGeometry.Bounds.Width;
354
            this.Base_Image.Height = pathGeometry.Bounds.Height;
355
             this.Tag = pathGeometry;
356

    
357
            Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_Image.Width / 2);
358
            Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_Image.Height / 2);
359

    
360
            this.PathData = pathGeometry;
361
            ApplyOverViewData();
362

    
363
        }
364
        public override void UpdateControl()
365
        {
366
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
367
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
368
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
369
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
370
        }
371

    
372
        public double LineSize { get; set; }
373

    
374
        public Geometry PathData { get; set; }
375

    
376
        /// <summary>
377
        /// call when mouse is moving while drawing control
378
        /// </summary>
379
        /// <param name="pt"></param>
380
        /// <param name="bAxisLocked"></param>
381
        public override void OnCreatingMouseMove(Point pt, bool bAxisLocked)
382
        {
383
            this.EndPoint = pt;
384
            this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
385
            this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
386
            
387
            this.PointSet = new List<Point>
388
            {
389
                this.StartPoint,
390
                this.LeftBottomPoint,
391
                this.EndPoint,
392
                this.TopRightPoint,
393
            };
394
        }
395

    
396
        /// <summary>
397
        /// move control point has same location of given pt along given delta
398
        /// </summary>
399
        /// <author>humkyung</author>
400
        /// <date>2019.06.20</date>
401
        /// <param name="pt"></param>
402
        /// <param name="dx"></param>
403
        /// <param name="dy"></param>
404
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy, bool bAxisLocked = false)
405
        {
406
            IPath path = (this as IPath);
407

    
408
            Point selected = MathSet.getNearPoint(path.PointSet, pt);
409
            selected.X += dx;
410
            selected.Y += dy;
411

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

    
414
            var OppositeP = (idx + path.PointSet.Count / 2) % path.PointSet.Count;
415
            var PreviousP = (idx + (path.PointSet.Count - 1)) % path.PointSet.Count;
416
            var NextP = (idx + 1) % path.PointSet.Count;
417
            if (bAxisLocked)
418
            {
419
                var PrevV = path.PointSet[PreviousP] - path.PointSet[OppositeP];
420
                double PrevLength = PrevV.Length;
421
                PrevV.Normalize();
422

    
423
                var NextV = path.PointSet[NextP] - path.PointSet[OppositeP];
424
                double NextVLength = NextV.Length;
425
                NextV.Normalize();
426

    
427
                double _dx = selected.X - path.PointSet[OppositeP].X;
428
                double _dy = selected.Y - path.PointSet[OppositeP].Y;
429
                var dir = new Vector(_dx, _dy);
430
                if (PrevLength > NextVLength)
431
                {
432
                    double ratio = NextVLength / PrevLength;
433

    
434
                    double dot = MathSet.DotProduct(PrevV.X, PrevV.Y, dir.X, dir.Y);
435

    
436
                    path.PointSet[PreviousP] = path.PointSet[OppositeP] + PrevV * dot;
437
                    path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * dot * ratio;
438
                    path.PointSet[idx] = path.PointSet[OppositeP] + PrevV * dot + NextV * dot * ratio;
439
                }
440
                else
441
                {
442
                    double ratio = PrevLength / NextVLength;
443

    
444
                    double dot = MathSet.DotProduct(NextV.X, NextV.Y, dir.X, dir.Y);
445

    
446
                    path.PointSet[PreviousP] = path.PointSet[OppositeP] + PrevV * dot * ratio;
447
                    path.PointSet[NextP] = path.PointSet[OppositeP] + NextV * dot;
448
                    path.PointSet[idx] = path.PointSet[OppositeP] + PrevV * dot * ratio + NextV * dot;
449
                }
450
            }
451
            else
452
            {
453
                var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[PreviousP]);
454
                var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[idx].X - path.PointSet[OppositeP].X,
455
                    path.PointSet[idx].Y - path.PointSet[OppositeP].Y);
456
                path.PointSet[PreviousP] = new Point(path.PointSet[OppositeP].X + PreviousV.X * l, path.PointSet[OppositeP].Y + PreviousV.Y * l);
457

    
458
                var NextV = MathSet.GetNormVectorBetween(path.PointSet[OppositeP], path.PointSet[NextP]);
459
                l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[idx].X - path.PointSet[OppositeP].X, path.PointSet
460
                    [idx].Y - path.PointSet[OppositeP].Y);
461
                path.PointSet[NextP] = new Point(path.PointSet[OppositeP].X + NextV.X * l, path.PointSet[OppositeP].Y + NextV.Y * l);
462

    
463
                path.PointSet[idx] = selected;
464
            }
465

    
466
            this.UpdateControl();
467
        }
468

    
469
        /// <summary>
470
        /// return ImgControl's area
471
        /// </summary>
472
        /// <author>humkyung</author>
473
        /// <date>2019.06.13</date>
474
        public override Rect ItemRect
475
        {
476
            get
477
            {
478
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
479
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
480
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
481
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
482

    
483
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
484
            }
485
        }
486

    
487
        /// <summary>
488
        /// Serialize this
489
        /// </summary>
490
        /// <param name="sUserId"></param>
491
        /// <returns></returns>
492
        public override string Serialize()
493
        {
494
            using (S_ImgControl STemp = new S_ImgControl())
495
            {
496
                STemp.Angle = this.CommentAngle;
497
                STemp.EndPoint = this.EndPoint;
498
                STemp.LB = this.LeftBottomPoint;
499
                STemp.Name = this.GetType().Name;
500
                STemp.PointSet = this.PointSet;
501
                STemp.StartPoint = this.StartPoint;
502
                STemp.UserID = this.UserID;
503
                STemp.Opac = this.Opacity;
504
                STemp.TR = this.TopRightPoint;
505
                STemp.ImagePath = this.FilePath;
506
                ///강인구 추가(2017.11.02)
507
                ///Memo 추가
508
                STemp.Memo = this.Memo;
509

    
510
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
511
            }
512
        }
513

    
514
        /// <summary>
515
        /// create a imgcontrol from given string
516
        /// </summary>
517
        /// <param name="str"></param>
518
        /// <returns></returns>
519
        public static ImgControl FromString(string str, SolidColorBrush brush, string sProjectNo, string baseUri)
520
        {
521
            ImgControl instance = null;
522
            using (S_ImgControl s = JsonSerializerHelper.JsonDeserialize<S_ImgControl>(str))
523
            {
524
                UriBuilder downloadUri = new UriBuilder(baseUri);
525
                UriBuilder uri = new UriBuilder(s.ImagePath);
526
                uri.Host = downloadUri.Host;
527
                uri.Port = downloadUri.Port;
528

    
529
                Image img = new Image();
530
                if (s.ImagePath.Contains(".svg"))
531
                {
532
                    SharpVectors.Converters.SvgImageExtension svgImage = new SharpVectors.Converters.SvgImageExtension(uri.ToString());
533
                    var svg = svgImage.ProvideValue(null);
534

    
535
                    img.Source = (DrawingImage)svg;
536
                }
537
                else
538
                {
539
                    img.Source = new BitmapImage(uri.Uri);
540
                }
541

    
542
                instance = new ImgControl
543
                {
544
                    CommentAngle = s.Angle,
545
                    StartPoint = s.StartPoint,
546
                    TopRightPoint = s.TR,
547
                    EndPoint = s.EndPoint,
548
                    LeftBottomPoint = s.LB,
549
                    PointSet = s.PointSet,
550
                    Opacity = s.Opac,
551
                    FilePath = uri.Uri.ToString(),
552
                    UserID = s.UserID,
553
                    ImageData = img.Source,
554
                    Memo = s.Memo 
555
                };
556

    
557
                instance.ImageData = img.Source;
558
            }
559

    
560
            return instance;
561
        }
562
    }
563
}
클립보드 이미지 추가 (최대 크기: 500 MB)