PgMainEditGraph.xaml.cs 14.9 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2
using MultiLayout.UiModule;
using System;
潘栩锋's avatar
潘栩锋 committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

潘栩锋's avatar
潘栩锋 committed
19
namespace MultiLayout.MainEdit
潘栩锋's avatar
潘栩锋 committed
20 21 22 23
{
    /// <summary>
    /// Page_MainEditGraph.xaml 的交互逻辑
    /// </summary>
24
    public partial class PgMainEditGraph : Page
潘栩锋's avatar
潘栩锋 committed
25
    {
潘栩锋's avatar
潘栩锋 committed
26 27 28
        FlyLayoutManager manager;
        FlyLayout layout;
        GageTabItem gageTabItem;
潘栩锋's avatar
潘栩锋 committed
29

潘栩锋's avatar
潘栩锋 committed
30 31
        ObservableCollection<MeGraphTabItem> Items;
        ObservableCollection<MeComponentType> ToolboxItems;
32
        public PgMainEditGraph()
潘栩锋's avatar
潘栩锋 committed
33 34 35 36 37 38 39 40
        {
            InitializeComponent();

            //测试
            //DefaultToolbox();
            //DefaultData();
        }

潘栩锋's avatar
潘栩锋 committed
41
        public void Init(GageTabItem gageTabItem, FlyLayoutManager manager)
潘栩锋's avatar
潘栩锋 committed
42
        {
潘栩锋's avatar
潘栩锋 committed
43 44 45 46 47 48
            this.manager = manager;
            layout = this.manager.layout;
            this.gageTabItem = gageTabItem;
            ToolboxItems = GraphConverter.GetToolboxItems(this.manager, ComponentType.Graph);
            Items = GraphConverter.GetMeGraphTabItems(gageTabItem, ToolboxItems);
            this.DataContext = gageTabItem;
潘栩锋's avatar
潘栩锋 committed
49 50 51 52
            itemsControl_toolbox.ItemsSource = ToolboxItems;
            UpdateTabControl();
        }

潘栩锋's avatar
潘栩锋 committed
53
        private void Page_MainEditGraph_RCChanged(MeGraphTabItem obj)
潘栩锋's avatar
潘栩锋 committed
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
        {
            //查找对应的TabItem
            Grid g = obj.mTabItem.Content as Grid;
            if (g == null)
                return;
            UpdateGrid(g, obj);
            UpdateComponentTypeCount();
        }

        void UpdateTabControl()
        {
            tabcontrol.Items.Clear();
            for (int i = 0; i < Items.Count(); i++)
            {
                TabControlAdd(Items[i]);
            }
            Items.CollectionChanged += Items_CollectionChanged;
            UpdateComponentTypeCount();
        }

        private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    {
潘栩锋's avatar
潘栩锋 committed
80
                        foreach (MeGraphTabItem item in e.NewItems)
潘栩锋's avatar
潘栩锋 committed
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
                        {
                            TabControlAdd(item);
                        }
                        tabcontrol.SelectedIndex = tabcontrol.Items.Count - 1;
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                    {
                        TabItem ti_old = tabcontrol.Items[e.OldStartingIndex] as TabItem;
                        if (tabcontrol.SelectedItem == ti_old)
                        {
                            tabcontrol.Items.RemoveAt(e.OldStartingIndex);
                            tabcontrol.Items.Insert(e.NewStartingIndex, ti_old);
                            tabcontrol.SelectedItem = ti_old;
                        }
                        else
                        {
                            tabcontrol.Items.RemoveAt(e.OldStartingIndex);
                            tabcontrol.Items.Insert(e.NewStartingIndex, ti_old);
                        }
                        
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    {
                        tabcontrol.Items.RemoveAt(e.OldStartingIndex);
                    }
                    break;
            }
        }

潘栩锋's avatar
潘栩锋 committed
112
        void TabControlAdd(MeGraphTabItem item)
潘栩锋's avatar
潘栩锋 committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        {
            TabItem ti = new TabItem()
            {
                Tag = item
            };
            ti.SetBinding(TabItem.HeaderProperty, new Binding("Title") { Source = item });
            item.mTabItem = ti;

            Grid g = new Grid();
            g.Style = this.Resources["GridStyle_grid"] as Style;
            UpdateGrid(g, item);
            ti.Content = g;

            tabcontrol.Items.Add(ti);
            item.RCChanged += Page_MainEditGraph_RCChanged;
        }
潘栩锋's avatar
潘栩锋 committed
129
        void UpdateGrid(Grid grid, MeGraphTabItem item)
潘栩锋's avatar
潘栩锋 committed
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
        {
            grid.RowDefinitions.Clear();
            {
                RowDefinition rd = new RowDefinition();
                rd.Height = new GridLength(20);
                grid.RowDefinitions.Add(rd);
            }

            for (int i = 0; i < item.RowDefinitions.Count(); i++)
            {
                RowDefinition rd = new RowDefinition();
                grid.RowDefinitions.Add(rd);
            }

            grid.ColumnDefinitions.Clear();
            {
                ColumnDefinition cd = new ColumnDefinition();
                cd.Width = new GridLength(20);
                grid.ColumnDefinitions.Add(cd);
            }
            for (int i = 0; i < item.ColumnDefinitions.Count(); i++)
            {
                ColumnDefinition cd = new ColumnDefinition();
                grid.ColumnDefinitions.Add(cd);
            }



            grid.Children.Clear();
            //添加Definitions标示
            for (int x = 0; x < item.ColumnDefinitions.Count(); x++)
            {
                ToggleButton tb = new ToggleButton();
                tb.SetValue(Grid.ColumnProperty, x + 1);
                tb.Style = this.Resources["ToggleButtonStyle_auto"] as Style;
                tb.SetBinding(CheckBox.IsCheckedProperty,
                    new Binding("ColumnDefinitions[" + x.ToString() + "]") { Source = item});
                grid.Children.Add(tb);
            }

            for (int y = 0; y < item.RowDefinitions.Count(); y++)
            {
                ToggleButton tb = new ToggleButton();
                tb.SetValue(Grid.RowProperty, y + 1);
                tb.Style = this.Resources["ToggleButtonStyle_auto"] as Style;
                tb.SetBinding(CheckBox.IsCheckedProperty,
                    new Binding("RowDefinitions[" + y.ToString() + "]") { Source = item });
                grid.Children.Add(tb);
            }

            //添加 表格线
            for (int x = 0; x < item.ColumnDefinitions.Count() - 1; x++)
            {
                Border b = new Border();
                b.SetValue(Grid.ColumnProperty, x + 1);
                b.SetValue(Grid.RowSpanProperty, item.RowDefinitions.Count() + 1);
                b.Style = this.Resources["BorderStyle_vGridLine"] as Style;
                grid.Children.Add(b);
            }
            for (int y = 0; y < item.RowDefinitions.Count() - 1; y++)
            {
                Border b = new Border();
                b.SetValue(Grid.RowProperty, y + 1);
                b.SetValue(Grid.ColumnSpanProperty, item.ColumnDefinitions.Count() + 1);
                b.Style = this.Resources["BorderStyle_hGridLine"] as Style;
                grid.Children.Add(b);
            }

            //添加 控件
            for (int i = 0; i < item.Components.Count(); i++)
            {
潘栩锋's avatar
潘栩锋 committed
201
                MeGraphComponent c = item.Components[i];
潘栩锋's avatar
潘栩锋 committed
202 203 204 205 206 207
                //加入统计
                ButtonComponentAdd(grid, c);
            }
        }
        void UpdateComponentTypeCount()
        {
潘栩锋's avatar
潘栩锋 committed
208
            foreach (MeComponentType type in ToolboxItems)
潘栩锋's avatar
潘栩锋 committed
209 210 211 212 213 214
            {
                type.Count = Items.Sum(((item) => {
                    return item.Components.Count((c) => { return c.Type == type; });
                }));
            }
        }
潘栩锋's avatar
潘栩锋 committed
215
        void ButtonComponentAdd(Grid grid, MeGraphComponent c)
潘栩锋's avatar
潘栩锋 committed
216 217 218 219 220 221 222 223 224 225 226 227 228
        {
            Button b = new Button();
            c.mButton = b;
            b.Name = "button_component";
            b.Style = this.Resources["ButtonStyle_component"] as Style;
            b.Background = c.Type.Background;
            b.Click += button_componentEdit_Click;
            SetButtonComponentRect(c);
            c.PropertyChanged += C_PropertyChanged;
            b.DataContext = c;
            b.Tag = c;
            grid.Children.Add(b);
        }
潘栩锋's avatar
潘栩锋 committed
229
        void SetButtonComponentRect(MeGraphComponent c)
潘栩锋's avatar
潘栩锋 committed
230 231 232 233 234 235 236 237 238 239 240
        {
            Button b = c.mButton;
            b.SetValue(Grid.ColumnProperty, c.Rect.X + 1);
            b.SetValue(Grid.RowProperty, c.Rect.Y + 1);
            b.SetValue(Grid.ColumnSpanProperty, c.Rect.Width);
            b.SetValue(Grid.RowSpanProperty, c.Rect.Height);
        }
        private void C_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Rect")
            {
潘栩锋's avatar
潘栩锋 committed
241
                var c = sender as MeGraphComponent;
潘栩锋's avatar
潘栩锋 committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
                if (c.Rect.IsEmpty)
                {
                    Grid g = c.mItem.mTabItem.Content as Grid;
                    g.Children.Remove(c.mButton);
                }
                else
                {
                    SetButtonComponentRect(c);
                }
            }
        }

        private void button_itemDel_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            //向上找TabItem
            TabItem tabitem = FLY.ControlLibrary.COMMON.GetParent<TabItem>(sender as DependencyObject);
            if (tabitem == null)
            {
                //异常
                return;
            }
潘栩锋's avatar
潘栩锋 committed
264
            int idx = Items.IndexOf(tabitem.Tag as MeGraphTabItem);
潘栩锋's avatar
潘栩锋 committed
265 266 267 268 269 270
            if (idx < 0)
            {
                //异常
                return;
            }
            //提示是否删除
潘栩锋's avatar
潘栩锋 committed
271
            string strTipTabItemDel = "页面将要被删除, 是否确定?";
潘栩锋's avatar
潘栩锋 committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
            if (FLY.ControlLibrary.MyMessageBox.Show(strTipTabItemDel) == true)
            {
                Items.RemoveAt(idx);
            }
            
            
        }

        private void button_itemEdit_Click(object sender, RoutedEventArgs e)
        {
            //向上找TabItem
            TabItem tabitem = FLY.ControlLibrary.COMMON.GetParent<TabItem>(sender as DependencyObject);
            if (tabitem == null)
            {
                //异常
                return;
            }
潘栩锋's avatar
潘栩锋 committed
289
            int idx = Items.IndexOf( tabitem.Tag as MeGraphTabItem);
潘栩锋's avatar
潘栩锋 committed
290 291 292 293 294 295
            if (idx < 0)
            {
                //异常
                return;
            }
            
296
            WdGraphTabItemConfig w = new WdGraphTabItemConfig();
潘栩锋's avatar
潘栩锋 committed
297 298 299 300 301 302 303
            w.Init(Items, idx);
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }

        private void button_itemAdd_Click(object sender, RoutedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
304
            MeGraphTabItem item = new MeGraphTabItem();
潘栩锋's avatar
潘栩锋 committed
305 306 307 308 309 310 311 312
            Items.Add(item);
        }

        private void button_componentNew_Click(object sender, RoutedEventArgs e)
        {
            TabItem tabitem = tabcontrol.SelectedItem as TabItem;
            if (tabitem == null)
            {
潘栩锋's avatar
潘栩锋 committed
313 314
                string strError = "错误";
                string strTipNoTabItem = "没有选择图表页面";
潘栩锋's avatar
潘栩锋 committed
315 316 317
                FLY.ControlLibrary.Window_WarningTip.Show(strError, strTipNoTabItem);
                return;
            }
潘栩锋's avatar
潘栩锋 committed
318
            var item = tabitem.Tag as MeGraphTabItem;
潘栩锋's avatar
潘栩锋 committed
319 320 321 322

            System.Drawing.Point p = item.GetFreeLocal();
            if (p.X == -1)
            {
潘栩锋's avatar
潘栩锋 committed
323 324
                string strError = "错误";
                string strTipNoSize = "没有空间放置组件";
潘栩锋's avatar
潘栩锋 committed
325 326 327 328
                FLY.ControlLibrary.Window_WarningTip.Show(strError, strTipNoSize);
                return;
            }
            Button button = sender as Button;
潘栩锋's avatar
潘栩锋 committed
329
            var gct = button.Tag as MeComponentType;
潘栩锋's avatar
潘栩锋 committed
330 331 332

            if (!gct.IsEnable)
            {
潘栩锋's avatar
潘栩锋 committed
333 334 335
                string strError = "错误";
                string strTipCantAddAgain = "不能重复放置组件";
                FLY.ControlLibrary.Window_WarningTip.Show(strError, strTipCantAddAgain);
潘栩锋's avatar
潘栩锋 committed
336 337 338 339
                return;
            }


潘栩锋's avatar
潘栩锋 committed
340
            var c = new MeGraphComponent()
潘栩锋's avatar
潘栩锋 committed
341 342
            {
                Type = gct,
潘栩锋's avatar
潘栩锋 committed
343 344
                Rect = new System.Drawing.Rectangle(p.X, p.Y, 1, 1),
                ServiceContainerName = gageTabItem.ServiceContainerName
潘栩锋's avatar
潘栩锋 committed
345 346
            };

347
            WdGraphConfig w = new WdGraphConfig();
潘栩锋's avatar
潘栩锋 committed
348
            w.Init(c, manager.serviceNames);
潘栩锋's avatar
潘栩锋 committed
349 350 351 352 353 354
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            if (w.ShowDialog() == true)
            {
                //检测位置是否合法
                if (!item.IsComponentRectVaild(w.Result.Rect))
                {
潘栩锋's avatar
潘栩锋 committed
355 356
                    string strError = "错误";
                    string strTipErrorComponentAdd = " 组件位置尺寸出错,不能添加";
潘栩锋's avatar
潘栩锋 committed
357 358 359 360
                    FLY.ControlLibrary.Window_WarningTip.Show(strError, strTipErrorComponentAdd);
                    return;
                }
                c.Rect = w.Result.Rect;
潘栩锋's avatar
潘栩锋 committed
361
                c.ServiceContainerName = w.Result.ServiceContainerName;
潘栩锋's avatar
潘栩锋 committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
                item.Components.Add(c);
                c.mItem = item;
                gct.Count++;
                ButtonComponentAdd(tabitem.Content as Grid, c);
            }
        }

        private void button_componentDel_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            //向上找TabItem
            Button button = FLY.ControlLibrary.COMMON.GetParent<Button>(sender as DependencyObject, "button_component" );
            if (button == null)
            {
                //异常
                return;
            }
