USGS

Isis 3.0 Object Programmers' Reference

Home

Preference.cpp
Go to the documentation of this file.
1 
24 #include <fstream>
25 #include <iostream>
26 #include <iomanip>
27 
28 #include <QDir>
29 #include <QThreadPool>
30 
31 #include "Preference.h"
32 #include "FileName.h"
33 #include "IException.h"
34 
35 using namespace std;
36 namespace Isis {
40  Preference::Preference() : Isis::Pvl() {
41  atexit(Shutdown);
42  }
43 
44  void Preference::Load(const QString &file) {
45  // Read the input PVL preference file
46  Isis::Pvl pvl;
47 
48  if(Isis::FileName(file).fileExists()) {
49  pvl.read(file);
50  }
51  else {
52  // Throw an exception if the preference file isn't found
53  throw IException(IException::User,
54  QString("The preference file %1 was not found or does not exist").arg(file),
55  _FILEINFO_);
56  }
57 
58 
59  // Override parameters each time load is called
60  for(int i = 0; i < pvl.groups(); i++) {
61  Isis::PvlGroup &inGroup = pvl.group(i);
62  if(this->hasGroup(inGroup.name())) {
63  Isis::PvlGroup &outGroup = this->findGroup(inGroup.name());
64  for(int k = 0; k < inGroup.keywords(); k++) {
65  Isis::PvlKeyword &inKey = inGroup[k];
66  while(outGroup.hasKeyword(inKey.name())) {
67  outGroup.deleteKeyword(inKey.name());
68  }
69  outGroup += inKey;
70  }
71  }
72  else {
73  this->addGroup(inGroup);
74  }
75  }
76 
77  // Configure Isis to use the user performance preferences where
78  // appropriate.
79  if (hasGroup("Performance")) {
80  PvlGroup &performance = findGroup("Performance");
81  if (performance.hasKeyword("GlobalThreads")) {
82  IString threadsPreference = performance["GlobalThreads"][0];
83 
84  if (threadsPreference.DownCase() != "optimized") {
85  // We need a no-iException conversion here
86  int threads = threadsPreference.ToQt().toInt();
87 
88  if (threads > 0) {
89  QThreadPool::globalInstance()->setMaxThreadCount(threads);
90  }
91  }
92  }
93  }
94  }
95 
96 
97  // Instantiation and initialization of static member variables
98  Preference *Preference::p_preference = NULL;
99  bool Preference::p_unitTest = false;
100 
101 
102  Preference &Preference::Preferences(bool unitTest) {
103  if(p_preference == NULL) {
104  p_unitTest = unitTest;
105  // Create the singleton
106  p_preference = new Preference();
107 
108  // Make sure the user has a .Isis directory
109  Isis::FileName setup("$HOME/.Isis");
110  if(!setup.fileExists()) {
111  QDir dir;
112  QString dirName(IString(setup.expanded()).ToQt());
113  dir.mkdir(dirName);
114  }
115 
116  // If its a unitTest then load with the unitTest preference file
117  if(unitTest) {
118  p_preference->Load("$ISISROOT/src/base/objs/Preference/TestPreferences");
119  }
120  // Otherwise load the Isis system and personal preferences.
121  else {
122  p_preference->Load("$ISISROOT/IsisPreferences");
123 
124  Isis::FileName userPref("$HOME/.Isis/IsisPreferences");
125  if(userPref.fileExists()) {
126  p_preference->Load("$HOME/.Isis/IsisPreferences");
127  }
128  }
129  }
130  // Note: Keep this here
131  // During unitTests some other class may call Preferences before the
132  // unitTest does. This would cause the preferences to be loaded with
133  // the system and user preferences instead of the TestPreferences.
134  else if(unitTest) {
135  p_unitTest = unitTest;
136  p_preference->clear();
137  p_preference->Load("$ISISROOT/src/base/objs/Preference/TestPreferences");
138  }
139 
140  return *p_preference;
141  }
142 
143  void Preference::Shutdown() {
144  if(p_preference) {
145  delete p_preference;
146  p_preference = NULL;
147  }
148  }
149 } // end namespace isis