diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-11-17 19:44:09 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-11-17 19:44:09 (GMT) |
| commit | 058a2edd75debbd0297f92572316daa704bd379f (patch) | |
| tree | ad303f091f9a08b209b91eb34a9fcad996a3de69 /src/tasks | |
| parent | e3594aba9e05c6865d396418c028049cda92c2f3 (diff) | |
| parent | 7a21ae192fe19868539956f3fe28e62b2c7c4429 (diff) | |
| download | powder-058a2edd75debbd0297f92572316daa704bd379f.zip powder-058a2edd75debbd0297f92572316daa704bd379f.tar.gz | |
Merge branch 'master' of github.com:FacialTurd/PowderToypp
Diffstat (limited to 'src/tasks')
| -rw-r--r-- | src/tasks/Task.cpp | 185 | ||||
| -rw-r--r-- | src/tasks/Task.h | 63 | ||||
| -rw-r--r-- | src/tasks/TaskListener.h | 21 | ||||
| -rw-r--r-- | src/tasks/TaskWindow.cpp | 133 | ||||
| -rw-r--r-- | src/tasks/TaskWindow.h | 38 |
5 files changed, 440 insertions, 0 deletions
diff --git a/src/tasks/Task.cpp b/src/tasks/Task.cpp new file mode 100644 index 0000000..d29fe04 --- /dev/null +++ b/src/tasks/Task.cpp @@ -0,0 +1,185 @@ +/* + * Task.cpp + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#include "Task.h" +#include "TaskListener.h" + +void Task::AddTaskListener(TaskListener * listener) +{ + this->listener = listener; + notifyProgressMain(); + notifyStatusMain(); +} + +void Task::Start() +{ + thDone = false; + done = false; + progress = 0; + status = ""; + //taskMutex = PTHREAD_MUTEX_INITIALIZER; + before(); + pthread_mutex_init (&taskMutex, NULL); + pthread_create(&doWorkThread, 0, &Task::doWork_helper, this); +} + +int Task::GetProgress() +{ + return progress; +} + +std::string Task::GetStatus() +{ + return status; +} + +std::string Task::GetError() +{ + return error; +} + +bool Task::GetDone() +{ + return done; +} + +bool Task::GetSuccess() +{ + return success; +} + +void Task::Poll() +{ + if(!done) + { + int newProgress; + bool newDone = false; + bool newSuccess = false; + std::string newStatus; + std::string newError; + pthread_mutex_lock(&taskMutex); + newProgress = thProgress; + newDone = thDone; + newSuccess = thSuccess; + newStatus = std::string(thStatus); + newError = std::string(thError); + pthread_mutex_unlock(&taskMutex); + + success = newSuccess; + + if(newProgress!=progress) { + progress = newProgress; + notifyProgressMain(); + } + + if(newError!=error) { + error = std::string(newError); + notifyErrorMain(); + } + + if(newStatus!=status) { + status = std::string(newStatus); + notifyStatusMain(); + } + + if(newDone!=done) + { + done = newDone; + + pthread_join(doWorkThread, NULL); + pthread_mutex_destroy(&taskMutex); + + after(); + + notifyDoneMain(); + } + } +} + +Task::~Task() +{ + if(!done) + { + pthread_join(doWorkThread, NULL); + pthread_mutex_destroy(&taskMutex); + } +} + +void Task::before() +{ + +} + +bool Task::doWork() +{ + notifyStatus("Fake progress"); + for(int i = 0; i < 100; i++) + { + notifyProgress(i); + } + return true; +} + +void Task::after() +{ + +} + +void * Task::doWork_helper(void * ref) +{ + bool newSuccess = ((Task*)ref)->doWork(); + pthread_mutex_lock(&((Task*)ref)->taskMutex); + ((Task*)ref)->thSuccess = newSuccess; + ((Task*)ref)->thDone = true; + pthread_mutex_unlock(&((Task*)ref)->taskMutex); + return NULL; +} + +void Task::notifyProgress(int progress) +{ + pthread_mutex_lock(&taskMutex); + thProgress = progress; + pthread_mutex_unlock(&taskMutex); +} + +void Task::notifyStatus(std::string status) +{ + pthread_mutex_lock(&taskMutex); + thStatus = std::string(status); + pthread_mutex_unlock(&taskMutex); +} + +void Task::notifyError(std::string error) +{ + pthread_mutex_lock(&taskMutex); + thError = std::string(error); + pthread_mutex_unlock(&taskMutex); +} + +void Task::notifyProgressMain() +{ + if(listener) + listener->NotifyProgress(this); +} + +void Task::notifyStatusMain() +{ + if(listener) + listener->NotifyStatus(this); +} + +void Task::notifyDoneMain() +{ + if(listener) + listener->NotifyDone(this); +} + +void Task::notifyErrorMain() +{ + if(listener) + listener->NotifyError(this); +} diff --git a/src/tasks/Task.h b/src/tasks/Task.h new file mode 100644 index 0000000..a025ac1 --- /dev/null +++ b/src/tasks/Task.h @@ -0,0 +1,63 @@ +/* + * Task.h + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#ifndef TASK_H_ +#define TASK_H_ + +#include <string> +#include <pthread.h> +#undef GetUserName //God dammit microsoft! +#include "TaskListener.h" + +class TaskListener; +class Task { +public: + void AddTaskListener(TaskListener * listener); + void Start(); + int GetProgress(); + bool GetDone(); + bool GetSuccess(); + std::string GetError(); + std::string GetStatus(); + void Poll(); + Task() : listener(NULL) { progress = 0; } + virtual ~Task(); +protected: + int progress; + bool done; + bool success; + std::string status; + std::string error; + + int thProgress; + bool thDone; + bool thSuccess; + std::string thStatus; + std::string thError; + + TaskListener * listener; + pthread_t doWorkThread; + pthread_mutex_t taskMutex; + pthread_cond_t taskCond; + + + virtual void before(); + virtual void after(); + virtual bool doWork(); + static void * doWork_helper(void * ref); + + virtual void notifyProgress(int progress); + virtual void notifyError(std::string error); + virtual void notifyStatus(std::string status); + + virtual void notifyProgressMain(); + virtual void notifyErrorMain(); + virtual void notifyStatusMain(); + virtual void notifyDoneMain(); +}; + +#endif /* TASK_H_ */ diff --git a/src/tasks/TaskListener.h b/src/tasks/TaskListener.h new file mode 100644 index 0000000..7405c03 --- /dev/null +++ b/src/tasks/TaskListener.h @@ -0,0 +1,21 @@ +/* + * TaskListener.h + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#ifndef TASKLISTENER_H_ +#define TASKLISTENER_H_ + +class Task; +class TaskListener { +public: + virtual void NotifyDone(Task * task) {} + virtual void NotifyError(Task * task) {} + virtual void NotifyProgress(Task * task) {} + virtual void NotifyStatus(Task * task) {} + virtual ~TaskListener() {} +}; + +#endif /* TASK_H_ */ diff --git a/src/tasks/TaskWindow.cpp b/src/tasks/TaskWindow.cpp new file mode 100644 index 0000000..b3055d1 --- /dev/null +++ b/src/tasks/TaskWindow.cpp @@ -0,0 +1,133 @@ +/* + * TaskWindow.cpp + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#include <sstream> +#include "interface/Label.h" +#include "TaskWindow.h" +#include "dialogues/ErrorMessage.h" +#include "Style.h" +#include "Task.h" + +TaskWindow::TaskWindow(std::string title_, Task * task_, bool closeOnDone): + task(task_), + title(title_), + ui::Window(ui::Point(-1, -1), ui::Point(240, 60)), + progress(0), + done(false), + closeOnDone(closeOnDone), + progressStatus("0%") +{ + + ui::Label * tempLabel = new ui::Label(ui::Point(4, 5), ui::Point(Size.X-8, 15), title); + tempLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + tempLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + tempLabel->SetTextColour(style::Colour::WarningTitle); + AddComponent(tempLabel); + + statusLabel = new ui::Label(ui::Point(4, 23), ui::Point(Size.X-8, 15), ""); + statusLabel->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + statusLabel->Appearance.VerticalAlign = ui::Appearance::AlignMiddle; + AddComponent(statusLabel); + + ui::Engine::Ref().ShowWindow(this); + + task->AddTaskListener(this); + task->Start(); +} + +void TaskWindow::NotifyStatus(Task * task) +{ + statusLabel->SetText(task->GetStatus()); +} + +void TaskWindow::NotifyError(Task * task) +{ + new ErrorMessage("Error", task->GetError()); +} + +void TaskWindow::NotifyDone(Task * task) +{ + if(closeOnDone) + Exit(); +} + +void TaskWindow::Exit() +{ + if(ui::Engine::Ref().GetWindow()==this) + { + ui::Engine::Ref().CloseWindow(); + SelfDestruct(); + } +} + +void TaskWindow::NotifyProgress(Task * task) +{ + progress = task->GetProgress(); + std::stringstream pStream; + if(progress>-1) + { + pStream << progress << "%"; + } + else + { + pStream << "Please wait..."; + } + progressStatus = pStream.str(); +} + +void TaskWindow::OnTick(float dt) +{ + intermediatePos += 1.0f*dt; + if(intermediatePos>100.0f) + intermediatePos = 0.0f; + task->Poll(); +} + +void TaskWindow::OnDraw() +{ + Graphics * g = ui::Engine::Ref().g; + g->clearrect(Position.X-2, Position.Y-2, Size.X+3, Size.Y+3); + g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255); + + g->draw_line(Position.X, Position.Y + Size.Y-17, Position.X + Size.X - 1, Position.Y + Size.Y-17, 255, 255, 255, 255); + + ui::Colour progressBarColour = style::Colour::WarningTitle; + + if(progress!=-1) + { + if(progress > 0) + { + if(progress > 100) + progress = 100; + float size = float(Size.X-4)*(float(progress)/100.0f); // TIL... + size = std::min(std::max(size, 0.0f), float(Size.X-4)); + g->fillrect(Position.X + 2, Position.Y + Size.Y-15, size, 13, progressBarColour.Red, progressBarColour.Green, progressBarColour.Blue, 255); + } + } else { + int size = 40, rsize = 0; + float position = float(Size.X-4)*(intermediatePos/100.0f); + if(position + size - 1 > Size.X-4) + { + size = (Size.X-4)-position+1; + rsize = 40-size; + } + g->fillrect(Position.X + 2 + position, Position.Y + Size.Y-15, size, 13, progressBarColour.Red, progressBarColour.Green, progressBarColour.Blue, 255); + if(rsize) + { + g->fillrect(Position.X + 2, Position.Y + Size.Y-15, rsize, 13, progressBarColour.Red, progressBarColour.Green, progressBarColour.Blue, 255); + } + } + if(progress<50) + g->drawtext(Position.X + ((Size.X-Graphics::textwidth(progressStatus.c_str()))/2), Position.Y + Size.Y-13, progressStatus, 255, 255, 255, 255); + else + g->drawtext(Position.X + ((Size.X-Graphics::textwidth(progressStatus.c_str()))/2), Position.Y + Size.Y-13, progressStatus, 0, 0, 0, 255); +} + +TaskWindow::~TaskWindow() { + delete task; +} + diff --git a/src/tasks/TaskWindow.h b/src/tasks/TaskWindow.h new file mode 100644 index 0000000..820265c --- /dev/null +++ b/src/tasks/TaskWindow.h @@ -0,0 +1,38 @@ +/* + * TaskWindow.h + * + * Created on: Apr 6, 2012 + * Author: Simon + */ + +#ifndef TASKWINDOW_H_ +#define TASKWINDOW_H_ + +#include <string> +#include "interface/Label.h" +#include "interface/Window.h" +#include "tasks/TaskListener.h" + +class Task; +class TaskWindow: public ui::Window, public TaskListener { + Task * task; + std::string title; + int progress; + float intermediatePos; + bool done; + bool closeOnDone; + ui::Label * statusLabel; + std::string progressStatus; +public: + TaskWindow(std::string title_, Task * task_, bool closeOnDone = true); + virtual void NotifyStatus(Task * task); + virtual void NotifyDone(Task * task); + virtual void NotifyProgress(Task * task); + virtual void NotifyError(Task * task); + virtual void OnTick(float dt); + virtual void OnDraw(); + virtual void Exit(); + virtual ~TaskWindow(); +}; + +#endif /* TASKWINDOW_H_ */ |
