File: parser.cpp

package info (click to toggle)
socnetv 0.90-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,028 kB
  • sloc: cpp: 12,953; makefile: 75
file content (2135 lines) | stat: -rwxr-xr-x 70,791 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
/***************************************************************************
 SocNetV: Social Networks Visualizer 
 version: 0.90
 Written in Qt 4.4
 
                         parser.cpp  -  description
                             -------------------
    copyright            : (C) 2005-2010 by Dimitris B. Kalamaras
    email                : dimitris.kalamaras@gmail.com
 ***************************************************************************/

/*******************************************************************************
*     This program is free software: you can redistribute it and/or modify     *
*     it under the terms of the GNU General Public License as published by     *
*     the Free Software Foundation, either version 3 of the License, or        *
*     (at your option) any later version.                                      *
*                                                                              *
*     This program is distributed in the hope that it will be useful,          *
*     but WITHOUT ANY WARRANTY; without even the implied warranty of           *
*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
*     GNU General Public License for more details.                             *
*                                                                              *
*     You should have received a copy of the GNU General Public License        *
*     along with this program.  If not, see <http://www.gnu.org/licenses/>.    *
********************************************************************************/

#include "parser.h"

#include <QFile>
#include <QTextStream>
#include <QString>
#include <QRegExp>
#include <QStringList>
#include <QtDebug>		//used for qDebug messages
#include <QPointF>
#include <QMessageBox>
#include <list>
#include "graph.h"	//needed for setParent

bool Parser::load(QString fn, int iNS, QString iNC, QString iNSh, 
					QString iNNC, int iNNS, 
					QString iNLC, int iNLS , 
					QString iEC, 
					int width, int height,
					int fFormat,
					int sm_mode
					)
{
	qDebug("Parser: load()");
	initNodeSize=iNS;
	initNodeColor=iNC;
	initNodeShape=iNSh;
	initNodeNumberColor=iNNC;
	initNodeNumberSize=iNNS;
	initNodeLabelColor=iNLC;
	initNodeLabelSize=iNLS;
	
	initEdgeColor=iEC;
	
	undirected=0; arrows=FALSE; bezier=FALSE;
	fileName=fn;
	networkName=(fileName.split ("/")).last();
	gwWidth=width;
	gwHeight=height;
	randX=0;
	randY=0;
	fileFormat= fFormat;
	two_sm_mode = sm_mode;

	//qDebug()<< "Parser: start() a new parsing thread for file format: " << fileFormat ;
	if (!isRunning()) 
		start(QThread::NormalPriority);
		
	if (isFinished()) {
		//qDebug()<< "**** Parser:: thread has finished! " 
				//<< " fileFormat now "<< fileFormat ;
		if ( fileFormat != -1 ) {
			return true;
		}
		else 
			return false;
	}
	else {
		qDebug()<< "**** Parser:: thread has not finished yet but we return back to Graph and MW! " 
				<< " fileFormat now "<< fileFormat ;
	}
	return true;	
}



/**
	Tries to load a file as DL-formatted network. If not it returns -1
*/
bool Parser::loadDL(){

	qDebug ("\n\nParser: loadDL");
	QFile file ( fileName );
	if ( ! file.open(QIODevice::ReadOnly )) return false;
	QTextStream ts( &file );

	QString str, label;
	
	int i=0, j=0, lineCounter=0, mark=0, nodeNum=0;
	edgeWeight=0;
	bool labels_flag=false, data_flag=false, intOK=false, floatOK=false;
	QStringList lineElement;
	totalLinks=0;

	while ( !ts.atEnd() )   {
		str= ts.readLine();
		lineCounter++;


		if ( isComment(str) ) 
			continue;
				
		if ( (lineCounter == 1) &&  (!str.startsWith("DL",Qt::CaseInsensitive)  ) ) {  
			qDebug("*** Parser-loadDL(): not a DL file. Aborting!");
			file.close();
			return false;
		}

		if (str.startsWith( "N=", Qt::CaseInsensitive) ||  str.startsWith( "N =", Qt::CaseInsensitive) )  {   
			mark=str.indexOf("=");
			str=str.right(str.size()-mark-1);
			qDebug()<< "N = : " << str.toAscii() ;
			aNodes=str.toInt(&intOK,10);   
			if (!intOK) { qDebug()<< "Parser: loadDL(): conversion error..." ; return false;}
		}

		if (str.startsWith( "FORMAT =", Qt::CaseInsensitive) || str.startsWith( "FORMAT=", Qt::CaseInsensitive))  {   
			mark=str.indexOf("=");
			str=str.right(str.size()-mark-1);
			qDebug()<<  "FORMAT = : " <<  str.toAscii() ;
		}

		if (str.startsWith( "labels", Qt::CaseInsensitive) ) {
		 	labels_flag=true; data_flag=false;
			continue;
		}
		else if ( str.startsWith( "data:", Qt::CaseInsensitive) || str.startsWith( "data :", Qt::CaseInsensitive) ) {
		 	data_flag=true; labels_flag=false;
			continue;
		}

		if (labels_flag) {  //read a label and create a node in a random position

			label=str;
			randX=rand()%gwWidth;
			randY=rand()%gwHeight;
			nodeNum++;
			qDebug()<<"Creating node at "<< randX<<","<< randY;
			emit createNode(
						nodeNum, initNodeSize,initNodeColor, 
						initNodeNumberColor, initNodeNumberSize,
						label, initNodeLabelColor, initNodeLabelSize, 
						QPointF(randX, randY), 
						initNodeShape, false
						);
		
		}
		if ( data_flag){		//read edges
			//SPLIT EACH LINE (ON EMPTY SPACE CHARACTERS) 
			lineElement=str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
			j=0;
			for (QStringList::Iterator it1 = lineElement.begin(); it1!=lineElement.end(); ++it1)   {
				qDebug()<< (*it1).toAscii() ;
				if ( (*it1)!="0"){ //here is an non-zero edge weight...
					qDebug()<<  "Parser-loadDL(): there is an edge here";
					edgeWeight=(*it1).toFloat(&floatOK); 
					undirected=0;
					arrows=true;
					bezier=false;
					emit createEdge(i+1, j+1, edgeWeight, initEdgeColor, undirected, arrows, bezier);
					totalLinks++;
					qDebug()<<  "Link from Node i= " <<  i+1 << " to j= "<< j+1;
					qDebug() << "TotalLinks= " << totalLinks;

				}
				j++;
			}
			i++;
		

		}
	}
	//sanity check
	if (nodeNum != aNodes) { 
		qDebug()<< "Error: aborting";
		return false;
	}
	//The network has been loaded. Tell MW the statistics and network type
	// 0: no format, 1: GraphML, 2:Pajek, 3:Adjacency, 4: Dot, 5:DL, 6:GML, 7: List 
	emit fileType(5, networkName, aNodes, totalLinks, undirected);
	qDebug() << "Parser-loadDL()";
	return true;

}

