]> AND Private Git Repository - blast.git/blob - BlockWidget.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
added admittance computation
[blast.git] / BlockWidget.cpp
1 #include "BlockWidget.h"\r
2 \r
3 using namespace std;\r
4 using namespace Qt;\r
5 \r
6 BlockWidget::BlockWidget(QWidget *parent) : QWidget(parent)\r
7 {\r
8 \r
9     rxComment = new QRegExp("(.*)--.*");\r
10     rxComma = new QRegExp("(.*)[;]");\r
11     rxPort = new QRegExp("[\\s\\t]*(.*)[\\s\\t]*:[\\s\\t]*(in|out|inout)[\\s\\t]*(.*)",CaseInsensitive,QRegExp::RegExp);\r
12     rxEnt = new QRegExp("[\\s\\t]*entity[\\s\\t]*(.*)[\\s\\t]*is",CaseInsensitive,QRegExp::RegExp);\r
13     rxArch = new QRegExp("[\\s\\t]*architecture[\\s\\t]*(.*)[\\s\\t]*of (.*)[\\s\\t]*is",CaseInsensitive,QRegExp::RegExp);\r
14     rxComp = new QRegExp("[\\s\\t]*component[\\s\\t]*(.*)[\\s\\t]*",CaseInsensitive,QRegExp::RegExp);\r
15     rxEnd = new QRegExp("[\\s\\t]*end(.*)",CaseInsensitive,QRegExp::RegExp);\r
16     rxComp = new QRegExp("[\\s\\t]*end component;",CaseInsensitive,QRegExp::RegExp);\r
17     rxGeneric = new QRegExp("[\\s\\t]*generic[\\s\\t]*[(][\\s\\t]*",CaseInsensitive,QRegExp::RegExp);\r
18     rxEndGen = new QRegExp("[\\s\\t]*[)]",CaseInsensitive,QRegExp::RegExp);\r
19     rxGen = new QRegExp("[\\s\\t]*(.*)[\\s\\t]*:[\\s\\t]*(.*)[\\s\\t]*:=[\\s\\t]*(.*)",CaseInsensitive,QRegExp::RegExp);\r
20     rxConst = new QRegExp("[\\s\\t]*constant[\\s\\t]*(.*)[\\s\\t]*:[\\s\\t]*(.)*[\\s\\t]*:=[\\s\\t]*(.*)",CaseInsensitive,QRegExp::RegExp);\r
21     rxWidth = new QRegExp(".*[(](.*)(downto|to)(.*)[)]",CaseInsensitive,QRegExp::RegExp);\r
22 \r
23     loadBt = new QPushButton("load VHDL");\r
24     genBt = new QPushButton("generate XML");\r
25     QHBoxLayout *widgetLayout = new QHBoxLayout;\r
26     QVBoxLayout *left = new QVBoxLayout;\r
27     QVBoxLayout *right = new QVBoxLayout;\r
28 \r
29     scrollPort = new QScrollArea;\r
30     scrollPort->setWidgetResizable(true);\r
31     twPort = new QTableWidget(this);\r
32     scrollPort->setWidget(twPort);\r
33     scrollGen = new QScrollArea;\r
34     scrollGen->setWidgetResizable(true);\r
35     twGen = new QTableWidget(this);\r
36     scrollGen->setWidget(twGen);\r
37     teName = new QTextEdit;\r
38     teBrief = new QTextEdit;\r
39     teDesc = new QTextEdit;\r
40     lblName = new QLabel("Block name :");\r
41     lblBrief = new QLabel("Enter a brief description : ");\r
42     lblDesc = new QLabel("Enter a detailled description : ");\r
43     lblPort = new QLabel("Ports :");\r
44     lblGen = new QLabel("Generics :");\r
45 \r
46     connect(loadBt, SIGNAL(clicked()),this, SLOT(loadCode()));\r
47     connect(genBt, SIGNAL(clicked()), this, SLOT(generateXml()));\r
48 \r
49     left->addWidget(loadBt);\r
50     left->addWidget(lblPort);\r
51     left->addWidget(scrollPort);\r
52     left->addWidget(lblGen);\r
53     left->addWidget(scrollGen);\r
54 \r
55     right->addWidget(lblName);\r
56     right->addWidget(teName);\r
57     right->addWidget(lblBrief);\r
58     right->addWidget(teBrief);\r
59     right->addWidget(lblDesc);\r
60     right->addWidget(teDesc);\r
61     right->addWidget(genBt);\r
62 \r
63     widgetLayout->addLayout(left);\r
64     widgetLayout->addLayout(right);\r
65     setLayout(widgetLayout);\r
66     show();\r
67 }\r
68 \r
69 BlockWidget::~BlockWidget()\r
70 {\r
71 \r
72 }\r
73 \r
74 // This function opens a VHDL file and get the informations about the entity :\r
75 // First the generics, then the signals.\r
76 // You can edit the descriptions in the right, one for the brief description, the other for the detailled.\r
77 void BlockWidget::loadCode() {\r
78 \r
79     QString line, portName, portType, portId, genName, genType, genValue;\r
80     QStringList *portNameList, *portTypeList, *portIdList, *genNameList, *genTypeList, *genValueList;\r
81     cpt = 0;\r
82     twPort->setColumnCount(3);\r
83     twPort->setRowCount(cpt);\r
84     twGen->setColumnCount(3);\r
85     twGen->setRowCount(cpt);\r
86     portNameList = new QStringList;\r
87     portTypeList = new QStringList;\r
88     portIdList = new QStringList;\r
89     genNameList = new QStringList;\r
90     genTypeList = new QStringList;\r
91     genValueList = new QStringList;\r
92 \r
93     fileName = QFileDialog::getOpenFileName(this,\r
94                                             tr("Open File"), "C:", tr("Files (*.txt *.vhd)"));\r
95     QFile file(fileName);\r
96 \r
97     if(!file.open(QIODevice::ReadOnly | QIODevice::Text))\r
98         return;\r
99     QTextStream ts(&file);\r
100     while (!ts.atEnd())\r
101     {\r
102         line = ts.readLine();\r
103         if(rxComment->indexIn(line) != -1) {\r
104             line = rxComment->cap(1);\r
105         }\r
106 \r
107         if(rxEnt->indexIn(line)!= -1) {\r
108 \r
109             entName = rxEnt->cap(1);\r
110             teName->setText(entName);\r
111             QSize size = teName->document()->size().toSize();\r
112             teName->setMaximumSize(size);\r
113 \r
114             while(rxEnd->indexIn(line) == -1) {\r
115                 line = ts.readLine();\r
116                 if(rxComment->indexIn(line) != -1) {\r
117                     line = rxComment->cap(1);\r
118                 }\r
119                 if(rxComma->indexIn(line) != -1) {\r
120                     line = rxComma->cap(1);\r
121                 }\r
122                 if(rxGeneric->indexIn(line) != -1) {\r
123                     while(rxEndGen->indexIn(line) == -1) {\r
124                         line = ts.readLine();\r
125                         if(rxComment->indexIn(line) != -1) {\r
126                             line = rxComment->cap(1);\r
127                         }\r
128                         if(rxComma->indexIn(line) != -1) {\r
129                             line = rxComma->cap(1);\r
130                         }\r
131                         if(rxGen->indexIn(line) != -1) {\r
132                             genName = rxGen->cap(1).simplified();\r
133                             genType = rxGen->cap(2).simplified();\r
134                             genValue = rxGen->cap(3).simplified();\r
135 \r
136                             genNameList->append(genName);\r
137                             genTypeList->append(genType);\r
138                             genValueList->append(genValue);\r
139                         }\r
140                     }\r
141                 }\r
142                 if(rxPort->indexIn(line) != -1) {\r
143                     if(rxComment->indexIn(line) != -1) {\r
144                         line = rxComment->cap(1);\r
145                     }\r
146                     if(rxComma->indexIn(line) != -1) {\r
147                         line = rxComma->cap(1);\r
148                     }\r
149                     portName = rxPort->cap(1).simplified();\r
150                     portId = rxPort->cap(2).simplified();\r
151                     portType = rxPort->cap(3).simplified();\r
152                     portNameList->append(portName);\r
153                     portIdList->append(portId);\r
154                     portTypeList->append(portType);\r
155                 }\r
156             }\r
157         }\r
158     }\r
159 \r
160     twGen->setRowCount(genNameList->size());\r
161     for(int i = 0; i < genNameList->size(); i++) {\r
162         twGen->setItem(i, 0, new QTableWidgetItem(genNameList->at(i)));\r
163         twGen->setItem(i, 1, new QTableWidgetItem(genTypeList->at(i)));\r
164         twGen->setItem(i, 2, new QTableWidgetItem(genValueList->at(i)));\r
165     }\r
166     twPort->setRowCount(portNameList->size());\r
167     for(int i = 0; i < portNameList->size(); i++) {\r
168         twPort->setItem(i, 0, new QTableWidgetItem(portIdList->at(i)));\r
169         twPort->setItem(i, 1, new QTableWidgetItem(portNameList->at(i)));\r
170         twPort->setItem(i, 2, new QTableWidgetItem(portTypeList->at(i)));\r
171     }\r
172 \r
173     file.close();\r
174     scrollPort->setWidget(twPort);\r
175     return;\r
176 }\r
177 \r
178 // This function gets the informations in the table and the descriptions, and creates a XML file with this content\r
179 void BlockWidget::generateXml() {\r
180 \r
181     QString portName, portType, portId, genName, genType, genValue;\r
182     QStringList *portNameList, *portTypeList, *portIdList, *genNameList, *genTypeList, *genValueList;\r
183     int x, y, width;\r
184     brief = teBrief->toPlainText();\r
185     desc = teDesc->toPlainText();\r
186     entName = teName->toPlainText();\r
187 \r
188     portNameList = new QStringList;\r
189     portTypeList = new QStringList;\r
190     portIdList = new QStringList;\r
191     genNameList = new QStringList;\r
192     genTypeList = new QStringList;\r
193     genValueList = new QStringList;\r
194     for(int i = 0; i < twGen->rowCount(); i++) {\r
195         genNameList->append(twGen->item(i,0)->text());\r
196         genTypeList->append(twGen->item(i,1)->text());\r
197         genValueList->append(twGen->item(i,2)->text());\r
198     }\r
199 \r
200     for(int i = 0; i < twPort->rowCount(); i++) {\r
201         portIdList->append(twPort->item(i,0)->text());\r
202         portNameList->append(twPort->item(i,1)->text());\r
203         portTypeList->append(twPort->item(i,2)->text());\r
204     }\r
205 \r
206     QDomDocument doc (entName);\r
207     QDomElement block = doc.createElement("block");\r
208     block.setAttribute("name",entName);\r
209     block.setAttribute("version", "0.1");\r
210     doc.appendChild(block);\r
211 \r
212     QDomElement comments = doc.createElement("comments");\r
213     QDomElement category = doc.createElement("caterory");\r
214     category.setAttribute("ids","");\r
215     comments.appendChild(category);\r
216 \r
217     QDomElement eBrief = doc.createElement("brief");\r
218     if(!brief.isEmpty()) {\r
219         QDomText txtBrief = doc.createTextNode(brief);\r
220         eBrief.appendChild(txtBrief);\r
221         comments.appendChild(eBrief);\r
222     }\r
223     QDomElement eDesc = doc.createElement("description");\r
224     if(!desc.isEmpty()) {\r
225         QDomText txtDesc = doc.createTextNode(desc);\r
226         eDesc.appendChild(txtDesc);\r
227         comments.appendChild(eDesc);\r
228     }\r
229     block.appendChild(comments);\r
230 \r
231     QDomElement parameters = doc.createElement("parameters");\r
232     QDomElement interfaces = doc.createElement("interfaces");\r
233     QDomElement inputs = doc.createElement("inputs");\r
234     QDomElement outputs = doc.createElement("outputs");\r
235     QDomElement bidirs = doc.createElement("bidirs");\r
236     block.appendChild(parameters);\r
237     block.appendChild(interfaces);\r
238     interfaces.appendChild(inputs);\r
239     interfaces.appendChild(outputs);\r
240     interfaces.appendChild(bidirs);\r
241 \r
242     for(int i = 0; i < twGen->rowCount(); i++) {\r
243         genName = genNameList->at(i);\r
244         genType = genTypeList->at(i);\r
245         genValue = genValueList->at(i);\r
246         QDomElement parameter = doc.createElement("parameter");\r
247         parameter.setAttribute("name",genName);\r
248         parameter.setAttribute("type",genType);\r
249         parameter.setAttribute("value",genValue);\r
250         parameter.setAttribute("context","generic");\r
251         parameters.appendChild(parameter);\r
252     }\r
253 \r
254     for(int i = 0; i < portIdList->size(); i++) {\r
255         portId = portIdList->at(i);\r
256         portName = portNameList->at(i);\r
257         portType = portTypeList->at(i);\r
258         if(rxWidth->indexIn(portType) != -1) {\r
259             x = rxWidth->cap(1).toInt();\r
260             y = rxWidth->cap(3).toInt();\r
261             if(x < y)\r
262                 width = y - x + 1;\r
263             else if(x > y)\r
264                 width = x - y + 1;\r
265             else\r
266                 width = 1;\r
267         }\r
268 \r
269         if(portId.compare("in", CaseInsensitive) == 0) {\r
270             QDomElement input = doc.createElement("input");\r
271             input.setAttribute("name",portName);\r
272             input.setAttribute("width", width);\r
273             inputs.appendChild(input);\r
274         }\r
275         else if(portId.compare("out", CaseInsensitive) == 0) {\r
276             QDomElement output = doc.createElement("output");\r
277             output.setAttribute("name",portName);\r
278             output.setAttribute("width", width);\r
279             outputs.appendChild(output);\r
280         }\r
281         else if(portId.compare("inout", CaseInsensitive) == 0) {\r
282             QDomElement bidir = doc.createElement("bidir");\r
283             bidir.setAttribute("name",portName);\r
284             bidir.setAttribute("width", width);\r
285             bidirs.appendChild(bidir);\r
286         }\r
287     }\r
288 \r
289     fileName = QFileDialog::getOpenFileName(this, tr("Open File"),\r
290                                             "C:", tr("Files (*.xml)"));\r
291     QFile file(fileName);\r
292     if(!file.open(QIODevice::WriteOnly | QIODevice::Text))\r
293         return;\r
294     QTextStream ts(&file);\r
295     doc.save(ts,QDomNode::EncodingFromTextStream);\r
296     file.close();\r
297 \r
298     QLabel *popup = new QLabel("Votre fichier XML est rempli");\r
299     popup->show();\r
300 }\r