1 #include "MainWindow.h"
2 #include "Dispatcher.h"
3 #include "Parameters.h"
4 #include "BlockLibraryWidget.h"
5 #include "GroupWidget.h"
6 #include "GroupScene.h"
7 #include "VHDLConverter.h"
9 #include "FunctionalBlock.h"
10 #include <QDomDocument>
11 #include <QDomElement>
14 #include "NewProjectDialog.h"
16 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
18 versionMaj = VERSION_MAJ;
19 versionMin = VERSION_MIN;
23 params = new Parameters();
25 params->loadBlastConfiguration("blastconfig.xml");
27 if (!QFileInfo::exists(params->refLib)) {
28 params->loadReferencesFromXml();
29 int ret = QMessageBox::question(this,tr("Building references library"),tr("The reference block library does not exists.\n References have been read directly from the xml descriptions of blocks.\n It can be saved into a library in order to start application faster. "), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
30 if (ret == QMessageBox::Ok) {
31 params->saveReferencesToLib();
35 params->loadReferencesFromLib();
38 if (!QFileInfo::exists(params->implLib)) {
39 params->loadImplementationsFromXml();
40 int ret = QMessageBox::question(this,tr("Building implementations library"),tr("The block implementations library does not exists.\nImplementations have been read directly from the xml descriptions.\n It can be saved into a library in order to start application faster. "), QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
41 if (ret == QMessageBox::Ok) {
42 params->saveImplementationsToLib();
46 params->loadImplementationsFromLib();
49 catch(Exception err) {
50 cerr << qPrintable(err.getDefaultMessage()) << endl;
51 cerr << "Aborting ..." << endl;
55 cout << "all references and implementations are loaded" << endl;
57 params->createDelayBlock();
59 // create the menu, action, ...
60 dispatcher = new Dispatcher(params,this);
61 params->setDispatcher(dispatcher);
63 // creating block library
64 library = new BlockLibraryWidget(dispatcher,params);
65 params->isCurrentProject = false;
67 QLabel* labDefault = new QLabel("BLAST: BLock ASsembler Tool");
68 stackedWidget = new QStackedWidget;
69 stackedWidget->addWidget(labDefault);
70 stackedWidget->setCurrentIndex(0);
71 this->setCentralWidget(stackedWidget);
72 this->setMinimumSize(800,600);
76 setWindowTitle("blast - top group");
77 setFocusPolicy(Qt::StrongFocus);
86 MainWindow::~MainWindow() {}
88 void MainWindow::initialize() {
93 stackedWidget->setCurrentIndex(0);
94 enableProjectActions(true,PROJECT_NEW | PROJECT_OPEN, OP_RAZ);
95 enableAnalysisActions(false);
97 stackedWidget->setCurrentIndex(0);
102 void MainWindow::readSettings() {
104 settings.beginGroup("init");
105 checkNewVersion = settings.value("check_new_version").toString();
107 if (checkNewVersion.isEmpty()) {
108 checkNewVersion = "true";
111 cout << "test nouvelle version : " << qPrintable(checkNewVersion) << endl;
112 QUrl url("http://www.hrafnheim.fr/download/Cartomagic/carto_currentversion.txt");
113 QNetworkRequest request(url);
114 manager = new QNetworkAccessManager(this);
115 connect(manager,SIGNAL(finished(QNetworkReply*)), this, SLOT(slotCheckNewVersion(QNetworkReply*)));
116 manager->get(request);
120 void MainWindow::writeSettings() {
122 settings.beginGroup("init");
123 settings.setValue("check_new_version",checkNewVersion);
127 void MainWindow::slotCheckNewVersion(QNetworkReply *reply) {
129 if (reply->error() == QNetworkReply::NoError) {
130 QString txt = reply->readAll();
131 QString currentVer = QString("%1.%2.%3").arg(versionMaj).arg(versionMin).arg(revision);
132 if (txt.contains(currentVer)) {
133 cout << "a jour" << endl;
136 cout << "dernière version : " << qPrintable(txt) << ", version actuelle = " << qPrintable(currentVer) << endl;
140 cout << "erreur = " << qPrintable(reply->errorString()) << endl;
142 reply->deleteLater();
145 void MainWindow::enableProjectActions(bool enbMenu, quint16 mask, quint8 op) {
147 projectMenu->setEnabled(true);
150 projectMenu->setEnabled(false);
154 projectMenuEnb = projectMenuEnb | mask;
156 else if (op == OP_REM) {
157 projectMenuEnb = (projectMenuEnb | mask) ^ mask;
159 else if (op == OP_RAZ) {
160 projectMenuEnb = mask;
164 if (projectMenuEnb & PROJECT_NEW) {
165 newProject->setEnabled(true);
168 newProject->setEnabled(false);
170 if (projectMenuEnb & PROJECT_OPEN) {
171 openProject->setEnabled(true);
174 openProject->setEnabled(false);
176 if (projectMenuEnb & PROJECT_SAVE) {
177 saveProject->setEnabled(true);
180 saveProject->setEnabled(false);
182 if (projectMenuEnb & PROJECT_SAVEAS) {
183 saveAsProject->setEnabled(true);
186 saveAsProject->setEnabled(false);
188 if (projectMenuEnb & PROJECT_CLOSE) {
189 closeProject->setEnabled(true);
192 closeProject->setEnabled(false);
194 if (projectMenuEnb & PROJECT_LIB) {
195 openLibrary->setEnabled(true);
198 openLibrary->setEnabled(false);
202 void MainWindow::enableAnalysisActions(bool enbMenu, quint16 mask, quint8 op) {
204 analysisMenu->setEnabled(true);
207 analysisMenu->setEnabled(false);
211 analysisMenuEnb = analysisMenuEnb | mask;
213 else if (op == OP_REM) {
214 analysisMenuEnb = (analysisMenuEnb | mask) ^ mask;
216 else if (op == OP_RAZ) {
217 analysisMenuEnb = mask;
221 if (analysisMenuEnb & ANALYSIS_ANALYZE) {
222 graphAnalysis->setEnabled(true);
225 graphAnalysis->setEnabled(false);
227 if (analysisMenuEnb & ANALYSIS_GENERATE) {
228 generateVHDL->setEnabled(true);
231 generateVHDL->setEnabled(false);
235 void MainWindow::createMenus(){
237 allMenuBar = menuBar();
239 projectMenu = allMenuBar->addMenu(tr("&Project"));
240 analysisMenu = allMenuBar->addMenu(tr("&Analysis"));
241 toolsMenu = allMenuBar->addMenu(tr("&Tools"));
243 projectMenu->addAction(newProject);
244 projectMenu->addAction(openProject);
245 projectMenu->addAction(saveProject);
246 projectMenu->addAction(saveAsProject);
247 projectMenu->addAction(closeProject);
248 projectMenu->addAction(openLibrary);
250 analysisMenu->addAction(graphAnalysis);
251 analysisMenu->addAction(generateVHDL);
253 toolsMenu->addAction(vhdlToXmlAct);
258 void MainWindow::createActions() {
260 newProject = new QAction(tr("&New project"), this);
261 newProject->setIcon(QPixmap::fromImage(QImage("icons/new.ico")));
262 newProject->setStatusTip(tr("Create a new project"));
263 connect(newProject, SIGNAL(triggered()), this, SLOT(slotNewProject()));
265 openProject = new QAction(tr("&Open project"), this);
266 openProject->setIcon(QPixmap::fromImage(QImage("icons/load.png")));
267 openProject->setStatusTip(tr("Open an existing project"));
268 connect(openProject, SIGNAL(triggered()), this, SLOT(slotLoadProject()));
270 saveProject = new QAction(tr("&Save project"), this);
271 saveProject->setIcon(QPixmap::fromImage(QImage("icons/save.png")));
272 saveProject->setStatusTip(tr("Save the current project"));
273 connect(saveProject, SIGNAL(triggered()), this, SLOT(slotSaveProject()));
275 saveAsProject = new QAction(tr("&Save project as..."), this);
276 saveAsProject->setIcon(QPixmap::fromImage(QImage("icons/save-as.png")));
277 saveAsProject->setStatusTip(tr("Save the current project as..."));
278 connect(saveAsProject, SIGNAL(triggered()), this, SLOT(slotSaveAsProject()));
280 closeProject = new QAction(tr("&Close project"), this);
281 closeProject->setIcon(QPixmap::fromImage(QImage("icons/close.png")));
282 closeProject->setStatusTip(tr("Close the current project"));
283 connect(closeProject, SIGNAL(triggered()), this, SLOT(slotCloseProject()));
285 openLibrary = new QAction(tr("Open block &Library"), this);
286 openLibrary->setIcon(QPixmap::fromImage(QImage("icons/add_block.png")));
287 openLibrary->setStatusTip(tr("Open block library window"));
288 connect(openLibrary, SIGNAL(triggered()), this, SLOT(slotOpenBlockLibrary()));
290 vhdlToXmlAct = new QAction(tr("&XML generator"), this);
291 vhdlToXmlAct->setIcon(QPixmap::fromImage(QImage("icons/new.ico")));
292 vhdlToXmlAct->setStatusTip(tr("Create a new XML generator"));
293 connect(vhdlToXmlAct, SIGNAL(triggered()), this, SLOT(slotVHDLToXml()));
295 graphAnalysis = new QAction(tr("&graph analysis"), this);
296 graphAnalysis->setIcon(QPixmap::fromImage(QImage("icons/new.ico")));
297 graphAnalysis->setStatusTip(tr("validate the graph"));
298 connect(graphAnalysis, SIGNAL(triggered()), this, SLOT(slotGraphAnalysis()));
300 generateVHDL = new QAction(tr("generate &VHDL"), this);
301 generateVHDL->setIcon(QPixmap::fromImage(QImage("icons/new.ico")));
302 generateVHDL->setStatusTip(tr("generate the VHDL code for the whole design"));
303 connect(generateVHDL, SIGNAL(triggered()), this, SLOT(slotGenerateVHDL()));
307 void MainWindow::save(QString projectFile) {
308 params->save(projectFile);
311 void MainWindow::slotLoadProject(){
313 params->projectFile = QFileDialog::getOpenFileName(0, "select a project file", "save/",tr("sauvegardes (*.xml)"));
315 if(! params->projectFile.isEmpty()){
316 GroupWidget* topGroup = dispatcher->loadProject(params->projectFile);
317 if (topGroup != NULL) {
318 addTopGroup(topGroup);
319 library->updateComboScene();
320 params->isCurrentProject = true;
321 enableProjectActions(true, PROJECT_CLOSE | PROJECT_SAVE | PROJECT_SAVEAS | PROJECT_LIB, OP_RAZ);
322 enableAnalysisActions(true, ANALYSIS_ANALYZE, OP_RAZ);
326 msgBox.setText("Cannot open the project.");
327 msgBox.setStandardButtons(QMessageBox::Cancel);
328 msgBox.setDefaultButton(QMessageBox::Cancel);
336 void MainWindow::slotNewProject(){
338 NewProjectDialog* dialog = new NewProjectDialog(params);
339 int ret = dialog->exec();
342 enableProjectActions(true, PROJECT_CLOSE | PROJECT_SAVE | PROJECT_SAVEAS | PROJECT_LIB, OP_RAZ);
343 enableAnalysisActions(true, ANALYSIS_ANALYZE | ANALYSIS_GENERATE, OP_RAZ);
344 GroupWidget* topGroup = dispatcher->createTopScene();
345 addTopGroup(topGroup);
346 library->updateComboScene();
348 params->isCurrentProject = true;
352 bool MainWindow::slotCloseProject(){
354 bool doClose = false;
356 if(params->isCurrentProject) {
357 if (params->unsaveModif) {
359 msgBox.setText("The project has been modified.");
360 msgBox.setInformativeText("Do you want to save your changes?");
361 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
362 msgBox.setDefaultButton(QMessageBox::Save);
364 int ret = msgBox.exec();
367 case QMessageBox::Save :
371 case QMessageBox::Discard :
380 // removing the GroupWidget from stack
381 QWidget *widget = stackedWidget->widget(1);
382 stackedWidget->removeWidget(widget);
383 stackedWidget->setCurrentIndex(0);
385 dispatcher->closeCurrentProject();
387 params->isCurrentProject = false;
388 params->unsaveModif = false;
389 params->projectFile = QString();
398 void MainWindow::slotVHDLToXml() {
402 void MainWindow::slotSaveProject(){
403 if(params->projectFile != QString()){
404 save(params->projectFile);
410 void MainWindow::slotSaveAsProject(){
411 if(params->isCurrentProject){
412 QFileDialog dial(0, "Select a file", "save/");
413 dial.setDefaultSuffix(".xml");
414 dial.setAcceptMode(QFileDialog::AcceptSave);
415 if(dial.exec() == QFileDialog::AcceptSave){
416 params->projectFile = dial.selectedFiles().at(0);
417 if(params->projectFile != QString())
418 save(params->projectFile);
423 void MainWindow::slotOpenBlockLibrary() {
424 dispatcher->showBlocksLibrary();
428 void MainWindow::slotGraphAnalysis() {
431 params->getGraph()->computeOutputPatterns(1);
434 cerr << qPrintable(e.getMessage()) << endl;
436 if (e.getType() == IP_AP_NOTCOMPAT) {
437 FunctionalBlock* toBlock = (FunctionalBlock*)(e.getSource());
438 QString msg = tr("");
439 msg.append(toBlock->getName());
440 msg += " is not compatible with its input pattern.\nDo you want to launch automatic modification process to ensure the compatibility ?";
441 int ret = QMessageBox::question(this,tr("Building references library"),msg, QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
442 if (ret == QMessageBox::Ok) {
443 dispatcher->findGraphModifications(toBlock);
449 void MainWindow::slotGenerateVHDL() {
451 params->getGraph()->generateVHDL(params->projectPath);
454 cerr << qPrintable(e.getMessage()) << endl;
458 void MainWindow::addTopGroup(GroupWidget *_topGroup) {
459 topGroup = _topGroup;
460 stackedWidget->addWidget(topGroup);
461 stackedWidget->setCurrentIndex(1);
464 void MainWindow::removeTopGroup() {
465 stackedWidget->removeWidget(topGroup);
466 topGroup->deleteLater();
467 stackedWidget->setCurrentIndex(0);
470 void MainWindow::closeEvent(QCloseEvent *event) {
472 if (params->isCurrentProject) {
477 library->deleteLater();
482 void MainWindow::mousePressEvent(QMouseEvent *e) {
484 if (dispatcher->getCurrentGroup() != NULL) {
485 dispatcher->setCurrentGroupWidget(dispatcher->getCurrentGroup());
487 QMainWindow::mousePressEvent(e);
490 void MainWindow::focusInEvent(QFocusEvent *e) {
492 if(params->currentWindow != this){
493 params->setCurrentWindow(this);
494 changeConnectionMode(false);