qin
2024-06-04 9490957edfc694a0378ee52af47e921b8ac44e7a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//using DevExpress.XtraEditors;
//using DevExpress.XtraEditors.Repository;
using Hydro.CommonBase;
//using ConfigApp;
using Hydro.MapView;
//using DevExpress.XtraEditors;
//using DevExpress.XtraGrid.Views.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Colour = Hydro.MapView.Colour;
 
namespace Hydro.MapUI
{
    public partial class Form_Colour : Form
    {
        public Template Template { get; private set; }
 
        private List<Colour> Colours = new List<Colour>();
        private Colour CurrenColour
        {
            get {
                
                return comboBox1.SelectedItem as Colour;
            }
        }
        private List<ColourItem> floors 
        { 
            get 
            {
                return  CurrenColour?.Items;
            }
            set 
            { 
                if (Colours.Count == 0) Colours.Add(new Colour());  Colours[0].Items = value; 
            }
        }
        private Stack<List<Colour>> undoStack;
        private Stack<List<Colour>> redoStack;
        MapViewer map;
        public Form_Colour(List<Colour> colours,Template template)
        {
            InitializeComponent();
            //comboBox1.DataSource = this.regions;
 
 
            this.Template = template;
            this.Colours = colours;
            //floors = new List<ColourItem>();
            undoStack = new Stack<List<Colour>>();
            redoStack = new Stack<List<Colour>>();
        }
        
        private void Form_EditFloors_Load(object sender,EventArgs e)
        {
            //根据ColourType更新comboBox1的数据集
 
            int i = 0;
            foreach (ColourType colour in Enum.GetValues(typeof(ColourType)))
            {
                if (i!=0)    comboBox3.Items.Add(colour);
                i++;
            }
            comboBox3.Refresh();
            comboBox3.SelectedIndex = 0;    
 
 
            if (GlobalObject.map == null)
            {
                GlobalObject.map = map;
 
            }
            UpdateColourList();
            UpdateColourItemList();
 
            if (floors == null)
            {
                floors = new List<ColourItem>();
                //为floors添加一系列默认的颜色分级设置
              
            }
 
        }
        private void AddFloorButton_Click(object sender, EventArgs e)
        {
            
 
            ColourItem newFloor = new ColourItem();
            //newFloor.FloorIndex = floors.Count + 1;
 
            floors.Add(newFloor);
 
            UpdateColourItemList();
            FloorsListBox.SelectedItem = newFloor;
            //foreach (ColourItem f in FloorsListBox.Items)
            //{
            //    if (newFloor==f)
            //    {
            //        FloorsListBox.SelectedItem = newFloor;
            //        break;
            //    }
            //}
        }
 
        private void DeleteFloorButton_Click(object sender, EventArgs e)
        {
            if (FloorsListBox.SelectedIndex != -1)
            {
                int selectedIndex = FloorsListBox.SelectedIndex;
                ColourItem removedFloor = floors[selectedIndex];
                floors.RemoveAt(selectedIndex);
 
                // Save the current state for undo
                undoStack.Push(new List<Colour>(Colours));
 
                // Clear redo stack since a new operation is performed
                redoStack.Clear();
 
                UpdateColourItemList();
            }
        }
        private void UpdateColourList()
        {
            comboBox1.Items.Clear();
            foreach (Colour region in Colours.FindAll(cl=>cl.Type== Current_ColourType))
            {
                comboBox1.Items.Add(region);
            }
            if (comboBox1.Items.Count>0)
                comboBox1.SelectedIndex = 0;
        }
        private void UpdateColourItemList()
        {
            FloorsListBox.Items.Clear();
            if (floors!=null)
                FloorsListBox.Items.AddRange(floors.ToArray());
           
            
            
        }
 
        private void UndoButton_Click(object sender, EventArgs e)
        {
            if (undoStack.Count > 0)
            {
                redoStack.Push(new List<Colour>(Colours));
                Colours = undoStack.Pop();
 
                UpdateColourItemList();
            }
        }
 
