Fix bug when selecting a default profile when a user profile with the same name exists (closes #2074). Make it possible to specifiy a full path to a profile in the openspace.cfg file

This commit is contained in:
Alexander Bock
2022-05-05 15:54:52 -07:00
parent 4ee23d461e
commit 82b0b1fd87
2 changed files with 69 additions and 38 deletions

View File

@@ -395,7 +395,9 @@ void LauncherWindow::populateProfilesList(std::string preset) {
}
_profileBox->addItem(QString::fromStdString("--- User Profiles ---"));
const QStandardItemModel* model = qobject_cast<const QStandardItemModel*>(_profileBox->model());
const QStandardItemModel* model = qobject_cast<const QStandardItemModel*>(
_profileBox->model()
);
model->item(_userAssetCount)->setEnabled(false);
++_userAssetCount;
@@ -410,7 +412,10 @@ void LauncherWindow::populateProfilesList(std::string preset) {
}
std::sort(profiles.begin(), profiles.end());
for (const fs::directory_entry& p : profiles) {
_profileBox->addItem(QString::fromStdString(p.path().stem().string()));
_profileBox->addItem(
QString::fromStdString(p.path().stem().string()),
QString::fromStdString(p.path().string())
);
}
_profileBox->addItem(QString::fromStdString("--- OpenSpace Profiles ---"));
@@ -430,14 +435,30 @@ void LauncherWindow::populateProfilesList(std::string preset) {
// Add sorted items to list
for (const fs::directory_entry& profile : profiles) {
_profileBox->addItem(QString::fromStdString(profile.path().stem().string()));
std::string abc = profile.path().string();
_profileBox->addItem(
QString::fromStdString(profile.path().stem().string()),
QString::fromStdString(profile.path().string())
);
}
// Try to find the requested profile and set it as the current one
const int idx = _profileBox->findText(QString::fromStdString(std::move(preset)));
if (idx != -1) {
_profileBox->setCurrentIndex(idx);
int idx = _profileBox->findText(QString::fromStdString(preset));
if (idx == -1) {
// We didn't find the preset, so the user probably specified a path in the
// configuration file that doesn't match any value in the list
_profileBox->addItem(QString::fromStdString("--- Configuration File ---"));
model = qobject_cast<const QStandardItemModel*>(_profileBox->model());
model->item(_profileBox->count() - 1)->setEnabled(false);
_profileBox->addItem(
QString::fromStdString(preset),
QString::fromStdString(preset)
);
idx = _profileBox->count() - 1;
}
_profileBox->setCurrentIndex(idx);
}
// Returns 'true' if the file was a configuration file, 'false' otherwise
@@ -602,7 +623,8 @@ bool LauncherWindow::wasLaunchSelected() const {
}
std::string LauncherWindow::selectedProfile() const {
return _profileBox->currentText().toStdString();
// The user data stores the full path to the profile
return _profileBox->currentData().toString().toStdString();
}
std::string LauncherWindow::selectedWindowConfig() const {

View File

@@ -303,41 +303,50 @@ void OpenSpaceEngine::initialize() {
// Process profile file (must be provided in configuration file)
if (!global::configuration->profile.empty()) {
std::string inputProfilePath = absPath("${PROFILES}").string();
std::string outputScenePath = absPath("${TEMPORARY}").string();
std::string inputProfile = inputProfilePath + "/" + global::configuration->profile
+ ".profile";
std::string inputUserProfile = absPath("${USER_PROFILES}").string() + "/" +
global::configuration->profile + ".profile";
std::filesystem::path profile;
if (!std::filesystem::is_regular_file(global::configuration->profile)) {
std::filesystem::path userCandidate = absPath(fmt::format(
"${{USER_PROFILES}}/{}.profile", global::configuration->profile
));
std::filesystem::path profileCandidate = absPath(fmt::format(
"${{PROFILES}}/{}.profile", global::configuration->profile
));
if (std::filesystem::is_regular_file(inputUserProfile)) {
inputProfile = inputUserProfile;
}
if (!std::filesystem::is_regular_file(inputProfile)) {
LERROR(fmt::format(
"Could not load profile '{}': File does not exist", inputProfile)
);
// Give the user profile priority if there are both
if (std::filesystem::is_regular_file(userCandidate)) {
profile = userCandidate;
}
else if (std::filesystem::is_regular_file(profileCandidate)) {
profile = profileCandidate;
}
else {
throw ghoul::RuntimeError(fmt::format(
"Could not load profile '{}': File does not exist",
global::configuration->profile
));
}
}
else {
// Load the profile
std::ifstream inFile;
try {
inFile.open(inputProfile, std::ifstream::in);
}
catch (const std::ifstream::failure& e) {
throw ghoul::RuntimeError(fmt::format(
"Exception opening profile file for read: {} ({})",
inputProfile, e.what())
);
}
std::string content(
(std::istreambuf_iterator<char>(inFile)),
std::istreambuf_iterator<char>()
);
*global::profile = Profile(content);
profile = global::configuration->profile;
}
// Load the profile
std::ifstream inFile;
try {
inFile.open(profile, std::ifstream::in);
}
catch (const std::ifstream::failure& e) {
throw ghoul::RuntimeError(fmt::format(
"Exception opening profile file for read: {} ({})",
profile, e.what())
);
}
std::string content(
(std::istreambuf_iterator<char>(inFile)),
std::istreambuf_iterator<char>()
);
*global::profile = Profile(content);
}
// Set up asset loader