OSDN Git Service

Improved queue support + some GUI redesign.
[x264-launcher/x264-launcher.git] / src / win_main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "win_main.h"
23
24 #include "global.h"
25 #include "model_jobList.h"
26
27 #include <QDate>
28 #include <QTimer>
29 #include <QCloseEvent>
30 #include <QMessageBox>
31
32 ///////////////////////////////////////////////////////////////////////////////
33 // Constructor & Destructor
34 ///////////////////////////////////////////////////////////////////////////////
35
36 MainWindow::MainWindow(void)
37 {
38         //Init the dialog, from the .ui file
39         setupUi(this);
40         setWindowFlags(windowFlags() ^ Qt::WindowMaximizeButtonHint);
41
42         //Register meta types
43         qRegisterMetaType<QUuid>("QUuid");
44         qRegisterMetaType<EncodeThread::JobStatus>("EncodeThread::JobStatus");
45
46         //Freeze minimum size
47         setMinimumSize(size());
48
49         //Show version
50         setWindowTitle(QString("%1 [%2]").arg(windowTitle(), x264_version_date().toString(Qt::ISODate)));
51
52         //Create model
53         m_jobList = new JobListModel();
54         connect(m_jobList, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(jobChangedData(QModelIndex, QModelIndex)));
55         jobsView->setModel(m_jobList);
56
57         //Setup view
58         jobsView->horizontalHeader()->setSectionHidden(3, true);
59         jobsView->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
60         jobsView->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);
61         jobsView->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);
62         jobsView->horizontalHeader()->resizeSection(1, 150);
63         jobsView->horizontalHeader()->resizeSection(2, 90);
64         jobsView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
65         connect(jobsView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(jobSelected(QModelIndex, QModelIndex)));
66
67         //Enable buttons
68         connect(buttonAddJob, SIGNAL(clicked()), this, SLOT(addButtonPressed()));
69         connect(buttonStartJob, SIGNAL(clicked()), this, SLOT(startButtonPressed()));
70         connect(buttonAbortJob, SIGNAL(clicked()), this, SLOT(abortButtonPressed()));
71
72         //Enable menu
73         connect(actionAbout, SIGNAL(triggered()), this, SLOT(showAbout()));
74 }
75
76 MainWindow::~MainWindow(void)
77 {
78         X264_DELETE(m_jobList);
79 }
80
81 ///////////////////////////////////////////////////////////////////////////////
82 // Slots
83 ///////////////////////////////////////////////////////////////////////////////
84
85 void MainWindow::addButtonPressed(void)
86 {
87         EncodeThread *thrd = new EncodeThread();
88         QModelIndex newIndex = m_jobList->insertJob(thrd);
89         jobsView->selectRow(newIndex.row());
90 }
91
92 void MainWindow::startButtonPressed(void)
93 {
94         m_jobList->startJob(jobsView->currentIndex());
95 }
96
97 void MainWindow::abortButtonPressed(void)
98 {
99         m_jobList->abortJob(jobsView->currentIndex());
100 }
101
102 void MainWindow::jobSelected(const QModelIndex & current, const QModelIndex & previous)
103 {
104         qDebug("Job selected: %d", current.row());
105         
106         if(logView->model())
107         {
108                 disconnect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
109         }
110         
111         logView->setModel(m_jobList->getLogFile(current));
112         connect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
113         QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
114         
115         progressBar->setValue(m_jobList->getJobProgress(current));
116         editDetails->setText(m_jobList->data(m_jobList->index(current.row(), 3, QModelIndex()), Qt::DisplayRole).toString());
117         updateButtons(m_jobList->getJobStatus(current));
118 }
119
120 void MainWindow::jobChangedData(const QModelIndex &topLeft, const  QModelIndex &bottomRight)
121 {
122         int selected = jobsView->currentIndex().row();
123         
124         if(topLeft.column() <= 1 && bottomRight.column() >= 1)
125         {
126                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
127                 {
128                         EncodeThread::JobStatus status =  m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
129                         if(i == selected)
130                         {
131                                 qDebug("Current job changed status!");
132                                 updateButtons(status);
133                         }
134                         if(status == EncodeThread::JobStatus_Completed)
135                         {
136                                 QTimer::singleShot(0, this, SLOT(launchNextJob()));
137                         }
138                 }
139         }
140         else if(topLeft.column() <= 2 && bottomRight.column() >= 2)
141         {
142                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
143                 {
144                         if(i == selected)
145                         {
146                                 progressBar->setValue(m_jobList->getJobProgress(m_jobList->index(i, 0, QModelIndex())));
147                                 break;
148                         }
149                 }
150         }
151         else if(topLeft.column() <= 3 && bottomRight.column() >= 3)
152         {
153                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
154                 {
155                         if(i == selected)
156                         {
157                                 editDetails->setText(m_jobList->data(m_jobList->index(i, 3, QModelIndex()), Qt::DisplayRole).toString());
158                                 break;
159                         }
160                 }
161         }
162 }
163
164 void MainWindow::jobLogExtended(const QModelIndex & parent, int start, int end)
165 {
166         QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
167 }
168
169 void MainWindow::showAbout(void)
170 {
171         QString text;
172         const char *url = "http://mulder.brhack.net/";
173
174         text += QString().sprintf("<nobr><tt>Simple x264 Launcher v%u.%02u - use 64-Bit x264 with 32-Bit Avisynth<br>", x264_version_major(), x264_version_minor());
175         text += QString().sprintf("Copyright (c) 2004-%04d LoRd_MuldeR &lt;mulder2@gmx.de&gt;. Some rights reserved.<br>", qMax(x264_version_date().year(),QDate::currentDate().year()));
176         text += QString().sprintf("Built on %s at %s with %s for Win-%s.<br><br>", x264_version_date().toString(Qt::ISODate).toLatin1().constData(), x264_version_time(), x264_version_compiler(), x264_version_arch());
177         text += QString().sprintf("This program is free software: you can redistribute it and/or modify<br>");
178         text += QString().sprintf("it under the terms of the GNU General Public License &lt;http://www.gnu.org/&gt;.<br>");
179         text += QString().sprintf("Note that this program is distributed with ABSOLUTELY NO WARRANTY.<br><br>");
180         text += QString().sprintf("Please check the web-site at <a href=\"%s\">%s</a> for updates !!!<br></tt></nobr>", url, url);
181
182         QMessageBox::information(this, tr("About..."), text.replace("-", "&minus;"));
183 }
184
185 void MainWindow::launchNextJob(void)
186 {
187         const int rows = m_jobList->rowCount(QModelIndex());
188
189         for(int i = 0; i < rows; i++)
190         {
191                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
192                 if(status == EncodeThread::JobStatus_Running || status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2)
193                 {
194                         qWarning("Still have a job running, won't launch next yet!");
195                         return;
196                 }
197         }
198
199         for(int i = 0; i < rows; i++)
200         {
201                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
202                 if(status == EncodeThread::JobStatus_Enqueued)
203                 {
204                         m_jobList->startJob(m_jobList->index(i, 0, QModelIndex()));
205                         return;
206                 }
207         }
208
209         qWarning("No enqueued jobs left!");
210 }
211
212 ///////////////////////////////////////////////////////////////////////////////
213 // Event functions
214 ///////////////////////////////////////////////////////////////////////////////
215
216 void MainWindow::closeEvent(QCloseEvent *e)
217 {
218         const int rows = m_jobList->rowCount(QModelIndex());
219         
220         for(int i = 0; i < rows; i++)
221         {
222                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
223                 if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed)
224                 {
225                         e->ignore();
226                         MessageBeep(MB_ICONWARNING);
227                         break;
228                 }
229         }
230 }
231
232 ///////////////////////////////////////////////////////////////////////////////
233 // Private functions
234 ///////////////////////////////////////////////////////////////////////////////
235
236 void MainWindow::updateButtons(EncodeThread::JobStatus status)
237 {
238         qDebug("MainWindow::updateButtons(void)");
239
240         buttonStartJob->setEnabled(status == EncodeThread::JobStatus_Enqueued);
241         buttonAbortJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running ||
242                 status == EncodeThread::JobStatus_Running_Pass1 || status == EncodeThread::JobStatus_Running_Pass2 );
243 }