/**
	Tries to load the file as Pajek-formatted network. If not it returns -1
*/
bool Parser::loadPajek(){

	qDebug ("\n\nParser: loadPajek");
	QFile file ( fileName );
	if ( ! file.open(QIODevice::ReadOnly )) return false;
	QTextStream ts( &file );
	QString str, label, temp;
	nodeColor="";
	edgeColor="";
	nodeShape="";
	QStringList lineElement;
	bool ok=FALSE, intOk=FALSE, check1=FALSE, check2=FALSE;
	bool nodes_flag=FALSE, edges_flag=FALSE, arcs_flag=FALSE, arcslist_flag=FALSE, matrix_flag=FALSE;
	bool fileContainsNodeColors=FALSE, fileContainsNodesCoords=FALSE;
	bool fileContainsLinksColors=FALSE;
	bool zero_flag=FALSE;
	int   i=0, j=0, miss=0, source= -1, target=-1, nodeNum, colorIndex=-1, coordIndex=-1;
	unsigned long int lineCounter=0;
	float weight=1;
	list<int> listDummiesPajek;
	totalLinks=0;
	aNodes=0;
	j=0;  //counts how many real nodes exist in the file 
	miss=0; //counts missing nodeNumbers. 
	//if j + miss < nodeNum, it creates (nodeNum-miss) dummy nodes which are deleted in the end.
	QList <int> toBeDeleted;
	
	while ( !ts.atEnd() )   {
		str= ts.readLine();
		
		if ( isComment(str)  ) 
			continue;

		lineCounter++;

		if (lineCounter==1) {
			if ( str.contains("graph",Qt::CaseInsensitive)  
				|| str.contains("digraph",Qt::CaseInsensitive) 
				|| str.contains("DL",Qt::CaseInsensitive) 
				|| str.contains("list",Qt::CaseInsensitive)
				|| str.contains("graphml",Qt::CaseInsensitive) 
				|| str.contains("xml",Qt::CaseInsensitive)
				) {
				qDebug()<< "*** Parser:loadPajeck(): Not an Pajek-formatted file. Aborting!!";
				file.close();		
	 		 	return false;    
			}
		}

		if (!edges_flag && !arcs_flag && !nodes_flag && !arcslist_flag && !matrix_flag) {
			//qDebug("Parser-loadPajek(): reading headlines");
			if ( (lineCounter == 1) &&  (!str.contains("network",Qt::CaseInsensitive) && !str.contains("vertices",Qt::CaseInsensitive) ) ) {  
				//this is not a pajek file. Abort
				qDebug("*** Parser-loadPajek(): Not a Pajek file. Aborting!");
				file.close();
				return false;
			}
   			else if (str.contains( "network",Qt::CaseInsensitive) )  { //NETWORK NAME
				if (str.contains(" ")) {
					lineElement=str.split(QRegExp("\\s+"));	//split at one or more spaces
					//qDebug()<<"Parser-loadPajek(): possible net name: "<<lineElement[1];
					if (lineElement[1]!="") 
						networkName=lineElement[1];
				}
				qDebug()<<"Parser-loadPajek(): network name: "<<networkName;
				continue;
			}
			if (str.contains( "vertices", Qt::CaseInsensitive) )  {   
				lineElement=str.split(QRegExp("\\s+"));
				if (!lineElement[1].isEmpty()) 	aNodes=lineElement[1].toInt(&intOk,10);   
				qDebug ("Parser-loadPajek(): Vertices %i.",aNodes);
				continue;
			}
			qDebug("Parser-loadPajek(): headlines end here");
		}
		/**SPLIT EACH LINE (ON EMPTY SPACE CHARACTERS) IN SEVERAL ELEMENTS*/
		lineElement=str.split(QRegExp("\\s+"), QString::SkipEmptyParts);

		if ( str.contains( "*edges", Qt::CaseInsensitive) ) {
		 	edges_flag=true; arcs_flag=false; arcslist_flag=false; matrix_flag=false;
			continue;
		}
		else if ( str.contains( "*arcs", Qt::CaseInsensitive) ) { 
			arcs_flag=true; edges_flag=false; arcslist_flag=false; matrix_flag=false;
			continue;
		}
		else if ( str.contains( "*arcslist", Qt::CaseInsensitive) ) { 
			arcs_flag=false; edges_flag=false; arcslist_flag=true; matrix_flag=false;
			continue;
		}
		else if ( str.contains( "*matrix", Qt::CaseInsensitive) ) { 
			arcs_flag=false; edges_flag=false; arcslist_flag=false; matrix_flag=true;
			continue;
		}

		/** READING NODES */
		if (!edges_flag && !arcs_flag && !arcslist_flag && !matrix_flag) {
			//qDebug("=== Reading nodes ===");
			nodes_flag=TRUE;
			nodeNum=lineElement[0].toInt(&intOk, 10);
			//qDebug()<<"node number: "<<nodeNum;
			if (nodeNum==0) {
				qDebug ("Node is zero numbered! Raising zero-start-flag - increasing nodenum");
				zero_flag=TRUE;
			}
			if (zero_flag){
				nodeNum++;
			}
			if (lineElement.size() < 2 ){
				label=lineElement[0];
				randX=rand()%gwWidth;
				randY=rand()%gwHeight;
				nodeColor=initNodeColor;
				nodeShape=initNodeShape;
			}
			else {	/** NODELABEL */
				label=lineElement[1];
				//qDebug()<< "node label: " << lineElement[1].toAscii();
				str.remove (0, str.lastIndexOf(label)+label.size() );	
				//qDebug()<<"cropped str: "<< str.toAscii();
				if (label.contains('"', Qt::CaseInsensitive) )
					label=label.remove('\"');
				//qDebug()<<"node label now: " << label.toAscii();
								
				/** NODESHAPE: There are four possible . */
				if (str.contains("Ellipse", Qt::CaseInsensitive) ) nodeShape="ellipse";
				else if (str.contains("circle", Qt::CaseInsensitive) ) nodeShape="circle";
				else if (str.contains("box", Qt::CaseInsensitive) ) nodeShape="box";
				else if (str.contains("triangle", Qt::CaseInsensitive) ) nodeShape="triangle";
				else nodeShape="diamond";
				/** NODECOLORS */
				//if there is an "ic" tag, a specific NodeColor for this node follows...
				if (str.contains("ic",Qt::CaseInsensitive)) { 
					for (register int c=0; c< lineElement.count(); c++) {
						if (lineElement[c] == "ic") { 
							//the colourname is at c+1 position.
							nodeColor=lineElement[c+1];
							fileContainsNodeColors=TRUE;
							break;
						}
					}
					//qDebug()<<"nodeColor:" << nodeColor;
					if (nodeColor.contains (".") )  nodeColor=initNodeColor;
				}
				else { //there is no nodeColor. Use the default
					//qDebug("No nodeColor");
					fileContainsNodeColors=FALSE;
					nodeColor=initNodeColor;
					
				}
				/**READ NODE COORDINATES */
				if ( str.contains(".",Qt::CaseInsensitive) ) { 
					for (register int c=0; c< lineElement.count(); c++)   {
						temp=lineElement.at(c);
				//		qDebug()<< temp.toAscii();
						if ((coordIndex=temp.indexOf(".", Qt::CaseInsensitive)) != -1 ){ 	
							if (lineElement.at(c-1) == "ic" ) continue;  //pajek declares colors with numbers!
							if ( !temp[coordIndex-1].isDigit()) continue;  //needs 0.XX
							if (c+1 == lineElement.count() ) {//first coord zero, i.e: 0  0.455
								//qDebug ()<<"coords: " <<lineElement.at(c-1).toAscii() << " " <<temp.toAscii() ;
								randX=lineElement.at(c-1).toDouble(&check1);
								randY=temp.toDouble(&check2);
							}
							else {
								//qDebug ()<<"coords: " << temp.toAscii() << " " <<lineElement[c+1].toAscii();
								randX=temp.toDouble(&check1);
								randY=lineElement[c+1].toDouble(&check2);
							}

							if (check1 && check2)    {
								randX=randX * gwWidth;
								randY=randY * gwHeight;
								fileContainsNodesCoords=TRUE;
							}
							if (randX <= 0.0 || randY <= 0.0 ) {
								randX=rand()%gwWidth;
								randY=rand()%gwHeight;
							}
							break;
						}
					}
					//qDebug()<<"Coords: "<<randX << randY<< gwHeight;
				}
				else { 
					fileContainsNodesCoords=FALSE;
					randX=rand()%gwWidth;
					randY=rand()%gwHeight;
					//qDebug()<<"No coords. Using random "<<randX << randY;
				}
			}
			/**START NODE CREATION */
			qDebug ()<<"Creating node numbered "<< nodeNum << " Real nodes count (j)= "<< j+1;
			j++;  //Controls the real number of nodes.
			//If the file misses some nodenumbers then we create dummies and delete them afterwards!
			if ( j + miss < nodeNum)  {
				qDebug ()<<"MW There are "<< j << " nodes but this node has number "<< nodeNum
						<<"Creating node at "<< randX<<","<< randY;
				for (int num=j; num< nodeNum; num++) {
					//qDebug()<< "Parser-loadPajek(): Creating dummy node number num = "<< num;
					//qDebug()<<"Creating node at "<< randX<<","<< randY;
					
					emit createNode(
									num, initNodeSize, nodeColor,
									initNodeNumberColor, initNodeNumberSize,
									label, lineElement[3], initNodeLabelSize,
									QPointF(randX, randY), 
									nodeShape, false
									);
					listDummiesPajek.push_back(num);  
					miss++;
				}
			}
			else if ( j > nodeNum ) {
				qDebug ("Error: This Pajek net declares this node with nodeNumber smaller than previous nodes. Aborting");
				return false;	
			}

			emit createNode(
							nodeNum,initNodeSize, nodeColor, 
							initNodeNumberColor, initNodeNumberSize,
							label, initNodeLabelColor, initNodeLabelSize, 
							QPointF(randX, randY), 
							nodeShape, false
							);
			initNodeColor=nodeColor; 
		} 	
		/**NODES CREATED. CREATE EDGES/ARCS NOW. */		
		else {
			if (j && j!=aNodes)  {  //if there were more or less nodes than the file declared
				qDebug()<<"*** WARNING ***: The Pajek file declares " << aNodes <<"  nodes, but I found " <<  j << " nodes...." ;
				aNodes=j;
			}
			else if (j==0) {  //if there were no nodes at all, we need to create them now.
				qDebug()<< "The Pajek file declares "<< aNodes<< " but I didnt found any nodes. I will create them....";
				for (int num=j+1; num<= aNodes; num++) {
					qDebug() << "Parser-loadPajek(): Creating node number i = "<< num;
					randX=rand()%gwWidth;
					randY=rand()%gwHeight;
					emit createNode(
									num,initNodeSize, initNodeColor,
									initNodeNumberColor, initNodeNumberSize, 
									QString::number(i), initNodeLabelColor,initNodeLabelSize, 
									 QPointF(randX, randY), 
									 initNodeShape, false
									 );
				}
				j=aNodes;
			}
			if (edges_flag && !arcs_flag)   {  /**EDGES */
				//qDebug("Parser-loadPajek(): ==== Reading edges ====");
				qDebug()<<lineElement;
				source =  lineElement[0].toInt(&ok, 10);
				target = lineElement[1].toInt(&ok,10);
				if (source == 0 || target == 0 ) return false;  //  i --> (i-1)   internally
				else if (source < 0 && target >0  ) {  //weights come first...
					edgeWeight  = lineElement[0].toFloat(&ok);
					source=  lineElement[1].toInt(&ok, 10);
					if (lineElement.count()>2) {
						target = lineElement[2].toInt(&ok,10);
					}
					else {
						target = lineElement[1].toInt(&ok,10);  //self link
					}
				}
				else if (lineElement.count()>2)
					edgeWeight =lineElement[2].toFloat(&ok);
				else 
					edgeWeight =1.0;

				//qDebug()<<"Parser-loadPajek(): weight "<< weight;

				if (lineElement.contains("c", Qt::CaseSensitive ) ) {
					//qDebug("Parser-loadPajek(): file with link colours");
					fileContainsLinksColors=TRUE;
					colorIndex=lineElement.indexOf( QRegExp("[c]"), 0 )  +1;
					if (colorIndex >= lineElement.count()) edgeColor=initEdgeColor;
					else 	edgeColor=lineElement [ colorIndex ];
					if (edgeColor.contains (".") )  edgeColor=initEdgeColor;
					//qDebug()<< " current color "<< edgeColor;
 				}
				else  {
					//qDebug("Parser-loadPajek(): file with no link colours");
					edgeColor=initEdgeColor;
				}
				undirected=2;
				arrows=true;
				bezier=false;
				qDebug()<< "Parser-loadPajek(): EDGES: Create edge between " << source << " - "<< target;
				emit createEdge(source, target, edgeWeight, edgeColor, undirected, arrows, bezier);
				totalLinks=totalLinks+2;

			} //end if EDGES 
			else if (!edges_flag && arcs_flag)   {  /** ARCS */
				//qDebug("Parser-loadPajek(): === Reading arcs ===");
				source=  lineElement[0].toInt(&ok, 10);
				target = lineElement[1].toInt(&ok,10);
				if (source == 0 || target == 0 ) return false;   //  i --> (i-1)   internally
				else if (source < 0 && target >0 ) {  //weights come first...
					edgeWeight  = lineElement[0].toFloat(&ok);
					source=  lineElement[1].toInt(&ok, 10);
					if (lineElement.count()>2) {
						target = lineElement[2].toInt(&ok,10);
					}
					else {
						target = lineElement[1].toInt(&ok,10);  //self link
					}
				}
				else if (lineElement.count()>2)
					edgeWeight  =lineElement[2].toFloat(&ok);
				else 
					edgeWeight =1.0;

				if (lineElement.contains("c", Qt::CaseSensitive ) ) {
					//qDebug("Parser-loadPajek(): file with link colours");
					edgeColor=lineElement.at ( lineElement.indexOf( QRegExp("[c]"), 0 ) + 1 );
					fileContainsLinksColors=TRUE;
				}
				else  {
					//qDebug("Parser-loadPajek(): file with no link colours");
					edgeColor=initEdgeColor;
				}
				undirected=0;
				arrows=true;
				bezier=false;
				qDebug()<<"Parser-loadPajek(): ARCS: Creating arc from node "<< source << " to node "<< target << " with weight "<< weight;
				emit createEdge(source, target, edgeWeight , edgeColor, undirected, arrows, bezier);
				totalLinks++;
			} //else if ARCS
			else if (arcslist_flag)   {  /** ARCSlist */
				//qDebug("Parser-loadPajek(): === Reading arcs list===");
				if (lineElement[0].startsWith("-") ) lineElement[0].remove(0,1);
				source= lineElement[0].toInt(&ok, 10);
				fileContainsLinksColors=FALSE;
				edgeColor=initEdgeColor;
				undirected=0;
				arrows=true;
				bezier=false;
				for (register int index = 1; index < lineElement.size(); index++) {
					target = lineElement.at(index).toInt(&ok,10);					
					qDebug()<<"Parser-loadPajek(): ARCS LIST: Creating ARC source "<< source << " target "<< target << " with weight "<< weight;
					emit createEdge(source, target, edgeWeight, edgeColor, undirected, arrows, bezier);
					totalLinks++;
				}
			} //else if ARCSLIST
			else if (matrix_flag)   {  /** matrix */
				//qDebug("Parser-loadPajek(): === Reading matrix of edges===");
				i++;
				source= i;
				fileContainsLinksColors=FALSE;
				edgeColor=initEdgeColor;
				undirected=0;
				arrows=true;
				bezier=false;
				for (target = 0; target < lineElement.size(); target ++) {
					if ( lineElement.at(target) != "0" ) {
						edgeWeight  = lineElement.at(target).toFloat(&ok);					
						qDebug()<<"Parser-loadPajek():  MATRIX: Creating arc source "<< source << " target "<< target +1<< " with weight "<< weight;
						emit createEdge(source, target+1, edgeWeight, edgeColor, undirected, arrows, bezier);
						totalLinks++;
					}
				}
			} //else if matrix
		} //end if BOTH ARCS AND EDGES
	} //end WHILE
	file.close();
	if (j==0) return false;
	//The network has been loaded. Tell MW the statistics and network type
	// 0: no format, 1: GraphML, 2:Pajek, 3:Adjacency, 4: Dot, 5:DL, 6:GML, 7: List	
	emit fileType(2, networkName, aNodes, totalLinks, undirected);
	
	qDebug("Parser-loadPajek(): Removing all dummy aNodes, if any");
	if (listDummiesPajek.size() > 0 ) {
		qDebug("Trying to delete the dummies now");
		for ( list<int>::iterator it=listDummiesPajek.begin(); it!=listDummiesPajek.end(); it++ ) {
			emit removeDummyNode(*it);
		}
	}
	qDebug("Parser-loadPajek(): Clearing DumiesList from Pajek");
	listDummiesPajek.clear();
	exit(0);
	return true;

}






