Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

optionsdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 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)
6 #include <config/dash-config.h>
7 #endif
8 
9 #include <qt/optionsdialog.h>
10 #include <qt/forms/ui_optionsdialog.h>
11 
12 #include <qt/appearancewidget.h>
13 #include <qt/bitcoinunits.h>
14 #include <qt/guiutil.h>
15 #include <qt/optionsmodel.h>
16 
17 #include <validation.h> // for DEFAULT_SCRIPTCHECK_THREADS and MAX_SCRIPTCHECK_THREADS
18 #include <netbase.h>
19 #include <txdb.h> // for -dbcache defaults
20 
21 #ifdef ENABLE_WALLET
23 #endif // ENABLE_WALLET
24 
25 #include <QDataWidgetMapper>
26 #include <QDir>
27 #include <QIntValidator>
28 #include <QLocale>
29 #include <QMessageBox>
30 #include <QSettings>
31 #include <QShowEvent>
32 #include <QTimer>
33 
34 OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) :
35  QDialog(parent),
36  ui(new Ui::OptionsDialog),
37  model(0),
38  mapper(0)
39 {
40  ui->setupUi(this);
41 
42  GUIUtil::setFont({ui->statusLabel}, GUIUtil::FontWeight::Bold, 16);
43 
45 
47 
48 #ifdef Q_OS_MAC
49  /* Hide some options on Mac */
50  ui->hideTrayIcon->hide();
51  ui->minimizeToTray->hide();
52  ui->minimizeOnClose->hide();
53 #endif
54 
55  /* Main elements init */
56  ui->databaseCache->setMinimum(nMinDbCache);
57  ui->databaseCache->setMaximum(nMaxDbCache);
58  ui->threadsScriptVerif->setMinimum(-GetNumCores());
59  ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
60 
61  /* Network elements init */
62 #ifndef USE_UPNP
63  ui->mapPortUpnp->setEnabled(false);
64 #endif
65 
66  ui->proxyIp->setEnabled(false);
67  ui->proxyPort->setEnabled(false);
68  ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
69 
70  ui->proxyIpTor->setEnabled(false);
71  ui->proxyPortTor->setEnabled(false);
72  ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));
73 
74  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
75  connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
76  connect(ui->connectSocks, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
77 
78  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyIpTor, SLOT(setEnabled(bool)));
79  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), ui->proxyPortTor, SLOT(setEnabled(bool)));
80  connect(ui->connectSocksTor, SIGNAL(toggled(bool)), this, SLOT(updateProxyValidationState()));
81 
82  pageButtons.addButton(ui->btnMain, pageButtons.buttons().size());
83  /* Remove Wallet/PrivateSend tabs in case of -disablewallet */
84  if (!enableWallet) {
85  ui->stackedWidgetOptions->removeWidget(ui->pageWallet);
86  ui->btnWallet->hide();
87  ui->stackedWidgetOptions->removeWidget(ui->pagePrivateSend);
88  ui->btnPrivateSend->hide();
89  } else {
90  pageButtons.addButton(ui->btnWallet, pageButtons.buttons().size());
91  pageButtons.addButton(ui->btnPrivateSend, pageButtons.buttons().size());
92  }
93  pageButtons.addButton(ui->btnNetwork, pageButtons.buttons().size());
94  pageButtons.addButton(ui->btnDisplay, pageButtons.buttons().size());
95  pageButtons.addButton(ui->btnAppearance, pageButtons.buttons().size());
96 
97  connect(&pageButtons, SIGNAL(buttonClicked(int)), this, SLOT(showPage(int)));
98 
99  showPage(0);
100 
101  /* Display elements init */
102 
103  /* Number of displayed decimal digits selector */
104  QString digits;
105  for(int index = 2; index <=8; index++){
106  digits.setNum(index);
107  ui->digits->addItem(digits, digits);
108  }
109 
110  /* Language selector */
111  QDir translations(":translations");
112 
113  ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(tr(PACKAGE_NAME)));
114  ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(tr(PACKAGE_NAME)));
115 
116  ui->lang->setToolTip(ui->lang->toolTip().arg(tr(PACKAGE_NAME)));
117  ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
118  for (const QString &langStr : translations.entryList())
119  {
120  QLocale locale(langStr);
121 
123  if(langStr.contains("_"))
124  {
125 #if QT_VERSION >= 0x040800
126 
127  ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
128 #else
129 
130  ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
131 #endif
132  }
133  else
134  {
135 #if QT_VERSION >= 0x040800
136 
137  ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
138 #else
139 
140  ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
141 #endif
142  }
143  }
144 #if QT_VERSION >= 0x040700
145  ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
146 #endif
147 
148  ui->unit->setModel(new BitcoinUnits(this));
149 
150  /* Widget-to-option mapper */
151  mapper = new QDataWidgetMapper(this);
152  mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
153  mapper->setOrientation(Qt::Vertical);
154 
155  /* setup/change UI elements when proxy IPs are invalid/valid */
156  ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
157  ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
158  connect(ui->proxyIp, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
159  connect(ui->proxyIpTor, SIGNAL(validationDidChange(QValidatedLineEdit *)), this, SLOT(updateProxyValidationState()));
160  connect(ui->proxyPort, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
161  connect(ui->proxyPortTor, SIGNAL(textChanged(const QString&)), this, SLOT(updateProxyValidationState()));
162 
163  QVBoxLayout* appearanceLayout = new QVBoxLayout();
164  appearanceLayout->setContentsMargins(0, 0, 0, 0);
165  appearance = new AppearanceWidget(ui->widgetAppearance);
166  appearanceLayout->addWidget(appearance);
167  ui->widgetAppearance->setLayout(appearanceLayout);
168 
170  updateWidth();
171  Q_EMIT appearanceChanged();
172  });
173 
175 
176  // Store the current PrivateSend enabled state to recover it if it gets changed but the dialog gets not accepted but declined.
177 #ifdef ENABLE_WALLET
179  connect(this, &OptionsDialog::rejected, [=]() {
181  ui->privateSendEnabled->click();
182  }
183  });
184 #endif
185 }
186 
188 {
189  delete ui;
190 }
191 
193 {
194  this->model = _model;
195 
196  if(_model)
197  {
198  /* check if client restart is needed and show persistent message */
199  if (_model->isRestartRequired())
200  showRestartWarning(true);
201 
202  QString strLabel = _model->getOverriddenByCommandLine();
203  if (strLabel.isEmpty()) {
204  ui->frame->setHidden(true);
205  } else {
206  ui->overriddenByCommandLineLabel->setText(strLabel);
207  }
208 
209 
210 #ifdef ENABLE_WALLET
211  // If -enableprivatesend was passed in on the command line, set the checkbox
212  // to the value given via commandline and disable it (make it unclickable).
213  if (strLabel.contains("-enableprivatesend")) {
214  bool fEnabled = privateSendClient.fEnablePrivateSend;
215  ui->privateSendEnabled->setChecked(fEnabled);
216  ui->privateSendEnabled->setEnabled(false);
217  }
218 #endif
219 
220  mapper->setModel(_model);
221  setMapper();
222  mapper->toFirst();
223 
224  appearance->setModel(_model);
225 
227  }
228 
229  /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
230 
231  /* Main */
232  connect(ui->databaseCache, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
233  connect(ui->threadsScriptVerif, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
234  /* Wallet */
235  connect(ui->showMasternodesTab, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
236  connect(ui->spendZeroConfChange, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
237  /* Network */
238  connect(ui->allowIncoming, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
239  connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
240  connect(ui->connectSocksTor, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
241  /* Display */
242  connect(ui->digits, SIGNAL(valueChanged()), this, SLOT(showRestartWarning()));
243  connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning()));
244  connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning()));
245 
246  connect(ui->privateSendEnabled, &QCheckBox::clicked, [=](bool fChecked) {
247 #ifdef ENABLE_WALLET
248  privateSendClient.fEnablePrivateSend = fChecked;
249 #endif
250  updatePrivateSendVisibility();
251  if (_model != nullptr) {
252  _model->emitPrivateSendEnabledChanged();
253  }
254  updateWidth();
255  });
256 }
257 
259 {
260  /* Main */
261  mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
262 #ifndef Q_OS_MAC
263  mapper->addMapping(ui->hideTrayIcon, OptionsModel::HideTrayIcon);
264  mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
265  mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
266 #endif
267  mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
268  mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
269  mapper->addMapping(ui->privateSendEnabled, OptionsModel::PrivateSendEnabled);
270 
271  /* Wallet */
272  mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
273  mapper->addMapping(ui->showMasternodesTab, OptionsModel::ShowMasternodesTab);
274  mapper->addMapping(ui->showAdvancedPSUI, OptionsModel::ShowAdvancedPSUI);
275  mapper->addMapping(ui->showPrivateSendPopups, OptionsModel::ShowPrivateSendPopups);
276  mapper->addMapping(ui->lowKeysWarning, OptionsModel::LowKeysWarning);
277  mapper->addMapping(ui->privateSendMultiSession, OptionsModel::PrivateSendMultiSession);
278  mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
279  mapper->addMapping(ui->privateSendRounds, OptionsModel::PrivateSendRounds);
280  mapper->addMapping(ui->privateSendAmount, OptionsModel::PrivateSendAmount);
281 
282  /* Network */
283  mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
284  mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
285 
286  mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
287  mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
288  mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
289 
290  mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
291  mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
292  mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);
293 
294  /* Display */
295  mapper->addMapping(ui->digits, OptionsModel::Digits);
296  mapper->addMapping(ui->lang, OptionsModel::Language);
297  mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
298  mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
299 
300  /* Appearance
301  See AppearanceWidget::setModel
302  */
303 }
304 
305 void OptionsDialog::showPage(int index)
306 {
307  std::vector<QWidget*> vecNormal;
308  QAbstractButton* btnActive = pageButtons.button(index);
309  for (QAbstractButton* button : pageButtons.buttons()) {
310  if (button != btnActive) {
311  vecNormal.push_back(button);
312  }
313  }
314 
318 
319  ui->stackedWidgetOptions->setCurrentIndex(index);
320  btnActive->setChecked(true);
321 }
322 
324 {
325  ui->okButton->setEnabled(fState);
326 }
327 
329 {
330  if(model)
331  {
332  // confirmation dialog
333  QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
334  tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
335  QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
336 
337  if(btnRetVal == QMessageBox::Cancel)
338  return;
339 
340  /* reset all options and close GUI */
341  model->Reset();
343  QApplication::quit();
344  }
345 }
346 
348 {
349  mapper->submit();
350  appearance->accept();
351 #ifdef ENABLE_WALLET
352  privateSendClient.nCachedNumBlocks = std::numeric_limits<int>::max();
353  if(HasWallets())
354  GetWallets()[0]->MarkDirty();
355 #endif // ENABLE_WALLET
356  accept();
358 }
359 
361 {
362  reject();
363 }
364 
366 {
367  if(fState)
368  {
369  ui->minimizeToTray->setChecked(false);
370  ui->minimizeToTray->setEnabled(false);
371  }
372  else
373  {
374  ui->minimizeToTray->setEnabled(true);
375  }
376 }
377 
378 void OptionsDialog::showRestartWarning(bool fPersistent)
379 {
381 
382  if(fPersistent)
383  {
384  ui->statusLabel->setText(tr("Client restart required to activate changes."));
385  }
386  else
387  {
388  ui->statusLabel->setText(tr("This change would require a client restart."));
389  // clear non-persistent status label after 10 seconds
390  // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
391  QTimer::singleShot(10000, this, SLOT(clearStatusLabel()));
392  }
393 }
394 
396 {
397  ui->statusLabel->clear();
398  if (model && model->isRestartRequired()) {
399  showRestartWarning(true);
400  }
401 }
402 
404 {
405  QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
406  QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
407  if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
408  {
409  setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
411  }
412  else
413  {
414  setOkButtonState(false);
416  ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
417  }
418 }
419 
421 {
422  proxyType proxy;
423  std::string strProxy;
424  QString strDefaultProxyGUI;
425 
426  GetProxy(NET_IPV4, proxy);
427  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
428  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
429  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);
430 
431  GetProxy(NET_IPV6, proxy);
432  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
433  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
434  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);
435 
436  GetProxy(NET_TOR, proxy);
437  strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
438  strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
439  (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
440 }
441 
443 {
444 #ifdef ENABLE_WALLET
445  bool fEnabled = privateSendClient.fEnablePrivateSend;
446 #else
447  bool fEnabled = false;
448 #endif
449  ui->btnPrivateSend->setVisible(fEnabled);
450 }
451 
453 {
454  int nWidthWidestButton{0};
455  int nButtonsVisible{0};
456  for (QAbstractButton* button : pageButtons.buttons()) {
457  if (!button->isVisible()) {
458  continue;
459  }
460  QFontMetrics fm(button->font());
461  nWidthWidestButton = std::max<int>(nWidthWidestButton, fm.width(button->text()));
462  ++nButtonsVisible;
463  }
464  // Add 10 per button as padding and use minimum 585 which is what we used in css before
465  int nWidth = std::max<int>(585, (nWidthWidestButton + 10) * nButtonsVisible);
466  setMinimumWidth(nWidth);
467  resize(nWidth, height());
468 }
469 
470 void OptionsDialog::showEvent(QShowEvent* event)
471 {
472  if (!event->spontaneous()) {
473  updateWidth();
474  }
475  QDialog::showEvent(event);
476 }
477 
479 QValidator(parent)
480 {
481 }
482 
483 QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) const
484 {
485  Q_UNUSED(pos);
486  // Validate the proxy
487  CService serv(LookupNumeric(input.toStdString().c_str(), DEFAULT_GUI_PROXY_PORT));
488  proxyType addrProxy = proxyType(serv, true);
489  if (addrProxy.IsValid())
490  return QValidator::Acceptable;
491 
492  return QValidator::Invalid;
493 }
Ui::OptionsDialog * ui
Definition: optionsdialog.h:74
std::string ToStringPort() const
Definition: netaddress.cpp:567
bool fPrivateSendEnabledPrev
Definition: optionsdialog.h:80
OptionsDialog(QWidget *parent, bool enableWallet)
Dash unit definitions.
Definition: bitcoinunits.h:48
Proxy address widget validator, checks for a valid proxy address.
Definition: optionsdialog.h:26
void appearanceChanged()
void setFont(const std::vector< QWidget *> &vecWidgets, FontWeight weight, int nPointSize, bool fItalic)
Workaround to set correct font styles in all themes since there is a bug in macOS which leads to issu...
Definition: guiutil.cpp:1552
QButtonGroup pageButtons
Definition: optionsdialog.h:77
CService LookupNumeric(const char *pszName, int portDefault)
Definition: netbase.cpp:168
void setOkButtonState(bool fState)
const QString & getOverriddenByCommandLine()
Definition: optionsmodel.h:87
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:35
void on_resetButton_clicked()
Line edit that can be marked as "invalid" to show input validation feedback.
std::string ToStringIP(bool fUseGetnameinfo=true) const
Definition: netaddress.cpp:261
bool isRestartRequired() const
std::vector< CWallet * > GetWallets()
Definition: wallet.cpp:79
void updateFonts()
Update the font of all widgets where a custom font has been set with GUIUtil::setFont.
Definition: guiutil.cpp:1563
OptionsModel * model
Definition: optionsdialog.h:75
void showPage(int index)
custom tab buttons clicked
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:143
State validate(QString &input, int &pos) const
void setModel(OptionsModel *model)
QDataWidgetMapper * mapper
Definition: optionsdialog.h:76
void on_okButton_clicked()
void updateDefaultProxyNets()
void disableMacFocusRect(const QWidget *w)
Disable the OS default focus rect for macOS because we have custom focus rects set in the css files...
Definition: guiutil.cpp:1789
void on_hideTrayIcon_stateChanged(int fState)
void appearanceChanged()
CService proxy
Definition: netbase.h:37
bool IsValid() const
Definition: netbase.h:35
CPrivateSendClientManager privateSendClient
void setModel(OptionsModel *model)
Interface from Qt to configuration data structure for Bitcoin client.
Definition: optionsmodel.h:25
AppearanceWidget * appearance
Definition: optionsdialog.h:79
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:33
QString getThemedStyleQString(ThemedStyle style)
Helper to get css style strings which are injected into rich text through qt.
Definition: guiutil.cpp:210
void showRestartWarning(bool fPersistent=false)
void updatePrivateSendVisibility()
void on_cancelButton_clicked()
bool GetProxy(enum Network net, proxyType &proxyInfoOut)
Definition: netbase.cpp:556
Preferences dialog.
Definition: optionsdialog.h:37
void showEvent(QShowEvent *event) override
bool resetSettingsOnShutdown
Definition: optionsmodel.h:93
void clearStatusLabel()
int GetNumCores()
Return the number of physical cores available on the current system.
Definition: util.cpp:1355
void updateProxyValidationState()
bool HasWallets()
Definition: wallet.cpp:73
ProxyAddressValidator(QObject *parent)
static constexpr unsigned short DEFAULT_GUI_PROXY_PORT
Definition: optionsmodel.h:17
static const int MAX_SCRIPTCHECK_THREADS
Maximum number of script-checking threads allowed.
Definition: validation.h:83
Released under the MIT license