]> Git Repo - VerusCoin.git/blob - src/qt/optionsdialog.cpp
qt: add license header to source files
[VerusCoin.git] / src / qt / optionsdialog.cpp
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #if defined(HAVE_CONFIG_H)
6 #include "bitcoin-config.h"
7 #endif
8
9 #include "optionsdialog.h"
10 #include "ui_optionsdialog.h"
11
12 #include "bitcoinunits.h"
13 #include "monitoreddatamapper.h"
14 #include "netbase.h"
15 #include "optionsmodel.h"
16
17 #include <QDir>
18 #include <QIntValidator>
19 #include <QLocale>
20 #include <QMessageBox>
21
22 OptionsDialog::OptionsDialog(QWidget *parent) :
23     QDialog(parent),
24     ui(new Ui::OptionsDialog),
25     model(0),
26     mapper(0),
27     fRestartWarningDisplayed_Proxy(false),
28     fRestartWarningDisplayed_Lang(false),
29     fProxyIpValid(true)
30 {
31     ui->setupUi(this);
32
33     /* Network elements init */
34 #ifndef USE_UPNP
35     ui->mapPortUpnp->setEnabled(false);
36 #endif
37
38     ui->proxyIp->setEnabled(false);
39     ui->proxyPort->setEnabled(false);
40     ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
41
42     ui->socksVersion->setEnabled(false);
43     ui->socksVersion->addItem("5", 5);
44     ui->socksVersion->addItem("4", 4);
45     ui->socksVersion->setCurrentIndex(0);
46
47     connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
48     connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
49     connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->socksVersion, SLOT(setEnabled(bool)));
50     connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning_Proxy()));
51
52     ui->proxyIp->installEventFilter(this);
53
54     /* Window elements init */
55 #ifdef Q_OS_MAC
56     /* remove Window tab on Mac */
57     ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
58 #endif
59
60     /* Display elements init */
61     QDir translations(":translations");
62     ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
63     foreach(const QString &langStr, translations.entryList())
64     {
65         QLocale locale(langStr);
66
67         /** check if the locale name consists of 2 parts (language_country) */
68         if(langStr.contains("_"))
69         {
70 #if QT_VERSION >= 0x040800
71             /** display language strings as "native language - native country (locale name)", e.g. "Deutsch - Deutschland (de)" */
72             ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
73 #else
74             /** display language strings as "language - country (locale name)", e.g. "German - Germany (de)" */
75             ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
76 #endif
77         }
78         else
79         {
80 #if QT_VERSION >= 0x040800
81             /** display language strings as "native language (locale name)", e.g. "Deutsch (de)" */
82             ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
83 #else
84             /** display language strings as "language (locale name)", e.g. "German (de)" */
85             ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
86 #endif
87         }
88     }
89
90     ui->unit->setModel(new BitcoinUnits(this));
91
92     /* Widget-to-option mapper */
93     mapper = new MonitoredDataMapper(this);
94     mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
95     mapper->setOrientation(Qt::Vertical);
96
97     /* enable apply button when data modified */
98     connect(mapper, SIGNAL(viewModified()), this, SLOT(enableApplyButton()));
99     /* disable apply button when new data loaded */
100     connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(disableApplyButton()));
101     /* setup/change UI elements when proxy IP is invalid/valid */
102     connect(this, SIGNAL(proxyIpValid(QValidatedLineEdit *, bool)), this, SLOT(handleProxyIpValid(QValidatedLineEdit *, bool)));
103 }
104
105 OptionsDialog::~OptionsDialog()
106 {
107     delete ui;
108 }
109
110 void OptionsDialog::setModel(OptionsModel *model)
111 {
112     this->model = model;
113
114     if(model)
115     {
116         connect(model, SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
117
118         mapper->setModel(model);
119         setMapper();
120         mapper->toFirst();
121     }
122
123     /* update the display unit, to not use the default ("BTC") */
124     updateDisplayUnit();
125
126     /* warn only when language selection changes by user action (placed here so init via mapper doesn't trigger this) */
127     connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning_Lang()));
128
129     /* disable apply button after settings are loaded as there is nothing to save */
130     disableApplyButton();
131 }
132
133 void OptionsDialog::setMapper()
134 {
135     /* Main */
136     mapper->addMapping(ui->transactionFee, OptionsModel::Fee);
137     mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
138
139     /* Network */
140     mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
141
142     mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
143     mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
144     mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
145     mapper->addMapping(ui->socksVersion, OptionsModel::ProxySocksVersion);
146
147     /* Window */
148 #ifndef Q_OS_MAC
149     mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
150     mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
151 #endif
152
153     /* Display */
154     mapper->addMapping(ui->lang, OptionsModel::Language);
155     mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
156     mapper->addMapping(ui->displayAddresses, OptionsModel::DisplayAddresses);
157 }
158
159 void OptionsDialog::enableApplyButton()
160 {
161     ui->applyButton->setEnabled(true);
162 }
163
164 void OptionsDialog::disableApplyButton()
165 {
166     ui->applyButton->setEnabled(false);
167 }
168
169 void OptionsDialog::enableSaveButtons()
170 {
171     /* prevent enabling of the save buttons when data modified, if there is an invalid proxy address present */
172     if(fProxyIpValid)
173         setSaveButtonState(true);
174 }
175
176 void OptionsDialog::disableSaveButtons()
177 {
178     setSaveButtonState(false);
179 }
180
181 void OptionsDialog::setSaveButtonState(bool fState)
182 {
183     ui->applyButton->setEnabled(fState);
184     ui->okButton->setEnabled(fState);
185 }
186
187 void OptionsDialog::on_resetButton_clicked()
188 {
189     if(model)
190     {
191         // confirmation dialog
192         QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
193             tr("Some settings may require a client restart to take effect.") + "<br><br>" + tr("Do you want to proceed?"),
194             QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
195
196         if(btnRetVal == QMessageBox::Cancel)
197             return;
198
199         disableApplyButton();
200
201         /* disable restart warning messages display */
202         fRestartWarningDisplayed_Lang = fRestartWarningDisplayed_Proxy = true;
203
204         /* reset all options and save the default values (QSettings) */
205         model->Reset();
206         mapper->toFirst();
207         mapper->submit();
208
209         /* re-enable restart warning messages display */
210         fRestartWarningDisplayed_Lang = fRestartWarningDisplayed_Proxy = false;
211     }
212 }
213
214 void OptionsDialog::on_okButton_clicked()
215 {
216     mapper->submit();
217     accept();
218 }
219
220 void OptionsDialog::on_cancelButton_clicked()
221 {
222     reject();
223 }
224
225 void OptionsDialog::on_applyButton_clicked()
226 {
227     mapper->submit();
228     disableApplyButton();
229 }
230
231 void OptionsDialog::showRestartWarning_Proxy()
232 {
233     if(!fRestartWarningDisplayed_Proxy)
234     {
235         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting Bitcoin."), QMessageBox::Ok);
236         fRestartWarningDisplayed_Proxy = true;
237     }
238 }
239
240 void OptionsDialog::showRestartWarning_Lang()
241 {
242     if(!fRestartWarningDisplayed_Lang)
243     {
244         QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting Bitcoin."), QMessageBox::Ok);
245         fRestartWarningDisplayed_Lang = true;
246     }
247 }
248
249 void OptionsDialog::updateDisplayUnit()
250 {
251     if(model)
252     {
253         /* Update transactionFee with the current unit */
254         ui->transactionFee->setDisplayUnit(model->getDisplayUnit());
255     }
256 }
257
258 void OptionsDialog::handleProxyIpValid(QValidatedLineEdit *object, bool fState)
259 {
260     // this is used in a check before re-enabling the save buttons
261     fProxyIpValid = fState;
262
263     if(fProxyIpValid)
264     {
265         enableSaveButtons();
266         ui->statusLabel->clear();
267     }
268     else
269     {
270         disableSaveButtons();
271         object->setValid(fProxyIpValid);
272         ui->statusLabel->setStyleSheet("QLabel { color: red; }");
273         ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
274     }
275 }
276
277 bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
278 {
279     if(event->type() == QEvent::FocusOut)
280     {
281         if(object == ui->proxyIp)
282         {
283             CService addr;
284             /* Check proxyIp for a valid IPv4/IPv6 address and emit the proxyIpValid signal */
285             emit proxyIpValid(ui->proxyIp, LookupNumeric(ui->proxyIp->text().toStdString().c_str(), addr));
286         }
287     }
288     return QDialog::eventFilter(object, event);
289 }
This page took 0.04499 seconds and 4 git commands to generate.