OSDN Git Service

Happy new year 2014!
[x264-launcher/x264-launcher.git] / src / win_help.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 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_help.h"
23 #include "uic_win_help.h"
24
25 #include "global.h"
26
27 #include <QProcess>
28 #include <QScrollBar>
29 #include <QTimer>
30
31 #define AVS2_BINARY(BIN_DIR, IS_X64) QString("%1/%2/avs2yuv_%2.exe").arg((BIN_DIR), ((IS_X64) ? "x64" : "x86"))
32 #define X264_BINARY(BIN_DIR, IS_10BIT, IS_X64) QString("%1/%2/x264_%3_%2.exe").arg((BIN_DIR), ((IS_X64) ? "x64" : "x86"), ((IS_10BIT) ? "10bit" : "8bit"))
33
34 ///////////////////////////////////////////////////////////////////////////////
35 // Constructor & Destructor
36 ///////////////////////////////////////////////////////////////////////////////
37
38 HelpDialog::HelpDialog(QWidget *parent, bool avs2yuv, bool x64supported, bool use10BitEncoding)
39 :
40         QDialog(parent),
41         m_appDir(QApplication::applicationDirPath() + "/toolset"),
42         m_avs2yuv(avs2yuv),
43         m_x64supported(x64supported),
44         m_use10BitEncoding(use10BitEncoding),
45         m_process(new QProcess()),
46         ui(new Ui::HelpDialog())
47 {
48         //Init the dialog, from the .ui file
49         ui->setupUi(this);
50         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
51
52         //Fix size
53         setMinimumSize(size());
54
55         //Prepare process
56         m_process->setReadChannelMode(QProcess::MergedChannels);
57         m_process->setReadChannel(QProcess::StandardOutput);
58         connect(m_process, SIGNAL(readyRead()), this, SLOT(readyRead()));
59         connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished()));
60
61         m_startAgain = true;
62 }
63
64 HelpDialog::~HelpDialog(void)
65 {
66         delete m_process;
67         delete ui;
68 }
69
70 ///////////////////////////////////////////////////////////////////////////////
71 // Events
72 ///////////////////////////////////////////////////////////////////////////////
73
74 void HelpDialog::showEvent(QShowEvent *event)
75 {
76         ui->logo_x264->setHidden(m_avs2yuv);
77         ui->logo_avisynth->setVisible(m_avs2yuv);
78         
79         QDialog::showEvent(event);
80         
81         m_startAgain = true;
82
83         if(!m_avs2yuv)
84         {
85                 m_process->start(X264_BINARY(m_appDir, m_use10BitEncoding, m_x64supported), QStringList() << "--version");
86         }
87         else
88         {
89                 m_process->start(AVS2_BINARY(m_appDir, m_x64supported), QStringList());
90         }
91
92         if(!m_process->waitForStarted())
93         {
94                 ui->plainTextEdit->appendPlainText(tr("Failed to create x264 process :-("));
95         }
96 }
97
98 void HelpDialog::closeEvent(QCloseEvent *e)
99 {
100         if(m_process->state() != QProcess::NotRunning)
101         {
102                 e->ignore();
103                 x264_beep(x264_beep_warning);
104                 return;
105         }
106
107         QDialog::closeEvent(e);
108 }
109
110 ///////////////////////////////////////////////////////////////////////////////
111 // Slots
112 ///////////////////////////////////////////////////////////////////////////////
113
114 void HelpDialog::readyRead(void)
115 {
116         while(m_process->canReadLine())
117         {
118                 QString line = QString::fromLatin1(m_process->readLine());
119                 while(line.endsWith('\r') || line.endsWith('\n'))
120                 {
121                         line = line.left(line.length() - 1);
122                 }
123                 ui->plainTextEdit->appendPlainText(line);
124         }
125 }
126
127 void HelpDialog::finished(void)
128 {
129         if(m_startAgain)
130         {
131                 m_startAgain = false;
132                 if(!m_avs2yuv)
133                 {
134                         m_process->start(X264_BINARY(m_appDir, m_use10BitEncoding, m_x64supported), QStringList() << "--fullhelp");
135                         ui->plainTextEdit->appendPlainText("\n--------\n");
136
137                         if(!m_process->waitForStarted())
138                         {
139                                 ui->plainTextEdit->appendPlainText(tr("Failed to create x264 process :-("));
140                         }
141                 }
142         }
143         else
144         {
145                 ui->plainTextEdit->verticalScrollBar()->setSliderPosition(0);
146         }
147 }