ProgressDisplayOld.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00024 #include "ProgressDisplayOld.h"
00025 
00026 #ifdef WIN32
00027 #define snprintf _snprintf
00028 #endif
00029 
00030 namespace AppBase {
00031     
00032     
00033 ProgressTask::ProgressTask(std::string shortMessage, std::string message,
00034                            double subStepProgress, double progress)
00035   : shortMessage(shortMessage), message(message),
00036     subStepProgress(subStepProgress), progress(progress),
00037     measureProgress(true), last_displayed_progress(-1)
00038 {
00039 
00040 }
00041 
00042 ProgressTask::ProgressTask(std::string shortMessage, std::string message)
00043   : shortMessage(shortMessage), message(message),
00044     subStepProgress(0), progress(0),
00045     measureProgress(false), last_displayed_progress(-1)
00046 {
00047     
00048 }
00049 
00050 
00051 
00052 
00053 MultiProgressDisplay::MultiProgressDisplay(double minPrintStep)
00054 : m_minProgressStep(minPrintStep) 
00055 {
00056 
00057 }
00058 
00059 void MultiProgressDisplay::pushTask(const ProgressTask & task)
00060 {
00061     tasks.push_back(task);
00062     taskAdded();
00063     updateProgressDisplay();
00064 }
00065 
00066 
00068 void MultiProgressDisplay::popTask()
00069 {
00070     taskRemove();
00071     if (!tasks.back().measureProgress && tasks.size()>1) {
00072         tasks[tasks.size()-2].progress += tasks[tasks.size()-2].subStepProgress;
00073     }
00074     tasks.pop_back();
00075     updateProgressDisplay();
00076 }
00077 
00078 
00080 void MultiProgressDisplay::setShortMessage(const std::string & msg)
00081 {
00082     tasks.back().shortMessage = msg;
00083     updateProgressDisplay();
00084 }
00085 
00086 
00088 void MultiProgressDisplay::setMessage(const std::string & msg)
00089 {
00090     tasks.back().message = msg;
00091     updateProgressDisplay();
00092 }
00093 
00095 void MultiProgressDisplay::setProgress(double progress)
00096 {
00097     propagateProgress(progress);
00098     double displayStep = tasks.back().progress - tasks.back().last_displayed_progress;
00099     if (displayStep > m_minProgressStep)
00100     {
00101         updateProgressDisplay();
00102         tasks.back().last_displayed_progress = tasks.back().progress;
00103     }
00104 }
00105 
00107 void MultiProgressDisplay::increase()
00108 {
00109     // substep progress.
00110     setProgress(tasks.back().progress + tasks.back().subStepProgress);
00111 }
00112 
00113 
00114 void MultiProgressDisplay::propagateProgress(double progress)
00115 {
00116     std::vector<ProgressTask>::reverse_iterator it = tasks.rbegin();
00117     double diff = progress - it->progress;
00118     it->progress = progress;
00119     it++;
00120     while (it != tasks.rend()) {
00121         // scale previous change
00122         diff *= it->subStepProgress;
00123         // propagate to next level
00124         it->progress += diff;
00125         ++it;
00126     }
00127 }
00128 
00129 
00130 
00131 
00133 MultiProgressDisplayAdaptor::MultiProgressDisplayAdaptor(ProgressDisplay& myProgressDisplay)
00134   : MultiProgressDisplay(0.0), o_progressDisplay(myProgressDisplay)
00135 {};
00136 
00138 MultiProgressDisplay* MultiProgressDisplayAdaptor::newMultiProgressDisplay(ProgressDisplay* myProgressDisplay)
00139 {
00140     if(myProgressDisplay != NULL)
00141         return new MultiProgressDisplayAdaptor(*myProgressDisplay);
00142     else
00143         return new DummyMultiProgressDisplay();
00144 }
00145 
00146 
00148 void MultiProgressDisplayAdaptor::taskAdded()
00149 {
00150     assert(tasks.size() > 0);
00151     if (tasks.size() > 1) {
00152         o_progressDisplay.setParentProgressOfNewSubtasks(( ++(tasks.rbegin()) )->subStepProgress, true);
00153     }
00154     o_progressDisplay.startSubtask(tasks.back().getMessage(), 1.0);
00155 };
00156 
00158 void MultiProgressDisplayAdaptor::taskRemove()
00159 {
00160     o_progressDisplay.finishSubtask();
00161 };
00162 
00164 void MultiProgressDisplayAdaptor::updateProgressDisplay()
00165 {
00166     if(tasks.empty())
00167     {
00168         DEBUG_INFO("There are no tasks.");
00169         return;
00170     }
00171     
00172     if(tasks.back().getMessage().length() == 0)
00173         o_progressDisplay.setSubtaskMessage(tasks.back().getShortMessage());
00174     else if(tasks.back().getShortMessage().length() == 0)
00175         o_progressDisplay.setSubtaskMessage(tasks.back().getMessage());
00176     else
00177         o_progressDisplay.setSubtaskMessage(tasks.back().getShortMessage() + " ("+tasks.back().getMessage()+")");
00178     
00179     o_progressDisplay.updateSubtaskProgress(tasks.back().getProgress());
00180 }
00181 
00182 
00183 
00184 
00185 
00186 StreamMultiProgressDisplay::StreamMultiProgressDisplay(std::ostream& o, double minPrintStep)
00187   : MultiProgressDisplay(minPrintStep),
00188     m_stream(o), m_printedLines(0),
00189     m_whizz("-\\|/"), m_whizzCount(0)
00190 {
00191 
00192 }
00193 
00194 
00195 void StreamMultiProgressDisplay::updateProgressDisplay()
00196 {
00197     int lines = m_printedLines;
00198     // step back the line printed before.
00199     if (lines !=0) {
00200         m_stream << "\033[" << m_printedLines << "A" << "\r";
00201     }
00202     m_printedLines = 0;
00203     // build the message:
00204     for (std::vector<ProgressTask>::iterator it = tasks.begin();
00205          it != tasks.end(); ++it)
00206     {
00207         m_printedLines++;
00208         char tmp[81];
00209         tmp[80]=0;
00210         if (it->measureProgress) {
00211             snprintf(tmp,80,"%20s: %-50s : %3.0f %%",
00212                      it->getShortMessage().c_str(),
00213                      it->getMessage().c_str(),
00214                      100 * it->getProgress());
00215         } else if (! it->measureProgress && it+1 == tasks.end()) {
00216             m_whizzCount = (++m_whizzCount) % (int)m_whizz.size();
00217             snprintf(tmp,80,"%20s: %-50s :   %c  ",
00218                      it->getShortMessage().c_str(),
00219                      it->getMessage().c_str(),
00220                      m_whizz[m_whizzCount]);
00221         } else {
00222             snprintf(tmp,80,"%20s: %-50s :   -  ",
00223                      it->getShortMessage().c_str(),
00224                      it->getMessage().c_str());
00225         }
00226         
00227         m_stream << tmp << std::endl;
00228     }
00229     // print empty lines..
00230     while (m_printedLines < lines) {
00231         m_stream << "                                                                               " << std::endl;
00232         m_printedLines++;
00233     }
00234 }
00235 
00236 
00237 
00238 } //namespace
00239 

Generated on 5 Dec 2014 for Hugintrunk by  doxygen 1.4.7