/**
	Tries to load the file as adjacency sociomatrix-formatted. If not it returns -1
*/
bool Parser::loadAdjacency(){
	qDebug("\n\nParser: loadAdjacency()");
	QFile file ( fileName );
	if ( ! file.open(QIODevice::ReadOnly )) return false;
	QTextStream ts( &file );
	QString str;
	QStringList lineElement;
	int i=0, j=0,  aNodes=0, newCount=0, lastCount=0;
	edgeWeight=1.0;
	bool intOK=FALSE;
	totalLinks=0;
	i=1;
	while ( i < 11 &&  !ts.atEnd() )   {
		str= ts.readLine() ;
		str=str.simplified();  			

		if ( isComment(str) ) 
			continue;

		if ( str.contains("vertices",Qt::CaseInsensitive) 
			|| str.contains("network",Qt::CaseInsensitive) 
			|| str.contains("graph",Qt::CaseInsensitive)  
			|| str.contains("digraph",Qt::CaseInsensitive) 
			|| str.contains("DL",Qt::CaseInsensitive) 
			|| str.contains("list",Qt::CaseInsensitive)
			|| str.contains("graphml",Qt::CaseInsensitive) 
			|| str.contains("xml",Qt::CaseInsensitive)  
			) {
			qDebug()<< "*** Parser:loadAdjacency(): Not an Adjacency-formatted file. Aborting!!";
			file.close();		
 		 	return false;    
		}
		if ( str.contains (","))
			newCount = (str.split(",")).count();
		else  
			newCount = (str.split(" ")).count();
		qDebug() << str;
		qDebug() << "newCount "<<newCount << " i " << i;		
		if  ( (newCount != lastCount && i>1 ) || (newCount < i) ) {
			// line element count differ, therefore this can't be an adjacency matrix
			qDebug()<< "*** Parser:loadAdjacency(): Not an Adjacency-formatted file. Aborting!!";
			file.close();		
 		 	return false;    
		}

		lastCount=newCount;
			
		i++;
	}
		
	ts.reset();
	ts.seek(0);
	i=0;


	while ( !ts.atEnd() )   {
		str= ts.readLine() ;
		str=str.simplified();  // transforms "/t", "  ", etc to plain " ".
			
		if ( isComment(str) ) 
			continue; 

		if ( str.contains (","))
			lineElement=str.split(",");
		else
			lineElement=str.split(" ");
		if (i == 0 ) {
			aNodes=lineElement.count();
			qDebug("Parser-loadAdjacency(): There are %i nodes in this file", aNodes);		
			for (j=0; j<aNodes; j++) {

				randX=rand()%gwWidth;
				randY=rand()%gwHeight;
				qDebug()<<"Parser-loadAdjacency(): Calling createNode() for node "<< j+1
					<<" No coords found. Using random "<<randX <<", " << randY;

				emit createNode( j+1,initNodeSize,  initNodeColor, 
								initNodeNumberColor, initNodeNumberSize, 				
								QString::number(j+1), initNodeLabelColor, initNodeLabelSize, 
								QPointF(randX, randY), 
								initNodeShape, false
								);
			}
		}
		qDebug("Parser-loadAdjacency(): Finished creation of nodes");
		if ( aNodes != (int) lineElement.count() ) return false;	
		j=0;
		for (QStringList::Iterator it1 = lineElement.begin(); it1!=lineElement.end(); ++it1)   {
			if ( (*it1)!="0"){
				edgeWeight =(*it1).toFloat(&intOK);
				undirected=0;
				arrows=true;
				bezier=false;
				emit createEdge(i+1, j+1, edgeWeight, initEdgeColor, undirected, arrows, bezier);
				totalLinks++;
				qDebug() << "Parser-loadAdjacency(): Link from i=" << i+1 << " to j=" <<  j+1
					<< "weight=" << edgeWeight << ". TotalLinks= " << totalLinks;
			}

			j++;
		}
		i++;
	}
	file.close();

	// 0: no format, 1: GraphML, 2:Pajek, 3:Adjacency, 4: Dot, 5:DL, 6:GML, 7: List
	qDebug() << "Parser: SM network has been loaded. Tell MW the statistics and network type";
	emit fileType(3, networkName, aNodes, totalLinks, undirected);
	
	return true;
	
}




/**
	Tries to load the file as adjacency sociomatrix-formatted. If not it returns -1
*/
bool Parser::loadTwoModeSociomatrix(){
	qDebug("\n\nParser: loadTwoModeSociomatrix()");
	QFile file ( fileName );
	if ( ! file.open(QIODevice::ReadOnly ))
	    return false;
	QTextStream ts( &file );
	QString str;
	QStringList lineElement;
	int i=0, j=0,  aNodes=0, newCount=0, lastCount=0;
	edgeWeight=1.0;

	while (  !ts.atEnd() )  {
		i++;
		str= ts.readLine();
		str=str.simplified();
		if ( isComment(str) )
			continue;
		if ( str.contains("vertices",Qt::CaseInsensitive)
			|| str.contains("network",Qt::CaseInsensitive)
			|| str.contains("graph",Qt::CaseInsensitive)
			|| str.contains("digraph",Qt::CaseInsensitive)
			|| str.contains("DL",Qt::CaseInsensitive)
			|| str.contains("list",Qt::CaseInsensitive)
			|| str.contains("graphml",Qt::CaseInsensitive)
			|| str.contains("xml",Qt::CaseInsensitive)
			) {
			qDebug()<< "*** Parser:loadTwoModeSociomatrix(): Not a two mode sociomatrix-formatted file. Aborting!!";
			file.close();
			return false;
		}
		if ( str.contains (",")){
			lineElement=str.split(",");
			newCount = lineElement.count();
		}
		else {
			lineElement=str.split(" ");
			newCount = lineElement.count();
		}
		qDebug() << str;
		qDebug() << "newCount "<<newCount << " nodes. We are at i = " << i;
		if  ( (newCount != lastCount && i>1 )  ) { // line element count differ
			qDebug()<< "*** Parser:loadTwoModeSociomatrix(): Not a Sociomatrix-formatted file. Aborting!!";
			file.close();
			return false;
		}
		lastCount=newCount;
		randX=rand()%gwWidth;
		randY=rand()%gwHeight;
		qDebug()<< "Parser-loadTwoModeSociomatrix(): Calling createNode() for node "
				<< i << " at random x,y: "<<randX << randY;
		emit createNode( i,initNodeSize, initNodeColor,
				 initNodeNumberColor, initNodeNumberSize,
				 QString::number(i), initNodeLabelColor, initNodeLabelSize,
				 QPointF(randX, randY),
				 initNodeShape, false
				 );
		j=1;
		qDebug()<< "Parser-loadTwoModeSociomatrix(): reading actor affiliations...";
		for (QStringList::Iterator it1 = lineElement.begin(); it1!=lineElement.end(); ++it1)   {
			if ( (*it1)!="0"){
				qDebug() << "Parser-loadTwoModeSociomatrix(): there is an 1 from "<< i << " to "<<  j;
				firstModeMultiMap.insert(i, j);
				secondModeMultiMap.insert(j, i);
                                QList<int> values;
                                for (int k = 1; k < i ; ++k) {
				    qDebug() << "Checking earlier discovered actor k = " << k;
                                    if ( firstModeMultiMap.contains(k, j) ) {
					undirected=2;
                                        arrows=true;
                                        bezier=false;
                                        edgeWeight = 1;
					qDebug() << " Actor " << i << " on the same event as actor " << k << ". Creating edge ";
                                        emit createEdge(i, k, edgeWeight, initEdgeColor, undirected, arrows, bezier);
                                        totalLinks++;
                                    }
                                }

			}
			j++;
		}
	}
	file.close();
	// 0: no format, 1: GraphML, 2:Pajek, 3:Adjacency, 4: Dot, 5:DL, 6:GML, 7: List, 8 List, 9, TwoModeSociomatrix
	qDebug() << "Parser: Two-mode SM network has been loaded. Tell MW the statistics and network type";
	emit fileType(9, networkName, aNodes, totalLinks, undirected);

	return true;

}




