Browse Source

MatrixControl is drawn by WPF.

Text is drawn by System.Drawing.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6395 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
pull/1/head
Tomáš Linhart 15 years ago
parent
commit
d21fc2d0c9
  1. 11
      src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj
  2. 180
      src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs
  3. 11
      src/AddIns/Analysis/CodeQuality/Src/Controls/TreeMatrixControl.xaml
  4. 9
      src/AddIns/Analysis/CodeQuality/Src/Controls/TreeMatrixControl.xaml.cs

11
src/AddIns/Analysis/CodeQuality/CodeQualityAnalysis.csproj

@ -114,12 +114,16 @@
<Compile Include="Src\Controls\DependencyGraph.cs" /> <Compile Include="Src\Controls\DependencyGraph.cs" />
<Compile Include="Src\Controls\DependencyGraphLayout.cs" /> <Compile Include="Src\Controls\DependencyGraphLayout.cs" />
<Compile Include="Src\Controls\DependencyMatrixControl.cs"> <Compile Include="Src\Controls\DependencyMatrixControl.cs">
<SubType>Component</SubType> </Compile> <Compile Include="Src\Controls\DependencyMatrix.cs" /> <SubType>Component</SubType>
</Compile>
<Compile Include="Src\Controls\DependencyMatrix.cs" />
<Compile Include="Src\Controls\DependencyVertex.cs" /> <Compile Include="Src\Controls\DependencyVertex.cs" />
<Compile Include="Src\Controls\DependencyIconVertexConverter.cs" /> <Compile Include="Src\Controls\DependencyIconVertexConverter.cs" />
<Compile Include="Src\Controls\Matrix.cs" /> <Compile Include="Src\Controls\Matrix.cs" />
<Compile Include="Src\Controls\MatrixControl.cs"> <Compile Include="Src\Controls\MatrixControl.cs">
<SubType>Component</SubType> </Compile> <Compile Include="Src\Controls\TreeMatrixControl.xaml.cs"> <SubType>Component</SubType>
</Compile>
<Compile Include="Src\Controls\TreeMatrixControl.xaml.cs">
<DependentUpon>TreeMatrixControl.xaml</DependentUpon> <DependentUpon>TreeMatrixControl.xaml</DependentUpon>
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
@ -127,7 +131,8 @@
<Compile Include="Src\Event.cs" /> <Compile Include="Src\Event.cs" />
<Compile Include="Src\Field.cs" /> <Compile Include="Src\Field.cs" />
<Compile Include="Src\INode.cs" /> <Compile Include="Src\INode.cs" />
<Compile Include="Src\Instruction.cs" /> <Compile Include="Src\NodeIconService.cs" /> <Compile Include="Src\Instruction.cs" />
<Compile Include="Src\NodeIconService.cs" />
<Compile Include="Src\IDependency.cs" /> <Compile Include="Src\IDependency.cs" />
<Compile Include="Src\MetricsReader.cs" /> <Compile Include="Src\MetricsReader.cs" />
<Compile Include="Src\Method.cs" /> <Compile Include="Src\Method.cs" />

180
src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs

