public virtual void doDoubleClick(PanelDoubleBuffered pn, ShapeBase ns) { parentPanel = pn; namingShape = ns; if (parentPanel.isNaming) { parentPanel.AffectingShape.endEditName(); return; } parentPanel.AffectingShape = ns; parentPanel.isNaming = true; txtName = new TextBox(); //cho class con định vị xong rồi add lên shape theo vị trí đó dinhviTextBox(txtName); txtName.Text = sName; //hiện tên cũ sName = ""; this.Invalidate(); this.Controls.Add(txtName); txtName.SelectAll(); txtName.Focus(); txtName.KeyPress += new System.Windows.Forms.KeyPressEventHandler(txtName_KeyPress); //Data type cho attribute if (this is AttributeShape) { if (((AttributeShape)this).attributeChilds.Count == 0) { ucDataDescription = new DataDescription(); ucDataDescription.Location = new Point(this.Location.X + this.Width + 5, this.Location.Y); parentPanel.Controls.Add(ucDataDescription); //Cho USC nằm lên trên. parentPanel.Controls.SetChildIndex(ucDataDescription, 0); if (((AttributeShape)this).type == AttributeType.Key) ucDataDescription.chkNull.Enabled = false; if (((AttributeShape)this).dataType != null) ucDataDescription.cboDataType.SelectedItem = ((AttributeShape)this).dataType; if (((AttributeShape)this).dataLength != 0) ucDataDescription.txtLength.Text = ((AttributeShape)this).dataLength.ToString(); ucDataDescription.chkNull.Checked = ((AttributeShape)this).allowNull; if (((AttributeShape)this).description != "") ucDataDescription.txtDescription.Text = ((AttributeShape)this).description; ucDataDescription.btnOK.Click += new EventHandler(btnOK_Click); } } }
public static int CalculateCardiDirection(ShapeBase shape1, ShapeBase shape2) { if (shape1 == null || shape2 == null) return 0; //Cạnh đặt Shape - > 1,2,3,4: Up, Right, Down, Left int edgeCardiPlace = -1; Point point1 = shape1.CenterPoint; Point point2 = shape2.CenterPoint; //Cách khác: phương trình qua 2 điểm centerRelationship và point1 (y = mx + b) double m = point2.Y - point1.Y; if (point2.X != point1.X) m /= point2.X - point1.X; else m = 0; double b = point2.Y - m * point2.X; if (point1.X < point2.X) //shape 2 nằm bên phải { //thế "x + width" của shape 1 vào phương trình để tính y //Nếu y vượt quá khoảng y của shape 2 thì đặt cardi ở up hoặc down: else đặt right double ytemp = m * (shape1.Location.X + shape1.Width) + b; if (ytemp < shape1.Location.Y) edgeCardiPlace = 1; //Up else if (ytemp > shape1.Location.Y + shape1.Height) edgeCardiPlace = 3; //Down if (ytemp >= shape1.Location.Y && ytemp <= shape1.Location.Y + shape1.Height) edgeCardiPlace = 2; //Right } else //shape 2 nằm bên trái hoặc nằm thẳng hàng { //thế "x" của shape 1 vào phương trình để tính y //Nếu y vượt quá khoảng y của shape 2 thì đặt cardi ở up hoặc down: else đặt left double ytemp = m * (shape1.Location.X) + b; if (ytemp < shape1.Location.Y) edgeCardiPlace = 1; //Up else if (ytemp > shape1.Location.Y + shape1.Height) edgeCardiPlace = 3; //Down if (ytemp >= shape1.Location.Y && ytemp <= shape1.Location.Y + shape1.Height) edgeCardiPlace = 4; //Left } return edgeCardiPlace; }
private static Point getStartPos(int EdgeCardiPlace, ShapeBase shape, Point TopLeft, Point BottomRight, int stepX, int stepY, Bitmap myCardi) { Point StartPos = new Point(); switch (EdgeCardiPlace) { case 1: StartPos.X = TopLeft.X + stepX; StartPos.Y = TopLeft.Y - myCardi.Height; break; case 2: StartPos.X = BottomRight.X + myCardi.Width; StartPos.Y = BottomRight.Y - (shape.Height - stepY); break; case 3: StartPos.X = BottomRight.X - (shape.Width - stepX); StartPos.Y = BottomRight.Y + myCardi.Height; break; case 4: StartPos.X = TopLeft.X - myCardi.Width; StartPos.Y = TopLeft.Y + stepY; break; } return StartPos; }
private static Point getCardiPos(int EdgeCardiPlace, ShapeBase shape, Point TopLeft, Point BottomRight, int stepX, int stepY, Bitmap myCardi) { Point CardiPos = new Point(); switch (EdgeCardiPlace) { case 1: CardiPos.X = TopLeft.X + stepX - myCardi.Width / 2; CardiPos.Y = TopLeft.Y - myCardi.Height; break; case 2: CardiPos.X = BottomRight.X; CardiPos.Y = BottomRight.Y - (shape.Height - stepY) - myCardi.Height / 2; break; case 3: CardiPos.X = BottomRight.X - (shape.Width - stepX) - myCardi.Width / 2 + 1; CardiPos.Y = BottomRight.Y; break; case 4: CardiPos.X = TopLeft.X - myCardi.Width; CardiPos.Y = TopLeft.Y + stepY - myCardi.Height / 2 + 1; break; } return CardiPos; }
public override void doDoubleClick(PanelDoubleBuffered pn, ShapeBase ns) { }
protected override void OnMouseUp(MouseEventArgs e) { if (isDrawing) { CancelDrawing(); currentShape = null; } if (isSelecting) { isSelecting = false; if (Last_Mouse_Pos.X != -1) { JDrawReversibleRectangle(First_Mouse_Pos, Last_Mouse_Pos); } First_Mouse_Pos.X = -1; First_Mouse_Pos.Y = -1; Last_Mouse_Pos.X = -1; Last_Mouse_Pos.Y = -1; } }
//Panel Event protected override void OnMouseDown(MouseEventArgs e) { //Click vô panel khi đang sửa tên -> dừng sửa if (isNaming) { AffectingShape.endEditName(); return; } if (e.Button == MouseButtons.Right) { CancelDrawing(); return; } if (isDrawing) { if (isDrawEntity) { currentShape = new EntityShape(); ((EntityShape)currentShape).type = DrawingShapeState; } if (isDrawingAtt && AffectingShape != null) { if (AffectingShape is AttributeShape && ((AttributeShape)AffectingShape).isComposite && ((AttributeShape)AffectingShape).type != AttributeType.Derived && DrawingShapeState == AttributeType.Child) { currentShape = new AttributeShape(); ((AttributeShape)currentShape).type = AttributeType.Simple; ((AttributeShape)currentShape).isComposite = false; ((AttributeShape)AffectingShape).addAttribute((AttributeShape)currentShape); } else { if ((AffectingShape is EntityShape || AffectingShape is RelationshipShape) && DrawingShapeState != AttributeType.Child) { currentShape = new AttributeShape(); if (DrawingShapeState == AttributeType.Key) ((AttributeShape)currentShape).allowNull = false; ((AttributeShape)currentShape).type = DrawingShapeState; if (AffectingShape is EntityShape) { ((EntityShape)AffectingShape).addAttribute((AttributeShape)currentShape); } else if (AffectingShape is RelationshipShape) { RelationshipShape relshape = (RelationshipShape)AffectingShape; if ((relshape.cardinalities[0].MaxCardinality != -1 || relshape.cardinalities[1].MaxCardinality != -1) && ((AttributeShape)currentShape).type == AttributeType.Key) { DevExpress.XtraEditors.XtraMessageBox.Show("Relationship couldn't have an Identifier Attribute"); currentShape = null; } else relshape.addAttribute((AttributeShape)currentShape); } } } } if ((isDrawEntity || isDrawSubType || (isDrawingAtt && AffectingShape != null)) && currentShape != null) { Point mouse = this.PointToClient(Control.MousePosition); currentShape.CenterPoint = mouse; this.Controls.Add(currentShape); shapeDoubleClick(currentShape, e); } else { //đang vẽ sub type if (isDrawSubType && AffectingShape != null) { if (AffectingShape is EntityShape) { CreateSubTypeConnector((EntityShape)AffectingShape); } } //đang vẽ relationship (không vẽ shape, vẽ đoạn nối 2 entity) //phải click lên control, click lên panel thì sai, không cho vẽ nữa CancelDrawing(); } } //click panel mà không phải là đang drawing else { //mark flag để vẽ rubber band isSelecting = true; First_Mouse_Pos.X = e.X; First_Mouse_Pos.Y = e.Y; Last_Mouse_Pos.X = -1; Last_Mouse_Pos.Y = -1; } if (!isNaming) { AffectingShape = null; this.Invalidate(); } }
//Delete and Undo public bool deleteAffectingShape() { if (AffectingShape != null && !isDrawing && !isNaming && !isMoving && !isResizing && !isSelecting) { if (AffectingShape.GetType().Name == "EntityShape") { if (DevExpress.XtraEditors.XtraMessageBox.Show("Are you sure want to delete " + AffectingShape.sName + " Entity ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { saveUndoList(); AffectingShape.Dispose(); AffectingShape = null; this.Invalidate(); } } else { saveUndoList(); AffectingShape.Dispose(); AffectingShape = null; this.Invalidate(); } return true; } return false; }
//Shape (User control) Event void shapeMouseDown(object sender, MouseEventArgs e) { if (isNaming) { AffectingShape.endEditName(); return; } ShapeBase currentCtrl = (ShapeBase)sender; //Đang vẽ relationship if (e.Button == MouseButtons.Left && isDrawing && isDrawRelationship) { //Nếu chưa có điểm đầu if (First_Mouse_Click == false) { if (currentCtrl is EntityShape) { First_Mouse_Click = true; //Tracking First position for Drawing Virtual Line First_Mouse_Pos.X = e.X + currentCtrl.Location.X; First_Mouse_Pos.Y = e.Y + currentCtrl.Location.Y; FirstEntity = (EntityShape)currentCtrl; if (degreeOfRelationship == 1) { //Create Unary Relationship CreateUnaryRelationship(); CancelDrawing(); return; } } else CancelDrawing(); } else { //Ðã có điểm đầu, click lần thứ 2 if (Second_Mouse_Click == false) { if (currentCtrl is EntityShape) { Second_Mouse_Click = true; SecondEntity = (EntityShape)currentCtrl; if (degreeOfRelationship == 2) { //Create Binary Relationship CreateBinaryRelationship(); CancelDrawing(); return; } } else CancelDrawing(); } else { //Ðã có điểm thứ 1 và 2, click lần 3 if (Third_Mouse_Click == false) { if (currentCtrl is EntityShape) { Third_Mouse_Click = true; ThirdEntity = (EntityShape)currentCtrl; if (degreeOfRelationship == 3) { //Create Ternary Relationship CreateTernaryRelationship(); CancelDrawing(); return; } } else CancelDrawing(); } else CancelDrawing(); } } }//End Ðang vẽ relationship //Lưu vết lại Shape đang được tác động (Click) //và set cho shape đó nằm trên cùng AffectingShape = currentCtrl; this.Controls.SetChildIndex(AffectingShape, 0); // click trái: Resizing hoặc Moving hoặc Selecting // Nếu chuột ở mép (border) của Shape thì là resizing // bình thường thì moving if (e.Button == MouseButtons.Left && !isDrawing) { Last_Mouse_Pos.X = e.X; Last_Mouse_Pos.Y = e.Y; if ((e.X + 5) > currentCtrl.Width || (e.Y + 5) > currentCtrl.Height) isResizing = true; else isMoving = true; } // click phải: hiện context menu else if (e.Button == MouseButtons.Right) { ContextMenuStrip ctmn = getContexMenuForControl(currentCtrl); ctmn.Show(currentCtrl, new Point(e.X, e.Y)); } this.Invalidate(); }
//Generate Context Menu private ContextMenuStrip getContexMenuForControl(ShapeBase control) { ContextMenuStrip ctmn = new ContextMenuStrip(); ToolStripMenuItem del = new ToolStripMenuItem("Delete"); del.Click += new EventHandler(del_Click); if (control is EntityShape) { ToolStripMenuItem StrongEntity = new ToolStripMenuItem("Strong Entity Type"); StrongEntity.Click += new EventHandler(Change_Type_Click); ToolStripMenuItem WeakEntity = new ToolStripMenuItem("Weak Entity Type"); WeakEntity.Click += new EventHandler(Change_Type_Click); ctmn.Items.Add(StrongEntity); ctmn.Items.Add(WeakEntity); ctmn.Items.Add(new ToolStripSeparator()); ctmn.Items.Add(del); } if (control is RelationshipShape) { ToolStripMenuItem NormalRelation = new ToolStripMenuItem("Normal Relationship"); NormalRelation.Click += new EventHandler(Change_Type_Click); ToolStripMenuItem IdRelation = new ToolStripMenuItem("Identifier Relationship"); IdRelation.Click += new EventHandler(Change_Type_Click); ToolStripMenuItem AssoRelation = new ToolStripMenuItem("Associative Entity"); AssoRelation.Click += new EventHandler(Change_Type_Click); ctmn.Items.Add(NormalRelation); ctmn.Items.Add(IdRelation); ctmn.Items.Add(AssoRelation); ctmn.Items.Add(new ToolStripSeparator()); ToolStripMenuItem editRel = new ToolStripMenuItem("Properties"); editRel.Click += new EventHandler(editRel_Click); ctmn.Items.Add(del); ctmn.Items.Add(editRel); } if (control is AttributeShape && ((AttributeShape)control).isComposite) { ToolStripMenuItem KeyAtt = new ToolStripMenuItem("Key Attribute"); KeyAtt.Click += new EventHandler(Change_Type_Click); ToolStripMenuItem SimpleAtt = new ToolStripMenuItem("Simple Attribute"); SimpleAtt.Click += new EventHandler(Change_Type_Click); ToolStripMenuItem MultiAtt = new ToolStripMenuItem("Multi-Valued Attribute"); MultiAtt.Click += new EventHandler(Change_Type_Click); ctmn.Items.Add(SimpleAtt); ctmn.Items.Add(KeyAtt); ctmn.Items.Add(MultiAtt); if (((AttributeShape)control).attributeChilds.Count == 0) { ToolStripMenuItem DerivedAtt = new ToolStripMenuItem("Derived Attribute"); DerivedAtt.Click += new EventHandler(Change_Type_Click); ctmn.Items.Add(DerivedAtt); } ctmn.Items.Add(new ToolStripSeparator()); ctmn.Items.Add(del); } if (control is SubTypeConnector) { ToolStripMenuItem Total = new ToolStripMenuItem("Total Specialization"); Total.Click += new EventHandler(Change_Type_Click); ToolStripMenuItem Partial = new ToolStripMenuItem("Partial Specialization"); Partial.Click += new EventHandler(Change_Type_Click); ToolStripMenuItem Disjoint = new ToolStripMenuItem("Disjoint Constraint"); Disjoint.Click += new EventHandler(Change_Type_Click); ToolStripMenuItem Overlap = new ToolStripMenuItem("Overlap Constraint"); Overlap.Click += new EventHandler(Change_Type_Click); ctmn.Items.Add(Total); ctmn.Items.Add(Partial); ctmn.Items.Add(Disjoint); ctmn.Items.Add(Overlap); ctmn.Items.Add(new ToolStripSeparator()); ToolStripMenuItem editSub = new ToolStripMenuItem("Properties"); editSub.Click += new EventHandler(editSub_Click); ctmn.Items.Add(del); ctmn.Items.Add(editSub); } return ctmn; }
public virtual void doDoubleClick(PanelDoubleBuffered pn, ShapeBase ns) { parentPanel = pn; namingShape = ns; if (parentPanel.isNaming) { parentPanel.AffectingShape.endEditName(); return; } parentPanel.AffectingShape = ns; parentPanel.isNaming = true; txtName = new TextBox(); //cho class con định vị xong rồi add lên shape theo vị trí đó dinhviTextBox(txtName); txtName.Text = sName; //hiện tên cũ sName = ""; this.Invalidate(); this.Controls.Add(txtName); txtName.SelectAll(); txtName.Focus(); txtName.KeyPress += new System.Windows.Forms.KeyPressEventHandler(txtName_KeyPress); //Data type cho attribute if (this is AttributeShape) { if (((AttributeShape)this).attributeChilds.Count == 0) { ucDataDescription = new DataDescription(); ucDataDescription.Location = new Point(this.Location.X + this.Width + 5, this.Location.Y); parentPanel.Controls.Add(ucDataDescription); //Cho USC nằm lên trên. parentPanel.Controls.SetChildIndex(ucDataDescription, 0); if (((AttributeShape)this).type == AttributeType.Key) { ucDataDescription.chkNull.Enabled = false; } if (((AttributeShape)this).dataType != null) { ucDataDescription.cboDataType.SelectedItem = ((AttributeShape)this).dataType; } if (((AttributeShape)this).dataLength != 0) { ucDataDescription.txtLength.Text = ((AttributeShape)this).dataLength.ToString(); } ucDataDescription.chkNull.Checked = ((AttributeShape)this).allowNull; if (((AttributeShape)this).description != "") { ucDataDescription.txtDescription.Text = ((AttributeShape)this).description; } ucDataDescription.btnOK.Click += new EventHandler(btnOK_Click); } } }