PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/koffice-trinity-20100511/kivio/plugins/kiviosmlconnector/sml_connector/sml_connector.cpp

#
C++ | 434 lines | 285 code | 83 blank | 66 comment | 20 complexity | 81f36df74f1e00cd8c1e4e7160a083a7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1
  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 <stdio.h>
  20. #include <math.h>
  21. #include "sml_connector.h"
  22. #include "kivio_common.h"
  23. #include "kivio_connector_target.h"
  24. #include "kivio_layer.h"
  25. #include "kivio_line_style.h"
  26. #include "kivio_page.h"
  27. #include "kivio_painter.h"
  28. #include "kivio_stencil.h"
  29. #include "kivio_stencil_spawner.h"
  30. #include "kivio_stencil_spawner_info.h"
  31. #include "kivio_stencil_spawner_set.h"
  32. #include "kivio_text_style.h"
  33. #include "kivio_shape.h"
  34. #include "kivio_shape_data.h"
  35. #include "kivio_point.h"
  36. #include "tkmath.h"
  37. #include <qcolor.h>
  38. #include <qpixmap.h>
  39. #include <kdebug.h>
  40. #include <KoZoomHandler.h>
  41. #include <kiconloader.h>
  42. static KivioStencilSpawnerInfo sinfo = KivioStencilSpawnerInfo( "Ian Reinhart Geiser", "SML Connector", "SML Connector", "SML Based Connector", "0.1", "http://localhost/", "", "off" );
  43. #include <kgenericfactory.h>
  44. K_EXPORT_COMPONENT_FACTORY( sml_connector, KGenericFactory<KivioSMLConnectorFactory>( "KivioSMLConnectorFactory" ) )
  45. KivioSMLConnectorFactory::KivioSMLConnectorFactory( QObject *parent, const char* name, const QStringList& args ) :
  46. KivioStencilFactory( parent, name, args )
  47. {
  48. kdDebug(43000) << "new sml stencil factory: " << endl;
  49. }
  50. KivioStencil *KivioSMLConnectorFactory::NewStencil( const QString& name )
  51. {
  52. return new KivioSMLConnector( name );
  53. }
  54. KivioStencil *KivioSMLConnectorFactory::NewStencil()
  55. {
  56. return new KivioSMLConnector( "basic_line" );
  57. }
  58. QPixmap *KivioSMLConnectorFactory::GetIcon()
  59. {
  60. return & BarIcon( "SML_conn" );
  61. }
  62. KivioStencilSpawnerInfo *KivioSMLConnectorFactory::GetSpawnerInfo()
  63. {
  64. return & sinfo;
  65. }
  66. KivioSMLConnector::KivioSMLConnector( const QString &name )
  67. : Kivio1DStencil(), m_name( name )
  68. {
  69. m_pStart->setPosition( 0.0f, 0.0f, false );
  70. m_pEnd->setPosition( 72.0f, 72.0f, false );
  71. m_startAH = new KivioArrowHead();
  72. m_endAH = new KivioArrowHead();
  73. m_needsWidth = false;
  74. m_needsText = true;
  75. m_pCanProtect->clearBit( kpAspect );
  76. m_pCanProtect->clearBit( kpWidth );
  77. m_pCanProtect->clearBit( kpHeight );
  78. m_pCanProtect->clearBit( kpX );
  79. m_pCanProtect->clearBit( kpY );
  80. // This is a stencil of type connector
  81. setType( kstConnector );
  82. // We only have one shape, an open path.
  83. //m_shape = new KivioShape();
  84. //m_shape->shapeData() ->setShapeType( KivioShapeData::kstOpenPath );
  85. loadPath( name );
  86. }
  87. KivioSMLConnector::~KivioSMLConnector()
  88. {
  89. delete m_startAH;
  90. delete m_endAH;
  91. }
  92. void KivioSMLConnector::setStartPoint( double x, double y )
  93. {
  94. m_pStart->setPosition( x, y, false );
  95. m_pStart->disconnect();
  96. if ( m_needsText )
  97. {
  98. m_pTextConn->setPosition( ( m_pEnd->x() + m_pStart->x() ) / 2.0f,
  99. ( m_pEnd->y() + m_pStart->y() ) / 2.0f,
  100. false );
  101. }
  102. // Set list end point
  103. m_pConnectorPoints ->first() ->setX( x );
  104. m_pConnectorPoints ->first() ->setY( y );
  105. }
  106. void KivioSMLConnector::setEndPoint( double x, double y )
  107. {
  108. m_pEnd->setPosition( x, y, false );
  109. m_pEnd->disconnect();
  110. m_pConnectorPoints ->last() ->setX( x );
  111. m_pConnectorPoints ->last() ->setY( y );
  112. }
  113. KivioCollisionType KivioSMLConnector::checkForCollision( KivioPoint *p, double threshold )
  114. {
  115. const double end_thresh = 4.0f;
  116. double px = p->x();
  117. double py = p->y();
  118. KivioConnectorPoint *pPoint;
  119. int i = kctCustom + 1;
  120. pPoint = m_pConnectorPoints->first();
  121. while ( pPoint )
  122. {
  123. if ( px >= pPoint->x() - end_thresh &&
  124. px <= pPoint->x() + end_thresh &&
  125. py >= pPoint->y() - end_thresh &&
  126. py <= pPoint->y() + end_thresh )
  127. {
  128. return ( KivioCollisionType ) i;
  129. }
  130. i++;
  131. pPoint = m_pConnectorPoints->next();
  132. }
  133. if ( collisionLine( m_pStart->x(), m_pStart->y(),
  134. m_pEnd->x(), m_pEnd->y(),
  135. px, py,
  136. threshold ) )
  137. {
  138. return kctBody;
  139. }
  140. return kctNone;
  141. }
  142. KivioStencil *KivioSMLConnector::duplicate()
  143. {
  144. KivioSMLConnector * pStencil = new KivioSMLConnector( m_name );
  145. // TODO: Copy internal pointlist information?
  146. // Copy the arrow head information
  147. pStencil->setStartAHType( m_startAH->type() );
  148. pStencil->setStartAHWidth( m_startAH->width() );
  149. pStencil->setStartAHLength( m_startAH->length() );
  150. pStencil->setEndAHType( m_endAH->type() );
  151. pStencil->setEndAHWidth( m_endAH->width() );
  152. pStencil->setEndAHLength( m_endAH->length() );
  153. *( pStencil->protection() ) = *m_pProtection;
  154. *( pStencil->canProtect() ) = *m_pCanProtect;
  155. return pStencil;
  156. }
  157. /*
  158. void KivioSMLConnector::drawOpenPath( KivioShape *pShape, KivioIntraStencilData *pData )
  159. {
  160. KivioPainter * painter;
  161. KivioShapeData *pShapeData = pShape->shapeData();
  162. KivioPoint *pPoint, *pNewPoint;
  163. QPtrList <KivioPoint> *pPointList = pShapeData->pointList();
  164. QPtrList <KivioPoint> *pNewPoints = new QPtrList<KivioPoint>;
  165. pNewPoints->setAutoDelete( true );
  166. pPoint = pPointList->first();
  167. while ( pPoint )
  168. {
  169. pNewPoint = new KivioPoint( pData->zoomHandler->zoomItX( pPoint->x() ),
  170. pData->zoomHandler->zoomItY( pPoint->y() ),
  171. pPoint->pointType() );
  172. pNewPoints->append( pNewPoint );
  173. pPoint = pPointList->next();
  174. }
  175. painter = pData->painter;
  176. double lineWidth = pShapeData->lineStyle() ->width();
  177. painter->setLineWidth( pData->zoomHandler->zoomItY( lineWidth ) );
  178. painter->setFGColor( pShapeData->lineStyle() ->color() );
  179. painter->drawOpenPath( pNewPoints );
  180. delete pNewPoints;
  181. }
  182. */
  183. void KivioSMLConnector::paint( KivioIntraStencilData *pData )
  184. {
  185. KivioPainter * painter = pData->painter;
  186. KoZoomHandler* zoomHandler = pData->zoomHandler;
  187. double x1, y1, x2, y2;
  188. double vecX, vecY;
  189. double len;
  190. painter->setFGColor( m_pLineStyle->color() );
  191. painter->setLineWidth( zoomHandler->zoomItY( m_pLineStyle->width() ) );
  192. x1 = zoomHandler->zoomItX( m_pStart->x() );
  193. x2 = zoomHandler->zoomItX( m_pEnd->x() );
  194. y1 = zoomHandler->zoomItY( m_pStart->y() );
  195. y2 = zoomHandler->zoomItY( m_pEnd->y() );
  196. // Calculate the direction vector from start -> end
  197. vecX = m_pEnd->x() - m_pStart->x();
  198. vecY = m_pEnd->y() - m_pStart->y();
  199. // Normalize the vector
  200. len = sqrt( vecX * vecX + vecY * vecY );
  201. if ( len )
  202. {
  203. vecX /= len;
  204. vecY /= len;
  205. // Move the endpoints by the cuts
  206. x1 += vecX * zoomHandler->zoomItX( m_startAH->cut() );
  207. y1 += vecY * zoomHandler->zoomItY( m_startAH->cut() );
  208. x2 -= vecX * zoomHandler->zoomItX( m_endAH->cut() );
  209. y2 -= vecY * zoomHandler->zoomItY( m_endAH->cut() );
  210. }
  211. // Build shape up from m_pConnectorPoints
  212. QPtrList <KivioPoint> *pNewPoints = new QPtrList<KivioPoint>;
  213. pNewPoints->setAutoDelete( true );
  214. pNewPoints->append(new KivioPoint( zoomHandler->zoomItX( m_pStart->x() ),
  215. zoomHandler->zoomItY( m_pStart->y() )));
  216. KivioConnectorPoint *pPoint = m_PointList.first();
  217. while ( pPoint )
  218. {
  219. KivioPoint *pNewPoint = new KivioPoint( zoomHandler->zoomItX( pPoint->x() ),
  220. zoomHandler->zoomItY( pPoint->y() ));
  221. pNewPoints->append( pNewPoint );
  222. pPoint = m_PointList.next();
  223. }
  224. pNewPoints->append(new KivioPoint( zoomHandler->zoomItX( m_pEnd->x() ),
  225. zoomHandler->zoomItY( m_pEnd->y() )));
  226. painter = pData->painter;
  227. double lineWidth = m_pLineStyle ->width();
  228. painter->setLineWidth( zoomHandler->zoomItY( lineWidth ) );
  229. painter->setFGColor( m_pLineStyle ->color() );
  230. painter->drawOpenPath( pNewPoints );
  231. delete pNewPoints;
  232. // Now draw the arrow heads
  233. if ( len )
  234. {
  235. painter->setBGColor( m_pFillStyle->color() );
  236. m_startAH->paint( painter, m_pStart->x(), m_pStart->y(), -vecX, -vecY, zoomHandler );
  237. m_endAH->paint( painter, m_pEnd->x(), m_pEnd->y(), vecX, vecY, zoomHandler );
  238. }
  239. // TODO: Text
  240. drawText( pData );
  241. }
  242. void KivioSMLConnector::paintOutline( KivioIntraStencilData *pData )
  243. {
  244. paint( pData );
  245. }
  246. bool KivioSMLConnector::saveCustom( QDomElement &e, QDomDocument &doc )
  247. {
  248. kdDebug(43000) << "Save custom " << endl;
  249. e.appendChild( saveArrowHeads( doc ) );
  250. //QDomElement type = m_shape->saveXML(doc);
  251. KivioConnectorPoint *p = m_PointList.first();
  252. while ( p )
  253. {
  254. QDomElement pt = p->saveXML( doc );
  255. e.appendChild( pt );
  256. p = m_PointList.next();
  257. }
  258. return true;
  259. }
  260. bool KivioSMLConnector::loadCustom( const QDomElement &e )
  261. {
  262. kdDebug(43000) << "Load custom " << endl;
  263. QDomNode node;
  264. QString name;
  265. node = e.firstChild();
  266. while ( !node.isNull() )
  267. {
  268. name = node.nodeName();
  269. if ( name == "KivioArrowHeads" )
  270. {
  271. loadArrowHeads( node.toElement() );
  272. }
  273. else if ( name == "KivioShape" )
  274. {
  275. //m_shape->loadXML(node.toElement());
  276. QDomNode pts = node.firstChild();
  277. while ( !pts.isNull() )
  278. {
  279. KivioConnectorPoint * pt = new KivioConnectorPoint();
  280. pt->loadXML( pts.toElement() );
  281. m_pConnectorPoints->append( pt );
  282. m_PointList.append( pt );
  283. pts = pts.nextSibling();
  284. }
  285. }
  286. node = node.nextSibling();
  287. }
  288. updateGeometry();
  289. return true;
  290. }
  291. QDomElement KivioSMLConnector::saveArrowHeads( QDomDocument &doc )
  292. {
  293. QDomElement e = doc.createElement( "KivioArrowHeads" );
  294. e.appendChild( m_startAH->saveXML( doc ) );
  295. e.appendChild( m_endAH->saveXML( doc ) );
  296. return e;
  297. }
  298. bool KivioSMLConnector::loadArrowHeads( const QDomElement &e )
  299. {
  300. QDomNode node;
  301. QString nodeName;
  302. QDomElement arrowE;
  303. bool first = true;
  304. node = e.firstChild();
  305. while ( !node.isNull() )
  306. {
  307. nodeName = node.nodeName();
  308. arrowE = node.toElement();
  309. if ( nodeName == "KivioArrowHead" )
  310. {
  311. if ( first == true )
  312. {
  313. m_startAH->loadXML( arrowE );
  314. first = false;
  315. }
  316. else
  317. {
  318. m_endAH->loadXML( arrowE );
  319. }
  320. }
  321. node = node.nextSibling();
  322. }
  323. return true;
  324. }
  325. bool KivioSMLConnector::loadPath( const QString &file )
  326. {
  327. kdDebug(43000) << "Loading :" << file << endl;
  328. m_PointList.clear();
  329. m_PointList.append(m_pStart);
  330. KivioConnectorPoint *pt = new KivioConnectorPoint(this,false);
  331. pt->setPosition(m_pStart->x()+10,m_pStart->y()+10,false);
  332. // m_PointList.append( pt );
  333. m_pConnectorPoints->append(pt);
  334. pt = new KivioConnectorPoint(this,false);
  335. pt->setPosition(m_pStart->x()+20,m_pStart->y()+20,false);
  336. m_PointList.append( pt );
  337. m_pConnectorPoints->append(pt);
  338. pt = new KivioConnectorPoint(this,false);
  339. pt->setPosition(m_pStart->x()+30,m_pStart->y()+30,false);
  340. m_PointList.append( pt );
  341. m_pConnectorPoints->append(pt);
  342. // m_PointList.append(m_pEnd);
  343. m_name = file;
  344. return true;
  345. }
  346. #include "sml_connector.moc"