Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/29/15 09:27:31 (9 years ago)
Author:
jkarder
Message:

#1265: implemented new chart mode concept

Location:
branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3
Files:
5 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/ChartControl.Designer.cs

    r13045 r13076  
    5555      this.propertiesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
    5656      this.toolTip = new System.Windows.Forms.ToolTip(this.components);
    57       this.selectButton = new System.Windows.Forms.RadioButton();
    58       this.moveButton = new System.Windows.Forms.RadioButton();
    59       this.zoomInButton = new System.Windows.Forms.RadioButton();
    60       this.zoomOutButton = new System.Windows.Forms.RadioButton();
    6157      this.buttonPanel = new System.Windows.Forms.FlowLayoutPanel();
    6258      this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
     
    173169      this.saveFileDialog.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF (*.emf)|*.emf|PNG (*.png)|*.png|GIF (" +
    174170    "*.gif)|*.gif|TIFF (*.tif)|*.tif";
    175       //
    176       // selectButton
    177       //
    178       this.selectButton.Appearance = System.Windows.Forms.Appearance.Button;
    179       this.selectButton.Image = HeuristicLab.Common.Resources.VSImageLibrary.Pointer;
    180       this.selectButton.Location = new System.Drawing.Point(3, 3);
    181       this.selectButton.Name = "selectButton";
    182       this.selectButton.Size = new System.Drawing.Size(26, 24);
    183       this.selectButton.TabIndex = 1;
    184       this.selectButton.TabStop = true;
    185       this.selectButton.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    186       this.toolTip.SetToolTip(this.selectButton, "Select");
    187       this.selectButton.UseVisualStyleBackColor = true;
    188       this.selectButton.CheckedChanged += new System.EventHandler(this.SelectButtonOnCheckedChanged);
    189       //
    190       // moveButton
    191       //
    192       this.moveButton.Appearance = System.Windows.Forms.Appearance.Button;
    193       this.moveButton.Image = HeuristicLab.Common.Resources.VSImageLibrary.Breakpoint;
    194       this.moveButton.Location = new System.Drawing.Point(3, 33);
    195       this.moveButton.Name = "moveButton";
    196       this.moveButton.Size = new System.Drawing.Size(26, 24);
    197       this.moveButton.TabIndex = 1;
    198       this.moveButton.TabStop = true;
    199       this.moveButton.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    200       this.toolTip.SetToolTip(this.moveButton, "Move");
    201       this.moveButton.UseVisualStyleBackColor = true;
    202       this.moveButton.CheckedChanged += new System.EventHandler(this.MoveButtonOnCheckedChanged);
    203       //
    204       // zoomInButton
    205       //
    206       this.zoomInButton.Appearance = System.Windows.Forms.Appearance.Button;
    207       this.zoomInButton.Image = HeuristicLab.Common.Resources.VSImageLibrary.ZoomIn;
    208       this.zoomInButton.Location = new System.Drawing.Point(3, 63);
    209       this.zoomInButton.Name = "zoomInButton";
    210       this.zoomInButton.Size = new System.Drawing.Size(26, 24);
    211       this.zoomInButton.TabIndex = 1;
    212       this.zoomInButton.TabStop = true;
    213       this.zoomInButton.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    214       this.toolTip.SetToolTip(this.zoomInButton, "Zoom In");
    215       this.zoomInButton.UseVisualStyleBackColor = true;
    216       this.zoomInButton.CheckedChanged += new System.EventHandler(this.ZoomInButtonOnCheckedChanged);
    217       //
    218       // zoomOutButton
    219       //
    220       this.zoomOutButton.Appearance = System.Windows.Forms.Appearance.Button;
    221       this.zoomOutButton.Image = HeuristicLab.Common.Resources.VSImageLibrary.ZoomOut;
    222       this.zoomOutButton.Location = new System.Drawing.Point(3, 93);
    223       this.zoomOutButton.Name = "zoomOutButton";
    224       this.zoomOutButton.Size = new System.Drawing.Size(26, 24);
    225       this.zoomOutButton.TabIndex = 1;
    226       this.zoomOutButton.TabStop = true;
    227       this.zoomOutButton.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    228       this.toolTip.SetToolTip(this.zoomOutButton, "Zoom Out");
    229       this.zoomOutButton.UseVisualStyleBackColor = true;
    230       this.zoomOutButton.CheckedChanged += new System.EventHandler(this.ZoomOutButtonOnCheckedChanged);
    231       //
    232       // buttonPanel
    233       //
    234171      this.buttonPanel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    235172            | System.Windows.Forms.AnchorStyles.Left)));
    236       this.buttonPanel.Controls.Add(this.selectButton);
    237       this.buttonPanel.Controls.Add(this.moveButton);
    238       this.buttonPanel.Controls.Add(this.zoomInButton);
    239       this.buttonPanel.Controls.Add(this.zoomOutButton);
    240173      this.buttonPanel.Location = new System.Drawing.Point(0, 0);
    241174      this.buttonPanel.Name = "buttonPanel";
     
    275208    protected System.Windows.Forms.ToolStripSeparator toolStripMenuItem1;
    276209    protected System.Windows.Forms.ToolStripSeparator toolStripMenuItem2;
    277     protected System.Windows.Forms.RadioButton selectButton;
    278     protected System.Windows.Forms.RadioButton moveButton;
    279     protected System.Windows.Forms.RadioButton zoomInButton;
    280     protected System.Windows.Forms.RadioButton zoomOutButton;
    281210    protected System.Windows.Forms.FlowLayoutPanel buttonPanel;
    282211    protected System.Windows.Forms.SaveFileDialog saveFileDialog;
  • branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/ChartControl.cs

    r13045 r13076  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.ComponentModel;
    2425using System.Drawing;
    25 using System.Drawing.Drawing2D;
    2626using System.Drawing.Imaging;
    2727using System.Linq;
     
    3030namespace HeuristicLab.Visualization {
    3131  public partial class ChartControl : UserControl {
    32     public static readonly Cursor HandCursor = new Cursor(typeof(ChartControl), "Resources.Hand.cur");
    33     public static readonly Cursor ZoomOutCursor = new Cursor(typeof(ChartControl), "Resources.ZoomOut.cur");
    34     public static readonly Cursor ZoomInCursor = new Cursor(typeof(ChartControl), "Resources.ZoomIn.cur");
    35 
    3632    protected Bitmap Picture;
    3733    protected bool RenderingRequired;
    38     protected Point ButtonDownPoint;
    39     protected Point PreviousLocation;
    40     protected int MouseClickCount;
     34    protected Dictionary<Type, ChartMode> Modes = new Dictionary<Type, ChartMode>();
    4135
    4236    protected bool SuppressEvents { get; set; }
    4337    private bool SuppressRender { get; set; }
     38
     39    public PictureBox PictureBox { get { return pictureBox; } }
    4440
    4541    private IChart chart;
     
    7874      InitializeComponent();
    7975      ScaleOnResize = true;
    80       Mode = ChartMode.Select;
    8176      Chart = defaultChart ?? new Chart(0, 0, Width, Height);
     77    }
     78
     79    public void AddChartModes(params ChartMode[] chartModes) {
     80      foreach (var chartMode in chartModes)
     81        Modes.Add(chartMode.GetType(), chartMode);
     82
     83      RebuildButtonPanel();
     84    }
     85
     86    protected virtual void RebuildButtonPanel() {
     87      buttonPanel.Controls.Clear();
     88
     89      int locationY = 3;
     90      foreach (var chartMode in Modes.Values) {
     91        var rb = CreateChartModeRadioButton(chartMode);
     92        rb.Location = new Point(3, locationY);
     93        toolTip.SetToolTip(rb, chartMode.ToolTip);
     94        buttonPanel.Controls.Add(rb);
     95        locationY += rb.Height + rb.Margin.Vertical;
     96      }
    8297    }
    8398
     
    92107
    93108    protected virtual void SetButtonCheckedSate() {
    94       selectButton.Checked = false;
    95       moveButton.Checked = false;
    96       zoomInButton.Checked = false;
    97       zoomOutButton.Checked = false;
    98       if (mode == ChartMode.Select) selectButton.Checked = true;
    99       else if (mode == ChartMode.Move) moveButton.Checked = true;
    100       else if (mode == ChartMode.ZoomIn) zoomInButton.Checked = true;
    101       else if (mode == ChartMode.ZoomOut) zoomOutButton.Checked = true;
     109      foreach (var rb in buttonPanel.Controls.OfType<RadioButton>())
     110        rb.Checked = rb.Tag == Mode;
    102111    }
    103112
     
    107116
    108117    protected virtual Cursor GetPictureBoxCursor() {
    109       if (Mode == ChartMode.Select)
    110         return DefaultCursor;
    111       if (Mode == ChartMode.Move)
    112         return HandCursor;
    113       if (Mode == ChartMode.ZoomIn)
    114         return ZoomInCursor;
    115       if (Mode == ChartMode.ZoomOut)
    116         return ZoomOutCursor;
    117       return null;
     118      if (mode == null) return null;
     119
     120      return mode.Cursor;
    118121    }
    119122
     
    150153
    151154    protected virtual void PictureBoxOnMouseClick(object sender, MouseEventArgs e) {
    152 
     155      if (mode == null) return;
     156      mode.HandleOnMouseClick(sender, e);
    153157    }
    154158
    155159    protected virtual void PictureBoxOnMouseDoubleClick(object sender, MouseEventArgs e) {
    156 
     160      if (mode == null) return;
     161      mode.HandleOnMouseDoubleClick(sender, e);
    157162    }
    158163
    159164    protected virtual void OnKeyDown(object sender, KeyEventArgs e) {
    160       if (Mode == ChartMode.Select) {
    161         if (e.KeyCode == Keys.Delete) {
    162           try {
    163             SuspendRendering();
    164             foreach (var primitive in Chart.Group.SelectedPrimitives)
    165               Chart.Group.Remove(primitive);
    166           } finally { ResumeRendering(); }
    167         }
    168       }
     165      if (mode == null) return;
     166      mode.HandleOnKeyDown(sender, e);
    169167    }
    170168
    171169    protected virtual void OnKeyUp(object sender, KeyEventArgs e) {
    172 
     170      if (mode == null) return;
     171      mode.HandleOnKeyUp(sender, e);
    173172    }
    174173
    175174    protected virtual void PictureBoxOnMouseWheel(object sender, MouseEventArgs e) {
    176 
     175      if (mode == null) return;
     176      mode.HandleOnMouseWheel(sender, e);
    177177    }
    178178
    179179    protected virtual void PictureBoxOnMouseDown(object sender, MouseEventArgs e) {
    180       if (Chart == null) return;
    181       MouseClickCount = e.Clicks;
    182       ButtonDownPoint = e.Location;
    183       if (Mode != ChartMode.Select) return;
    184 
    185       try {
    186         SuspendRendering();
    187         var worldLocation = Chart.TransformPixelToWorld(e.Location);
    188         foreach (var sp in Chart.Group.SelectedPrimitives.Where(x => !x.ContainsPoint(worldLocation)))
    189           sp.Selected = false;
    190         var p = Chart.GetPrimitive(e.Location);
    191         if (p != null) p.Selected = true;
    192       } finally { ResumeRendering(); }
     180      if (mode == null) return;
     181      mode.HandleOnMouseDown(sender, e);
    193182    }
    194183
    195184    protected virtual void PictureBoxOnMouseUp(object sender, MouseEventArgs e) {
    196       if (Chart == null) return;
    197       if (e.Button == MouseButtons.Left) {
    198         if (Mode == ChartMode.ZoomIn) {
    199           //pictureBox.Refresh();
    200           var lowerLeft = new Point(Math.Min(e.X, ButtonDownPoint.X), Math.Max(e.Y, ButtonDownPoint.Y));
    201           var upperRight = new Point(Math.Max(e.X, ButtonDownPoint.X), Math.Min(e.Y, ButtonDownPoint.Y));
    202           if ((lowerLeft.X != upperRight.X) && (lowerLeft.Y != upperRight.Y))
    203             Chart.ZoomIn(lowerLeft, upperRight);
    204           else Chart.ZoomIn(ButtonDownPoint);
    205         } else if (Mode == ChartMode.ZoomOut) {
    206           Chart.ZoomOut();
    207         }
    208       } else if (e.Button == MouseButtons.Right) {
     185      if (mode == null) return;
     186      mode.HandleOnMouseUp(sender, e);
     187
     188      if (Chart == null) return;
     189
     190      if (e.Button == MouseButtons.Right) {
    209191        propertiesToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
    210192        oneLayerUpToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
     
    212194        intoForegroundToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
    213195        intoBackgroundToolStripMenuItem.Enabled = Chart.Group.SelectedPrimitives.Count() == 1;
    214       } else if (e.Button == MouseButtons.Middle) {
    215         if (Mode == ChartMode.Select) {
    216           try {
    217             SuspendRendering();
    218             foreach (var p in Chart.Group.SelectedPrimitives)
    219               p.Selected = false;
    220           } finally { ResumeRendering(); }
    221         } else if (Mode == ChartMode.Move || Mode == ChartMode.ZoomIn || Mode == ChartMode.ZoomOut) {
    222           Chart.Unzoom();
    223         }
    224196      }
    225197    }
     
    227199    protected virtual void PictureBoxOnMouseMove(object sender, MouseEventArgs e) {
    228200      if (InvokeRequired) {
    229         Invoke((Action<Object, MouseEventArgs>)PictureBoxOnMouseMove, sender, e);
    230         return;
    231       }
    232       var toolTipText = Chart.GetToolTipText(e.Location);
    233       if (toolTip.GetToolTip(pictureBox) != toolTipText) toolTip.SetToolTip(pictureBox, toolTipText);
    234 
    235       Cursor cursor;
    236       if (Mode == ChartMode.Select) {
    237         cursor = Chart.GetCursor(e.Location) ?? GetPictureBoxCursor();
    238       } else cursor = GetPictureBoxCursor();
    239       SetPictureBoxCursor(cursor);
    240 
    241       try {
    242         if (e.Button == MouseButtons.None) return;
    243 
    244         if (Mode == ChartMode.Select && e.Button == MouseButtons.Left) {
    245           var previousWorldLocation = Chart.TransformPixelToWorld(PreviousLocation);
    246           var worldLocation = Chart.TransformPixelToWorld(e.Location);
    247           var offset = worldLocation - previousWorldLocation;
    248           try {
    249             SuspendRendering();
    250             foreach (var primitive in Chart.Group.SelectedPrimitives.Where(p => p.ContainsPoint(previousWorldLocation)))
    251               primitive.Move(previousWorldLocation, offset);
    252           } finally { ResumeRendering(); }
    253         } else if (Mode == ChartMode.Move) {
    254           var previousWorldLocation = Chart.TransformPixelToWorld(PreviousLocation);
    255           var worldLocation = Chart.TransformPixelToWorld(e.Location);
    256           var offset = worldLocation - previousWorldLocation;
    257           Chart.Move(new Offset(-1 * offset.DX, -1 * offset.DY));
    258         } else if (Mode == ChartMode.ZoomIn && e.Button == MouseButtons.Left) {
    259           pictureBox.Refresh();
    260           using (var graphics = pictureBox.CreateGraphics()) {
    261             using (var pen = new Pen(Color.Gray) { DashStyle = DashStyle.Dash }) {
    262               graphics.DrawRectangle(pen,
    263                 Math.Min(e.X, ButtonDownPoint.X),
    264                 Math.Min(e.Y, ButtonDownPoint.Y),
    265                 Math.Abs(e.X - ButtonDownPoint.X),
    266                 Math.Abs(e.Y - ButtonDownPoint.Y));
    267             }
    268           }
    269         }
    270       } finally {
    271         PreviousLocation = e.Location;
    272         pictureBox.Update();
     201        Invoke((Action<object, MouseEventArgs>)PictureBoxOnMouseMove, sender, e);
     202      } else {
     203        var toolTipText = Chart.GetToolTipText(e.Location);
     204        if (toolTip.GetToolTip(pictureBox) != toolTipText) toolTip.SetToolTip(pictureBox, toolTipText);
     205
     206        if (mode == null) return;
     207
     208        mode.HandleOnMouseMove(sender, e);
     209
     210        SetPictureBoxCursor(mode.Cursor);
    273211      }
    274212    }
     
    276214    protected virtual void PictureBoxOnMouseEnter(object sender, EventArgs e) {
    277215      if (InvokeRequired) {
    278         Invoke((Action<Object, MouseEventArgs>)PictureBoxOnMouseEnter, sender, e);
     216        Invoke((Action<object, MouseEventArgs>)PictureBoxOnMouseEnter, sender, e);
    279217        return;
    280218      }
    281219      if (!Focused) this.pictureBox.Focus();
     220
     221      if (mode == null) return;
     222
     223      mode.HandleOnMouseEnter(sender, e);
    282224    }
    283225
    284226    protected virtual void PictureBoxOnMouseLeave(object sender, EventArgs e) {
    285 
     227      if (mode == null) return;
     228      mode.HandleOnMouseLeave(sender, e);
    286229    }
    287230
     
    292235      }
    293236      if (!Focused) this.pictureBox.Focus();
     237
     238      if (mode == null) return;
     239
     240      mode.HandleOnClick(sender, e);
    294241    }
    295242    #endregion
     
    392339    }
    393340
    394     protected virtual void SelectButtonOnCheckedChanged(object sender, EventArgs e) {
    395       if (selectButton.Checked) Mode = ChartMode.Select;
    396     }
    397 
    398     protected virtual void MoveButtonOnCheckedChanged(object sender, EventArgs e) {
    399       if (moveButton.Checked) Mode = ChartMode.Move;
    400     }
    401 
    402     protected virtual void ZoomInButtonOnCheckedChanged(object sender, EventArgs e) {
    403       if (zoomInButton.Checked) Mode = ChartMode.ZoomIn;
    404     }
    405 
    406     protected virtual void ZoomOutButtonOnCheckedChanged(object sender, EventArgs e) {
    407       if (zoomOutButton.Checked) Mode = ChartMode.ZoomOut;
    408     }
    409 
    410341    private void ChartControl_Load(object sender, EventArgs e) {
    411342      if (!SuppressRender) GenerateImage();
    412343    }
     344
     345    #region Helpers
     346    private RadioButton CreateChartModeRadioButton(ChartMode chartMode) {
     347      var rb = new RadioButton {
     348        Appearance = Appearance.Button,
     349        Image = chartMode.Image,
     350        Size = new Size(24, 24),
     351        TabStop = true,
     352        Tag = chartMode,
     353        UseVisualStyleBackColor = true
     354      };
     355
     356      rb.CheckedChanged += (sender, args) => { if (rb.Checked) Mode = chartMode; };
     357
     358      return rb;
     359    }
     360    #endregion
    413361  }
    414362}
  • branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/ChartMode.cs

    r13045 r13076  
    2020#endregion
    2121
     22using System;
     23using System.Drawing;
     24using System.Windows.Forms;
     25
    2226namespace HeuristicLab.Visualization {
    23   public class ChartMode {
    24     protected ChartMode() { }
     27  public abstract class ChartMode {
     28    protected readonly ChartControl chartControl;
     29    protected Cursor cursor;
     30    protected Point buttonDownPoint;
     31    protected Point previousLocation;
     32    protected int mouseClickCount;
    2533
    26     public static readonly ChartMode Select = new ChartMode();
    27     public static readonly ChartMode Move = new ChartMode();
    28     public static readonly ChartMode ZoomIn = new ChartMode();
    29     public static readonly ChartMode ZoomOut = new ChartMode();
     34    public abstract Image Image { get; }
     35    public abstract string ToolTip { get; }
     36
     37    public virtual Cursor Cursor {
     38      get { return cursor ?? (cursor = Cursors.Default); }
     39    }
     40
     41    protected ChartMode(ChartControl control) {
     42      chartControl = control;
     43    }
     44
     45    public virtual void HandleOnKeyDown(object sender, KeyEventArgs e) { }
     46    public virtual void HandleOnKeyUp(object sender, KeyEventArgs e) { }
     47    public virtual void HandleOnMouseClick(object sender, MouseEventArgs e) { }
     48    public virtual void HandleOnMouseDoubleClick(object sender, MouseEventArgs e) { }
     49    public virtual void HandleOnMouseWheel(object sender, MouseEventArgs e) { }
     50
     51    public virtual void HandleOnMouseDown(object sender, MouseEventArgs e) {
     52      buttonDownPoint = e.Location;
     53      mouseClickCount = e.Clicks;
     54    }
     55
     56    public virtual void HandleOnMouseUp(object sender, MouseEventArgs e) { }
     57    public virtual void HandleOnMouseMove(object sender, MouseEventArgs e) { }
     58    public virtual void HandleOnMouseEnter(object sender, EventArgs e) { }
     59    public virtual void HandleOnMouseLeave(object sender, EventArgs e) { }
     60    public virtual void HandleOnClick(object sender, EventArgs e) { }
    3061  }
    3162}
  • branches/HeuristicLab.Visualization/HeuristicLab.Visualization/3.3/HeuristicLab.Visualization-3.3.csproj

    r13045 r13076  
    130130    <Compile Include="ChartMode.cs" />
    131131    <Compile Include="AxisType.cs" />
     132    <Compile Include="ChartModes\MoveChartMode.cs" />
    132133    <Compile Include="Primitives\Grid.cs" />
    133134    <Compile Include="Properties\Resources.Designer.cs">
     
    136137      <DependentUpon>Resources.resx</DependentUpon>
    137138    </Compile>
     139    <Compile Include="ChartModes\SelectChartMode.cs" />
    138140    <Compile Include="Trees\LayoutEngines\BoxesLayoutEngine.cs" />
    139141    <Compile Include="Trees\LayoutEngines\ILayoutEngine.cs" />
     
    142144    <Compile Include="Trees\VisualTreeNode.cs" />
    143145    <Compile Include="Trees\VisualTreeNodeConnection.cs" />
     146    <Compile Include="ChartModes\ZoomInChartMode.cs" />
     147    <Compile Include="ChartModes\ZoomOutChartMode.cs" />
    144148    <None Include="Plugin.cs.frame" />
    145149    <Compile Include="Plugin.cs" />
Note: See TracChangeset for help on using the changeset viewer.