summaryrefslogtreecommitdiff
path: root/src/tasks
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2012-04-06 23:45:24 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2012-04-06 23:45:24 (GMT)
commitbbfbb81086897d50b67bf1494ac150eb607add72 (patch)
tree80bab3ddb1f62f847f12947bc81fac2c091669b8 /src/tasks
parent8f8de875c6f7a68a3e47252a8653abb72fd398c1 (diff)
downloadpowder-bbfbb81086897d50b67bf1494ac150eb607add72.zip
powder-bbfbb81086897d50b67bf1494ac150eb607add72.tar.gz
Confirmation Dialogue, Save selection and multi-delete
Diffstat (limited to 'src/tasks')
-rw-r--r--src/tasks/Task.cpp84
-rw-r--r--src/tasks/Task.h38
-rw-r--r--src/tasks/TaskListener.h20
-rw-r--r--src/tasks/TaskWindow.cpp66
-rw-r--r--src/tasks/TaskWindow.h32
5 files changed, 240 insertions, 0 deletions
diff --git a/src/tasks/Task.cpp b/src/tasks/Task.cpp
new file mode 100644
index 0000000..c20a0ea
--- /dev/null
+++ b/src/tasks/Task.cpp
@@ -0,0 +1,84 @@
+/*
+ * Task.cpp
+ *
+ * Created on: Apr 6, 2012
+ * Author: Simon
+ */
+
+
+#include <unistd.h>
+#include "Task.h"
+#include "TaskListener.h"
+
+void Task::SetTaskListener(TaskListener * listener)
+{
+ this->listener = listener;
+}
+
+void Task::Start()
+{
+ pthread_create(&doWorkThread, 0, &Task::doWork_helper, this);
+}
+
+int Task::GetProgress()
+{
+ return progress;
+}
+
+std::string Task::GetStatus()
+{
+ return status;
+}
+
+bool Task::GetDone()
+{
+ return done;
+}
+
+Task::~Task()
+{
+
+}
+
+void Task::doWork()
+{
+ notifyStatus("Fake progress");
+ for(int i = 0; i < 100; i++)
+ {
+ notifyProgress(i);
+ usleep((100)*1000);
+ }
+}
+
+void * Task::doWork_helper(void * ref)
+{
+ ((Task*)ref)->doWork();
+ ((Task*)ref)->notifyDone();
+ return NULL;
+}
+
+void Task::notifyProgress(int progress)
+{
+ if(this->progress!=progress) {
+ this->progress = progress;
+ if(listener)
+ listener->NotifyProgress(this);
+ }
+}
+
+void Task::notifyStatus(std::string status)
+{
+ if(this->status!=status) {
+ this->status = status;
+ if(listener)
+ listener->NotifyStatus(this);
+ }
+}
+
+void Task::notifyDone()
+{
+ if(listener)
+ {
+ done = true; listener->NotifyDone(this);
+ }
+}
diff --git a/src/tasks/Task.h b/src/tasks/Task.h
new file mode 100644
index 0000000..90463cb
--- /dev/null
+++ b/src/tasks/Task.h
@@ -0,0 +1,38 @@
+/*
+ * Task.h
+ *
+ * Created on: Apr 6, 2012
+ * Author: Simon
+ */
+
+#ifndef TASK_H_
+#define TASK_H_
+
+#include <string>
+#include <pthread.h>
+#include "TaskListener.h"
+
+class TaskListener;
+class Task {
+public:
+ void SetTaskListener(TaskListener * listener);
+ void Start();
+ int GetProgress();
+ bool GetDone();
+ std::string GetStatus();
+ Task() {}
+ virtual ~Task();
+protected:
+ int progress;
+ bool done;
+ std::string status;
+ TaskListener * listener;
+ pthread_t doWorkThread;
+ virtual void doWork();
+ static void * doWork_helper(void * ref);
+ void notifyProgress(int progress);
+ void notifyStatus(std::string status);
+ void notifyDone();
+};
+
+#endif /* TASK_H_ */
diff --git a/src/tasks/TaskListener.h b/src/tasks/TaskListener.h
new file mode 100644
index 0000000..fc5d5e7
--- /dev/null
+++ b/src/tasks/TaskListener.h
@@ -0,0 +1,20 @@
+/*
+ * 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 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..ac22c79
--- /dev/null
+++ b/src/tasks/TaskWindow.cpp
@@ -0,0 +1,66 @@
+/*
+ * TaskWindow.cpp
+ *
+ * Created on: Apr 6, 2012
+ * Author: Simon
+ */
+
+#include "interface/Label.h"
+#include "TaskWindow.h"
+#include "Task.h"
+
+TaskWindow::TaskWindow(std::string title_, Task * task_):
+ task(task_),
+ title(title_),
+ ui::Window(ui::Point(-1, -1), ui::Point(300, 200)),
+ progress(0),
+ done(false)
+{
+
+ ui::Label * tempLabel = new ui::Label(ui::Point(3, 3), ui::Point(Size.X-6, 16), title);
+ AddComponent(tempLabel);
+
+ statusLabel = new ui::Label(ui::Point(3, 19), ui::Point(Size.X-6, 16), "");
+ AddComponent(statusLabel);
+
+ ui::Engine::Ref().ShowWindow(this);
+
+ task->SetTaskListener(this);
+ task->Start();
+}
+
+void TaskWindow::NotifyStatus(Task * task)
+{
+ statusLabel->SetText(task->GetStatus());
+}
+
+void TaskWindow::NotifyDone(Task * task)
+{
+ if(ui::Engine::Ref().GetWindow()==this)
+ {
+ ui::Engine::Ref().CloseWindow();
+ delete this;
+ }
+}
+
+void TaskWindow::NotifyProgress(Task * task)
+{
+ progress = task->GetProgress();
+}
+
+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->drawrect(Position.X + 20, Position.Y + 36, Size.X-40, 24, 255, 255, 255, 255);
+
+ 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);
+}
+
+TaskWindow::~TaskWindow() {
+ delete task;
+}
+
diff --git a/src/tasks/TaskWindow.h b/src/tasks/TaskWindow.h
new file mode 100644
index 0000000..cacb59c
--- /dev/null
+++ b/src/tasks/TaskWindow.h
@@ -0,0 +1,32 @@
+/*
+ * 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;
+ bool done;
+ ui::Label * statusLabel;
+public:
+ TaskWindow(std::string title_, Task * task_);
+ virtual void NotifyStatus(Task * task);
+ virtual void NotifyDone(Task * task);
+ virtual void NotifyProgress(Task * task);
+ virtual void OnDraw();
+ virtual ~TaskWindow();
+};
+
+#endif /* TASKWINDOW_H_ */