OSDN Git Service

Added sound effect when a job has completed or failed (optionally, disabled by default).
[x264-launcher/x264-launcher.git] / src / win_preferences.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2013 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         labelSaveToSourceFolder->installEventFilter(this);
52         labelEnableSounds->installEventFilter(this);
53
54         connect(resetButton, SIGNAL(clicked()), this, SLOT(resetButtonPressed()));
55         connect(checkUse10BitEncoding, SIGNAL(toggled(bool)), this, SLOT(use10BitEncodingToggled(bool)));
56
57         m_preferences = preferences;
58 }
59
60 PreferencesDialog::~PreferencesDialog(void)
61 {
62 }
63
64 void PreferencesDialog::showEvent(QShowEvent *event)
65 {
66         if(event) QDialog::showEvent(event);
67         
68         UPDATE_CHECKBOX(checkRunNextJob, m_preferences->autoRunNextJob);
69         UPDATE_CHECKBOX(checkShutdownComputer, m_preferences->shutdownComputer);
70         UPDATE_CHECKBOX(checkUse64BitAvs2YUV, m_preferences->useAvisyth64Bit);
71         UPDATE_CHECKBOX(checkSaveLogFiles, m_preferences->saveLogFiles);
72         UPDATE_CHECKBOX(checkSaveToSourceFolder, m_preferences->saveToSourcePath);
73         UPDATE_CHECKBOX(checkEnableSounds, m_preferences->enableSounds);
74
75         checkUse10BitEncoding->blockSignals(true);
76         UPDATE_CHECKBOX(checkUse10BitEncoding, m_preferences->use10BitEncoding);
77         checkUse10BitEncoding->blockSignals(false);
78
79         spinBoxJobCount->setValue(m_preferences->maxRunningJobCount);
80         comboBoxPriority->setCurrentIndex(qBound(0U, m_preferences->processPriority, 2U));
81
82         checkUse64BitAvs2YUV->setEnabled(m_x64);
83         labelUse64BitAvs2YUV->setEnabled(m_x64);
84 }
85
86 bool PreferencesDialog::eventFilter(QObject *o, QEvent *e)
87 {
88         emulateMouseEvent(o, e, labelRunNextJob, checkRunNextJob);
89         emulateMouseEvent(o, e, labelShutdownComputer, checkShutdownComputer);
90         emulateMouseEvent(o, e, labelUse10BitEncoding, checkUse10BitEncoding);
91         emulateMouseEvent(o, e, labelUse64BitAvs2YUV, checkUse64BitAvs2YUV);
92         emulateMouseEvent(o, e, labelSaveLogFiles, checkSaveLogFiles);
93         emulateMouseEvent(o, e, labelSaveToSourceFolder, checkSaveToSourceFolder);
94         emulateMouseEvent(o, e, labelEnableSounds, checkEnableSounds);
95         return false;
96 }
97
98 void PreferencesDialog::emulateMouseEvent(QObject *object, QEvent *event, QWidget *source, QWidget *target)
99 {
100         if(object == source)
101         {
102                 if((event->type() == QEvent::MouseButtonPress) || (event->type() == QEvent::MouseButtonRelease))
103                 {
104                         if(QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event))
105                         {
106                                 qApp->postEvent(target, new QMouseEvent
107                                 (
108                                         event->type(),
109                                         qApp->widgetAt(mouseEvent->globalPos()) == source ? QPoint(1, 1) : QPoint(INT_MAX, INT_MAX),
110                                         Qt::LeftButton,
111                                         0, 0
112                                 ));
113                         }
114                 }
115         }
116 }
117
118 void PreferencesDialog::done(int n)
119 {
120         m_preferences->autoRunNextJob = checkRunNextJob->isChecked();
121         m_preferences->shutdownComputer = checkShutdownComputer->isChecked();
122         m_preferences->use10BitEncoding = checkUse10BitEncoding->isChecked();
123         m_preferences->useAvisyth64Bit = checkUse64BitAvs2YUV->isChecked();
124         m_preferences->saveLogFiles = checkSaveLogFiles->isChecked();
125         m_preferences->saveToSourcePath = checkSaveToSourceFolder->isChecked();
126         m_preferences->maxRunningJobCount = spinBoxJobCount->value();
127         m_preferences->processPriority = comboBoxPriority->currentIndex();
128         m_preferences->enableSounds = checkEnableSounds->isChecked();
129
130         savePreferences(m_preferences);
131         QDialog::done(n);
132 }
133
134 void PreferencesDialog::resetButtonPressed(void)
135 {
136         initPreferences(m_preferences);
137         showEvent(NULL);
138 }
139
140 void PreferencesDialog::use10BitEncodingToggled(bool checked)
141 {
142         if(checked)
143         {
144                 QString text;
145                 text += tr("<nobr>Please note that 10&minus;Bit H.264 streams are <b>not</b> currently supported by hardware (standalone) players!</nobr><br>");
146                 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>");
147                 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>");
148                 
149                 if(QMessageBox::warning(this, tr("10-Bit Encoding"), text, tr("Continue"), tr("Revert"), QString(), 1) != 0)
150                 {
151                         UPDATE_CHECKBOX(checkUse10BitEncoding, false);
152                 }
153         }
154 }
155
156 ///////////////////////////////////////////////////////////////////////////////
157 // Static Functions
158 ///////////////////////////////////////////////////////////////////////////////
159
160 void PreferencesDialog::initPreferences(Preferences *preferences)
161 {
162         memset(preferences, 0, sizeof(Preferences));
163
164         preferences->autoRunNextJob = true;
165         preferences->maxRunningJobCount = 1;
166         preferences->shutdownComputer = false;
167         preferences->use10BitEncoding = false;
168         preferences->useAvisyth64Bit = false;
169         preferences->saveLogFiles = false;
170         preferences->saveToSourcePath = false;
171         preferences->processPriority = X264_PRIORITY_BELOWNORMAL;
172         preferences->enableSounds = false;
173 }
174
175 void PreferencesDialog::loadPreferences(Preferences *preferences)
176 {
177         const QString appDir = x264_data_path();
178         QSettings settings(QString("%1/preferences.ini").arg(appDir), QSettings::IniFormat);
179
180         Preferences defaults;
181         initPreferences(&defaults);
182
183         settings.beginGroup("preferences");
184         preferences->autoRunNextJob = settings.value("auto_run_next_job", QVariant(defaults.autoRunNextJob)).toBool();
185         preferences->maxRunningJobCount = qBound(1U, settings.value("max_running_job_count", QVariant(defaults.maxRunningJobCount)).toUInt(), 16U);
186         preferences->shutdownComputer = settings.value("shutdown_computer_on_completion", QVariant(defaults.shutdownComputer)).toBool();
187         preferences->use10BitEncoding = settings.value("use_10bit_encoding", QVariant(defaults.use10BitEncoding)).toBool();
188         preferences->useAvisyth64Bit = settings.value("use_64bit_avisynth", QVariant(defaults.useAvisyth64Bit)).toBool();
189         preferences->saveLogFiles = settings.value("save_log_files", QVariant(defaults.saveLogFiles)).toBool();
190         preferences->saveToSourcePath = settings.value("save_to_source_path", QVariant(defaults.saveToSourcePath)).toBool();
191         preferences->processPriority = settings.value("process_priority", QVariant(defaults.processPriority)).toUInt();
192         preferences->enableSounds = settings.value("enable_sounds", QVariant(defaults.enableSounds)).toBool();
193 }
194
195 void PreferencesDialog::savePreferences(Preferences *preferences)
196 {
197         const QString appDir = x264_data_path();
198         QSettings settings(QString("%1/preferences.ini").arg(appDir), QSettings::IniFormat);
199
200         settings.beginGroup("preferences");
201         settings.setValue("auto_run_next_job", preferences->autoRunNextJob);
202         settings.setValue("shutdown_computer_on_completion", preferences->shutdownComputer);
203         settings.setValue("max_running_job_count", preferences->maxRunningJobCount);
204         settings.setValue("use_10bit_encoding", preferences->use10BitEncoding);
205         settings.setValue("use_64bit_avisynth", preferences->useAvisyth64Bit);
206         settings.setValue("save_log_files", preferences->saveLogFiles);
207         settings.setValue("save_to_source_path", preferences->saveToSourcePath);
208         settings.setValue("process_priority", preferences->processPriority);
209         settings.setValue("enable_sounds", preferences->enableSounds);
210         settings.sync();
211 }