        private void RedoButton_Click(object sender, EventArgs e)
        {
            if (redoStack.Count > 0)
            {
                undoStack.Push(new List<Colour>(Colours));
                Colours = redoStack.Pop();
 
                UpdateColourItemList();
            }
        }
        private ColourItem copiedFloor; // 保存被复制的楼层
 
        private void CopyFloorButton_Click(object sender, EventArgs e)
        {
            if (FloorsListBox.SelectedIndex != -1)
            {
                int selectedIndex = FloorsListBox.SelectedIndex;
                copiedFloor = new ColourItem(floors[selectedIndex]);
            }
        }
 
        private void PasteFloorButton_Click(object sender, EventArgs e)
        {
            if (copiedFloor != null)
            {
                floors.Add(new ColourItem(copiedFloor));
                // Save the current state for undo
                undoStack.Push(new List<Colour>(Colours));
 
                // Clear redo stack since a new operation is performed
                redoStack.Clear();
                UpdateColourItemList();
            }
        }
 
        private void EditFloorButton_Click(object sender, EventArgs e)
        {
            if (FloorsListBox.SelectedIndex != -1)
            {
                lockEditing();
                int selectedIndex = FloorsListBox.SelectedIndex;
                ColourItem selectedFloor = floors[selectedIndex];
 
                // 显示并允许编辑楼层属性的组件
                EditProperties(selectedFloor);
            }
        }
 
        private void lockEditing(bool state=true)
        {
            simpleButton4.Enabled = !state;
            FloorsListBox.Enabled = !state;
            SaveButton.Enabled = state;
            CancelButton.Enabled = state;
            //simpleButton1.Enabled = state;
            tb_Range1.ReadOnly = !state;
            colorPickEdit1.ReadOnly = !state;
            //ElevTextBox.ReadOnly = !state;
            
        }
 
        private void FloorsListBox_Click(object sender, EventArgs e)
        {
            
        }
 
        private void EditProperties(ColourItem floor)
        {
            tb_Range1.Text = floor.DRange.Min.ToString("0.000");
            tb_Range2.Text = floor.DRange.Max.ToString("0.000");
 
            colorPickEdit1.Color = floor.value;
            //ElevTextBox.Text = floor.Elev.ToString();
            //MapViewTextBox.Text = floor.MapView?.ToString() ?? "";
 
            
 
            SaveButton.Visible = true;
            CancelButton.Visible = true;
        }
 
        private void SaveButton_Click(object sender, EventArgs e)
        {
            int selectedIndex = FloorsListBox.SelectedIndex;
            if (selectedIndex == -1 || selectedIndex >= floors.Count) return;
 
            ColourItem editedFloor = floors[selectedIndex];
 
            //editedFloor.BackgroundImg = colorPickEdit1.Text;
            editedFloor.value = colorPickEdit1.Color;
            
            //editedFloor.MapView = new MapDimensions(MapViewTextBox.Text);
 
            UpdateColourItemList();
 
            ClearProperties();
            // Save the current state for undo
            undoStack.Push(new List<Colour>(Colours));
 
            // Clear redo stack since a new operation is performed
            redoStack.Clear();
            lockEditing(false);
        }
        private void SelectImageButton_Click(object sender, EventArgs e)
        {
            //OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.Filter = "Image Files (*.jpg;*.jpeg;*.png;*.gif;*.bmp)|*.jpg;*.jpeg;*.png;*.gif;*.bmp";
 
            //if (openFileDialog.ShowDialog() == DialogResult.OK)
            //{
            //    colorPickEdit1.Text = openFileDialog.FileName;
            //}
            int selectedIndex = FloorsListBox.SelectedIndex;
            if (selectedIndex == -1 || selectedIndex >= floors.Count) return;
            ColourItem editedFloor = floors[selectedIndex];
            模板管理 fs = new 模板管理(true);
            if (fs.ShowDialog()==DialogResult.OK)
            {
                colorPickEdit1.Text = fs.TemplateID;
                //editedFloor.TemplateID = fs.TemplateID;
                map.SetData(TemplateList.GetTemplate(fs.TemplateID));
            }
 
        }
        private void CancelButton_Click(object sender, EventArgs e)
        {
            ClearProperties();
            lockEditing(false);
        }
        private void ClearFloorsButton_Click(object sender, EventArgs e)
        {
 
            floors.Clear();
            UpdateColourItemList();
            // Save the current state for undo
            undoStack.Push(new List<Colour>(Colours));
 
            // Clear redo stack since a new operation is performed
            redoStack.Clear();
        }
 
