Isis 3 Programmer Reference
Progress.cpp
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <sys/socket.h>
4 #include <sys/un.h>
10 /* SPDX-License-Identifier: CC0-1.0 */
11 #include "Progress.h"
12 #include "Application.h"
13 #include "Preference.h"
14 
15 using namespace std;
16 namespace Isis {
22  Progress::Progress() {
23  // Get user preferences
24  int percent;
25  bool printPercent;
26  Isis::PvlGroup &group = Isis::Preference::Preferences().findGroup("UserInterface");
27  percent = group["ProgressBarPercent"];
28  QString temp = (QString) group["ProgressBar"];
29  printPercent = temp.toUpper() == "ON";
30 
31  // Check for an error
32  if((percent != 1) && (percent != 2) &&
33  (percent != 5) && (percent != 10)) {
34  string m = "Invalid preference for [ProgressBarPercent] in ";
35  m += "group [UserInterface] must be 1, 2, 5, or 10";
36  throw IException(IException::User, m, _FILEINFO_);
37  }
38 
39  // Initialize private variables
40  p_text = "Working";
41  p_maximumSteps = 0;
42  p_currentStep = 0;
43  p_currentPercent = 0;
44  p_percentIncrement = percent;
45  p_printPercent = printPercent;
46  p_autoDisplay = true;
47  }
48 
50  Progress::~Progress() {
51  }
52 
61  void Progress::SetText(const QString &text) {
62  p_text = text;
63  }
64 
72  QString Progress::Text() const {
73  return p_text;
74  }
75 
85  void Progress::SetMaximumSteps(const int steps) {
86  if(steps < 0) {
87  string m = "Value for [steps] must be greater than ";
88  m += "or equal to zero in [Progress::SetMaximumSteps]";
89  throw IException(IException::Programmer, m, _FILEINFO_);
90  }
91  p_maximumSteps = steps;
92  p_currentStep = 0;
93  p_currentPercent = 0;
94  }
95 
105  void Progress::CheckStatus() {
106  // Error check
107  //std::cout << p_currentStep << " / " << p_maximumSteps << std::endl;
108  if(p_currentStep > p_maximumSteps) {
109  string m = "Step exceeds maximumSteps in [Progress::CheckStatus]";
110  throw IException(IException::Programmer, m, _FILEINFO_);
111  }
112 
113  if(p_currentStep == 0) {
114  if(Isis::iApp != NULL) {
115  if (p_autoDisplay) {
116  Isis::iApp->UpdateProgress(p_text, p_printPercent);
117  }
118  }
119  else {
120  if(p_printPercent && p_autoDisplay) {
121  cout << p_text << endl;
122  }
123  }
124  }
125 
126  // See if the percent processed needs to be updated
127  while(100.0 * p_currentStep / p_maximumSteps >= p_currentPercent) {
128  if(Isis::iApp != NULL) {
129  if (p_autoDisplay) {
130  Isis::iApp->UpdateProgress(p_currentPercent, p_printPercent);
131  }
132  }
133  else {
134  if(p_printPercent && p_autoDisplay) {
135  if(p_currentPercent < 100) {
136  cout << p_currentPercent << "% Processed\r" << flush;
137  }
138  else {
139  cout << p_currentPercent << "% Processed" << endl;
140  }
141  }
142  }
143  p_currentPercent += p_percentIncrement;
144  }
145 
146  if(p_autoDisplay && Isis::iApp != NULL) {
147  Isis::iApp->ProcessGuiEvents();
148  }
149 
150  // Increment to the next step
151  p_currentStep++;
152 
153  return;
154  }
155 
156 
161  void Progress::DisableAutomaticDisplay() {
162  p_autoDisplay = false;
163  }
164 
165 
172  int Progress::MaximumSteps() const {
173  return p_maximumSteps;
174  }
175 
176 
185  int Progress::CurrentStep() const {
186  return p_currentStep;
187  }
188 
189 
199  void Progress::AddSteps(const int steps) {
200  if(steps == 0) {
201  string m = "Value for [steps] must not be zero in [Progress::AddSteps]";
202  throw IException(IException::Programmer, m, _FILEINFO_);
203  }
204 
205  p_maximumSteps += steps;
206 
207  if(p_currentStep > p_maximumSteps) {
208  string m = "Step exceeds maximumSteps in [Progress::AddSteps]";
209  throw IException(IException::Programmer, m, _FILEINFO_);
210  }
211 
212  if(p_maximumSteps <= 0) {
213  string m = "Maximum steps must be greater than zero in [Progress::AddSteps]";
214  throw IException(IException::Programmer, m, _FILEINFO_);
215  }
216  }
217 } // end namespace isis
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::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::Application::ProcessGuiEvents
void ProcessGuiEvents()
Processes the gui events.
Definition: Application.cpp:786
Isis::Application::UpdateProgress
void UpdateProgress(const QString &text, bool print)
Updates the progress bar in the gui.
Definition: Application.cpp:740
std
Namespace for the standard library.
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16