OSDN Git Service

Fixed potential deadlock.
[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
30 #define UPDATE_CHECKBOX(CHKBOX, VALUE) \
31 { \
32         if((CHKBOX)->isChecked() != (VALUE)) (CHKBOX)->click(); \
33         if((CHKBOX)->isChecked() != (VALUE)) (CHKBOX)->setChecked(VALUE); \
34 }
35
36 PreferencesDialog::PreferencesDialog(QWidget *parent, Preferences *preferences, bool x64)
37 :
38         QDialog(parent),
39         m_x64(x64)
40 {
41         setupUi(this);
42         setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint));
43         setFixedSize(minimumSize());
44
45         labelRunNextJob->installEventFilter(this);
46         labelUse64BitAvs2YUV->installEventFilter(this);
47         labelShutdownComputer->installEventFilter(this);
48
49         connect(resetButton, SIGNAL(clicked()), this, SLOT(resetButtonPressed()));
50
51         m_preferences = preferences;
52 }
53
54 PreferencesDialog::~PreferencesDialog(void)
55 {
56 }
57
58 void PreferencesDialog::showEvent(QShowEvent *event)
59 {
60         if(event) QDialog::showEvent(event);
61         
62         UPDATE_CHECKBOX(checkRunNextJob, m_preferences->autoRunNextJob);
63         UPDATE_CHECKBOX(checkShutdownComputer, m_preferences->shutdownComputer);
64         UPDATE_CHECKBOX(checkUse64BitAvs2YUV, m_preferences->useAvisyth64Bit);
65         
66         spinBoxJobCount->setValue(m_preferences->maxRunningJobCount);
67         
68         checkUse64BitAvs2YUV->setEnabled(m_x64);
69         labelUse64BitAvs2YUV->setEnabled(m_x64);
70 }
71
72 bool PreferencesDialog::eventFilter(QObject *o, QEvent *e)
73 {
74         emulateMouseEvent(o, e, labelRunNextJob, checkRunNextJob);
75         emulateMouseEvent(o, e, labelShutdownComputer, checkShutdownComputer);
76         emulateMouseEvent(o, e, labelUse64BitAvs2YUV, checkUse64BitAvs2YUV);
77         return false;
78 }
79
80 void PreferencesDialog::emulateMouseEvent(QObject *object, QEvent *event, QWidget *source, QWidget *target)
81 {
82         if(object == source)
83         {
84                 if((event->type() == QEvent::MouseButtonPress) || (event->type() == QEvent::MouseButtonRelease))
85                 {
86                         if(QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event))
87                         {
88                                 qApp->postEvent(target, new QMouseEvent
89                                 (
90                                         event->type(),
91                                         qApp->widgetAt(mouseEvent->globalPos()) == source ? QPoint(1, 1) : QPoint(INT_MAX, INT_MAX),
92                                         Qt::LeftButton,
93                                         0, 0
94                                 ));
95                         }
96                 }
97         }
98 }
99
100 void PreferencesDialog::done(int n)
101 {
102         m_preferences->autoRunNextJob = checkRunNextJob->isChecked();
103         m_preferences->shutdownComputer = checkShutdownComputer->isChecked();
104         m_preferences->useAvisyth64Bit = checkUse64BitAvs2YUV->isChecked();
105         m_preferences->maxRunningJobCount = spinBoxJobCount->value();
106
107         savePreferences(m_preferences);
108         QDialog::done(n);
109 }
110
111 void PreferencesDialog::resetButtonPressed(void)
112 {
113         initPreferences(m_preferences);
114         showEvent(NULL);
115 }
116
117 ///////////////////////////////////////////////////////////////////////////////
118 // Static Functions
119 ///////////////////////////////////////////////////////////////////////////////
120
121 void PreferencesDialog::initPreferences(Preferences *preferences)
122 {
123         memset(preferences, 0, sizeof(Preferences));
124
125         preferences->autoRunNextJob = true;
126         preferences->maxRunningJobCount = 1;
127         preferences->shutdownComputer = false;
128         preferences->useAvisyth64Bit = false;
129 }
130
131 void PreferencesDialog::loadPreferences(Preferences *preferences)
132 {
133         const QString appDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
134         QSettings settings(QString("%1/preferences.ini").arg(appDir), QSettings::IniFormat);
135
136         Preferences defaults;
137         initPreferences(&defaults);
138
139         settings.beginGroup("preferences");
140         preferences->autoRunNextJob = settings.value("auto_run_next_job", QVariant(defaults.autoRunNextJob)).toBool();
141         preferences->maxRunningJobCount = qBound(1U, settings.value("max_running_job_count", QVariant(defaults.maxRunningJobCount)).toUInt(), 16U);
142         preferences->shutdownComputer = settings.value("shutdown_computer_on_completion", QVariant(defaults.shutdownComputer)).toBool();
143         preferences->useAvisyth64Bit = settings.value("use_64bit_avisynth", QVariant(defaults.useAvisyth64Bit)).toBool();
144 }
145
146 void PreferencesDialog::savePreferences(Preferences *preferences)
147 {
148         const QString appDir = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
149         QSettings settings(QString("%1/preferences.ini").arg(appDir), QSettings::IniFormat);
150
151         settings.beginGroup("preferences");
152         settings.setValue("auto_run_next_job", preferences->autoRunNextJob);
153         settings.setValue("shutdown_computer_on_completion", preferences->shutdownComputer);
154         settings.setValue("max_running_job_count", preferences->maxRunningJobCount);
155         settings.setValue("use_64bit_avisynth", preferences->useAvisyth64Bit);
156         settings.sync();
157 }
158