Skip to content

Commit

Permalink
Finally works reliably on Mac
Browse files Browse the repository at this point in the history
  • Loading branch information
nfette committed Jan 31, 2018
1 parent a89fdd2 commit 19e7a01
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 16 deletions.
26 changes: 19 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
#include "sorputils.h"
#include <QApplication>
#include <QDomImplementation>
#include <QDebug>
// #include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QDateTime>

/// start a new mainwindow and leave all interactions to the mainwindow
/// - once the mainwindow is closed, the application is closed as well
Expand All @@ -29,15 +32,24 @@ int main(int argc, char *argv[])
QApplication a(argc, argv);
QDomImplementation::setInvalidDataPolicy(QDomImplementation::ReturnNullNode);

QFile logfile(Sorputils::sorpLog());
logfile.open(QFile::Append | QFile::Text);
QTextStream log(&logfile);
log << QString("[%1] Starting up.").arg(QDateTime::currentDateTime().toString(Qt::ISODate)) << endl;

QDir dir = qApp->applicationDirPath();
qDebug() << "applicationDirPath() =" << dir;
qDebug() << "absolutePath() =" << dir.absolutePath();
qDebug() << "canonicalPath =" << dir.canonicalPath();
log << "applicationDirPath() =" << dir.path() << endl;
log << "absolutePath() =" << dir.absolutePath() << endl;
log << "canonicalPath =" << dir.canonicalPath() << endl;

QDir dir2;
qDebug() << "Default dir =" << dir2;
qDebug() << "absolutePath =" << dir2.absolutePath();
qDebug() << "canonicalPath =" << dir2.canonicalPath();
log << "Default dir =" << dir2.path() << endl;
log << "absolutePath =" << dir2.absolutePath() << endl;
log << "canonicalPath =" << dir2.canonicalPath() << endl;

log << endl << endl;
log.flush();
logfile.close();

Sorputils::init();

Expand Down
9 changes: 5 additions & 4 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
#include "calcdetaildialog.h"
#include "guessdialog.h"
#include "sorputils.h"

#include "version.h"

