OSDN Git Service

Removed a lot of old cruft and use MUtils functions where possible.
[x264-launcher/x264-launcher.git] / src / win_preferences.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_preferences.h"
23 #include "UIC_win_preferences.h"
24
25 //Internal
26 #include "global.h"
27 #include "model_preferences.h"
28 #include "model_sysinfo.h"
29
30 //MUtils
31 #include <MUtils/GUI.h>
32
33 //Qt
34 #include <QSettings>
35 #include <QDesktopServices>
36 #include <QMouseEvent>
37 #include <QMessageBox>
38
39 static inline void UPDATE_CHECKBOX(QCheckBox *const chkbox, const bool value, const bool block = false)
40 {
41         if(block) { chkbox->blockSignals(true); }
42         if(chkbox->isChecked() != value) chkbox->click();
43         if(chkbox->isChecked() != value) chkbox->setChecked(value);
44         if(block) { chkbox->blockSignals(false); }
45 }
46
47 static inline void UPDATE_COMBOBOX(QComboBox *const cobox, const int value, const int defVal)
48 {
49         const int count = cobox->count();
50         for(int i = 0; i < count; i++)
51         {
52                 const int current = cobox->itemData(i).toInt();
53                 if((current == value) || (current == defVal))
54                 {
55                         cobox->setCurrentIndex(i);
56                         if((current == value)) break;
57                 }
58         }
59 }
60
61 PreferencesDialog::PreferencesDialog(QWidget *parent, PreferencesModel *preferences, const SysinfoModel *sysinfo)
62 :
63         QDialog(parent),
64         m_sysinfo(sysinfo),
65         ui(new Ui::PreferencesDialog())
66 {
67         ui->setupUi(this);
68         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
69         setFixedSize(minimumSize());
70         MUtils::GUI::enable_close_button(this, false);
71         
72         ui->comboBoxPriority->setItemData(0, QVariant::fromValue( 1)); //Above Normal
73         ui->comboBoxPriority->setItemData(1, QVariant::fromValue( 0)); //Normal
74         ui->comboBoxPriority->setItemData(2, QVariant::fromValue(-1)); //Below Normal
75         ui->comboBoxPriority->setItemData(3, QVariant::fromValue(-2)); //Idle
76
77         ui->labelRunNextJob->installEventFilter(this);
78         ui->labelUse64BitAvs2YUV->installEventFilter(this);
79         ui->labelShutdownComputer->installEventFilter(this);
80         ui->labelSaveLogFiles->installEventFilter(this);
81         ui->labelSaveToSourceFolder->installEventFilter(this);
82         ui->labelEnableSounds->installEventFilter(this);
83         ui->labelDisableWarnings->installEventFilter(this);
84         ui->labelNoUpdateReminder->installEventFilter(this);
85
86         ui->checkBoxDummy1->installEventFilter(this);
87         ui->checkBoxDummy2->installEventFilter(this);
88
89         connect(ui->resetButton, SIGNAL(clicked()), this, SLOT(resetButtonPressed()));
90         connect(ui->checkDisableWarnings, SIGNAL(toggled(bool)), this, SLOT(disableWarningsToggled(bool)));
91         
92         m_preferences = preferences;
93 }
94
95 PreferencesDialog::~PreferencesDialog(void)
96 {
97         delete ui;
98 }
99
100 void PreferencesDialog::showEvent(QShowEvent *event)
101 {
102         if(event) QDialog::showEvent(event);
103         
104         UPDATE_CHECKBOX(ui->checkRunNextJob,         m_preferences->getAutoRunNextJob());
105         UPDATE_CHECKBOX(ui->checkShutdownComputer,   m_preferences->getShutdownComputer());
106         UPDATE_CHECKBOX(ui->checkUse64BitAvs2YUV,    m_preferences->getUseAvisyth64Bit() && m_sysinfo->hasX64Support());
107         UPDATE_CHECKBOX(ui->checkSaveLogFiles,       m_preferences->getSaveLogFiles());
108         UPDATE_CHECKBOX(ui->checkSaveToSourceFolder, m_preferences->getSaveToSourcePath());
109         UPDATE_CHECKBOX(ui->checkEnableSounds,       m_preferences->getEnableSounds());
110         UPDATE_CHECKBOX(ui->checkNoUpdateReminder,   m_preferences->getNoUpdateReminder());
111         UPDATE_CHECKBOX(ui->checkDisableWarnings,    m_preferences->getDisableWarnings(), true);
112         
113         ui->spinBoxJobCount->setValue(m_preferences->getMaxRunningJobCount());
114         
115         UPDATE_COMBOBOX(ui->comboBoxPriority, qBound(-2, m_preferences->getProcessPriority(), 1), 0);
116         
117         ui->checkUse64BitAvs2YUV->setEnabled(m_sysinfo->hasX64Support());
118         ui->labelUse64BitAvs2YUV->setEnabled(m_sysinfo->hasX64Support());
119 }
120
121 bool PreferencesDialog::eventFilter(QObject *o, QEvent *e)
122 {
123         if(e->type() == QEvent::Paint)
124         {
125                 if(o == ui->checkBoxDummy1) return true;
126                 if(o == ui->checkBoxDummy2) return true;
127         }
128         else if((e->type() == QEvent::MouseButtonPress) || (e->type() == QEvent::MouseButtonRelease))
129         {
130                 emulateMouseEvent(o, e, ui->labelRunNextJob,         ui->checkRunNextJob);
131                 emulateMouseEvent(o, e, ui->labelShutdownComputer,   ui->checkShutdownComputer);
132                 emulateMouseEvent(o, e, ui->labelUse64BitAvs2YUV,    ui->checkUse64BitAvs2YUV);
133                 emulateMouseEvent(o, e, ui->labelSaveLogFiles,       ui->checkSaveLogFiles);
134                 emulateMouseEvent(o, e, ui->labelSaveToSourceFolder, ui->checkSaveToSourceFolder);
135                 emulateMouseEvent(o, e, ui->labelEnableSounds,       ui->checkEnableSounds);
136                 emulateMouseEvent(o, e, ui->labelDisableWarnings,    ui->checkDisableWarnings);
137                 emulateMouseEvent(o, e, ui->labelNoUpdateReminder,   ui->checkNoUpdateReminder);
138         }
139         return false;
140 }
141
142 void PreferencesDialog::emulateMouseEvent(QObject *object, QEvent *event, QWidget *source, QWidget *target)
143 {
144         if(object == source)
145         {
146                 if(QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event))
147                 {
148                         qApp->postEvent(target, new QMouseEvent
149                         (
150                                 event->type(),
151                                 (qApp->widgetAt(mouseEvent->globalPos()) == source) ? QPoint(1, 1) : QPoint(INT_MAX, INT_MAX),
152                                 Qt::LeftButton,
153                                 0, 0
154                         ));
155                 }
156         }
157 }
158
159 void PreferencesDialog::done(int n)
160 {
161         m_preferences->setAutoRunNextJob(ui->checkRunNextJob->isChecked());
162         m_preferences->setShutdownComputer(ui->checkShutdownComputer->isChecked());
163         m_preferences->setUseAvisyth64Bit(ui->checkUse64BitAvs2YUV->isChecked());
164         m_preferences->setSaveLogFiles(ui->checkSaveLogFiles->isChecked());
165         m_preferences->setSaveToSourcePath(ui->checkSaveToSourceFolder->isChecked());
166         m_preferences->setMaxRunningJobCount(ui->spinBoxJobCount->value());
167         m_preferences->setProcessPriority(ui->comboBoxPriority->itemData(ui->comboBoxPriority->currentIndex()).toInt());
168         m_preferences->setEnableSounds(ui->checkEnableSounds->isChecked());
169         m_preferences->setDisableWarnings(ui->checkDisableWarnings->isChecked());
170         m_preferences->setNoUpdateReminder(ui->checkNoUpdateReminder->isChecked());
171
172         PreferencesModel::savePreferences(m_preferences);
173         QDialog::done(n);
174 }
175
176 void PreferencesDialog::resetButtonPressed(void)
177 {
178         PreferencesModel::initPreferences(m_preferences);
179         showEvent(NULL);
180 }
181
182 //void PreferencesDialog::use10BitEncodingToggled(bool checked)
183 //{
184 //      if(checked)
185 //      {
186 //              QString text;
187 //              text += QString("<nobr>%1</nobr><br>").arg(tr("Please note that 10&minus;Bit H.264 streams are <b>not</b> currently supported by hardware (standalone) players!"));
188 //              text += QString("<nobr>%1</nobr><br>").arg(tr("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."));
189 //              text += QString("<nobr>%1</nobr><br>").arg(tr("Also be aware that hardware&minus;acceleration (CUDA, DXVA, etc) usually will <b>not</b> work with 10&minus;Bit H.264 streams."));
190 //              
191 //              if(QMessageBox::warning(this, tr("10-Bit Encoding"), text.replace("-", "&minus;"), tr("Continue"), tr("Revert"), QString(), 1) != 0)
192 //              {
193 //                      UPDATE_CHECKBOX(ui->checkUse10BitEncoding, false, true);
194 //              }
195 //      }
196 //}
197
198 void PreferencesDialog::disableWarningsToggled(bool checked)
199 {
200         if(checked)
201         {
202                 QString text;
203                 text += QString("<nobr>%1</nobr><br>").arg(tr("Please note that Avisynth and/or VapourSynth support might be unavailable <b>without</b> any notice!"));
204                 text += QString("<nobr>%1</nobr><br>").arg(tr("Also note that the CLI option <tt>--console</tt> may be used to get more diagnostic infomation."));
205
206                 if(QMessageBox::warning(this, tr("Avisynth/VapourSynth Warnings"), text.replace("-", "&minus;"), tr("Continue"), tr("Revert"), QString(), 1) != 0)
207                 {
208                         UPDATE_CHECKBOX(ui->checkDisableWarnings, false, true);
209                 }
210         }
211 }