-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathdelegationitemwidget.cpp
294 lines (261 loc) · 8.65 KB
/
delegationitemwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#if defined(HAVE_CONFIG_H)
#include <config/bitcoin-config.h>
#endif
#include <qt/delegationitemwidget.h>
#include <qt/platformstyle.h>
#include <qt/forms/ui_delegationitemwidget.h>
#include <qt/bitcoinunits.h>
#include <qt/optionsmodel.h>
#include <qt/walletmodel.h>
#include <qt/clientmodel.h>
#include <qt/delegationitemmodel.h>
#include <interfaces/node.h>
#include <chainparams.h>
#include <qt/guiutil.h>
#include <QFile>
class DelegationItemWidgetPriv
{
public:
QString fee;
QString staker;
QString address;
int32_t blockHight = -1;
int64_t balance = 0;
int64_t stake = 0;
int64_t weight = 0;
bool staking = false;
int32_t status = 0;
QLabel* light = 0;
};
#define DELEGATION_ITEM_ICONSIZE 24
#define LIGHT_ICONSIZE 14
const QString LIGHT_STYLE = "QLabel{background-color: %1; border-radius: 7px; border: 2px solid transparent;}";
#define DELEGATION_STAKER_SIZE 210
DelegationItemWidget::DelegationItemWidget(const PlatformStyle *platformStyle, QWidget *parent, ItemType type) :
QWidget(parent),
ui(new Ui::DelegationItemWidget),
m_platfromStyle(platformStyle),
m_type(type),
m_position(-1),
m_model(0),
m_clientModel(0)
{
ui->setupUi(this);
ui->stackedWidget->setCurrentIndex(type);
ui->buttonSplit->setIcon(platformStyle->MultiStatesIcon(":/icons/split", PlatformStyle::PushButtonIcon));
ui->buttonRemove->setIcon(platformStyle->MultiStatesIcon(":/icons/remove_entry", PlatformStyle::PushButtonIcon));
ui->buttonAdd->setIcon(platformStyle->MultiStatesIcon(":/icons/plus_full", PlatformStyle::PushButtonIcon));
ui->buttonRestore->setIcon(platformStyle->MultiStatesIcon(":/icons/restore", PlatformStyle::PushButtonIcon));
ui->delegationLogo->setPixmap(platformStyle->MultiStatesIcon(m_type == New ? ":/icons/delegate" : ":/icons/staking_off").pixmap(DELEGATION_ITEM_ICONSIZE, DELEGATION_ITEM_ICONSIZE));
ui->buttonSplit->setToolTip(tr("Split coins for offline staking"));
ui->buttonRemove->setToolTip(tr("Remove delegation"));
ui->buttonAdd->setToolTip(tr("Add delegation"));
ui->buttonRestore->setToolTip(tr("Restore delegations"));
d = new DelegationItemWidgetPriv();
if(m_type == Record)
{
d->light = new QLabel();
d->light->setFixedSize(LIGHT_ICONSIZE, LIGHT_ICONSIZE);
d->light->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QVBoxLayout *layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(d->light, 0, Qt::AlignRight|Qt::AlignBottom);
ui->delegationLogo->setLayout(layout);
setLight(Transparent);
}
}
DelegationItemWidget::~DelegationItemWidget()
{
delete ui;
delete d;
}
void DelegationItemWidget::setData(const QString &fee, const QString &staker, const QString &address, const int32_t &blockHight, const int64_t &balance, const int64_t &stake, const int64_t &weight, const int32_t &status)
{
// Set data
d->fee = fee;
d->staker = staker;
d->address = address;
d->blockHight = blockHight;
d->balance = balance;
d->stake = stake;
d->weight = weight;
d->status = status;
// Update GUI
if(d->fee != ui->labelFee->text())
ui->labelFee->setText(d->fee);
if(d->staker != ui->labelStaker->toolTip())
updateLabelStaker();
if(d->address != ui->labelAddress->text())
ui->labelAddress->setText(d->address);
d->staking = (d->blockHight > 0 && d->weight > 0);
updateLogo();
updateBalance();
}
void DelegationItemWidget::setPosition(int position)
{
m_position = position;
}
void DelegationItemWidget::on_buttonAdd_clicked()
{
Q_EMIT clicked(m_position, Buttons::Add);
}
void DelegationItemWidget::on_buttonRemove_clicked()
{
Q_EMIT clicked(m_position, Buttons::Remove);
}
void DelegationItemWidget::on_buttonSplit_clicked()
{
Q_EMIT clicked(m_position, Buttons::Split);
}
void DelegationItemWidget::on_buttonRestore_clicked()
{
Q_EMIT clicked(m_position, Buttons::Restore);
}
int DelegationItemWidget::position() const
{
return m_position;
}
void DelegationItemWidget::updateLogo()
{
if(!m_model || !m_clientModel)
return;
if(m_model->node().shutdownRequested())
return;
QString filename = d->staking ? ":/icons/staking_on" : ":/icons/staking_off";
if(m_filename != filename)
{
m_filename = filename;
QPixmap pixmap = m_platfromStyle->MultiStatesIcon(m_filename).pixmap(DELEGATION_ITEM_ICONSIZE, DELEGATION_ITEM_ICONSIZE);
ui->delegationLogo->setPixmap(pixmap);
}
uint64_t nWeight = d->weight;
if (d->staking)
{
uint64_t nNetworkWeight = m_model->node().getPoSKernelPS();
int headersTipHeight = m_clientModel->getHeaderTipHeight();
int64_t nTargetSpacing = Params().GetConsensus().TargetSpacing(headersTipHeight);
unsigned nEstimateTime = nTargetSpacing * nNetworkWeight / nWeight;
QString text;
if (nEstimateTime < 60)
{
text = tr("%n second(s)", "", nEstimateTime);
}
else if (nEstimateTime < 60*60)
{
text = tr("%n minute(s)", "", nEstimateTime/60);
}
else if (nEstimateTime < 24*60*60)
{
text = tr("%n hour(s)", "", nEstimateTime/(60*60));
}
else
{
text = tr("%n day(s)", "", nEstimateTime/(60*60*24));
}
nWeight /= COIN;
nNetworkWeight /= COIN;
ui->delegationLogo->setToolTip(tr("Confirmed delegation<br>Your weight is %1<br>Network weight is %2<br>Expected time to earn reward is %3").arg(nWeight).arg(nNetworkWeight).arg(text));
}
else
{
if(d->blockHight < 0)
ui->delegationLogo->setToolTip(tr("Not staking because the delegation is not confirmed"));
else if (!nWeight)
ui->delegationLogo->setToolTip(tr("Not staking because you don't have mature coins"));
else
ui->delegationLogo->setToolTip(tr("Not staking"));
}
switch (d->status)
{
case DelegationItemModel::CreateTxConfirmed:
setLight(Green);
d->light->setToolTip(tr("Create transaction confirmed"));
break;
case DelegationItemModel::CreateTxNotConfirmed:
setLight(Orange);
d->light->setToolTip(tr("Create transaction not confirmed"));
break;
case DelegationItemModel::CreateTxError:
setLight(Red);
d->light->setToolTip(tr("Create transaction error"));
break;
case DelegationItemModel::RemoveTxNotConfirmed:
setLight(Orange);
d->light->setToolTip(tr("Remove transaction not confirmed"));
break;
case DelegationItemModel::RemoveTxError:
setLight(Red);
d->light->setToolTip(tr("Remove transaction error"));
break;
default:
setLight(Transparent);
break;
}
}
void DelegationItemWidget::setModel(WalletModel *_model)
{
m_model = _model;
if(m_model && m_model->getOptionsModel())
{
connect(m_model->getOptionsModel(), &OptionsModel::displayUnitChanged, this, &DelegationItemWidget::updateDisplayUnit);
}
updateDisplayUnit();
}
void DelegationItemWidget::setClientModel(ClientModel *_clientModel)
{
m_clientModel = _clientModel;
}
void DelegationItemWidget::updateDisplayUnit()
{
updateBalance();
}
void DelegationItemWidget::updateBalance()
{
BitcoinUnit unit = BitcoinUnit::BTC;
if(m_model && m_model->getOptionsModel())
unit = m_model->getOptionsModel()->getDisplayUnit();
ui->labelAssets->setText(BitcoinUnits::formatWithUnit(unit, d->balance, false, BitcoinUnits::SeparatorStyle::ALWAYS));
ui->labelStake->setText(BitcoinUnits::formatWithUnit(unit, d->stake, false, BitcoinUnits::SeparatorStyle::ALWAYS));
}
void DelegationItemWidget::setLight(DelegationItemWidget::LightType type)
{
QString lightColor;
switch (type) {
case Red:
lightColor = "#DC143C";
break;
case Orange:
lightColor = "#FFA500";
break;
case Green:
lightColor = "#32CD32";
break;
default:
break;
}
if(d && d->light)
{
if(lightColor != "")
{
d->light->setStyleSheet(LIGHT_STYLE.arg(lightColor));
d->light->setVisible(true);
}
else
{
d->light->setVisible(false);
}
}
}
void DelegationItemWidget::updateLabelStaker()
{
QString text = d->staker;
QFontMetrics fm = ui->labelStaker->fontMetrics();
for(int i = d->staker.length(); i>3; i--)
{
text = GUIUtil::cutString(d->staker, i);
if(GUIUtil::TextWidth(fm, text) < DELEGATION_STAKER_SIZE)
break;
}
ui->labelStaker->setText(text);
ui->labelStaker->setToolTip(d->staker);
}