/**
	Tries to load a file as GraphML (not GML) formatted network. 
	If not it returns -1
*/
bool Parser::loadGraphML(){

	qDebug("\n\nParser: loadGraphML()");
	aNodes=0;
	totalLinks=0;
	nodeNumber.clear();
	bool_key=false; bool_node=false; bool_edge=false;
	key_id = "";
	key_name = "";
	key_type = "";
	key_value = "";
	initEdgeWeight = 1;
	QFile file ( fileName );
	if ( ! file.open(QIODevice::ReadOnly )) return false;

	QXmlStreamReader *xml = new QXmlStreamReader();
	
	xml->setDevice(&file);
	
	while (!xml->atEnd()) {
		xml->readNext();
		qDebug()<< " loadGraphML(): xml->token "<< xml->tokenString();
		if (xml->isStartElement()) {
			qDebug()<< " loadGraphML(): element name "<< xml->name().toString()<<" version " << xml->attributes().value("version").toString()  ;
			if (xml->name() == "graphml") {	//this is a GraphML document, call method.
				qDebug()<< " loadGraphML(): OK. NamespaceUri is "<< xml->namespaceUri().toString();
				readGraphML(*xml);				
			}
			else {	//not a GraphML doc, return false.
				xml->raiseError(QObject::tr(" loadGraphML(): The file is not an GraphML version 1.0 file."));
				qDebug()<< "*** loadGraphML(): Error in startElement - The file is not an GraphML version 1.0 file ";
				file.close();
 				return false;
			}
		}
		else if  ( xml->tokenString() == "Invalid" ){
			xml->raiseError(QObject::tr(" loadGraphML(): The file is not an GraphML version 1.0 file."));
			qDebug()<< "*** loadGraphML(): Error in startElement - The file is not an GraphML version 1.0 file ";
			file.close();
			return false;
		}
	}

	//The network has been loaded. Tell MW the statistics and network type
	// 0: no format, 1: GraphML, 2:Pajek, 3:Adjacency, 4: Dot, 5:DL, 6:GML, 7: List
	emit fileType(1, networkName, aNodes, totalLinks, undirected);
	//clear our mess - remove every hash element...
	keyFor.clear();
	keyName.clear();
	keyType.clear(); 
	keyDefaultValue.clear();
	nodeNumber.clear();
	return true;
}


/*
 * Called from loadGraphML
 * This method checks the xml token name and calls the appropriate function.
 */
void Parser::readGraphML(QXmlStreamReader &xml){
	qDebug()<< " Parser: readGraphML()";
	bool_node=false;
	bool_edge=false;
	bool_key=false;
	//Q_ASSERT(xml.isStartElement() && xml.name() == "graph");

	while (!xml.atEnd()) { //start reading until QXmlStreamReader end().
		xml.readNext();	//read next token
	
		if (xml.isStartElement()) {	//new token (graph, node, or edge) here
			qDebug()<< "\n  readGraphML(): start of element: "<< xml.name().toString() ;
			if (xml.name() == "graph")	//graph definition token
				readGraphMLElementGraph(xml);
				
			else if (xml.name() == "key")	{//key definition token
				QXmlStreamAttributes xmlStreamAttr = xml.attributes();
				readGraphMLElementKey(  xmlStreamAttr );
			}
			else if (xml.name() == "default") //default key value token 
				readGraphMLElementDefaultValue(xml);

			else if (xml.name() == "node")	//graph definition token
				readGraphMLElementNode(xml);
				
			else if (xml.name() == "data")	//data definition token
				readGraphMLElementData(xml);
			
			else if ( xml.name() == "ShapeNode") {
				bool_node =  true;
			}			
			else if ( ( xml.name() == "Geometry"
					|| xml.name() == "Fill"
					|| xml.name() == "BorderStyle"
					|| xml.name() == "NodeLabel"
					|| xml.name() == "Shape"
				) && 	bool_node
				  ) {
				readGraphMLElementNodeGraphics(xml);
			}
			
			else if (xml.name() == "edge")	{//edge definition token
				QXmlStreamAttributes xmlStreamAttr = xml.attributes();
				readGraphMLElementEdge( xmlStreamAttr  );
			}
				
			else if ( xml.name() == "BezierEdge") {
				bool_edge =  true;
			}			

			else if (	 ( 
							xml.name() == "Path"
							|| xml.name() == "LineStyle"
							|| xml.name() == "Arrows"
							|| xml.name() == "EdgeLabel" 
						)
						&& 	bool_edge 
					) {
				readGraphMLElementEdgeGraphics(xml);
			}
			
			else
				readGraphMLElementUnknown(xml);
		}
		
		if (xml.isEndElement()) {		//token ends here
			qDebug()<< "  readGraphML():  element ends here: "<< xml.name().toString() ;
				if (xml.name() == "node")	//node definition end 
					endGraphMLElementNode(xml);
				else if (xml.name() == "edge")	//edge definition end 
					endGraphMLElementEdge(xml);
		}
	}
	
}


// this method reads a graph definition 
// called at Graph element
void Parser::readGraphMLElementGraph(QXmlStreamReader &xml){
	qDebug()<< "   Parser: readGraphMLElementGraph()";
	QXmlStreamAttributes xmlStreamAttr = xml.attributes();
	QString defaultDirection = xmlStreamAttr.value("edgedefault").toString();
	qDebug()<< "    edgedefault "<< defaultDirection;
	if (defaultDirection=="undirected"){
		undirected = 2;
	}
	else {
		undirected = 0;
	}
	networkName = xmlStreamAttr.value("id").toString();
	qDebug()<< "    graph id  "  << networkName; //store graph id to return it afterwards 
}



// this method is needed because the QXmlStreamReader::hasAttribute
// has been implemented in Qt 4.5. Therefore we need this ugly hack to 
// be able to compile SocNetV in all previous Qt4 version. :(
//FIXME: This will be obsolete soon
bool Parser::xmlStreamHasAttribute( QXmlStreamAttributes &xmlStreamAttr, QString str) const
{
	int size = xmlStreamAttr.size();
	for (register int  i = 0 ; i < size ; i++) {
		qDebug() << "		xmlStreamHasAttribute(): " << xmlStreamAttr.at(i).name().toString() << endl;
		if ( xmlStreamAttr.at(i).name() == str) 
			return true;
	}
	return false;	
}



// this method reads a key definition 
// called at key element
void Parser::readGraphMLElementKey ( QXmlStreamAttributes &xmlStreamAttr )
{
	qDebug()<< "   Parser: readGraphMLElementKey()";
	key_id = xmlStreamAttr.value("id").toString();
 	qDebug()<< "    key id "<< key_id;
	key_what = xmlStreamAttr.value("for").toString();
	keyFor [key_id] = key_what;
	qDebug()<< "    key for "<< key_what;

	// if (xmlStreamAttr.hasAttribute("attr.name") ) {  // to be enabled in later versions..	
	if ( xmlStreamHasAttribute( xmlStreamAttr , QString ("attr.name") ) ) {
		key_name =xmlStreamAttr.value("attr.name").toString();
		keyName [key_id] = key_name;
		qDebug()<< "    key attr.name "<< key_name;		
	}
	//if (xmlStreamAttr.hasAttribute("attr.type") ) {
	if ( xmlStreamHasAttribute( xmlStreamAttr , QString ("attr.type") ) ) {		
		key_type=xmlStreamAttr.value("attr.type").toString();
		keyType [key_id] = key_type;
		qDebug()<< "    key attr.type "<< key_type;
	}
	//else if (xmlStreamAttr.hasAttribute("yfiles.type") ) {
	else if ( xmlStreamHasAttribute( xmlStreamAttr , QString ("yfiles.type") ) ) {
		key_type=xmlStreamAttr.value("yfiles.type").toString();
		keyType [key_id] = key_type;
		qDebug()<< "    key yfiles.type "<< key_type;
	}

}


// this method reads default key values 
// called at a default element (usually nested inside key element)
void Parser::readGraphMLElementDefaultValue(QXmlStreamReader &xml) {
	qDebug()<< "   Parser: readGraphMLElementDefaultValue()";

	key_value=xml.readElementText();
	keyDefaultValue [key_id] = key_value;	//key_id is already stored 
	qDebug()<< "    key default value is "<< key_value;
	if (keyName.value(key_id) == "size" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "    this key default value "<< key_value << " is for node size";
			conv_OK=false;
			initNodeSize= key_value.toInt(&conv_OK);
			if (!conv_OK) initNodeSize = 8;  
	}	
	if (keyName.value(key_id) == "shape" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "    this key default value "<< key_value << " is for nodes shape";
			initNodeShape= key_value; 
	}
	if (keyName.value(key_id) == "color" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "    this key default value "<< key_value << " is for nodes color";
			initNodeColor= key_value; 
	}
	if (keyName.value(key_id) == "label.color" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "    this key default value "<< key_value << " is for node labels color";
			initNodeLabelColor= key_value; 
	}
	if (keyName.value(key_id) == "label.size" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "    this key default value "<< key_value << " is for node labels size";
			conv_OK=false;
			initNodeLabelSize= key_value.toInt(&conv_OK);
			if (!conv_OK) initNodeLabelSize = 8;  
	}	
	if (keyName.value(key_id) == "weight" && keyFor.value(key_id) == "edge" ) {
			qDebug()<< "    this key default value "<< key_value << " is for edges weight";
			conv_OK=false;
			initEdgeWeight= key_value.toFloat(&conv_OK);
			if (!conv_OK) initEdgeWeight = 1;  
	}
	if (keyName.value(key_id) == "color" && keyFor.value(key_id) == "edge" ) {
			qDebug()<< "    this key default value "<< key_value << " is for edges color";
			initEdgeColor= key_value; 
	}

}



