OSDN Git Service

Added an option to save the log automatically.
[x264-launcher/x264-launcher.git] / src / win_preferences.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_preferences.h"
23
24 #include "global.h"
25
26 #include <QSettings>
27 #include <QDesktopServices>
28 #include <QMouseEvent>
29 #include <QMessageBox>
30
31 #define UPDATE_CHECKBOX(CHKBOX, VALUE) \
32 { \
33         if((CHKBOX)->isChecked() != (VALUE)) (CHKBOX)->click(); \
34         if((CHKBOX)->isChecked() != (VALUE)) (CHKBOX)->setChecked(VALUE); \
35 }
36
37 PreferencesDialog::PreferencesDialog(QWidget *parent, Preferences *preferences, bool x64)
38 :
39         QDialog(parent),
40         m_x64(x64)
41 {
42         setupUi(this);
43         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
44         setFixedSize(minimumSize());
45
46         labelRunNextJob->installEventFilter(this);
47         labelUse10BitEncoding->installEventFilter(this);
48         labelUse64BitAvs2YUV->installEventFilter(this);
49         labelShutdownComputer->installEventFilter(this);
50         labelSaveLogFiles->installEventFilter(this);
51
52         connect(resetButton, SIGNAL(clicked()), this, SLOT(resetButtonPressed()));
53         connect(checkUse10BitEncoding, SIGNAL(toggled(bool)), this, SLOT(use10BitEncodingToggled(bool)));
54
55         m_preferences = preferences;
56 }
57
58 PreferencesDialog::~PreferencesDialog(void)
59 {
60 }
61
62 void PreferencesDialog::showEvent(QShowEvent *event)
63 {
64         if(event) QDialog::showEvent(event);
65         
66         UPDATE_CHECKBOX(checkRunNextJob, m_preferences->autoRunNextJob);
67         UPDATE_CHECKBOX(checkShutdownComputer, m_preferences->shutdownComputer);
68         UPDATE_CHECKBOX(checkUse64BitAvs2YUV, m_preferences->useAvisyth64Bit);
69         UPDATE_CHECKBOX(checkSaveLogFiles, m_preferences->saveLogFiles);
70
71         checkUse10BitEncoding->blockSignals(true);
72         UPDATE_CHECKBOX(checkUse10BitEncoding, m_preferences->use10BitEncoding);
73         checkUse10BitEncoding->blockSignals(false);
74
75         spinBoxJobCount->setValue(m_preferences->maxRunningJobCount);
76         
77         checkUse64BitAvs2YUV->setEnabled(m_x64);
78         labelUse64BitAvs2YUV->setEnabled(m_x64);
79 }
80
81 bool PreferencesDialog::eventFilter(QObject *o, QEvent *e)
82 {
83         emulateMouseEvent(o, e, labelRunNextJob, checkRunNextJob);
84         emulateMouseEvent(o, e, labelShutdownComputer, checkShutdownComputer);
85         emulateMouseEvent(o, e, labelUse10BitEncoding, checkUse10BitEncoding);
86         emulateMouseEvent(o, e, labelUse64BitAvs2YUV, checkUse64BitAvs2YUV);
87         emulateMouseEvent(o, e, labelSaveLogFiles, checkSaveLogFiles);
88         return false;
89 }
90
91 void PreferencesDialog::emulateMouseEvent(QObject *object, QEvent *event, QWidget *source, QWidget *target)
92 {
93         if(object == source)
94         {
95                 if((event->type() == QEvent::MouseButtonPress) || (event->type() == QEvent::MouseButtonRelease))
96                 {
97                         if(QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event))
98                         {
99                                 qApp->postEvent(target, new QMouseEvent
100                                 (
101                                         event->type(),
102                                         qApp->widgetAt(mouseEvent->globalPos()) == source ? QPoint(1, 1) : QPoint(INT_MAX, INT_MAX),
103                                         Qt::LeftButton,
104                                         0, 0
105                                 ));
106                         }
107                 }
108         }
109 }
110
111 void PreferencesDialog::done(int n)
112 {
113         m_preferences->autoRunNextJob = checkRunNextJob->isChecked();
114         m_preferences->shutdownComputer = checkShutdownComputer->isChecked();
115         m_preferences->use10BitEncoding = checkUse10BitEncoding->isChecked();
116         m_preferences->useAvisyth64Bit = checkUse64BitAvs2YUV->isChecked();
117         m_preferences->saveLogFiles = checkSaveLogFiles->isChecked();
118         m_preferences->maxRunningJobCount = spinBoxJobCount->value();
119
120         savePreferences(m_preferences);
121         QDialog::done(n);
122 }
123
124 void PreferencesDialog::resetButtonPressed(void)
125 {
126         initPreferences(m_preferences);
127         showEvent(NULL);
128 }
129
130 void PreferencesDialog::use10BitEncodingToggled(bool checked)
131 {
132         if(checked)
133         {
134                 QString text;
135                 text += tr("<nobr>Please note that 10&minus;Bit H.264 streams are <b>not</b> currently supported by hardware (standalone) players!</nobr><br>");
136                 text += tr("<nobr>To play such streams, you will need an <i>up&minus;to&minus;date</i> ffdshow&minus;tryouts, CoreAVC 3.x or another supported s/w decoder.</nobr><br>");
137                 text += tr("<nobr>Also be aware that hardware&minus;acceleration (CUDA, DXVA, etc) usually will <b>not</b> work with 10&minus;Bit H.264 streams.</nobr><br>");
138                 
139                 if(QMessageBox::warning(this, tr("10-Bit Encoding"), text, tr("Continue"), tr("Revert"), QString(), 1) != 0)
140                 {
141                         UPDATE_CHECKBOX(checkUse10BitEncoding, false);
142                 }
143         }
144 }
145
146 ///////////////////////////////////////////////////////////////////////////////
147 // Static Functions
148 ///////////////////////////////////////////////////////////////////////////////
149
150 void PreferencesDialog::initPreferences(Preferences *preferences)
151 {
152         memset(preferences, 0, sizeof(Preferences));
153
154         preferences->autoRunNextJob = true;
155         preferences->maxRunningJobCount = 1;
156         preferences->shutdownComputer = false;
157         preferences->use10BitEncoding = false;
158         preferences->useAvisyth64Bit = false;
159         preferences->saveLogFiles = false;
160 }
161
162 void PreferencesDialog::loadPreferences(Preferences *preferences)
163 {
164         const QString appDir = x264_data_path();
165         QSettings settings(QString("%1/preferences.ini").arg(appDir), QSettings::IniFormat);
166
167         Preferences defaults;
168         initPreferences(&defaults);
169
170         settings.beginGroup("preferences");
171         preferences->autoRunNextJob = settings.value("auto_run_next_job", QVariant(defaults.autoRunNextJob)).toBool();
172         preferences->maxRunningJobCount = qBound(1U, settings.value("max_running_job_count", QVariant(defaults.maxRunningJobCount)).toUInt(), 16U);
173         preferences->shutdownComputer = settings.value("shutdown_computer_on_completion", QVariant(defaults.shutdownComputer)).toBool();
174         preferences->use10BitEncoding = settings.value("use_10bit_encoding", QVariant(defaults.use10BitEncoding)).toBool();
175         preferences->useAvisyth64Bit = settings.value("use_64bit_avisynth", QVariant(defaults.useAvisyth64Bit)).toBool();
176         preferences->saveLogFiles = settings.value("save_log_files", QVariant(defaults.saveLogFiles)).toBool();
177 }
178
179 void PreferencesDialog::savePreferences(Preferences *preferences)
180 {
181         const QString appDir = x264_data_path();
182         QSettings settings(QString("%1/preferences.ini").arg(appDir), QSettings::IniFormat);
183
184         settings.beginGroup("preferences");
185         settings.setValue("auto_run_next_job", preferences->autoRunNextJob);
186         settings.setValue("shutdown_computer_on_completion", preferences->shutdownComputer);
187         settings.setValue("max_running_job_count", preferences->maxRunningJobCount);
188         settings.setValue("use_10bit_encoding", preferences->use10BitEncoding);
189         settings.setValue("use_64bit_avisynth", preferences->useAvisyth64Bit);
190         settings.setValue("save_log_files", preferences->saveLogFiles);
191         settings.sync();
192 }
193