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();
277 msgBox.setText("Cannot open the project.");
278 msgBox.setInformativeText("Do you want to save your changes?");
279 msgBox.setStandardButtons(QMessageBox::Cancel);
280 msgBox.setDefaultButton(QMessageBox::Cancel);
282 int ret = msgBox.exec();
288 void MainWindow::slotNewProject(){
290 enableProjectActions(true, PROJECT_CLOSE | PROJECT_SAVE | PROJECT_SAVEAS | PROJECT_LIB, OP_RAZ);
291 GroupWidget* topGroup = dispatcher->createTopScene();
292 addTopGroup(topGroup);
293 library->updateComboScene();
294 params->isCurrentProject = true;
297 void MainWindow::slotCloseProject(){
299 // removing the GroupWidget from stack
300 QWidget *widget = stackedWidget->widget(1);
301 stackedWidget->removeWidget(widget);
302 stackedWidget->setCurrentIndex(0);
304 dispatcher->closeCurrentProject();
307 params->isCurrentProject = false;
308 params->unsaveModif = false;
309 absoluteFilename = QString();
315 void MainWindow::slotNewBlockWidget() {
319 void MainWindow::slotSaveProject(){
320 if(absoluteFilename != QString()){
321 save(absoluteFilename);
327 void MainWindow::slotSaveAsProject(){
328 if(params->isCurrentProject){
329 QFileDialog dial(0, "Select a file", "save/");
330 dial.setDefaultSuffix(".xml");
331 dial.setAcceptMode(QFileDialog::AcceptSave);
332 if(dial.exec() == QFileDialog::AcceptSave){
333 absoluteFilename = dial.selectedFiles().at(0);
334 if(absoluteFilename != QString())
335 save(absoluteFilename);
340 void MainWindow::slotOpenBlockLibrary() {
341 dispatcher->showBlocksLibrary();
345 void MainWindow::slotGraphValidation() {
346 params->parametersValidation();
349 void MainWindow::addTopGroup(GroupWidget *_topGroup) {
350 topGroup = _topGroup;
351 stackedWidget->addWidget(topGroup);
352 stackedWidget->setCurrentIndex(1);
355 void MainWindow::removeTopGroup() {
356 stackedWidget->removeWidget(topGroup);
357 topGroup->deleteLater();
358 stackedWidget->setCurrentIndex(0);
361 void MainWindow::closeEvent(QCloseEvent *event){
362 if(params->isCurrentProject){
364 msgBox.setText("The project has been modified.");
365 msgBox.setInformativeText("Do you want to save your changes?");
366 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
367 msgBox.setDefaultButton(QMessageBox::Save);
369 int ret = msgBox.exec();
372 case QMessageBox::Save :
376 case QMessageBox::Discard :
386 void MainWindow::mousePressEvent(QMouseEvent *e) {
388 if (dispatcher->getCurrentGroup() != NULL) {
389 dispatcher->setCurrentGroupWidget(dispatcher->getCurrentGroup());
391 QMainWindow::mousePressEvent(e);
394 void MainWindow::focusInEvent(QFocusEvent *e) {
396 if(params->currentWindow != this){
397 params->setCurrentWindow(this);
398 changeConnectionMode(false);