OSDN Git Service

Refactored code to better manage the encoder binary paths: They are now handled by...
[x264-launcher/x264-launcher.git] / src / win_help.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 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 //Internal
26 #include "global.h"
27 #include "model_options.h"
28 #include "binaries.h"
29 #include "encoder_factory.h"
30
31 //MUtils
32 #include <MUtils/Sound.h>
33
34 //Qt
35 #include <QProcess>
36 #include <QScrollBar>
37 #include <QTimer>
38
39 ///////////////////////////////////////////////////////////////////////////////
40 // Constructor & Destructor
41 ///////////////////////////////////////////////////////////////////////////////
42
43 HelpDialog::HelpDialog(QWidget *parent, bool avs2yuv, const SysinfoModel *const sysinfo, const OptionsModel *const options, const PreferencesModel *const preferences)
44 :
45         QDialog(parent),
46         m_avs2yuv(avs2yuv),
47         m_sysinfo(sysinfo),
48         m_preferences(preferences),
49         m_options(options),
50         m_process(new QProcess()),
51         ui(new Ui::HelpDialog())
52 {
53         //Init the dialog, from the .ui file
54         ui->setupUi(this);
55         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
56
57         //Fix size
58         setMinimumSize(size());
59
60         //Prepare process
61         m_process->setReadChannelMode(QProcess::MergedChannels);
62         m_process->setReadChannel(QProcess::StandardOutput);
63         connect(m_process, SIGNAL(readyRead()), this, SLOT(readyRead()));
64         connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished()));
65
66         m_startAgain = true;
67 }
68
69 HelpDialog::~HelpDialog(void)
70 {
71         delete m_process;
72         delete ui;
73 }
74
75 ///////////////////////////////////////////////////////////////////////////////
76 // Events
77 ///////////////////////////////////////////////////////////////////////////////
78
79 void HelpDialog::showEvent(QShowEvent *event)
80 {
81         ui->logo_x264->setHidden(m_avs2yuv);
82         ui->logo_avisynth->setVisible(m_avs2yuv);
83         
84         QDialog::showEvent(event);
85         
86         m_startAgain = true;
87
88         if(!m_avs2yuv)
89         {
90                 m_process->start(EncoderFactory::getEncoderInfo(m_options->encType()).getBinaryPath(m_sysinfo, m_options->encArch(), m_options->encVariant()), QStringList() << "--version");
91         }
92         else
93         {
94                 m_process->start(AVS_BINARY(m_sysinfo, m_preferences), QStringList());
95         }
96
97         if(!m_process->waitForStarted())
98         {
99                 ui->plainTextEdit->appendPlainText(tr("Failed to create x264 process :-("));
100         }
101 }
102
103 void HelpDialog::closeEvent(QCloseEvent *e)
104 {
105         if(m_process->state() != QProcess::NotRunning)
106         {
107                 e->ignore();
108                 MUtils::Sound::beep(MUtils::Sound::BEEP_WRN);
109                 return;
110         }
111
112         QDialog::closeEvent(e);
113 }
114
115 ///////////////////////////////////////////////////////////////////////////////
116 // Slots
117 ///////////////////////////////////////////////////////////////////////////////
118
119 void HelpDialog::readyRead(void)
120 {
121         while(m_process->canReadLine())
122         {
123                 QString line = QString::fromLatin1(m_process->readLine());
124                 while(line.endsWith('\r') || line.endsWith('\n'))
125                 {
126                         line = line.left(line.length() - 1);
127                 }
128                 ui->plainTextEdit->appendPlainText(line);
129         }
130 }
131
132 void HelpDialog::finished(void)
133 {
134         if(m_startAgain)
135         {
136                 m_startAgain = false;
137                 if(!m_avs2yuv)
138                 {
139                         m_process->start(EncoderFactory::getEncoderInfo(m_options->encType()).getBinaryPath(m_sysinfo, m_options->encArch(), m_options->encVariant()), QStringList() << "--fullhelp");
140                         ui->plainTextEdit->appendPlainText("\n--------\n");
141
142                         if(!m_process->waitForStarted())
143                         {
144                                 ui->plainTextEdit->appendPlainText(tr("Failed to create x264 process :-("));
145                         }
146                 }
147         }
148         else
149         {
150                 ui->plainTextEdit->verticalScrollBar()->setSliderPosition(0);
151         }
152 }