Skip to content

Commit

Permalink
Refactor MeltJob into AbstractJob and EncodeJob.
Browse files Browse the repository at this point in the history
It does not make sense to offer Open and Show In Folder menu items for
filter analysis jobs. And in the future, there will be perhaps ffmpeg-
  • Loading branch information
ddennedy committed Jan 24, 2015
1 parent 191d942 commit 640869b
Show file tree
Hide file tree
Showing 15 changed files with 421 additions and 254 deletions.
3 changes: 2 additions & 1 deletion src/docks/encodedock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "mainwindow.h"
#include "settings.h"
#include "qmltypes/qmlapplication.h"
#include "jobs/encodejob.h"

#include <QtDebug>
#include <QtWidgets>
Expand Down Expand Up @@ -375,7 +376,7 @@ MeltJob* EncodeDock::createMeltJob(const QString& target, int realtime, int pass
dom.save(ts, 2);
f1.close();

return new MeltJob(target, tmpName);
return new EncodeJob(target, tmpName);
}

void EncodeDock::runMelt(const QString& target, int realtime)
Expand Down
55 changes: 13 additions & 42 deletions src/docks/jobsdock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <QtWidgets>
#include <QDebug>
#include "dialogs/textviewerdialog.h"
#include "mainwindow.h"

JobsDock::JobsDock(QWidget *parent) :
QDockWidget(parent),
Expand All @@ -46,24 +45,30 @@ JobsDock::~JobsDock()
delete ui;
}

AbstractJob *JobsDock::currentJob() const
{
QModelIndex index = ui->treeView->currentIndex();
if (!index.isValid()) return 0;
return JOBS.jobFromIndex(index);
}

void JobsDock::on_treeView_customContextMenuRequested(const QPoint &pos)
{
QModelIndex index = ui->treeView->currentIndex();
if (!index.isValid()) return;
QMenu menu(this);
MeltJob* job = JOBS.jobFromIndex(index);
AbstractJob* job = JOBS.jobFromIndex(index);
if (job) {
if (job->ran() && job->state() == QProcess::NotRunning && job->exitStatus() == QProcess::NormalExit) {
menu.addAction(ui->actionOpen);
menu.addAction(ui->actionOpenFolder);
menu.addActions(job->successActions());
}
if (job->stopped() || (JOBS.isPaused() && !job->ran()))
menu.addAction(ui->actionRun);
if (job->state() == QProcess::Running)
menu.addAction(ui->actionStopJob);
if (job->ran())
menu.addAction(ui->actionViewLog);
menu.addAction(ui->actionViewXml);
menu.addActions(job->standardActions());
}
menu.exec(mapToGlobal(pos));
}
Expand All @@ -72,15 +77,15 @@ void JobsDock::on_actionStopJob_triggered()
{
QModelIndex index = ui->treeView->currentIndex();
if (!index.isValid()) return;
MeltJob* job = JOBS.jobFromIndex(index);
AbstractJob* job = JOBS.jobFromIndex(index);
if (job) job->stop();
}

void JobsDock::on_actionViewLog_triggered()
{
QModelIndex index = ui->treeView->currentIndex();
if (!index.isValid()) return;
MeltJob* job = JOBS.jobFromIndex(index);
AbstractJob* job = JOBS.jobFromIndex(index);
if (job) {
TextViewerDialog dialog(this);
dialog.setWindowTitle(tr("Job Log"));
Expand All @@ -89,19 +94,6 @@ void JobsDock::on_actionViewLog_triggered()
}
}

void JobsDock::on_actionViewXml_triggered()
{
QModelIndex index = ui->treeView->currentIndex();
if (!index.isValid()) return;
MeltJob* job = JOBS.jobFromIndex(index);
if (job) {
TextViewerDialog dialog(this);
dialog.setWindowTitle(tr("MLT XML"));
dialog.setText(job->xml());
dialog.exec();
}
}

void JobsDock::on_pauseButton_toggled(bool checked)
{
if (checked)
Expand All @@ -110,36 +102,15 @@ void JobsDock::on_pauseButton_toggled(bool checked)
JOBS.resume();
}

void JobsDock::on_actionOpen_triggered()
{
QModelIndex index = ui->treeView->currentIndex();
if (!index.isValid()) return;
MeltJob* job = JOBS.jobFromIndex(index);
if (job)
MAIN.open(job->objectName().toUtf8().constData());
}

void JobsDock::on_actionRun_triggered()
{
QModelIndex index = ui->treeView->currentIndex();
if (!index.isValid()) return;
MeltJob* job = JOBS.jobFromIndex(index);
AbstractJob* job = JOBS.jobFromIndex(index);
if (job) job->start();
}

void JobsDock::on_menuButton_clicked()
{
on_treeView_customContextMenuRequested(ui->menuButton->mapToParent(QPoint(0, 0)));
}

void JobsDock::on_actionOpenFolder_triggered()
{
QModelIndex index = ui->treeView->currentIndex();
if (!index.isValid()) return;
MeltJob* job = JOBS.jobFromIndex(index);
if (job) {
QFileInfo fi(job->objectName());
QUrl url(QString("file://").append(fi.path()), QUrl::TolerantMode);
QDesktopServices::openUrl(url);
}
}
6 changes: 3 additions & 3 deletions src/docks/jobsdock.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <QDockWidget>

class AbstractJob;

namespace Ui {
class JobsDock;
}
Expand All @@ -32,6 +34,7 @@ class JobsDock : public QDockWidget
public:
explicit JobsDock(QWidget *parent = 0);
~JobsDock();
AbstractJob* currentJob() const;

private:
Ui::JobsDock *ui;
Expand All @@ -40,12 +43,9 @@ private slots:
void on_treeView_customContextMenuRequested(const QPoint &pos);
void on_actionStopJob_triggered();
void on_actionViewLog_triggered();
void on_actionViewXml_triggered();
void on_pauseButton_toggled(bool checked);
void on_actionOpen_triggered();
void on_actionRun_triggered();
void on_menuButton_clicked();
void on_actionOpenFolder_triggered();
};

#endif // JOBSDOCK_H
23 changes: 1 addition & 22 deletions src/docks/jobsdock.ui
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<x>0</x>
<y>0</y>
<width>232</width>
<height>235</height>
<height>225</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -167,22 +167,6 @@ the list. This does not stop a currently running job. Right-
<string>View the messages of MLT and FFmpeg </string>
</property>
</action>
<action name="actionViewXml">
<property name="text">
<string>View XML</string>
</property>
<property name="toolTip">
<string>View the MLT XML for this job</string>
</property>
</action>
<action name="actionOpen">
<property name="text">
<string>Open</string>
</property>
<property name="toolTip">
<string>Open the output file in the Shotcut player</string>
</property>
</action>
<action name="actionRun">
<property name="text">
<string>Run</string>
Expand All @@ -191,11 +175,6 @@ the list. This does not stop a currently running job. Right-
<string>Restart a stopped job</string>
</property>
</action>
<action name="actionOpenFolder">
<property name="text">
<string>Show In Folder</string>
</property>
</action>
</widget>
<resources>
<include location="../../icons/resources.qrc"/>
Expand Down
125 changes: 8 additions & 117 deletions src/jobqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,115 +22,6 @@
#include "mainwindow.h"
#include "settings.h"

MeltJob::MeltJob(const QString& name, const QString& xml)
: QProcess(0)
, m_xml(xml)
, m_ran(false)
, m_killed(false)
, m_label(name)
{
setObjectName(name);
connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onFinished(int, QProcess::ExitStatus)));
connect(this, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}

MeltJob::~MeltJob()
{
QFile::remove(m_xml);
}

void MeltJob::start()
{
QString shotcutPath = qApp->applicationDirPath();
// QString shotcutPath("/Applications/Shotcut.app/Contents/MacOS");
#ifdef Q_OS_WIN
QFileInfo meltPath(shotcutPath, "qmelt.exe");
#else
QFileInfo meltPath(shotcutPath, "qmelt");
#endif
setReadChannel(QProcess::StandardError);
QStringList args;
args << "-progress2";
args << m_xml;
qDebug() << meltPath.absoluteFilePath() << args;
#ifdef Q_OS_WIN
QProcess::start(meltPath.absoluteFilePath(), args);
#else
args.prepend(meltPath.absoluteFilePath());
QProcess::start("/usr/bin/nice", args);
#endif
m_ran = true;
}

