/// <summary> /// Creates visual tool actions, which allow to enable/disable visual tools (<see cref="ScrollPages"/> and /// the composite visual tool with <see cref="RectangularSelectionTool"/> and <see cref="ScrollPages"/>) in image viewer, and adds actions to the toolstrip. /// </summary> /// <param name="toolStrip">The toolstrip, where actions must be added.</param> public static void CreateActions(VisualToolsToolStrip toolStrip) { // create action, which allows to scroll pages in image viewer VisualToolAction scrollPagesVisualToolAction = new VisualToolAction( new ScrollPages(), "Scroll Pages", "Scroll Pages", GetIcon("ScrollPagesTool.png")); // add the action to the toolstrip toolStrip.AddAction(scrollPagesVisualToolAction); // create visual tool, which allows to select rectangular area in image viewer and scroll pages in image viewer CompositeVisualTool selectionAndScrollPages = new CompositeVisualTool( new RectangularSelectionTool(), new ScrollPages()); // create action, which allows to select rectangular area in image viewer and scroll pages in image viewer VisualToolAction rectangularSelectionAndScrollPagesVisualToolAction = new VisualToolAction( selectionAndScrollPages, selectionAndScrollPages.Name, selectionAndScrollPages.Name, GetIcon("SelectionScrollingTool.png")); // add the action to the toolstrip toolStrip.AddAction(rectangularSelectionAndScrollPagesVisualToolAction); }
/// <summary> /// Changes image viewer visual tool and unchecks the action. /// </summary> private void visualToolAction_Deactivated(object sender, EventArgs e) { VisualToolAction action = (VisualToolAction)sender; // update action check state UpdateActionCheckState(action); // if visual tool can not be changed if (!action.CanChangeImageViewerVisualTool) { return; } if (_canChangeVisualTool && ImageViewer != null) { bool isActiveToolChanged = false; if (ImageViewer.VisualTool is CompositeVisualTool) { isActiveToolChanged = ChangeActiveTool((CompositeVisualTool)ImageViewer.VisualTool, null); } if (!isActiveToolChanged) { // remove visual tool ImageViewer.VisualTool = null; } } if (_currentActionOfVisualTool == action) { // remove current visual tool action _currentActionOfVisualTool = null; } }
/// <summary> /// Creates visual tool action, which allows to enable/disable visual tool <see cref="MagnifierTool"/> in image viewer, and adds action to the toolstrip. /// </summary> /// <param name="toolStrip">The toolstrip, where actions must be added.</param> public static void CreateActions(VisualToolsToolStrip toolStrip) { // create action, which allows to magnify of image region in image viewer MagnifierToolAction magnifierToolAction = new MagnifierToolAction( new MagnifierTool(), "Magnifier Tool", "Magnifier", GetIcon("MagnifierTool.png")); // add the action to the toolstrip toolStrip.AddAction(magnifierToolAction); // create action, which allows to zoom an image region in image viewer VisualToolAction zoomSelectionToolAction = new VisualToolAction( new ZoomSelectionTool(), "Zoom Selection Tool", "Zoom selection", GetIcon("ZoomSelection.png")); // add the action to the toolstrip toolStrip.AddAction(zoomSelectionToolAction); // create action, which allows to zoom an image in image viewer VisualToolAction zoomToolAction = new VisualToolAction( new ZoomTool(), "Zoom Tool", "Zoom", GetIcon("ZoomTool.png")); // add the action to the toolstrip toolStrip.AddAction(zoomToolAction); }
/// <summary> /// Adds the measurement annotation actions to the specified action list. /// </summary> /// <param name="actions">The actions list.</param> private void AddMeasurementAnnotationActions(List <VisualToolAction> actions) { _measureTypeActionToMeasurementType = new Dictionary <VisualToolAction, MeasurementType>(); VisualToolAction lineMeasureAction = new VisualToolAction( "Line Measure", "Starts the measurement using line", null, true); _measureTypeActionToMeasurementType.Add(lineMeasureAction, MeasurementType.Line); VisualToolAction linesMeasureAction = new VisualToolAction( "Lines Measure", "Starts the measurement using lines", null, true); _measureTypeActionToMeasurementType.Add(linesMeasureAction, MeasurementType.Lines); VisualToolAction ellipseMeasureAction = new VisualToolAction( "Ellipse Measure", "Starts the measurement using ellipse", null, true); _measureTypeActionToMeasurementType.Add(ellipseMeasureAction, MeasurementType.Ellipse); VisualToolAction angleMeasureAction = new VisualToolAction( "Angle Measure", "Starts the measurement using angle", null, true); _measureTypeActionToMeasurementType.Add(angleMeasureAction, MeasurementType.Angle); foreach (VisualToolAction action in _measureTypeActionToMeasurementType.Keys) { action.Activated += new EventHandler(measureTypeAction_Activated); action.Clicked += new EventHandler(measureTypeAction_Clicked); actions.Add(action); } lineMeasureAction.Activate(); }
/// <summary> /// Adds the 'refresh' action to the specified action list. /// </summary> /// <param name="actions">The actions list.</param> private void AddRefreshActions(List <VisualToolAction> actions) { VisualToolAction refreshAction = new VisualToolAction("Refresh Measurements", "Refresh all measurements of focused image", null, false); refreshAction.Clicked += new EventHandler(refreshAction_Clicked); actions.Add(refreshAction); }
/// <summary> /// The measurement type action is activated. /// </summary> private void measureTypeAction_Activated(object sender, EventArgs e) { VisualToolAction action = (VisualToolAction)sender; if (_currentActivatedMeasureTypeAction != null) { _currentActivatedMeasureTypeAction.Deactivate(); } _currentActivatedMeasureTypeAction = action; }
/// <summary> /// Creates the tool strip item for specified action. /// </summary> /// <param name="visualToolAction">The visual tool action.</param> /// <param name="createToolStripButton">A value indicating whether the tool strip button must be created.</param> /// <returns> /// The tool strip item /// </returns> private ToolStripItem CreateToolStripItem( VisualToolAction visualToolAction, bool createToolStripButton) { // if tool strip separator must be created if (visualToolAction is SeparatorToolStripAction) { return(new ToolStripSeparator()); } ToolStripItem toolStripItem; // if tool strip button must be created if (createToolStripButton) { // if action has subactions if (visualToolAction.HasSubactions) { // create tool strip split button toolStripItem = new CheckedToolStripSplitButton(); } else { // create tool strip button toolStripItem = new ToolStripButton(); } } else { // create tool strip menu item toolStripItem = new ToolStripMenuItem(); } // set tool strip item text toolStripItem.Text = visualToolAction.Text; // set tool strip item tooltip toolStripItem.ToolTipText = visualToolAction.ToolTip; // if tool strip items is button if (createToolStripButton) { toolStripItem.DisplayStyle = ToolStripItemDisplayStyle.Image; } else { toolStripItem.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText; } // set tool strip item icon toolStripItem.Image = visualToolAction.Icon; toolStripItem.ImageScaling = ToolStripItemImageScaling.None; return(toolStripItem); }
/// <summary> /// Activates the action of drop-down menu. /// </summary> private void Button_DropDownOpened(object sender, EventArgs e) { // get current action VisualToolAction action = GetAction((ToolStripItem)sender); // if current action is not activated if (action != null && !action.IsActivated) { action.Activate(); } }
/// <summary> /// Adds the properties actions to the specified action list. /// </summary> /// <param name="actions">The actions list.</param> private void AddPropertiesActions(List <VisualToolAction> actions) { List <ImageMeasureToolUnitsOfMeasureAction> unitsOfMeasureSubActions = new List <ImageMeasureToolUnitsOfMeasureAction>(); ImageMeasureTool tool = (ImageMeasureTool)VisualTool; foreach (UnitOfMeasure unitOfMeasure in Enum.GetValues(typeof(UnitOfMeasure))) { ImageMeasureToolUnitsOfMeasureAction unitsOfMeasureAction = new ImageMeasureToolUnitsOfMeasureAction( unitOfMeasure, string.Format("{0}", unitOfMeasure), string.Format("{0} ({1})", unitOfMeasure, GetUnitOfMeasureString(unitOfMeasure)), null); unitsOfMeasureAction.Activated += new EventHandler(unitOfMeasureAction_Activated); if (unitOfMeasure == tool.UnitsOfMeasure) { unitsOfMeasureAction.Activate(); } if (_unitOfMeasureToUnitOfMeasureAction == null) { _unitOfMeasureToUnitOfMeasureAction = new Dictionary <UnitOfMeasure, ImageMeasureToolUnitsOfMeasureAction>(); } _unitOfMeasureToUnitOfMeasureAction.Add(unitOfMeasure, unitsOfMeasureAction); unitsOfMeasureSubActions.Add(unitsOfMeasureAction); } actions.Add(new VisualToolAction("Units Of Measure", "Units Of Measure", null, false, unitsOfMeasureSubActions.ToArray())); VisualToolAction propertiesAction = new VisualToolAction("Properties...", "Show properties form for image measure tool", null, false); propertiesAction.Clicked += new EventHandler(propertiesAction_Clicked); actions.Add(propertiesAction); VisualToolAction measurementPropertiesAction = new VisualToolAction("Measurement Properties...", "Show properties form for focused measurement annotation", null, false); measurementPropertiesAction.Clicked += new EventHandler(measurementPropertiesAction_Clicked); actions.Add(measurementPropertiesAction); }
/// <summary> /// Removes the specified tool strip item from the dictionary. /// </summary> /// <param name="item">The tool strip item to remove from the dictionary.</param> /// <returns> /// <b>True</b> if tool strip item was removed successfully; otherwise, <b>false</b>. /// </returns> internal bool Remove(ToolStripItem item) { if (Contains(item)) { VisualToolAction action = GetAction(item); _actionToToolStripItem.Remove(action); _toolStripItemToAction.Remove(item); return(true); } return(false); }
/// <summary> /// Adds the actions, which allows to load and save measurement annotations, to the specified action list. /// </summary> /// <param name="actions">The actions list.</param> private void AddLoadAndSaveActions(List <VisualToolAction> actions) { VisualToolAction loadMeasurementsAction = new VisualToolAction("Load Measurements...", "Load the measurement annotations from a file", null, false); loadMeasurementsAction.Clicked += new EventHandler(loadMeasurementsAction_Clicked); actions.Add(loadMeasurementsAction); VisualToolAction saveMeasurementsAction = new VisualToolAction("Save Measurements...", " Save the measurement annotations to a file", null, false); saveMeasurementsAction.Clicked += new EventHandler(saveMeasurementsAction_Clicked); actions.Add(saveMeasurementsAction); }
/// <summary> /// Updates the check state of action. /// </summary> /// <param name="visualToolAction">The visual tool action.</param> private void UpdateActionCheckState(VisualToolAction visualToolAction) { // if action can not be checked if (!visualToolAction.CheckActionButtonOnActivate) { return; } if (_toolStripDictionary.Contains(visualToolAction)) { UpdateToolStripItemCheckState(visualToolAction, _toolStripDictionary.GetItem(visualToolAction)); } if (_visualToolMenuItemDictionary.Contains(visualToolAction)) { UpdateToolStripItemCheckState(visualToolAction, _visualToolMenuItemDictionary.GetItem(visualToolAction)); } }
/// <summary> /// Activates the action. /// </summary> private void ActionItem_Click(object sender, EventArgs e) { VisualToolAction action = GetAction((ToolStripItem)sender); // if action is activated if (action.IsActivated) { // show visual tool settings action.ShowVisualToolSettings(); } else { // activate action action.Activate(); } // click on action action.Click(); }
/// <summary> /// Adds the visual tool action to this tool strip. /// </summary> /// <param name="visualToolAction">The visual tool action.</param> public void AddAction(VisualToolAction visualToolAction) { AddVisualToolActionToToolstrip(Items, visualToolAction, _toolStripDictionary); if (_visualToolsMenuItem != null) { AddVisualToolActionToToolstrip( _visualToolsMenuItem.DropDownItems, visualToolAction, _visualToolMenuItemDictionary); } SubscribeToVisualToolActions(visualToolAction); // save added item _visualToolActions.Add(visualToolAction); UpdateSelectedItem(); }
/// <summary> /// Creates visual tool actions, which allow to enable/disable visual tools (<see cref="CropSelectionTool"/>, <see cref="DragDropSelectionTool"/>, /// <see cref="OverlayImageTool"/> and <see cref="PanTool"/>) in image viewer, and adds actions to the toolstrip. /// </summary> /// <param name="toolStrip">The toolstrip, where actions must be added.</param> public static void CreateActions(VisualToolsToolStrip toolStrip) { // create action, which allows to crop an image in image viewer VisualToolAction cropSelectionToolAction = new VisualToolAction( new CropSelectionTool(), "Crop Selection Tool", "Crop selection", GetIcon("CropSelectionTool.png")); // add the action to the toolstrip toolStrip.AddAction(cropSelectionToolAction); // create action, which allows to drag-and-drop an image region in image viewer VisualToolAction dragDropSelectionToolAction = new VisualToolAction( new DragDropSelectionTool(), "Drag-n-drop Selection Tool", "Drag-n-drop selection", GetIcon("DragDropTool.png")); // add the action to the toolstrip toolStrip.AddAction(dragDropSelectionToolAction); // create action, which allows to overlay an image on a top of image in image viewer OverlayImageToolAction overlayImageToolAction = new OverlayImageToolAction( new OverlayImageTool(), "Overlay Image Tool", "Overlay Image", GetIcon("OverlayTool.png")); // add the action to the toolstrip toolStrip.AddAction(overlayImageToolAction); // create action, which allows to pan an image in image viewer VisualToolAction panToolAction = new VisualToolAction( new PanTool(), "Pan Tool", "Pan", GetIcon("PanTool.png")); // add the action to the toolstrip toolStrip.AddAction(panToolAction); }
/// <summary> /// Begins the measuremenet. /// </summary> private void measureTypeAction_Clicked(object sender, EventArgs e) { if (!_isInitialized) { return; } VisualToolAction action = (VisualToolAction)sender; MeasurementType measurementType = _measureTypeActionToMeasurementType[action]; ImageMeasureToolAction measureToolAction = (ImageMeasureToolAction)action.Parent; if (!measureToolAction.IsActivated) { measureToolAction.Activate(); } BeginMeasurement(measurementType); }
/// <summary> /// Updates the check state the specified tool strip item of action. /// </summary> /// <param name="visualToolAction">The visual tool action.</param> /// <param name="toolStripItem">The tool strip item.</param> private void UpdateToolStripItemCheckState(VisualToolAction visualToolAction, ToolStripItem toolStripItem) { // get tool strip button ToolStripButton button = toolStripItem as ToolStripButton; // if tool strip item is tool strip button if (button != null) { // update check state button.Checked = visualToolAction.IsActivated; } else { // get tool strip split button CheckedToolStripSplitButton splitButton = toolStripItem as CheckedToolStripSplitButton; // if tool strip item is tool strip split button if (splitButton != null) { // update check state splitButton.Checked = visualToolAction.IsActivated; } else { // get tool strip menu item ToolStripMenuItem menuItem = toolStripItem as ToolStripMenuItem; // if tool strip item is tool strip menu item if (menuItem != null) { // update check state menuItem.Checked = visualToolAction.IsActivated; } else { throw new NotImplementedException(); } } } }
/// <summary> /// Updates the selected visual tool item. /// </summary> private void UpdateSelectedItem() { // if image viewer is not specified if (ImageViewer == null) { return; } // if current visual tool action is specified if (_currentActionOfVisualTool != null) { // if visual tool action is selected if (ImageViewer.VisualTool == GetVisualTool(_currentActionOfVisualTool)) { return; } } // get current visual tool action VisualToolAction currentVisualToolAction = _noneVisualToolAction; // for each visual tool actions foreach (VisualToolAction action in _visualToolActions) { // if action must be selected if (ImageViewer.VisualTool == GetVisualTool(action)) { currentVisualToolAction = action; break; } } // if action is not activated if (!currentVisualToolAction.IsActivated) { _canChangeVisualTool = false; currentVisualToolAction.Activate(); _canChangeVisualTool = true; } }
/// <summary> /// Subscribes to the events of visual tool action and sub-actions. /// </summary> /// <param name="visualToolAction">The visual tool action.</param> private void SubscribeToVisualToolActions(VisualToolAction visualToolAction) { if (visualToolAction is SeparatorToolStripAction) { return; } // subscribe to the visual tool action events visualToolAction.Activated += new EventHandler(visualToolAction_Activated); visualToolAction.Deactivated += new EventHandler(visualToolAction_Deactivated); visualToolAction.StatusChanged += new EventHandler(visualToolAction_StatusChanged); visualToolAction.IconChanged += new EventHandler(visualToolAction_IconChanged); // if action contains sub-actions if (visualToolAction.HasSubactions) { foreach (VisualToolAction subAction in visualToolAction.SubActions) { SubscribeToVisualToolActions(subAction); } } }
/// <summary> /// Initializes a new instance of the <see cref="VisualToolAction"/> class. /// </summary> /// <param name="visualTool">The visual tool.</param> /// <param name="text">The action text.</param> /// <param name="toolTip">The action tool tip.</param> /// <param name="icon">The action icon.</param> /// <param name="isCheckOnActivate">A value indicating whether the action must be checked after activation.</param> /// <param name="subActions">The sub-actions of the action.</param> public VisualToolAction( VisualTool visualTool, string text, string toolTip, Image icon, bool isCheckOnActivate, params VisualToolAction[] subActions) { _isActivated = false; _visualTool = visualTool; _text = text; _toolTip = toolTip; _icon = icon; _checkActionButtonOnActivate = isCheckOnActivate; if (subActions == null) { subActions = new VisualToolAction[0]; } SetSubActions(subActions); }
/// <summary> /// Updates icon of action. /// </summary> private void visualToolAction_IconChanged(object sender, EventArgs e) { // get action VisualToolAction action = (VisualToolAction)sender; if (_toolStripDictionary.Contains(action)) { // get tool strip item of action ToolStripItem toolStripItem = _toolStripDictionary.GetItem(action); // update icon toolStripItem.Image = action.Icon; } if (_visualToolMenuItemDictionary.Contains(action)) { // get tool strip item of action ToolStripItem visualToolsMenuItem = _visualToolMenuItemDictionary.GetItem(action); // update icon visualToolsMenuItem.Image = action.Icon; } }
/// <summary> /// Changes image viewer visual tool and checks the action. /// </summary> private void visualToolAction_Activated(object sender, EventArgs e) { // update previous action of visual tool _previousActionOfVisualTool = _currentActionOfVisualTool; _actionActivationCount++; VisualToolAction action = (VisualToolAction)sender; // update action check state UpdateActionCheckState(action); // if visual tool must be changed if (action.CanChangeImageViewerVisualTool) { if (_currentActionOfVisualTool != null) { // deactivate previous visual tool action _currentActionOfVisualTool.Deactivate(); } if (_canChangeVisualTool && ImageViewer != null) { // change visual tool ImageViewer.VisualTool = GetVisualTool(action); if (ImageViewer.VisualTool is CompositeVisualTool) { ChangeActiveTool((CompositeVisualTool)ImageViewer.VisualTool, action.VisualTool); } } _currentActionOfVisualTool = action; } _actionActivationCount--; }
/// <summary> /// Returns the visual tool, which is associated with specified visual tool action. /// </summary> /// <param name="visualToolAction">The visual tool action.</param> /// <returns> /// The visual tool. /// </returns> protected virtual VisualTool GetVisualTool(VisualToolAction visualToolAction) { // if mandatory visual tool is not specified if (MandatoryVisualTool == null) { return(visualToolAction.VisualTool); } // get current visual tool VisualTool result = visualToolAction.VisualTool; // if visual tool must be created if (!_visualToolItemToVisualTool.TryGetValue(visualToolAction, out result)) { // if action visual tool is not specified if (visualToolAction.VisualTool == null) { // use mandatory visual tool result = MandatoryVisualTool; } else { // create composite visual tool: mandatory visual tool + action visual tool CompositeVisualTool compositeTool = new CompositeVisualTool( MandatoryVisualTool, visualToolAction.VisualTool); // set active visual tool compositeTool.ActiveTool = visualToolAction.VisualTool; result = compositeTool; } // save current visual tool _visualToolItemToVisualTool.Add(visualToolAction, result); } return(result); }
/// <summary> /// Sets the parent of this action. /// </summary> /// <param name="parent">The parent.</param> protected virtual void SetParent(VisualToolAction parent) { _parent = parent; }
/// <summary> /// Returns the tool strip item at the specified action. /// </summary> /// <param name="action">The action.</param> /// <returns> /// The tool strip item of action. /// </returns> internal ToolStripItem GetItem(VisualToolAction action) { return(_actionToToolStripItem[action]); }
/// <summary> /// Adds the specified action and tool strip item. /// </summary> /// <param name="action">The action.</param> /// <param name="item">The tool strip item.</param> internal void Add(VisualToolAction action, ToolStripItem item) { _actionToToolStripItem.Add(action, item); _toolStripItemToAction.Add(item, action); }
/// <summary> /// Adds the specified visual tool action to the specified collection. /// </summary> /// <param name="toolStripItemCollection">The item collection of toolstrip.</param> /// <param name="visualToolAction">The visual tool action.</param> /// <param name="visualToolActionsDictionary">A dictionary, which contains links between visual tool actions and buttons.</param> private void AddVisualToolActionToToolstrip( ToolStripItemCollection toolStripItemCollection, VisualToolAction visualToolAction, VisualToolActionButtonsDictionary visualToolActionsDictionary) { ToolStripItem toolStripItem; // if new toolstrip item must be added to the collection of this toolstrip if (toolStripItemCollection == Items) { // create the toolstip item for visual tool action toolStripItem = CreateToolStripItem(visualToolAction, true); } // if new toolstrip item must be added to the collection of other toolstrip else { // create the toolstip item for visual tool action toolStripItem = CreateToolStripItem(visualToolAction, false); } // if action is not separator if (!(visualToolAction is SeparatorToolStripAction)) { // save information about link between action and toolstrip item visualToolActionsDictionary.Add(visualToolAction, toolStripItem); // update action check state UpdateActionCheckState(visualToolAction); // if toolstrip item is split button if (toolStripItem is CheckedToolStripSplitButton) { CheckedToolStripSplitButton button = (CheckedToolStripSplitButton)toolStripItem; // subscribe to the button click event button.ButtonClick += new EventHandler(ActionItem_Click); } else { // subscribe to the item click event toolStripItem.Click += new EventHandler(ActionItem_Click); } // if visual tool action contains sub-actions if (visualToolAction.HasSubactions) { ToolStripItemCollection subActions = null; if (toolStripItem is CheckedToolStripSplitButton) { CheckedToolStripSplitButton checkedToolStripSplitButton = (CheckedToolStripSplitButton)toolStripItem; checkedToolStripSplitButton.DropDownOpened += Button_DropDownOpened; subActions = checkedToolStripSplitButton.DropDownItems; } else if (toolStripItem is ToolStripMenuItem) { ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)toolStripItem; toolStripMenuItem.DropDownOpened += Button_DropDownOpened; subActions = toolStripMenuItem.DropDownItems; } // for each sub-action foreach (VisualToolAction subAction in visualToolAction.SubActions) { AddVisualToolActionToToolstrip(subActions, subAction, visualToolActionsDictionary); } } } // add the toolstrip item to the toolstrip toolStripItemCollection.Add(toolStripItem); }
/// <summary> /// Determines whether the dictionary contains the specified action. /// </summary> /// <param name="action">The action to locate in the dictionary.</param> /// <returns> /// <b>True</b> if action is found in the dictionary; otherwise; <b>false</b>. /// </returns> internal bool Contains(VisualToolAction action) { return(_actionToToolStripItem.ContainsKey(action)); }