From e27c3e15b8fa19e35015120f5b9ddbd5466eaec8 Mon Sep 17 00:00:00 2001 From: Amar Takhar Date: Mon, 7 Feb 2011 00:12:57 +0000 Subject: [PATCH] Add several progress factories to handle progress bars. The GUI version will come from code within aegisub. Originally committed to SVN as r5313. --- aegisub/libaegisub/Makefile | 1 + aegisub/libaegisub/common/progress.cpp | 104 ++++++++++++++++++ .../libaegisub/include/libaegisub/progress.h | 60 ++++++++++ 3 files changed, 165 insertions(+) create mode 100644 aegisub/libaegisub/common/progress.cpp create mode 100644 aegisub/libaegisub/include/libaegisub/progress.h diff --git a/aegisub/libaegisub/Makefile b/aegisub/libaegisub/Makefile index 09d07c4f2..a2619c7df 100644 --- a/aegisub/libaegisub/Makefile +++ b/aegisub/libaegisub/Makefile @@ -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 \ diff --git a/aegisub/libaegisub/common/progress.cpp b/aegisub/libaegisub/common/progress.cpp new file mode 100644 index 000000000..e5f4bde61 --- /dev/null +++ b/aegisub/libaegisub/common/progress.cpp @@ -0,0 +1,104 @@ +// Copyright (c) 2011, Niels Martin Hansen +// +// 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 +#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 diff --git a/aegisub/libaegisub/include/libaegisub/progress.h b/aegisub/libaegisub/include/libaegisub/progress.h new file mode 100644 index 000000000..fd5227810 --- /dev/null +++ b/aegisub/libaegisub/include/libaegisub/progress.h @@ -0,0 +1,60 @@ +// Copyright (c) 2011, Niels Martin Hansen +// +// 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 +#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