Skip to content
Snippets Groups Projects
Commit a4799524 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

ProjectManager now correctly handles error during opening of old projects

parent ae2cae4b
No related branches found
No related tags found
No related merge requests found
......@@ -95,7 +95,7 @@ MainWindow::MainWindow(QWidget *parent)
// QCoreApplication::setApplicationVersion(QLatin1String(Constants::APPLICATION_VERSION));
// QCoreApplication::setOrganizationName(QLatin1String(Constants::APPLICATION_NAME));
initModels();
createModels();
if (!Utils::HostOsInfo::isMacHost())
QApplication::setWindowIcon(QIcon(":/images/BornAgain.ico"));
......@@ -241,66 +241,80 @@ void MainWindow::closeEvent(QCloseEvent *event)
}
}
void MainWindow::initModels()
//! creates and initializes models
void MainWindow::createModels()
{
initMaterialModel(); // should be first
createMaterialModel(); // should be first
initSampleModel();
createSampleModel();
initJobModel();
createJobModel();
initInstrumentModel();
createInstrumentModel();
//initFitModel();
//createFitModel();
resetModels();
}
void MainWindow::initMaterialModel()
void MainWindow::createMaterialModel()
{
delete m_materialModel;
m_materialModel = new MaterialModel(this);
m_materialModel->addMaterial("Default", 1e-3, 1e-5);
m_materialModel->addMaterial("Air", 0.0, 0.0);
m_materialModel->addMaterial("Particle", 6e-4, 2e-8);
m_materialModel->addMaterial("Substrate", 6e-6, 2e-8);
m_materialEditor = new MaterialEditor(m_materialModel);
// m_materialModel->addMaterial("Default", 1e-3, 1e-5);
// m_materialModel->addMaterial("Air", 0.0, 0.0);
// m_materialModel->addMaterial("Particle", 6e-4, 2e-8);
// m_materialModel->addMaterial("Substrate", 6e-6, 2e-8);
// m_materialEditor = new MaterialEditor(m_materialModel);
}
void MainWindow::initSampleModel()
void MainWindow::createSampleModel()
{
Q_ASSERT(m_materialModel);
delete m_sampleModel;
m_sampleModel = new SampleModel(this);
connect(m_materialModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), m_sampleModel, SLOT(onMaterialModelChanged(QModelIndex,QModelIndex)));
// ParameterizedItem *multilayer = m_sampleModel->insertNewItem(Constants::MultiLayerType);
// ParameterizedItem *layer0 = m_sampleModel->insertNewItem(Constants::LayerType, m_sampleModel->indexOfItem(multilayer));
// layer0->setItemName("layer0");
// ParameterizedItem *layer1 = m_sampleModel->insertNewItem(Constants::LayerType, m_sampleModel->indexOfItem(multilayer));
// layer1->setItemName("layer1");
connect(m_materialModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
m_sampleModel, SLOT(onMaterialModelChanged(QModelIndex,QModelIndex)));
}
void MainWindow::initJobModel()
void MainWindow::createJobModel()
{
delete m_jobModel;
m_jobModel = new JobModel(this);
}
void MainWindow::initInstrumentModel()
void MainWindow::createInstrumentModel()
{
delete m_instrumentModel;
m_instrumentModel = new InstrumentModel(this);
m_instrumentModel->setIconProvider(new IconProvider());
}
void MainWindow::initFitModel()
void MainWindow::createFitModel()
{
m_fitProxyModel = new FitProxyModel;
}
//! reset all models to initial state
void MainWindow::resetModels()
{
m_materialModel->clear();
m_materialModel->addMaterial("Default", 1e-3, 1e-5);
m_materialModel->addMaterial("Air", 0.0, 0.0);
m_materialModel->addMaterial("Particle", 6e-4, 2e-8);
m_materialModel->addMaterial("Substrate", 6e-6, 2e-8);
m_sampleModel->clear();
m_jobModel->clear();
m_instrumentModel->clear();
ParameterizedItem *instrument = m_instrumentModel->insertNewItem(Constants::InstrumentType);
instrument->setItemName("Default GISAS");
m_instrumentModel->insertNewItem(Constants::DetectorType, m_instrumentModel->indexOfItem(instrument));
m_instrumentModel->insertNewItem(Constants::BeamType, m_instrumentModel->indexOfItem(instrument));
}
void MainWindow::testGUIObjectBuilder()
{
SampleBuilderFactory factory;
......
......@@ -75,6 +75,7 @@ public slots:
void writeSettings();
void onRunSimulationShortcut();
void onAboutApplication();
void resetModels();
protected:
virtual void closeEvent(QCloseEvent *event);
......@@ -104,16 +105,12 @@ private:
ToolTipDataBase *m_toolTipDataBase;
FitProxyModel *m_fitProxyModel;
void initModels();
void initMaterialModel();
void initSampleModel();
void initJobModel();
void initInstrumentModel();
void initFitModel();
void updateSimModel();
void updateSamples();
void updateInstruments();
void createModels();
void createMaterialModel();
void createSampleModel();
void createJobModel();
void createInstrumentModel();
void createFitModel();
void testGUIObjectBuilder();
};
......
......@@ -158,23 +158,31 @@ bool ProjectDocument::save()
}
bool ProjectDocument::load()
bool ProjectDocument::load(const QString &project_file_name)
{
//qDebug() << "ProjectDocument::load() -> " << getProjectFileName();
bool success_read(false);
m_error_message.clear();
setProjectFileName(project_file_name);
QFile file(getProjectFileName());
if (!file.open(QFile::ReadOnly | QFile::Text)) {
//qDebug() << "ProjectDocument::openExistingDocument -> Error. Can't open file" << getProjectFileName();
return 0;
m_error_message = QString("Can't open '%1'. Wrong permissions or binary file.")
.arg(project_file_name);
return false;
}
//ProjectDocument *result = new ProjectDocument(info.path(), info.baseName());
bool success_read = readFrom(&file);
file.close();
// loading non-xml data
loadOutputData();
try{
// loading project file
success_read = readFrom(&file);
file.close();
// loading accompanying non-xml data
loadOutputData();
} catch(const std::exception &ex) {
m_error_message = QString("Exception was thrown with the error message '%1'")
.arg(QString(ex.what()));
success_read = false;
}
return success_read;
}
......@@ -221,8 +229,10 @@ bool ProjectDocument::readFrom(QIODevice *device)
}
}
if (reader.hasError())
throw GUIHelpers::Error(reader.errorString());
if (reader.hasError()) {
m_error_message = QString("File parse error with error message '%1").arg(reader.errorString());
return false;
}
connect(m_materialModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
connect(m_instrumentModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onDataChanged(QModelIndex, QModelIndex)) );
......
......@@ -53,7 +53,7 @@ public:
ProjectDocument(const QString &path, const QString &name);
bool save();
bool load();
bool load(const QString &project_file_name);
QString getProjectPath() const { return m_project_path; }
QString getProjectName() const { return m_project_name; }
......@@ -73,6 +73,8 @@ public:
bool hasValidNameAndPath();
QString getErrorMessage() const { return m_error_message; }
signals:
void modified();
......@@ -94,6 +96,7 @@ private:
SampleModel *m_sampleModel;
JobModel *m_jobModel;
bool m_modified;
QString m_error_message;
};
......
......@@ -193,17 +193,39 @@ void ProjectManager::openProject(QString fileName)
qDebug() << "ProjectManager::openProject()" << fileName;
if( !closeCurrentProject()) return;
// if(fileName.isEmpty()) {
// fileName = QFileDialog::getOpenFileName(m_mainWindow, tr("Open project file"),
// getDefaultProjectPath(),
// tr("BornAgain project Files (*.pro)"));
// }
if(fileName.isEmpty()) {
fileName = QFileDialog::getOpenFileName(m_mainWindow, tr("Open project file"),
getDefaultProjectPath(),
tr("BornAgain project Files (*.pro)"));
QMessageBox::warning(m_mainWindow, tr("Error while opening project file"),
tr("File name is empty."));
return;
}
QFile fin(fileName);
if(!fin.exists()) {
QMessageBox::warning(m_mainWindow, tr("Error while opening project file"),
QString("File '%1' doesn't exist").arg(fileName));
return;
}
if(!fileName.isEmpty()) {
createNewProject();
m_project_document->setProjectFileName(fileName);
m_project_document->load();
emit modified();
bool success_read = m_project_document->load(fileName);
if(success_read) {
emit modified();
} else {
QMessageBox::warning(m_mainWindow, tr("Error while opening project file"),
QString("Can't load the project '%1' \n\n%2").arg(fileName).arg(m_project_document->getErrorMessage()));
delete m_project_document;
m_project_document = 0;
m_mainWindow->resetModels();
createNewProject();
return;
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment