LayoutManager.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #include "LayoutManager.h"
  2. LayoutManager::LayoutManager()
  3. {
  4. }
  5. SubGraph &LayoutManager::applyLayout(SubGraph &gMainGraph, const LayoutType &enLayoutType)
  6. {
  7. switch(enLayoutType)
  8. {
  9. case CircularLayout:
  10. {
  11. //Apply Circular Layout
  12. CircularLayoutGenerator CircularLayouter;
  13. LayoutEnum::VertexOrderCriteria enVertexOrder = LayoutEnum::DefaultOrder;
  14. // XXX enVertexOrder must be initialized -- fixed
  15. CircularLayouter.applyCircularLayout(gMainGraph, enVertexOrder);
  16. }
  17. break;
  18. case HierarchicalLayout:
  19. {
  20. //Apply Hierarchical Layout
  21. HierarchicalLayouter hierarchicalLayouter;
  22. hierarchicalLayouter.applyHierarchicalLayout(gMainGraph);
  23. }
  24. break;
  25. case RandomLayout:
  26. {
  27. //Apply random Layout
  28. RandomLayoutGenerator randomLayouter;
  29. randomLayouter.applyRandomLayout(gMainGraph);
  30. }
  31. break;
  32. case GridBasedLayout:
  33. {
  34. //Apply Grid Based Layout
  35. this->applyGridBasedLayout(&gMainGraph);
  36. }
  37. break;
  38. case ForceDirectedLayout:
  39. {
  40. //Apply Force Directed Layout
  41. applyForceDirectedLayout(&gMainGraph,iForceDirectedIterations);
  42. }
  43. break;
  44. default:
  45. // XXX huh?
  46. break;
  47. }
  48. return gMainGraph;
  49. }
  50. bool LayoutManager::applyLayout(LayoutType enLayoutType , QString strInputGraphmlPath,
  51. QString strOutputGraphMLPath)
  52. {
  53. LAYOUT_ASSERT(!strInputGraphmlPath.isEmpty() && !strOutputGraphMLPath.isEmpty(),
  54. LayoutException(__FUNCTION__
  55. ,LayoutExceptionEnum::REQUIRED_PARAMETER_NOT_SET
  56. ,"Input and Output graphml path"
  57. ,"applyLayout"));
  58. bool bLayoutStatus = false;
  59. //Read graphml
  60. QFile inFile(strInputGraphmlPath);
  61. GraphMLReader reader;
  62. SubGraph &gInGraph = reader.readGraphML(&inFile);
  63. //Initialize order property
  64. BoostGraphWrapper boostGraphWrapper;
  65. boostGraphWrapper.initGraphVertexOrderValues(gInGraph);
  66. //Apply layout
  67. SubGraph &gLaidoutGraph = applyLayout(gInGraph , enLayoutType);
  68. //Write back
  69. QFile outFile(strOutputGraphMLPath);
  70. if (!outFile.open(QFile::WriteOnly | QFile::Truncate))
  71. {
  72. //cout<<"Write path file could not be open. Exiting.\n";
  73. }
  74. else
  75. {
  76. GraphMLWriter writer;
  77. writer.writeGraphml(gLaidoutGraph , &outFile);
  78. bLayoutStatus = true;
  79. }
  80. return bLayoutStatus;
  81. }
  82. GraphLayoutErrorCodes::LayoutErrorCode LayoutManager::applyCircularLayout(QString sInputGraphMLFilePath, QString sOutputGraphMLFilePath, LayoutEnum::VertexOrderCriteria enVertexOrder)
  83. {
  84. GraphLayoutErrorCodes::LayoutErrorCode enCircularLayoutErrorCode= GraphLayoutErrorCodes::LAYOUT_SUCCESS;
  85. if( (sInputGraphMLFilePath.isEmpty() == true))
  86. {
  87. enCircularLayoutErrorCode = GraphLayoutErrorCodes::INVALID_FILE_NAME;
  88. }
  89. if((sInputGraphMLFilePath.trimmed().endsWith(GRAPHML, Qt::CaseInsensitive) == false)
  90. && (enCircularLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
  91. {
  92. enCircularLayoutErrorCode = GraphLayoutErrorCodes::UNSUPPORTED_FILE_FORMAT;
  93. }
  94. if((QFile::exists(sInputGraphMLFilePath) == false) && (enCircularLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
  95. {
  96. enCircularLayoutErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
  97. }
  98. // create global graph pointer.
  99. SubGraph *gMainGraph = (SubGraph *)0;
  100. if(enCircularLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  101. {
  102. // read input GraphML file
  103. QFile inFile(sInputGraphMLFilePath);
  104. GraphMLReader reader;
  105. try
  106. {
  107. SubGraph &gInputGraph = reader.readGraphML(&inFile);
  108. gMainGraph = &(gInputGraph);
  109. inFile.close();
  110. }
  111. catch(LayoutFileIOException& eException)
  112. {
  113. inFile.close();
  114. //We return the file exception enums
  115. enCircularLayoutErrorCode = (GraphLayoutErrorCodes::LayoutErrorCode)eException.getErrorCode();
  116. }
  117. catch(...)
  118. {
  119. inFile.close();
  120. enCircularLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  121. }
  122. }
  123. // XXX gMainGraph can be NULL at this point -- fixed
  124. if(gMainGraph != (SubGraph *)0) {
  125. enCircularLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  126. return enCircularLayoutErrorCode;
  127. }
  128. if(enCircularLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  129. {
  130. // apply circular layout
  131. CircularLayoutGenerator circularLayoutGenerator;
  132. try
  133. {
  134. circularLayoutGenerator.applyCircularLayout(*gMainGraph, enVertexOrder);
  135. }
  136. catch(...)
  137. {
  138. DELETE_AND_SET_NULL(gMainGraph);
  139. enCircularLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  140. }
  141. }
  142. if(enCircularLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  143. {
  144. // write file to output path
  145. QFile outFile(sOutputGraphMLFilePath);
  146. if ((!outFile.open(QFile::WriteOnly | QFile::Truncate)))
  147. {
  148. enCircularLayoutErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
  149. }
  150. if(enCircularLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  151. {
  152. GraphMLWriter writer;
  153. try
  154. {
  155. writer.writeGraphml(*gMainGraph , &outFile);
  156. outFile.close();
  157. }
  158. catch(...)
  159. {
  160. DELETE_AND_SET_NULL(gMainGraph);
  161. enCircularLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  162. outFile.close();
  163. }
  164. }
  165. }
  166. return enCircularLayoutErrorCode;
  167. }
  168. GraphLayoutErrorCodes::LayoutErrorCode LayoutManager::applyGridBasedLayout(QString sInputGraphMLFilePath,
  169. QString sOutputGraphMLFilePath)
  170. {
  171. GraphLayoutErrorCodes::LayoutErrorCode enGridBasedLayoutErrorCode= GraphLayoutErrorCodes::LAYOUT_SUCCESS;
  172. //Read input file-------------------------------------------------------------------------------------------
  173. if( (sInputGraphMLFilePath.isEmpty() == true))
  174. {
  175. enGridBasedLayoutErrorCode = GraphLayoutErrorCodes::INVALID_FILE_NAME;
  176. }
  177. if((sInputGraphMLFilePath.trimmed().endsWith(GRAPHML, Qt::CaseInsensitive) == false)
  178. && (enGridBasedLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
  179. {
  180. enGridBasedLayoutErrorCode = GraphLayoutErrorCodes::UNSUPPORTED_FILE_FORMAT;
  181. }
  182. if((QFile::exists(sInputGraphMLFilePath) == false) && (enGridBasedLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
  183. {
  184. enGridBasedLayoutErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
  185. }
  186. // create global graph pointer.
  187. SubGraph *gMainGraph = ( SubGraph *)0;
  188. if(enGridBasedLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  189. {
  190. // read input GraphML file
  191. QFile inFile(sInputGraphMLFilePath);
  192. GraphMLReader reader;
  193. try
  194. {
  195. SubGraph &gInputGraph = reader.readGraphML(&inFile);
  196. gMainGraph = &(gInputGraph);
  197. inFile.close();
  198. }
  199. catch(LayoutFileIOException& eException)
  200. {
  201. inFile.close();
  202. //We return the file exception enums
  203. enGridBasedLayoutErrorCode = (GraphLayoutErrorCodes::LayoutErrorCode)eException.getErrorCode();
  204. }
  205. catch(...)
  206. {
  207. enGridBasedLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  208. }
  209. }
  210. //---------------------------------------------------------------------------------------------------------
  211. // XXX gMainGraph can be NULL -- fixed
  212. if(gMainGraph == ( SubGraph *)0) {
  213. enGridBasedLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  214. return enGridBasedLayoutErrorCode;
  215. }
  216. this->applyGridBasedLayout(gMainGraph);
  217. //Write output file----------------------------------------------------------------------------------------
  218. if(enGridBasedLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  219. {
  220. QFile outFile(sOutputGraphMLFilePath);
  221. if ((!outFile.open(QFile::WriteOnly | QFile::Truncate)))
  222. {
  223. enGridBasedLayoutErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
  224. }
  225. if(enGridBasedLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  226. {
  227. GraphMLWriter writer;
  228. try
  229. {
  230. writer.writeGraphml(*gMainGraph , &outFile);
  231. outFile.close();
  232. }
  233. catch(...)
  234. {
  235. outFile.close();
  236. //DELETE_AND_SET_NULL(*gMainInputGraph);
  237. enGridBasedLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  238. }
  239. }
  240. }
  241. //---------------------------------------------------------------------------------------------------------
  242. return enGridBasedLayoutErrorCode;
  243. }
  244. void LayoutManager::applyGridBasedLayout(SubGraph * gMainInputGraph)
  245. {
  246. //FIND LAYOUT--------------------------------------------------------------------------------------------
  247. //Initialize cluster and vertex IDs, that will be used to identify which vertex is in which cluster
  248. MembershipInitializer membershipInitializer;
  249. membershipInitializer.initializeMembers(*gMainInputGraph);
  250. //Remove parallel edges
  251. int iInitialNumEdges = boost::num_edges(*gMainInputGraph);
  252. // XXX iInitialNumEdges is not used
  253. qDebug() << "iInitialNumEdges=" << iInitialNumEdges;
  254. GraphPreProcessor graphPreProcessor;
  255. std::vector<QString> oParallelEdgeIDs;
  256. graphPreProcessor.removeParallelEdges(*gMainInputGraph, m_oParallelEdges, oParallelEdgeIDs);
  257. // XXX compare of signed and unsigned -- obselete
  258. // LAYOUT_ASSERT(iInitialNumEdges == (boost::num_edges(*gMainInputGraph) + size(m_oParallelEdges)),
  259. // LayoutException("removeParallelEdges",
  260. // LayoutExceptionEnum::INCONSISTENT_DATASTRUCTURE,
  261. // "gMainInputGraph",
  262. // "after removeParallelEdges"));
  263. //Call gridLayouterForClusteredGraph()
  264. GridLayouter gridLayouter;
  265. gridLayouter.gridLayouterForClusteredGraph(*gMainInputGraph, *gMainInputGraph);
  266. // gridLayouter.gridLayouterForNonClusteredGraph(*gMainInputGraph);
  267. //Add parallel edges back
  268. // XXX unused - fixed
  269. int iIntermediateNumEdges = boost::num_edges(*gMainInputGraph);
  270. qDebug() << "iIntermediateNumEdges=" << iIntermediateNumEdges;
  271. graphPreProcessor.addParallelEdges(*gMainInputGraph, m_oParallelEdges, oParallelEdgeIDs);
  272. LAYOUT_ASSERT(boost::num_edges(*gMainInputGraph) == (iIntermediateNumEdges + size(m_oParallelEdges)),
  273. LayoutException("addParallelEdges",
  274. LayoutExceptionEnum::INCONSISTENT_DATASTRUCTURE,
  275. "gMainInputGraph",
  276. "after addParallelEdges"));
  277. //------------------------------------------------------------------------------------------------------
  278. //set default cluster size------------------------------------------------------------------------------
  279. BoostGraphWrapper boostGraphWrapper;
  280. int iDefaultGraphHeight = boostGraphWrapper.getGraphHeight(*gMainInputGraph);
  281. int iDefaultGraphWidth = boostGraphWrapper.getGraphWidth(*gMainInputGraph);
  282. boostGraphWrapper.setGraphHeight(iDefaultGraphHeight + 130, *gMainInputGraph);
  283. boostGraphWrapper.setGraphWidth(iDefaultGraphWidth + 130, *gMainInputGraph);
  284. int iDefaultGraphLeftTopX = boostGraphWrapper.getGraphLeftTopCoordX(*gMainInputGraph);
  285. int iDefaultGraphLeftTopY = boostGraphWrapper.getGraphLeftTopCoordY(*gMainInputGraph);
  286. boostGraphWrapper.setGraphLeftTopCoordX(iDefaultGraphLeftTopX - 65, *gMainInputGraph);
  287. boostGraphWrapper.setGraphLeftTopCoordY(iDefaultGraphLeftTopY - 65, *gMainInputGraph);
  288. //------------------------------------------------------------------------------------------------------
  289. //set NodeType and EdgeType-----------------------------------------------------------------------------
  290. VertexIterator itrVert, itrVertEnd;
  291. for(boost::tie(itrVert, itrVertEnd)=vertices(*gMainInputGraph); itrVert!=itrVertEnd; ++itrVert)
  292. {
  293. VertexDescriptor vCurrentVertex = *itrVert;
  294. //Enum::NodeType enNodeType = Enum::GraphNode;
  295. boostGraphWrapper.setVertexType(vCurrentVertex, *gMainInputGraph, LayoutEnum::GraphNode);
  296. }
  297. EdgeIterator itrEdge, itrEdgeEnd;
  298. for(boost::tie(itrEdge, itrEdgeEnd)=edges(*gMainInputGraph); itrEdge!=itrEdgeEnd; ++itrEdge)
  299. {
  300. EdgeDescriptor eCurrentEdge = *itrEdge;
  301. //Enum::EdgeType enEdgeType = Enum::GraphEdge;
  302. boostGraphWrapper.setEdgeType(eCurrentEdge, *gMainInputGraph, LayoutEnum::GraphEdge);
  303. }
  304. //------------------------------------------------------------------------------------------------------
  305. }
  306. GraphLayoutErrorCodes::LayoutErrorCode LayoutManager::applyHierarchicalLayout(QString sInputGraphMLFilePath, QString sOutputGraphMLFilePath)
  307. {
  308. GraphLayoutErrorCodes::LayoutErrorCode enHierarchicalLayoutErrorCode = GraphLayoutErrorCodes::LAYOUT_SUCCESS;
  309. if( (sInputGraphMLFilePath.isEmpty() == true))
  310. {
  311. enHierarchicalLayoutErrorCode = GraphLayoutErrorCodes::INVALID_FILE_NAME;
  312. }
  313. if((sInputGraphMLFilePath.trimmed().endsWith(GRAPHML, Qt::CaseInsensitive) == false)
  314. && (enHierarchicalLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
  315. {
  316. enHierarchicalLayoutErrorCode = GraphLayoutErrorCodes::UNSUPPORTED_FILE_FORMAT;
  317. }
  318. if((QFile::exists(sInputGraphMLFilePath) == false) && (enHierarchicalLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
  319. {
  320. enHierarchicalLayoutErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
  321. }
  322. // create global graph pointer.
  323. SubGraph *gMainGraph = ( SubGraph *)0;
  324. if(enHierarchicalLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  325. {
  326. // read input GraphML file
  327. QFile inFile(sInputGraphMLFilePath);
  328. GraphMLReader reader;
  329. try
  330. {
  331. SubGraph &gInputGraph = reader.readGraphML(&inFile);
  332. gMainGraph = &(gInputGraph);
  333. }
  334. catch(LayoutFileIOException& eException)
  335. {
  336. //We return the file exception enums
  337. enHierarchicalLayoutErrorCode = (GraphLayoutErrorCodes::LayoutErrorCode)eException.getErrorCode();
  338. }
  339. catch(...)
  340. {
  341. enHierarchicalLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  342. }
  343. }
  344. // XXX gMainGraph can be NULL -- fixed
  345. if(gMainGraph == ( SubGraph *)0){
  346. enHierarchicalLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  347. return enHierarchicalLayoutErrorCode;
  348. }
  349. if(enHierarchicalLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  350. {
  351. // apply hierarchical layout
  352. HierarchicalLayouter hierarchicalLayouter;
  353. try
  354. {
  355. hierarchicalLayouter.applyHierarchicalLayout(*gMainGraph);
  356. }
  357. catch(...)
  358. {
  359. DELETE_AND_SET_NULL(gMainGraph);
  360. enHierarchicalLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  361. }
  362. }
  363. if(enHierarchicalLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  364. {
  365. // write file to output path
  366. QFile outFile(sOutputGraphMLFilePath);
  367. if ((!outFile.open(QFile::WriteOnly | QFile::Truncate)))
  368. {
  369. enHierarchicalLayoutErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
  370. }
  371. if(enHierarchicalLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  372. {
  373. GraphMLWriter writer;
  374. try
  375. {
  376. writer.writeGraphml(*gMainGraph , &outFile);
  377. outFile.flush();
  378. outFile.close();
  379. }
  380. catch(...)
  381. {
  382. DELETE_AND_SET_NULL(gMainGraph);
  383. enHierarchicalLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  384. }
  385. }
  386. }
  387. return enHierarchicalLayoutErrorCode;
  388. }
  389. GraphLayoutErrorCodes::LayoutErrorCode LayoutManager::applyForceDirectedLayout(SubGraph *gMainInputGraph
  390. ,int iIterations)
  391. {
  392. GraphLayoutErrorCodes::LayoutErrorCode enForceLayoutErrorCode = GraphLayoutErrorCodes::LAYOUT_SUCCESS;
  393. LAYOUT_ASSERT(iIterations != 0,LayoutException(__FUNCTION__
  394. ,LayoutExceptionEnum::INVALID_ATTRIBUTE_VALUE
  395. ,"ITERATIONS"
  396. ,"Iterations"));
  397. LAYOUT_ASSERT(iIterations < 5000,LayoutException(__FUNCTION__
  398. ,LayoutExceptionEnum::INVALID_ATTRIBUTE_VALUE
  399. ,"ITERATIONS"
  400. ,"Set Iteration less than 5000"));
  401. if(enForceLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  402. {
  403. try{
  404. // LAYOUT_ASSERT(num_vertices(*gMainInputGraph) != 0,LayoutException(__FUNCTION__
  405. // ,ExceptionEnum::INVALID_ATTRIBUTE_VALUE
  406. // ,"applyForceDirectedLayout"
  407. // ,"Number of vertices"));
  408. MembershipInitializer *memInit = new MembershipInitializer();
  409. memInit->initializeMembers(*gMainInputGraph);
  410. Reingold *reingold = new Reingold();
  411. // reingold->SetClusterCompartmentPropsNew(*gMainInputGraph);
  412. reingold->setCompartMentProps(*gMainInputGraph,10);
  413. ClusteredSpringEmbedder *clusteredSpring = new ClusteredSpringEmbedder();
  414. clusteredSpring->springForEachCluster(*gMainInputGraph,iIterations);
  415. // clusteredSpring->springTestRecursive(gMainInputGraph);
  416. // reingold->SetClusterCompartmentPropsNew(*gMainInputGraph);
  417. reingold->setCompartMentProps(*gMainInputGraph,10);
  418. }
  419. catch(boost::exception &eBoostException)
  420. {
  421. throw *boost::get_error_info<errmsg_info>(eBoostException);
  422. enForceLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  423. }
  424. catch(LayoutException &eLayoutException)
  425. {
  426. throw eLayoutException;
  427. }
  428. }
  429. // XXX must return here something
  430. return enForceLayoutErrorCode;
  431. }
  432. GraphLayoutErrorCodes::LayoutErrorCode LayoutManager::applyForceDirectedLayout(QString sInputGraphMLFilePath,
  433. QString sOutputGraphMLFilePath,
  434. int iIterations)
  435. {
  436. GraphLayoutErrorCodes::LayoutErrorCode enForceLayoutErrorCode =
  437. GraphLayoutErrorCodes::LAYOUT_SUCCESS;
  438. if( (sInputGraphMLFilePath.isEmpty() == true))
  439. {
  440. enForceLayoutErrorCode = GraphLayoutErrorCodes::INVALID_FILE_NAME;
  441. }
  442. if((sInputGraphMLFilePath.trimmed().endsWith(GRAPHML, Qt::CaseInsensitive) == false)
  443. && (enForceLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
  444. {
  445. enForceLayoutErrorCode = GraphLayoutErrorCodes::UNSUPPORTED_FILE_FORMAT;
  446. }
  447. if((QFile::exists(sInputGraphMLFilePath) == false) && (enForceLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS))
  448. {
  449. enForceLayoutErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
  450. }
  451. // create global graph pointer.
  452. SubGraph *gMainGraph = ( SubGraph *)0;
  453. if(enForceLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  454. {
  455. // read input GraphML file
  456. QFile inFile(sInputGraphMLFilePath);
  457. GraphMLReader reader;
  458. try
  459. {
  460. SubGraph &gInputGraph = reader.readGraphML(&inFile);
  461. gMainGraph = &(gInputGraph);
  462. inFile.close();
  463. }
  464. catch(LayoutFileIOException& eException)
  465. {
  466. inFile.close();
  467. //We return the file exception enums
  468. enForceLayoutErrorCode = (GraphLayoutErrorCodes::LayoutErrorCode)eException.getErrorCode();
  469. }
  470. }
  471. // XXX gmaingraph can be NULL -- fixed
  472. if (gMainGraph == ( SubGraph *)0){
  473. enForceLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  474. return enForceLayoutErrorCode;
  475. }
  476. if(enForceLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  477. {
  478. // apply Force Based layout
  479. applyForceDirectedLayout(gMainGraph,iIterations);
  480. }
  481. if(enForceLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  482. {
  483. // write file to output path
  484. QFile outFile(sOutputGraphMLFilePath);
  485. if ((!outFile.open(QFile::WriteOnly | QFile::Truncate)))
  486. {
  487. enForceLayoutErrorCode = GraphLayoutErrorCodes::FILE_NOT_FOUND;
  488. }
  489. if(enForceLayoutErrorCode == GraphLayoutErrorCodes::LAYOUT_SUCCESS)
  490. {
  491. GraphMLWriter writer;
  492. try
  493. {
  494. writer.writeGraphml(*gMainGraph , &outFile);
  495. outFile.flush();
  496. outFile.close();
  497. }
  498. catch(...)
  499. {
  500. outFile.close();
  501. DELETE_AND_SET_NULL(gMainGraph);
  502. enForceLayoutErrorCode = GraphLayoutErrorCodes::UNKNOWN_LAYOUT_EXCEPTION;
  503. outFile.close();
  504. }
  505. }
  506. }
  507. // XXX huh?
  508. //return gMainGraph;
  509. return enForceLayoutErrorCode;
  510. }
  511. GraphLayoutErrorCodes::LayoutErrorCode LayoutManager::applyVertexOverlapRemoval(QString sInputGraphMLFilePath,
  512. QString sOutputGraphMLFilePath)
  513. {
  514. GraphLayoutErrorCodes::LayoutErrorCode enVertexOVerlapErrorcode;
  515. VertexOverlapRemoval verOverlapRemoval;
  516. enVertexOVerlapErrorcode = verOverlapRemoval.removeOverlaps(sInputGraphMLFilePath
  517. ,sOutputGraphMLFilePath);
  518. return enVertexOVerlapErrorcode;
  519. }
  520. void LayoutManager::setNumberOfIterations(int iterations)
  521. {
  522. iForceDirectedIterations = iterations;
  523. }