// this method reads basic node attributes and sets the nodeNumber.
// called at the start of a node element
void Parser::readGraphMLElementNode(QXmlStreamReader &xml){
	QXmlStreamAttributes xmlStreamAttr = xml.attributes();
	node_id = (xmlStreamAttr.value("id")).toString();
	aNodes++;
	qDebug()<<"   Parser: readGraphMLElementNode() node id "<<  node_id << " index " << aNodes << " added to nodeNumber map";

	nodeNumber[node_id]=aNodes;

	//copy default node attribute values. 
	//Some might change when reading element data, some will stay the same...   	
	nodeColor = initNodeColor;
	nodeShape = initNodeShape;
	nodeSize = initNodeSize;
	nodeNumberSize=initNodeNumberSize;
	nodeNumberColor=initNodeNumberColor;
	nodeLabel = node_id;
	nodeLabelSize=initNodeLabelSize;
	nodeLabelColor=initNodeLabelColor;
	bool_node = true;
	randX=rand()%gwWidth;
	randY=rand()%gwHeight;

}


// this method emits the node creation signal.
// called at the end of a node element   
void Parser::endGraphMLElementNode(QXmlStreamReader &xml){
	Q_UNUSED(xml);
	
	qDebug()<<"   Parser: endGraphMLElementNode() *** signal to create node with id "
		<< node_id << " nodenumber "<< aNodes << " coords " << randX << ", " << randY;
	emit createNode(
					aNodes, nodeSize, nodeColor,
					nodeNumberColor, nodeNumberSize,  
					nodeLabel, nodeLabelColor, nodeLabelSize, 
					QPointF(randX,randY), 
					nodeShape, false
					);
	bool_node = false;
	
}


// this method reads basic edge creation properties.
// called at the start of an edge element
void Parser::readGraphMLElementEdge(QXmlStreamAttributes &xmlStreamAttr){
	qDebug()<< "   Parser: readGraphMLElementEdge() id: " <<	xmlStreamAttr.value("id").toString();
	QString s = xmlStreamAttr.value("source").toString();
	QString t = xmlStreamAttr.value("target").toString();
	if ( (xmlStreamAttr.value("directed")).toString() == "false") 
		undirected = 2;
	source = nodeNumber [s];
	target = nodeNumber [t];
	edgeWeight=initEdgeWeight;
	bool_edge= true;
	qDebug()<< "    edge source "<< s << " num "<< source;
	qDebug()<< "    edge target "<< t << " num "<< target;

	
}


// this method emits the edge creation signal.
// called at the end of edge element   
void Parser::endGraphMLElementEdge(QXmlStreamReader &xml){
	Q_UNUSED(xml);
	qDebug()<<"   Parser: endGraphMLElementEdge() *** emitting signal to create edge from "<< source << " to " << target;
	//FIXME need to return edge label as well!
	emit createEdge(source, target, edgeWeight, edgeColor, undirected, arrows, bezier);
	totalLinks++;
	bool_edge= false;
}


/*
 * this method reads data for edges and nodes 
 * called at a data element (usually nested inside a node an edge element) 
 */ 
void Parser::readGraphMLElementData (QXmlStreamReader &xml){
	QXmlStreamAttributes xmlStreamAttr = xml.attributes();
	key_id = xmlStreamAttr.value("key").toString();
	key_value=xml.text().toString();
	qDebug()<< "   Parser: readGraphMLElementData(): key_id: " <<  key_id <<  " key_value "<< key_value;
	if (key_value.trimmed() == "") 
	{
		qDebug()<< "   Parser: readGraphMLElementData(): text: " << key_value;
		xml.readNext();
		key_value=xml.text().toString();
		qDebug()<< "   Parser: readGraphMLElementData(): text: " << key_value; 
		if (  key_value.trimmed() != "" ) { //if there's simple text after the StartElement,
				qDebug()<< "   Parser: readGraphMLElementData(): key_id " << key_id 
						<< " value is simple text " <<key_value ;
		}
		else {  //no text, probably more tags. Return...
			qDebug()<< "   Parser: readGraphMLElementData(): key_id " << key_id 
							<< " for " <<keyFor.value(key_id) << ". More elements nested here, continuing";
			return;  
		}
		
	}
	
	if (keyName.value(key_id) == "color" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "     Data found. Node color: "<< key_value << " for this node";
			nodeColor= key_value; 
	}
	else if (keyName.value(key_id) == "label" && keyFor.value(key_id) == "node" ){
		 	qDebug()<< "     Data found. Node label: "<< key_value << " for this node";
		 	nodeLabel = key_value;
	}
	else if (keyName.value(key_id) == "x_coordinate" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "     Data found. Node x: "<< key_value << " for this node";
			conv_OK=false;
			randX= key_value.toFloat( &conv_OK ) ;
			if (!conv_OK)
				randX = 0; 
			else 
				randX=randX * gwWidth;
			qDebug()<< "     Using: "<< randX;
	}
	else if (keyName.value(key_id) == "y_coordinate" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "     Data found. Node y: "<< key_value << " for this node"; 
			conv_OK=false;
			randY= key_value.toFloat( &conv_OK );
			if (!conv_OK)
				randY = 0;  
			else 
				randY=randY * gwHeight;	
			qDebug()<< "     Using: "<< randY;
	}
	else if (keyName.value(key_id) == "size" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "     Data found. Node size: "<< key_value << " for this node"; 
			conv_OK=false;
			nodeSize= key_value.toInt ( &conv_OK );
			if (!conv_OK)
				nodeSize = initNodeSize;  
			qDebug()<< "     Using: "<< nodeSize;
	}
	else if (keyName.value(key_id) == "label.size" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "     Data found. Node label size: "<< key_value << " for this node"; 
			conv_OK=false;
			nodeLabelSize= key_value.toInt ( &conv_OK );
			if (!conv_OK)
				nodeLabelSize = initNodeLabelSize;  
			qDebug()<< "     Using: "<< nodeSize;
	}
	else if (keyName.value(key_id) == "label.color" && keyFor.value(key_id) == "node" ){
		 	qDebug()<< "     Data found. Node label Color: "<< key_value << " for this node";
		 	nodeLabelColor = key_value;
	}
	else if (keyName.value(key_id) == "shape" && keyFor.value(key_id) == "node" ) {
			qDebug()<< "     Data found. Node shape: "<< key_value << " for this node";
			nodeShape= key_value; 
	}	
	else if (keyName.value(key_id) == "color" && keyFor.value(key_id) == "edge" ) {
			qDebug()<< "     Data found. Edge color: "<< key_value << " for this edge";
			edgeColor= key_value; 
	}
	else if ( ( keyName.value(key_id) == "value" ||  keyName.value(key_id) == "weight" ) && keyFor.value(key_id) == "edge" ) {
			conv_OK=false;
			edgeWeight= key_value.toFloat( &conv_OK );
			if (!conv_OK) 
				edgeWeight = 1.0;  	
 			qDebug()<< "     Data found. Edge value: "<< key_value << " Using "<< edgeWeight << " for this edge";       
	}
	else if ( keyName.value(key_id) == "size of arrow"  && keyFor.value(key_id) == "edge" ) {
			conv_OK=false;
			float temp = key_value.toFloat( &conv_OK );
			if (!conv_OK) arrowSize = 1;
			else  arrowSize = temp;
			qDebug()<< "     Data found. Edge arrow size: "<< key_value << " Using  "<< arrowSize << " for this edge";
	}


	
}



/**
 * 	Reads node graphics data and properties: label, color, shape, size, coordinates, etc.
 */
void Parser::readGraphMLElementNodeGraphics(QXmlStreamReader &xml) {
	qDebug()<< "       Parser: readGraphMLElementNodeGraphics(): element name "<< xml.name().toString();
	float tempX =-1, tempY=-1, temp=-1;
	QString color;
	QXmlStreamAttributes xmlStreamAttr = xml.attributes();
	
	if ( xml.name() == "Geometry" ) {

			if ( xmlStreamHasAttribute ( xmlStreamAttr, "x") ) {
				conv_OK=false;
				tempX = xml.attributes().value("x").toString().toFloat (&conv_OK) ;
				if (conv_OK) 
					randX = tempX;	
			}
			if ( xmlStreamHasAttribute ( xmlStreamAttr, "y") ) {
				conv_OK=false;
				tempY = xml.attributes().value("y").toString().toFloat (&conv_OK) ;
				if (conv_OK)
					randY = tempY;
			}
			qDebug()<< "        Node Coordinates: " << tempX << " " << tempY << " Using coordinates" << randX<< " "<<randY;
			if (xmlStreamHasAttribute ( xmlStreamAttr, "width") ) {
				conv_OK=false;
				temp = xmlStreamAttr.value("width").toString().toFloat (&conv_OK) ;
				if (conv_OK)
					nodeSize = temp;
				qDebug()<< "        Node Size: " << temp<< " Using nodesize" << nodeSize;
			}
			if (xmlStreamHasAttribute ( xmlStreamAttr, "shape") ) {
				nodeShape = xmlStreamAttr.value("shape").toString();
				qDebug()<< "        Node Shape: " << nodeShape;
			}

	}
	else if (xml.name() == "Fill" ){
		if ( xmlStreamHasAttribute ( xmlStreamAttr, "color") ) {
			nodeColor= xmlStreamAttr.value("color").toString();	
			qDebug()<< "        Node color: " << nodeColor;
		}
		
	}
	else if ( xml.name() == "BorderStyle" ) {
		
		
	}
	else if (xml.name() == "NodeLabel" ) {
		key_value=xml.readElementText();  //see if there's simple text after the StartElement
		if (!xml.hasError()) {
			qDebug()<< "         Node Label "<< key_value;		
			nodeLabel = key_value;
		}
		else {
			qDebug()<< "         Can't read Node Label. There must be more elements nested here, continuing";  
		}
	}
	else if (xml.name() == "Shape" ) {
		if ( xmlStreamHasAttribute ( xmlStreamAttr, "type") ) {
			nodeShape= xmlStreamAttr.value("type").toString();	
			qDebug()<< "        Node shape: " << nodeShape;
		}
	
	}
		 

}