        private void ClearProperties()
        {
            tb_Range1.Text = "";
            colorPickEdit1.Text = "";
            //ElevTextBox.Text = "";
            //MapViewTextBox.Text = "";
 
            tb_Range1.Enabled = true;
 
            SaveButton.Visible = false;
            CancelButton.Visible = false;
        }
 
        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void GenerateFloorsButton_Click(object sender, EventArgs e)
        {
            if (CurrenColour==null)
            {
                simpleButton9_Click(1,new EventArgs());
            }
            // 隐藏错误标签
            ErrorLabel.Visible = false;
 
            
 
            float minFloorIndex;
            float maxFloorIndex;
            int ColourNum = 0;
            Color color0 = colorPickEdit2.Color;
            Color color1 = colorPickEdit3.Color;
 
            // 尝试解析文本框中的值
            if (!float.TryParse(tb_minFloorIndex.Text, out minFloorIndex))
            {
                ErrorLabel.Text = "最小值必须是数字";
                ErrorLabel.Visible = true;
                return;
            }
            if (!float.TryParse(tb_maxFloorIndex.Text, out maxFloorIndex))
            {
                ErrorLabel.Text = "最大值必须是数字";
                ErrorLabel.Visible = true;
                return;
            }
            if (!int.TryParse(tb_ColourNum.Text,out ColourNum))
            {
                ErrorLabel.Text = "级数必须是数字";
                ErrorLabel.Visible = true;
                return;
            }
            //if (!float.TryParse(tb_Height.Text, out floorHeight))
            //{
            //    ErrorLabel.Text = "每层层高必须是数字";
            //    ErrorLabel.Visible = true;
            //    return;
            //}
            //if (!float.TryParse(tb_minFloorElev.Text, out minFloorElev))
            //{
            //    ErrorLabel.Text = "最低层地面高程必须是数字";
            //    ErrorLabel.Visible = true;
            //    return;
            //}
 
 
            CurrenColour.color0= color0;
            CurrenColour.color1 = color1;
            CurrenColour.maxNum = maxFloorIndex;
            CurrenColour.minNum = minFloorIndex;
            CurrenColour.ColourCount = ColourNum;
 
 
            //string tempID = BackgroundImgTextBox_q.Text;
 
            // 逐层生成楼层
            for (int i = 0; i < ColourNum; i++)
            {
                DRange currentRange =new DRange(minFloorIndex + i * (maxFloorIndex- minFloorIndex)/ColourNum, minFloorIndex + (i+1) * (maxFloorIndex - minFloorIndex) / ColourNum);
                Color c;
                //获取color0和color1,Colour个等分颜色,第一个为color0,最后一个为color1,中间的为等分颜色
                if (i == 0)
                {
                    c = color0;
                }
                else if (i == ColourNum-1)
                {
                    c = color1;
                }
                else
                {
                    c = Color.FromArgb(color0.R + (color1.R - color0.R) / (ColourNum - 1) * i, color0.G + (color1.G - color0.G) / (ColourNum - 1) * i, color0.B + (color1.B - color0.B) / (ColourNum-1) * i);
                    
                }
 
                
                ColourItem newFloor = new ColourItem(currentRange,c);
                floors.Add(newFloor);
            }
 
            // 更新楼层列表
            UpdateColourItemList();
        }
 
