프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Etc / SignControl.cs @ a6272c57

이력 | 보기 | 이력해설 | 다운로드 (17.6 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
//강인구 추가
21
namespace MarkupToPDF.Controls.Etc
22
{
23
    [TemplatePart(Name = "PART_Image", Type = typeof(Image))]
24
    public class SignControl : CommentUserInfo, IDisposable, INormal, INotifyPropertyChanged, IPath, IViewBox, IMarkupCommonData
25
    {
26
        #region 초기선언
27
        public string ProjectNO { get; set; }
28

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

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

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

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

    
90
        }
91
            public static void OnCenterXYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
92
        {
93
            var instance = (SignControl)sender;
94
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
95
            {
96
                instance.SetValue(e.Property, e.NewValue);
97
                instance.SetImage();
98
            }
99
        }
100
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
101
        {
102
            var instance = (SignControl)sender;
103
            if (e.OldValue != e.NewValue && instance.Base_Image != null)
104
            {
105
                instance.SetValue(e.Property, e.NewValue);
106
                instance.SetImage();
107
            }
108
        }
109
        #endregion
110
        #region Properties
111
        public string UserID
112
        {
113
            get { return (string)GetValue(UserIDProperty); }
114
            set
115
            {
116
                if (this.UserID != value)
117
                {
118
                    SetValue(UserIDProperty, value);
119
                    RaisePropertyChanged("UserID");
120
                }
121
            }
122
        }
123
        public double CenterX
124
        {
125
            get { return (double)GetValue(CenterXProperty); }
126
            set { SetValue(CenterXProperty, value); }
127
        }
128
        public double CenterY
129
        {
130
            get { return (double)GetValue(CenterYProperty); }
131
            set { SetValue(CenterYProperty, value); }
132
        }
133
        public Point EndPoint
134
        {
135
            get { return (Point)GetValue(EndPointProperty); }
136
            set
137
            {
138
                if (this.EndPoint != value)
139
                {
140
                    SetValue(EndPointProperty, value);
141
                    RaisePropertyChanged("EndPoint");
142
                }
143
            }
144
        }
145
        public List<Point> PointSet
146
        {
147
            get { return (List<Point>)GetValue(PointSetProperty); }
148
            set { SetValue(PointSetProperty, value); }
149
        }
150
        public Point StartPoint
151
        {
152
            get { return (Point)GetValue(StartPointProperty); }
153
            set
154
            {
155
                if (this.StartPoint != value)
156
                {
157
                    SetValue(StartPointProperty, value);
158
                    RaisePropertyChanged("StartPoint");
159
                }
160
            }
161
        }
162
        public string UserNumber
163
        {
164
            get { return (string)GetValue(UserNubmerProperty); }
165
            set
166
            {
167
                if (this.UserNumber != value)
168
                {
169
                    SetValue(UserNubmerProperty, value);
170
                    RaisePropertyChanged("UserNumber");
171
                }
172
            }
173
        }
174
        public Point TopRightPoint
175
        {
176
            get { return (Point)GetValue(TopRightPointProperty); }
177
            set
178
            {
179
                SetValue(TopRightPointProperty, value);
180
                RaisePropertyChanged("TopRightPoint");
181
            }
182
        }
183
        public Point LeftBottomPoint
184
        {
185
            get { return (Point)GetValue(LeftBottomPointProperty); }
186
            set
187
            {
188
                SetValue(LeftBottomPointProperty, value);
189
                RaisePropertyChanged("LeftBottomPoint");
190
            }
191
        }
192
        public double Angle
193
        {
194
            get { return (double)GetValue(AngleProperty); }
195
            set
196
            {
197
                if (this.Angle != value)
198
                {
199
                    SetValue(AngleProperty, value);
200
                }
201
            }
202
        }
203
        public ImageSource SignImage
204
        {
205
            get { return (ImageSource)this.GetValue(SignImageProperty); }
206
            set { this.SetValue(SignImageProperty, value); }
207
        }
208

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

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

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

    
241
        private void SetImage()
242
        {
243
            this.ApplyTemplate();
244

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

    
253
            double AngleData = this.Angle * -1;
254

    
255
            PathFigure pathFigure = new PathFigure();
256
            pathFigure.StartPoint = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
257

    
258
            LineSegment lineSegment0 = new LineSegment();
259
            lineSegment0.Point = MathSet.RotateAbout(mid, this.StartPoint, AngleData);
260
            pathFigure.Segments.Add(lineSegment0);
261

    
262
            LineSegment lineSegment1 = new LineSegment();
263
            lineSegment1.Point = MathSet.RotateAbout(mid, this.LeftBottomPoint, AngleData);
264
            pathFigure.Segments.Add(lineSegment1);
265

    
266
            LineSegment lineSegment2 = new LineSegment();
267
            lineSegment2.Point = MathSet.RotateAbout(mid, this.EndPoint, AngleData);
268
            pathFigure.Segments.Add(lineSegment2);
269

    
270
            LineSegment lineSegment3 = new LineSegment();
271
            lineSegment3.Point = MathSet.RotateAbout(mid, this.TopRightPoint, AngleData);
272
            pathFigure.Segments.Add(lineSegment3);
273

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

    
282
            Canvas.SetLeft(this, MathSet.RotateAbout(mid, mid, AngleData).X - this.Base_Image.Width / 2);
283
            Canvas.SetTop(this, MathSet.RotateAbout(mid, mid, AngleData).Y - this.Base_Image.Height / 2);
284
        }
285
        public event PropertyChangedEventHandler PropertyChanged;
286
        protected void RaisePropertyChanged(string propName)
287
        {
288
            if (PropertyChanged != null)
289
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
290
        }
291
        public void updateControl()
292
        {
293
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
294
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
295
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
296
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
297
        }
298

    
299
        /// <summary>
300
        /// call when mouse is moving while drawing control
301
        /// </summary>
302
        /// <author>humkyung</author>
303
        /// <param name="pt"></param>
304
        /// <param name="bAxisLocked"></param>
305
        public override void OnCreatingMouseMove(Point pt, bool bAxisLocked, bool bShiftKeyPressed)
306
        {
307
            this.EndPoint = pt;
308
            this.LeftBottomPoint = new Point(this.StartPoint.X, this.EndPoint.Y);
309
            this.TopRightPoint = new Point(this.EndPoint.X, this.StartPoint.Y);
310
            
311
            if (this.StartPoint != this.EndPoint && this.SignImage == null)
312
            {
313
                var _sign = GetUserSign.GetSign(this.UserNumber, this.ProjectNO);
314
                byte[] imageBytes = System.Convert.FromBase64String(_sign);
315
                System.IO.MemoryStream stream = new System.IO.MemoryStream();
316
                stream.Write(imageBytes, 0, imageBytes.Length);
317
                stream.Position = 0;
318
                System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
319
                BitmapImage returnImage = new BitmapImage();
320
                returnImage.BeginInit();
321
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
322
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
323
                ms.Seek(0, System.IO.SeekOrigin.Begin);
324
                returnImage.StreamSource = ms;
325
                returnImage.EndInit();
326
                stream.Close();
327

    
328
                this.SignImage = returnImage;
329
            }
330
            this.PointSet = new List<Point>
331
            {
332
                this.StartPoint,
333
                this.LeftBottomPoint,
334
                this.EndPoint,
335
                this.TopRightPoint,
336
            };
337
        }
338

    
339
        /// <summary>
340
        /// return SignControl's area
341
        /// </summary>
342
        /// <author>humkyung</author>
343
        /// <date>2019.06.13</date>
344
        public override Rect ItemRect
345
        {
346
            get
347
            {
348
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
349
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
350
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
351
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
352

    
353
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
354
            }
355
        }
356

    
357
        /// <summary>
358
        /// Serialize this
359
        /// </summary>
360
        /// <param name="sUserId"></param>
361
        /// <returns></returns>
362
        public override string Serialize()
363
        {
364
            using (S_SignControl STemp = new S_SignControl())
365
            {
366
                STemp.Angle = this.Angle;
367
                STemp.EndPoint = this.EndPoint;
368
                STemp.UserID = this.UserID;
369
                STemp.LB = this.LeftBottomPoint;
370
                STemp.Name = this.GetType().Name;
371
                STemp.PointSet = this.PointSet;
372
                STemp.StartPoint = this.StartPoint;
373
                STemp.Opac = this.Opacity;
374
                STemp.TR = this.TopRightPoint;
375
                STemp.UserNumber = this.UserNumber == null ? this.UserID: this.UserNumber;
376

    
377
                ///강인구 추가(2017.11.02)
378
                ///Memo 추가
379
                STemp.Memo = this.Memo;
380

    
381
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
382
            }
383
        }
384

    
385
        /// <summary>
386
        /// create a signcontrol from given string
387
        /// </summary>
388
        /// <param name="str"></param>
389
        /// <returns></returns>
390
        public static SignControl FromString(string str, SolidColorBrush brush, string sProjectNo)
391
        {
392
            SignControl instance = null;
393
            using (S_SignControl s = JsonSerializerHelper.JsonDeserialize<S_SignControl>(str))
394
            {
395
                instance = new SignControl
396
                {
397
                    Angle = s.Angle,
398
                    StartPoint = s.StartPoint,
399
                    TopRightPoint = s.TR,
400
                    EndPoint = s.EndPoint,
401
                    LeftBottomPoint = s.LB,
402
                    PointSet = s.PointSet,
403
                    Opacity = s.Opac,
404
                    SignImage = null,
405
                    UserID = s.UserID,
406
                    Memo = s.Memo
407
                };
408

    
409
                if (s.UserNumber != null)
410
                {
411
                    var _sign = GetUserSign.GetSign(s.UserNumber, sProjectNo);
412
                    if (_sign != null)
413
                    {
414
                        byte[] imageBytes = System.Convert.FromBase64String(_sign);
415

    
416
                        System.IO.MemoryStream stream = new System.IO.MemoryStream();
417
                        stream.Write(imageBytes, 0, imageBytes.Length);
418
                        stream.Position = 0;
419
                        System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
420
                        BitmapImage returnImage = new BitmapImage();
421
                        returnImage.BeginInit();
422
                        System.IO.MemoryStream ms = new System.IO.MemoryStream();
423
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
424
                        ms.Seek(0, System.IO.SeekOrigin.Begin);
425
                        returnImage.StreamSource = ms;
426
                        returnImage.EndInit();
427
                        stream.Close();
428
                        instance.SignImage = returnImage;
429
                    }
430
                }
431
            }
432

    
433
            return instance;
434
        }
435

    
436
        public double LineSize
437
        {
438
            get;
439
            set;
440
        }
441
        public Geometry PathData
442
        {
443
            get;
444
            set;
445
        }
446
    }
447
}
클립보드 이미지 추가 (최대 크기: 500 MB)