void Parser::readGraphMLElementEdgeGraphics(QXmlStreamReader &xml) {
	qDebug()<< "       Parser: readGraphMLElementEdgeGraphics() element name "<< xml.name().toString();

	float tempX =-1, tempY=-1, temp=-1;
	QString color, tempString;
	QXmlStreamAttributes xmlStreamAttr = xml.attributes();
	
	if ( xml.name() == "Path" ) {
			//if ( xmlStreamAttr.hasAttribute("sx") ) {
			if ( xmlStreamHasAttribute ( xmlStreamAttr, "sx") ) {
				conv_OK=false;
				tempX = xmlStreamAttr.value("sx").toString().toFloat (&conv_OK) ;
				if (conv_OK) 
					bez_p1_x = tempX;
				else bez_p1_x = 0 ;
			}
			//if ( xmlStreamAttr.hasAttribute("sy") ) {
			if ( xmlStreamHasAttribute ( xmlStreamAttr, "sy") ) {
				conv_OK=false;
				tempY = xmlStreamAttr.value("sy").toString().toFloat (&conv_OK) ;
				if (conv_OK)
					bez_p1_y = tempY;
				else bez_p1_y = 0 ;
			}
			//if ( xmlStreamAttr.hasAttribute("tx") ) {
			if ( xmlStreamHasAttribute ( xmlStreamAttr, "tx") ) {
				conv_OK=false;
				tempX = xmlStreamAttr.value("tx").toString().toFloat (&conv_OK) ;
				if (conv_OK) 
					bez_p2_x = tempX;
				else bez_p2_x = 0 ;
			}
			//if ( xmlStreamAttr.hasAttribute("ty") ) {
			if ( xmlStreamHasAttribute ( xmlStreamAttr, "ty") ) {
				conv_OK=false;
				tempY = xmlStreamAttr.value("ty").toString().toFloat (&conv_OK) ;
				if (conv_OK)
					bez_p2_y = tempY;
				else bez_p2_y = 0 ;
			}
			qDebug()<< "        Edge Path control points: " << bez_p1_x << " " << bez_p1_y << " " << bez_p2_x << " " << bez_p2_y;
	}
	else if (xml.name() == "LineStyle" ){
		if ( xmlStreamHasAttribute ( xmlStreamAttr, "color") ) {
			edgeColor= xmlStreamAttr.value("color").toString();	
			qDebug()<< "        Edge color: " << edgeColor;
		}
		if ( xmlStreamHasAttribute ( xmlStreamAttr, "type") ) {
			edgeType= xmlStreamAttr.value("type").toString();	
			qDebug()<< "        Edge type: " << edgeType;
		}
		if ( xmlStreamHasAttribute ( xmlStreamAttr, "width") ) {
			temp = xmlStreamAttr.value("width").toString().toFloat (&conv_OK) ;
			if (conv_OK)
				edgeWeight = temp;
			else 
				edgeWeight=1.0;
			qDebug()<< "        Edge width: " << edgeWeight;
		}
		
	}
	else if ( xml.name() == "Arrows" ) {
		if ( xmlStreamHasAttribute ( xmlStreamAttr, "source") ) {
			tempString = xmlStreamAttr.value("source").toString();	
			qDebug()<< "        Edge source arrow type: " << tempString;
		}
		if ( xmlStreamHasAttribute ( xmlStreamAttr, "target") ) {
			tempString = xmlStreamAttr.value("target").toString();	
			qDebug()<< "        Edge target arrow type: " << tempString;
		}

	
		
	}
	else if (xml.name() == "EdgeLabel" ) {
		key_value=xml.readElementText();  //see if there's simple text after the StartElement
		if (!xml.hasError()) {
			qDebug()<< "         Edge Label "<< key_value;		
			//probably there's more than simple text after StartElement
			edgeLabel = key_value;
		}
		else {
			qDebug()<< "         Can't read Edge Label. More elements nested ? Continuing with blank edge label....";
			edgeLabel = "" ;  
		}
	}
		 


}


void Parser::readGraphMLElementUnknown(QXmlStreamReader &xml) {
	qDebug()<< "Parser: readGraphMLElementUnknown()";
    Q_ASSERT(xml.isStartElement());
	qDebug()<< "   "<< xml.name().toString() ;
}





/**
	Tries to load a file as GML formatted network. If not it returns -1
*/
bool Parser::loadGML(){

	qDebug("\n\nParser: loadGML()");
	QFile file ( fileName );
	QString str, temp;
	int fileLine=0, start=0, end=0;
	Q_UNUSED(start);
	Q_UNUSED(end);

	if ( ! file.open(QIODevice::ReadOnly )) return false;
	QTextStream ts( &file );
	while (!ts.atEnd() )   {
		str= ts.readLine() ;
		fileLine++;
		qDebug ()<<"Reading fileLine "<< fileLine;
		if ( fileLine == 1 ) {
			qDebug ()<<"Reading fileLine = "<< fileLine;
			if ( !str.startsWith("graph", Qt::CaseInsensitive) ) {
				qDebug() << "*** Parser:loadGML(): Not an GML-formatted file. Aborting";
				file.close();
				return false;  
			}

		}
		if ( str.startsWith("directed",Qt::CaseInsensitive) ) { 	 //key declarations
		}
		else if ( str.startsWith("id",Qt::CaseInsensitive) ) { 	 
		}
		else if ( str.startsWith("label",Qt::CaseInsensitive) ) { 	 
		}
		else if ( str.startsWith("node",Qt::CaseInsensitive) ) { 	 //node declarations
		}
		else if ( str.startsWith("edge",Qt::CaseInsensitive) ) { 	 //edge declarations
		}
	}
	//The network has been loaded. Tell MW the statistics and network type
	// 0: no format, 1: GraphML, 2:Pajek, 3:Adjacency, 4: Dot, 5:DL, 6:GML, 7: List
	emit fileType(6, networkName, aNodes, totalLinks, undirected);
	qDebug() << "Parser-loadGML()";
	return true;
}


