kivio

tool_connector.cpp

00001 /*
00002  * Kivio - Visual Modelling and Flowcharting
00003  * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 #include "tool_connector.h"
00020 
00021 #include <qcursor.h>
00022 #include <kdebug.h>
00023 #include <kiconloader.h>
00024 #include <kstandarddirs.h>
00025 #include <KoPoint.h>
00026 #include <KoZoomHandler.h>
00027 #include <kactionclasses.h>
00028 #include <klocale.h>
00029 
00030 #include "kivio_view.h"
00031 #include "kivio_canvas.h"
00032 #include "kivio_page.h"
00033 #include "kivio_doc.h"
00034 #include "kivio_factory.h"
00035 
00036 #include "kivio_stencil_spawner_set.h"
00037 #include "kivio_stencil_spawner.h"
00038 #include "kivio_custom_drag_data.h"
00039 #include "kivio_layer.h"
00040 #include "kivio_point.h"
00041 #include "kivio_stencil.h"
00042 #include "sml_connector.h"
00043 
00044 SMLConnector::SMLConnector( KivioView* view )
00045  : Kivio::MouseTool(view, "SMLConnector")
00046 {
00047   m_connectorAction = new KToggleAction(i18n("Polyline Connector"), "", 0,
00048     actionCollection(), "sml_connector");
00049   connect(m_connectorAction, SIGNAL(toggled(bool)), this, SLOT(setActivated(bool)));
00050 
00051   m_mode = stmNone;
00052 
00053   m_pConnectorCursor1 = new QCursor(BarIcon("kivio_connector_cursor1",KivioFactory::global()),2,2);
00054   m_pConnectorCursor2 = new QCursor(BarIcon("kivio_connector_cursor2",KivioFactory::global()),2,2);
00055 }
00056 
00057 SMLConnector::~SMLConnector()
00058 {
00059   delete m_pConnectorCursor1;
00060   delete m_pConnectorCursor2;
00061 }
00062 
00063 
00070 bool SMLConnector::processEvent( QEvent* e )
00071 {
00072     switch (e->type())
00073     {
00074     case QEvent::MouseButtonPress:
00075         mousePress( (QMouseEvent*)e );
00076         return true;
00077         break;
00078 
00079     case QEvent::MouseButtonRelease:
00080         mouseRelease( (QMouseEvent*)e );
00081         return true;
00082         break;
00083 
00084     case QEvent::MouseMove:
00085         mouseMove( (QMouseEvent*)e );
00086         return true;
00087         break;
00088 
00089     default:
00090       break;
00091     }
00092 
00093     return false;
00094 }
00095 
00096 void SMLConnector::setActivated(bool a)
00097 {
00098   m_connectorAction->setChecked(a);
00099   
00100   if(a) {
00101     kdDebug(43000) << "SMLConnector activate" << endl;
00102     view()->canvasWidget()->setCursor(*m_pConnectorCursor1);
00103     m_mode = stmNone;
00104     m_pStencil = 0;
00105     m_pDragData = 0;
00106     emit activated(this);
00107   } else {
00108     m_pStencil = 0;
00109     delete m_pDragData;
00110     m_pDragData = 0;
00111   }
00112 }
00113 
00114 void SMLConnector::connector(QRect)
00115 {
00116     if (!m_pStencil)
00117       return;
00118 
00119     delete m_pDragData;
00120     m_pDragData = 0;
00121 
00122     KivioDoc* doc = view()->doc();
00123     KivioPage* page = view()->activePage();
00124 
00125     if (m_pStencil->w() < 3.0 && m_pStencil->h() < 3.0) {
00126         page->unselectAllStencils();
00127         page->selectStencil(m_pStencil);
00128         page->deleteSelectedStencils();
00129         m_pStencil = 0;
00130         doc->updateView(page);
00131         return;
00132     }
00133 
00134     m_pStencil->searchForConnections(page, view()->zoomHandler()->unzoomItY(4));
00135     doc->updateView(page);
00136 }
00137 
00138 void SMLConnector::mousePress( QMouseEvent *e )
00139 {
00140   if(e->button() == RightButton)
00141   {
00142     return;
00143   }
00144   if( startRubberBanding( e ) )
00145   {
00146     m_mode = stmDrawRubber;
00147   }
00148 }
00149 
00150 
00154 bool SMLConnector::startRubberBanding( QMouseEvent *e )
00155 {
00156   KivioDoc* doc = view()->doc();
00157   KivioPage* pPage = view()->activePage();
00158   KivioCanvas* canvas = view()->canvasWidget();
00159 
00160   startPoint = canvas->snapToGrid(canvas->mapFromScreen( e->pos() ));
00161 
00162   // Create the stencil
00163     KivioStencilSpawner* ss = doc->findInternalStencilSpawner("SML Connector");
00164 
00165   if (!ss) {
00166     kdDebug(43000) << "SMLTool: Failed to find StencilSpawner!" << endl;
00167     return false;
00168   }
00169 
00170   startPoint = canvas->snapToGrid(canvas->mapFromScreen( e->pos() ));
00171 
00172   // Create the stencil
00173   m_pStencil = (KivioSMLConnector*)ss->newStencil("basic_line");
00174   m_pStencil->setTextFont(doc->defaultFont());
00175 
00176   // Unselect everything, add the stencil to the page, and select it
00177   pPage->unselectAllStencils();
00178   pPage->addStencil(m_pStencil);
00179   pPage->selectStencil(m_pStencil);
00180 
00181   // Get drag info ready
00182   m_pDragData = new KivioCustomDragData();
00183   m_pDragData->page = pPage;
00184   m_pDragData->x = startPoint.x();
00185   m_pDragData->y = startPoint.y();
00186   m_pDragData->id = kctCustom + 2;
00187 
00188   m_pStencil->setStartPoint(startPoint.x() + 10.0f, startPoint.y() + 10.0f);
00189   m_pStencil->setEndPoint(startPoint.x(), startPoint.y());
00190   m_pStencil->customDrag(m_pDragData);
00191 
00192 
00193   canvas->repaint();
00194   canvas->setCursor(*m_pConnectorCursor2);
00195   return true;
00196 }
00197 
00198 void SMLConnector::mouseMove( QMouseEvent * e )
00199 {
00200     switch( m_mode )
00201     {
00202         case stmDrawRubber:
00203             continueRubberBanding(e);
00204             break;
00205 
00206         default:
00207             break;
00208     }
00209 }
00210 
00211 void SMLConnector::continueRubberBanding( QMouseEvent *e )
00212 {
00213   KivioCanvas* canvas = view()->canvasWidget();
00214   KoPoint endPoint = canvas->mapFromScreen( e->pos() );
00215   endPoint = canvas->snapToGrid(endPoint);
00216 
00217   m_pStencil->setStartPoint(endPoint.x(), endPoint.y());
00218 
00219 
00220   m_pDragData->x = endPoint.x();
00221   m_pDragData->y = endPoint.y();
00222   m_pDragData->id = kctCustom + 1;
00223   m_pStencil->customDrag(m_pDragData);
00224 
00225   m_pStencil->updateGeometry();
00226   canvas->repaint();
00227 }
00228 
00229 void SMLConnector::mouseRelease( QMouseEvent *e )
00230 {
00231   switch( m_mode )
00232   {
00233     case stmDrawRubber:
00234       endRubberBanding(e);
00235       break;
00236   }
00237 
00238   view()->canvasWidget()->setCursor(*m_pConnectorCursor1);
00239   m_mode = stmNone;
00240 }
00241 
00242 void SMLConnector::endRubberBanding(QMouseEvent *)
00243 {
00244   connector(view()->canvasWidget()->rect());
00245   m_pStencil = 0;
00246 }
00247 
00248 #include "tool_connector.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys