프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Etc / SignControl.cs @ 366f00c2

이력 | 보기 | 이력해설 | 다운로드 (19.9 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 System.Threading.Tasks;
20

    
21
//강인구 추가
22
namespace MarkupToPDF.Controls.Etc
23
{
24
    [TemplatePart(Name = "PART_Image", Type = typeof(Image))]
25
    public class SignControl : CommentUserInfo, IDisposable, INormal, INotifyPropertyChanged, IPath, IViewBox, IMarkupCommonData
26
    {
27
        #region 초기선언
28
        public string ProjectNO { get; set; }
29

    
30
        private const string PART_Image = "PART_Image";
31
        public Image Base_Image = null;
32
        #endregion
33

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

    
44
        public void Dispose()
45
        {
46
            //GC.Collect();
47
            //GC.SuppressFinalize(this);
48
            this.Base_Image = null;
49
        }
50
        #region Dependency Properties
51
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
52
                "UserID", typeof(string), typeof(SignControl), new PropertyMetadata(null));
53
        public static readonly DependencyProperty SignImageProperty = DependencyProperty.Register(
54
            "SignImage", typeof(ImageSource), typeof(SignControl), new PropertyMetadata(null));
55
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
56
            "StartPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
57
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
58
            "EndPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
59
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
60
            "PointSet", typeof(List<Point>), typeof(SignControl), new PropertyMetadata(new List<Point>()));
61
        public static readonly DependencyProperty UserNubmerProperty = DependencyProperty.Register(
62
            "UserNumber", typeof(string), typeof(SignControl), new PropertyMetadata(null));
63
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
64
             "TopRightPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
65
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
66
              "LeftBottomPoint", typeof(Point), typeof(SignControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
67
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register("Angle", typeof(double), typeof(SignControl),
68
                        new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
69
        public static readonly DependencyProperty CenterXProperty = DependencyProperty.Register("CenterX", typeof(double), typeof(SignControl),
70
                        new PropertyMetadata((double)0, OnCenterXYChanged));
71
        public static readonly DependencyProperty CenterYProperty = DependencyProperty.Register("CenterY", typeof(double), typeof(SignControl),
72
                        new PropertyMetadata((double)0, OnCenterXYChanged));
73
        public static readonly DependencyProperty IsSelectedProperty =
74
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(SignControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
75

    
76
        public static readonly DependencyProperty ControlTypeProperty =
77
            DependencyProperty.Register("ControlType", typeof(ControlType), typeof(SignControl), new FrameworkPropertyMetadata(ControlType.Sign));
78
        #endregion
79
        #region PropertyChanged Method
80
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
81
        {
82
            var instance = (SignControl)sender;
83
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
84
            {
85
                instance.SetValue(e.Property, e.NewValue);
86
                instance.SetImage();
87
            }
88
        }
89
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
90
        {
91

    
92
        }
93
            public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
94
        {
95
            var instance = (SignControl)sender;
96
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
97
            {
98
                instance.SetValue(e.Property, e.NewValue);
99
                instance.SetImage();
100
            }
101
        }
102
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
103
        {
104
            var instance = (SignControl)sender;
105
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
106
            {
107
                instance.SetValue(e.Property, e.NewValue);
108
                instance.SetImage();
109
            }
110
        }
111
        #endregion
112
        #region Properties
113
        public string UserID
114
        {
115
            get { return (string)GetValue(UserIDProperty); }
116
            set
117
            {
118
                if (this.UserID != value)
119
                {
120
                    SetValue(UserIDProperty, value);
121
                    RaisePropertyChanged("UserID");
122
                }
123
            }
124
        }
125

    
126
        public double CenterX
127
        {
128
            get { return (double)GetValue(CenterXProperty); }
129
            set { SetValue(CenterXProperty, value); }
130
        }
131
        public double CenterY
132
        {
133
            get { return (double)GetValue(CenterYProperty); }
134
            set { SetValue(CenterYProperty, value); }
135
        }
136
        public Point EndPoint
137
        {
138
            get { return (Point)GetValue(EndPointProperty); }
139
            set
140
            {
141
                if (this.EndPoint != value)
142
                {
143
                    SetValue(EndPointProperty, value);
144
                    RaisePropertyChanged("EndPoint");
145
                }
146
            }
147
        }
148
        public List<Point> PointSet
149
        {
150
            get { return (List<Point>)GetValue(PointSetProperty); }
151
            set { SetValue(PointSetProperty, value); }
152
        }
153
        public Point StartPoint
154
        {
155
            get { return (Point)GetValue(StartPointProperty); }
156
            set
157
            {
158
                if (this.StartPoint != value)
159
                {
160
                    SetValue(StartPointProperty, value);
161
                    RaisePropertyChanged("StartPoint");
162
                }
163
            }
164
        }
165
        public string UserNumber
166
        {
167
            get { return (string)GetValue(UserNubmerProperty); }
168
            set
169
            {
170
                if (this.UserNumber != value)
171
                {
172
                    SetValue(UserNubmerProperty, value);
173
                    RaisePropertyChanged("UserNumber");
174
                }
175
            }
176
        }
177
        public Point TopRightPoint
178
        {
179
            get { return (Point)GetValue(TopRightPointProperty); }
180
            set
181
            {
182
                SetValue(TopRightPointProperty, value);
183
                RaisePropertyChanged("TopRightPoint");
184
            }
185
        }
186
        public Point LeftBottomPoint
187
        {
188
            get { return (Point)GetValue(LeftBottomPointProperty); }
189
            set
190
            {
191
                SetValue(LeftBottomPointProperty, value);
192
                RaisePropertyChanged("LeftBottomPoint");
193
            }
194
        }
195
        public override double CommentAngle
196
        {
197
            get { return (double)GetValue(AngleProperty); }
198
            set
199
            {
200
                if (this.CommentAngle != value)
201
                {
202
                    SetValue(AngleProperty, value);
203
                }
204
            }
205
        }
206
        public ImageSource SignImage
207
        {
208
            get { return (ImageSource)this.GetValue(SignImageProperty); }
209
            set { this.SetValue(SignImageProperty, value); }
210
        }
211

    
212
        public override bool IsSelected
213
        {
214
            get
215
            {
216
                return (bool)GetValue(IsSelectedProperty);
217
            }
218
            set
219
            {
220
                SetValue(IsSelectedProperty, value);
221
            }
222
        }
223

    
224
        public override ControlType ControlType
225
        {
226
            set
227
            {
228
                SetValue(ControlTypeProperty, value);
229
            }
230
            get
231
            {
232
                return (ControlType)GetValue(ControlTypeProperty);
233
            }
234
        }
235

    
236
        #endregion
237
        public override void OnApplyTemplate()
238
        {
239
            base.OnApplyTemplate();
240
            Base_Image = GetTemplateChild(PART_Image) as Image;
241
            SetImage();
242
        }
243

    
244
        private void SetImage()
245
        {
246
            this.ApplyTemplate();
247

    
248
            Point mid = MathSet.FindCentroid(new List<Point>()
249
            {
250
                this.StartPoint,
251
                this.LeftBottomPoint,
252
                this.EndPoint,
253
                this.TopRightPoint,                
254
            });
255

    
256
            double AngleData = this.CommentAngle * -1;
257

    
258
            PathFigure pathFigure = new PathFigure();
259
            pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
260

    
261
            LineSegment lineSegment0 = new LineSegment();
262
            lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
263
            pathFigure.Segments.Add(lineSegment0);
264

    
265
            LineSegment lineSegment1 = new LineSegment();
266
            lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData);
267
            pathFigure.Segments.Add(lineSegment1);
268

    
269
            LineSegment lineSegment2 = new LineSegment();
270
            lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData);
271
            pathFigure.Segments.Add(lineSegment2);
272

    
273
            LineSegment lineSegment3 = new LineSegment();
274
            lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData);
275
            pathFigure.Segments.Add(lineSegment3);
276

    
277
            PathGeometry pathGeometry = new PathGeometry();
278
            pathGeometry.Figures = new PathFigureCollection();
279
            pathFigure.IsClosed = true;
280
            pathGeometry.Figures.Add(pathFigure);
281
            this.Base_Image.Width = pathGeometry.Bounds.Width;
282
            this.Base_Image.Height = pathGeometry.Bounds.Height;
283
            this.Tag = pathGeometry;
284

    
285
            Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_Image.Width / 2);
286
            Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_Image.Height / 2);