@ -1,110 +1,124 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows;
using System.Drawing; using System.Windows.Media;
using ICSharpCode.SharpDevelop.Gui.ClassBrowser; using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace ICSharpCode.CodeQualityAnalysis.Controls namespace ICSharpCode.CodeQualityAnalysis.Controls
{ {
public class MatrixControl<TValue> : DataGridView public class MatrixControl<TValue> : FrameworkElement // TODO: Virtualization
{ {
Matrix<TValue> matrix; Dictionary<string, ImageSource> imgs = new Dictionary<string, ImageSource>();
object [,] cache; Point currentPoint = new Point(-1, -1);
string font;
public Matrix<TValue> Matrix // will be loaded from Matrix
{ int cellsVertically = 25;
get int cellsHorizontally = 25;
{
return matrix; public Matrix<TValue> Matrix { get; set; }
}
public int CellHeight { get; set; }
set
{ public int CellWidth { get; set; }
Rows.Clear();
Columns.Clear();
cache = new object[value.HeaderRows.Count, value.HeaderColumns.Count];
matrix = value;
}
}
public MatrixControl() public MatrixControl()
{ {
BackgroundColor = Color.White; CellHeight = CellWidth = 36;
BorderStyle = BorderStyle.None; font = "Verdana";
}
AllowUserToAddRows = false;
AllowUserToDeleteRows = false; protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
AllowUserToResizeRows = false; {
AllowUserToResizeColumns = false; base.OnMouseMove(e);
AllowUserToOrderColumns = false;
AllowDrop = false;
EnableHeadersVisualStyles = false;
EditMode = DataGridViewEditMode.EditProgrammatically;
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
CellBorderStyle = DataGridViewCellBorderStyle.None;
ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
ColumnHeadersDefaultCellStyle.BackColor = Color.White;
RowHeadersDefaultCellStyle.BackColor = Color.White;
RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
// disable selection
DefaultCellStyle.SelectionForeColor = Color.Black;
DefaultCellStyle.SelectionBackColor = Color.White;
ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.Black;
ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.White;
RowHeadersDefaultCellStyle.SelectionForeColor = Color.Black; var point = e.GetPosition(this);
RowHeadersDefaultCellStyle.SelectionBackColor = Color.White; if (point.X < cellsHorizontally * CellWidth && point.Y < cellsVertically * CellHeight)
currentPoint = point;
else
currentPoint = new Point(-1, -1);
SelectionMode = DataGridViewSelectionMode.CellSelect; InvalidateVisual();
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
// background
var background = new Rect(0, 0, cellsHorizontally * CellWidth, cellsVertically * CellHeight);
var backgroundColor = new SolidColorBrush(Colors.Yellow);
drawingContext.DrawRectangle(backgroundColor, null, background);
// hover cell
if (currentPoint.X > 0 || currentPoint.Y > 0) {
var rect = new Rect(currentPoint.X - currentPoint.X % CellWidth, currentPoint.Y - currentPoint.Y % CellHeight, CellWidth, CellHeight);
var brush = new SolidColorBrush(Colors.Red);
drawingContext.DrawRectangle(brush, null, rect);
}
ReadOnly = true; // grid
VirtualMode = true; var pen = new Pen(Brushes.Black, 1);
EditMode = DataGridViewEditMode.EditProgrammatically;
}
public void DrawMatrix() for (int i = 0; i <= cellsHorizontally; i++) {
{ drawingContext.DrawLine(pen,
DrawHeaders(); new Point(i * CellWidth, 0),
} new Point(i * CellWidth,
cellsHorizontally * CellWidth));
}
protected void DrawHeaders() for (int i = 0; i <= cellsVertically; i++) {
{ drawingContext.DrawLine(pen,
foreach (var headerColumn in Matrix.HeaderColumns) { new Point(0, i * CellHeight),
var column = new DataGridViewTextBoxColumn(); new Point(cellsVertically * CellHeight,
column.HeaderText = headerColumn.Value.ToString(); i * CellHeight));
this.Columns.Add(column);
} }
foreach (var headerRow in Matrix.HeaderRows) { // text
var row = new DataGridViewRow(); for (int i = 0; i < cellsHorizontally; i++) {
row.HeaderCell.Value = headerRow.Value.ToString(); for (int j = 0; j < cellsVertically; j++) {
this.Rows.Add(row); drawingContext.DrawImage(CreateText(i * j), new Rect(i * CellWidth, j * CellHeight, CellWidth, CellHeight));
}
//if (!row.Displayed) // crashes sharpdevelop debugger
// break;
} }
} }
protected override void OnCellValueNeeded(DataGridViewCellValueEventArgs e) public ImageSource CreateText(int number)
{ {
if (cache[e.RowIndex, e.ColumnIndex] != null) return CreateText(number.ToString());
e.Value = cache[e.RowIndex, e.ColumnIndex]; }
if (e.RowIndex < Matrix.HeaderRows.Count && e.ColumnIndex < Matrix.HeaderColumns.Count) {
e.Value = Matrix.EvaluateCell(Matrix.HeaderRows[e.RowIndex], Matrix.HeaderColumns[e.ColumnIndex]); public ImageSource CreateText(string text)
} {
if (imgs.ContainsKey(text))
return imgs[text];
var bmp = new System.Drawing.Bitmap(CellWidth, CellHeight);
var g = System.Drawing.Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
var fontOjb = new System.Drawing.Font(font, 12);
var size = g.MeasureString(text, fontOjb);
float spanWidth = CellWidth / 2 - size.Width / 2;
float spanHeight = CellHeight / 2 - size.Height / 2;
g.DrawString(text, fontOjb, System.Drawing.Brushes.Black, new System.Drawing.PointF(spanWidth, spanHeight));
g.Dispose();
var img = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height));
imgs.Add(text, img);
return img;
} }
} }
} }

11
src/AddIns/Analysis/CodeQuality/Src/Controls/TreeMatrixControl.xaml

@ -21,7 +21,7 @@
<TreeView Name="leftTree" Grid.Row="1" Grid.Column="0"> <TreeView Name="leftTree" Grid.Row="1" Grid.Column="0">
<TreeView.Resources> <TreeView.Resources>
</TreeView.Resources> </TreeView.Resources>
</TreeView> </TreeView>
@ -31,8 +31,11 @@
</TreeView.LayoutTransform> </TreeView.LayoutTransform>
</TreeView> </TreeView>
<WindowsFormsHost Grid.Column="1" Grid.Row="1"> <Controls:DependencyMatrixControl Grid.Column="1" Grid.Row="1"
<Controls:DependencyMatrixControl x:Name="matrixControl"></Controls:DependencyMatrixControl> x:Name="matrixControl"
</WindowsFormsHost> HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
</Controls:DependencyMatrixControl>
</Grid> </Grid>
</UserControl> </UserControl>

9
src/AddIns/Analysis/CodeQuality/Src/Controls/TreeMatrixControl.xaml.cs

@ -33,18 +33,11 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls
public TreeMatrixControl() public TreeMatrixControl()
{ {
InitializeComponent(); InitializeComponent();
matrixControl.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
matrixControl.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
matrixControl.BorderStyle = BorderStyle.FixedSingle;
matrixControl.RowHeadersVisible = false;
matrixControl.ColumnHeadersVisible = false;
} }
public void DrawMatrix() public void DrawMatrix()
{ {
matrixControl.DrawMatrix(); // matrixControl.DrawMatrix();
} }
public void DrawTree(Module module) public void DrawTree(Module module)

Loading…
Cancel
Save