Loading [MathJax]/jax/output/NativeMML/config.js
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 
19 using namespace std;
20 namespace Isis {
24  Preference::Preference() : Isis::Pvl() {
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
90  p_preference = new Preference();
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
Isis::PvlObject::clear
void clear()
Remove everything from the current PvlObject.
Definition: PvlObject.h:341
Isis::PvlKeyword::name
QString name() const
Returns the keyword name.
Definition: PvlKeyword.h:98
Isis::PvlObject::findGroup
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:129
Isis::PvlObject::group
PvlGroup & group(const int index)
Return the group at the specified index.
Definition: PvlObject.cpp:452
Isis::PvlKeyword
A single keyword-value pair.
Definition: PvlKeyword.h:82
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::PvlObject::groups
int groups() const
Returns the number of groups contained.
Definition: PvlObject.h:75
Isis::Preference::p_preference
static Preference * p_preference
Pointer to a Preference object.
Definition: Preference.h:85
Isis::PvlObject::hasGroup
bool hasGroup(const QString &name) const
Returns a boolean value based on whether the object has the specified group or not.
Definition: PvlObject.h:210
Isis::PvlContainer::hasKeyword
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
Definition: PvlContainer.cpp:159
Isis::Pvl
Container for cube-like labels.
Definition: Pvl.h:119
Isis::Preference::Preference
Preference()
Constructs a Preference object.
Definition: Preference.cpp:24
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::Preference::p_unitTest
static bool p_unitTest
Flag indicating whether the file is a unitTest or not.
Definition: Preference.h:86
Isis::Pvl::read
void read(const QString &file)
Loads PVL information from a stream.
Definition: Pvl.cpp:90
Isis::PvlContainer::name
QString name() const
Returns the container name.
Definition: PvlContainer.h:63
Isis::PvlObject::addGroup
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:186
Isis::PvlContainer::deleteKeyword
void deleteKeyword(const QString &name)
Remove a specified keyword.
Definition: PvlContainer.cpp:97
std
Namespace for the standard library.
Isis::PvlContainer::keywords
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:86
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::IException::User
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition: IException.h:126

U.S. Department of the Interior | U.S. Geological Survey
ISIS | Privacy & Disclaimers | Astrogeology Research Program
To contact us, please post comments and questions on the USGS Astrogeology Discussion Board
To report a bug, or suggest a feature go to: ISIS Github
File Modified: 07/13/2023 15:17:02