287
        }
288
        public event PropertyChangedEventHandler PropertyChanged;
289
        protected void RaisePropertyChanged(string propName)
290
        {
291
            if (PropertyChanged != null)
292
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
293
        }
294

    
295
        public override void UpdateControl()
296
        {
297
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
298
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
299
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
300
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
301
        }
302

    
303
        /// <summary>
304
        /// call when mouse is moving while drawing control
305
        /// </summary>
306
        /// <author>humkyung</author>
307
        /// <param name="pt"></param>
308
        /// <param name="bAxisLocked"></param>
309
        public override  void OnCreatingMouseMove(Point pt, bool bAxisLocked, bool bShiftKeyPressed)
310
        {
311
            this.EndPoint = pt;
312
            this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
313
            this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
314
            
315
            if (this.StartPoint != this.EndPoint && this.SignImage == null)
316
            {
317
                var _sign = GetUserSign.GetSign(this.UserNumber, this.ProjectNO);
318

    
319
                byte[] imageBytes = System.Convert.FromBase64String(_sign);
320

    
321
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
322
                stream.Write(imageBytes, 0, imageBytes.Length);
323
                stream.Position = 0;
324
                System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
325
                BitmapImage returnImage = new BitmapImage();
326
                returnImage.BeginInit();
327
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
328
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
329
                ms.Seek(0, System.IO.SeekOrigin.Begin);
330
                returnImage.StreamSource = ms;
331
                returnImage.EndInit();
332
                stream.Close();
333

    
334
                this.SignImage = returnImage;
335
            }
336

    
337
            this.PointSet = new List<Point>
338
            {
339
                this.StartPoint,
340
                this.LeftBottomPoint,
341
                this.EndPoint,
342
                this.TopRightPoint,
343
            };
344
        }
