OSDN Git Service

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