Add several progress factories to handle progress bars. The GUI version will come from code within aegisub.

Originally committed to SVN as r5313.
This commit is contained in:
Amar Takhar 2011-02-07 00:12:57 +00:00
parent 73913fb0b8
commit e27c3e15b8
3 changed files with 165 additions and 0 deletions

View file

@ -27,6 +27,7 @@ SRC = \
common/mru.cpp \
common/option.cpp \
common/option_visit.cpp \
common/progress.cpp \
common/keyframe.cpp \
common/log.cpp \
common/validator.cpp \

View file

@ -0,0 +1,104 @@
// 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
#include <string>
#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:
FILE *file;
std::string operation;
float progress;
void print();
public:
StdioProgressSink(FILE *file, const std::string &title);
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 {
return new StdioProgressSink(file, title);
}
StdioProgressSink::StdioProgressSink(FILE *file, const std::string &title)
: file(file)
, operation(title)
, progress(0) {
fprintf(file, "=== %s ===\n", title.c_str());
print();
}
StdioProgressSink::~StdioProgressSink()
{
fprintf(file, "\n -===-\n");
}
void StdioProgressSink::set_progress(int steps, int max) {
assert(steps >= 0);
assert(steps <= max);
float old_progress = progress;
progress = (100.f * steps)/max;
if (fabs(progress-old_progress) > 0.8)
{
print();
}
}
void StdioProgressSink::set_operation(const std::string &operation) {
this->operation = operation;
print();
}
void StdioProgressSink::print() {
fprintf(file, "%s: %.1f%%\r", operation.c_str(), progress);
}
} // namespace agi

View file

@ -0,0 +1,60 @@
// 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.h
/// @brief Progress bars.
/// @ingroup libaegisub
#ifndef LAG_PRE
#include <string>
#endif
namespace agi {
class ProgressSink;
class ProgressSinkFactory {
public:
virtual ~ProgressSinkFactory() { }
virtual ProgressSink * create_progress_sink(const std::string &title) const = 0;
};
class ProgressSink {
public:
virtual ~ProgressSink() { }
virtual void set_progress(int steps, int max) = 0;
virtual void set_operation(const std::string &operation) = 0;
virtual bool get_cancelled() const = 0;
};
class NullProgressSinkFactory : public ProgressSinkFactory {
public:
virtual ProgressSink * create_progress_sink(const std::string &title) const;
};
class StdioProgressSinkFactory : public ProgressSinkFactory {
private:
FILE *file;
public:
StdioProgressSinkFactory(FILE *file) : file(file) { }
virtual ProgressSink * create_progress_sink(const std::string &title) const;
};
} // namespace agi