Bitcoin Core  21.99.0
P2P Digital Currency
walletframe.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 #include <qt/walletframe.h>
6 
7 #include <qt/bitcoingui.h>
9 #include <qt/overviewpage.h>
10 #include <qt/walletcontroller.h>
11 #include <qt/walletmodel.h>
12 #include <qt/walletview.h>
13 
14 #include <cassert>
15 
16 #include <QGroupBox>
17 #include <QHBoxLayout>
18 #include <QLabel>
19 #include <QPushButton>
20 #include <QVBoxLayout>
21 
22 WalletFrame::WalletFrame(const PlatformStyle* _platformStyle, BitcoinGUI* _gui)
23  : QFrame(_gui),
24  gui(_gui),
25  platformStyle(_platformStyle),
26  m_size_hint(OverviewPage{platformStyle, nullptr}.sizeHint())
27 {
28  // Leave HBox hook for adding a list view later
29  QHBoxLayout *walletFrameLayout = new QHBoxLayout(this);
30  setContentsMargins(0,0,0,0);
31  walletStack = new QStackedWidget(this);
32  walletFrameLayout->setContentsMargins(0,0,0,0);
33  walletFrameLayout->addWidget(walletStack);
34 
35  // hbox for no wallet
36  QGroupBox* no_wallet_group = new QGroupBox(walletStack);
37  QVBoxLayout* no_wallet_layout = new QVBoxLayout(no_wallet_group);
38 
39  QLabel *noWallet = new QLabel(tr("No wallet has been loaded.\nGo to File > Open Wallet to load a wallet.\n- OR -"));
40  noWallet->setAlignment(Qt::AlignCenter);
41  no_wallet_layout->addWidget(noWallet, 0, Qt::AlignHCenter | Qt::AlignBottom);
42 
43  // A button for create wallet dialog
44  QPushButton* create_wallet_button = new QPushButton(tr("Create a new wallet"), walletStack);
45  connect(create_wallet_button, &QPushButton::clicked, [this] {
46  auto activity = new CreateWalletActivity(gui->getWalletController(), this);
47  connect(activity, &CreateWalletActivity::finished, activity, &QObject::deleteLater);
48  activity->create();
49  });
50  no_wallet_layout->addWidget(create_wallet_button, 0, Qt::AlignHCenter | Qt::AlignTop);
51  no_wallet_group->setLayout(no_wallet_layout);
52 
53  walletStack->addWidget(no_wallet_group);
54 }
55 
57 {
58 }
59 
61 {
62  this->clientModel = _clientModel;
63 
64  for (auto i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i) {
65  i.value()->setClientModel(_clientModel);
66  }
67 }
68 
70 {
71  if (!gui || !clientModel || !walletModel) return false;
72 
73  if (mapWalletViews.count(walletModel) > 0) return false;
74 
75  WalletView *walletView = new WalletView(platformStyle, this);
76  walletView->setClientModel(clientModel);
77  walletView->setWalletModel(walletModel);
78  walletView->showOutOfSyncWarning(bOutOfSync);
79  walletView->setPrivacy(gui->isPrivacyModeActivated());
80 
81  WalletView* current_wallet_view = currentWalletView();
82  if (current_wallet_view) {
83  walletView->setCurrentIndex(current_wallet_view->currentIndex());
84  } else {
85  walletView->gotoOverviewPage();
86  }
87 
88  walletStack->addWidget(walletView);
89  mapWalletViews[walletModel] = walletView;
90 
92  connect(walletView, &WalletView::transactionClicked, gui, &BitcoinGUI::gotoHistoryPage);
93  connect(walletView, &WalletView::coinsSent, gui, &BitcoinGUI::gotoHistoryPage);
94  connect(walletView, &WalletView::message, [this](const QString& title, const QString& message, unsigned int style) {
95  gui->message(title, message, style);
96  });
97  connect(walletView, &WalletView::encryptionStatusChanged, gui, &BitcoinGUI::updateWalletStatus);
98  connect(walletView, &WalletView::incomingTransaction, gui, &BitcoinGUI::incomingTransaction);
99  connect(walletView, &WalletView::hdEnabledStatusChanged, gui, &BitcoinGUI::updateWalletStatus);
100  connect(gui, &BitcoinGUI::setPrivacy, walletView, &WalletView::setPrivacy);
101 
102  return true;
103 }
104 
106 {
107  if (mapWalletViews.count(wallet_model) == 0) return;
108 
109  // Stop the effect of hidden widgets on the size hint of the shown one in QStackedWidget.
110  WalletView* view_about_to_hide = currentWalletView();
111  if (view_about_to_hide) {
112  QSizePolicy sp = view_about_to_hide->sizePolicy();
113  sp.setHorizontalPolicy(QSizePolicy::Ignored);
114  view_about_to_hide->setSizePolicy(sp);
115  }
116 
117  WalletView *walletView = mapWalletViews.value(wallet_model);
118  assert(walletView);
119 
120  // Set or restore the default QSizePolicy which could be set to QSizePolicy::Ignored previously.
121  QSizePolicy sp = walletView->sizePolicy();
122  sp.setHorizontalPolicy(QSizePolicy::Preferred);
123  walletView->setSizePolicy(sp);
124  walletView->updateGeometry();
125 
126  walletStack->setCurrentWidget(walletView);
127  walletView->updateEncryptionStatus();
128 }
129 
131 {
132  if (mapWalletViews.count(wallet_model) == 0) return;
133 
134  WalletView *walletView = mapWalletViews.take(wallet_model);
135  walletStack->removeWidget(walletView);
136  delete walletView;
137 }
138 
140 {
141  QMap<WalletModel*, WalletView*>::const_iterator i;
142  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
143  walletStack->removeWidget(i.value());
144  mapWalletViews.clear();
145 }
146 
148 {
149  WalletView *walletView = currentWalletView();
150  if (!walletView)
151  return false;
152 
153  return walletView->handlePaymentRequest(recipient);
154 }
155 
157 {
158  bOutOfSync = fShow;
159  QMap<WalletModel*, WalletView*>::const_iterator i;
160  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
161  i.value()->showOutOfSyncWarning(fShow);
162 }
163 
165 {
166  QMap<WalletModel*, WalletView*>::const_iterator i;
167  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
168  i.value()->gotoOverviewPage();
169 }
170 
172 {
173  QMap<WalletModel*, WalletView*>::const_iterator i;
174  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
175  i.value()->gotoHistoryPage();
176 }
177 
179 {
180  QMap<WalletModel*, WalletView*>::const_iterator i;
181  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
182  i.value()->gotoReceiveCoinsPage();
183 }
184 
186 {
187  QMap<WalletModel*, WalletView*>::const_iterator i;
188  for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
189  i.value()->gotoSendCoinsPage(addr);
190 }
191 
193 {
194  WalletView *walletView = currentWalletView();
195  if (walletView)
196  walletView->gotoSignMessageTab(addr);
197 }
198 
200 {
201  WalletView *walletView = currentWalletView();
202  if (walletView)
203  walletView->gotoVerifyMessageTab(addr);
204 }
205 
206 void WalletFrame::gotoLoadPSBT(bool from_clipboard)
207 {
208  WalletView *walletView = currentWalletView();
209  if (walletView) {
210  walletView->gotoLoadPSBT(from_clipboard);
211  }
212 }
213 
215 {
216  WalletView *walletView = currentWalletView();
217  if (walletView)
218  walletView->encryptWallet();
219 }
220 
222 {
223  WalletView *walletView = currentWalletView();
224  if (walletView)
225  walletView->backupWallet();
226 }
227 
229 {
230  WalletView *walletView = currentWalletView();
231  if (walletView)
232  walletView->changePassphrase();
233 }
234 
236 {
237  WalletView *walletView = currentWalletView();
238  if (walletView)
239  walletView->unlockWallet();
240 }
241 
243 {
244  WalletView *walletView = currentWalletView();
245  if (walletView)
246  walletView->usedSendingAddresses();
247 }
248 
250 {
251  WalletView *walletView = currentWalletView();
252  if (walletView)
253  walletView->usedReceivingAddresses();
254 }
255 
257 {
258  return qobject_cast<WalletView*>(walletStack->currentWidget());
259 }
260 
262 {
263  WalletView* wallet_view = currentWalletView();
264  return wallet_view ? wallet_view->getWalletModel() : nullptr;
265 }
266 
268 {
269  Q_EMIT requestedSyncWarningInfo();
270 }
WalletFrame::currentWalletView
WalletView * currentWalletView() const
Definition: walletframe.cpp:256
WalletFrame::gotoReceiveCoinsPage
void gotoReceiveCoinsPage()
Switch to receive coins page.
Definition: walletframe.cpp:178
WalletFrame::gotoSignMessageTab
void gotoSignMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to sign message tab.
Definition: walletframe.cpp:192
WalletFrame::unlockWallet
void unlockWallet()
Ask for passphrase to unlock wallet temporarily.
Definition: walletframe.cpp:235
WalletView
Definition: walletview.h:33
WalletFrame::requestedSyncWarningInfo
void requestedSyncWarningInfo()
Notify that the user has requested more information about the out-of-sync warning.
WalletModel
Interface to Bitcoin wallet from Qt view code.
Definition: walletmodel.h:51
WalletControllerActivity::finished
void finished()
WalletView::showOutOfSyncWarning
void showOutOfSyncWarning(bool fShow)
Definition: walletview.cpp:251
walletcontroller.h
walletmodel.h
WalletFrame::addWallet
bool addWallet(WalletModel *walletModel)
Definition: walletframe.cpp:69
WalletFrame::WalletFrame
WalletFrame(const PlatformStyle *platformStyle, BitcoinGUI *_gui=nullptr)
Definition: walletframe.cpp:22
WalletFrame::outOfSyncWarningClicked
void outOfSyncWarningClicked()
Pass on signal over requested out-of-sync-warning information.
Definition: walletframe.cpp:267
OverviewPage
Overview ("home") page widget.
Definition: overviewpage.h:28
WalletFrame::encryptWallet
void encryptWallet()
Encrypt the wallet.
Definition: walletframe.cpp:214
WalletFrame::platformStyle
const PlatformStyle * platformStyle
Definition: walletframe.h:62
WalletView::setPrivacy
void setPrivacy(bool privacy)
WalletFrame::changePassphrase
void changePassphrase()
Change encrypted wallet passphrase.
Definition: walletframe.cpp:228
WalletView::handlePaymentRequest
bool handlePaymentRequest(const SendCoinsRecipient &recipient)
Definition: walletview.cpp:246
BitcoinGUI::message
void message(const QString &title, QString message, unsigned int style, bool *ret=nullptr, const QString &detailed_message=QString())
Notify the user of an event from the core network or transaction handling code.
Definition: bitcoingui.cpp:1079
WalletView::gotoSignMessageTab
void gotoSignMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to sign message tab.
Definition: walletview.cpp:184
WalletView::encryptWallet
void encryptWallet()
Encrypt the wallet.
Definition: walletview.cpp:261
overviewpage.h
WalletView::incomingTransaction
void incomingTransaction(const QString &date, int unit, const CAmount &amount, const QString &type, const QString &address, const QString &label, const QString &walletName)
Notify that a new transaction appeared.
WalletFrame::backupWallet
void backupWallet()
Backup the wallet.
Definition: walletframe.cpp:221
BitcoinGUI::isPrivacyModeActivated
bool isPrivacyModeActivated() const
Definition: bitcoingui.cpp:1437
WalletView::hdEnabledStatusChanged
void hdEnabledStatusChanged()
HD-Enabled status of wallet changed (only possible during startup)
WalletFrame::bOutOfSync
bool bOutOfSync
Definition: walletframe.h:60
SendCoinsRecipient
Definition: sendcoinsrecipient.h:19
BitcoinGUI::setPrivacy
void setPrivacy(bool privacy)
WalletView::getWalletModel
WalletModel * getWalletModel()
Definition: walletview.h:45
WalletView::setClientModel
void setClientModel(ClientModel *clientModel)
Set the client model.
Definition: walletview.cpp:97
WalletFrame::setCurrentWallet
void setCurrentWallet(WalletModel *wallet_model)
Definition: walletframe.cpp:105
WalletFrame::~WalletFrame
~WalletFrame()
Definition: walletframe.cpp:56
WalletFrame::usedReceivingAddresses
void usedReceivingAddresses()
Show used receiving addresses.
Definition: walletframe.cpp:249
WalletView::updateEncryptionStatus
void updateEncryptionStatus()
Re-emit encryption status signal.
Definition: walletview.cpp:256
WalletView::message
void message(const QString &title, const QString &message, unsigned int style)
Fired when a message should be reported to the user.
createwalletdialog.h
WalletFrame::currentWalletModel
WalletModel * currentWalletModel() const
Definition: walletframe.cpp:261
WalletFrame::gui
BitcoinGUI * gui
Definition: walletframe.h:56
WalletView::encryptionStatusChanged
void encryptionStatusChanged()
Encryption status of wallet changed.
WalletView::gotoVerifyMessageTab
void gotoVerifyMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to verify message tab.
Definition: walletview.cpp:196
CreateWalletActivity
Definition: walletcontroller.h:114
walletview.h
WalletFrame::clientModel
ClientModel * clientModel
Definition: walletframe.h:57
WalletFrame::usedSendingAddresses
void usedSendingAddresses()
Show used sending addresses.
Definition: walletframe.cpp:242
WalletFrame::gotoSendCoinsPage
void gotoSendCoinsPage(QString addr="")
Switch to send coins page.
Definition: walletframe.cpp:185
WalletFrame::gotoLoadPSBT
void gotoLoadPSBT(bool from_clipboard=false)
Load Partially Signed Bitcoin Transaction.
Definition: walletframe.cpp:206
ClientModel
Model for Bitcoin network client.
Definition: clientmodel.h:46
WalletFrame::handlePaymentRequest
bool handlePaymentRequest(const SendCoinsRecipient &recipient)
Definition: walletframe.cpp:147
WalletView::outOfSyncWarningClicked
void outOfSyncWarningClicked()
Notify that the out of sync warning icon has been pressed.
walletframe.h
WalletFrame::setClientModel
void setClientModel(ClientModel *clientModel)
Definition: walletframe.cpp:60
WalletView::coinsSent
void coinsSent()
WalletView::gotoOverviewPage
void gotoOverviewPage()
Switch to overview (home) page.
Definition: walletview.cpp:161
WalletView::setWalletModel
void setWalletModel(WalletModel *walletModel)
Set the wallet model.
Definition: walletview.cpp:106
WalletFrame::gotoVerifyMessageTab
void gotoVerifyMessageTab(QString addr="")
Show Sign/Verify Message dialog and switch to verify message tab.
Definition: walletframe.cpp:199
WalletView::changePassphrase
void changePassphrase()
Change encrypted wallet passphrase.
Definition: walletview.cpp:291
WalletFrame::gotoOverviewPage
void gotoOverviewPage()
Switch to overview (home) page.
Definition: walletframe.cpp:164
WalletFrame::walletStack
QStackedWidget * walletStack
Definition: walletframe.h:55
WalletView::unlockWallet
void unlockWallet()
Ask for passphrase to unlock wallet temporarily.
Definition: walletview.cpp:298
WalletView::gotoLoadPSBT
void gotoLoadPSBT(bool from_clipboard=false)
Load Partially Signed Bitcoin Transaction.
Definition: walletview.cpp:208
bitcoingui.h
WalletView::backupWallet
void backupWallet()
Backup the wallet.
Definition: walletview.cpp:272
WalletFrame::removeAllWallets
void removeAllWallets()
Definition: walletframe.cpp:139
WalletView::usedSendingAddresses
void usedSendingAddresses()
Show used sending addresses.
Definition: walletview.cpp:311
WalletView::usedReceivingAddresses
void usedReceivingAddresses()
Show used receiving addresses.
Definition: walletview.cpp:319
WalletFrame::showOutOfSyncWarning
void showOutOfSyncWarning(bool fShow)
Definition: walletframe.cpp:156
PlatformStyle
Definition: platformstyle.h:13
WalletView::transactionClicked
void transactionClicked()
BitcoinGUI
Bitcoin GUI main class.
Definition: bitcoingui.h:67
WalletFrame::gotoHistoryPage
void gotoHistoryPage()
Switch to history (transactions) page.
Definition: walletframe.cpp:171
WalletFrame::mapWalletViews
QMap< WalletModel *, WalletView * > mapWalletViews
Definition: walletframe.h:58
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
WalletFrame::removeWallet
void removeWallet(WalletModel *wallet_model)
Definition: walletframe.cpp:130