프로젝트

일반

사용자정보

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

markus / MarkupToPDF / Controls / Shape / RectangleControl.cs @ f513c215

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

1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.Windows;
8
using System.Windows.Shapes;
9
using System.Windows.Controls;
10
using System.Windows.Media;
11
using MarkupToPDF.Controls.Common;
12
using MarkupToPDF.Common;
13
using MarkupToPDF.Serialize.Core;
14
using MarkupToPDF.Serialize.S_Control;
15

    
16
namespace MarkupToPDF.Controls.Shape
17
{
18
//강인구 추가 IShapeControl
19
    public class RectangleControl : CommentUserInfo, IDisposable, INotifyPropertyChanged, IMarkupCommonData, IShapeControl, IDashControl
20
    {
21
        public Path Base_RectPath { get; set; }
22
        private const string PART_RectPath = "PART_RectPath";
23

    
24
        #region Property
25

    
26

    
27
        public MouseMode mousemode
28
        {
29
            get
30
            {
31
                return (MouseMode)GetValue(mousemodeProperty);
32
            }
33
            set
34
            {
35
                SetValue(mousemodeProperty, value);
36
                OnPropertyChanged("mousemode");
37
            }
38
        }
39

    
40
        public override SolidColorBrush StrokeColor
41
        {
42
            get
43
            {
44
                return (SolidColorBrush)GetValue(StrokeColorProperty);
45
            }
46
            set
47
            {
48
                SetValue(StrokeColorProperty, value);
49
                OnPropertyChanged("StrokeColor");
50
            }
51
        }
52

    
53
        public Double LineSize
54
        {
55
            get
56
            {
57
                return (Double)GetValue(LineSizeProperty);
58
            }
59
            set
60
            {
61
                SetValue(LineSizeProperty, value);
62
                OnPropertyChanged("LineSize");
63
            }
64
        }
65

    
66
        public Geometry PathData
67
        {
68
            get
69
            {
70
                return (Geometry)GetValue(PathDataProperty);
71
            }
72
            set
73
            {
74
                SetValue(PathDataProperty, value);
75
                OnPropertyChanged("PathData");
76
            }
77

    
78
        }
79

    
80
        public Geometry OverViewPathData
81
        {
82
            get
83
            {
84
                return (Geometry)GetValue(OverViewPathDataProperty);
85
            }
86
            set
87
            {
88
                SetValue(OverViewPathDataProperty, value);
89
                OnPropertyChanged("OverViewPathData");
90
            }
91
        }
92

    
93
        public SolidColorBrush FillColor
94
        {
95
            get { return (SolidColorBrush)GetValue(FillColorProperty); }
96
            set
97
            {
98
                SetValue(FillColorProperty, value);
99
                OnPropertyChanged("FillColor");
100
            }
101
        }
102
		//강인구 추가
103
        public DoubleCollection DashSize
104
        {
105
            get { return (DoubleCollection)GetValue(DashSizeProperty); }
106
            set
107
            {
108
                if (this.DashSize != value)
109
                {
110
                    SetValue(DashSizeProperty, value);
111
                }
112
            }
113
        }
114

    
115
        public Point StartPoint
116
        {
117
            get
118
            {
119
                return (Point)GetValue(StartPointProperty);
120
            }
121
            set
122
            {
123
                SetValue(StartPointProperty, value);
124
                OnPropertyChanged("StartPoint");
125
            }
126
        }
127

    
128
        public Point SetPoint
129
        {
130
            get
131
            {
132
                return (Point)GetValue(SetPointProperty);
133
            }
134
            set
135
            {
136
                SetValue(SetPointProperty, value);
137
                OnPropertyChanged("SetPoint");
138
            }
139
        }
140

    
141
        public Point OriginPoint
142
        {
143
            get
144
            {
145
                return (Point)GetValue(OriginPointProperty);
146
            }
147
            set
148
            {
149
                SetValue(OriginPointProperty, value);
150
                OnPropertyChanged("OriginPoint");
151
            }
152
        }
153

    
154
        public Point TopRightPoint
155
        {
156
            get
157
            {
158
                return (Point)GetValue(TopRightPointProperty);
159
            }
160
            set
161
            {
162
                SetValue(TopRightPointProperty, value);
163
                OnPropertyChanged("TopRightPoint");
164
            }
165
        }
166

    
167
        public Point LeftBottomPoint
168
        {
169
            get
170
            {
171
                return (Point)GetValue(LeftBottomPointProperty);
172
            }
173
            set
174
            {
175
                SetValue(LeftBottomPointProperty, value);
176
                OnPropertyChanged("LeftBottomPoint");
177
            }
178
        }
179

    
180
        public Point EndPoint
181
        {
182
            get
183
            {
184
                return (Point)GetValue(EndPointProperty);
185
            }
186
            set
187
            {
188
                SetValue(EndPointProperty, value);
189
                OnPropertyChanged("EndPoint");
190
            }
191
        }
192

    
193
        public override bool IsSelected
194
        {
195
            get
196
            {
197
                return (bool)GetValue(IsSelectedProperty);
198
            }
199
            set
200
            {
201
                SetValue(IsSelectedProperty, value);
202
                OnPropertyChanged("IsSelected");
203
            }
204
        }
205

    
206
        public override ControlType ControlType
207
        {
208
            set
209
            {
210
                SetValue(ControlTypeProperty, value);
211
                OnPropertyChanged("ControlType");
212
            }
213
            get
214
            {
215
                return (ControlType)GetValue(ControlTypeProperty);
216
            }
217
        }
218

    
219
        public List<Point> PointSet
220
        {
221
            get { return (List<Point>)GetValue(PointSetProperty); }
222
            set { SetValue(PointSetProperty, value); }
223
        }
224

    
225

    
226
        public double CanvasX
227
        {
228
            get { return (double)GetValue(CanvasXProperty); }
229
            set
230
            {
231
                if (this.CanvasX != value)
232
                {
233
                    SetValue(CanvasXProperty, value);
234
                    OnPropertyChanged("CanvasX");
235
                }
236
            }
237
        }
238

    
239
        public double CanvasY
240
        {
241
            get { return (double)GetValue(CanvasYProperty); }
242
            set
243
            {
244
                if (this.CanvasY != value)
245
                {
246
                    SetValue(CanvasYProperty, value);
247
                    OnPropertyChanged("CanvasY");
248
                }
249
            }
250
        }
251

    
252
        public PaintSet Paint
253
        {
254
            get { return (PaintSet)GetValue(PaintProperty); }
255
            set
256
            {
257
                if (this.Paint != value)
258
                {
259
                    SetValue(PaintProperty, value);
260
                }
261
            }
262
        }
263

    
264
        public double Angle
265
        {
266
            get { return (double)GetValue(AngleProperty); }
267
            set
268
            {
269
                if (this.Angle != value)
270
                {
271
                    SetValue(AngleProperty, value);
272
                }
273
            }
274
        }
275
        public string UserID
276
        {
277
            get { return (string)GetValue(UserIDProperty); }
278
            set
279
            {
280
                if (this.UserID != value)
281
                {
282
                    SetValue(UserIDProperty, value);
283
                    OnPropertyChanged("UserID");
284
                }
285
            }
286
        }
287

    
288
        #endregion
289

    
290
        #region Dependency Property
291

    
292

    
293
        public static readonly DependencyProperty UserIDProperty = DependencyProperty.Register(
294
        "UserID", typeof(string), typeof(RectangleControl), new PropertyMetadata(null));
295

    
296
        public static readonly DependencyProperty mousemodeProperty =
297
                DependencyProperty.Register("mousemode", typeof(MouseMode), typeof(RectCloudControl), new PropertyMetadata(MouseMode.None, PointValueChanged));
298

    
299
        public static readonly DependencyProperty IsSelectedProperty =
300
          DependencyProperty.Register("IsSelected", typeof(bool), typeof(RectangleControl), new FrameworkPropertyMetadata(false, IsSelectedChanged));
301

    
302
        public static readonly DependencyProperty ControlTypeProperty =
303
                DependencyProperty.Register("ControlType", typeof(ControlType), typeof(RectangleControl), new FrameworkPropertyMetadata(ControlType.Rectangle));
304

    
305
        public static readonly DependencyProperty StrokeColorProperty = DependencyProperty.Register(
306
                "StrokeColor", typeof(SolidColorBrush), typeof(RectangleControl), new PropertyMetadata(new SolidColorBrush(Colors.Red)));
307

    
308
        public static readonly DependencyProperty LineSizeProperty = DependencyProperty.Register(
309
              "LineSize", typeof(double), typeof(RectangleControl), new PropertyMetadata((Double)3));
310

    
311
        //강인구 추가
312
        public static readonly DependencyProperty DashSizeProperty = DependencyProperty.Register(
313
                "DashSize", typeof(DoubleCollection), typeof(RectangleControl), new PropertyMetadata(new DoubleCollection { 1, 1 }, PointValueChanged));
314
        public static readonly DependencyProperty PathDataProperty = DependencyProperty.Register(
315
               "PathData", typeof(Geometry), typeof(RectangleControl), null);
316

    
317
        public static readonly DependencyProperty OverViewPathDataProperty = DependencyProperty.Register(
318
               "OverViewPathData", typeof(Geometry), typeof(RectangleControl), null);
319

    
320
        //강인구 추가
321
        public static readonly DependencyProperty PaintProperty = DependencyProperty.Register(
322
        "Paint", typeof(PaintSet), typeof(RectangleControl), new PropertyMetadata(PaintSet.None, PointValueChanged));
323

    
324

    
325
        public static readonly DependencyProperty FillColorProperty = DependencyProperty.Register(
326
                "FillColor", typeof(SolidColorBrush), typeof(RectangleControl), new PropertyMetadata(new SolidColorBrush(Colors.White)));
327

    
328
        public static readonly DependencyProperty CanvasXProperty = DependencyProperty.Register(
329
                 "CanvasX", typeof(double), typeof(RectangleControl), new PropertyMetadata((double)0, OnSetCansvasChanged));
330

    
331
        public static readonly DependencyProperty CanvasYProperty = DependencyProperty.Register(
332
                "CanvasY", typeof(double), typeof(RectangleControl), new PropertyMetadata((double)0, OnSetCansvasChanged));
333

    
334
        public static readonly DependencyProperty TopRightPointProperty = DependencyProperty.Register(
335
               "TopRightPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
336

    
337
        public static readonly DependencyProperty LeftBottomPointProperty = DependencyProperty.Register(
338
                 "LeftBottomPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
339

    
340
        public static readonly DependencyProperty StartPointProperty = DependencyProperty.Register(
341
            "StartPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
342

    
343
        public static readonly DependencyProperty SetPointProperty = DependencyProperty.Register(
344
        "SetPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
345

    
346
        public static readonly DependencyProperty OriginPointProperty = DependencyProperty.Register(
347
        "OriginPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
348
        public static readonly DependencyProperty AngleProperty = DependencyProperty.Register(
349
            "Angle", typeof(double), typeof(RectangleControl), new PropertyMetadata((double)0.0, new PropertyChangedCallback(AngleValueChanged)));
350
        public static readonly DependencyProperty PointSetProperty = DependencyProperty.Register(
351
         "PointSet", typeof(List<Point>), typeof(RectangleControl), new PropertyMetadata(new List<Point>()));
352

    
353
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register(
354
              "EndPoint", typeof(Point), typeof(RectangleControl), new PropertyMetadata(new Point(0, 0), PointValueChanged));
355

    
356
        #endregion  Dependency Property
357

    
358
        #region Dependency PropertyChanged
359
        public static void OnSetCansvasChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
360
        {
361
            var instance = (RectangleControl)sender;
362

    
363
            if (e.OldValue != e.NewValue && instance != null)
364
            {
365
                instance.SetValue(e.Property, e.NewValue);
366
                Canvas.SetLeft(instance, instance.SetPoint.X);
367
                Canvas.SetTop(instance, instance.SetPoint.Y);
368
                //Canvas.SetLeft(instance, instance.CanvasX);
369
                //Canvas.SetTop(instance, instance.CanvasY);
370
            }
371
        }
372

    
373

    
374
        public static void OnUpdateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
375
        {
376
            var instance = (RectangleControl)sender;
377

    
378
            if (e.OldValue != e.NewValue && instance != null)
379
            {
380
                instance.SetValue(e.Property, e.NewValue);
381
                instance.SetRectPath();
382
            }
383
        }
384

    
385
        public static void AngleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
386
        {
387
            var instance = (RectangleControl)sender;
388
            if (e.OldValue != e.NewValue && instance.Base_RectPath != null)
389
            {
390
                instance.SetValue(e.Property, e.NewValue);
391
                instance.SetRectPath();
392
            }
393
        }
394

    
395
        public static void IsSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
396
        {
397
            //var instance = (RectangleControl)sender;
398

    
399
            //if (e.OldValue != e.NewValue && instance.Base_RectPath != null)
400
            //{
401
            //    instance.SetValue(e.Property, e.NewValue);
402

    
403
            //    if (instance.IsSelected)
404
            //    {
405
            //        instance.Base_RectPath.Stroke = new SolidColorBrush(Colors.Blue);
406
            //    }
407
            //    else
408
            //    {
409
            //        instance.Base_RectPath.Stroke = new SolidColorBrush(Colors.Red);
410
            //    }
411
            //}
412
        }
413

    
414
        public static void PointValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
415
        {
416
            var instance = (RectangleControl)sender;
417

    
418
            if (e.OldValue != e.NewValue && instance.Base_RectPath != null)
419
            {
420
                instance.SetValue(e.Property, e.NewValue);
421
                instance.SetRectPath();
422
            }
423
        }
424
        #endregion Dependency PropertyChanged
425

    
426
        private void SetRectPath()
427
        {
428
            this.ApplyTemplate();
429

    
430
            if (Base_RectPath != null)
431
            {
432
                //강인구 추가
433
                Base_RectPath.StrokeDashArray.Clear();
434
                if (DashSize != null)
435
                {
436
                    foreach (var item in this.DashSize)
437
                    {
438
                        Base_RectPath.StrokeDashArray.Add(item);
439
                    }
440
                    Base_RectPath.StrokeDashCap = PenLineCap.Square;
441
                }
442

    
443
                PathFigure pathFigure = new PathFigure
444
                {
445
                    IsClosed = true
446
                };
447

    
448
                if (ControlType == ControlType.Rectangle)
449
                {
450
                    switch (this.Paint)
451
                    {
452
                        case PaintSet.None:
453
                            this.FillColor = null;
454
                            pathFigure.IsFilled = false;
455
                            break;
456
                        case PaintSet.Fill:
457
                            this.FillColor = this.StrokeColor;
458
                            Base_RectPath.Fill = this.FillColor;
459
                            pathFigure.IsFilled = true;
460
                            break;
461
                        case PaintSet.Hatch:
462
                            Base_RectPath.Fill = HatchMake.CreateHatchBrush(this.StrokeColor);
463
                            pathFigure.IsFilled = true;
464
                            break;
465
                        default:
466
                            {
467

    
468
                            }
469
                            break;
470
                    }
471
                }
472

    
473
                pathFigure.StartPoint = this.StartPoint;
474

    
475
                List<Point> points = new List<Point>() { this.LeftBottomPoint, this.EndPoint, this.TopRightPoint, this.StartPoint };
476
                PolyLineSegment polyline = new PolyLineSegment(points, true);
477
                pathFigure.Segments.Add(polyline);
478

    
479
                PathGeometry pathGeometry = new PathGeometry();
480
                pathGeometry.Figures = new PathFigureCollection();
481
                pathGeometry.Figures.Add(pathFigure);
482

    
483
                //Base_RectPath.StrokeThickness = 3;
484
                this.FillColor = null;
485
                this.PathData = pathGeometry;
486
                ApplyOverViewData();
487
                //OverViewPathData = PathData;
488
                //AdornerControl adornerControl = new Adorner.AdornerControl();
489
                ////adornerControl
490

    
491
                //adornerControl.Content = pathGeometry;
492

    
493
            }
494
        }
495

    
496
        public override void ApplyOverViewData()
497
        {
498
            this.OverViewPathData = this.PathData;
499
        }
500

    
501
        #region Internal Method
502

    
503

    
504
        static RectangleControl()
505
        {
506
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RectangleControl), new FrameworkPropertyMetadata(typeof(RectangleControl)));
507
            ResourceDictionary dictionary = new ResourceDictionary();
508
            dictionary.Source = new Uri("/MarkupToPDF;component/themes/generic.xaml", UriKind.RelativeOrAbsolute);
509
            Application.Current.Resources.MergedDictionaries.Add(dictionary);
510
        }
511

    
512

    
513
        public override void OnApplyTemplate()
514
        {
515
            base.OnApplyTemplate();
516

    
517
            Base_RectPath = GetTemplateChild(PART_RectPath) as Path;
518

    
519

    
520
            if (Base_RectPath == null)
521
                return;
522

    
523
            SetRectPath();
524
            Base_RectPath.Focus();
525
        }
526

    
527
        public void updateControl()
528
        {
529
            this.StartPoint = new Point(this.PointSet[0].X, this.PointSet[0].Y);
530
            this.LeftBottomPoint = new Point(this.PointSet[1].X, this.PointSet[1].Y);
531
            this.EndPoint = new Point(this.PointSet[2].X, this.PointSet[2].Y);
532
            this.TopRightPoint = new Point(this.PointSet[3].X, this.PointSet[3].Y);
533
        }
534

    
535
        private void OnPropertyChanged(string name)
536
        {
537
            if (PropertyChanged != null)
538
            {
539
                PropertyChanged(this, new PropertyChangedEventArgs(name));
540
            }
541
        }
542

    
543
        public void ChangePaint(PaintSet state)
544
        {
545
            this.Paint = state;
546
            this.SetRectPath();
547
        }
548

    
549
        public event PropertyChangedEventHandler PropertyChanged;
550

    
551
        /// <summary>
552
        /// return rectanglecontrols' area
553
        /// </summary>
554
        public override Rect ItemRect
555
        {
556
            get
557
            {
558
                double dMinX = Math.Min(this.StartPoint.X, this.EndPoint.X);
559
                double dMinY = Math.Min(this.StartPoint.Y, this.EndPoint.Y);
560
                double dMaxX = Math.Max(this.StartPoint.X, this.EndPoint.X);
561
                double dMaxY = Math.Max(this.StartPoint.Y, this.EndPoint.Y);
562

    
563
                return new Rect(new Point(dMinX, dMinY), new Point(dMaxX, dMaxY));
564
            }
565
        }
566

    
567
        /// <summary>
568
        /// Serialize this
569
        /// </summary>
570
        /// <param name="sUserId"></param>
571
        /// <returns></returns>
572
        public override string Serialize()
573
        {
574
            using (S_RectControl STemp = new S_RectControl())
575
            {
576
                STemp.TransformPoint = "0|0";
577
                STemp.SizeSet = String.Format("{0}", this.LineSize);
578
                STemp.PaintState = this.Paint;
579
                STemp.PointSet = this.PointSet;
580
                //STemp.StrokeColor = "#FF00FF00";
581
                STemp.StrokeColor = this.StrokeColor.Color.ToString();
582
                if (this.FillColor != null)
583
                {
584
                    STemp.FillColor = this.FillColor.Color.ToString();
585
                }
586
                STemp.StartPoint = this.StartPoint;
587
                STemp.UserID = this.UserID;
588
                //STemp.Angle = this.a;
589
                STemp.EndPoint = this.EndPoint;
590
                STemp.LB = this.LeftBottomPoint;
591
                STemp.TR = this.TopRightPoint;
592
                STemp.DashSize = this.DashSize;
593
                STemp.Opac = this.Opacity;
594
                STemp.Name = this.GetType().Name.ToString();
595
                ///강인구 추가(2017.11.02)
596
                ///Memo 추가
597
                STemp.Memo = this.Memo;
598

    
599
                return "|DZ|" + JsonSerializerHelper.CompressString((STemp.JsonSerialize()));
600
            };
601
        }
602

    
603
        /// <summary>
604
        /// create a rectanglecontrol from given string
605
        /// </summary>
606
        /// <param name="item"></param>
607
        /// <returns></returns>
608
        public static RectangleControl FromString(string str, SolidColorBrush brush, string sProjectNo)
609
        {
610
            using (S_RectControl s = JsonSerializerHelper.JsonDeserialize<S_RectControl>(str))
611
            {
612
                string[] data2 = s.SizeSet.Split(CommentUserInfo.delimiterChars2, StringSplitOptions.RemoveEmptyEntries);
613
                return new RectangleControl
614
                {
615
                    LineSize = Convert.ToDouble(data2.First()),
616
                    Paint = s.PaintState,
617
                    StartPoint = s.StartPoint,
618
                    EndPoint = s.EndPoint,
619
                    Angle = s.Angle,
620
                    StrokeColor = brush,
621
                    DashSize = s.DashSize,
622
                    Opacity = s.Opac,
623
                    LeftBottomPoint = s.LB,
624
                    TopRightPoint = s.TR,
625
                    PointSet = s.PointSet,
626
                    UserID = s.UserID,
627
                    Memo = s.Memo
628
                };
629
            }
630
        }
631

    
632
        public void Dispose()
633
        {
634
            GC.Collect();
635
            GC.SuppressFinalize(this);
636
        }
637
        #endregion Internal Method
638
    }
639
}
클립보드 이미지 추가 (최대 크기: 500 MB)