OSDN Git Service

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