Commit 5dcd1ace authored by Huff, Israel's avatar Huff, Israel
Browse files

- added Settings class

- added stub file and help menus
parent 8b0cf073
Pipeline #97915 passed with stages
in 5 minutes and 26 seconds
......@@ -3,6 +3,7 @@ SET(PORTAL_HEADERS
rsmportalexample.hh
aptenswidget.hh
aptensdatamodel.hh
settings.hh
)
QT5_WRAP_CPP(PORTAL_MOC_FILES
${PORTAL_HEADERS}
......@@ -14,5 +15,6 @@ TRIBITS_ADD_EXECUTABLE(rsmportalexample
rsmportalexample.cc
aptensdatamodel.cc
aptenswidget.cc
settings.cc
${PORTAL_MOC_FILES} ${PORTAL_HEADERS}
)
#include "aptenswidget.hh"
#include "radixbug/bug.hh"
#include "settings.hh"
#include <QApplication>
#include <QFileDialog>
#include <QFileInfo>
#include <QGridLayout>
#include <QMessageBox>
#include <QTimer>
using namespace AptEns;
......@@ -374,6 +378,38 @@ APTEnsWidget::APTEnsWidget(QWidget *parent)
readFromDataModel();
}
void APTEnsWidget::createMenus(QMenuBar *menuBar)
{
mFileMenu = menuBar->addMenu("File");
mFileMenu->addAction("Open Ensemble", this, SLOT(openEnsembleEvent()),
QKeySequence(tr("Ctrl+o", "File|Open")));
mFileMenu->addAction("Save Ensemble", this, SLOT(saveEnsembleEvent()),
QKeySequence(tr("Ctrl+s", "File|Save")));
mFileMenu->addAction("Close Ensemble", this, SLOT(closeEnsembleEvent()),
QKeySequence(tr("Ctrl+w")));
mFileMenu->addSeparator();
mFileMenu->addAction("Settings", this, SLOT(settingsEvent()),
QKeySequence(tr("F2", "File|Settings")));
mFileMenu->addAction("Exit", this, SLOT(exitEvent()),
QKeySequence(tr("Ctrl+F4", "File|Exit")));
mHelpMenu = menuBar->addMenu("Help");
mHelpMenu->addAction("About", this, SLOT(aboutEvent()),
QKeySequence(tr("Ctrl+hShift+a", "Help|About")));
}
void APTEnsWidget::resetWindowTitle()
{
QString windowTitle = QApplication::applicationName()
.append(": version ")
.append(QApplication::applicationVersion());
if (!mCurrentFile.isEmpty())
windowTitle.append(" Current project: ").append(mCurrentFile);
this->setWindowTitle(windowTitle);
}
void APTEnsWidget::readFromDataModel()
{
APTEnsDataModel &model = mDataModels[mCurrentModel];
......@@ -420,6 +456,102 @@ void APTEnsWidget::readFromDataModel()
mMetEnsNumLineEdit->setText(QString::number(model.metEnsNum()));
}
void APTEnsWidget::openEnsembleEvent()
{
Settings settings;
QString fileName = QFileDialog::getOpenFileName(
this, tr("Open ensemble file"), settings.workspace().path(),
tr("MSTAR project, *.ensz"));
if (!fileName.isEmpty())
{
radix_line("Opening ensemble: " << fileName.toStdString());
mCurrentFile = fileName;
QFileInfo fileInfo(fileName);
// mProjectController.load(Manager::instance()->project().get(),
// fileInfo.absoluteFilePath(),
// Settings::instance()->workspace().path());
resetWindowTitle();
}
}
void APTEnsWidget::saveEnsembleEvent()
{
if (mCurrentFile.isEmpty())
{
saveAsEnsembleEvent();
return;
}
QFileInfo fileInfo(mCurrentFile);
// mProjectController.save(Manager::instance()->project().get(),
// fileInfo.absoluteFilePath(),
// Settings::instance()->workspace().path());
}
void APTEnsWidget::saveAsEnsembleEvent()
{
Settings settings;
QString fileName = QFileDialog::getSaveFileName(
this, tr("Save as"), settings.workspace().path(),
tr("MSTAR project, *.mstarz"));
if (fileName.isEmpty()) return;
if (!fileName.endsWith(".mstarz")) fileName.append(".mstarz");
QFileInfo fileInfo(fileName);
// mProjectController.save(Manager::instance()->project().get(),
// fileInfo.absoluteFilePath(),
// Settings::instance()->workspace().path());
mCurrentFile = fileInfo.absoluteFilePath();
resetWindowTitle();
}
void APTEnsWidget::closeEnsembleEvent()
{
// TODO: close file and clear GUI elements
mCurrentFile.clear();
}
void APTEnsWidget::settingsEvent()
{
// TODO
// SettingsDialog *settingsDialog = new SettingsDialog(this);
// settingsDialog->exec();
// delete settingsDialog;
}
void APTEnsWidget::exitEvent()
{
radix_line("APTEnsWidget::exitEvent()");
this->close();
}
void APTEnsWidget::aboutEvent()
{
QString aboutTitle = QApplication::applicationName().append(": About");
QString aboutContent =
QApplication::applicationName()
.append(": version ")
.append(QApplication::applicationVersion())
.append("\n\n")
.append("<h2>Developed by Oak Ridge National Laboratory:</h2>\n")
.append("<h3>Nuclear Security Modeling Group</h3>\n\n")
.append("For help, email nsmhelp@ornl.gov");
QMessageBox *aboutMessageBox =
new QMessageBox(QMessageBox::Information, aboutTitle, aboutContent);
// TODO
// aboutMessageBox->setWindowIcon(QIcon(":graphics/images/app_icon.png"));
aboutMessageBox->setTextFormat(Qt::RichText);
aboutMessageBox->exec();
delete aboutMessageBox;
}
void APTEnsWidget::yieldDefineEnsStateChange(int state)
{
bool checked = (Qt::Checked == state);
......
......@@ -7,6 +7,8 @@
#include <QLabel>
#include <QLineEdit>
#include <QListWidget>
#include <QMenu>
#include <QMenuBar>
#include <QPushButton>
#include "aptensdatamodel.hh"
......@@ -49,6 +51,11 @@ class APTEnsWidget : public QWidget
QMap<QString, APTEnsDataModel> mDataModels;
QString mCurrentModel;
QString mCurrentFile;
QMenu *mFileMenu;
QMenu *mHelpMenu;
CenterTitleGroupBox *mSelectModelBox;
CenterTitleGroupBox *mCurrentModelBox;
......@@ -116,12 +123,22 @@ class APTEnsWidget : public QWidget
StyledLabel *mMetEnsNumLabel;
LineEditFocusSelectAll *mMetEnsNumLineEdit;
void createMenus(QMenuBar *menuBar);
void resetWindowTitle();
void readFromDataModel();
public:
APTEnsWidget(QWidget *parent = nullptr);
public slots:
void openEnsembleEvent();
void saveEnsembleEvent();
void saveAsEnsembleEvent();
void closeEnsembleEvent();
void settingsEvent();
void exitEvent();
void aboutEvent();
void yieldDefineEnsStateChange(int state);
void hobDefineEnsStateChange(int state);
......
#include <QCoreApplication>
#include <QFileInfo>
#include <QJsonDocument>
#include <QProcessEnvironment>
#include <QSettings>
#include "settings.hh"
#include "radixbug/bug.hh"
namespace AptEns
{
const QString Settings::KEY_WORKSPACE = "public/aptens_workspace";
Settings::Settings()
{
radix_line("Loading JSON settings from config file: "
<< QCoreApplication::instance()->applicationDirPath().toStdString()
<< QDir::separator() << "config.json");
QFileInfo fileInfo(QCoreApplication::instance()->applicationDirPath() +
QDir::separator() + "config.json");
QFile configFile(fileInfo.absoluteFilePath());
if (!configFile.open(QIODevice::ReadOnly))
{
std::stringstream ss;
ss << "Fatal error! Unable to load required config file: "
<< fileInfo.absoluteFilePath().toStdString() << std::endl;
throw std::logic_error(ss.str());
}
QByteArray configFileData = configFile.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(configFileData));
mJsonConfig = jsonDoc.object();
}
// Static pointer allocation
Settings::SP Settings::mInstance = nullptr;
Settings::SP& Settings::instance()
{
if (!mInstance)
{
mInstance = std::make_shared<Settings>();
}
return mInstance;
}
QDir Settings::workspace()
{
QSettings settings;
radix_line("Getting workspace setting. "
<< "First choice: "
<< settings.value(Settings::KEY_WORKSPACE).toString().toStdString()
<< "; Second choice: "
<< QDir::home().absoluteFilePath(".aptens").toStdString());
// Use home/.aptens if setting isn't set
return QDir(settings
.value(Settings::KEY_WORKSPACE,
QDir::home().absoluteFilePath(".aptens"))
.toString());
}
void Settings::updateSetting(QString key, QVariant value)
{
radix_line("Setting updated with key: " << key.toStdString() << "; value: "
<< value.toString().toStdString());
QSettings settings;
settings.setValue(key, value);
emit settingUpdated(key);
}
QString Settings::getJsonConfig(const char* key) const
{
radix_line("Getting environment/JSON config element: " << key);
QString qkey = key;
qkey = qkey.toUpper();
// Allow the system environment to take precedence
// i.e. if there is an env. variable with this name, use that instead
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString qvalue = env.value(qkey);
if (!qvalue.isEmpty())
{
radix_line("Found in system environment: " << qvalue.toStdString());
return qvalue;
}
// Otherwise obtain value from JSON config
if (mJsonConfig.contains(key))
{
QString value = mJsonConfig[key].toString();
radix_line("Found in JSON config: " << value.toStdString());
return value;
}
// Otherwise we don't have it, return empty string
radix_line("Element not found");
return QString();
}
QString Settings::getJsonConfigAsPath(const char* key) const
{
// Get the initial configuration
QString value = getJsonConfig(key);
// Prepend application directory to ensure correct directory found
value.prepend(
QCoreApplication::applicationDirPath().append(QDir::separator()));
value = QDir::toNativeSeparators(value);
radix_line("Value with prepended path:" << value.toStdString());
return value;
}
} // namespace AptEns
#ifndef RSM_RSMWIDGETSEXAMPLE_SETTINGS_HH
#define RSM_RSMWIDGETSEXAMPLE_SETTINGS_HH
#include <memory>
#include <QDir>
#include <QJsonObject>
#include <QObject>
#include <QString>
#include <QVariant>
namespace AptEns
{
class Settings : public QObject
{
Q_OBJECT
public:
typedef std::shared_ptr<Settings> SP;
// singleton member
static Settings::SP& instance();
// workspace (working directory)
static const QString KEY_WORKSPACE;
static QDir workspace();
void updateSetting(QString key, QVariant value);
Settings();
QString getJsonConfig(const char* key) const;
QString getJsonConfigAsPath(const char* key) const;
private:
static Settings::SP mInstance;
QJsonObject mJsonConfig;
signals:
void settingUpdated(QString key);
}; // class Settings
} // namespace AptEns
#endif // RSM_RSMWIDGETSEXAMPLE_SETTINGS_HH
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment