OSDN Git Service

Show progress and progress details in main window!
[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->selectionModel()->setCurrentIndex(newIndex, QItemSelectionModel::ClearAndSelect);
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 void MainWindow::jobSelected(const QModelIndex & current, const QModelIndex & previous)
102 {
103         qDebug("Job selected: %d", current.row());
104         
105         if(logView->model())
106         {
107                 disconnect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
108         }
109         
110         logView->setModel(m_jobList->getLogFile(current));
111         connect(logView->model(), SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(jobLogExtended(QModelIndex, int, int)));
112         QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
113         
114         progressBar->setValue(m_jobList->getJobProgress(current));
115         editDetails->setText(m_jobList->data(m_jobList->index(current.row(), 3, QModelIndex()), Qt::DisplayRole).toString());
116         updateButtons(m_jobList->getJobStatus(current));
117 }
118
119 void MainWindow::jobChangedData(const QModelIndex &topLeft, const  QModelIndex &bottomRight)
120 {
121         int selected = jobsView->currentIndex().row();
122         
123         if(topLeft.column() <= 1 && bottomRight.column() >= 1)
124         {
125                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
126                 {
127                         if(i == selected)
128                         {
129                                 qDebug("Current job changed status!");
130                                 updateButtons(m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex())));
131                                 break;
132                         }
133                 }
134         }
135         else if(topLeft.column() <= 2 && bottomRight.column() >= 2)
136         {
137                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
138                 {
139                         if(i == selected)
140                         {
141                                 progressBar->setValue(m_jobList->getJobProgress(m_jobList->index(i, 0, QModelIndex())));
142                                 break;
143                         }
144                 }
145         }
146         else if(topLeft.column() <= 3 && bottomRight.column() >= 3)
147         {
148                 for(int i = topLeft.row(); i <= bottomRight.row(); i++)
149                 {
150                         if(i == selected)
151                         {
152                                 editDetails->setText(m_jobList->data(m_jobList->index(i, 3, QModelIndex()), Qt::DisplayRole).toString());
153                                 break;
154                         }
155                 }
156         }
157 }
158
159 void MainWindow::jobLogExtended(const QModelIndex & parent, int start, int end)
160 {
161         QTimer::singleShot(0, logView, SLOT(scrollToBottom()));
162 }
163
164 void MainWindow::showAbout(void)
165 {
166         QString text;
167         const char *url = "http://mulder.brhack.net/";
168
169         text += QString().sprintf("<nobr><tt>Simple x264 Launcher v%u.%02u &minus; use 64&minus;Bit x264 with 32&minus;Bit Avisynth<br>", x264_version_major(), x264_version_minor());
170         text += QString().sprintf("Copyright (c) 2004&minus;%04d LoRd_MuldeR &lt;mulder2@gmx.de&gt;. Some rights reserved.<br>", qMax(x264_version_date().year(),QDate::currentDate().year()));
171         text += QString().sprintf("Built on %s at %s with %s for Win&minus;%s.<br><br>", x264_version_date().toString(Qt::ISODate).toLatin1().constData(), x264_version_time(), x264_version_compiler(), x264_version_arch());
172         text += QString().sprintf("This program is free software: you can redistribute it and/or modify<br>");
173         text += QString().sprintf("it under the terms of the GNU General Public License &lt;http://www.gnu.org/&gt;.<br>");
174         text += QString().sprintf("Note that this program is distributed with ABSOLUTELY NO WARRANTY.<br><br>");
175         text += QString().sprintf("Please check the web&minus;site at <a href=\"%s\">%s</a> for updates !!!<br></tt></nobr>", url, url);
176
177         QMessageBox::information(this, tr("About..."), text);
178 }
179
180 ///////////////////////////////////////////////////////////////////////////////
181 // Event functions
182 ///////////////////////////////////////////////////////////////////////////////
183
184 void MainWindow::closeEvent(QCloseEvent *e)
185 {
186         const int rows = m_jobList->rowCount(QModelIndex());
187         
188         for(int i = 0; i < rows; i++)
189         {
190                 EncodeThread::JobStatus status = m_jobList->getJobStatus(m_jobList->index(i, 0, QModelIndex()));
191                 if(status != EncodeThread::JobStatus_Completed && status != EncodeThread::JobStatus_Aborted && status != EncodeThread::JobStatus_Failed)
192                 {
193                         e->ignore();
194                         MessageBeep(MB_ICONWARNING);
195                         break;
196                 }
197         }
198 }
199
200 ///////////////////////////////////////////////////////////////////////////////
201 // Private functions
202 ///////////////////////////////////////////////////////////////////////////////
203
204 void MainWindow::updateButtons(EncodeThread::JobStatus status)
205 {
206         qDebug("MainWindow::updateButtons(void)");
207
208         buttonStartJob->setEnabled(status == EncodeThread::JobStatus_Enqueued);
209         buttonAbortJob->setEnabled(status == EncodeThread::JobStatus_Indexing || status == EncodeThread::JobStatus_Running);
210 }