Isis 3 Programmer Reference
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/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/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
int keywords() const
Returns the number of keywords contained in the PvlContainer.
Definition: PvlContainer.h:100
bool hasKeyword(const QString &name) const
Check to see if a keyword exists.
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
void clear()
Remove everything from the current PvlObject.
Definition: PvlObject.h:353
File name manipulation and expansion.
Definition: FileName.h:116
static Preference * p_preference
Pointer to a Preference object.
Definition: Preference.h:102
void addGroup(const Isis::PvlGroup &group)
Add a group to the object.
Definition: PvlObject.h:198
Namespace for the standard library.
bool hasGroup(const QString &name) const
Returns a boolean value based on whether the object has the specified group or not.
Definition: PvlObject.h:222
QString name() const
Returns the container name.
Definition: PvlContainer.h:77
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
A type of error that could only have occurred due to a mistake on the user&#39;s part (e...
Definition: IException.h:142
A single keyword-value pair.
Definition: PvlKeyword.h:98
Container for cube-like labels.
Definition: Pvl.h:135
PvlGroup & group(const int index)
Return the group at the specified index.
Definition: PvlObject.cpp:423
QString name() const
Returns the keyword name.
Definition: PvlKeyword.h:114
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
int groups() const
Returns the number of groups contained.
Definition: PvlObject.h:87
Preference()
Constructs a Preference object.
Definition: Preference.cpp:40
void read(const QString &file)
Loads PVL information from a stream.
Definition: Pvl.cpp:76
static bool p_unitTest
Flag indicating whether the file is a unitTest or not.
Definition: Preference.h:103
void deleteKeyword(const QString &name)
Remove a specified keyword.