/**
	Tries to load the file as Dot (Graphviz) formatted network. If not it returns -1
*/
bool Parser::loadDot(){

	qDebug("\n\nParser: loadDotNetwork");
	int fileLine=0, aNum=-1;
	int start=0, end=0, next=0;
	QString label, node, nodeLabel, fontName, fontColor, edgeShape, edgeColor, edgeLabel, networkLabel;
	QString str, temp, prop, value ;
	QStringList lineElement;
	nodeColor="red"; 
	edgeColor="black";
	nodeShape="";
	edgeWeight=1.0;
	float nodeValue=1.0;
	QStringList labels;
	QList<QString> nodeSequence;   //holds edges
	QList<QString> nodesDiscovered; //holds nodes;
		
	undirected=0; arrows=TRUE; bezier=FALSE;
	source=0, target=0;
	QFile file ( fileName );
	if ( ! file.open(QIODevice::ReadOnly )) return false;
	QTextStream ts( &file );
	aNodes=0;
	while (!ts.atEnd() )   {
		str= ts.readLine() ;
		str=str.simplified();
		str=str.trimmed();

		if ( isComment (str) ) 
			continue;

		fileLine++;
		qDebug ()<<"Reading fileLine "<< fileLine;

		if ( fileLine == 1 ) {
			if ( str.contains("vertices",Qt::CaseInsensitive) 	//Pajek
				|| str.contains("network",Qt::CaseInsensitive)	//Pajek?
				|| str.contains("[",Qt::CaseInsensitive)    	// GML
				|| str.contains("DL",Qt::CaseInsensitive) 		//DL format
				|| str.contains("list",Qt::CaseInsensitive)		//list 
				|| str.startsWith("<graphml",Qt::CaseInsensitive)  // GraphML
				|| str.startsWith("<?xml",Qt::CaseInsensitive)
				) {
				qDebug() << "*** Parser:loadDot(): Not an GraphViz -formatted file. Aborting";
				file.close();				
				return false;
			}

			if ( str.contains("digraph", Qt::CaseInsensitive) ) {
				lineElement=str.split(" ");
				if (lineElement[1]!="{" ) networkName=lineElement[1];
				qDebug() << "This is a DOT DIGRAPH named " << networkName;
				continue; 
			}
			else if ( str.contains("graph", Qt::CaseInsensitive) ) {
				lineElement=str.split(" ");
				if (lineElement[1] !="{" ) networkName=lineElement[1];
				qDebug() << "This is a DOT GRAPH named " << networkName;
				continue;
			}
			else {
				qDebug()<<" *** Parser:loadDot(): Not a GraphViz file. Abort: dot format can only start with \" (di)graph netname {\"";
				return false;  				
			}
		}

		if ( str.startsWith("label",Qt::CaseInsensitive)
			|| str.startsWith("mincross",Qt::CaseInsensitive)
			|| str.startsWith("ratio",Qt::CaseInsensitive) 
			) { 	 //Default network properties
			next=str.indexOf('=', 1);
			qDebug("Found next = at %i. Start is at %i", next, 1);
			prop=str.mid(0, next).simplified();	
			qDebug()<<"Prop: "<<prop.toAscii() ;
			value=str.right(str.count()-next-1).simplified();
			qDebug() << "Value "<< value;
			if ( prop == "label" ){
				networkLabel= value;
			}
			else if ( prop == "ratio" ){
				
			}
			else if ( prop == "mincross" ){
				
			}

		}
		else if ( str.startsWith("node",Qt::CaseInsensitive) ) { 	 //Default node properties
			qDebug() << "* Node properties found...";
			start=str.indexOf('[');
			end=str.indexOf(']');
			temp=str.right(str.size()-end-1);
			str=str.mid(start+1, end-start-1);  //keep whatever is inside [ and ]
			qDebug()<<"Properties start at "<< start<< " and end at "<< end;
			qDebug()<<str.toAscii();
			str=str.simplified();
			qDebug()<<str.toAscii();
			start=0;
			end=str.count();
			dotProperties(str, nodeValue, nodeLabel, initNodeShape, initNodeColor, fontName, fontColor );
			qDebug ("* Finished NODE PROPERTIES");
		}
		else if ( str.startsWith("edge",Qt::CaseInsensitive) ) { //Default edge properties
			qDebug("* Edge properties found...");
			start=str.indexOf('[');
			end=str.indexOf(']');
			str=str.mid(start+1, end-start-1);  //keep whatever is inside [ and ]
			qDebug()<<"Properties start at "<< start <<" and end at "<< end;
			qDebug()<<str.toAscii();
			str=str.simplified();
			qDebug()<<str.toAscii();
			start=0;
			end=str.count();
			qDebug ("* Finished EDGE PROPERTIES!");
		}
		//ti ginetai an exeis mesa sxolia ? p.x. sto telos tis grammis //
		else if ( !str.startsWith('[', Qt::CaseInsensitive) 
					&& !str.contains("--",Qt::CaseInsensitive)
					&& !str.contains("->",Qt::CaseInsensitive) 
					) {
				qDebug()<< "* A node definition must be here ..." << str;
				end=str.indexOf('['); 
				if (end!=-1) {
					temp=str.right(str.size()-end-1); //keep the properties
					temp=temp.remove(']');
					temp=temp.remove(';');
					node=str.left(end-1);
					node=node.remove('\"');
					qDebug()<<"node named "<<node.toAscii();
					qDebug()<<"node properties "<<temp.toAscii();
					nodeLabel=node;  //Will change only if label exists in dotProperties
					dotProperties(temp, nodeValue, nodeLabel, initNodeShape, initNodeColor, fontName, fontColor );
					if (nodeLabel=="") nodeLabel=node;
					aNodes++;
					randX=rand()%gwWidth;
					randY=rand()%gwHeight;
					qDebug()<<" *** Creating node "<< aNodes 
							<< " at "<< randX <<","<< randY
							<<" label "<<node.toAscii() 
							<< " colored "<< initNodeColor
							<< "initNodeSize " << initNodeSize
							<< "initNodeNumberColor " <<initNodeNumberColor
							<< "initNodeNumberSize " << initNodeNumberSize
							<< "initNodeLabelColor " << initNodeLabelColor
							<< "nodeShape" <<  initNodeShape; 
					emit createNode(
									aNodes, initNodeSize, initNodeColor,
									initNodeNumberColor, initNodeNumberSize,  
									nodeLabel , initNodeLabelColor, initNodeLabelSize,
									QPointF(randX,randY), 
									initNodeShape, false
									);
					nodesDiscovered.push_back( node  );			// Note that we push the numbered nodelabel whereas we create the node with its file specified node label.  
					qDebug()<<" * Total aNodes " << aNodes<< " nodesDiscovered  "<< nodesDiscovered.size() ;
					target=aNodes;
					
				}
				else 
					end=str.indexOf(';');
				qDebug ("* Finished node!");
		}
		else if (str.contains('-',Qt::CaseInsensitive)) {  
			qDebug("* Edge definition found ...");
			end=str.indexOf('[');
			if (end!=-1) {
				temp=str.right(str.size()-end-1); //keep the properties
				temp=temp.remove(']');
				temp=temp.remove(';');
				qDebug()<<"edge properties "<<temp.toAscii();
				dotProperties(temp, edgeWeight, edgeLabel, edgeShape, edgeColor, fontName, fontColor );
			}
			else 
				end=str.indexOf(';');
				
			//FIXME Cannot parse nodes named with '-' character
			str=str.mid(0, end).remove('\"');  //keep only edges

			qDebug()<<"edges "<<str.toAscii();
			
			if (!str.contains("->",Qt::CaseInsensitive) ){  //non directed = symmetric links
				if ( str.contains("--",Qt::CaseInsensitive) ) 
						nodeSequence=str.split("--");
				else 
						nodeSequence=str.split("-");
			}
			else { 											//is directed
				nodeSequence=str.split("->");
			}
			//Create all nodes defined in nodeSequence
			for ( QList<QString>::iterator it=nodeSequence.begin(); it!=nodeSequence.end(); it++ )  {
				node=(*it).simplified();
				qDebug () << " nodeSequence node "<< node;
				if ( (aNum=nodesDiscovered.indexOf( node ) ) == -1) {
					aNodes++;
					randX=rand()%gwWidth;
					randY=rand()%gwHeight;
					qDebug()<<" *** Creating node "<< aNodes 
							<< " at "<< randX <<","<< randY
							<<" label "<<node.toAscii() 
							<< " colored "<< nodeColor
							<< "initNodeSize " << initNodeSize
							<< "initNodeNumberColor " <<initNodeNumberColor
							<< "initNodeNumberSize " << initNodeNumberSize
							<< "initNodeLabelColor " << initNodeLabelColor
							<< "nodeShape" <<  initNodeShape; 
					emit createNode(
									aNodes, initNodeSize, nodeColor,
									initNodeNumberColor, initNodeNumberSize,  
									node , initNodeLabelColor, initNodeLabelSize,
									QPointF(randX,randY), 
									initNodeShape, false
									);
					nodesDiscovered.push_back( node  );
					qDebug()<<" * Total aNodes " << aNodes<< " nodesDiscovered  "<< nodesDiscovered.size() ;
					target=aNodes;
					if (it!=nodeSequence.begin()) {
						qDebug()<<"-- Drawing Link between node "<< source<< " and node " <<target;
						emit createEdge(source,target, edgeWeight, edgeColor, undirected, arrows, bezier);
					}
				}
				else {
					target=aNum+1;
					qDebug("# Node already exists. Vector num: %i ",target);
					if (it!=nodeSequence.begin()) {
						qDebug()<<"-- Drawing Link between node "<<source<<" and node " << target;
						emit createEdge(source,target, edgeWeight , edgeColor, undirected, arrows, bezier);
					}
				}
				source=target;
			}
			nodeSequence.clear();
			qDebug("Finished reading fileLine %i ",fileLine);
		}
		else if ( str.contains ("[",Qt::CaseInsensitive) ) { 	 //Default node properties - no node keyword
			qDebug("* Node properties found but with no Node keyword in the beginning!");
			start=str.indexOf('[');
			end=str.indexOf(']');
			temp=str.mid(start+1, end-start-1);  //keep whatever is inside [ and ]
			qDebug()<<"Properties start at "<< start<< " and end at " << end;
			temp=temp.simplified();
			qDebug()<<temp.toAscii();
			dotProperties(temp, nodeValue, label, nodeShape, nodeColor, fontName, fontColor );
			qDebug ("Finished the properties!");

			if (start > 2 ) {//there is a node definition here
				node=str.left(start).remove('\"').simplified();
				qDebug()<<"node label: "<<node.toAscii()<<"." ;
				if (!nodesDiscovered.contains(node)) {
					qDebug("not discovered node"); 
					aNodes++;
					randX=rand()%gwWidth;
					randY=rand()%gwHeight;
					qDebug()<<"***  Creating node at "<<  randX << " "<< randY<< " label "<<node.toAscii() << " colored "<< nodeColor; 
					emit createNode(
									aNodes, initNodeSize, nodeColor,
									initNodeNumberColor, initNodeNumberSize,  
									label, initNodeLabelColor, initNodeLabelSize, 
									QPointF(randX,randY),
									nodeShape, false
									);
					aNum=aNodes;
					nodesDiscovered.push_back( node);
					qDebug()<<" Total aNodes: "<<  aNodes<< " nodesDiscovered = "<< nodesDiscovered.size();
				}
				else {
					qDebug("discovered node - skipping it!");
				}
			}
		}
		else {
			qDebug() <<  "  Redudant data: "<< str.toAscii();
		}
	}
  	file.close();
	//The network has been loaded. Tell MW the statistics and network type
	// 0: no format, 1: GraphML, 2:Pajek, 3:Adjacency, 4: Dot, 5:DL, 6:GML, 7: List
	emit fileType(4, networkName, aNodes, totalLinks, undirected);
	return true;
}





void Parser::dotProperties(QString str, float &nValue, QString &label, QString &shape, QString &color, QString &fontName, QString &fontColor ){
	int next=0;
	QString prop, value;
	
	//FIXME Implement a qstringlist here splitted from str in ,	
	bool ok=FALSE;
			do  {		//stops when it passes the index of ']'
				next=str.indexOf('=', 1);
				qDebug("Found next = at %i. Start is at %i", next, 1);
				prop=str.mid(0, next).simplified();	
				qDebug()<<"Prop: "<<prop.toAscii() ;
				str=str.right(str.count()-next-1).simplified();
				if (str.startsWith(",")) 
					str=str.right(str.count()-1).simplified();

				qDebug()<<"whatsleft: "<<str.toAscii() ;

					
				if ( str.indexOf('\"') == 0) {
					qDebug("found text, parsing...");
					next=str.indexOf('\"', 1);
					value=str.left(next).simplified().remove('\"');
					
					if (prop=="label") {
						label=value.trimmed();
						qDebug()<<"Found label "<<value.toAscii() <<". Assigned label "<<label.toAscii();
					}
					else if (prop=="fontname"){
						qDebug()<<"Found fontname"<<value.toAscii();
						fontName=value.trimmed();
					}
					str=str.right(str.count()-next-1).simplified();
					qDebug()<<"whatsleft: "<<str.toAscii() <<".";
				}
				else {
					if (str.isEmpty()) break;
					if ( str.contains(',') ){
						next=str.indexOf(',');
						value=str.mid(0, next).simplified();
					}

					else if ( str.contains(' ') ){
						next=str.indexOf(' ');
						value=str.mid(0, next).simplified();
					}
					else		{
						value=str;
					}
					
					qDebug()<<"Prop Value: "<<value.toAscii() ;
					if (prop=="value") {
						qDebug()<<"Found value "<<value.toAscii();
						nValue=value.trimmed().toFloat(&ok);
						if ( ok ) 
							qDebug()<<"Assigned value "<<nValue;
						else 
							 qDebug()<<"Error in value conversion ";
					}
					else if (prop=="color") {
						color=value.trimmed();
						qDebug()<<"Found color "<<value.toAscii()  <<". Assigned color "<<color.toAscii()<<".";
					}
					else if (prop=="fillcolor") {
						qDebug()<<"Found color "<<value.toAscii();
						color=value.trimmed();
						qDebug()<<"Assigned node color "<<color.toAscii()<<".";
					}

					else if (prop=="fontcolor") {
						qDebug()<<"Found fontcolor "<<value.toAscii();
						fontColor=value.trimmed();
					}
					else if (prop=="shape") {
						shape=value.trimmed();
						qDebug()<<"Found node shape "<<shape.toAscii();
					}
					qDebug()<<"count"<< str.count()<<  " next "<< next;
					str=str.right(str.count()-next-1).simplified();
					qDebug()<<"whatsleft: "<<str.toAscii()<<".";
					if ( (next=str.indexOf('=', 1))==-1) break;
				}
			} while (!str.isEmpty());

}