345

    
346
        /// <summary>
347
        /// move control point has same location of given pt along given delta
348
        /// </summary>
349
        /// <author>humkyung</author>
350
        /// <date>2019.06.20</date>
351
        /// <param name="pt"></param>
352
        /// <param name="dx"></param>
353
        /// <param name="dy"></param>
354
        public override void OnMoveCtrlPoint(Point pt, double dx, double dy)
355
        {
356
            IPath path = (this as IPath);
357

    
358
            Point selected = MathSet.getNearPoint(path.PointSet, pt);
359
            selected.X += dx;
360
            selected.Y += dy;
361
            int i = 0;
362
            for (i = 0; i < (this as IPath).PointSet.Count; i++)
363
            {
364
                if (pt.Equals((this as IPath).PointSet[i]))
365
                {
366
                    path.PointSet[i] = selected;
367
                    break;
368
                }
369
            }
370

    
371
            var ReverseP = (i + path.PointSet.Count() / 2) % path.PointSet.Count();
372
            var PreviousP = (i + (path.PointSet.Count() - 1)) % path.PointSet.Count();
373
            var NextP = (i + 1) % path.PointSet.Count();
374

    
375
            var distance = MathSet.DistanceTo(path.PointSet[ReverseP], path.PointSet[i]);
376

    
377
            var PreviousV = MathSet.GetNormVectorBetween(path.PointSet[ReverseP], path.PointSet[PreviousP]);
378
            var l = MathSet.DotProduct(PreviousV.X, PreviousV.Y, path.PointSet[i].X - path.PointSet[ReverseP].X,
379
                path.PointSet[i].Y - path.PointSet[ReverseP].Y);
380
            path.PointSet[PreviousP] = new Point(path.PointSet[ReverseP].X + PreviousV.X * l, path.PointSet[ReverseP].Y + PreviousV.Y * l);
381

    
382
            var NextV = MathSet.GetNormVectorBetween(path.PointSet[ReverseP], path.PointSet[NextP]);
383
            l = MathSet.DotProduct(NextV.X, NextV.Y, path.PointSet[i].X - path.PointSet[ReverseP].X, path.PointSet
384
                [i].Y - path.PointSet[ReverseP].Y);
385
            path.PointSet[NextP] = new Point(path.PointSet[ReverseP].X + NextV.X * l, path.PointSet[ReverseP].Y + NextV.Y * l);
386

    
387
            this.UpdateControl();
388
        }
