File: hdConnectionCreationTool.cpp

package info (click to toggle)
pgadmin3 1.20.0~beta2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 73,704 kB
  • ctags: 18,591
  • sloc: cpp: 193,786; ansic: 18,736; sh: 5,154; pascal: 1,120; yacc: 927; makefile: 516; lex: 421; xml: 126; perl: 40
file content (140 lines) | stat: -rw-r--r-- 4,236 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// hdConnectionCreationTool.cpp - A Tool that allow to create a connection figure between two figures
//
//////////////////////////////////////////////////////////////////////////

#include "pgAdmin3.h"

// wxWindows headers
#include <wx/wx.h>

// App headers
#include "hotdraw/tools/hdConnectionCreationTool.h"
#include "hotdraw/tools/hdSelectionTool.h"
#include "hotdraw/main/hdDrawingView.h"

hdConnectionCreationTool::hdConnectionCreationTool(hdDrawingView *view, hdLineConnection *figure):
	hdAbstractTool(view)
{
	toolConnection = figure;
	toolConnection->disconnectStart();
	toolConnection->disconnectEnd();
	handle = NULL;
	numClicks = 0;
	dragged = false;
}

hdConnectionCreationTool::~hdConnectionCreationTool()
{
}

void hdConnectionCreationTool::mouseDrag(hdMouseEvent &event)
{
	if(handle && event.LeftIsDown())
	{
		dragged = true;
		handle->invokeStep(event, event.getView());
	}
}

void hdConnectionCreationTool::mouseDown(hdMouseEvent &event)
{
	hdAbstractTool::mouseDown(event);
	if(event.LeftDown())
	{
		numClicks++;
		int x = event.getScrolledPosX(), y = event.getScrolledPosY();
		hdDrawingView *view = event.getView();
		hdIFigure *figure = view->getDrawing()->findFigure(view->getIdx(), x, y);

		if(figure)
		{
			if(numClicks == 1) //first mouse click to select start/end figure
			{
				toolConnection->setEndPoint(view->getIdx(), hdPoint(x, y));
				toolConnection->setStartPoint(view->getIdx(), hdPoint(x, y));
				toolConnection->connectStart(figure->connectorAt(view->getIdx(), x, y));
				toolConnection->updateConnection(view->getIdx());
				view->editor()->addDiagramFigure(view->getIdx(), toolConnection);
				view->getDrawing()->clearSelection();
				view->getDrawing()->addToSelection(toolConnection);
				handle = toolConnection->getEndHandle();
			}
			else if(numClicks > 1) //second mouse click to select end figure only
			{
				toolConnection->setEndPoint(view->getIdx(), hdPoint(x, y));
				toolConnection->updateConnection(event.getView()->getIdx());
			}
		}
		else
		{
			event.getView()->setTool(new hdSelectionTool(event.getView()));
		}
	}
}

void hdConnectionCreationTool::mouseUp(hdMouseEvent &event)
{
	if(event.LeftUp())
	{
		//Hack to allow one click and drag creation of connections
		if(handle)
		{
			if(!dragged && numClicks == 1) //mouse haven't be dragged and is first click of mouse at this tool
			{
				toolConnection->setEndPoint(event.getView()->getIdx(), event.GetPosition());
				toolConnection->updateConnection(event.getView()->getIdx());
			}
			else
			{
				handle->invokeEnd(event, event.getView());
			}
		}

		if((toolConnection->getEndConnector() == NULL && numClicks > 1) || (toolConnection->getEndConnector() == NULL && dragged)) //Delete connection only if a second click a connection figures isn't found
		{
			//check if exists at drawing because automatically integrity check
			if(event.getView()->getDrawing()->includes(toolConnection))
				event.getView()->getDrawing()->remove(toolConnection);
			event.getView()->getDrawing()->clearSelection();
			toolConnection->disconnectStart();
			toolConnection->disconnectEnd();
			event.getView()->editor()->deleteModelFigure(toolConnection);
		}
	}
	if(dragged || numClicks > 1) //if drag to select a figure or is second or higher click (to select end figure) then this tool ends.
	{
		event.getView()->setTool(new hdSelectionTool(event.getView()));
	}
	else if(!dragged && numClicks == 1) //if not dragged before and is first click then allow to select end, disconnecting it
	{
		toolConnection->disconnectEnd();
	}

}

void hdConnectionCreationTool::mouseMove(hdMouseEvent &event)
{
	int x = event.GetPosition().x, y = event.GetPosition().y;
	hdDrawingView *view = event.getView();
	hdIFigure *figure = view->getDrawing()->findFigure(view->getIdx(), x, y);
	if(figure)
	{
		view->SetCursor(wxCursor(wxCURSOR_PENCIL));
	}
	else
	{
		view->SetCursor(wxCursor(wxCURSOR_CROSS));
	}

	if(toolConnection && handle && numClicks > 0)
	{
		handle->invokeStep(event, view);
	}
}