bool Parser::loadWeighedList(){
	qDebug() << "Parser: loadWeighedList()";
	QFile file ( fileName );
	if ( ! file.open(QIODevice::ReadOnly ))
		return false;
	QTextStream ts( &file );
	QString str;
	QStringList lineElement;
	int  j=0,  source=0, target=0, newCount=0,  maxNodeCreated=0;
	bool intOK=false;

	edgeWeight=1.0;
	undirected=0;
	arrows=true;
	bezier=false;

	while ( !ts.atEnd() )   {
		str= ts.readLine() ;
		qDebug()<< " str " << str;
		str=str.simplified();

		if ( isComment(str) )
			continue;

		if ( str.contains("vertices",Qt::CaseInsensitive)
			|| str.contains("network",Qt::CaseInsensitive)
			|| str.contains("graph",Qt::CaseInsensitive)
			|| str.contains("digraph",Qt::CaseInsensitive)
			|| str.contains("DL",Qt::CaseInsensitive)
			|| str.contains("list",Qt::CaseInsensitive)
			|| str.contains("graphml",Qt::CaseInsensitive)
			|| str.contains("xml",Qt::CaseInsensitive)
			) {
			qDebug()<< "*** Parser:loadWeighedList(): Not a Weighted list-formatted file. Aborting!!";
			file.close();
			return false;
		}

		lineElement=str.split(" ");
		newCount = lineElement.count();

		if ( newCount != 3 ) {
			qDebug()<< "*** Parser:loadWeighedList(): Not a Weighted list-formatted file. Aborting!!";
			file.close();
			return false;
		}

		source =  (lineElement[0]).toInt(&intOK);
		target =  (lineElement[1]).toInt(&intOK);
		qDebug() << "	source node " << source  << " target node " << target;

		edgeWeight=(lineElement[2]).toDouble(&intOK);
		if (intOK) {
			qDebug () << "	list file declares edge weight: " << edgeWeight;
		}
		else {
			edgeWeight=1.0;
			qDebug () << "	list file NOT declaring edge weight. Setting default: " << edgeWeight;
		}
		if (maxNodeCreated < source ) {
			for ( j = maxNodeCreated ; j != source ; j++ ) {
				randX=rand()%gwWidth;
				randY=rand()%gwHeight;
				qDebug()<< "	source node " << source
						<< "is less than maxNodeCreated - creating node "<< j+1
						<< "using random coords "<<randX << " "<< randY;
				emit createNode( j+1, initNodeSize,  initNodeColor,
						 initNodeNumberColor, initNodeNumberSize,
						 QString::number(j+1), initNodeLabelColor, initNodeLabelSize,
						 QPointF(randX, randY),
						 initNodeShape, false
						 );
			}
			maxNodeCreated = source ;
		}
		if (maxNodeCreated < target ) {
			for ( j = maxNodeCreated ; j != target; j++ ) {
				randX=rand()%gwWidth;
				randY=rand()%gwHeight;
				qDebug()<< "	target node " << target
						<< "is less than maxNodeCreated - creating node "<< j+1
						<<" using random coords "<<randX << " "<< randY;
				emit createNode( j+1, initNodeSize,  initNodeColor,
						 initNodeNumberColor, initNodeNumberSize,
						 QString::number(j+1), initNodeLabelColor, initNodeLabelSize,
						 QPointF(randX, randY),
						 initNodeShape, false
						 );
			}
			maxNodeCreated = target ;
		}
		qDebug()<< "	Parser-loadWeighedList(): Creating link now... "
			<< " link from i= " << source << " to j= "<< target << " weight= "<< edgeWeight <<  " TotalLinks=  " << totalLinks+1;
		emit createEdge(source, target, edgeWeight, initEdgeColor, undirected, arrows, bezier);
		totalLinks++;
	} //end ts.stream while here
	file.close();

	//The network has been loaded. Tell MW the statistics and network type
	// 0: no format, 1: GraphML, 2:Pajek, 3:Adjacency, 4: Dot, 5:DL, 6:GML, 7: PlainList, 8: WeightedList
	emit fileType(7, networkName, aNodes, totalLinks, undirected);
	qDebug() << "Parser-loadWeighedList() ending and returning...";
	return true;

}




bool Parser::loadSimpleList(){
	qDebug() << "Parser: loadSimpleList()";
	QFile file ( fileName );
	if ( ! file.open(QIODevice::ReadOnly )) 
		return false;
	QTextStream ts( &file );
	QString str;
	QStringList lineElement;
	int i=0, j=0, num=0, source=0, target=0, maxNodeCreated=0;
	bool intOK=false;
        initEdgeWeight=1.0;

	undirected=false;
	arrows=true;
	bezier=false;

	while ( !ts.atEnd() )   {
		str= ts.readLine() ;
		qDebug()<< " str " << str;
		str=str.simplified();  

		if ( isComment(str) ) 
			continue; 

		if ( str.contains("vertices",Qt::CaseInsensitive) 
			|| str.contains("network",Qt::CaseInsensitive) 
			|| str.contains("graph",Qt::CaseInsensitive)  
			|| str.contains("digraph",Qt::CaseInsensitive) 
			|| str.contains("DL",Qt::CaseInsensitive) 
			|| str.contains("list",Qt::CaseInsensitive)
			|| str.contains("graphml",Qt::CaseInsensitive) 
			|| str.contains("xml",Qt::CaseInsensitive)  
			) {
			qDebug()<< "*** Parser:loadSimpleList(): Not an Adjacency-formatted file. Aborting!!";
			file.close();		
			return false;
		}

		lineElement=str.split(" ");

		qDebug () << " Parser::loadSimpleList() - file lines have different amount of elements...";
		i=0;
		for (QStringList::Iterator it1 = lineElement.begin(); it1!=lineElement.end(); ++it1)   {
			num = (*it1).toInt(&intOK);
			if ( !intOK ||  num == 0 ) {
				qDebug()<< "Error! Stumbled upon a zero or an error occured during a string conversion to integer...";
				file.close();
				return false;
			}
			if (i==0){
				source = num;
				qDebug() << "	source node: " << source;
			}
			else {
				target = num;
				qDebug() << "	target node: " << target;
			}

			if (maxNodeCreated < num ) {
				for ( j = maxNodeCreated ; j != num ; j++ ) {
					randX=rand()%gwWidth;
					randY=rand()%gwHeight;
					qDebug()<<"		random coords "<<randX << " "<< randY;
					emit createNode( j+1, initNodeSize,  initNodeColor,
							 initNodeNumberColor, initNodeNumberSize,
							 QString::number(j+1), initNodeLabelColor, initNodeLabelSize,
							 QPointF(randX, randY),
							 initNodeShape, false
							 );
				}
				maxNodeCreated = num ;
			}
			if ( i != 0) {
				qDebug("	there is a link here");
				emit createEdge(source, target, initEdgeWeight, initEdgeColor, undirected, arrows, bezier);
				totalLinks++;
                                qDebug() << "link from Node i= "<< source  << "  to j = " <<  target << " weight = "<< initEdgeWeight<< ". TotalLinks =  " << totalLinks;
			}
			i++;
		}

	} //end ts.stream while here
	file.close();
	//The network has been loaded. Tell MW the statistics and network type
	// 0: no format, 1: GraphML, 2:Pajek, 3:Adjacency, 4: Dot, 5:DL, 6:GML, 7: Weighted List, 8: simple list
	emit fileType(8, networkName, aNodes, totalLinks, undirected);
	qDebug() << "Parser-loadSimpleList() ending and returning...";
	return true;
	
}





/** starts the new thread calling the load* methods
*/
void Parser::run()  {
	qDebug()<< "**** Parser:: run(). This is a new thread! " 
			<< " networkName "<< networkName
			<< " fileFormat "<< fileFormat ;

	switch (fileFormat){
	case 1:	//GraphML
		if (loadGraphML()){
			qDebug("* Parser: that was  a GraphML network");
		}
		else fileFormat=-1;
		break;
	case 2: //Pajek
		if ( loadPajek() ) {
			qDebug("* Parser: that was a Pajek network");
		}
		else fileFormat=-1;
		break;
	case 3: //Adjacency
		if (loadAdjacency() ) {
			qDebug("* Parser: that was an adjacency-matrix network");
		}
		else fileFormat=-1;
		break;
	case 4: //Dot
		if (loadDot() ) {
			qDebug("* Parser: that was a GraphViz (dot) network");
		}
		else fileFormat=-1;
		break;
		case 5:	//GML
		if (loadGML() ){
			qDebug("* Parser: that was a GML (gml) network");
		}
		else fileFormat=-1;
		break;
	case 6: //DL
		if (loadDL() ){
			qDebug("Parser: this is a DL formatted (.dl) network");
		}
		else fileFormat=-1;
		break;

	case 7:	// Weighted List
		if (loadWeighedList() ){
			qDebug("Parser: this is a weighted list formatted (.list) network");
		}
		else fileFormat=-1;
		break;

	case 8:	// List
		if (loadSimpleList() ){
			qDebug("Parser: this is a simple list formatted (.list) network");
		}
		else fileFormat=-1;
		break;

	case 9:	// twomode sociomatrix, affiliation network matrix
		if (loadTwoModeSociomatrix() ){
			qDebug("Parser: OK, this is a two-mode sociomatrix (.tsm) network");
		}
		else fileFormat=-1;
		break;

	default:	//GraphML
		if (loadGraphML() ){
			qDebug("Parser: this is a GraphML network");
		}
		else fileFormat=-1;
		break;
	}


}



//Returns TRUE if QString str is a comment inside the network file. 
bool Parser::isComment(QString str){
	if ( str.startsWith("#", Qt::CaseInsensitive) 
			|| str.startsWith("/*", Qt::CaseInsensitive)
			|| str.startsWith("%", Qt::CaseInsensitive)
			|| str.startsWith("/*", Qt::CaseInsensitive)
			|| str.startsWith("//", Qt::CaseInsensitive)
			|| str.isEmpty()
			) {
			qDebug () << " Parser: a comment or an empty line was found. Skipping..."; 
			return true;
		}
		return false;

}