/*! \name Units and links
Expand Down Expand Up @@ -3815,15 +3815,16 @@ void MainWindow::evokeAbout()
QMessageBox aboutDialog(this);
aboutDialog.setWindowTitle("Welcome to SorpSim");
aboutDialog.setTextFormat(Qt::RichText);
aboutDialog.setText("<p align='center'><font size = 8 color = blue style = 'italic'>SorpSim 1.2</font><br>"
aboutDialog.setText("<p align='center'><font size=8 color=blue style='italic'>SorpSim&nbsp;</font>"
+ QString("<font size=2 color=blue>%1</font><br>").arg(SORP_VERSION) +
"<br>"
// fixed: Create a plaintext file for license
// TODO: update copyright holders to match code
"<font size = 2>Copyright 2015, UT-Battelle, LLC<br>"
"Copyright 2017-2018, Nicholas Fette<br>"
"All rights reserved<br>"
"Sorption system Simulation program (SorpSim), Version 1.2<br>"
"OPEN SOURCE LICENSE</font></p>"
"Sorption system Simulation program (SorpSim)<br>"
"OPEN SOURCE LICENSE V1</font></p>"
// TODO: update license
// TODO: wrap with scroll area from here ...
"<p align = 'left'><font size = 2>Subject to the conditions of this License, UT-Bettelle, LLC (the 'Licensor')\
Expand Down
28 changes: 24 additions & 4 deletions src/sorpsim-opt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ TEMPLATE = app
# Conveniences for building to debug and deploy
#----------------------------------------------

defineReplace(sorpVersion){
SORPVERSION = $$system("git describe --tags")
SORPVERSION_DEFINE = \\\"$$quote($$SORPVERSION)\\\"
message($$SORPVERSION_DEFINE)
export(SORPVERSION)
return($$SORPVERSION_DEFINE)
}

DEFINES += SORPVERSION=$$sorpVersion()

# https://stackoverflow.com/questions/3984104/qmake-how-to-copy-a-file-to-the-output
# Copies the given files to the destination directory
defineReplace(copySafe){
Expand Down Expand Up @@ -100,8 +110,12 @@ BUNDLE_NAME = $${TARGET}.app
BUNDLE_PATH = $$OUT_PWD/$$BUNDLE_NAME
EXE_PATH = $$BUNDLE_PATH/Contents/MacOS/$$TARGET
QWT_DEST = $$BUNDLE_PATH/Contents/Frameworks/qwt.framework
#deployall.target = deploy
deployall.depends = deployqwt deployqt
# Usage: make deploy
# - Installs qwt.framework in the bundle (deployqwt)
# - Runs the Qt deployment tool (deployqt)
# - Edits the Info.plist file for "Get info" dialogs (deployinfo)
deployall.target = deploy
deployall.depends = deployqwt deployqt deployinfo
MACDEPLOY = macdeployqt
deployqt.depends = deployqwt
deployqt.commands = $$MACDEPLOY $$BUNDLE_PATH
Expand All @@ -110,8 +124,13 @@ deployqwt1.target = $$BUNDLE_NAME/Contents/Frameworks/qwt.framework/qwt
deployqwt1.commands = test -d $$BUNDLE_PATH/Contents/Frameworks || mkdir -p $$BUNDLE_PATH/Contents/Frameworks $$escape_expand(\\n\\t)
deployqwt1.commands += $$QMAKE_DEL_TREE $$QWT_DEST $$escape_expand(\\n\\t)
deployqwt1.commands += $$QMAKE_COPY_DIR $$QWT_INSTALL_LIBS/qwt.framework $$QWT_DEST
deployqwt2.depends = $(TARGET)
deployqwt2.commands = install_name_tool -change qwt.framework/Versions/6/qwt @executable_path/../Frameworks/qwt.framework/Versions/6/qwt $$EXE_PATH
QMAKE_EXTRA_TARGETS += deployall deployqt deployqwt deployqwt1 deployqwt2
deployinfo.depends = $(TARGET)
deployinfo.commands = defaults write $$BUNDLE_PATH/Contents/Info.plist \"CFBundleGetInfoString\" \'$$SORPVERSION\' $$escape_expand(\\n\\t)
deployinfo.commands += defaults write $$BUNDLE_PATH/Contents/Info.plist \"CFBundleIdentifier\" \'info.nfette.sorpsim-opt\'

QMAKE_EXTRA_TARGETS += deployall deployqt deployqwt deployqwt1 deployqwt2 deployinfo

# This goes with something else above ... need to improve structure
macx:mythinga.path = $$BUNDLE_PATH/Contents/Resources/settings
Expand Down Expand Up @@ -246,7 +265,8 @@ HEADERS += \
unitsettingdialog.h \
curvesettingdialog.h \
ifixdialog.h \
sorputils.h
sorputils.h \
version.h

FORMS += \
treedialog.ui \
Expand Down
13 changes: 13 additions & 0 deletions src/sorputils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,27 @@ QDir Sorputils::sorpResourceDir()

QDir Sorputils::sorpTempDir()
{
#if defined Q_OS_MACOS
QDir result = sorpResourceDir();
if (!result.exists("temp"))
result.mkdir("temp");
result.cd("temp");
return result;
#else
return QDir();
#endif
}

QString Sorputils::sorpSettings()
{
return sorpResourceDir().absoluteFilePath("settings/systemSetting.xml");
}

QString Sorputils::sorpLog()
{
return sorpResourceDir().absoluteFilePath("log");
}

Sorputils::Sorputils()
{

Expand Down
31 changes: 30 additions & 1 deletion src/sorputils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Sorputils
static void init();

//!
//! \brief mapElementsByAttribute lets you use an attribute as a key
//! \brief mapElementsByAttribute lets you use an attribute as a key for traversing a QDomNodeList
//!
//! Assumes that all the nodes in the given list can be cast toElement().
//!
Expand All @@ -36,6 +36,31 @@ class Sorputils
//!
static QMap<QString, QDomElement> mapElementsByAttribute(const QDomNodeList &, QString attr);

/// \name Application file accessors
///
/// Aside from the case files, the GUI part of the application uses files it owns,
/// namely settings, log, and temporary files.
/// We usually want to locate these relative to the executable,
/// but on different operating systems we may want different behavior.
/// So all code in this project should use these functions to determine paths.
///
/// Expected behavior
/// - On Windows and Linux, the executable is just a file, and it can look for all its files in
/// the same directory (or nearby).
/// - On Mac, the executable lives in an app bundle subpath and may want to locate files in a
/// nearby folder called "../Resources".
/// - For now, I expect users to run the executable without installing to system directories.
/// (TODO) For system wide installations, we would want to fix standard
/// paths in user directories.
/// - It is possible for users to run the program via command line intentionally
/// in an arbitrary "current" directory, and they may want to see temp or log files go there.
/// But probably not. On Mac, launching through Finder may set the current directory as /,
/// and other platforms vary as well. So probably it is best to avoid assigning much meaning
/// to the current directory at launch.
///
/// \{
///

/// Returns the directory where resources are based.
/// Resources are files created at install time, including read-only examples and help.
static QDir sorpResourceDir();
Expand All @@ -47,6 +72,10 @@ class Sorputils
/// Returns the path to the settings file.
static QString sorpSettings();

static QString sorpLog();

/// \}

private:
Sorputils();

Expand Down
10 changes: 10 additions & 0 deletions src/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef VERSION_H
#define VERSION_H

#ifdef SORPVERSION
const QString SORP_VERSION = SORPVERSION;
#else
const QString SORP_VERSION = "Custom build";
#endif

#endif // VERSION_H

0 comments on commit 19e7a01

Please sign in to comment.