潘栩锋's avatar
潘栩锋 committed
379
            string strTipComponentDel = " 组件将要被删除,是否确定";
潘栩锋's avatar
潘栩锋 committed
380 381
            if (FLY.ControlLibrary.MyMessageBox.Show(strTipComponentDel) == true)
            {
潘栩锋's avatar
潘栩锋 committed
382
                var c = button.Tag as MeGraphComponent;
潘栩锋's avatar
潘栩锋 committed
383 384 385 386 387 388 389 390 391 392 393
                c.mItem.Components.Remove(c);
                c.Type.Count--;

                Grid g = c.mItem.mTabItem.Content as Grid;
                g.Children.Remove(button);
            }
        }

        private void button_componentEdit_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
潘栩锋's avatar
潘栩锋 committed
394
            var c = button.Tag as MeGraphComponent;
潘栩锋's avatar
潘栩锋 committed
395

396
            WdGraphConfig w = new WdGraphConfig();
潘栩锋's avatar
潘栩锋 committed
397
            w.Init(c, manager.serviceNames);
潘栩锋's avatar
潘栩锋 committed
398 399 400 401 402 403
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            if (w.ShowDialog() == true)
            {
                //检测位置是否合法
                if (!c.mItem.IsComponentRectVaild(w.Result.Rect, c))
                {
潘栩锋's avatar
潘栩锋 committed
404 405
                    string strError = "错误";
                    string strTipErrorComponentAdd = "组件添加出错";
潘栩锋's avatar
潘栩锋 committed
406 407 408 409
                    FLY.ControlLibrary.Window_WarningTip.Show(strError, strTipErrorComponentAdd);
                    return;
                }
                c.Rect = w.Result.Rect;
潘栩锋's avatar
潘栩锋 committed
410
                c.ServiceContainerName = w.Result.ServiceContainerName;
潘栩锋's avatar
潘栩锋 committed
411 412 413
            }
        }

潘栩锋's avatar
潘栩锋 committed
414 415 416


        private void btnBackClick(object sender, RoutedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
417
        {
潘栩锋's avatar
潘栩锋 committed
418 419
            GraphConverter.ToGraphTabItems(Items, gageTabItem);
            NavigationService.GoBack();
潘栩锋's avatar
潘栩锋 committed
420 421 422 423 424
        }
    }


}