389

    
390
        /// <summary>
391
        /// return SignControl's area
392
        /// </summary>
393
        /// <author>humkyung</author>
394
        /// <date>2019.06.13</date>
395
        public override Rect ItemRect
396
        {
397
            get
398
            {
399
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
400
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
401
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
402
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
403

    
404
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
405
            }
406
        }
407

    
408
        /// <summary>
409
        /// Serialize this
410
        /// </summary>
411
        /// <param name="sUserId"></param>
412
        /// <returns></returns>
413
        public override string Serialize()
414
        {
415
            using (S_SignControl STemp = new S_SignControl())
416
            {
417
                STemp.Angle = this.CommentAngle;
418
                STemp.EndPoint = this.EndPoint;
419
                STemp.UserID = this.UserID;
420
                STemp.LB = this.LeftBottomPoint;
421
                STemp.Name = this.GetType().Name;
422
                STemp.PointSet = this.PointSet;
423
                STemp.StartPoint = this.StartPoint;
424
                STemp.Opac = this.Opacity;
425
                STemp.TR = this.TopRightPoint;
426
                STemp.UserNumber = this.UserNumber == null ? this.UserID: this.UserNumber;
427

    
428
                ///강인구 추가(2017.11.02)
429
                ///Memo 추가
430
                STemp.Memo = this.Memo;
431

    
432
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
433
            }
434
        }
435

    
436
        /// <summary>
437
        /// create a signcontrol from given string
438
        /// </summary>
439
        /// <param name="str"></param>
440
        /// <returns></returns>
441
        public static SignControl FromString(string str, SolidColorBrush brush, string sProjectNo)
442
        {
443
            SignControl instance = null;
444
            using (S_SignControl s = JsonSerializerHelper.JsonDeserialize<S_SignControl>(str))
445
            {
446
                instance = new SignControl
447
                {
448
                    CommentAngle = s.Angle,
449
                    StartPoint = s.StartPoint,
450
                    TopRightPoint = s.TR,
451
                    EndPoint = s.EndPoint,
452
                    LeftBottomPoint = s.LB,
453
                    PointSet = s.PointSet,
454
                    Opacity = s.Opac,
455
                    SignImage = null,
456
                    UserID = s.UserID,
457
                    UserNumber = s.UserNumber,
458
                    Memo = s.Memo
459
                };
460

    
461
                if (s.UserNumber != null)
462
                {
463
                    var _sign = GetUserSign.GetSign(s.UserNumber, sProjectNo);
464
                    if (_sign != null)
465
                    {
466
                        byte[] imageBytes = System.Convert.FromBase64String(_sign);
467

    
468
                        BitmapImage returnImage = new BitmapImage();
469

    
470
                        using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
471
                        {
472
                            stream.WriteAsync(imageBytes, 0, imageBytes.Length);
473

    
474
                            stream.Position = 0;
475
                            System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
476
                            returnImage.BeginInit();
477
                            System.IO.MemoryStream ms = new System.IO.MemoryStream();
478
                            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
479
                            ms.Seek(0, System.IO.SeekOrigin.Begin);
480
                            returnImage.StreamSource = ms;
481
                            returnImage.EndInit();
482
                            stream.Close();
483
                        }
484

    
485
                        instance.SignImage = returnImage;
486
                    }
487
                }
488
            }
489

    
490
            return instance;
491
        }
492

    
493
        public double LineSize
494
        {
495
            get;
496
            set;
497
        }
498
        public Geometry PathData
499
        {
500
            get;
501
            set;
502
        }
503
    }
504
}
클립보드 이미지 추가 (최대 크기: 500 MB)