Isis 3 Programmer Reference
Preference.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7
8#include <fstream>
9#include <iostream>
10#include <iomanip>
11
12#include <QDir>
13#include <QThreadPool>
14
15#include "Preference.h"
16#include "FileName.h"
17#include "IException.h"
18
19using namespace std;
20namespace Isis {
25 atexit(Shutdown);
26 }
27
28 void Preference::Load(const QString &file) {
29 // Read the input PVL preference file
30 Isis::Pvl pvl;
31
32 if(Isis::FileName(file).fileExists()) {
33 pvl.read(file);
34 }
35 else {
36 // Throw an exception if the preference file isn't found
37 throw IException(IException::User,
38 QString("The preference file %1 was not found or does not exist").arg(file),
39 _FILEINFO_);
40 }
41
42
43 // Override parameters each time load is called
44 for(int i = 0; i < pvl.groups(); i++) {
45 Isis::PvlGroup &inGroup = pvl.group(i);
46 if(this->hasGroup(inGroup.name())) {
47 Isis::PvlGroup &outGroup = this->findGroup(inGroup.name());
48 for(int k = 0; k < inGroup.keywords(); k++) {
49 Isis::PvlKeyword &inKey = inGroup[k];
50 while(outGroup.hasKeyword(inKey.name())) {
51 outGroup.deleteKeyword(inKey.name());
52 }
53 outGroup += inKey;
54 }
55 }
56 else {
57 this->addGroup(inGroup);
58 }
59 }
60
61 // Configure Isis to use the user performance preferences where
62 // appropriate.
63 if (hasGroup("Performance")) {
64 PvlGroup &performance = findGroup("Performance");
65 if (performance.hasKeyword("GlobalThreads")) {
66 IString threadsPreference = performance["GlobalThreads"][0];
67
68 if (threadsPreference.DownCase() != "optimized") {
69 // We need a no-iException conversion here
70 int threads = threadsPreference.ToQt().toInt();
71
72 if (threads > 0) {
73 QThreadPool::globalInstance()->setMaxThreadCount(threads);
74 }
75 }
76 }
77 }
78 }
79
80
81 // Instantiation and initialization of static member variables
82 Preference *Preference::p_preference = NULL;
83 bool Preference::p_unitTest = false;
84
85
86 Preference &Preference::Preferences(bool unitTest) {
87 if(p_preference == NULL) {
88 p_unitTest = unitTest;
89 // Create the singleton
91
92 // Make sure the user has a .Isis directory
93 Isis::FileName setup("$HOME/.Isis");
94 if(!setup.fileExists()) {
95 QDir dir;
96 QString dirName(IString(setup.expanded()).ToQt());
97 dir.mkdir(dirName);
98 }
99
100 // If its a unitTest then load with the unitTest preference file
101 if(unitTest) {
102 p_preference->Load("$ISISROOT/TestPreferences");
103 }
104 // Otherwise load the Isis system and personal preferences.
105 else {
106 p_preference->Load("$ISISROOT/IsisPreferences");
107
108 Isis::FileName userPref("$HOME/.Isis/IsisPreferences");
109 if(userPref.fileExists()) {
110 p_preference->Load("$HOME/.Isis/IsisPreferences");
111 }
112 }
113 }
114 // Note: Keep this here
115 // During unitTests some other class may call Preferences before the
116 // unitTest does. This would cause the preferences to be loaded with
117 // the system and user preferences instead of the TestPreferences.
118 else if(unitTest) {
119 p_unitTest = unitTest;
120 p_preference->clear();
121 p_preference->Load("$ISISROOT/TestPreferences");
122 }
123
124 return *p_preference;
125 }
126
127 void Preference::Shutdown() {
128 if(p_preference) {
129 delete p_preference;
130 p_preference = NULL;
131 }
132 }
133} // end namespace isis
File name manipulation and expansion.
Definition FileName.h:100
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
Preference()
Constructs a Preference object.
static Preference * p_preference
Pointer to a Preference object.
Definition Preference.h:85
static bool p_unitTest
Flag indicating whether the file is a unitTest or not.
Definition Preference.h:86
QString name() const
Returns the container name.
Contains multiple PvlContainers.
Definition PvlGroup.h:41
Container for cube-like labels.
Definition Pvl.h:119
void read(const QString &file)
Loads PVL information from a stream.
Definition Pvl.cpp:90
A single keyword-value pair.
Definition PvlKeyword.h:87
bool hasGroup(const QString &name) const
Returns a boolean value based on whether the object has the specified group or not.
Definition PvlObject.h:212
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition PvlObject.h:186
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition PvlObject.h:129
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.