base task and taskmanager class
This commit is contained in:
+4
-2
@@ -31,6 +31,7 @@
|
||||
#include "panel/panelfocusmanager.h"
|
||||
#include "panel/project/project.h"
|
||||
#include "project/item/footage/footage.h"
|
||||
#include "task/taskmanager.h"
|
||||
#include "ui/icons/icons.h"
|
||||
#include "ui/style/style.h"
|
||||
#include "widget/menu/menushared.h"
|
||||
@@ -151,9 +152,10 @@ void Core::ImportFiles(const QStringList &urls)
|
||||
|
||||
f->set_filename(url);
|
||||
f->set_name(file_info.fileName());
|
||||
//file_info.lastModified();
|
||||
f->set_timestamp(file_info.lastModified());
|
||||
|
||||
olive::ProbeMedia(f);
|
||||
//olive::ProbeMedia(f);
|
||||
olive::task_manager.AddTask(new Task());
|
||||
|
||||
active_project->root()->add_child(f);
|
||||
|
||||
|
||||
@@ -20,5 +20,7 @@ set(OLIVE_SOURCES
|
||||
task/task.cpp
|
||||
task/taskmanager.h
|
||||
task/taskmanager.cpp
|
||||
task/taskthread.h
|
||||
task/taskthread.cpp
|
||||
PARENT_SCOPE
|
||||
)
|
||||
|
||||
+46
-1
@@ -20,7 +20,52 @@
|
||||
|
||||
#include "task.h"
|
||||
|
||||
Task::Task()
|
||||
Task::Task() :
|
||||
status_(kWaiting),
|
||||
thread_(this)
|
||||
{
|
||||
connect(&thread_, SIGNAL(finished()), this, SLOT(ThreadComplete()));
|
||||
}
|
||||
|
||||
void Task::Start()
|
||||
{
|
||||
set_status(kWorking);
|
||||
|
||||
thread_.start();
|
||||
}
|
||||
|
||||
bool Task::Action()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
const Task::Status &Task::status()
|
||||
{
|
||||
return status_;
|
||||
}
|
||||
|
||||
const QString &Task::error()
|
||||
{
|
||||
return error_;
|
||||
}
|
||||
|
||||
void Task::SetError(const QString &s)
|
||||
{
|
||||
error_ = s;
|
||||
}
|
||||
|
||||
void Task::set_status(const Task::Status &status)
|
||||
{
|
||||
status_ = status;
|
||||
|
||||
emit StatusChanged(status_);
|
||||
}
|
||||
|
||||
void Task::ThreadComplete()
|
||||
{
|
||||
if (thread_.result()) {
|
||||
set_status(kFinished);
|
||||
} else {
|
||||
set_status(kError);
|
||||
}
|
||||
}
|
||||
|
||||
+38
-1
@@ -21,11 +21,48 @@
|
||||
#ifndef TASK_H
|
||||
#define TASK_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class Task
|
||||
#include "task/taskthread.h"
|
||||
|
||||
class Task : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Status {
|
||||
kWaiting,
|
||||
kWorking,
|
||||
kFinished,
|
||||
kError
|
||||
};
|
||||
|
||||
Task();
|
||||
|
||||
void Start();
|
||||
|
||||
virtual bool Action();
|
||||
|
||||
const Status& status();
|
||||
|
||||
const QString& error();
|
||||
|
||||
protected:
|
||||
void SetError(const QString& s);
|
||||
|
||||
signals:
|
||||
void StatusChanged(Task::Status s);
|
||||
|
||||
private:
|
||||
void set_status(const Status& status);
|
||||
|
||||
Status status_;
|
||||
|
||||
TaskThread thread_;
|
||||
|
||||
QString error_;
|
||||
|
||||
private slots:
|
||||
void ThreadComplete();
|
||||
};
|
||||
|
||||
#endif // TASK_H
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "taskmanager.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QThread>
|
||||
|
||||
TaskManager olive::task_manager;
|
||||
@@ -28,3 +29,32 @@ TaskManager::TaskManager()
|
||||
{
|
||||
maximum_task_count_ = QThread::idealThreadCount();
|
||||
}
|
||||
|
||||
void TaskManager::AddTask(Task* t)
|
||||
{
|
||||
connect(t, SIGNAL(StatusChanged(Task::Status)), this, SLOT(TaskCallback(Task::Status)));
|
||||
|
||||
tasks_.append(t);
|
||||
|
||||
if (tasks_.size() < maximum_task_count_) {
|
||||
t->Start();
|
||||
}
|
||||
}
|
||||
|
||||
void TaskManager::TaskCallback(Task::Status status)
|
||||
{
|
||||
switch (status) {
|
||||
case Task::kWaiting:
|
||||
qDebug() << sender() << "is waiting...";
|
||||
break;
|
||||
case Task::kWorking:
|
||||
qDebug() << sender() << "is working...";
|
||||
break;
|
||||
case Task::kFinished:
|
||||
qDebug() << sender() << "finished successfully.";
|
||||
break;
|
||||
case Task::kError:
|
||||
qDebug() << sender() << "failed:" << static_cast<Task*>(sender())->error();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
+11
-2
@@ -21,17 +21,26 @@
|
||||
#ifndef TASKMANAGER_H
|
||||
#define TASKMANAGER_H
|
||||
|
||||
#include <QVector>
|
||||
|
||||
class TaskManager
|
||||
#include "task/task.h"
|
||||
|
||||
class TaskManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
TaskManager();
|
||||
|
||||
void AddTask();
|
||||
void AddTask(Task *t);
|
||||
|
||||
private:
|
||||
QVector<Task*> tasks_;
|
||||
|
||||
int maximum_task_count_;
|
||||
|
||||
private slots:
|
||||
void TaskCallback(Task::Status status);
|
||||
|
||||
};
|
||||
|
||||
namespace olive {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "taskthread.h"
|
||||
|
||||
#include "task/task.h"
|
||||
|
||||
TaskThread::TaskThread(Task *parent) :
|
||||
parent_(parent),
|
||||
result_(false)
|
||||
{
|
||||
}
|
||||
|
||||
void TaskThread::run()
|
||||
{
|
||||
result_ = parent_->Action();
|
||||
}
|
||||
|
||||
bool TaskThread::result()
|
||||
{
|
||||
return result_;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#ifndef TASKTHREAD_H
|
||||
#define TASKTHREAD_H
|
||||
|
||||
#include <QThread>
|
||||
|
||||
class Task;
|
||||
|
||||
class TaskThread : public QThread
|
||||
{
|
||||
public:
|
||||
TaskThread(Task* parent);
|
||||
|
||||
virtual void run() override;
|
||||
|
||||
bool result();
|
||||
|
||||
private:
|
||||
Task* parent_;
|
||||
|
||||
bool result_;
|
||||
};
|
||||
|
||||
#endif // TASKTHREAD_H
|
||||
Reference in New Issue
Block a user