// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using Core.Common.Controls.TreeView; using Core.Common.Controls.Views; using Core.Common.Gui.ContextMenu; using Core.Components.Gis.Data; using Core.Components.Gis.Forms; using MapResources = Core.Plugins.Map.Properties.Resources; using GuiResources = Core.Common.Gui.Properties.Resources; namespace Core.Plugins.Map.Legend { /// /// The view which shows the data that is added to a . /// public sealed partial class MapLegendView : UserControl, ISelectionProvider { private readonly IContextMenuBuilderProvider contextMenuBuilderProvider; private IMapControl mapControl; public event EventHandler SelectionChanged; /// /// Creates a new instance of . /// /// The to create context menus. /// Thrown when is null. public MapLegendView(IContextMenuBuilderProvider contextMenuBuilderProvider) { if (contextMenuBuilderProvider == null) { throw new ArgumentNullException(nameof(contextMenuBuilderProvider), $@"Cannot create a {typeof(MapLegendView).Name} when the context menu builder provider is null."); } this.contextMenuBuilderProvider = contextMenuBuilderProvider; InitializeComponent(); Text = MapResources.General_Map; RegisterTreeNodeInfos(); treeViewControl.SelectedDataChanged += TreeViewControlSelectedDataChanged; } /// /// Gets or sets the for which this legend view is configured /// to show the internal map data for. /// public IMapControl MapControl { get { return mapControl; } set { mapControl = value; Data = value?.Data; } } public object Data { get { return (MapData) treeViewControl.Data; } set { treeViewControl.Data = (MapData) value; } } public object Selection { get { return treeViewControl.SelectedData; } } private void TreeViewControlSelectedDataChanged(object sender, EventArgs e) { SelectionChanged?.Invoke(this, new EventArgs()); } private void RegisterTreeNodeInfos() { treeViewControl.RegisterTreeNodeInfo(new TreeNodeInfo { Text = mapPointData => mapPointData.Name, Image = mapPointData => MapResources.PointsIcon, ContextMenuStrip = (nodeData, parentData, treeView) => contextMenuBuilderProvider.Get(nodeData, treeView) .AddCustomItem(CreateZoomToExtentsItem(nodeData)) .AddSeparator() .AddPropertiesItem() .Build(), CanDrag = (mapPointData, parentData) => true, CanCheck = mapPointData => true, IsChecked = mapPointData => mapPointData.IsVisible, OnNodeChecked = MapDataOnNodeChecked }); treeViewControl.RegisterTreeNodeInfo(new TreeNodeInfo { Text = mapLineData => mapLineData.Name, Image = mapLineData => MapResources.LineIcon, ContextMenuStrip = (nodeData, parentData, treeView) => contextMenuBuilderProvider.Get(nodeData, treeView) .AddCustomItem(CreateZoomToExtentsItem(nodeData)) .AddSeparator() .AddPropertiesItem() .Build(), CanDrag = (mapLineData, parentData) => true, CanCheck = mapLineData => true, IsChecked = mapLineData => mapLineData.IsVisible, OnNodeChecked = MapDataOnNodeChecked }); treeViewControl.RegisterTreeNodeInfo(new TreeNodeInfo { Text = mapPolygonData => mapPolygonData.Name, Image = mapPolygonData => MapResources.AreaIcon, ContextMenuStrip = (nodeData, parentData, treeView) => contextMenuBuilderProvider.Get(nodeData, treeView) .AddCustomItem(CreateZoomToExtentsItem(nodeData)) .AddSeparator() .AddPropertiesItem() .Build(), CanDrag = (mapPolygonData, parentData) => true, CanCheck = mapPolygonData => true, IsChecked = mapPolygonData => mapPolygonData.IsVisible, OnNodeChecked = MapDataOnNodeChecked }); treeViewControl.RegisterTreeNodeInfo(new TreeNodeInfo { Text = mapDataCollection => mapDataCollection.Name, Image = mapDataCollection => GuiResources.folder, ChildNodeObjects = mapDataCollection => mapDataCollection.Collection.Reverse().Cast().ToArray(), CanDrop = MapControlCanDrop, CanInsert = MapControlCanInsert, OnDrop = MapControlOnDrop, ContextMenuStrip = (mapDataCollection, parentData, treeView) => contextMenuBuilderProvider.Get(mapDataCollection, treeView) .AddCustomImportItem( MapResources.MapLegendView_MapDataCollectionContextMenuStrip_Add_MapLayer, MapResources.MapLegendView_MapDataCollectionContextMenuStrip_Add_MapLayer_ToolTip, MapResources.MapPlusIcon) .AddSeparator() .AddCustomItem(CreateZoomToExtentsItem(mapDataCollection)) .AddSeparator() .AddPropertiesItem() .Build() }); } private StrictContextMenuItem CreateZoomToExtentsItem(FeatureBasedMapData nodeData) { return new StrictContextMenuItem($"&{MapResources.Ribbon_ZoomToAll}", nodeData.IsVisible ? MapResources.MapLegendView_CreateZoomToExtentsItem_ZoomToAll_Tooltip : MapResources.MapLegendView_CreateZoomToExtentsItem_ZoomToAllDisabled_Tooltip, MapResources.ZoomToAllIcon, (sender, args) => { MapControl?.ZoomToAllVisibleLayers(nodeData); }) { Enabled = nodeData.IsVisible }; } private StrictContextMenuItem CreateZoomToExtentsItem(MapDataCollection nodeData) { var enabled = nodeData.GetFeatureBasedMapDataRecursively().Any(fbmd => fbmd.IsVisible); return new StrictContextMenuItem($"&{MapResources.Ribbon_ZoomToAll}", enabled ? MapResources.MapLegendView_CreateZoomToExtentsItem_MapDataCollection_ZoomToAll_Tooltip : MapResources.MapLegendView_CreateZoomToExtentsItem_MapDataCollection_ZoomToAllDisabled_Tooltip, MapResources.ZoomToAllIcon, (sender, args) => { MapControl?.ZoomToAllVisibleLayers(nodeData); }) { Enabled = enabled }; } #region MapData private static void MapDataOnNodeChecked(FeatureBasedMapData featureBasedMapData, object parentData) { featureBasedMapData.IsVisible = !featureBasedMapData.IsVisible; featureBasedMapData.NotifyObservers(); } #endregion #region MapDataCollection private static bool MapControlCanDrop(object draggedData, object targetData) { return draggedData is MapData; } private static bool MapControlCanInsert(object draggedData, object targetData) { return draggedData is MapData; } private static void MapControlOnDrop(object droppedData, object newParentData, object oldParentData, int position, TreeViewControl control) { var mapData = (MapData) droppedData; var target = (MapDataCollection) newParentData; target.Remove(mapData); target.Insert(target.Collection.Count() - position, mapData); // Note: target is the same as the previous parent in this case target.NotifyObservers(); } #endregion } }