        private void SelectImageButton_Click_q(object sender, EventArgs e)
        {
            //OpenFileDialog openFileDialog = new OpenFileDialog();
            //openFileDialog.Filter = "Image Files (*.jpg;*.jpeg;*.png;*.gif;*.bmp)|*.jpg;*.jpeg;*.png;*.gif;*.bmp";
 
            //if (openFileDialog.ShowDialog() == DialogResult.OK)
            //{
            //    BackgroundImgTextBox_q.Text = openFileDialog.FileName;
            //}
            //int selectedIndex = FloorsListBox.SelectedIndex;
            //if (selectedIndex == -1 || selectedIndex >= floors.Count) return;
            //ColourItem editedFloor = floors[selectedIndex];
            模板管理 fs = new 模板管理(true);
            if (fs.ShowDialog() == DialogResult.OK)
            {
                //BackgroundImgTextBox_q.Text = fs.TemplateID;
                //editedFloor.TemplateID = fs.TemplateID;
                //map.SetData(TemplateList.GetTemplate(fs.TemplateID));
            }
 
        }
 
        private void FloorsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FloorsListBox.SelectedIndex != -1)
            {
                int selectedIndex = FloorsListBox.SelectedIndex;
                ColourItem selectedFloor = floors[selectedIndex];
 
                // 显示并允许编辑楼层属性的组件
                EditProperties(selectedFloor);
                //var template = TemplateList.GetTemplate(selectedFloor.TemplateID);
                ////if (template != null)
                ////    this.map.SetData(template);
 
 
            }
        }
 
        private void simpleButton9_Click(object sender, EventArgs e)
        {
            
            int i = 1;
            string str = (Current_ColourType+"_"+i.ToString()).ToString();
            while (Colours.FindAll(cl=>cl.Name==str).Count>0)
            {
                i++;
                str = (Current_ColourType + "_" + i.ToString()).ToString();
            }
            InputBox inputBox = new InputBox("输入名称",str);
            string name= inputBox.ShowDialog();
          
            if (name != null &&name.Trim() !="")
            {
                var region = new Colour(Current_ColourType,new List<ColourItem>(),name);
                if (Colours.Find(r => r.Name == region.Name) != null)
                {
                    MessageBox.Show($"名称重复[{region.Name}]");
                    return;
                }
                Colours.Add(region);
                UpdateColourList();
            }
            
        }
 
        private void simpleButton10_Click(object sender, EventArgs e)
        {
            if (message.show("", "确认删除", MessageBoxButtons.YesNo))
            {
                Colours.RemoveAt(comboBox1.SelectedIndex);
                UpdateColourList();
            }
           
            
        }
        ColourType Current_ColourType
        {
            get
            {
                if(comboBox3.SelectedItem==null) return ColourType.节点自由压力;
                return (ColourType)Enum.Parse(typeof(ColourType), comboBox3.SelectedItem.ToString());
            }
        }
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateColourList();
            UpdateColourItemList();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CurrenColour == null) return;
            colorPickEdit2.Color = CurrenColour.color0;
            colorPickEdit3.Color = CurrenColour.color1;
            tb_minFloorIndex.Text = CurrenColour.minNum.ToString();
            tb_maxFloorIndex.Text = CurrenColour.maxNum.ToString();
            tb_ColourNum.Text = CurrenColour.ColourCount.ToString();
 
 
        }
        private void InsertIntoNet_Click(object sender, EventArgs e)
        {
            //int step = 1;
            //int startIndex = 0;
            //int endIndex = CurrenColour.Items.Count;
            ////if (!CurrenColour.isDirectionUp)
            ////{
            ////    step = -1;
            ////    startIndex = CurrenColour.Floors.Count - 1;
            ////    endIndex = -1;
            ////}
            //MapViewNetWork net = new MapViewNetWork();
            //ColourItem lastFloor = null;
            //NodeViewModel lastPoint = null;
            //NodeViewModel node1 = null;
            //for(int i=startIndex; i < endIndex;i+=step)
            //{
            //    var floor = CurrenColour.Items[i];
            //    if (floor.template.network==null)
            //    {
            //        floor.template.loadInpFile();
                    
            //    }
            //    var n= floor.template.network;
            //    var point = n.Nodes.Find(node => node.ID==floor.template.Node1);
            //    if (point == null) point = n.Nodes[0];
            //    PointF3D p = new PointF3D(0- point.X, 0- point.Y, floor.Elev-point.Elev);
            //    n.Nodes.Select(node => (NodeViewModel)node).ToList().ForEach(node => { node.regionName = CurrenColour.Name; });
            //    n.Links.Select(link=>(LinkViewModel)link).ToList().ForEach(link => { link.regionName = CurrenColour.Name; });
            //    var list=net.Add(n, p,true);
            //    NodeViewModel CurrentPoint = (NodeViewModel)list[0];
            //    if (lastPoint!=null)
            //    {
            //        var j=net.AddPipe(lastPoint, CurrentPoint);
            //        j.regionName = CurrenColour.Name;
            //    }
            //    lastPoint = (NodeViewModel)list[0];
            //    if (node1 == null) node1 = lastPoint;
                
            //}
            //Template temp=new Template();
            //temp.network = net;
            //temp.Node1 = node1.ID;
            //GlobalObject.map.InsertNet(temp);
            //this.Close();
 
        }
 
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //CurrenColour.isDirectionUp = comboBox2.SelectedIndex == 0;
        }
 
        private void btn_readNetData_Click(object sender, EventArgs e)
        {
            //根据当前选中的ColourType,判断是否有对应的网络数据
            if (CurrenColour == null) return;
            double max = 0;
            double min = 0;
 
            switch(CurrenColour.Type)
            {
                case ColourType.节点自由压力:
                    max=this.Template.network.Nodes.Where(node=>!(node is ReservoirViewModel || node is TankViewModel)).Max(node => node.EN_PRESSURE);
                    min=this.Template.network.Nodes.Where(node => !(node is ReservoirViewModel || node is TankViewModel)).Min(node => node.EN_PRESSURE);
                    break;
                case ColourType.节点绝对压力:
                    max = this.Template.network.Nodes.Where(node => !(node is ReservoirViewModel || node is TankViewModel)).Max(node => node.EN_PRESSURE);
                    min = this.Template.network.Nodes.Where(node => !(node is ReservoirViewModel || node is TankViewModel)).Min(node => node.EN_PRESSURE);
                    break;
                case ColourType.节点需水量:
                    max = this.Template.network.Nodes.Where(node => !(node is ReservoirViewModel || node is TankViewModel)).Max(node => node.EN_DEMAND);
                    min = this.Template.network.Nodes.Where(node => !(node is ReservoirViewModel || node is TankViewModel)).Min(node => node.EN_DEMAND);
                    break;
                case ColourType.管线流量:
                    max = this.Template.network.Links.Max(link => link.EN_FLOW);
                    min = this.Template.network.Links.Min(link => link.EN_FLOW);
                    break;
                case ColourType.管线流速:
                    max = this.Template.network.Links.Max(link => link.EN_VELOCITY);
                    min = this.Template.network.Links.Min(link => link.EN_VELOCITY);
                    break;
                default:
                    break;
 
            }
            tb_minFloorIndex.Text = min.ToString();
            tb_maxFloorIndex.Text = max.ToString();
        }
 
        private void btn_useColour_Click(object sender, EventArgs e)
        {
            Colours.ForEach(c => c.isChoosed = false); 
            CurrenColour.isChoosed = true;
        }
 
        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void btn_apply_Click(object sender, EventArgs e)
        {
            Colours.ForEach(c => c.isChoosed = false);
            CurrenColour.isChoosed = true;
            if (CurrenColour.isNode)
            {
                GlobalObject.map.cb_Node_Colour.SelectedIndex =(int)CurrenColour.Type ;
            }
            else
            {
                GlobalObject.map.cb_Link_Colour.SelectedIndex = ((int)CurrenColour.Type - Colour.NodeTypeCount);
            }
            TemplateList.SaveToFile();
            this.Close();
 
        }
 
        private void btn_颜色对调_Click(object sender, EventArgs e)
        {
            var color=colorPickEdit2.Color;
            colorPickEdit2.Color = colorPickEdit3.Color;
            colorPickEdit3.Color = color;
 
        }
    }
 
    
}