PIVX Core  5.6.99
P2P Digital Currency
sendcustomfeedialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 The PIVX 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 #include "sendcustomfeedialog.h"
6 #include "ui_sendcustomfeedialog.h"
7 
8 #include "guiutil.h"
9 #include "optionsmodel.h"
10 #include "qt/walletmodel.h"
11 #include "qtutils.h"
12 #include "wallet/fees.h"
13 
14 #include <QComboBox>
15 #include <QListView>
16 
18  FocusedDialog(parent),
19  ui(new Ui::SendCustomFeeDialog),
20  walletModel(model)
21 {
22  if (!walletModel)
23  throw std::runtime_error(strprintf("%s: No wallet model set", __func__));
24  ui->setupUi(this);
25 
26  // Stylesheet
27  this->setStyleSheet(parent->styleSheet());
28  setCssProperty(ui->frame, "container-dialog");
29 
30  // Text
31  setCssProperty(ui->labelTitle, "text-title-dialog");
32  setCssProperty(ui->labelMessage, "text-main-grey");
33 
34  // Recommended
35  setCssProperty(ui->labelFee, "text-main-grey-big");
36  setCssProperty(ui->comboBoxRecommended, "btn-combo-dialog");
37  ui->comboBoxRecommended->setView(new QListView());
38  ui->comboBoxRecommended->addItem(tr("Normal"), 5);
39  ui->comboBoxRecommended->addItem(tr("Slow"), 20);
40  ui->comboBoxRecommended->addItem(tr("Fast"), 1);
41 
42  // Custom
43  setCssProperty(ui->labelCustomFee, "label-subtitle-dialog");
44  initCssEditLine(ui->lineEditCustomFee, true);
45  GUIUtil::setupAmountWidget(ui->lineEditCustomFee, this);
46 
47  // Buttons
48  setCssProperty(ui->btnEsc, "ic-close");
49  setCssProperty(ui->btnCancel, "btn-dialog-cancel");
50  setCssBtnPrimary(ui->btnSave);
51 
52  connect(ui->btnEsc, &QPushButton::clicked, this, &SendCustomFeeDialog::close);
53  connect(ui->btnCancel, &QPushButton::clicked, this, &SendCustomFeeDialog::close);
54  connect(ui->btnSave, &QPushButton::clicked, this, &SendCustomFeeDialog::accept);
55  connect(ui->checkBoxCustom, &QCheckBox::clicked, this, &SendCustomFeeDialog::onCustomChecked);
56  connect(ui->checkBoxRecommended, &QCheckBox::clicked, this, &SendCustomFeeDialog::onRecommendedChecked);
57  connect(ui->comboBoxRecommended, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),
59  if (parent)
61  ui->checkBoxRecommended->setChecked(true);
62 }
63 
64 void SendCustomFeeDialog::showEvent(QShowEvent* event)
65 {
67  updateFee();
68 
69  ui->labelCustomFee->setText(BitcoinUnits::name(walletModel->getOptionsModel()->getDisplayUnit()) + "/kB");
70 
72  ui->checkBoxCustom->setChecked(true);
74  } else {
75  ui->checkBoxRecommended->setChecked(true);
77  }
78 }
79 
81 {
82  bool isChecked = ui->checkBoxCustom->checkState() == Qt::Checked;
83  ui->lineEditCustomFee->setEnabled(isChecked);
84  ui->comboBoxRecommended->setEnabled(!isChecked);
85  ui->checkBoxRecommended->setChecked(!isChecked);
86 
87  if (isChecked) {
88  CAmount nFee;
90  ui->lineEditCustomFee->setText(BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), nFee));
91  } else {
92  ui->lineEditCustomFee->clear();
93  }
94 }
95 
97 {
98  bool isChecked = ui->checkBoxRecommended->checkState() == Qt::Checked;
99  ui->lineEditCustomFee->setEnabled(!isChecked);
100  ui->comboBoxRecommended->setEnabled(isChecked);
101  ui->checkBoxCustom->setChecked(!isChecked);
102  if (isChecked) {
103  ui->lineEditCustomFee->clear();
104  }
105 }
106 
107 // Fast = 1.
108 // Medium = 5
109 // Slow = 20
111 {
112  if (!walletModel->getOptionsModel()) return;
113 
114  QVariant num = ui->comboBoxRecommended->currentData();
115  bool res = false;
116  int nBlocksToConfirm = num.toInt(&res);
117  if (res) {
118  feeRate = mempool.estimateFee(nBlocksToConfirm);
119  if (feeRate < CWallet::minTxFee) feeRate = CWallet::minTxFee; // not enough data => minfee
121  feeRate.GetFeePerK()) + "/kB");
122  }
123 }
124 
126 {
127  const bool fUseCustomFee = ui->checkBoxCustom->checkState() == Qt::Checked;
128  const CAmount customFee = getFeeRate().GetFeePerK();
129  // Check insane fee
130  const CAmount insaneFee = ::minRelayTxFee.GetFeePerK() * 10000;
131  if (customFee >= insaneFee) {
132  ui->lineEditCustomFee->setText(BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), insaneFee - walletModel->getNetMinFee()));
133  inform(tr("Fee too high. Must be below: %1").arg(
135  } else if (customFee < walletModel->getNetMinFee()) {
136  CAmount nFee = 0;
139  } else {
140  nFee = walletModel->getNetMinFee();
141  }
142  ui->lineEditCustomFee->setText(BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), nFee));
143  inform(tr("Fee too low. Must be at least: %1").arg(
145  } else {
146  walletModel->setWalletCustomFee(fUseCustomFee, customFee);
147  QDialog::accept();
148  }
149 }
150 
152 {
153  ui->comboBoxRecommended->setCurrentIndex(0);
154 }
155 
157 {
158  if (ui->checkBoxRecommended->isChecked()) {
159  return feeRate;
160  }
161 
162  // Parse custom value
163  auto value = GUIUtil::parseValue(ui->lineEditCustomFee->text(), walletModel->getOptionsModel()->getDisplayUnit());
164  if (value <= 0) {
165  inform(tr("Invalid custom fee amount"));
166  }
167  return CFeeRate(value);
168 }
169 
171 {
172  return ui->checkBoxCustom->checkState() == Qt::Checked;
173 }
174 
176 {
177  this->setStyleSheet(theme);
178  updateStyle(this);
179 }
180 
181 void SendCustomFeeDialog::inform(const QString& text)
182 {
183  if (!snackBar) snackBar = new SnackBar(nullptr, this);
184  snackBar->setText(text);
185  snackBar->resize(this->width(), snackBar->height());
186  openDialog(snackBar, this);
187 }
188 
190 {
191  delete ui;
192 }
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard, bool cleanRemainderZeros=true)
Format as string.
static QString formatWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string (with unit)
static QString name(int unit, bool isZpiv=false)
Short name.
Fee rate in PIV per kilobyte: CAmount / kB.
Definition: feerate.h:20
CAmount GetFeePerK() const
Definition: feerate.h:29
CFeeRate estimateFee(int nBlocks) const
Estimate fee rate needed to get into the next nBlocks.
Definition: txmempool.cpp:1200
static CFeeRate minTxFee
Fees smaller than this (in upiv) are considered zero fee (for transaction creation) We are ~100 times...
Definition: wallet.h:1129
void showEvent(QShowEvent *event)
int getDisplayUnit()
Definition: optionsmodel.h:74
PIVX GUI main class.
Definition: pivxgui.h:46
void themeChanged(bool isLightTheme, QString &theme)
void inform(const QString &text)
Ui::SendCustomFeeDialog * ui
void showEvent(QShowEvent *event) override
void onChangeTheme(bool isLightTheme, QString &theme)
SendCustomFeeDialog(PIVXGUI *parent, WalletModel *model)
void setText(const QString &text)
Definition: snackbar.cpp:51
Interface to PIVX wallet from Qt view code.
Definition: walletmodel.h:109
CAmount getNetMinFee()
void setWalletCustomFee(bool fUseCustomFee, const CAmount nFee=DEFAULT_TRANSACTION_FEE)
bool getWalletCustomFee(CAmount &nFeeRet)
OptionsModel * getOptionsModel()
bool hasWalletCustomFee()
void setupAmountWidget(QLineEdit *widget, QWidget *parent)
Definition: guiutil.cpp:141
bool isLightTheme()
Definition: qtutils.cpp:210
void initCssEditLine(QLineEdit *edit, bool isDialog)
Definition: qtutils.cpp:272
void setCssProperty(std::initializer_list< QWidget * > args, const QString &value)
Definition: qtutils.cpp:334
void setCssBtnPrimary(QPushButton *btn, bool forceUpdate)
Definition: qtutils.cpp:302
bool openDialog(QDialog *widget, QWidget *gui)
Definition: qtutils.cpp:23
void updateStyle(QWidget *widget)
Definition: qtutils.cpp:225
#define strprintf
Definition: tinyformat.h:1056
CTxMemPool mempool(::minRelayTxFee)
CFeeRate minRelayTxFee
Fees smaller than this (in upiv) are considered zero fee (for relaying, mining and transaction creati...
Definition: validation.cpp:108