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

New ProjectDocument and possibility to open existing project.

parent d397f5ff
No related branches found
No related tags found
No related merge requests found
......@@ -88,12 +88,13 @@ MainWindow::~MainWindow()
void MainWindow::newProject()
{
m_projectManager->newProject();
m_projectManager->createNewProject();
}
void MainWindow::openProject()
{
m_projectManager->openExistingProject();
}
......
......@@ -51,7 +51,7 @@ private:
FitView * m_fitView;
ActionManager *m_actionManager; //!< responsible for menus and actions
ProjectManager *m_projectManager; //! responsible for opening and saving projects
ProjectManager *m_projectManager; //! handles activity related to opening/saving projects
SimulationDataModel *mp_sim_data_model;
// dummy simulation model initializer for test purposes
......
......@@ -158,7 +158,11 @@ void NewProjectDialog::updateWarningStatus()
m_warningLabel->setText("<font color='darkRed'> The path '"+getProjectPath()+"' does not exist. </font>");
} else if(!m_valid_projectName ) {
m_createButton->setEnabled(false);
m_warningLabel->setText("<font color='darkRed'> The directory '"+getProjectName()+"' already exists. </font>");
if(getProjectName().isEmpty()) {
m_warningLabel->setText("<font color='darkRed'> Please specify project name. </font>");
} else {
m_warningLabel->setText("<font color='darkRed'> The directory '"+getProjectName()+"' already exists. </font>");
}
}
}
......@@ -167,7 +171,7 @@ void NewProjectDialog::createProjectDir()
{
QDir parentDir = getProjectPath();
if( !parentDir.mkdir(getProjectName()) ) {
m_warningLabel->setText("<font color='darkRed'> Can't make subdirectory' '"+getProjectName()+"' in '."+getProjectPath()+"' </font>");
m_warningLabel->setText("<font color='darkRed'> Can't make subdirectory' '"+getProjectName()+"' in '"+getProjectPath()+"' </font>");
} else {
close();
}
......
#include "projectdocument.h"
#include <QFile>
#include <QTextStream>
#include <QFileInfo>
#include <QDir>
#include <iostream>
ProjectDocument::ProjectDocument(const QString &path, const QString &name)
: m_project_path(path)
, m_project_name(name)
{
// generate_clean_document();
}
//void ProjectDocument::generate_clean_document()
//{
// QDomProcessingInstruction xmlHeaderPI = m_dom_document.createProcessingInstruction("xml", "version=\"1.0\" " );
// m_dom_document.appendChild(xmlHeaderPI);
// QDomElement root = m_dom_document.createElement("BornAgain");
// m_dom_document.appendChild(root);
// QDomElement childElement = m_dom_document.createElement("project");
// childElement.setAttribute(QString("name"), QString("Untitled"));
// root.appendChild(childElement);
//}
bool ProjectDocument::save()
{
QString filename = getProjectFileName();
std::cout << "ProjectDocument::saveProjectFile " << filename.toStdString()<< std::endl;
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
std::cout << "ProjectDocument::save() -> Error! Can't save file" << std::endl;
return false;
}
write(&file);
file.close();
return true;
}
ProjectDocument *ProjectDocument::openExistingDocument(const QString &filename)
{
std::cout << "ProjectDocument::openExistingDocument -> " << filename.toStdString() << std::endl;
QFileInfo info(filename);
std::cout << " " << info.baseName().toStdString() << " " << info.path().toStdString() << std::endl;
QFile file(filename);
if (!file.open(QFile::ReadOnly | QFile::Text)) {
std::cout << "ProjectDocument::openExistingDocument -> Error. Can't open file" << std::endl;
return 0;
}
ProjectDocument *result = new ProjectDocument(info.path(), info.baseName());
bool success_read = result->read(&file);
file.close();
if(success_read) {
return result;
} else {
delete result;
return 0;
}
}
bool ProjectDocument::read(QIODevice *device)
{
QString errorStr;
int errorLine;
int errorColumn;
QDomDocument domDocument;
if (!domDocument.setContent(device, true, &errorStr, &errorLine,
&errorColumn)) {
std::cout << "ProjectDocument::read() -> parse error!" << std::endl;
// QMessageBox::information(0, tr("DOM Bookmarks"),
// tr("Parse error at line %1, column %2:\n%3")
// .arg(errorLine)
// .arg(errorColumn)
// .arg(errorStr));
return false;
}
QDomElement root = domDocument.documentElement();
if (root.tagName() != "BornAgain") {
std::cout << "ProjectDocument::read() -> This is not BornAgain file" << std::endl;
return false;
}
QDomElement child = root.firstChildElement("project");
QString name = child.attribute("name");
std::cout << "ProjectDocument::read() " << name.toStdString() << std::endl;
return true;
}
bool ProjectDocument::write(QIODevice *device)
{
const int IndentSize = 4;
QTextStream out(device);
QDomDocument dom_document("BornAgain");
QDomProcessingInstruction xmlHeaderPI = dom_document.createProcessingInstruction("xml", "version=\"1.0\" " );
dom_document.appendChild(xmlHeaderPI);
QDomElement root = dom_document.createElement("BornAgain");
dom_document.appendChild(root);
QDomElement childElement = dom_document.createElement("project");
childElement.setAttribute(QString("name"), getProjectName());
root.appendChild(childElement);
dom_document.save(out, IndentSize);
return true;
}
//! constructs project file name from ProjectPath and ProjectName
//! if ProjectPath=/home/username and ProjectName=MultiLayer then project file
//! will be /home/username/MultiLayer/MultiLayer.pro
QString ProjectDocument::getProjectFileName()
{
QString result = getProjectPath() + "/" + getProjectName() + "/"+getProjectName()+".pro";
return result;
}
#ifndef PROJECTDOCUMENT_H
#define PROJECTDOCUMENT_H
#include <QDomDocument>
//! Project document class handles all data related to the opened project
//! (sample, jobModel, project specific windows settings)
class ProjectDocument
{
public:
ProjectDocument(){}
ProjectDocument(const QString &path, const QString &name);
bool save();
QString getProjectPath() const { return m_project_path; }
QString getProjectName() const { return m_project_name; }
void setProjectPath(const QString &text) { m_project_path = text; }
void setProjectName(const QString &text) { m_project_name = text; }
static ProjectDocument *openExistingDocument(const QString &filename);
private:
bool write(QIODevice *device);
bool read(QIODevice *device);
void generate_clean_document();
QString getProjectFileName();
QString m_project_path;
QString m_project_name;
// QDomDocument m_dom_document;
};
#endif
#include "projectmanager.h"
#include "newprojectdialog.h"
#include "mainwindow.h"
#include "projectdocument.h"
#include <QDir>
#include <QFileDialog>
#include <iostream>
ProjectManager::ProjectManager(MainWindow *parent)
: m_mainWindow(parent)
, m_project_path(QDir::homePath())
, m_project_document(0)
{
setParent(parent);
}
ProjectManager::~ProjectManager()
{
delete m_project_document;
}
void ProjectManager::closeProject()
{
delete m_project_document;
m_project_document = 0;
void ProjectManager::newProject()
}
//! call dialog window to define project path and name
void ProjectManager::createNewProject()
{
NewProjectDialog dialog(m_mainWindow);
dialog.setProjectPath(getProjectPath());
// give projectDialog something to start with
dialog.setProjectPath(getDefaultProjectPath());
dialog.setProjectName(getUntitledProjectName());
dialog.exec();
closeProject();
m_project_document = new ProjectDocument(dialog.getProjectPath(), dialog.getProjectName());
m_project_document->save();
m_mainWindow->setWindowTitle(m_project_document->getProjectName());
}
// will return 'Untitled' if it doesn't exist in the project_path (i.e. /home/username),
// otherwise Untitled1, Untitled2 etc...
void ProjectManager::openExistingProject()
{
QString fileName = QFileDialog::getOpenFileName(m_mainWindow, tr("Open project file"),
getDefaultProjectPath(),
tr("BornAgain project Files (*.pro)"));
if(!fileName.isEmpty()) {
ProjectDocument *newdocument = ProjectDocument::openExistingDocument(fileName);
if(newdocument) {
closeProject();
m_project_document = newdocument;
m_mainWindow->setWindowTitle(m_project_document->getProjectName());
}
}
}
//! Will return 'Untitled' if the directory with such name doesn't exist in project
//! path. Otherwise will return Untitled1, Untitled2 etc.
QString ProjectManager::getUntitledProjectName()
{
QString result = "Untitled";
QDir projectDir = getProjectPath() + "/" + result;
QDir projectDir = getDefaultProjectPath() + "/" + result;
if(projectDir.exists()) {
std::cout << "ProjectManager::getUntitledProjectName() -> exists " << result.toStdString() << std::endl;
for(size_t i=1; i<99; ++i) {
result = QString("Untitled")+QString::number(i);
projectDir = getProjectPath() + "/" + result;
projectDir = getDefaultProjectPath() + "/" + result;
if(!projectDir.exists()) break;
std::cout << "ProjectManager::getUntitledProjectName() -> exists " << result.toStdString() << std::endl;
}
}
return result;
}
//! return default project path
QString ProjectManager::getDefaultProjectPath()
{
return QDir::homePath();
}
......@@ -5,28 +5,31 @@
#include <QObject>
#include <QString>
class MainWindow;
class ProjectDocument;
//! class responsible for opening and saving projects
//! handles activity related to opening/save projects
class ProjectManager : public QObject
{
Q_OBJECT
public:
ProjectManager(MainWindow *parent);
~ProjectManager();
QString getProjectPath() const { return m_project_path; }
QString getProjectName() const { return m_project_name; }
//! call dialog window to define project path and name
void newProject();
void createNewProject();
void openExistingProject();
void closeProject();
private:
QString getUntitledProjectName();
QString getDefaultProjectPath();
MainWindow *m_mainWindow;
QString m_project_path;
QString m_project_name;
ProjectDocument *m_project_document;
};
......
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