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();
295 params->isCurrentProject = true;
298 bool MainWindow::slotCloseProject(){
300 bool doClose = false;
302 if(params->isCurrentProject) {
303 if (params->unsaveModif) {
305 msgBox.setText("The project has been modified.");
306 msgBox.setInformativeText("Do you want to save your changes?");
307 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
308 msgBox.setDefaultButton(QMessageBox::Save);
310 int ret = msgBox.exec();
313 case QMessageBox::Save :
317 case QMessageBox::Discard :
323 // removing the GroupWidget from stack
324 QWidget *widget = stackedWidget->widget(1);
325 stackedWidget->removeWidget(widget);
326 stackedWidget->setCurrentIndex(0);
328 dispatcher->closeCurrentProject();
330 params->isCurrentProject = false;
331 params->unsaveModif = false;
332 absoluteFilename = QString();
341 void MainWindow::slotNewBlockWidget() {
345 void MainWindow::slotSaveProject(){
346 if(absoluteFilename != QString()){
347 save(absoluteFilename);
353 void MainWindow::slotSaveAsProject(){
354 if(params->isCurrentProject){
355 QFileDialog dial(0, "Select a file", "save/");
356 dial.setDefaultSuffix(".xml");
357 dial.setAcceptMode(QFileDialog::AcceptSave);
358 if(dial.exec() == QFileDialog::AcceptSave){
359 absoluteFilename = dial.selectedFiles().at(0);
360 if(absoluteFilename != QString())
361 save(absoluteFilename);
366 void MainWindow::slotOpenBlockLibrary() {
367 dispatcher->showBlocksLibrary();
371 void MainWindow::slotGraphValidation() {
372 params->parametersValidation();
375 void MainWindow::addTopGroup(GroupWidget *_topGroup) {
376 topGroup = _topGroup;
377 stackedWidget->addWidget(topGroup);
378 stackedWidget->setCurrentIndex(1);
381 void MainWindow::removeTopGroup() {
382 stackedWidget->removeWidget(topGroup);
383 topGroup->deleteLater();
384 stackedWidget->setCurrentIndex(0);
387 void MainWindow::closeEvent(QCloseEvent *event) {
389 if (params->isCurrentProject) {
396 void MainWindow::mousePressEvent(QMouseEvent *e) {
398 if (dispatcher->getCurrentGroup() != NULL) {
399 dispatcher->setCurrentGroupWidget(dispatcher->getCurrentGroup());
401 QMainWindow::mousePressEvent(e);
404 void MainWindow::focusInEvent(QFocusEvent *e) {
406 if(params->currentWindow != this){
407 params->setCurrentWindow(this);
408 changeConnectionMode(false);