void setCurrentAnchorRow(int?anchorId, bool clearSelection) { if (settingCurrentAnchorRow) { return; } try { settingCurrentAnchorRow = true; if (anchorId == null) { anchors.ClearSelection(); anchors.CurrentCell = null; currentAnchorControl = null; return; } Template.Anchor a = getAnchor(anchorId, out DataGridViewRow row); if (row == null || a == null) { throw new Exception("Anchor[Id=" + anchorId + "] does not exist."); } anchors.CurrentCell = anchors[0, row.Index]; if (clearSelection) { anchors.ClearSelection(); } else { setCurrentConditionRow(null); setCurrentFieldRow(null); } Template.Anchor.Types t = ((Template.Anchor)row.Tag).Type; switch (t) { case Template.Anchor.Types.PdfText: { currentAnchorControl = new AnchorPdfTextControl(new TextAutoInsertSpace { Threshold = (float)textAutoInsertSpaceThreshold.Value, Representative = Regex.Unescape(textAutoInsertSpaceRepresentative.Text) }); } break; case Template.Anchor.Types.OcrText: { currentAnchorControl = new AnchorOcrTextControl(new TextAutoInsertSpace { Threshold = (float)textAutoInsertSpaceThreshold.Value, Representative = Regex.Unescape(textAutoInsertSpaceRepresentative.Text) }); } break; case Template.Anchor.Types.ImageData: { currentAnchorControl = new AnchorImageDataControl((float)pictureScale.Value); } break; case Template.Anchor.Types.CvImage: { currentAnchorControl = new AnchorCvImageControl((float)pictureScale.Value); } break; //case Template.Anchor.Types.Script: // { // if (currentAnchorControl == null || !(currentAnchorControl is AnchorScriptControl)) // currentAnchorControl = new AnchorScriptControl(); // } // break; default: throw new Exception("Unknown option: " + t); } currentAnchorControl.Initialize(row, (DataGridViewRow r) => { setAnchorRow(r, a); }); } finally { settingCurrentAnchorRow = false; } }
void setCurrentAnchorRow(int?anchorId, bool clearSelection) { if (settingCurrentAnchorRow) { return; } try { settingCurrentAnchorRow = true; if (anchorId == null) { anchors.ClearSelection(); anchors.CurrentCell = null; currentAnchorControl = null; return; } Template.Anchor a = getAnchor(anchorId, out DataGridViewRow row); if (row == null || a == null) { throw new Exception("Anchor[Id=" + anchorId + "] does not exist."); } if (anchors.CurrentCell?.RowIndex != row.Index) { anchors.CurrentCell = anchors[0, row.Index]; } if (clearSelection) { anchors.ClearSelection(); } else { setCurrentConditionRow(null); setCurrentFieldRow(null); } Template.Anchor.Types t = ((Template.Anchor)row.Tag).Type; switch (t) { case Template.Anchor.Types.PdfText: { currentAnchorControl = new AnchorPdfTextControl(new TextAutoInsertSpace { Threshold = (float)textAutoInsertSpace_Threshold.Value, IgnoreSourceSpaces = textAutoInsertSpace_IgnoreSourceSpaces.Checked /*, Representative//default*/ }); } break; case Template.Anchor.Types.OcrText: { currentAnchorControl = new AnchorOcrTextControl(new TextAutoInsertSpace { Threshold = (float)textAutoInsertSpace_Threshold.Value, IgnoreSourceSpaces = textAutoInsertSpace_IgnoreSourceSpaces.Checked /*, Representative//default*/ }); } break; case Template.Anchor.Types.ImageData: { currentAnchorControl = new AnchorImageDataControl((float)pictureScale.Value); } break; case Template.Anchor.Types.CvImage: { currentAnchorControl = new AnchorCvImageControl((float)pictureScale.Value); } break; default: throw new Exception("Unknown option: " + t); } currentAnchorControl.Initialize(row, (DataGridViewRow r) => { setAnchorRow(r, a); }); settingsControlHeader.Text = "Anchor [" + a?.Id + "]:"; } finally { settingCurrentAnchorRow = false; } }
void initializeAnchorsTable() { Id3.ValueType = typeof(int); Type3.ValueType = typeof(Template.Anchor.Types); Type3.DataSource = Enum.GetValues(typeof(Template.Anchor.Types)); ParentAnchorId3.ValueType = typeof(int); ParentAnchorId3.ValueMember = "Id"; ParentAnchorId3.DisplayMember = "Name"; anchors.EnableHeadersVisualStyles = false;//needed to set row headers anchors.CellPainting += delegate(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex < 0) { return; } if (e.ColumnIndex != -1) { return; } rowStates?rowState = anchors.Rows[e.RowIndex].HeaderCell.Tag as rowStates?; if (rowState == null || rowState == rowStates.NULL) { return; } e.Paint(e.ClipBounds, DataGridViewPaintParts.All); string s; switch (rowState) { case rowStates.Selected: return; case rowStates.Parent: s = "●"; break; case rowStates.Condition: s = "▶"; break; case rowStates.Linked: return; default: throw new Exception("Unknown option: " + rowState); } Rectangle r = e.CellBounds; r.X += 2; TextRenderer.DrawText(e.Graphics, s, e.CellStyle.Font, r, Color.Black, TextFormatFlags.VerticalCenter | TextFormatFlags.Left); e.Handled = true; }; anchors.CellBeginEdit += delegate(object sender, DataGridViewCellCancelEventArgs e) { if (anchors.Columns[e.ColumnIndex].Name != "ParentAnchorId3") { return; } setAnchorParentAnchorIdList(anchors.Rows[e.RowIndex]); }; anchors.RowsAdded += delegate(object sender, DataGridViewRowsAddedEventArgs e) { }; anchors.DataError += delegate(object sender, DataGridViewDataErrorEventArgs e) { DataGridViewRow r = anchors.Rows[e.RowIndex]; Message.Error("Anchor[Id=" + r.Cells["Id3"].Value + "] has unacceptable value of " + anchors.Columns[e.ColumnIndex].HeaderText + ":\r\n" + e.Exception.Message); }; anchors.UserDeletedRow += delegate(object sender, DataGridViewRowEventArgs e) { onAnchorsChanged(); }; anchors.UserDeletingRow += delegate(object sender, DataGridViewRowCancelEventArgs e) { if (anchors.Rows.Count < 3 && anchors.SelectedRows.Count > 0) { anchors.SelectedRows[0].Selected = false;//to avoid auto-creating row } }; anchors.CurrentCellDirtyStateChanged += delegate { if (anchors.IsCurrentCellDirty) { anchors.CommitEdit(DataGridViewDataErrorContexts.Commit); } }; anchors.CellValueChanged += delegate(object sender, DataGridViewCellEventArgs e) { if (loadingTemplate) { return; } if (e.ColumnIndex < 0)//row's header { return; } var row = anchors.Rows[e.RowIndex]; Template.Anchor a = (Template.Anchor)row.Tag; switch (anchors.Columns[e.ColumnIndex].Name) { //case "Id3": // { // int? anchorId = (int?)row.Cells["Id3"].Value; // if (anchorId == null) // break; // Template.Anchor a = (Template.Anchor)row.Tag; // a.Id = (int)anchorId; // setAnchorRow(row, a); // break; // } case "Type3": { Template.Anchor.Types t2 = (Template.Anchor.Types)row.Cells["Type3"].Value; if (t2 == a.Type) { break; } Template.Anchor a2; switch (t2) { case Template.Anchor.Types.PdfText: a2 = new Template.Anchor.PdfText(); break; case Template.Anchor.Types.OcrText: a2 = new Template.Anchor.OcrText(); break; case Template.Anchor.Types.ImageData: a2 = new Template.Anchor.ImageData(); break; case Template.Anchor.Types.CvImage: a2 = new Template.Anchor.CvImage(); break; //case Template.Anchor.Types.Script: // a2 = new Template.Anchor.Script(); // break; default: throw new Exception("Unknown option: " + t2); } a2.Id = a.Id; a = a2; break; } case "ParentAnchorId3": { a.ParentAnchorId = (int?)row.Cells["ParentAnchorId3"].Value; break; } } setAnchorRow(row, a); clearImageFromBoxes(); findAndDrawAnchor(a.Id); }; anchors.SelectionChanged += delegate(object sender, EventArgs e) { try { if (loadingTemplate) { return; } if (settingCurrentAnchorRow) { return; } if (anchors.SelectedRows.Count < 1) { setCurrentAnchorRow(null, true); return; } var row = anchors.SelectedRows[0]; Template.Anchor a = (Template.Anchor)row.Tag; if (a == null)//hacky forcing to commit a newly added row and display the blank row { int i = anchors.Rows.Add(); row = anchors.Rows[i]; a = templateManager.CreateDefaultAnchor(); setAnchorRow(row, a); onAnchorsChanged(); row.Selected = true; return; } setCurrentAnchorRow(a.Id, false); showAnchorRowAs(a.Id, rowStates.Selected, true); clearImageFromBoxes(); findAndDrawAnchor(a.Id); } catch (Exception ex) { Message.Error2(ex); } }; }
void initializeAnchorsTable() { Id3.ValueType = typeof(int); Type3.ValueType = typeof(Template.Anchor.Types); Type3.DataSource = Enum.GetValues(typeof(Template.Anchor.Types)); ParentAnchorId3.ValueType = typeof(int); ParentAnchorId3.ValueMember = "Id"; ParentAnchorId3.DisplayMember = "Name"; Pattern.DefaultCellStyle.NullValue = null; //to avoid error when changing cell type to image anchors.EnableHeadersVisualStyles = false; //needed to set row headers anchors.CellPainting += delegate(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex < 0) { return; } if (e.ColumnIndex != -1) { return; } rowStates?rowState = anchors.Rows[e.RowIndex].HeaderCell.Tag as rowStates?; if (rowState == null || rowState == rowStates.NULL) { return; } e.Paint(e.ClipBounds, DataGridViewPaintParts.All); string s; switch (rowState) { case rowStates.Selected: return; case rowStates.Parent: s = "●"; break; case rowStates.Condition: s = "▶"; break; case rowStates.Linked: return; default: throw new Exception("Unknown option: " + rowState); } Rectangle r = e.CellBounds; r.X += 2; TextRenderer.DrawText(e.Graphics, s, e.CellStyle.Font, r, Color.Black, TextFormatFlags.VerticalCenter | TextFormatFlags.Left); e.Handled = true; }; anchors.CellBeginEdit += delegate(object sender, DataGridViewCellCancelEventArgs e) { string cn = anchors.Columns[e.ColumnIndex].Name; if (cn == "SearchRectangleMargin") { DataGridViewCell c = anchors[e.ColumnIndex, e.RowIndex]; if (string.IsNullOrWhiteSpace((string)c.Value) || !int.TryParse((string)c.Value, out int searchRectangleMargin)) { c.Value = Settings.Constants.InitialSearchRectangleMargin.ToString(); } return; } if (cn == "ParentAnchorId3") { setAnchorParentAnchorIdList(anchors.Rows[e.RowIndex]); return; } }; anchors.RowsAdded += delegate(object sender, DataGridViewRowsAddedEventArgs e) { }; anchors.DataError += delegate(object sender, DataGridViewDataErrorEventArgs e) { DataGridViewRow r = anchors.Rows[e.RowIndex]; Message.Error("Anchor[Id=" + r.Cells["Id3"].Value + "] has unacceptable value of " + anchors.Columns[e.ColumnIndex].HeaderText + ":\r\n" + e.Exception.Message, this); }; anchors.UserDeletedRow += delegate(object sender, DataGridViewRowEventArgs e) { onAnchorsChanged(); }; anchors.UserDeletingRow += delegate(object sender, DataGridViewRowCancelEventArgs e) { if (anchors.Rows.Count < 3 && anchors.SelectedRows.Count > 0) { anchors.SelectedRows[0].Selected = false;//to avoid auto-creating row } }; //anchors.CellFormatting += delegate (object sender, DataGridViewCellFormattingEventArgs e) // { // if (anchors.Columns[e.ColumnIndex].Name == "Pattern") // e.FormattingApplied = false; // }; anchors.CurrentCellDirtyStateChanged += delegate(object sender, EventArgs e) { if (anchors.IsCurrentCellDirty) { //string cn = anchors.Columns[anchors.CurrentCell.ColumnIndex].Name; //if (cn == "SearchRectangleMargin") // return; anchors.CommitEdit(DataGridViewDataErrorContexts.Commit); } }; anchors.RowValidating += delegate(object sender, DataGridViewCellCancelEventArgs e) { //if (loadingTemplate) // return; //DataGridViewRow r = anchors.Rows[e.RowIndex]; //try //{ // if (r.Tag != null) // { // //if (!string.IsNullOrWhiteSpace((string)r.Cells["SearchRectangleMargin"].Value) && !int.TryParse((string)r.Cells["SearchRectangleMargin"].Value, out int searchRectangleMargin)) // // throw new Exception("SearchRectangleMargin must be a non-negative integer."); // } //} //catch (Exception ex) //{ // Message.Error2(ex, this); // e.Cancel = true; //} }; bool isWithinCellValueChanged = false; anchors.CellValueChanged += delegate(object sender, DataGridViewCellEventArgs e) { if (loadingTemplate || isWithinCellValueChanged) { return; } try { isWithinCellValueChanged = true; if (e.ColumnIndex < 0)//row's header { return; } var row = anchors.Rows[e.RowIndex]; Template.Anchor a = (Template.Anchor)row.Tag; DataGridViewCell c = anchors[e.ColumnIndex, e.RowIndex]; switch (anchors.Columns[e.ColumnIndex].Name) { case "ParentAnchorId3": { a.ParentAnchorId = (int?)row.Cells["ParentAnchorId3"].Value; break; } case "Pattern": return; case "Type3": { Template.Anchor.Types t2 = (Template.Anchor.Types)c.Value; if (t2 == a.Type) { break; } Template.Anchor a2; switch (t2) { case Template.Anchor.Types.PdfText: a2 = new Template.Anchor.PdfText(); break; case Template.Anchor.Types.OcrText: a2 = new Template.Anchor.OcrText(); break; case Template.Anchor.Types.ImageData: a2 = new Template.Anchor.ImageData(); break; case Template.Anchor.Types.CvImage: a2 = new Template.Anchor.CvImage(); break; default: throw new Exception("Unknown option: " + t2); } a2.Id = a.Id; a = a2; break; } case "SearchRectangleMargin": { if (string.IsNullOrWhiteSpace((string)c.Value) || !int.TryParse((string)c.Value, out int searchRectangleMargin)) { a.SearchRectangleMargin = -1; break; } a.SearchRectangleMargin = searchRectangleMargin < 0 ? -1 : searchRectangleMargin; break; } } setAnchorRow(row, a); clearImageFromBoxes(); findAndDrawAnchor(a.Id); } finally { isWithinCellValueChanged = false; } }; anchors.SelectionChanged += delegate(object sender, EventArgs e) { try { if (loadingTemplate) { return; } if (settingCurrentAnchorRow) { return; } if (anchors.SelectedRows.Count < 1) { setCurrentAnchorRow(null, true); return; } var row = anchors.SelectedRows[0]; Template.Anchor a = (Template.Anchor)row.Tag; if (a == null)//hacky forcing to commit a newly added row and display the blank row { int i = anchors.Rows.Add(); row = anchors.Rows[i]; a = templateManager.CreateDefaultAnchor(); setAnchorRow(row, a); onAnchorsChanged(); row.Selected = true; return; } setCurrentAnchorRow(a.Id, false); showAnchorRowAs(a.Id, rowStates.Selected, true); clearImageFromBoxes(); findAndDrawAnchor(a.Id); } catch (Exception ex) { Message.Error2(ex, this); } }; }