2011-02-07 01:12:57 +01:00
|
|
|
// Copyright (c) 2011, Niels Martin Hansen <nielsm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file progress.cpp
|
|
|
|
/// @brief Progress bars.
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
|
|
|
#ifndef LAG_PRE
|
2011-02-07 01:46:49 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2011-02-07 01:12:57 +01:00
|
|
|
#include <string>
|
2011-02-07 01:46:49 +01:00
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cmath>
|
2011-02-07 01:12:57 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "libaegisub/progress.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
|
|
|
|
|
|
|
|
class NullProgressSink : public ProgressSink {
|
|
|
|
public:
|
|
|
|
virtual void set_progress(int steps, int max) { }
|
|
|
|
virtual void set_operation(const std::string &operation) { }
|
|
|
|
virtual bool get_cancelled() const { return false; }
|
|
|
|
};
|
|
|
|
|
|
|
|
ProgressSink * NullProgressSinkFactory::create_progress_sink(const std::string &title) const {
|
|
|
|
return new NullProgressSink;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class StdioProgressSink : public ProgressSink {
|
|
|
|
private:
|
|
|
|
std::string operation;
|
|
|
|
float progress;
|
|
|
|
|
|
|
|
void print();
|
|
|
|
|
|
|
|
public:
|
2011-02-07 01:46:49 +01:00
|
|
|
StdioProgressSink(const std::string &title);
|
2011-02-07 01:12:57 +01:00
|
|
|
virtual ~StdioProgressSink();
|
|
|
|
virtual void set_progress(int steps, int max);
|
|
|
|
virtual void set_operation(const std::string &operation);
|
|
|
|
virtual bool get_cancelled() const { return false; } // or maybe see if there is an ESC waiting or get a ^C sent flag
|
|
|
|
};
|
|
|
|
|
|
|
|
ProgressSink * StdioProgressSinkFactory::create_progress_sink(const std::string &title) const {
|
2011-02-07 01:46:49 +01:00
|
|
|
return new StdioProgressSink(title);
|
2011-02-07 01:12:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-07 01:46:49 +01:00
|
|
|
StdioProgressSink::StdioProgressSink(const std::string &title)
|
|
|
|
: operation(title)
|
2011-02-07 01:12:57 +01:00
|
|
|
, progress(0) {
|
2011-02-07 01:46:49 +01:00
|
|
|
std::cout << title << ": ";
|
2011-02-07 01:12:57 +01:00
|
|
|
print();
|
|
|
|
}
|
|
|
|
|
|
|
|
StdioProgressSink::~StdioProgressSink()
|
|
|
|
{
|
2011-02-07 01:46:49 +01:00
|
|
|
std::cout << "end" << std::endl;
|
2011-02-07 01:12:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StdioProgressSink::set_progress(int steps, int max) {
|
|
|
|
assert(steps >= 0);
|
|
|
|
assert(steps <= max);
|
|
|
|
|
|
|
|
float old_progress = progress;
|
|
|
|
progress = (100.f * steps)/max;
|
|
|
|
|
2011-02-07 01:46:49 +01:00
|
|
|
if (std::fabs(progress-old_progress) > 0.8)
|
2011-02-07 01:12:57 +01:00
|
|
|
{
|
|
|
|
print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StdioProgressSink::set_operation(const std::string &operation) {
|
|
|
|
this->operation = operation;
|
|
|
|
print();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StdioProgressSink::print() {
|
2011-02-07 01:46:49 +01:00
|
|
|
std::cout << operation << ": " << std::setprecision(2) << progress << std::endl;
|
2011-02-07 01:12:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace agi
|