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

Merge branch 'RecentFiles'

parents bad3e154 852fb31a
No related branches found
No related tags found
No related merge requests found
......@@ -14,7 +14,7 @@ set(BornAgain_VERSION_PATCH 4)
# --- General project settings ---
option(BORNAGAIN_PYTHON "Build with python support" ON)
option(BORNAGAIN_APP "Build test application" OFF)
option(BORNAGAIN_GUI "Build a graphical user interface" OFF)
option(BORNAGAIN_GUI "Build a graphical user interface" ON)
option(BORNAGAIN_MAN "Build a user manual" OFF)
option(BUILD_DEBIAN "Build a debian package" OFF)
......
......@@ -2,7 +2,13 @@
#include "hostosinfo.h"
#include "mainwindow.h"
#include "mainwindow_constants.h"
#include "projectmanager.h"
#include "stringutils.h"
#include <QMenuBar>
#include <QSettings>
#include <QFileInfo>
#include <QDebug>
#include <QDir>
#include <iostream>
ActionManager::ActionManager(MainWindow *parent)
......@@ -23,26 +29,27 @@ ActionManager::ActionManager(MainWindow *parent)
void ActionManager::createActions()
{
// new project action
QIcon icon = QIcon::fromTheme(QLatin1String("document-new"), QIcon(QLatin1String(Constants::ICON_NEWFILE)));
m_newAction = new QAction(icon, tr("&New Project"), m_mainWindow);
m_newAction->setShortcuts(QKeySequence::New);
m_newAction->setStatusTip(tr("Create a new project"));
connect(m_newAction, SIGNAL(triggered()), m_mainWindow, SLOT(newProject()) );
// open project action
icon = QIcon::fromTheme(QLatin1String("document-open"), QIcon(QLatin1String(Constants::ICON_OPENFILE)));
m_openAction = new QAction(icon, tr("&Open Project"), m_mainWindow);
m_openAction->setShortcuts(QKeySequence::Open);
m_openAction->setStatusTip(tr("Open an existing file"));
connect(m_openAction, SIGNAL(triggered()), m_mainWindow, SLOT(openProject()) );
// exit application action
icon = QIcon::fromTheme(QLatin1String("application-exit"));
m_exitAction = new QAction(icon, tr("E&xit Application"), this);
m_exitAction->setShortcuts(QKeySequence::Quit);
m_exitAction->setStatusTip(tr("Exit the application"));
connect(m_exitAction, SIGNAL(triggered()), m_mainWindow, SLOT(close()));
}
......@@ -53,12 +60,46 @@ void ActionManager::createMenus()
if (!Utils::HostOsInfo::isMacHost()) // System menu bar on Mac
m_mainWindow->setMenuBar(m_menuBar);
// File Menu
m_fileMenu = m_menuBar->addMenu(tr("&File"));
m_fileMenu->addAction(m_newAction);
m_fileMenu->addAction(m_openAction);
connect(m_fileMenu, SIGNAL(aboutToShow()), this, SLOT(aboutToShowRecentProjects()));
m_recentProjectsMenu = m_fileMenu->addMenu("&Recent Projects");
m_fileMenu->addSeparator();
m_fileMenu->addAction(m_exitAction);
// Help Menu
m_helpMenu = m_menuBar->addMenu(tr("&Help"));
}
void ActionManager::aboutToShowRecentProjects()
{
qDebug() << "ActionManager::aboutToShowRecentProjects() ->" << m_mainWindow->getProjectManager()->getRecentProjects();
m_recentProjectsMenu->clear();
bool hasRecentProjects = false;
foreach(QString file, m_mainWindow->getProjectManager()->getRecentProjects() ) {
hasRecentProjects = true;
qDebug() << file << QDir::toNativeSeparators(Utils::withTildeHomePath(file));
QAction *action = m_recentProjectsMenu->addAction(QDir::toNativeSeparators(Utils::withTildeHomePath(file)));
action->setData(qVariantFromValue(file));
connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(openRecentProject()));
}
m_recentProjectsMenu->setEnabled(hasRecentProjects);
if (hasRecentProjects) {
m_recentProjectsMenu->addSeparator();
QAction *action = m_recentProjectsMenu->addAction("Clear Menu");
connect(action, SIGNAL(triggered()), m_mainWindow->getProjectManager(), SLOT(clearRecentProjects()));
}
}
......@@ -3,6 +3,7 @@
#include <QObject>
#include <QList>
class QMenu;
......@@ -19,15 +20,20 @@ class ActionManager : public QObject
public:
ActionManager(MainWindow *parent);
public slots:
void aboutToShowRecentProjects();
private:
MainWindow *m_mainWindow;
QAction *m_newAction;
QAction *m_openAction;
QAction *m_exitAction;
QList<QAction *> m_recentProjectActions;
QMenuBar *m_menuBar;
QMenu *m_fileMenu;
QMenu *m_recentProjectsMenu;
QMenu *m_helpMenu;
void createActions();
......
......@@ -36,8 +36,13 @@ MainWindow::MainWindow(QWidget *parent)
, m_fitView(0)
, m_actionManager(0)
, m_projectManager(0)
, m_settings(new QSettings(Constants::APPLICATION_NAME, Constants::APPLICATION_NAME, this))
, mp_sim_data_model(0)
{
// QCoreApplication::setApplicationName(QLatin1String(Constants::APPLICATION_NAME));
// QCoreApplication::setApplicationVersion(QLatin1String(Constants::APPLICATION_VERSION));
// QCoreApplication::setOrganizationName(QLatin1String(Constants::APPLICATION_NAME));
// initialize simulation data model first:
initSimModel();
......@@ -45,6 +50,7 @@ MainWindow::MainWindow(QWidget *parent)
qApp->setStyle(new ManhattanStyle(baseName));
Manhattan::Utils::StyleHelper::setBaseColor(QColor(0x086FA1));
setDockNestingEnabled(true);
setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
......@@ -71,20 +77,55 @@ MainWindow::MainWindow(QWidget *parent)
setCentralWidget(m_tabWidget);
m_actionManager = new ActionManager(this);
m_projectManager = new ProjectManager(this);
m_projectManager = new ProjectManager(this);
setAcceptDrops(true);
// signals/slots
connect(m_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(onChangeTabWidget(int)));
readSettings();
}
MainWindow::~MainWindow()
{
// delete m_actionManager;
// delete m_settings;
// m_settings = 0;
}
void MainWindow::readSettings()
{
if(m_settings->childGroups().contains("MainWindow")) {
m_settings->beginGroup("MainWindow");
resize(m_settings->value("size", QSize(400, 400)).toSize());
move(m_settings->value("pos", QPoint(200, 200)).toPoint());
m_settings->endGroup();
}
assert(m_projectManager);
m_projectManager->readSettings(m_settings);
}
void MainWindow::writeSettings()
{
m_settings->beginGroup("MainWindow");
m_settings->setValue("size", size());
m_settings->setValue("pos", pos());
m_settings->endGroup();
m_projectManager->writeSettings(m_settings);
m_settings->sync();
}
QSettings *MainWindow::getSettings() const
{
return m_settings;
}
void MainWindow::newProject()
{
......@@ -94,10 +135,21 @@ void MainWindow::newProject()
void MainWindow::openProject()
{
m_projectManager->openExistingProject();
m_projectManager->openProject();
}
void MainWindow::openRecentProject()
{
if (const QAction *action = qobject_cast<const QAction*>(sender())) {
QString file = action->data().value<QString>();
qDebug() << "MainWindow::openRecentProject() -> " << file;
m_projectManager->openProject(file);
}
}
void MainWindow::onChangeTabWidget(int index)
{
// update views which depend on others
......@@ -108,6 +160,14 @@ void MainWindow::onChangeTabWidget(int index)
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
writeSettings();
event->accept();
}
void MainWindow::initSimModel()
{
if (mp_sim_data_model) delete mp_sim_data_model;
......
......@@ -3,6 +3,7 @@
#include <QMainWindow>
#include "fancymainwindow.h"
#include "mainwindow_constants.h"
namespace Manhattan {
class FancyTabWidget;
......@@ -21,6 +22,8 @@ class Instrument;
class ISample;
class ActionManager;
class ProjectManager;
class QCloseEvent;
class QSettings;
//class MainWindow : public QMainWindow
......@@ -38,7 +41,15 @@ public slots:
void onChangeTabWidget(int index);
void newProject();
void openProject();
void openRecentProject();
void readSettings();
void writeSettings();
QSettings *getSettings() const;
ActionManager *getActionManager() { return m_actionManager; }
ProjectManager *getProjectManager() { return m_projectManager; }
protected:
virtual void closeEvent(QCloseEvent *event);
private:
Manhattan::FancyTabWidget *m_tabWidget;
......@@ -51,7 +62,8 @@ private:
FitView * m_fitView;
ActionManager *m_actionManager; //!< responsible for menus and actions
ProjectManager *m_projectManager; //! handles activity related to opening/saving projects
ProjectManager *m_projectManager; //!< handles activity related to opening/saving projects
QSettings *m_settings; //!< application wide settings
SimulationDataModel *mp_sim_data_model;
// dummy simulation model initializer for test purposes
......
......@@ -4,6 +4,10 @@
namespace Constants {
// general application settings
const char APPLICATION_NAME[] = "BornAgain";
const char APPLICATION_VERSION[] = "0.9";
const char ORGANIZATION_NAME[] = "Scientific Computing at MLZ";
// Menubar
const char MENU_BAR[] = "BornAgain.MenuBar";
......@@ -12,10 +16,13 @@ const char MENU_BAR[] = "BornAgain.MenuBar";
const char G_FILE[] = "BornAgain.Group.File";
const char G_HELP[] = "BornAgain.Group.Help";
const char ICON_NEWFILE[] = ":/core/images/filenew.png";
const char ICON_OPENFILE[] = ":/core/images/fileopen.png";
const char ICON_NEWFILE[] = ":/core/images/filenew.png";
const char ICON_OPENFILE[] = ":/core/images/fileopen.png";
// settings groups
const char S_PROJECTMANAGER[] = "ProjectManager";
const int MAX_RECENT_PROJECTS = 7;
}
......
......@@ -47,7 +47,7 @@ NewProjectDialog::NewProjectDialog(QWidget *parent)
m_createButton = new QPushButton(tr("Create"));
connect(m_createButton, SIGNAL(clicked()), this, SLOT(createProjectDir()));
m_cancelButton = new QPushButton(tr("Cancel"));
connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(close()));
connect(m_cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
QGroupBox *projectGroup = new QGroupBox(tr("Project name and location"));
......@@ -85,6 +85,7 @@ void NewProjectDialog::setDirectory()
if (!dirname.isEmpty()) {
checkIfProjectPathIsValid(dirname);
checkIfProjectNameIsValid(getProjectName());
}
}
......@@ -173,7 +174,7 @@ void NewProjectDialog::createProjectDir()
if( !parentDir.mkdir(getProjectName()) ) {
m_warningLabel->setText("<font color='darkRed'> Can't make subdirectory' '"+getProjectName()+"' in '"+getProjectPath()+"' </font>");
} else {
close();
accept();
}
}
......@@ -19,6 +19,7 @@ public:
void setProjectPath(const QString &text) { m_project_path = text; }
void setProjectName(const QString &text) { m_project_name = text; }
QString getProjectFileName();
static ProjectDocument *openExistingDocument(const QString &filename);
......@@ -26,7 +27,6 @@ private:
bool write(QIODevice *device);
bool read(QIODevice *device);
void generate_clean_document();
QString getProjectFileName();
QString m_project_path;
QString m_project_name;
......
......@@ -2,8 +2,11 @@
#include "newprojectdialog.h"
#include "mainwindow.h"
#include "projectdocument.h"
#include "actionmanager.h"
#include <QDir>
#include <QFileDialog>
#include <QSettings>
#include <QDebug>
#include <iostream>
ProjectManager::ProjectManager(MainWindow *parent)
......@@ -21,8 +24,19 @@ ProjectManager::~ProjectManager()
}
void ProjectManager::closeProject()
void ProjectManager::closeCurrentProject()
{
if(m_project_document) {
std::cout << "ProjectManager::closeProject() -> XXX" << m_project_document->getProjectFileName().toStdString() << std::endl;
QString fileName = m_project_document->getProjectFileName();
m_recentProjects.removeAll(fileName);
m_recentProjects.prepend(fileName);
while (m_recentProjects.size() > Constants::MAX_RECENT_PROJECTS)
m_recentProjects.removeLast();
}
delete m_project_document;
m_project_document = 0;
......@@ -37,27 +51,31 @@ void ProjectManager::createNewProject()
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());
if (dialog.exec() == QDialog::Accepted) {
closeCurrentProject();
m_defaultProjectPath = dialog.getProjectPath();
m_project_document = new ProjectDocument(dialog.getProjectPath(), dialog.getProjectName());
m_project_document->save();
m_mainWindow->setWindowTitle(m_project_document->getProjectName());
}
}
void ProjectManager::openExistingProject()
//! Opens existing project. If fileName is empty, will popup file selection dialog
void ProjectManager::openProject(QString fileName)
{
QString fileName = QFileDialog::getOpenFileName(m_mainWindow, tr("Open project file"),
if(fileName.isEmpty()) {
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();
closeCurrentProject();
m_project_document = newdocument;
m_mainWindow->setWindowTitle(m_project_document->getProjectName());
}
......@@ -65,6 +83,54 @@ void ProjectManager::openExistingProject()
}
//! read settings of ProjectManager from global settings
void ProjectManager::readSettings(QSettings *settings)
{
m_defaultProjectPath = QDir::homePath();
if(settings->childGroups().contains(Constants::S_PROJECTMANAGER)) {
settings->beginGroup(Constants::S_PROJECTMANAGER);
m_defaultProjectPath = settings->value("DefaultProjectPath").toString();
m_recentProjects = settings->value("RecentProjects").toStringList();
settings->endGroup();
}
qDebug() << "ProjectManager::readSettings() -> " << this->objectName() << m_defaultProjectPath << m_recentProjects;
}
//! saves settings of ProjectManager in global settings
void ProjectManager::writeSettings(QSettings *settings)
{
qDebug() << "ProjectManager::writeSettings() -> Writing " << m_defaultProjectPath << m_recentProjects;
settings->beginGroup(Constants::S_PROJECTMANAGER);
settings->setValue("DefaultProjectPath", m_defaultProjectPath);
settings->setValue("RecentProjects", m_recentProjects);
settings->endGroup();
}
//! returns list of recent projects
QStringList ProjectManager::getRecentProjects()
{
return m_recentProjects;
}
//! clear list of recent projects
void ProjectManager::clearRecentProjects()
{
m_recentProjects.clear();
}
//! returns default project path
QString ProjectManager::getDefaultProjectPath()
{
return m_defaultProjectPath;
}
//! Will return 'Untitled' if the directory with such name doesn't exist in project
//! path. Otherwise will return Untitled1, Untitled2 etc.
QString ProjectManager::getUntitledProjectName()
......@@ -82,9 +148,3 @@ QString ProjectManager::getUntitledProjectName()
}
//! return default project path
QString ProjectManager::getDefaultProjectPath()
{
return QDir::homePath();
}
......@@ -4,8 +4,10 @@
#include <QObject>
#include <QString>
#include <QStringList>
class MainWindow;
class ProjectDocument;
class QSettings;
//! handles activity related to opening/save projects
class ProjectManager : public QObject
......@@ -16,19 +18,28 @@ public:
~ProjectManager();
void createNewProject();
void openExistingProject();
void closeProject();
void openProject(QString fileName = QString());
void closeCurrentProject();
void readSettings(QSettings *settings);
void writeSettings(QSettings *settings);
QStringList getRecentProjects();
public slots:
void clearRecentProjects();
private:
QString getUntitledProjectName();
QString getDefaultProjectPath();
QString getUntitledProjectName();
MainWindow *m_mainWindow;
ProjectDocument *m_project_document;
QString m_defaultProjectPath;
QStringList m_recentProjects;
};
......
#include "stringutils.h"
#include "hostosinfo.h"
#include <QFileInfo>
#include <QDir>
namespace Utils
{
QString withTildeHomePath(const QString &path)
{
if (Utils::HostOsInfo::isWindowsHost())
return path;
static const QString homePath = QDir::homePath();
QFileInfo fi(QDir::cleanPath(path));
QString outPath = fi.absoluteFilePath();
if (outPath.startsWith(homePath))
outPath = QLatin1Char('~') + outPath.mid(homePath.size());
else
outPath = path;
return outPath;
}
}
#ifndef STRINGUTILS_H
#define STRINGUTILS_H
#include <QString>
namespace Utils
{
QString withTildeHomePath(const QString &path);
}
#endif
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