Bitcoin Core 22.99.0
P2P Digital Currency
optionsdialog.cpp
Go to the documentation of this file.
1// Copyright (c) 2011-2020 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#if defined(HAVE_CONFIG_H)
7#endif
8
9#include <qt/optionsdialog.h>
11
12#include <qt/bitcoinunits.h>
13#include <qt/guiconstants.h>
14#include <qt/guiutil.h>
15#include <qt/optionsmodel.h>
16
17#include <interfaces/node.h>
18#include <validation.h> // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS
19#include <netbase.h>
20#include <txdb.h> // for -dbcache defaults
21
22#include <QDataWidgetMapper>
23#include <QDir>
24#include <QIntValidator>
25#include <QLocale>
26#include <QMessageBox>
27#include <QSettings>
28#include <QSystemTrayIcon>
29#include <QTimer>
30
31OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
32 QDialog(parent, GUIUtil::dialog_flags),
33 ui(new Ui::OptionsDialog),
34 model(nullptr),
35 mapper(nullptr)
36{
37 ui->setupUi(this);
38
39 /* Main elements init */
40 ui->databaseCache->setMinimum(nMinDbCache);
41 ui->databaseCache->setMaximum(nMaxDbCache);
42 ui->threadsScriptVerif->setMinimum(-GetNumCores());
44 ui->pruneWarning->setVisible(false);
45 ui->pruneWarning->setStyleSheet("QLabel { color: red; }");
46
47 ui->pruneSize->setEnabled(false);
48 connect(ui->prune, &QPushButton::toggled, ui->pruneSize, &QWidget::setEnabled);
49
50 /* Network elements init */
51#ifndef USE_UPNP
52 ui->mapPortUpnp->setEnabled(false);
53#endif
54#ifndef USE_NATPMP
55 ui->mapPortNatpmp->setEnabled(false);
56#endif
57 connect(this, &QDialog::accepted, [this](){
58 QSettings settings;
59 model->node().mapPort(settings.value("fUseUPnP").toBool(), settings.value("fUseNatpmp").toBool());
60 });
61
62 ui->proxyIp->setEnabled(false);
63 ui->proxyPort->setEnabled(false);
64 ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
65
66 ui->proxyIpTor->setEnabled(false);
67 ui->proxyPortTor->setEnabled(false);
68 ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
69
70 connect(ui->connectSocks, &QPushButton::toggled, ui->proxyIp, &QWidget::setEnabled);
71 connect(ui->connectSocks, &QPushButton::toggled, ui->proxyPort, &QWidget::setEnabled);
72 connect(ui->connectSocks, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
73
74 connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyIpTor, &QWidget::setEnabled);
75 connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyPortTor, &QWidget::setEnabled);
76 connect(ui->connectSocksTor, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);
77
78 /* Window elements init */
79#ifdef Q_OS_MAC
80 /* remove Window tab on Mac */
81 ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
82 /* hide launch at startup option on macOS */
83 ui->bitcoinAtStartup->setVisible(false);
86#endif
87
88 /* remove Wallet tab and 3rd party-URL textbox in case of -disablewallet */
89 if (!enableWallet) {
90 ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
91 ui->thirdPartyTxUrlsLabel->setVisible(false);
92 ui->thirdPartyTxUrls->setVisible(false);
93 }
94
95#ifndef ENABLE_EXTERNAL_SIGNER
96 //: "External signing" means using devices such as hardware wallets.
97 ui->externalSignerPath->setToolTip(tr("Compiled without external signing support (required for external signing)"));
98 ui->externalSignerPath->setEnabled(false);
99#endif
100 /* Display elements init */
101 QDir translations(":translations");
102
103 ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(PACKAGE_NAME));
104 ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(PACKAGE_NAME));
105
106 ui->openBitcoinConfButton->setToolTip(ui->openBitcoinConfButton->toolTip().arg(PACKAGE_NAME));
107
108 ui->lang->setToolTip(ui->lang->toolTip().arg(PACKAGE_NAME));
109 ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
110 for (const QString &langStr : translations.entryList())
111 {
112 QLocale locale(langStr);
113
115 if(langStr.contains("_"))
116 {
118 ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
119 }
120 else
121 {
123 ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
124 }
125 }
126 ui->unit->setModel(new BitcoinUnits(this));
127
128 /* Widget-to-option mapper */
129 mapper = new QDataWidgetMapper(this);
130 mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
131 mapper->setOrientation(Qt::Vertical);
132
134 connect(delegate, &GUIUtil::ItemDelegate::keyEscapePressed, this, &OptionsDialog::reject);
135 mapper->setItemDelegate(delegate);
136
137 /* setup/change UI elements when proxy IPs are invalid/valid */
142 connect(ui->proxyPort, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
143 connect(ui->proxyPortTor, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
144
145 if (!QSystemTrayIcon::isSystemTrayAvailable()) {
146 ui->showTrayIcon->setChecked(false);
147 ui->showTrayIcon->setEnabled(false);
148 ui->minimizeToTray->setChecked(false);
149 ui->minimizeToTray->setEnabled(false);
150 }
151
152 QFont embedded_font{GUIUtil::fixedPitchFont(true)};
153 ui->embeddedFont_radioButton->setText(ui->embeddedFont_radioButton->text().arg(QFontInfo(embedded_font).family()));
154 embedded_font.setWeight(QFont::Bold);
155 ui->embeddedFont_label_1->setFont(embedded_font);
156 ui->embeddedFont_label_9->setFont(embedded_font);
157
158 QFont system_font{GUIUtil::fixedPitchFont(false)};
159 ui->systemFont_radioButton->setText(ui->systemFont_radioButton->text().arg(QFontInfo(system_font).family()));
160 system_font.setWeight(QFont::Bold);
161 ui->systemFont_label_1->setFont(system_font);
162 ui->systemFont_label_9->setFont(system_font);
163 // Checking the embeddedFont_radioButton automatically unchecks the systemFont_radioButton.
164 ui->systemFont_radioButton->setChecked(true);
165
167}
168
170{
171 delete ui;
172}
173
175{
176 this->model = _model;
177
178 if(_model)
179 {
180 /* check if client restart is needed and show persistent message */
181 if (_model->isRestartRequired())
182 showRestartWarning(true);
183
184 // Prune values are in GB to be consistent with intro.cpp
185 static constexpr uint64_t nMinDiskSpace = (MIN_DISK_SPACE_FOR_BLOCK_FILES / GB_BYTES) + (MIN_DISK_SPACE_FOR_BLOCK_FILES % GB_BYTES) ? 1 : 0;
186 ui->pruneSize->setRange(nMinDiskSpace, std::numeric_limits<int>::max());
187
188 QString strLabel = _model->getOverriddenByCommandLine();
189 if (strLabel.isEmpty())
190 strLabel = tr("none");
191 ui->overriddenByCommandLineLabel->setText(strLabel);
192
193 mapper->setModel(_model);
194 setMapper();
195 mapper->toFirst();
196
198 }
199
200 /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
201
202 /* Main */
203 connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
204 connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::togglePruneWarning);
205 connect(ui->pruneSize, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
206 connect(ui->databaseCache, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
207 connect(ui->externalSignerPath, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
208 connect(ui->threadsScriptVerif, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
209 /* Wallet */
210 connect(ui->spendZeroConfChange, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
211 /* Network */
212 connect(ui->allowIncoming, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
213 connect(ui->enableServer, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
214 connect(ui->connectSocks, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
215 connect(ui->connectSocksTor, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
216 /* Display */
217 connect(ui->lang, qOverload<>(&QValueComboBox::valueChanged), [this]{ showRestartWarning(); });
218 connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
219}
220
222{
223 QWidget *tab_widget = nullptr;
224 if (tab == OptionsDialog::Tab::TAB_NETWORK) tab_widget = ui->tabNetwork;
225 if (tab == OptionsDialog::Tab::TAB_MAIN) tab_widget = ui->tabMain;
226 if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
227 ui->tabWidget->setCurrentWidget(tab_widget);
228 }
229}
230
232{
233 /* Main */
237 mapper->addMapping(ui->prune, OptionsModel::Prune);
239
240 /* Wallet */
245
246 /* Network */
251
253 mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
255
259
260 /* Window */
261#ifndef Q_OS_MAC
262 if (QSystemTrayIcon::isSystemTrayAvailable()) {
265 }
267#endif
268
269 /* Display */
270 mapper->addMapping(ui->lang, OptionsModel::Language);
274}
275
277{
278 ui->okButton->setEnabled(fState);
279}
280
282{
283 if(model)
284 {
285 // confirmation dialog
286 QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
287 tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
288 QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
289
290 if(btnRetVal == QMessageBox::Cancel)
291 return;
292
293 /* reset all options and close GUI */
294 model->Reset();
295 close();
296 Q_EMIT quitOnReset();
297 }
298}
299
301{
302 QMessageBox config_msgbox(this);
303 config_msgbox.setIcon(QMessageBox::Information);
304 //: Window title text of pop-up box that allows opening up of configuration file.
305 config_msgbox.setWindowTitle(tr("Configuration options"));
306 /*: Explanatory text about the priority order of instructions considered by client.
307 The order from high to low being: command-line, configuration file, GUI settings. */
308 config_msgbox.setText(tr("The configuration file is used to specify advanced user options which override GUI settings. "
309 "Additionally, any command-line options will override this configuration file."));
310
311 QPushButton* open_button = config_msgbox.addButton(tr("Continue"), QMessageBox::ActionRole);
312 config_msgbox.addButton(tr("Cancel"), QMessageBox::RejectRole);
313 open_button->setDefault(true);
314
315 config_msgbox.exec();
316
317 if (config_msgbox.clickedButton() != open_button) return;
318
319 /* show an error if there was some problem opening the file */
321 QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
322}
323
325{
326 mapper->submit();
327 accept();
329}
330
332{
333 reject();
334}
335
337{
338 if (state == Qt::Checked) {
339 ui->minimizeToTray->setEnabled(true);
340 } else {
341 ui->minimizeToTray->setChecked(false);
342 ui->minimizeToTray->setEnabled(false);
343 }
344}
345
347{
348 ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
349}
350
352{
353 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
354
355 if(fPersistent)
356 {
357 ui->statusLabel->setText(tr("Client restart required to activate changes."));
358 }
359 else
360 {
361 ui->statusLabel->setText(tr("This change would require a client restart."));
362 // clear non-persistent status label after 10 seconds
363 // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
364 QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
365 }
366}
367
369{
370 ui->statusLabel->clear();
371 if (model && model->isRestartRequired()) {
372 showRestartWarning(true);
373 }
374}
375
377{
378 QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
379 QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
380 if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
381 {
382 setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
384 }
385 else
386 {
387 setOkButtonState(false);
388 ui->statusLabel->setStyleSheet("QLabel { color: red; }");
389 ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
390 }
391}
392
394{
395 proxyType proxy;
396 std::string strProxy;
397 QString strDefaultProxyGUI;
398
399 model->node().getProxy(NET_IPV4, proxy);
400 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
401 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
402 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
403
404 model->node().getProxy(NET_IPV6, proxy);
405 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
406 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
407 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
408
409 model->node().getProxy(NET_ONION, proxy);
410 strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
411 strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
412 (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
413}
414
416QValidator(parent)
417{
418}
419
420QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
421{
422 Q_UNUSED(pos);
423 // Validate the proxy
424 CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
425 proxyType addrProxy = proxyType(serv, true);
426 if (addrProxy.IsValid())
427 return QValidator::Acceptable;
428
429 return QValidator::Invalid;
430}
#define PACKAGE_NAME
Bitcoin unit definitions.
Definition: bitcoinunits.h:32
std::string ToStringIP() const
Definition: netaddress.cpp:608
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
std::string ToStringPort() const
Preferences dialog.
Definition: optionsdialog.h:36
void setModel(OptionsModel *model)
OptionsModel * model
Definition: optionsdialog.h:75
void setCurrentTab(OptionsDialog::Tab tab)
void on_showTrayIcon_stateChanged(int state)
void on_okButton_clicked()
void on_openBitcoinConfButton_clicked()
void updateDefaultProxyNets()
void updateProxyValidationState()
void togglePruneWarning(bool enabled)
void showRestartWarning(bool fPersistent=false)
void on_resetButton_clicked()
Ui::OptionsDialog * ui
Definition: optionsdialog.h:74
OptionsDialog(QWidget *parent, bool enableWallet)
QDataWidgetMapper * mapper
Definition: optionsdialog.h:76
void clearStatusLabel()
void on_cancelButton_clicked()
void setOkButtonState(bool fState)
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:39
bool isRestartRequired() const
interfaces::Node & node() const
Definition: optionsmodel.h:103
@ UseEmbeddedMonospacedFont
Definition: optionsmodel.h:61
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:93
Proxy address widget validator, checks for a valid proxy address.
Definition: optionsdialog.h:25
ProxyAddressValidator(QObject *parent)
State validate(QString &input, int &pos) const override
Line edit that can be marked as "invalid" to show input validation feedback.
void validationDidChange(QValidatedLineEdit *validatedLineEdit)
void setEnabled(bool enabled)
void setCheckValidator(const QValidator *v)
QValueComboBox * lang
QRadioButton * systemFont_radioButton
QLabel * overriddenByCommandLineLabel
QLineEdit * proxyPortTor
QSpinBox * threadsScriptVerif
QSpinBox * pruneSize
QCheckBox * connectSocksTor
QSpinBox * databaseCache
QCheckBox * minimizeToTray
void setupUi(QDialog *OptionsDialog)
QCheckBox * bitcoinAtStartup
QVBoxLayout * verticalLayout_Main
QLineEdit * externalSignerPath
QCheckBox * subFeeFromAmount
QPushButton * openBitcoinConfButton
QCheckBox * coinControlFeatures
QPushButton * okButton
QCheckBox * mapPortUpnp
QValidatedLineEdit * proxyIpTor
QTabWidget * tabWidget
QCheckBox * allowIncoming
QLineEdit * thirdPartyTxUrls
QValueComboBox * unit
QLineEdit * proxyPort
QLabel * embeddedFont_label_1
QSpacerItem * horizontalSpacer_0_Main
QLabel * embeddedFont_label_9
QCheckBox * spendZeroConfChange
QCheckBox * mapPortNatpmp
QCheckBox * proxyReachIPv4
QCheckBox * proxyReachIPv6
QLabel * systemFont_label_1
QLabel * systemFont_label_9
QCheckBox * enableServer
QCheckBox * proxyReachTor
QCheckBox * showTrayIcon
QRadioButton * embeddedFont_radioButton
QValidatedLineEdit * proxyIp
QCheckBox * minimizeOnClose
QCheckBox * connectSocks
QLabel * thirdPartyTxUrlsLabel
virtual bool getProxy(Network net, proxyType &proxy_info)=0
Get proxy.
bool IsValid() const
Definition: netbase.h:54
CService proxy
Definition: netbase.h:56
if(na.IsAddrV1Compatible())
static constexpr uint64_t GB_BYTES
Definition: guiconstants.h:53
Utility functions used by the Bitcoin Qt UI.
Definition: bitcoingui.h:59
bool openBitcoinConf()
Definition: guiutil.cpp:423
QFont fixedPitchFont(bool use_embedded_font)
Definition: guiutil.cpp:88
void handleCloseWindowShortcut(QWidget *w)
Definition: guiutil.cpp:409
constexpr auto dialog_flags
Definition: guiutil.h:60
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
CService LookupNumeric(const std::string &name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
Resolve a service string with a numeric IP to its first corresponding service.
Definition: netbase.cpp:230
static constexpr uint16_t DEFAULT_GUI_PROXY_PORT
Definition: optionsmodel.h:20
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:33
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:31
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:1345
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES
Definition: validation.h:90
static const int MAX_SCRIPTCHECK_THREADS
Maximum number of dedicated script-checking threads allowed.
Definition: validation.h:66