kiviosmlconnector/tool_connector.cpp
1/*
2 * Kivio - Visual Modelling and Flowcharting
3 * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19#include "tool_connector.h"
20
21#include <tqcursor.h>
22#include <kdebug.h>
23#include <kiconloader.h>
24#include <kstandarddirs.h>
25#include <KoPoint.h>
26#include <KoZoomHandler.h>
27#include <tdeactionclasses.h>
28#include <tdelocale.h>
29
30#include "kivio_view.h"
31#include "kivio_canvas.h"
32#include "kivio_page.h"
33#include "kivio_doc.h"
34#include "kivio_factory.h"
35
36#include "kivio_stencil_spawner_set.h"
37#include "kivio_stencil_spawner.h"
38#include "kivio_custom_drag_data.h"
39#include "kivio_layer.h"
40#include "kivio_point.h"
41#include "kivio_stencil.h"
42#include "sml_connector.h"
43
44SMLConnector::SMLConnector( KivioView* view )
45 : Kivio::MouseTool(view, "SMLConnector")
46{
47 m_connectorAction = new TDEToggleAction(i18n("Polyline Connector"), "", 0,
48 actionCollection(), "sml_connector");
49 connect(m_connectorAction, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(setActivated(bool)));
50
51 m_mode = stmNone;
52
53 m_pConnectorCursor1 = new TQCursor(BarIcon("kivio_connector_cursor1",KivioFactory::global()),2,2);
54 m_pConnectorCursor2 = new TQCursor(BarIcon("kivio_connector_cursor2",KivioFactory::global()),2,2);
55}
56
57SMLConnector::~SMLConnector()
58{
59 delete m_pConnectorCursor1;
60 delete m_pConnectorCursor2;
61}
62
63
70bool SMLConnector::processEvent( TQEvent* e )
71{
72 switch (e->type())
73 {
74 case TQEvent::MouseButtonPress:
75 mousePress( (TQMouseEvent*)e );
76 return true;
77 break;
78
79 case TQEvent::MouseButtonRelease:
80 mouseRelease( (TQMouseEvent*)e );
81 return true;
82 break;
83
84 case TQEvent::MouseMove:
85 mouseMove( (TQMouseEvent*)e );
86 return true;
87 break;
88
89 default:
90 break;
91 }
92
93 return false;
94}
95
96void SMLConnector::setActivated(bool a)
97{
98 m_connectorAction->setChecked(a);
99
100 if(a) {
101 kdDebug(43000) << "SMLConnector activate" << endl;
102 view()->canvasWidget()->setCursor(*m_pConnectorCursor1);
103 m_mode = stmNone;
104 m_pStencil = 0;
105 m_pDragData = 0;
106 emit activated(this);
107 } else {
108 m_pStencil = 0;
109 delete m_pDragData;
110 m_pDragData = 0;
111 }
112}
113
114void SMLConnector::connector(TQRect)
115{
116 if (!m_pStencil)
117 return;
118
119 delete m_pDragData;
120 m_pDragData = 0;
121
122 KivioDoc* doc = view()->doc();
123 KivioPage* page = view()->activePage();
124
125 if (m_pStencil->w() < 3.0 && m_pStencil->h() < 3.0) {
126 page->unselectAllStencils();
127 page->selectStencil(m_pStencil);
128 page->deleteSelectedStencils();
129 m_pStencil = 0;
130 doc->updateView(page);
131 return;
132 }
133
134 m_pStencil->searchForConnections(page, view()->zoomHandler()->unzoomItY(4));
135 doc->updateView(page);
136}
137
138void SMLConnector::mousePress( TQMouseEvent *e )
139{
140 if(e->button() == RightButton)
141 {
142 return;
143 }
144 if( startRubberBanding( e ) )
145 {
146 m_mode = stmDrawRubber;
147 }
148}
149
150
154bool SMLConnector::startRubberBanding( TQMouseEvent *e )
155{
156 KivioDoc* doc = view()->doc();
157 KivioPage* pPage = view()->activePage();
158 KivioCanvas* canvas = view()->canvasWidget();
159
160 startPoint = canvas->snapToGrid(canvas->mapFromScreen( e->pos() ));
161
162 // Create the stencil
163 KivioStencilSpawner* ss = doc->findInternalStencilSpawner("SML Connector");
164
165 if (!ss) {
166 kdDebug(43000) << "SMLTool: Failed to find StencilSpawner!" << endl;
167 return false;
168 }
169
170 startPoint = canvas->snapToGrid(canvas->mapFromScreen( e->pos() ));
171
172 // Create the stencil
173 m_pStencil = (KivioSMLConnector*)ss->newStencil("basic_line");
174 m_pStencil->setTextFont(doc->defaultFont());
175
176 // Unselect everything, add the stencil to the page, and select it
177 pPage->unselectAllStencils();
178 pPage->addStencil(m_pStencil);
179 pPage->selectStencil(m_pStencil);
180
181 // Get drag info ready
182 m_pDragData = new KivioCustomDragData();
183 m_pDragData->page = pPage;
184 m_pDragData->x = startPoint.x();
185 m_pDragData->y = startPoint.y();
186 m_pDragData->id = kctCustom + 2;
187
188 m_pStencil->setStartPoint(startPoint.x() + 10.0f, startPoint.y() + 10.0f);
189 m_pStencil->setEndPoint(startPoint.x(), startPoint.y());
190 m_pStencil->customDrag(m_pDragData);
191
192
193 canvas->repaint();
194 canvas->setCursor(*m_pConnectorCursor2);
195 return true;
196}
197
198void SMLConnector::mouseMove( TQMouseEvent * e )
199{
200 switch( m_mode )
201 {
202 case stmDrawRubber:
203 continueRubberBanding(e);
204 break;
205
206 default:
207 break;
208 }
209}
210
211void SMLConnector::continueRubberBanding( TQMouseEvent *e )
212{
213 KivioCanvas* canvas = view()->canvasWidget();
214 KoPoint endPoint = canvas->mapFromScreen( e->pos() );
215 endPoint = canvas->snapToGrid(endPoint);
216
217 m_pStencil->setStartPoint(endPoint.x(), endPoint.y());
218
219
220 m_pDragData->x = endPoint.x();
221 m_pDragData->y = endPoint.y();
222 m_pDragData->id = kctCustom + 1;
223 m_pStencil->customDrag(m_pDragData);
224
225 m_pStencil->updateGeometry();
226 canvas->repaint();
227}
228
229void SMLConnector::mouseRelease( TQMouseEvent *e )
230{
231 switch( m_mode )
232 {
233 case stmDrawRubber:
234 endRubberBanding(e);
235 break;
236 }
237
238 view()->canvasWidget()->setCursor(*m_pConnectorCursor1);
239 m_mode = stmNone;
240}
241
242void SMLConnector::endRubberBanding(TQMouseEvent *)
243{
244 connector(view()->canvasWidget()->rect());
245 m_pStencil = 0;
246}
247
248#include "tool_connector.moc"
Baseclass for mouse tools (like eg the select tool)