void MeltJob::setModelIndex(const QModelIndex& index)
{
m_index = index;
}

QModelIndex MeltJob::modelIndex() const
{
return m_index;
}

bool MeltJob::ran() const
{
return m_ran;
}

bool MeltJob::stopped() const
{
return m_killed;
}

void MeltJob::appendToLog(const QString& s)
{
m_log.append(s);
}

QString MeltJob::log() const
{
return m_log;
}

QString MeltJob::xml() const
{
QFile f(m_xml);
f.open(QIODevice::ReadOnly);
QString s(f.readAll());
f.close();
return s;
}

void MeltJob::setLabel(const QString &label)
{
m_label = label;
}

void MeltJob::stop()
{
terminate();
QTimer::singleShot(2000, this, SLOT(kill()));
m_killed = true;
}

void MeltJob::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
if (exitStatus == QProcess::NormalExit && exitCode == 0) {
qDebug() << "melt succeeeded";
emit finished(this, true);
} else {
qDebug() << "melt failed with" << exitCode;
emit finished(this, false);
}
}

void MeltJob::onReadyRead()
{
emit messageAvailable(this);
}

///////////////////////////////////////////////////////////////////////////////

JobQueue::JobQueue(QObject *parent) :
QStandardItemModel(0, COLUMN_COUNT, parent),
m_paused(false)
Expand All @@ -151,7 +42,7 @@ void JobQueue::cleanup()
qDeleteAll(m_jobs);
}

MeltJob* JobQueue::add(MeltJob* job)
AbstractJob* JobQueue::add(AbstractJob* job)
{
int row = rowCount();
QList<QStandardItem*> items;
Expand All @@ -160,8 +51,8 @@ MeltJob* JobQueue::add(MeltJob* job)
appendRow(items);
job->setParent(this);
job->setModelIndex(index(row, COLUMN_STATUS));
connect(job, SIGNAL(messageAvailable(MeltJob*)), this, SLOT(onMessageAvailable(MeltJob*)));
connect(job, SIGNAL(finished(MeltJob*, bool)), this, SLOT(onFinished(MeltJob*, bool)));
connect(job, SIGNAL(messageAvailable(AbstractJob*)), this, SLOT(onMessageAvailable(AbstractJob*)));
connect(job, SIGNAL(finished(AbstractJob*, bool)), this, SLOT(onFinished(AbstractJob*, bool)));
m_mutex.lock();
m_jobs.append(job);
m_mutex.unlock();
Expand All @@ -171,7 +62,7 @@ MeltJob* JobQueue::add(MeltJob* job)
return job;
}

void JobQueue::onMessageAvailable(MeltJob* job)
void JobQueue::onMessageAvailable(AbstractJob* job)
{
QString msg = job->readLine();
// qDebug() << msg;
Expand All @@ -187,7 +78,7 @@ void JobQueue::onMessageAvailable(MeltJob* job)
}
}

void JobQueue::onFinished(MeltJob* job, bool isSuccess)
void JobQueue::onFinished(AbstractJob* job, bool isSuccess)
{
QStandardItem* item = itemFromIndex(job->modelIndex());
if (item) {
Expand All @@ -206,7 +97,7 @@ void JobQueue::startNextJob()
if (m_paused) return;
QMutexLocker locker(&m_mutex);
if (!m_jobs.isEmpty()) {
foreach(MeltJob* job, m_jobs) {
foreach(AbstractJob* job, m_jobs) {
// if there is already a job started or running, then exit
if (job->ran() && job->state() != QProcess::NotRunning)
break;
Expand All @@ -219,7 +110,7 @@ void JobQueue::startNextJob()
}
}

MeltJob* JobQueue::jobFromIndex(const QModelIndex& index) const
AbstractJob* JobQueue::jobFromIndex(const QModelIndex& index) const
{
return m_jobs.at(index.row());
}
Expand All @@ -242,7 +133,7 @@ bool JobQueue::isPaused() const

bool JobQueue::hasIncomplete() const
{
foreach (MeltJob* job, m_jobs) {
foreach (AbstractJob* job, m_jobs) {
if (!job->ran() || job->state() == QProcess::Running)
return true;
}
Expand Down

0 comments on commit 640869b

Please sign in to comment.