diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-06-21 14:49:32 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-06-21 14:49:32 (GMT) |
| commit | 550f6e28e012ceba8df9274fc71c7fbddfd90530 (patch) | |
| tree | f4047461c04db78e0d8c8b0a81125c6030074908 /src/tasks | |
| parent | 8fc3325b1c55a84d29c3b4fafe242af279c09fe5 (diff) | |
| download | powder-550f6e28e012ceba8df9274fc71c7fbddfd90530.zip powder-550f6e28e012ceba8df9274fc71c7fbddfd90530.tar.gz | |
Stage 1 of update process completed
Diffstat (limited to 'src/tasks')
| -rw-r--r-- | src/tasks/Task.cpp | 88 | ||||
| -rw-r--r-- | src/tasks/Task.h | 12 | ||||
| -rw-r--r-- | src/tasks/TaskListener.h | 1 | ||||
| -rw-r--r-- | src/tasks/TaskWindow.cpp | 56 | ||||
| -rw-r--r-- | src/tasks/TaskWindow.h | 2 |
5 files changed, 110 insertions, 49 deletions
diff --git a/src/tasks/Task.cpp b/src/tasks/Task.cpp index dfb8a2b..7be013e 100644 --- a/src/tasks/Task.cpp +++ b/src/tasks/Task.cpp @@ -10,7 +10,7 @@ #include "Task.h" #include "TaskListener.h" -void Task::SetTaskListener(TaskListener * listener) +void Task::AddTaskListener(TaskListener * listener) { this->listener = listener; } @@ -37,6 +37,11 @@ std::string Task::GetStatus() return status; } +std::string Task::GetError() +{ + return error; +} + bool Task::GetDone() { return done; @@ -44,38 +49,50 @@ bool Task::GetDone() void Task::Poll() { - int newProgress; - bool newDone = false; - std::string newStatus; - pthread_mutex_lock(&taskMutex); - newProgress = thProgress; - newDone = thDone; - newStatus = std::string(thStatus); - pthread_mutex_unlock(&taskMutex); - - if(newProgress!=progress) { - progress = newProgress; - if(listener) - listener->NotifyProgress(this); - } - if(newStatus!=status) { - status = std::string(newStatus); - if(listener) - listener->NotifyStatus(this); - } - - if(done) + if(!done) { - pthread_join(doWorkThread, NULL); - pthread_mutex_destroy(&taskMutex); - after(); - } - - if(newDone!=done) - { - done = newDone; - if(listener) - listener->NotifyDone(this); + int newProgress; + bool newDone = false; + std::string newStatus; + std::string newError; + pthread_mutex_lock(&taskMutex); + newProgress = thProgress; + newDone = thDone; + newStatus = std::string(thStatus); + newError = std::string(thError); + pthread_mutex_unlock(&taskMutex); + + if(newProgress!=progress) { + progress = newProgress; + if(listener) + listener->NotifyProgress(this); + } + + if(newError!=error) { + error = std::string(newError); + if(listener) + listener->NotifyError(this); + } + + if(newStatus!=status) { + status = std::string(newStatus); + if(listener) + listener->NotifyStatus(this); + } + + if(done) + { + pthread_join(doWorkThread, NULL); + pthread_mutex_destroy(&taskMutex); + after(); + } + + if(newDone!=done) + { + done = newDone; + if(listener) + listener->NotifyDone(this); + } } } @@ -131,3 +148,10 @@ void Task::notifyDone() thDone = true; pthread_mutex_unlock(&taskMutex); } + +void Task::notifyError(std::string error) +{ + pthread_mutex_lock(&taskMutex); + thError = std::string(error); + pthread_mutex_unlock(&taskMutex); +} diff --git a/src/tasks/Task.h b/src/tasks/Task.h index 10a8b90..ae83e1d 100644 --- a/src/tasks/Task.h +++ b/src/tasks/Task.h @@ -15,10 +15,11 @@ class TaskListener; class Task { public: - void SetTaskListener(TaskListener * listener); + void AddTaskListener(TaskListener * listener); void Start(); int GetProgress(); bool GetDone(); + std::string GetError(); std::string GetStatus(); void Poll(); Task() {} @@ -27,10 +28,12 @@ protected: int progress; bool done; std::string status; + std::string error; int thProgress; bool thDone; std::string thStatus; + std::string thError; TaskListener * listener; pthread_t doWorkThread; @@ -43,9 +46,10 @@ protected: virtual void doWork(); static void * doWork_helper(void * ref); - void notifyProgress(int progress); - void notifyStatus(std::string status); - void notifyDone(); + virtual void notifyProgress(int progress); + virtual void notifyError(std::string error); + virtual void notifyStatus(std::string status); + virtual void notifyDone(); }; #endif /* TASK_H_ */ diff --git a/src/tasks/TaskListener.h b/src/tasks/TaskListener.h index fc5d5e7..7405c03 100644 --- a/src/tasks/TaskListener.h +++ b/src/tasks/TaskListener.h @@ -12,6 +12,7 @@ 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() {} diff --git a/src/tasks/TaskWindow.cpp b/src/tasks/TaskWindow.cpp index c705d36..910e11a 100644 --- a/src/tasks/TaskWindow.cpp +++ b/src/tasks/TaskWindow.cpp @@ -5,28 +5,37 @@ * 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(300, 200)), + ui::Window(ui::Point(-1, -1), ui::Point(240, 60)), progress(0), done(false), - closeOnDone(closeOnDone) + closeOnDone(closeOnDone), + progressStatus("0%") { - ui::Label * tempLabel = new ui::Label(ui::Point(3, 3), ui::Point(Size.X-6, 16), title); + 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(3, 19), ui::Point(Size.X-6, 16), ""); + 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->SetTaskListener(this); + task->AddTaskListener(this); task->Start(); } @@ -35,6 +44,11 @@ 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) @@ -53,6 +67,16 @@ void TaskWindow::Exit() 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) @@ -69,26 +93,32 @@ void TaskWindow::OnDraw() 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->drawrect(Position.X + 20, Position.Y + 36, Size.X-40, 24, 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) { - float size = float(Size.X-40)*(float(progress)/100.0f); // TIL... - g->fillrect(Position.X + 20, Position.Y + 36, size, 24, 255, 255, 255, 255); + float size = float(Size.X-4)*(float(progress)/100.0f); // TIL... + 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-40)*(intermediatePos/100.0f); - if(position + size > Size.X-40) + float position = float(Size.X-4)*(intermediatePos/100.0f); + if(position + size - 1 > Size.X-4) { - size = (Size.X-40)-position; + size = (Size.X-4)-position+1; rsize = 40-size; } - g->fillrect(Position.X + 20 + position, Position.Y + 36, size, 24, 255, 255, 255, 255); + 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 + 20, Position.Y + 36, rsize, 24, 255, 255, 255, 255); + 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() { diff --git a/src/tasks/TaskWindow.h b/src/tasks/TaskWindow.h index 09893bc..820265c 100644 --- a/src/tasks/TaskWindow.h +++ b/src/tasks/TaskWindow.h @@ -22,11 +22,13 @@ class TaskWindow: public ui::Window, public TaskListener { 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(); |
