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 "BlockWidget.h"
8 #include "AbstractBoxItem.h"
10 #include "GroupItem.h"
11 #include <QDomDocument>
12 #include <QDomElement>
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 // create the menu, action, ...
58 dispatcher = new Dispatcher(params,this);
59 params->setDispatcher(dispatcher);
61 // creating block library
62 library = new BlockLibraryWidget(dispatcher,params);
63 params->isCurrentProject = false;
65 QLabel* labDefault = new QLabel("BLAST: BLock ASsembler Tool");
66 stackedWidget = new QStackedWidget;
67 stackedWidget->addWidget(labDefault);
68 stackedWidget->setCurrentIndex(0);
69 this->setCentralWidget(stackedWidget);
70 this->setMinimumSize(800,600);
74 setWindowTitle("blast - top group");
75 setFocusPolicy(Qt::StrongFocus);
84 MainWindow::~MainWindow() {}
86 void MainWindow::initialize() {
90 stackedWidget->setCurrentIndex(0);
91 enableProjectActions(true,PROJECT_NEW | PROJECT_OPEN, OP_RAZ);
93 stackedWidget->setCurrentIndex(0);
98 void MainWindow::readSettings() {
100 settings.beginGroup("init");
101 checkNewVersion = settings.value("check_new_version").toString();
103 if (checkNewVersion.isEmpty()) {
104 checkNewVersion = "true";
107 cout << "test nouvelle version : " << qPrintable(checkNewVersion) << endl;
108 QUrl url("http://www.hrafnheim.fr/download/Cartomagic/carto_currentversion.txt");
109 QNetworkRequest request(url);
110 manager = new QNetworkAccessManager(this);
111 connect(manager,SIGNAL(finished(QNetworkReply*)), this, SLOT(slotCheckNewVersion(QNetworkReply*)));
112 manager->get(request);
116 void MainWindow::writeSettings() {
118 settings.beginGroup("init");
119 settings.setValue("check_new_version",checkNewVersion);
123 void MainWindow::slotCheckNewVersion(QNetworkReply *reply) {
125 if (reply->error() == QNetworkReply::NoError) {
126 QString txt = reply->readAll();
127 QString currentVer = QString("%1.%2.%3").arg(versionMaj).arg(versionMin).arg(revision);
128 if (txt.contains(currentVer)) {
129 cout << "a jour" << endl;
132 cout << "dernière version : " << qPrintable(txt) << ", version actuelle = " << qPrintable(currentVer) << endl;
136 cout << "erreur = " << qPrintable(reply->errorString()) << endl;
138 reply->deleteLater();
141 void MainWindow::enableProjectActions(bool enbMenu, quint16 mask, quint8 op) {
143 projectMenu->setEnabled(true);
146 projectMenu->setEnabled(false);
150 projectMenuEnb = projectMenuEnb | mask;
152 else if (op == OP_REM) {
153 projectMenuEnb = (projectMenuEnb | mask) ^ mask;
155 else if (op == OP_RAZ) {
156 projectMenuEnb = mask;
160 if (projectMenuEnb & PROJECT_NEW) {
161 newProject->setEnabled(true);
164 newProject->setEnabled(false);
166 if (projectMenuEnb & PROJECT_OPEN) {
167 openProject->setEnabled(true);
170 openProject->setEnabled(false);
172 if (projectMenuEnb & PROJECT_SAVE) {
173 saveProject->setEnabled(true);
176 saveProject->setEnabled(false);
178 if (projectMenuEnb & PROJECT_SAVEAS) {
179 saveAsProject->setEnabled(true);
182 saveAsProject->setEnabled(false);
184 if (projectMenuEnb & PROJECT_CLOSE) {
185 closeProject->setEnabled(true);
188 closeProject->setEnabled(false);
190 if (projectMenuEnb & PROJECT_LIB) {
191 openLibrary->setEnabled(true);
194 openLibrary->setEnabled(false);
198 void MainWindow::createMenus(){
200 allMenuBar = menuBar();
202 projectMenu = allMenuBar->addMenu(tr("&Project"));
203 toolsMenu = allMenuBar->addMenu(tr("&Tools"));
205 projectMenu->addAction(newProject);
206 projectMenu->addAction(openProject);
207 projectMenu->addAction(saveProject);
208 projectMenu->addAction(saveAsProject);
209 projectMenu->addAction(closeProject);
210 projectMenu->addAction(openLibrary);
212 toolsMenu->addAction(newBlockWidgetAct);
213 toolsMenu->addAction(graphValidation);
217 void MainWindow::createActions() {
219 newProject = new QAction(tr("&New project"), this);
220 newProject->setIcon(QPixmap::fromImage(QImage("icons/new.ico")));
221 newProject->setStatusTip(tr("Create a new project"));
222 connect(newProject, SIGNAL(triggered()), this, SLOT(slotNewProject()));
224 openProject = new QAction(tr("&Open project"), this);
225 openProject->setIcon(QPixmap::fromImage(QImage("icons/load.png")));
226 openProject->setStatusTip(tr("Open an existing project"));
227 connect(openProject, SIGNAL(triggered()), this, SLOT(slotLoadProject()));
229 saveProject = new QAction(tr("&Save project"), this);
230 saveProject->setIcon(QPixmap::fromImage(QImage("icons/save.png")));
231 saveProject->setStatusTip(tr("Save the current project"));
232 connect(saveProject, SIGNAL(triggered()), this, SLOT(slotSaveProject()));
234 saveAsProject = new QAction(tr("&Save project as..."), this);
235 saveAsProject->setIcon(QPixmap::fromImage(QImage("icons/save-as.png")));
236 saveAsProject->setStatusTip(tr("Save the current project as..."));
237 connect(saveAsProject, SIGNAL(triggered()), this, SLOT(slotSaveAsProject()));
239 closeProject = new QAction(tr("&Close project"), this);
240 closeProject->setIcon(QPixmap::fromImage(QImage("icons/close.png")));
241 closeProject->setStatusTip(tr("Close the current project"));
242 connect(closeProject, SIGNAL(triggered()), this, SLOT(slotCloseProject()));
244 openLibrary = new QAction(tr("Open block &Library"), this);
245 openLibrary->setIcon(QPixmap::fromImage(QImage("icons/add_block.png")));
246 openLibrary->setStatusTip(tr("Open block library window"));
247 connect(openLibrary, SIGNAL(triggered()), this, SLOT(slotOpenBlockLibrary()));
249 newBlockWidgetAct = new QAction(tr("&XML generator"), this);
250 newBlockWidgetAct->setIcon(QPixmap::fromImage(QImage("icons/new.ico")));
251 newBlockWidgetAct->setStatusTip(tr("Create a new XML generator"));
252 connect(newBlockWidgetAct, SIGNAL(triggered()), this, SLOT(slotNewBlockWidget()));
254 graphValidation = new QAction(tr("&graph validation"), this);
255 graphValidation->setIcon(QPixmap::fromImage(QImage("icons/new.ico")));
256 graphValidation->setStatusTip(tr("validate the graph"));
257 connect(graphValidation, SIGNAL(triggered()), this, SLOT(slotGraphValidation()));
261 void MainWindow::save(QString absoluteFilename) {
262 params->save(absoluteFilename);
265 void MainWindow::slotLoadProject(){
267 absoluteFilename = QFileDialog::getOpenFileName(0, "select a project file", "save/",tr("sauvegardes (*.xml)"));
269 if(! absoluteFilename.isEmpty()){
270 GroupWidget* topGroup = dispatcher->loadProject(absoluteFilename);
271 if (topGroup != NULL) {
272 addTopGroup(topGroup);
273 library->updateComboScene();
274 params->isCurrentProject = true;
275 enableProjectActions(true, PROJECT_CLOSE | PROJECT_SAVE | PROJECT_SAVEAS | PROJECT_LIB, OP_RAZ);
279 msgBox.setText("Cannot open the project.");
280 msgBox.setStandardButtons(QMessageBox::Cancel);
281 msgBox.setDefaultButton(QMessageBox::Cancel);
283 int ret = msgBox.exec();
289 void MainWindow::slotNewProject(){
291 enableProjectActions(true, PROJECT_CLOSE | PROJECT_SAVE | PROJECT_SAVEAS | PROJECT_LIB, OP_RAZ);
292 GroupWidget* topGroup = dispatcher->createTopScene();
293 addTopGroup(topGroup);
294 library->updateComboScene();
296 params->isCurrentProject = true;
299 bool MainWindow::slotCloseProject(){
301 bool doClose = false;
303 if(params->isCurrentProject) {
304 if (params->unsaveModif) {
306 msgBox.setText("The project has been modified.");
307 msgBox.setInformativeText("Do you want to save your changes?");
308 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
309 msgBox.setDefaultButton(QMessageBox::Save);
311 int ret = msgBox.exec();
314 case QMessageBox::Save :
318 case QMessageBox::Discard :
327 // removing the GroupWidget from stack
328 QWidget *widget = stackedWidget->widget(1);
329 stackedWidget->removeWidget(widget);
330 stackedWidget->setCurrentIndex(0);
332 dispatcher->closeCurrentProject();
334 params->isCurrentProject = false;
335 params->unsaveModif = false;
336 absoluteFilename = QString();
345 void MainWindow::slotNewBlockWidget() {
349 void MainWindow::slotSaveProject(){
350 if(absoluteFilename != QString()){
351 save(absoluteFilename);
357 void MainWindow::slotSaveAsProject(){
358 if(params->isCurrentProject){
359 QFileDialog dial(0, "Select a file", "save/");
360 dial.setDefaultSuffix(".xml");
361 dial.setAcceptMode(QFileDialog::AcceptSave);
362 if(dial.exec() == QFileDialog::AcceptSave){
363 absoluteFilename = dial.selectedFiles().at(0);
364 if(absoluteFilename != QString())
365 save(absoluteFilename);
370 void MainWindow::slotOpenBlockLibrary() {
371 dispatcher->showBlocksLibrary();
375 void MainWindow::slotGraphValidation() {
376 params->parametersValidation();
379 void MainWindow::addTopGroup(GroupWidget *_topGroup) {
380 topGroup = _topGroup;
381 stackedWidget->addWidget(topGroup);
382 stackedWidget->setCurrentIndex(1);
385 void MainWindow::removeTopGroup() {
386 stackedWidget->removeWidget(topGroup);
387 topGroup->deleteLater();
388 stackedWidget->setCurrentIndex(0);
391 void MainWindow::closeEvent(QCloseEvent *event) {
393 if (params->isCurrentProject) {
398 library->deleteLater();
403 void MainWindow::mousePressEvent(QMouseEvent *e) {
405 if (dispatcher->getCurrentGroup() != NULL) {
406 dispatcher->setCurrentGroupWidget(dispatcher->getCurrentGroup());
408 QMainWindow::mousePressEvent(e);
411 void MainWindow::focusInEvent(QFocusEvent *e) {
413 if(params->currentWindow != this){
414 params->setCurrentWindow(this);
415 changeConnectionMode(false);