프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (15.8 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
        private const string PART_Image = "PART_Image";
28
        public Image Base_Image = null;
29
        #endregion
30

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

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

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

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

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

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

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

    
239
        private void SetImage()
240
        {
241
            this.ApplyTemplate();
242

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

    
251
            double AngleData = this.Angle * -1;
252

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

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

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

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

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

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

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

    
297
        /// <summary>
298
        /// return SignControl's area
299
        /// </summary>
300
        /// <author>humkyung</author>
301
        /// <date>2019.06.13</date>
302
        public override Rect ItemRect
303
        {
304
            get
305
            {
306
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
307
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
308
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
309
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
310

    
311
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
312
            }
313
        }
314

    
315
        /// <summary>
316
        /// Serialize this
317
        /// </summary>
318
        /// <param name="sUserId"></param>
319
        /// <returns></returns>
320
        public override string Serialize()
321
        {
322
            using (S_SignControl STemp = new S_SignControl())
323
            {
324
                STemp.Angle = this.Angle;
325
                STemp.EndPoint = this.EndPoint;
326
                STemp.UserID = this.UserID;
327
                STemp.LB = this.LeftBottomPoint;
328
                STemp.Name = this.GetType().Name;
329
                STemp.PointSet = this.PointSet;
330
                STemp.StartPoint = this.StartPoint;
331
                STemp.Opac = this.Opacity;
332
                STemp.TR = this.TopRightPoint;
333
                STemp.UserNumber = this.UserNumber == null ? this.UserID: this.UserNumber;
334

    
335
                ///강인구 추가(2017.11.02)
336
                ///Memo 추가
337
                STemp.Memo = this.Memo;
338

    
339
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
340
            }
341
        }
342

    
343
        /// <summary>
344
        /// create a signcontrol from given string
345
        /// </summary>
346
        /// <param name="str"></param>
347
        /// <returns></returns>
348
        public static SignControl FromString(string str, SolidColorBrush brush, string sProjectNo)
349
        {
350
            SignControl instance = null;
351
            using (S_SignControl s = JsonSerializerHelper.JsonDeserialize<S_SignControl>(str))
352
            {
353
                instance = new SignControl
354
                {
355
                    Angle = s.Angle,
356
                    StartPoint = s.StartPoint,
357
                    TopRightPoint = s.TR,
358
                    EndPoint = s.EndPoint,
359
                    LeftBottomPoint = s.LB,
360
                    PointSet = s.PointSet,
361
                    Opacity = s.Opac,
362
                    SignImage = null,
363
                    UserID = s.UserID,
364
                    Memo = s.Memo
365
                };
366

    
367
                if (s.UserNumber != null)
368
                {
369
                    var _sign = GetUserSign.GetSign(s.UserNumber, sProjectNo);
370
                    if (_sign != null)
371
                    {
372
                        byte[] imageBytes = System.Convert.FromBase64String(_sign);
373

    
374
                        System.IO.MemoryStream stream = new System.IO.MemoryStream();
375
                        stream.Write(imageBytes, 0, imageBytes.Length);
376
                        stream.Position = 0;
377
                        System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
378
                        BitmapImage returnImage = new BitmapImage();
379
                        returnImage.BeginInit();
380
                        System.IO.MemoryStream ms = new System.IO.MemoryStream();
381
                        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
382
                        ms.Seek(0, System.IO.SeekOrigin.Begin);
383
                        returnImage.StreamSource = ms;
384
                        returnImage.EndInit();
385
                        stream.Close();
386
                        instance.SignImage = returnImage;
387
                    }
388
                }
389
            }
390

    
391
            return instance;
392
        }
393

    
394
        public double LineSize
395
        {
396
            get;
397
            set;
398
        }
399
        public Geometry PathData
400
        {
401
            get;
402
            set;
403
        }
404
    }
405
}
클립보드 이미지 추가 (최대 크기: 500 MB)